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