Adds the company tables of the electronic invoice and the services that drive them: the builder turns an issued document into the FatturaPA model, the service generates, checks and sends the file through the channel chosen by the routing rules (with a fallback) and applies the SDI notifications to the e-invoice and its document. The channel settings keep their secrets protected and out of the audit trail; a job reads the PEC mailbox and the intermediaries every 15 minutes, storing supplier invoices in the inbox and acknowledging a message only once all its files are kept. The inbox registers received invoices through the purchase import, and the conservation export packs a period with a SHA-256 index. A rejected invoice can now be corrected and generated again; the courtesy PDF embeds the XML. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
8.2 KiB
C#
124 lines
8.2 KiB
C#
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 is not (DocumentStatus.Draft or DocumentStatus.SdiRejected)))
|
|
{
|
|
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.IsTrue(DocumentStateMachine.IsEditable(Invoice, DocumentStatus.SdiRejected), "A rejected invoice is corrected and sent again.");
|
|
Assert.IsFalse(DocumentStateMachine.Allows(Invoice, DocumentStatus.SdiRejected, DocumentAction.Delete));
|
|
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 };
|
|
}
|