feat: purchase and warehouse documents on the document screens (#55 #56 #58)

The document radars and records now serve /acquisti and /magazzino too:
supplier orders, receipts, supplier invoices and credit notes, self-invoices
and warehouse movements, with the supplier in place of the customer, the
supplier's number and date, the protocol and VAT date of registered invoices,
and reason and warehouses of a movement. Supplier invoices take their
protocol from the purchase numbering, refuse a second document with the same
supplier number in the same year and show price differences against their
order or receipt. The Italian pack advises the self-invoice a received
invoice needs (TD16-TD19, TD28) with its deadline, and the purchase service
drafts it with the ordinary rate. A credit note of an invoice now moves its
goods back instead of being skipped as already moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 21:25:25 +01:00
1 parent eeabb12f96
commit f8b72f4d17
21 files changed
+823 -58

No files matched your search

@@ -90,6 +90,15 @@ public static class DocumentErrors
/// <summary>Error: a line of an invoice without its VAT code.</summary>
public const string LineTaxRequired = "document.line.tax-required";
/// <summary>A supplier invoice needs the number and date the supplier gave it.</summary>
public const string SupplierNumberRequired = "document.purchase.number-required";
/// <summary>The same supplier already has a document of the kind with that number in that year.</summary>
public const string SupplierDuplicate = "document.purchase.duplicate";
/// <summary>The VAT date comes before the date of the supplier document.</summary>
public const string VatDateBefore = "document.purchase.vat-date-before";
}
/// <summary>
@@ -241,6 +250,7 @@ public sealed partial class DocumentEngine
}
var errors = Check(definition, record, before);
await CheckSupplierDocumentAsync(uow, record, confirming: false, errors, cancellationToken).ConfigureAwait(false);
if (errors.Count > 0)
{
throw new ValidationFailedException(errors);
@@ -302,6 +312,8 @@ public sealed partial class DocumentEngine
errors.Add("Lines:" + DocumentErrors.LinesRequired);
}
await CheckSupplierDocumentAsync(uow, record, confirming: true, errors, cancellationToken).ConfigureAwait(false);
if (definition.Behavior.Has(DocumentEffects.Ledger))
{
// A document that goes to the VAT registers needs the VAT of every line with an amount.
@@ -331,6 +343,13 @@ public sealed partial class DocumentEngine
}
header.Year = header.DocumentDate.Year;
if (IsRegistered(header.Kind))
{
// A supplier invoice takes its protocol from the numbering of the purchase register (§11.5).
header.VatProtocol = header.Number;
header.VatDate ??= header.DocumentDate;
}
header.Status = next;
header.ConfirmedAt = _clock.GetUtcNow().UtcDateTime;
header.ConfirmedBy = actor.UserName;
@@ -839,6 +858,52 @@ public sealed partial class DocumentEngine
header.Revision = 0;
}
// Supplier invoices and credit notes go to the purchase register with a protocol and a VAT date.
private static bool IsRegistered(DocumentKind kind) => kind is DocumentKind.PurchaseInvoice or DocumentKind.PurchaseCreditNote;
// The rules of a supplier document (§11.5): its own number and date, the VAT date not before it, no duplicate of
// the same supplier, kind, number and year among the documents in force or in draft.
private static async Task CheckSupplierDocumentAsync(IUnitOfWork uow, DocumentRecord record, bool confirming, List<string> errors, CancellationToken cancellationToken)
{
var header = record.Header;
if (!IsRegistered(header.Kind))
{
return;
}
var number = header.ExternalReference?.Trim();
if (confirming && string.IsNullOrEmpty(number))
{
errors.Add(nameof(DocumentHeader.ExternalReference) + ":" + DocumentErrors.SupplierNumberRequired);
}
if (confirming && header.ExternalDate is null)
{
errors.Add(nameof(DocumentHeader.ExternalDate) + ":" + DocumentErrors.SupplierNumberRequired);
}
if (header.VatDate is { } vatDate && header.ExternalDate is { } supplierDate && vatDate < supplierDate)
{
errors.Add(nameof(DocumentHeader.VatDate) + ":" + DocumentErrors.VatDateBefore);
}
if (string.IsNullOrEmpty(number) || header.PartyId is not { } party)
{
return;
}
var year = (header.ExternalDate ?? header.DocumentDate).Year;
var kind = header.Kind;
var id = header.Id;
var others = (await uow.Repository<PurchaseDocument, long>().ListAsync(
Query.For<PurchaseDocument>().Where(d => d.PartyId == party && d.Kind == kind && d.ExternalReference == number && d.Id != id && d.Status != DocumentStatus.Cancelled),
cancellationToken).ConfigureAwait(false)).Items;
if (others.Any(d => (d.ExternalDate ?? d.DocumentDate).Year == year))
{
errors.Add(nameof(DocumentHeader.ExternalReference) + ":" + DocumentErrors.SupplierDuplicate);
}
}
private static string Company(Actor actor, string companyCode)
{
ArgumentNullException.ThrowIfNull(actor);