feat: bank file contracts for remittances and statements (#70 #71)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-13 08:32:59 +01:00
1 parent cfca29d71c
commit 88a0c8ce9c
2 files changed
+290

No files matched your search

+232
View File
@@ -0,0 +1,232 @@
namespace Khaemhat.Core.Treasury;
/// <summary>What a remittance (distinta) carries to the bank.</summary>
public enum RemittanceKind
{
/// <summary>Italian bank receipts (ricevute bancarie), CBI fixed-width file.</summary>
Riba = 0,
/// <summary>SEPA direct debit, core scheme (consumers and companies), ISO 20022 <c>pain.008</c>.</summary>
SddCore = 1,
/// <summary>SEPA direct debit, business-to-business scheme, ISO 20022 <c>pain.008</c>.</summary>
SddB2b = 2,
/// <summary>SEPA credit transfers to suppliers, ISO 20022 <c>pain.001</c>.</summary>
SepaCreditTransfer = 3,
}
/// <summary>The sequence of a direct debit under its mandate.</summary>
public enum SddSequence
{
/// <summary>The first debit of a recurrent mandate (<c>FRST</c>).</summary>
First = 0,
/// <summary>A following debit (<c>RCUR</c>).</summary>
Recurrent = 1,
/// <summary>The last debit (<c>FNAL</c>).</summary>
Final = 2,
/// <summary>A one-off mandate (<c>OOFF</c>).</summary>
OneOff = 3,
}
/// <summary>
/// The company in a bank file: the creditor of receipts and direct debits, the debtor of credit transfers.
/// </summary>
/// <param name="Name">The company name.</param>
/// <param name="TaxCode">The VAT number or tax code.</param>
/// <param name="Address">The street and number.</param>
/// <param name="PostalCode">The postal code.</param>
/// <param name="City">The town.</param>
/// <param name="Province">The province, two letters.</param>
/// <param name="CountryCode">The country, ISO 3166 alpha-2.</param>
/// <param name="Iban">The IBAN of the company account, compact.</param>
/// <param name="Bic">The BIC of the company bank, if known.</param>
/// <param name="SiaCode">The SIA code of the company (RiBa sender), five characters.</param>
/// <param name="CucCode">The CBI CUC code (initiating party of SEPA files in Italy).</param>
/// <param name="CreditorId">The SEPA creditor identifier of direct debits, such as <c>IT66ZZZA1B2C3D4E5F6G7H8</c>.</param>
public sealed record BankFileCompany(
string Name,
string? TaxCode,
string? Address,
string? PostalCode,
string? City,
string? Province,
string CountryCode,
string Iban,
string? Bic,
string? SiaCode,
string? CucCode,
string? CreditorId);
/// <summary>One effect, debit or transfer of a remittance.</summary>
/// <param name="Number">The progressive number in the file, from 1.</param>
/// <param name="CounterpartyCode">The code of the customer or supplier in the company (RiBa debtor code).</param>
/// <param name="CounterpartyName">The customer (debtor) or supplier (creditor).</param>
/// <param name="CounterpartyTaxCode">Its VAT number or tax code.</param>
/// <param name="Address">Its street and number.</param>
/// <param name="PostalCode">Its postal code.</param>
/// <param name="City">Its town.</param>
/// <param name="Province">Its province.</param>
/// <param name="CountryCode">Its country.</param>
/// <param name="Iban">Its IBAN, compact (for RiBa only ABI and CAB are used).</param>
/// <param name="Bic">Its BIC, if known.</param>
/// <param name="BankName">Its bank, for the RiBa description.</param>
/// <param name="Amount">The amount, positive, in euro.</param>
/// <param name="DueDate">The due date (RiBa), the collection date (SDD) or the execution date (transfers).</param>
/// <param name="Reference">The remittance information: the invoices paid, such as <c>FT 2026/000388 del 31/08/2026</c>.</param>
/// <param name="EndToEndId">The end-to-end identifier, unique in the company, at most 35 characters.</param>
/// <param name="MandateId">The direct debit mandate reference (UMR).</param>
/// <param name="MandateDate">The date the mandate was signed.</param>
/// <param name="Sequence">The sequence of the direct debit.</param>
public sealed record BankFileItem(
int Number,
string? CounterpartyCode,
string CounterpartyName,
string? CounterpartyTaxCode,
string? Address,
string? PostalCode,
string? City,
string? Province,
string CountryCode,
string? Iban,
string? Bic,
string? BankName,
decimal Amount,
DateOnly DueDate,
string Reference,
string EndToEndId,
string? MandateId = null,
DateOnly? MandateDate = null,
SddSequence Sequence = SddSequence.Recurrent);
/// <summary>A remittance as the bank file writers read it.</summary>
/// <param name="Kind">What it carries.</param>
/// <param name="MessageId">The unique identifier of the file (also the RiBa support name), at most 35 characters.</param>
/// <param name="CreatedAt">When the file is created, local time.</param>
/// <param name="ExecutionDate">The presentation date (RiBa), or the requested execution date of transfers.</param>
/// <param name="Company">The company and its account.</param>
/// <param name="Items">The effects, debits or transfers.</param>
public sealed record BankFileRemittance(
RemittanceKind Kind,
string MessageId,
DateTime CreatedAt,
DateOnly ExecutionDate,
BankFileCompany Company,
IReadOnlyList<BankFileItem> Items)
{
/// <summary>The total of the items.</summary>
public decimal Total => Items.Sum(i => i.Amount);
}
/// <summary>A file written for the bank.</summary>
/// <param name="FileName">The proposed name.</param>
/// <param name="ContentType">The media type.</param>
/// <param name="Content">The bytes.</param>
public sealed record BankFile(string FileName, string ContentType, byte[] Content);
/// <summary>The formats of a bank statement.</summary>
public enum StatementFormat
{
/// <summary>A CSV export of the home banking, with a layout.</summary>
Csv = 0,
/// <summary>ISO 20022 <c>camt.053</c> (bank to customer statement).</summary>
Camt053 = 1,
/// <summary>The Italian CBI statement (rendicontazione movimenti, records RH, 61-64, EF).</summary>
Cbi = 2,
}
/// <summary>A movement of a bank statement.</summary>
/// <param name="BookingDate">The booking date.</param>
/// <param name="ValueDate">The value date.</param>
/// <param name="Amount">The amount: positive for money in, negative for money out.</param>
/// <param name="Description">The description of the movement, on one line.</param>
/// <param name="Reference">The bank reference (CRO, TRN, end-to-end id), if any.</param>
/// <param name="CounterpartyName">The other party, if the file says it.</param>
/// <param name="CounterpartyIban">Its IBAN, if the file says it.</param>
/// <param name="BankCode">The bank transaction code (CBI causale, ISO domain/family), if any.</param>
public sealed record StatementMovement(
DateOnly BookingDate,
DateOnly? ValueDate,
decimal Amount,
string Description,
string? Reference = null,
string? CounterpartyName = null,
string? CounterpartyIban = null,
string? BankCode = null);
/// <summary>A bank statement read from a file.</summary>
/// <param name="Format">The format.</param>
/// <param name="AccountIban">The IBAN of the account, if the file says it.</param>
/// <param name="Currency">The currency, if the file says it.</param>
/// <param name="FromDate">The first day.</param>
/// <param name="ToDate">The last day.</param>
/// <param name="OpeningBalance">The opening balance, if the file says it.</param>
/// <param name="ClosingBalance">The closing balance, if the file says it.</param>
/// <param name="Movements">The movements, in the order of the file.</param>
public sealed record StatementFile(
StatementFormat Format,
string? AccountIban,
string? Currency,
DateOnly? FromDate,
DateOnly? ToDate,
decimal? OpeningBalance,
decimal? ClosingBalance,
IReadOnlyList<StatementMovement> Movements);
/// <summary>
/// The columns of a CSV statement. Column indexes are zero-based. The amount is either one signed column
/// (<see cref="AmountColumn"/>) or two columns of money in and money out (<see cref="CreditColumn"/>,
/// <see cref="DebitColumn"/>, both read as positive numbers).
/// </summary>
/// <param name="Separator">The field separator.</param>
/// <param name="HeaderRows">The rows to skip before the movements.</param>
/// <param name="DateColumn">The booking date.</param>
/// <param name="ValueDateColumn">The value date.</param>
/// <param name="DescriptionColumn">The description.</param>
/// <param name="AmountColumn">A signed amount.</param>
/// <param name="CreditColumn">Money in.</param>
/// <param name="DebitColumn">Money out.</param>
/// <param name="ReferenceColumn">The bank reference.</param>
/// <param name="DateFormat">The date format, such as <c>dd/MM/yyyy</c>.</param>
/// <param name="DecimalComma">Numbers are written <c>1.234,56</c> (otherwise <c>1,234.56</c>).</param>
public sealed record CsvStatementLayout(
char Separator,
int HeaderRows,
int DateColumn,
int? ValueDateColumn,
int DescriptionColumn,
int? AmountColumn,
int? CreditColumn,
int? DebitColumn,
int? ReferenceColumn,
string DateFormat = "dd/MM/yyyy",
bool DecimalComma = true);
/// <summary>A bank file that cannot be read or written.</summary>
public sealed class BankFileException : Exception
{
/// <summary>Creates the exception.</summary>
public BankFileException()
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">What is wrong, with the line or element (logged; the screens show a generic text).</param>
public BankFileException(string message)
: base(message)
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">What is wrong.</param>
/// <param name="innerException">The cause.</param>
public BankFileException(string message, Exception innerException)
: base(message, innerException)
{
}
}
@@ -0,0 +1,58 @@
using Khaemhat.Core.Treasury;
namespace Khaemhat.Fiscal.Abstractions;
/// <summary>
/// Writes the national remittance files the SEPA formats do not cover (Italy: RiBa in the CBI fixed-width layout).
/// SEPA credit transfers and direct debits are written by <c>Khaemhat.Core.Treasury.SepaXmlWriter</c> for every
/// country.
/// </summary>
public interface IRemittanceFileWriter
{
/// <summary>Whether this writer writes a kind of remittance.</summary>
/// <param name="kind">The kind.</param>
/// <returns><see langword="true"/> when <see cref="Write"/> accepts it.</returns>
bool Supports(RemittanceKind kind);
/// <summary>Writes the file.</summary>
/// <param name="remittance">The remittance.</param>
/// <returns>The file.</returns>
/// <exception cref="BankFileException">A value cannot be written in the format.</exception>
BankFile Write(BankFileRemittance remittance);
}
/// <summary>
/// Reads the national bank statement format of the country (Italy: CBI). CSV and <c>camt.053</c> are read by
/// <c>Khaemhat.Core.Treasury</c> for every country.
/// </summary>
public interface IBankStatementReader
{
/// <summary>The format read.</summary>
StatementFormat Format { get; }
/// <summary>Whether the start of a file looks like this format.</summary>
/// <param name="head">The first bytes of the file (up to 4 KB).</param>
/// <returns><see langword="true"/> when <see cref="Read"/> should be tried.</returns>
bool Recognizes(ReadOnlySpan<byte> head);
/// <summary>Reads a statement.</summary>
/// <param name="content">The file.</param>
/// <returns>The statement.</returns>
/// <exception cref="BankFileException">The file is not in the format.</exception>
StatementFile Read(Stream content);
}
/// <summary>Late payment interest between companies (Italy: D.Lgs. 231/2002).</summary>
public interface ILatePaymentRules
{
/// <summary>The yearly rate, in percent, that applies to the days of a half-year.</summary>
/// <param name="day">A day.</param>
/// <returns>The reference rate of the half-year plus the margin.</returns>
decimal RateOn(DateOnly day);
/// <summary>The law the interest comes from, for the letters (Italy: <c>D.Lgs. 231/2002</c>).</summary>
string LegalReference { get; }
/// <summary>The fixed compensation for recovery costs, per invoice (Italy: 40 euro, art. 6).</summary>
decimal RecoveryCompensation { get; }
}