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.
///
/// Reads the e-invoice files received from suppliers: an optional feature of a country pack, like
/// . The reader only reads: it never contacts a service and never resolves external
/// references.
///
public interface IIncomingInvoiceReader
{
///
/// Whether the file looks like one the reader understands (Italy: .xml, .p7m in DER or as base64
/// text). The content is sniffed, not only the name.
///
/// The name of the file, extension included.
/// The content, or at least its first bytes.
/// when is worth trying.
bool CanRead(string fileName, ReadOnlySpan content);
/// Reads a file.
/// The name of the file, extension included.
/// The whole content.
/// The file with its invoices.
/// The file is not readable, not an invoice, or misses mandatory data;
/// is one of .
EInvoiceFile Read(string fileName, byte[] content);
}
/// A received e-invoice file that cannot be read; is one of .
public sealed class IncomingInvoiceException : Exception
{
/// Creates the exception.
public IncomingInvoiceException()
: this(IncomingInvoiceErrors.NotReadable, "The file is not readable.")
{
}
/// Creates the exception.
/// What went wrong.
public IncomingInvoiceException(string message)
: this(IncomingInvoiceErrors.NotReadable, message)
{
}
/// Creates the exception.
/// What went wrong.
/// The cause.
public IncomingInvoiceException(string message, Exception innerException)
: this(IncomingInvoiceErrors.NotReadable, message, innerException)
{
}
/// Creates the exception with a code.
/// One of .
/// What went wrong.
public IncomingInvoiceException(string code, string message)
: base(message) => Code = code;
/// Creates the exception with a code and its cause.
/// One of .
/// What went wrong.
/// The cause.
public IncomingInvoiceException(string code, string message, Exception? innerException)
: base(message, innerException) => Code = code;
/// The text code of the error, one of .
public string Code { get; }
}
/// The codes of .
public static class IncomingInvoiceErrors
{
/// Neither XML nor a signed envelope: garbage, an empty file, a truncated download.
public const string NotReadable = "incoming.not-readable";
/// A well-formed XML document that is not an e-invoice.
public const string NotInvoice = "incoming.not-invoice";
/// A signed envelope whose content cannot be extracted.
public const string SignatureBroken = "incoming.signature-broken";
/// An e-invoice that misses mandatory data (date, number, document type...).
public const string Invalid = "incoming.invalid";
}
///
/// 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).
///
public interface IIncomingInvoiceRules
{
/// The code of the document type that registers an invoice of this type of the format.
/// The type in the file (TD01...).
/// The document type code, or none when the type is not registered as a purchase (a self-invoice).
string? DocumentTypeCode(string formatType);
}