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.
/// An e-invoice file: the transmission header, the two parties and one or more invoices.
/// The name of the file, as received or as it will be sent.
/// The transmission format (Italy: FPR12, FPA12, FSM10).
/// The progressive number of the transmission given by the sender.
/// The recipient code of the transmission.
/// The supplier who issued the invoices.
/// The customer the invoices are addressed to (normally the company).
/// The invoices, one per body of the file (a batch has more than one).
/// The signature of the envelope, or for an unsigned file.
/// The XML of the invoice, taken out of the envelope when the file was signed.
public sealed record EInvoiceFile(
string FileName, string? TransmissionFormat, string? ProgressiveNumber, string? RecipientCode,
EInvoiceParty Supplier, EInvoiceParty Customer, IReadOnlyList Invoices,
EInvoiceSignature? Signature, byte[] Xml)
{
/// The country of the transmitter (Italy: IdTrasmittente/IdPaese).
public string? TransmitterCountry { get; init; }
/// The tax identifier of the transmitter (Italy: IdTrasmittente/IdCodice).
public string? TransmitterId { get; init; }
/// The certified mailbox the invoice is delivered to, when there is no recipient code.
public string? RecipientPec { get; init; }
/// The e-mail of the transmitter, for the exchange system (Italy: ContattiTrasmittente/Email).
public string? TransmitterEmail { get; init; }
/// The third party that issues the invoice for the supplier, if any (Italy: TerzoIntermediarioOSoggettoEmittente).
public EInvoiceParty? Intermediary { get; init; }
/// Who issues the document when not the supplier (Italy: SoggettoEmittente, CC customer, TZ third party).
public string? IssuedBy { get; init; }
}
///
/// The signature of a signed file. 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.
///
/// Whether the digest and the signature match the content.
/// The name of the signer, from the certificate.
/// The tax code of the signer, when the certificate carries it.
/// The signing time declared by the signer, if any (not a trusted timestamp).
/// The start of validity of the certificate.
/// The end of validity of the certificate.
/// Why the signature is not verified, or .
public sealed record EInvoiceSignature(bool Verified, string? Signer, string? SignerTaxCode, DateTimeOffset? SignedAt,
DateTimeOffset? CertificateNotBefore, DateTimeOffset? CertificateNotAfter, string? Problem);
/// A party of the invoice (supplier, customer or intermediary) as written in the file.
/// The country of the VAT number.
/// The VAT number without the country prefix.
/// The tax code.
/// The company name, when the party is a company.
/// The first name, when the party is a person.
/// The last name, when the party is a person.
/// The street of the registered office.
/// The street number.
/// The postal code.
/// The city.
/// The province.
/// The country of the registered office.
/// The tax regime (supplier only).
/// The e-mail address (supplier only).
/// The phone number (supplier only).
/// The office of the companies register.
/// The number in the companies register.
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)
{
/// The company name, or first and last name of a person.
public string DisplayName => Name ?? string.Join(' ', new[] { FirstName, LastName }.Where(s => !string.IsNullOrWhiteSpace(s)));
/// The share capital, for a company in the companies register.
public decimal? ShareCapital { get; init; }
/// One or more shareholders (Italy: SU sole, SM several).
public string? Shareholders { get; init; }
/// Whether the company is being wound up (Italy: LS yes, LN no).
public string? LiquidationStatus { get; init; }
/// The reference of the administration, for the public administration (Italy: RiferimentoAmministrazione).
public string? AdministrationReference { get; init; }
}
/// An invoice of the file (one body).
/// The document type (Italy: TD01, TD04...).
/// The currency code.
/// The date of the document.
/// The number of the document.
/// The total amount declared by the supplier, if any.
/// The rounding on the total, if any.
/// The free-text causals.
/// 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).
/// The withholding taxes.
/// The stamp duty, if any.
/// The social security contributions.
/// The discounts and surcharges on the whole document.
/// The purchase orders referred to.
/// The contracts referred to.
/// The invoices referred to (the invoice a credit note corrects).
/// The delivery notes referred to.
/// The lines.
/// The tax summary, one row per rate or nature.
/// The payment installments.
/// The attached files.
public sealed record EInvoiceBody(
string DocumentType, string Currency, DateOnly Date, string Number, decimal? Total, decimal? Rounding,
IReadOnlyList Causals, bool Art73,
IReadOnlyList Withholdings, EInvoiceStampDuty? StampDuty,
IReadOnlyList SocialSecurity, IReadOnlyList Discounts,
IReadOnlyList Orders, IReadOnlyList Contracts,
IReadOnlyList RelatedInvoices, IReadOnlyList DeliveryNotes,
IReadOnlyList Lines, IReadOnlyList TaxSummary,
IReadOnlyList Payments, IReadOnlyList Attachments)
{
/// The transport data of an accompanying invoice, if any.
public EInvoiceTransport? Transport { get; init; }
/// The payment conditions of the whole document (Italy: TP01, TP02, TP03).
public string? PaymentConditions { get; init; }
}
/// The transport data of an accompanying invoice (Italy: DatiTrasporto).
/// The carrier.
/// The country of the VAT number of the carrier.
/// The VAT number of the carrier.
/// The means of transport.
/// The reason of the transport.
/// The number of packages.
/// The appearance of the goods.
/// The gross weight, kg.
/// The net weight, kg.
/// When the transport starts.
/// The terms of delivery (Incoterms).
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);
/// A line of an invoice.
/// The line number.
/// The item codes (type and value).
/// The description.
/// The quantity, if given.
/// The unit of measure, if given.
/// The unit price.
/// The total of the line, net of tax.
/// The tax rate in percent (zero with a nature).
/// The reason of a zero rate (Italy: N2.2...).
/// The discounts and surcharges of the line.
/// Whether the line is subject to withholding tax.
/// The kind of line (Italy: SC discount, PR premium, AB rebate,
/// AC ancillary cost).
/// The start of the period the line refers to.
/// The end of the period the line refers to.
/// The administrative reference (cost centre, internal code).
public sealed record EInvoiceLine(
int Number, IReadOnlyList Codes, string Description, decimal? Quantity, string? Unit,
decimal UnitPrice, decimal TotalPrice, decimal TaxRate, string? Nature, IReadOnlyList Discounts,
bool Withheld, string? LineType, DateOnly? PeriodStart, DateOnly? PeriodEnd, string? AdministrativeReference)
{
/// Further data of the line (Italy: AltriDatiGestionali, such as the declaration of intent).
public IReadOnlyList OtherData { get; init; } = [];
}
/// Further data of a line (Italy: AltriDatiGestionali).
/// The kind of data (Italy: TipoDato, such as INTENTO).
/// A text value.
/// A number value.
/// A date value.
public sealed record EInvoiceOtherData(string Type, string? Text, decimal? Number, DateOnly? Date);
/// A code of the item of a line.
/// The kind of code (EAN, SKU, the supplier's code...).
/// The code.
public sealed record EInvoiceItemCode(string Type, string Value);
/// A discount or surcharge.
/// Whether it is a surcharge (Italy: MG) instead of a discount (SC).
/// The percentage, if given.
/// The amount, if given.
public sealed record EInvoiceDiscount(bool IsSurcharge, decimal? Percent, decimal? Amount);
/// A row of the tax summary.
/// The tax rate in percent.
/// The reason of a zero rate.
/// The taxable amount.
/// The tax.
/// When the tax is due (Italy: I immediate, D deferred, S split payment).
/// The law that justifies the nature.
/// Ancillary expenses, if any.
/// The rounding, if any.
public sealed record EInvoiceTaxSummary(decimal Rate, string? Nature, decimal Taxable, decimal Tax, string? Exigibility,
string? LegalReference, decimal? ExpensesAmount, decimal? Rounding);
/// A payment installment.
/// The payment conditions (Italy: TP01 installments, TP02 full, TP03 advance).
/// The payment method (Italy: MP05 bank transfer...).
/// The due date, if given.
/// The amount.
/// The IBAN to pay to, if given.
/// The BIC, if given.
/// The beneficiary, if different from the supplier.
public sealed record EInvoicePayment(string? Conditions, string? Method, DateOnly? DueDate, decimal Amount,
string? Iban, string? Bic, string? Beneficiary);
/// A reference to another document (order, contract, invoice, delivery note).
/// The number of the document.
/// The date of the document.
/// The lines of the invoice the reference applies to; empty when it applies to all.
/// The tender code, if any (public administration).
/// The project code, if any (public administration).
public sealed record EInvoiceReference(string? Id, DateOnly? Date, IReadOnlyList LineNumbers, string? Cig, string? Cup);
/// A withholding tax.
/// The kind of withholding (Italy: RT01...).
/// The amount withheld.
/// The rate in percent.
/// The payment reason (Italy: A...), if given.
public sealed record EInvoiceWithholding(string Type, decimal Amount, decimal Rate, string? Reason);
/// The stamp duty of an invoice.
/// Whether it is paid virtually by the issuer.
/// The amount.
public sealed record EInvoiceStampDuty(bool Virtual, decimal Amount);
/// A social security contribution added to the invoice.
/// The fund (Italy: TC01...).
/// The rate of the contribution in percent.
/// The amount of the contribution.
/// The amount the contribution is computed on, if given.
/// The tax rate applied to the contribution.
/// Whether the contribution is subject to withholding tax.
/// The reason of a zero tax rate.
public sealed record EInvoiceSocialSecurity(string Fund, decimal Rate, decimal Amount, decimal? Taxable, decimal TaxRate,
bool Withheld, string? Nature);
/// A file attached to the invoice.
/// The name of the attachment.
/// The format declared by the supplier (such as PDF), if any.
/// The description, if any.
/// The decoded content.
public sealed record EInvoiceAttachment(string Name, string? Format, string? Description, byte[] Content);