Remittances need files the banks accept and statements need readers for reconciliation, without external libraries: - SepaXmlWriter writes pain.001.001.09 credit transfers and pain.008.001.08 direct debits (one PmtInf per sequence and collection date), reducing texts to the SEPA Latin set and refusing what a bank would reject. - Camt053Reader reads any camt.053.001.xx by local names; CsvStatementReader reads home banking exports (UTF-8 or Windows-1252, Italian or English numbers) and guesses the layout from the header row. - The Italian pack gains RibaCbiWriter (CBI Ricevute bancarie, 120-character records IB/14/20/30/40/50/51/70/EF) and CbiStatementReader (RH/61-64/EF). - Tests validate the SEPA files against hand-written subsets of the ISO 20022 schemas, to be replaced by the official XSDs when available. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b12dc79317
commit
5fbbe4d3a4
14 files changed
+3467
No files matched your search
@@ -0,0 +1,147 @@
|
||||
using System.Text;
|
||||
using Khaemhat.Core.Treasury;
|
||||
|
||||
namespace Khaemhat.Fiscal.Italy.Tests;
|
||||
|
||||
/// <summary>The CBI current account statement (issue #71): balances, movements and their description lines.</summary>
|
||||
[TestClass]
|
||||
public sealed class CbiStatementReaderTests
|
||||
{
|
||||
private static readonly CbiStatementReader Reader = new();
|
||||
|
||||
[TestMethod]
|
||||
public void Read_SampleStatement_WithBalancesMovementsAndDescriptions()
|
||||
{
|
||||
var statement = Read(string.Join("\r\n", Sample()));
|
||||
|
||||
Assert.AreEqual(StatementFormat.Cbi, Reader.Format);
|
||||
Assert.AreEqual(StatementFormat.Cbi, statement.Format);
|
||||
Assert.AreEqual("IT60X0542811101000000123456", statement.AccountIban);
|
||||
Assert.AreEqual("EUR", statement.Currency);
|
||||
Assert.AreEqual(1000m, statement.OpeningBalance);
|
||||
Assert.AreEqual(2207.50m, statement.ClosingBalance);
|
||||
Assert.AreEqual(new DateOnly(2026, 10, 31), statement.FromDate);
|
||||
Assert.AreEqual(new DateOnly(2026, 11, 3), statement.ToDate);
|
||||
Assert.HasCount(3, statement.Movements);
|
||||
Assert.AreEqual(statement.ClosingBalance, statement.OpeningBalance + statement.Movements.Sum(m => m.Amount));
|
||||
|
||||
var first = statement.Movements[0];
|
||||
Assert.AreEqual(new DateOnly(2026, 11, 2), first.BookingDate);
|
||||
Assert.AreEqual(new DateOnly(2026, 11, 3), first.ValueDate);
|
||||
Assert.AreEqual(1220.50m, first.Amount);
|
||||
Assert.AreEqual("48", first.BankCode);
|
||||
Assert.AreEqual("CRO123456", first.Reference);
|
||||
Assert.AreEqual("BONIFICO A VOSTRO FAVORE DA CLIENTE UNO SPA SALDO FT 388", first.Description);
|
||||
|
||||
var second = statement.Movements[1];
|
||||
Assert.AreEqual(-12.50m, second.Amount);
|
||||
Assert.AreEqual("COMMISSIONI", second.Description);
|
||||
Assert.IsNull(second.Reference);
|
||||
|
||||
// The amount with the comma as some banks write it.
|
||||
Assert.AreEqual(-0.50m, statement.Movements[2].Amount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Read_LinesWithoutLeadingSpace_AndWithoutLineBreaks()
|
||||
{
|
||||
var trimmed = string.Join("\n", Sample().Select(l => l[1..]));
|
||||
Assert.HasCount(3, Read(trimmed).Movements);
|
||||
|
||||
var stream = string.Concat(Sample());
|
||||
var statement = Read(stream);
|
||||
Assert.HasCount(3, statement.Movements);
|
||||
Assert.AreEqual(2207.50m, statement.ClosingBalance);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Recognizes_TheRhHeader_WithOrWithoutLeadingSpace()
|
||||
{
|
||||
var lines = Sample();
|
||||
Assert.IsTrue(Reader.Recognizes(Encoding.ASCII.GetBytes(string.Join("\r\n", lines))));
|
||||
Assert.IsTrue(Reader.Recognizes(Encoding.ASCII.GetBytes("\r\n" + string.Join("\r\n", lines.Select(l => l[1..])))));
|
||||
Assert.IsTrue(Reader.Recognizes(Encoding.ASCII.GetBytes(string.Concat(lines))));
|
||||
Assert.IsFalse(Reader.Recognizes(Encoding.ASCII.GetBytes(" RH short\r\n")));
|
||||
Assert.IsFalse(Reader.Recognizes(Encoding.ASCII.GetBytes("Data;Descrizione;Importo\r\n")));
|
||||
Assert.IsFalse(Reader.Recognizes(Encoding.ASCII.GetBytes(" IB" + new string(' ', 117))));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Read_Errors_AreBankFileExceptions()
|
||||
{
|
||||
var lines = Sample();
|
||||
Assert.ThrowsExactly<BankFileException>(() => Read(string.Join("\r\n", lines.Skip(1))));
|
||||
|
||||
var badSign = lines.ToArray();
|
||||
badSign[2] = Put(badSign[2], 26, "X");
|
||||
Assert.ThrowsExactly<BankFileException>(() => Read(string.Join("\r\n", badSign)));
|
||||
|
||||
var badAmount = lines.ToArray();
|
||||
badAmount[2] = Put(badAmount[2], 27, "00000ABC0000000");
|
||||
Assert.ThrowsExactly<BankFileException>(() => Read(string.Join("\r\n", badAmount)));
|
||||
|
||||
var badDate = lines.ToArray();
|
||||
badDate[2] = Put(badDate[2], 20, "321326");
|
||||
Assert.ThrowsExactly<BankFileException>(() => Read(string.Join("\r\n", badDate)));
|
||||
|
||||
var orphan = lines.ToList();
|
||||
orphan.Insert(2, Record("63", (4, "0000001"), (11, "001"), (14, "ORFANA")));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Read(string.Join("\r\n", orphan)));
|
||||
}
|
||||
|
||||
private static List<string> Sample() =>
|
||||
[
|
||||
Record("RH", (4, "05428"), (9, "A1B2C"), (14, "041126"), (20, "ESTRATTO-2026-11"), (114, "E")),
|
||||
Record(
|
||||
"61",
|
||||
(4, "0000001"),
|
||||
(52, "X"),
|
||||
(53, "05428"),
|
||||
(58, "11101"),
|
||||
(63, "000000123456"),
|
||||
(75, "EUR"),
|
||||
(78, "311026"),
|
||||
(84, "C"),
|
||||
(85, "000000000100000"),
|
||||
(100, "IT"),
|
||||
(102, "60")),
|
||||
Record(
|
||||
"62",
|
||||
(4, "0000001"),
|
||||
(11, "001"),
|
||||
(14, "031126"),
|
||||
(20, "021126"),
|
||||
(26, "C"),
|
||||
(27, "000000000122050"),
|
||||
(42, "48"),
|
||||
(44, "00"),
|
||||
(62, "CRO123456"),
|
||||
(87, "BONIFICO A VOSTRO FAVORE")),
|
||||
Record("63", (4, "0000001"), (11, "001"), (14, "DA CLIENTE UNO SPA")),
|
||||
Record("63", (4, "0000001"), (11, "001"), (14, "SALDO FT 388")),
|
||||
Record("62", (4, "0000001"), (11, "002"), (14, "031126"), (20, "031126"), (26, "D"), (27, "000000000001250"), (42, "26"), (87, "COMMISSIONI")),
|
||||
Record("62", (4, "0000001"), (11, "003"), (14, "031126"), (20, "031126"), (26, "D"), (27, " 0,50"), (42, "26"), (87, "BOLLO")),
|
||||
Record("64", (4, "0000001"), (11, "EUR"), (14, "031126"), (20, "C"), (21, "000000000220750"), (36, "C"), (37, "000000000220750")),
|
||||
Record("EF", (4, "05428"), (9, "A1B2C"), (14, "041126"), (20, "ESTRATTO-2026-11"), (46, "0000001"), (83, "0000009"), (114, "E")),
|
||||
];
|
||||
|
||||
private static string Record(string type, params (int Position, string Value)[] fields)
|
||||
{
|
||||
var line = " " + type + new string(' ', 117);
|
||||
foreach (var (position, value) in fields)
|
||||
{
|
||||
line = Put(line, position, value);
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
private static string Put(string line, int position, string value) =>
|
||||
line[..(position - 1)] + value + line[(position - 1 + value.Length)..];
|
||||
|
||||
private static StatementFile Read(string text)
|
||||
{
|
||||
using var stream = new MemoryStream(Encoding.Latin1.GetBytes(text));
|
||||
return Reader.Read(stream);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
using System.Text;
|
||||
using Khaemhat.Core.Treasury;
|
||||
using Khaemhat.Fiscal.Abstractions;
|
||||
|
||||
namespace Khaemhat.Fiscal.Italy.Tests;
|
||||
|
||||
/// <summary>Bank receipts in the CBI fixed-width layout (issue #70): record lengths, order, positions and totals.</summary>
|
||||
[TestClass]
|
||||
public sealed class RibaCbiWriterTests
|
||||
{
|
||||
private static readonly RibaCbiWriter Writer = new();
|
||||
|
||||
private static readonly BankFileCompany Company = new(
|
||||
"Officine Bàlbo S.r.l.",
|
||||
"01234567890",
|
||||
"Via Roma 1",
|
||||
"10121",
|
||||
"Torino",
|
||||
"TO",
|
||||
"IT",
|
||||
"IT60X0542811101000000123456",
|
||||
null,
|
||||
"a1b2c",
|
||||
null,
|
||||
null);
|
||||
|
||||
[TestMethod]
|
||||
public void Supports_OnlyRiba()
|
||||
{
|
||||
Assert.IsTrue(Writer.Supports(RemittanceKind.Riba));
|
||||
Assert.IsFalse(Writer.Supports(RemittanceKind.SddCore));
|
||||
Assert.IsFalse(Writer.Supports(RemittanceKind.SepaCreditTransfer));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Write_EveryRecordIs120Characters_InTheCbiOrder()
|
||||
{
|
||||
var file = Writer.Write(Remittance());
|
||||
|
||||
Assert.AreEqual("RIBA_RB-2026-11.txt", file.FileName);
|
||||
Assert.AreEqual("text/plain", file.ContentType);
|
||||
var text = Encoding.ASCII.GetString(file.Content);
|
||||
Assert.IsTrue(text.EndsWith("\r\n", StringComparison.Ordinal));
|
||||
Assert.IsTrue(file.Content.All(b => b < 128));
|
||||
var lines = Lines(file);
|
||||
Assert.IsTrue(lines.All(l => l.Length == 120), string.Join("\n", lines.Where(l => l.Length != 120)));
|
||||
CollectionAssert.AreEqual(
|
||||
new[] { "IB", "14", "20", "30", "40", "50", "51", "70", "14", "20", "30", "40", "50", "51", "70", "EF" },
|
||||
lines.Select(l => l[1..3]).ToArray());
|
||||
Assert.IsTrue(lines.All(l => l[0] == ' '));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Write_HeaderAndReceipt_HaveTheValuesAtTheirPositions()
|
||||
{
|
||||
var lines = Lines(Writer.Write(Remittance()));
|
||||
|
||||
var header = lines[0];
|
||||
Assert.AreEqual("A1B2C", At(header, 4, 8));
|
||||
Assert.AreEqual("05428", At(header, 9, 13));
|
||||
Assert.AreEqual("131126", At(header, 14, 19));
|
||||
Assert.AreEqual("RB-2026-11 ", At(header, 20, 39));
|
||||
Assert.AreEqual("E", At(header, 114, 114));
|
||||
|
||||
var receipt = lines[1];
|
||||
Assert.AreEqual("0000001", At(receipt, 4, 10));
|
||||
Assert.AreEqual("311226", At(receipt, 23, 28));
|
||||
Assert.AreEqual("30000", At(receipt, 29, 33));
|
||||
Assert.AreEqual("0000000122050", At(receipt, 34, 46));
|
||||
Assert.AreEqual("-", At(receipt, 47, 47));
|
||||
Assert.AreEqual("05428", At(receipt, 48, 52));
|
||||
Assert.AreEqual("11101", At(receipt, 53, 57));
|
||||
Assert.AreEqual("000000123456", At(receipt, 58, 69));
|
||||
Assert.AreEqual("02008", At(receipt, 70, 74));
|
||||
Assert.AreEqual("01600", At(receipt, 75, 79));
|
||||
Assert.AreEqual("A1B2C", At(receipt, 92, 96));
|
||||
Assert.AreEqual("4", At(receipt, 97, 97));
|
||||
Assert.AreEqual("C0001 ", At(receipt, 98, 113));
|
||||
Assert.AreEqual("E", At(receipt, 120, 120));
|
||||
|
||||
var creditor = lines[2];
|
||||
Assert.AreEqual("OFFICINE BALBO S.R.L. ", At(creditor, 11, 34));
|
||||
Assert.AreEqual("VIA ROMA 1", At(creditor, 35, 58).TrimEnd());
|
||||
Assert.AreEqual("10121 TORINO TO", At(creditor, 59, 82).TrimEnd());
|
||||
Assert.AreEqual("01234567890", At(creditor, 83, 106).TrimEnd());
|
||||
|
||||
var debtor = lines[3];
|
||||
Assert.AreEqual("CITTA DI MONDOVI SOCIETA COOPE", At(debtor, 11, 40));
|
||||
Assert.AreEqual("RATIVA", At(debtor, 41, 70).TrimEnd());
|
||||
Assert.AreEqual("RSSMRA80A01H501U", At(debtor, 71, 86));
|
||||
|
||||
var address = lines[4];
|
||||
Assert.AreEqual("PIAZZA MAGGIORE 3", At(address, 11, 40).TrimEnd());
|
||||
Assert.AreEqual("12084", At(address, 41, 45));
|
||||
Assert.AreEqual("MONDOVI", At(address, 46, 67).TrimEnd());
|
||||
Assert.AreEqual("CN", At(address, 69, 70));
|
||||
Assert.AreEqual("UNICREDIT AG. 2", At(address, 71, 120).TrimEnd());
|
||||
|
||||
var reference = lines[5];
|
||||
Assert.AreEqual("FT 2026/000388 DEL 31/08/2026", At(reference, 11, 50).TrimEnd());
|
||||
Assert.AreEqual("01234567890", At(reference, 101, 116).TrimEnd());
|
||||
|
||||
var number = lines[6];
|
||||
Assert.AreEqual("0000000007", At(number, 11, 20));
|
||||
Assert.AreEqual("OFFICINE BALBO S.R.L", At(number, 21, 40));
|
||||
|
||||
Assert.AreEqual("0000001", At(lines[7], 4, 10));
|
||||
Assert.AreEqual(string.Empty, At(lines[7], 11, 120).Trim());
|
||||
Assert.AreEqual("0000002", At(lines[8], 4, 10));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Write_Footer_CountsReceiptsRecordsAndTotal()
|
||||
{
|
||||
var footer = Lines(Writer.Write(Remittance()))[^1];
|
||||
|
||||
Assert.AreEqual("EF", At(footer, 2, 3));
|
||||
Assert.AreEqual("A1B2C", At(footer, 4, 8));
|
||||
Assert.AreEqual("05428", At(footer, 9, 13));
|
||||
Assert.AreEqual("131126", At(footer, 14, 19));
|
||||
Assert.AreEqual("RB-2026-11", At(footer, 20, 39).TrimEnd());
|
||||
Assert.AreEqual("0000002", At(footer, 46, 52));
|
||||
Assert.AreEqual("000000000132050", At(footer, 53, 67));
|
||||
Assert.AreEqual("000000000000000", At(footer, 68, 82));
|
||||
Assert.AreEqual("0000016", At(footer, 83, 89));
|
||||
Assert.AreEqual("E", At(footer, 114, 114));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Write_Errors_AreBankFileExceptions()
|
||||
{
|
||||
var remittance = Remittance();
|
||||
var item = remittance.Items[0];
|
||||
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Company = Company with { SiaCode = null } }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Company = Company with { SiaCode = "123" } }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Company = Company with { Iban = "DE89370400440532013000" } }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Company = Company with { Iban = "IT61X0542811101000000123456" } }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Items = [item with { Iban = null }] }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Items = [item with { Iban = "DE89370400440532013000" }] }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Items = [item with { Amount = 0m }] }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Items = [item with { Amount = 100_000_000_000m }] }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Items = [] }));
|
||||
Assert.ThrowsExactly<BankFileException>(() => Writer.Write(remittance with { Kind = RemittanceKind.SddCore }));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Text_IsUpperCaseAsciiPaddedOrCut()
|
||||
{
|
||||
Assert.AreEqual("PERCHE CITTA ", RibaCbiWriter.Text("perché città", 14));
|
||||
Assert.AreEqual("ABC", RibaCbiWriter.Text("abcdef", 3));
|
||||
Assert.AreEqual("A B ", RibaCbiWriter.Text("a€b", 4));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pack_ExposesTheRemittanceWriterAndTheStatementReader()
|
||||
{
|
||||
var pack = new ItalyFiscalPack();
|
||||
|
||||
Assert.IsTrue(pack.Require<IRemittanceFileWriter>().Supports(RemittanceKind.Riba));
|
||||
Assert.AreEqual(StatementFormat.Cbi, pack.Require<IBankStatementReader>().Format);
|
||||
}
|
||||
|
||||
private static BankFileRemittance Remittance() => new(
|
||||
RemittanceKind.Riba,
|
||||
"RB-2026-11",
|
||||
new DateTime(2026, 11, 13, 9, 0, 0, DateTimeKind.Local),
|
||||
new DateOnly(2026, 11, 13),
|
||||
Company,
|
||||
[
|
||||
new BankFileItem(
|
||||
7, "C0001", "Città di Mondovì Società Cooperativa", "RSSMRA80A01H501U", "Piazza Maggiore 3", "12084", "Mondovì", "CN", "IT",
|
||||
"IT63A0200801600000101234567", null, "UniCredit Ag. 2", 1220.50m, new DateOnly(2026, 12, 31), "FT 2026/000388 del 31/08/2026", "E1"),
|
||||
new BankFileItem(
|
||||
8, "C0002", "Bianchi Snc", "01234567897", "Via Po 2", "20121", "Milano", "MI", "IT",
|
||||
"IT11B0503401633000000012345", null, "Banco BPM", 100m, new DateOnly(2027, 1, 31), "FT 2026/000401", "E2"),
|
||||
]);
|
||||
|
||||
private static string[] Lines(BankFile file) =>
|
||||
Encoding.ASCII.GetString(file.Content).Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
private static string At(string line, int from, int to) => line[(from - 1)..to];
|
||||
}
|
||||
Reference in new issue
Block a user