The Italian pack can now produce and check the e-invoice and talk to the SDI: - FatturaPaWriter writes FPR12/FPA12 v1.2 with SDI file naming; FatturaPaValidator checks it against an embedded schema and the AdE semantic rules (official codes). - SdiReceiptReader reads RC, NS, MC, NE, DT, AT, MT and SE notifications. - The incoming-invoice model becomes the shared EInvoice* model, read and written. - In-house ImapClient and MimeParser (no external libraries) back the PEC channel; SdICoop (SOAP/MTOM with client certificate) and two generic REST intermediaries sit behind the same IEInvoiceTransport contract, with fake-server tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
254 lines
16 KiB
C#
254 lines
16 KiB
C#
namespace Khaemhat.Fiscal.Abstractions;
|
|
|
|
// The model of an e-invoice file of the country's format (Italy: FatturaPA), the same in both directions: the reader
|
|
// fills it from a received file (M6), the writer turns it into the file to send (M9). It keeps the codes of the format.
|
|
|
|
/// <summary>An e-invoice file: the transmission header, the two parties and one or more invoices.</summary>
|
|
/// <param name="FileName">The name of the file, as received or as it will be sent.</param>
|
|
/// <param name="TransmissionFormat">The transmission format (Italy: <c>FPR12</c>, <c>FPA12</c>, <c>FSM10</c>).</param>
|
|
/// <param name="ProgressiveNumber">The progressive number of the transmission given by the sender.</param>
|
|
/// <param name="RecipientCode">The recipient code of the transmission.</param>
|
|
/// <param name="Supplier">The supplier who issued the invoices.</param>
|
|
/// <param name="Customer">The customer the invoices are addressed to (normally the company).</param>
|
|
/// <param name="Invoices">The invoices, one per body of the file (a batch has more than one).</param>
|
|
/// <param name="Signature">The signature of the envelope, or <see langword="null"/> for an unsigned file.</param>
|
|
/// <param name="Xml">The XML of the invoice, taken out of the envelope when the file was signed.</param>
|
|
public sealed record EInvoiceFile(
|
|
string FileName, string? TransmissionFormat, string? ProgressiveNumber, string? RecipientCode,
|
|
EInvoiceParty Supplier, EInvoiceParty Customer, IReadOnlyList<EInvoiceBody> Invoices,
|
|
EInvoiceSignature? Signature, byte[] Xml)
|
|
{
|
|
/// <summary>The country of the transmitter (Italy: <c>IdTrasmittente/IdPaese</c>).</summary>
|
|
public string? TransmitterCountry { get; init; }
|
|
|
|
/// <summary>The tax identifier of the transmitter (Italy: <c>IdTrasmittente/IdCodice</c>).</summary>
|
|
public string? TransmitterId { get; init; }
|
|
|
|
/// <summary>The certified mailbox the invoice is delivered to, when there is no recipient code.</summary>
|
|
public string? RecipientPec { get; init; }
|
|
|
|
/// <summary>The e-mail of the transmitter, for the exchange system (Italy: <c>ContattiTrasmittente/Email</c>).</summary>
|
|
public string? TransmitterEmail { get; init; }
|
|
|
|
/// <summary>The third party that issues the invoice for the supplier, if any (Italy: <c>TerzoIntermediarioOSoggettoEmittente</c>).</summary>
|
|
public EInvoiceParty? Intermediary { get; init; }
|
|
|
|
/// <summary>Who issues the document when not the supplier (Italy: <c>SoggettoEmittente</c>, <c>CC</c> customer, <c>TZ</c> third party).</summary>
|
|
public string? IssuedBy { get; init; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The signature of a signed file. <see cref="Verified"/> says only that the content is intact and signed by the key
|
|
/// of the embedded certificate: the trust chain and the revocation of the certificate are not checked, because no
|
|
/// trusted list is available offline.
|
|
/// </summary>
|
|
/// <param name="Verified">Whether the digest and the signature match the content.</param>
|
|
/// <param name="Signer">The name of the signer, from the certificate.</param>
|
|
/// <param name="SignerTaxCode">The tax code of the signer, when the certificate carries it.</param>
|
|
/// <param name="SignedAt">The signing time declared by the signer, if any (not a trusted timestamp).</param>
|
|
/// <param name="CertificateNotBefore">The start of validity of the certificate.</param>
|
|
/// <param name="CertificateNotAfter">The end of validity of the certificate.</param>
|
|
/// <param name="Problem">Why the signature is not verified, or <see langword="null"/>.</param>
|
|
public sealed record EInvoiceSignature(bool Verified, string? Signer, string? SignerTaxCode, DateTimeOffset? SignedAt,
|
|
DateTimeOffset? CertificateNotBefore, DateTimeOffset? CertificateNotAfter, string? Problem);
|
|
|
|
/// <summary>A party of the invoice (supplier, customer or intermediary) as written in the file.</summary>
|
|
/// <param name="CountryCode">The country of the VAT number.</param>
|
|
/// <param name="VatNumber">The VAT number without the country prefix.</param>
|
|
/// <param name="TaxCode">The tax code.</param>
|
|
/// <param name="Name">The company name, when the party is a company.</param>
|
|
/// <param name="FirstName">The first name, when the party is a person.</param>
|
|
/// <param name="LastName">The last name, when the party is a person.</param>
|
|
/// <param name="Address">The street of the registered office.</param>
|
|
/// <param name="StreetNumber">The street number.</param>
|
|
/// <param name="PostalCode">The postal code.</param>
|
|
/// <param name="City">The city.</param>
|
|
/// <param name="Province">The province.</param>
|
|
/// <param name="Country">The country of the registered office.</param>
|
|
/// <param name="TaxRegime">The tax regime (supplier only).</param>
|
|
/// <param name="Email">The e-mail address (supplier only).</param>
|
|
/// <param name="Phone">The phone number (supplier only).</param>
|
|
/// <param name="ReaOffice">The office of the companies register.</param>
|
|
/// <param name="ReaNumber">The number in the companies register.</param>
|
|
public sealed record EInvoiceParty(
|
|
string? CountryCode, string? VatNumber, string? TaxCode, string? Name, string? FirstName, string? LastName,
|
|
string? Address, string? StreetNumber, string? PostalCode, string? City, string? Province, string? Country,
|
|
string? TaxRegime, string? Email, string? Phone, string? ReaOffice, string? ReaNumber)
|
|
{
|
|
/// <summary>The company name, or first and last name of a person.</summary>
|
|
public string DisplayName => Name ?? string.Join(' ', new[] { FirstName, LastName }.Where(s => !string.IsNullOrWhiteSpace(s)));
|
|
|
|
/// <summary>The share capital, for a company in the companies register.</summary>
|
|
public decimal? ShareCapital { get; init; }
|
|
|
|
/// <summary>One or more shareholders (Italy: <c>SU</c> sole, <c>SM</c> several).</summary>
|
|
public string? Shareholders { get; init; }
|
|
|
|
/// <summary>Whether the company is being wound up (Italy: <c>LS</c> yes, <c>LN</c> no).</summary>
|
|
public string? LiquidationStatus { get; init; }
|
|
|
|
/// <summary>The reference of the administration, for the public administration (Italy: <c>RiferimentoAmministrazione</c>).</summary>
|
|
public string? AdministrationReference { get; init; }
|
|
}
|
|
|
|
/// <summary>An invoice of the file (one body).</summary>
|
|
/// <param name="DocumentType">The document type (Italy: <c>TD01</c>, <c>TD04</c>...).</param>
|
|
/// <param name="Currency">The currency code.</param>
|
|
/// <param name="Date">The date of the document.</param>
|
|
/// <param name="Number">The number of the document.</param>
|
|
/// <param name="Total">The total amount declared by the supplier, if any.</param>
|
|
/// <param name="Rounding">The rounding on the total, if any.</param>
|
|
/// <param name="Causals">The free-text causals.</param>
|
|
/// <param name="Art73">Whether the document is issued under the rule that allows several documents with the same
|
|
/// number in a year (Italy: art. 73 DPR 633/72).</param>
|
|
/// <param name="Withholdings">The withholding taxes.</param>
|
|
/// <param name="StampDuty">The stamp duty, if any.</param>
|
|
/// <param name="SocialSecurity">The social security contributions.</param>
|
|
/// <param name="Discounts">The discounts and surcharges on the whole document.</param>
|
|
/// <param name="Orders">The purchase orders referred to.</param>
|
|
/// <param name="Contracts">The contracts referred to.</param>
|
|
/// <param name="RelatedInvoices">The invoices referred to (the invoice a credit note corrects).</param>
|
|
/// <param name="DeliveryNotes">The delivery notes referred to.</param>
|
|
/// <param name="Lines">The lines.</param>
|
|
/// <param name="TaxSummary">The tax summary, one row per rate or nature.</param>
|
|
/// <param name="Payments">The payment installments.</param>
|
|
/// <param name="Attachments">The attached files.</param>
|
|
public sealed record EInvoiceBody(
|
|
string DocumentType, string Currency, DateOnly Date, string Number, decimal? Total, decimal? Rounding,
|
|
IReadOnlyList<string> Causals, bool Art73,
|
|
IReadOnlyList<EInvoiceWithholding> Withholdings, EInvoiceStampDuty? StampDuty,
|
|
IReadOnlyList<EInvoiceSocialSecurity> SocialSecurity, IReadOnlyList<EInvoiceDiscount> Discounts,
|
|
IReadOnlyList<EInvoiceReference> Orders, IReadOnlyList<EInvoiceReference> Contracts,
|
|
IReadOnlyList<EInvoiceReference> RelatedInvoices, IReadOnlyList<EInvoiceReference> DeliveryNotes,
|
|
IReadOnlyList<EInvoiceLine> Lines, IReadOnlyList<EInvoiceTaxSummary> TaxSummary,
|
|
IReadOnlyList<EInvoicePayment> Payments, IReadOnlyList<EInvoiceAttachment> Attachments)
|
|
{
|
|
/// <summary>The transport data of an accompanying invoice, if any.</summary>
|
|
public EInvoiceTransport? Transport { get; init; }
|
|
|
|
/// <summary>The payment conditions of the whole document (Italy: <c>TP01</c>, <c>TP02</c>, <c>TP03</c>).</summary>
|
|
public string? PaymentConditions { get; init; }
|
|
}
|
|
|
|
/// <summary>The transport data of an accompanying invoice (Italy: <c>DatiTrasporto</c>).</summary>
|
|
/// <param name="CarrierName">The carrier.</param>
|
|
/// <param name="CarrierCountry">The country of the VAT number of the carrier.</param>
|
|
/// <param name="CarrierVat">The VAT number of the carrier.</param>
|
|
/// <param name="Means">The means of transport.</param>
|
|
/// <param name="Reason">The reason of the transport.</param>
|
|
/// <param name="Packages">The number of packages.</param>
|
|
/// <param name="Description">The appearance of the goods.</param>
|
|
/// <param name="GrossWeight">The gross weight, kg.</param>
|
|
/// <param name="NetWeight">The net weight, kg.</param>
|
|
/// <param name="Start">When the transport starts.</param>
|
|
/// <param name="ShippingTerms">The terms of delivery (Incoterms).</param>
|
|
public sealed record EInvoiceTransport(
|
|
string? CarrierName, string? CarrierCountry, string? CarrierVat, string? Means, string? Reason, int? Packages,
|
|
string? Description, decimal? GrossWeight, decimal? NetWeight, DateTime? Start, string? ShippingTerms);
|
|
|
|
/// <summary>A line of an invoice.</summary>
|
|
/// <param name="Number">The line number.</param>
|
|
/// <param name="Codes">The item codes (type and value).</param>
|
|
/// <param name="Description">The description.</param>
|
|
/// <param name="Quantity">The quantity, if given.</param>
|
|
/// <param name="Unit">The unit of measure, if given.</param>
|
|
/// <param name="UnitPrice">The unit price.</param>
|
|
/// <param name="TotalPrice">The total of the line, net of tax.</param>
|
|
/// <param name="TaxRate">The tax rate in percent (zero with a nature).</param>
|
|
/// <param name="Nature">The reason of a zero rate (Italy: <c>N2.2</c>...).</param>
|
|
/// <param name="Discounts">The discounts and surcharges of the line.</param>
|
|
/// <param name="Withheld">Whether the line is subject to withholding tax.</param>
|
|
/// <param name="LineType">The kind of line (Italy: <c>SC</c> discount, <c>PR</c> premium, <c>AB</c> rebate,
|
|
/// <c>AC</c> ancillary cost).</param>
|
|
/// <param name="PeriodStart">The start of the period the line refers to.</param>
|
|
/// <param name="PeriodEnd">The end of the period the line refers to.</param>
|
|
/// <param name="AdministrativeReference">The administrative reference (cost centre, internal code).</param>
|
|
public sealed record EInvoiceLine(
|
|
int Number, IReadOnlyList<EInvoiceItemCode> Codes, string Description, decimal? Quantity, string? Unit,
|
|
decimal UnitPrice, decimal TotalPrice, decimal TaxRate, string? Nature, IReadOnlyList<EInvoiceDiscount> Discounts,
|
|
bool Withheld, string? LineType, DateOnly? PeriodStart, DateOnly? PeriodEnd, string? AdministrativeReference)
|
|
{
|
|
/// <summary>Further data of the line (Italy: <c>AltriDatiGestionali</c>, such as the declaration of intent).</summary>
|
|
public IReadOnlyList<EInvoiceOtherData> OtherData { get; init; } = [];
|
|
}
|
|
|
|
/// <summary>Further data of a line (Italy: <c>AltriDatiGestionali</c>).</summary>
|
|
/// <param name="Type">The kind of data (Italy: <c>TipoDato</c>, such as <c>INTENTO</c>).</param>
|
|
/// <param name="Text">A text value.</param>
|
|
/// <param name="Number">A number value.</param>
|
|
/// <param name="Date">A date value.</param>
|
|
public sealed record EInvoiceOtherData(string Type, string? Text, decimal? Number, DateOnly? Date);
|
|
|
|
/// <summary>A code of the item of a line.</summary>
|
|
/// <param name="Type">The kind of code (<c>EAN</c>, <c>SKU</c>, the supplier's code...).</param>
|
|
/// <param name="Value">The code.</param>
|
|
public sealed record EInvoiceItemCode(string Type, string Value);
|
|
|
|
/// <summary>A discount or surcharge.</summary>
|
|
/// <param name="IsSurcharge">Whether it is a surcharge (Italy: <c>MG</c>) instead of a discount (<c>SC</c>).</param>
|
|
/// <param name="Percent">The percentage, if given.</param>
|
|
/// <param name="Amount">The amount, if given.</param>
|
|
public sealed record EInvoiceDiscount(bool IsSurcharge, decimal? Percent, decimal? Amount);
|
|
|
|
/// <summary>A row of the tax summary.</summary>
|
|
/// <param name="Rate">The tax rate in percent.</param>
|
|
/// <param name="Nature">The reason of a zero rate.</param>
|
|
/// <param name="Taxable">The taxable amount.</param>
|
|
/// <param name="Tax">The tax.</param>
|
|
/// <param name="Exigibility">When the tax is due (Italy: <c>I</c> immediate, <c>D</c> deferred, <c>S</c> split payment).</param>
|
|
/// <param name="LegalReference">The law that justifies the nature.</param>
|
|
/// <param name="ExpensesAmount">Ancillary expenses, if any.</param>
|
|
/// <param name="Rounding">The rounding, if any.</param>
|
|
public sealed record EInvoiceTaxSummary(decimal Rate, string? Nature, decimal Taxable, decimal Tax, string? Exigibility,
|
|
string? LegalReference, decimal? ExpensesAmount, decimal? Rounding);
|
|
|
|
/// <summary>A payment installment.</summary>
|
|
/// <param name="Conditions">The payment conditions (Italy: <c>TP01</c> installments, <c>TP02</c> full, <c>TP03</c> advance).</param>
|
|
/// <param name="Method">The payment method (Italy: <c>MP05</c> bank transfer...).</param>
|
|
/// <param name="DueDate">The due date, if given.</param>
|
|
/// <param name="Amount">The amount.</param>
|
|
/// <param name="Iban">The IBAN to pay to, if given.</param>
|
|
/// <param name="Bic">The BIC, if given.</param>
|
|
/// <param name="Beneficiary">The beneficiary, if different from the supplier.</param>
|
|
public sealed record EInvoicePayment(string? Conditions, string? Method, DateOnly? DueDate, decimal Amount,
|
|
string? Iban, string? Bic, string? Beneficiary);
|
|
|
|
/// <summary>A reference to another document (order, contract, invoice, delivery note).</summary>
|
|
/// <param name="Id">The number of the document.</param>
|
|
/// <param name="Date">The date of the document.</param>
|
|
/// <param name="LineNumbers">The lines of the invoice the reference applies to; empty when it applies to all.</param>
|
|
/// <param name="Cig">The tender code, if any (public administration).</param>
|
|
/// <param name="Cup">The project code, if any (public administration).</param>
|
|
public sealed record EInvoiceReference(string? Id, DateOnly? Date, IReadOnlyList<int> LineNumbers, string? Cig, string? Cup);
|
|
|
|
/// <summary>A withholding tax.</summary>
|
|
/// <param name="Type">The kind of withholding (Italy: <c>RT01</c>...).</param>
|
|
/// <param name="Amount">The amount withheld.</param>
|
|
/// <param name="Rate">The rate in percent.</param>
|
|
/// <param name="Reason">The payment reason (Italy: <c>A</c>...), if given.</param>
|
|
public sealed record EInvoiceWithholding(string Type, decimal Amount, decimal Rate, string? Reason);
|
|
|
|
/// <summary>The stamp duty of an invoice.</summary>
|
|
/// <param name="Virtual">Whether it is paid virtually by the issuer.</param>
|
|
/// <param name="Amount">The amount.</param>
|
|
public sealed record EInvoiceStampDuty(bool Virtual, decimal Amount);
|
|
|
|
/// <summary>A social security contribution added to the invoice.</summary>
|
|
/// <param name="Fund">The fund (Italy: <c>TC01</c>...).</param>
|
|
/// <param name="Rate">The rate of the contribution in percent.</param>
|
|
/// <param name="Amount">The amount of the contribution.</param>
|
|
/// <param name="Taxable">The amount the contribution is computed on, if given.</param>
|
|
/// <param name="TaxRate">The tax rate applied to the contribution.</param>
|
|
/// <param name="Withheld">Whether the contribution is subject to withholding tax.</param>
|
|
/// <param name="Nature">The reason of a zero tax rate.</param>
|
|
public sealed record EInvoiceSocialSecurity(string Fund, decimal Rate, decimal Amount, decimal? Taxable, decimal TaxRate,
|
|
bool Withheld, string? Nature);
|
|
|
|
/// <summary>A file attached to the invoice.</summary>
|
|
/// <param name="Name">The name of the attachment.</param>
|
|
/// <param name="Format">The format declared by the supplier (such as <c>PDF</c>), if any.</param>
|
|
/// <param name="Description">The description, if any.</param>
|
|
/// <param name="Content">The decoded content.</param>
|
|
public sealed record EInvoiceAttachment(string Name, string? Format, string? Description, byte[] Content);
|
|
|