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:
1 parent
fa9304834a
commit
ddd2273f7e
33 files changed
+4081
-37
No files matched your search
@@ -0,0 +1,121 @@
|
||||
using Khaemhat.Core.Documents;
|
||||
|
||||
namespace Khaemhat.Core.Tests.Documents;
|
||||
|
||||
/// <summary>The declarative rules of the document engine (issue #47): behaviours, states, fulfilment.</summary>
|
||||
[TestClass]
|
||||
public sealed class DocumentRulesTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void EveryKind_HasABehaviour_AndFulfilsOnlyKindsOfTheSameDirection()
|
||||
{
|
||||
Assert.HasCount(Enum.GetValues<DocumentKind>().Length, DocumentBehaviors.All);
|
||||
foreach (var behavior in DocumentBehaviors.All)
|
||||
{
|
||||
Assert.IsNotEmpty(behavior.Lines, behavior.Kind.ToString());
|
||||
foreach (var source in behavior.Sources)
|
||||
{
|
||||
Assert.AreEqual(behavior.Direction, DocumentBehaviors.Of(source).Direction, $"{behavior.Kind} fulfils {source}");
|
||||
}
|
||||
|
||||
// Invoices are the only documents that reach the exchange system.
|
||||
Assert.AreEqual(behavior.Lifecycle == DocumentLifecycle.Invoice, behavior.Has(DocumentEffects.EInvoice), behavior.Kind.ToString());
|
||||
}
|
||||
|
||||
Assert.AreEqual(DocumentFamily.SaleDocument, DocumentBehaviors.Of(DocumentKind.DeliveryNote).Family);
|
||||
Assert.IsTrue(DocumentBehaviors.Of(DocumentKind.DeferredInvoice).Fulfils(DocumentKind.DeliveryNote));
|
||||
Assert.IsFalse(DocumentBehaviors.Of(DocumentKind.DeferredInvoice).Fulfils(DocumentKind.SalesOrder));
|
||||
Assert.IsTrue(DocumentBehaviors.Of(DocumentKind.Quotation).NeedsParty);
|
||||
Assert.IsFalse(DocumentBehaviors.Of(DocumentKind.InventoryEntry).NeedsParty);
|
||||
CollectionAssert.AreEquivalent(
|
||||
new[] { DocumentKind.DeliveryNote, DocumentKind.ImmediateInvoice, DocumentKind.AccompanyingInvoice, DocumentKind.DeferredInvoice, DocumentKind.DownPaymentInvoice,
|
||||
DocumentKind.CreditNote, DocumentKind.DebitNote, DocumentKind.Fee, DocumentKind.ProForma, DocumentKind.SelfInvoice },
|
||||
DocumentBehaviors.KindsOf(DocumentFamily.SaleDocument).ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrderLifecycle_MovesThroughItsStates_AndRefusesTheRest()
|
||||
{
|
||||
const DocumentLifecycle Order = DocumentLifecycle.Order;
|
||||
Assert.AreEqual(DocumentStatus.Confirmed, DocumentStateMachine.Apply(Order, DocumentStatus.Draft, DocumentAction.Confirm));
|
||||
Assert.AreEqual(DocumentStatus.Closed, DocumentStateMachine.Apply(Order, DocumentStatus.PartiallyFulfilled, DocumentAction.Close));
|
||||
Assert.AreEqual(DocumentStatus.Confirmed, DocumentStateMachine.Apply(Order, DocumentStatus.Closed, DocumentAction.Reopen));
|
||||
Assert.AreEqual(DocumentStatus.Cancelled, DocumentStateMachine.Apply(Order, DocumentStatus.Confirmed, DocumentAction.Cancel));
|
||||
Assert.IsTrue(DocumentStateMachine.IsEditable(Order, DocumentStatus.PartiallyFulfilled));
|
||||
Assert.IsFalse(DocumentStateMachine.IsEditable(Order, DocumentStatus.Fulfilled));
|
||||
|
||||
var refused = Assert.ThrowsExactly<InvalidDocumentActionException>(() => DocumentStateMachine.Apply(Order, DocumentStatus.PartiallyFulfilled, DocumentAction.Cancel));
|
||||
Assert.AreEqual(DocumentAction.Cancel, refused.Action);
|
||||
Assert.ThrowsExactly<InvalidDocumentActionException>(() => DocumentStateMachine.Apply(Order, DocumentStatus.Confirmed, DocumentAction.Delete));
|
||||
Assert.ThrowsExactly<InvalidDocumentActionException>(() => DocumentStateMachine.Apply(Order, DocumentStatus.Draft, DocumentAction.Issue));
|
||||
Assert.ThrowsExactly<InvalidDocumentActionException>(() => DocumentStateMachine.Apply(Order, DocumentStatus.Cancelled, DocumentAction.Reopen));
|
||||
Assert.ThrowsExactly<InvalidDocumentActionException>(() => DocumentStateMachine.Apply(Order, DocumentStatus.Issued, DocumentAction.Copy), "Not a state of the cycle.");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InvoiceLifecycle_IsUnchangeableOnceIssued_AndCancelledOnlyBeforeTheExchangeSystemHasIt()
|
||||
{
|
||||
const DocumentLifecycle Invoice = DocumentLifecycle.Invoice;
|
||||
var status = DocumentStatus.Draft;
|
||||
foreach (var action in new[] { DocumentAction.Issue, DocumentAction.GenerateXml, DocumentAction.Send, DocumentAction.MarkDelivered })
|
||||
{
|
||||
status = DocumentStateMachine.Apply(Invoice, status, action);
|
||||
}
|
||||
|
||||
Assert.AreEqual(DocumentStatus.SdiDelivered, status);
|
||||
CollectionAssert.AreEqual(new[] { DocumentAction.Copy }, DocumentStateMachine.Actions(Invoice, DocumentStatus.SdiDelivered).ToArray());
|
||||
foreach (var issued in DocumentStateMachine.States(Invoice).Where(s => s != DocumentStatus.Draft))
|
||||
{
|
||||
Assert.IsFalse(DocumentStateMachine.IsEditable(Invoice, issued), issued.ToString());
|
||||
Assert.IsFalse(DocumentStateMachine.Allows(Invoice, issued, DocumentAction.Delete), issued.ToString());
|
||||
}
|
||||
|
||||
Assert.IsTrue(DocumentStateMachine.Allows(Invoice, DocumentStatus.SdiGenerated, DocumentAction.Cancel));
|
||||
Assert.IsTrue(DocumentStateMachine.Allows(Invoice, DocumentStatus.SdiRejected, DocumentAction.Cancel));
|
||||
Assert.IsFalse(DocumentStateMachine.Allows(Invoice, DocumentStatus.SdiSent, DocumentAction.Cancel));
|
||||
Assert.IsFalse(DocumentStateMachine.Allows(Invoice, DocumentStatus.Draft, DocumentAction.Confirm), "Invoices are issued, not confirmed.");
|
||||
Assert.AreEqual(DocumentStatus.SdiGenerated, DocumentStateMachine.Apply(Invoice, DocumentStatus.SdiRejected, DocumentAction.GenerateXml));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SimpleLifecycle_HasDraftConfirmedCancelled()
|
||||
{
|
||||
CollectionAssert.AreEqual(
|
||||
new[] { DocumentStatus.Draft, DocumentStatus.Confirmed, DocumentStatus.Cancelled }, DocumentStateMachine.States(DocumentLifecycle.Simple).ToArray());
|
||||
Assert.ThrowsExactly<InvalidDocumentActionException>(() => DocumentStateMachine.Apply(DocumentLifecycle.Simple, DocumentStatus.Confirmed, DocumentAction.Close));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Fulfilment_SumsTheLinks_AndClosedLinesCountAsFulfilled()
|
||||
{
|
||||
var lines = new List<DocumentLine>
|
||||
{
|
||||
new SaleOrderLine { Id = 1, Kind = DocumentLineKind.Item, Quantity = 10m },
|
||||
new SaleOrderLine { Id = 2, Kind = DocumentLineKind.Free, Quantity = 4m },
|
||||
new SaleOrderLine { Id = 3, Kind = DocumentLineKind.Description },
|
||||
new SaleOrderLine { Id = 4, Kind = DocumentLineKind.Expense, Quantity = 1m },
|
||||
};
|
||||
Assert.AreEqual(FulfillmentState.None, FulfillmentCalculator.StateOf(lines));
|
||||
|
||||
var changed = FulfillmentCalculator.Apply(lines, [Link(1, 6m), Link(1, 1m)]);
|
||||
Assert.HasCount(1, changed);
|
||||
Assert.AreEqual(7m, lines[0].FulfilledQuantity);
|
||||
Assert.AreEqual(3m, lines[0].Residual);
|
||||
Assert.AreEqual(0m, lines[3].Residual, "Expenses are not fulfilled.");
|
||||
Assert.AreEqual(FulfillmentState.Partial, FulfillmentCalculator.StateOf(lines));
|
||||
|
||||
FulfillmentCalculator.Apply(lines, [Link(1, 12m), Link(2, 1m)]);
|
||||
Assert.AreEqual(0m, lines[0].Residual, "More than ordered leaves nothing to fulfil.");
|
||||
Assert.AreEqual(FulfillmentState.Partial, FulfillmentCalculator.StateOf(lines));
|
||||
lines[1].FulfillmentClosed = true;
|
||||
Assert.AreEqual(FulfillmentState.Fulfilled, FulfillmentCalculator.StateOf(lines));
|
||||
|
||||
Assert.AreEqual(DocumentStatus.Fulfilled, DocumentStateMachine.AfterFulfillment(DocumentLifecycle.Order, DocumentStatus.Confirmed, FulfillmentState.Fulfilled));
|
||||
Assert.AreEqual(DocumentStatus.Confirmed, DocumentStateMachine.AfterFulfillment(DocumentLifecycle.Order, DocumentStatus.PartiallyFulfilled, FulfillmentState.None));
|
||||
Assert.AreEqual(DocumentStatus.Closed, DocumentStateMachine.AfterFulfillment(DocumentLifecycle.Order, DocumentStatus.Closed, FulfillmentState.Fulfilled));
|
||||
Assert.AreEqual(DocumentStatus.Issued, DocumentStateMachine.AfterFulfillment(DocumentLifecycle.Invoice, DocumentStatus.Issued, FulfillmentState.Fulfilled));
|
||||
}
|
||||
|
||||
private static DocumentLink Link(long sourceLine, decimal quantity) =>
|
||||
new() { SourceFamily = DocumentFamily.SaleOrder, SourceLineId = sourceLine, Quantity = quantity };
|
||||
}
|
||||
Reference in new issue
Block a user