feat: document engine with kinds, states, atomic numbering and fulfilment (#47)

One engine for sales, purchases and warehouse documents (ADR 0032). Each document
type now carries a kind that decides its family of tables, life cycle, effects,
accepted lines, the kinds it fulfils and its permissions, so company-defined types
work without code.

- Six families of header and line tables plus document_links; document_types gets
  document_kind (seeded types filled by the migration and the Italian template),
  document_numerators gets last_date.
- DocumentStateMachine with the order, invoice and simple life cycles and the
  actions each state admits; issued invoices are immutable and are cancelled with
  a reason only before the file reaches the exchange system.
- DocumentNumberingService numbers at confirmation inside the transaction under
  the numerator lock (advisory lock on PostgreSQL, write lock on SQLite): no
  duplicates, a rollback gives the number back, out-of-sequence dates refused on
  gap-free numerators, the new year's numerator created on first use, gap report.
- DocumentEngine, the generic DocumentService<THeader, TLine> and
  DocumentCopyService: save, confirm or issue, cancel, close, reopen, delete,
  duplicate and transform; links are recorded at confirmation and removed at
  cancellation, and FulfillmentCalculator recomputes quantities and states.
- Tests: rules in Core, concurrent numbering on SQLite and PostgreSQL (data tests,
  run on PostgreSQL 18 locally), order to delivery notes to deferred invoice on a
  real instance. Wiki (Vendite, Tabelle azienda), CHANGELOG, STATUS, DATA-MODEL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 15:30:24 +01:00
1 parent fa9304834a
commit ddd2273f7e
33 files changed
+4081 -37

No files matched your search

@@ -148,6 +148,11 @@ internal static class KhaemhatServices
services.AddSingleton<Khaemhat.Application.Items.ItemService>();
services.AddSingleton<Khaemhat.Application.Items.PriceListService>();
// The document engine (ADR 0032): one engine, the copies, a typed service per family.
services.AddSingleton<Khaemhat.Application.Documents.DocumentEngine>();
services.AddSingleton<Khaemhat.Application.Documents.DocumentCopyService>();
services.AddSingleton(typeof(Khaemhat.Application.Documents.DocumentService<,>));
// Guided import and export (ADR 0031): the entities in the order of the screen.
services.AddSingleton<Khaemhat.Application.ImportExport.IImportTarget>(sp => ActivatorUtilities.CreateInstance<Khaemhat.Application.ImportExport.PartiesImportTarget>(
sp, Khaemhat.Core.Parties.PartyKind.Customer));
@@ -0,0 +1,48 @@
{
"document": {
"not-found": "Il documento non esiste più.",
"action": {
"not-allowed": "Nello stato attuale del documento questa operazione non è ammessa."
},
"type": {
"missing": "Il tipo documento non esiste.",
"no-kind": "Il tipo documento non ha un genere: sceglilo in Tabelle azienda, Tipi documento.",
"disabled": "Il tipo documento è disattivato.",
"family": "Questo tipo documento appartiene a un'altra famiglia di documenti.",
"locked": "Il tipo di un documento già salvato non si può cambiare."
},
"party": {
"required": "Indica il cliente o il fornitore."
},
"date": {
"required": "Indica la data del documento.",
"numbered": "La data di un documento numerato non si può cambiare."
},
"lines": {
"required": "Il documento non ha righe."
},
"line": {
"kind-not-allowed": "Questo tipo di riga non è ammesso in questo documento.",
"description-required": "Ogni riga ha bisogno di una descrizione.",
"item-required": "Una riga articolo ha bisogno dell'articolo.",
"below-fulfilled": "La quantità non può scendere sotto quella già evasa.",
"fulfilled-removed": "Una riga già evasa da altri documenti non si può togliere.",
"source-invalid": "La riga d'origine non esiste o non può essere evasa da questo documento."
},
"cancel": {
"reason-required": "Per annullare una fattura indica il motivo.",
"has-followers": "Altri documenti evadono questo: annulla prima quelli."
},
"copy": {
"direction": "Si trasforma solo in un documento della stessa direzione (vendita o acquisto).",
"nothing-left": "Non c'è più nulla da evadere.",
"source-not-open": "Il documento non è confermato o è chiuso o annullato: non si può evadere."
},
"number": {
"date-before-last": "La data è anteriore a quella dell'ultimo documento numerato: con questo numeratore non sono ammessi documenti fuori sequenza.",
"numerator-missing": "Il numeratore del tipo documento non esiste.",
"numerator-disabled": "Il numeratore del tipo documento è disattivato.",
"too-long": "Il numero formattato supera i 30 caratteri: accorcia il formato del numeratore."
}
}
}
@@ -255,6 +255,7 @@
"IsDefault": "Predefinito",
"Symbol": "Simbolo",
"Decimals": "Decimali",
"DocumentKind": "Genere",
"NumeratorId": "Numeratore",
"InventoryReasonId": "Causale di magazzino",
"EInvoiceType": "Tipo TD",
@@ -266,6 +267,7 @@
"Prefix": "Prefisso",
"Format": "Formato",
"LastNumber": "Ultimo numero",
"LastDate": "Data dell'ultimo numero",
"GapsAllowed": "Ammessi buchi di numerazione",
"CurrencyCode": "Valuta",
"VatIncluded": "IVA inclusa",
@@ -353,6 +355,25 @@
"Journal": "Prima nota",
"Other": "Altro"
},
"DocumentKind": {
"Quotation": "Preventivo",
"SalesOrder": "Ordine cliente",
"DeliveryNote": "DDT di vendita",
"ImmediateInvoice": "Fattura immediata",
"AccompanyingInvoice": "Fattura accompagnatoria",
"DeferredInvoice": "Fattura differita",
"DownPaymentInvoice": "Fattura di acconto",
"CreditNote": "Nota di credito",
"DebitNote": "Nota di debito",
"Fee": "Parcella",
"ProForma": "Pro forma",
"SelfInvoice": "Autofattura o integrazione",
"PurchaseOrder": "Ordine fornitore",
"PurchaseDeliveryNote": "DDT fornitore",
"PurchaseInvoice": "Fattura fornitore",
"PurchaseCreditNote": "Nota di credito fornitore",
"InventoryEntry": "Movimento di magazzino"
},
"StockEffect": {
"None": "Nessuno",
"Load": "Carico",
@@ -446,6 +467,9 @@
"numerator": {
"format": "Il formato deve contenere il segnaposto del numero e solo segnaposto noti (anno, prefisso, sezionale)."
},
"document-type": {
"kind-required": "Scegli il genere: dice come si comportano i documenti di questo tipo."
},
"year": {
"range": "Anno fuori intervallo."
},
+3 -1
View File
@@ -194,6 +194,7 @@ public static class TableScreens
Fields =
[
new(nameof(DocumentType.Kind), TableFieldKind.Choice) { Required = true, InGrid = true, Width = "170px" },
new(nameof(DocumentType.DocumentKind), TableFieldKind.Choice) { Required = true, InGrid = true, Width = "190px" },
new(nameof(DocumentType.NumeratorId), TableFieldKind.Lookup) { Lookup = typeof(DocumentNumerator), InGrid = true, Width = "120px" },
new(nameof(DocumentType.InventoryReasonId), TableFieldKind.Lookup) { Lookup = typeof(InventoryReason) },
new(nameof(DocumentType.EInvoiceType), TableFieldKind.CodeList) { CodeList = EInvoice(f => f.DocumentTypes), InGrid = true, Width = "90px" },
@@ -212,7 +213,8 @@ public static class TableScreens
new(nameof(DocumentNumerator.Prefix), TableFieldKind.Code) { MaxLength = 10, Span = 4 },
new(nameof(DocumentNumerator.Format)) { Required = true, Span = 8, InGrid = true, Width = "180px" },
new(nameof(DocumentNumerator.LastNumber), TableFieldKind.Number) { Min = 0, InGrid = true, Width = "100px", Span = 4 },
new(nameof(DocumentNumerator.GapsAllowed), TableFieldKind.Bool) { Span = 8 },
new(nameof(DocumentNumerator.LastDate), TableFieldKind.Date) { Span = 4 },
new(nameof(DocumentNumerator.GapsAllowed), TableFieldKind.Bool) { Span = 4 },
],
},
new("listini", "price-lists", typeof(PriceList), "tag", TableGroup.Sales)