Files
Khaemhat/src/Khaemhat.Fiscal.Abstractions/IncomingInvoices.cs
T
Alby96andClaude Opus 5.5 709bb13532 feat: FatturaPA writer, validator, SDI receipts and transport channels (#74 #75 #76 #77)
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>
2026-11-13 11:51:31 +01:00

99 lines
4.7 KiB
C#

namespace Khaemhat.Fiscal.Abstractions;
// The supplier invoices received as files of the country's e-invoice format (Italy: FatturaPA, also signed .p7m).
// The records keep the file as it is, with the codes of the format; the import service maps them to documents.
/// <summary>
/// Reads the e-invoice files received from suppliers: an optional feature of a country pack, like
/// <see cref="IEInvoiceFormat"/>. The reader only reads: it never contacts a service and never resolves external
/// references.
/// </summary>
public interface IIncomingInvoiceReader
{
/// <summary>
/// Whether the file looks like one the reader understands (Italy: <c>.xml</c>, <c>.p7m</c> in DER or as base64
/// text). The content is sniffed, not only the name.
/// </summary>
/// <param name="fileName">The name of the file, extension included.</param>
/// <param name="content">The content, or at least its first bytes.</param>
/// <returns><see langword="true"/> when <see cref="Read"/> is worth trying.</returns>
bool CanRead(string fileName, ReadOnlySpan<byte> content);
/// <summary>Reads a file.</summary>
/// <param name="fileName">The name of the file, extension included.</param>
/// <param name="content">The whole content.</param>
/// <returns>The file with its invoices.</returns>
/// <exception cref="IncomingInvoiceException">The file is not readable, not an invoice, or misses mandatory data;
/// <see cref="IncomingInvoiceException.Code"/> is one of <see cref="IncomingInvoiceErrors"/>.</exception>
EInvoiceFile Read(string fileName, byte[] content);
}
/// <summary>A received e-invoice file that cannot be read; <see cref="Code"/> is one of <see cref="IncomingInvoiceErrors"/>.</summary>
public sealed class IncomingInvoiceException : Exception
{
/// <summary>Creates the exception.</summary>
public IncomingInvoiceException()
: this(IncomingInvoiceErrors.NotReadable, "The file is not readable.")
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">What went wrong.</param>
public IncomingInvoiceException(string message)
: this(IncomingInvoiceErrors.NotReadable, message)
{
}
/// <summary>Creates the exception.</summary>
/// <param name="message">What went wrong.</param>
/// <param name="innerException">The cause.</param>
public IncomingInvoiceException(string message, Exception innerException)
: this(IncomingInvoiceErrors.NotReadable, message, innerException)
{
}
/// <summary>Creates the exception with a code.</summary>
/// <param name="code">One of <see cref="IncomingInvoiceErrors"/>.</param>
/// <param name="message">What went wrong.</param>
public IncomingInvoiceException(string code, string message)
: base(message) => Code = code;
/// <summary>Creates the exception with a code and its cause.</summary>
/// <param name="code">One of <see cref="IncomingInvoiceErrors"/>.</param>
/// <param name="message">What went wrong.</param>
/// <param name="innerException">The cause.</param>
public IncomingInvoiceException(string code, string message, Exception? innerException)
: base(message, innerException) => Code = code;
/// <summary>The text code of the error, one of <see cref="IncomingInvoiceErrors"/>.</summary>
public string Code { get; }
}
/// <summary>The codes of <see cref="IncomingInvoiceException"/>.</summary>
public static class IncomingInvoiceErrors
{
/// <summary>Neither XML nor a signed envelope: garbage, an empty file, a truncated download.</summary>
public const string NotReadable = "incoming.not-readable";
/// <summary>A well-formed XML document that is not an e-invoice.</summary>
public const string NotInvoice = "incoming.not-invoice";
/// <summary>A signed envelope whose content cannot be extracted.</summary>
public const string SignatureBroken = "incoming.signature-broken";
/// <summary>An e-invoice that misses mandatory data (date, number, document type...).</summary>
public const string Invalid = "incoming.invalid";
}
/// <summary>
/// How a received e-invoice becomes a document of the company: which document type registers each type of the format
/// (Italy: TD01 and the like as supplier invoice FF, TD04 and TD08 as supplier credit note NCF).
/// </summary>
public interface IIncomingInvoiceRules
{
/// <summary>The code of the document type that registers an invoice of this type of the format.</summary>
/// <param name="formatType">The type in the file (TD01...).</param>
/// <returns>The document type code, or none when the type is not registered as a purchase (a self-invoice).</returns>
string? DocumentTypeCode(string formatType);
}