feat: document totals, VAT summary, stamp duty, withholding and installments (#48)
The engine now computes the totals of every document it saves: line amounts with cascaded discounts, footer discount on goods, VAT summary per tax code with the tax on the group total, prices with VAT split back once per group, split payment and cash-basis exigibility, stamp duty above the threshold of the fiscal pack, social security contribution, withholding (also on part of the amount) and ENASARCO taken off the amount to pay. Installments come from the payment terms with end of month, fixed day, the customer's excluded months and VAT on the first installment. The figures live in two shared tables keyed by family (ADR 0033) and are rewritten in the same transaction as the document. DocumentEditor serves the screens: radars, values proposed by party and item, totals preview without saving, linked documents and open documents to fulfil. The copy service can merge several documents of one party, the base of deferred invoicing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ddd2273f7e
commit
f12848865d
21 files changed
+1916
-31
No files matched your search
@@ -18,6 +18,9 @@ public sealed class DocumentRecord
|
||||
|
||||
/// <summary>The lines, in order.</summary>
|
||||
public List<DocumentLine> Lines { get; init; } = [];
|
||||
|
||||
/// <summary>The tax summary and installments written by the last save.</summary>
|
||||
public DocumentFigures Figures { get; init; } = DocumentFigures.Empty;
|
||||
}
|
||||
|
||||
/// <summary>The error codes of the document engine; the interface translates them (<c>Documents.json</c>).</summary>
|
||||
@@ -76,6 +79,12 @@ public static class DocumentErrors
|
||||
|
||||
/// <summary>A closed or cancelled document cannot be fulfilled.</summary>
|
||||
public const string CopySourceNotOpen = "document.copy.source-not-open";
|
||||
|
||||
/// <summary>Error: documents of different parties cannot go into one document.</summary>
|
||||
public const string CopyParties = "document.copy.parties";
|
||||
|
||||
/// <summary>Error: a discount formula that cannot be read.</summary>
|
||||
public const string LineDiscountInvalid = "document.line.discount-invalid";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,6 +98,7 @@ public sealed class DocumentEngine
|
||||
private readonly PermissionService _permissions;
|
||||
private readonly AuditTrail _audit;
|
||||
private readonly FiscalYearService _fiscal;
|
||||
private readonly CompanyFiscalContext _packs;
|
||||
private readonly TimeProvider _clock;
|
||||
|
||||
/// <summary>Creates the engine.</summary>
|
||||
@@ -96,9 +106,12 @@ public sealed class DocumentEngine
|
||||
/// <param name="permissions">The permissions.</param>
|
||||
/// <param name="audit">The history.</param>
|
||||
/// <param name="fiscal">The fiscal calendar (open periods).</param>
|
||||
/// <param name="packs">The fiscal pack of each company (tax rounding, stamp duty).</param>
|
||||
/// <param name="clock">The clock.</param>
|
||||
public DocumentEngine(IUnitOfWorkFactory factory, PermissionService permissions, AuditTrail audit, FiscalYearService fiscal, TimeProvider clock)
|
||||
public DocumentEngine(IUnitOfWorkFactory factory, PermissionService permissions, AuditTrail audit, FiscalYearService fiscal, CompanyFiscalContext packs, TimeProvider clock)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(packs);
|
||||
_packs = packs;
|
||||
ArgumentNullException.ThrowIfNull(factory);
|
||||
ArgumentNullException.ThrowIfNull(permissions);
|
||||
ArgumentNullException.ThrowIfNull(audit);
|
||||
@@ -194,6 +207,7 @@ public sealed class DocumentEngine
|
||||
ArgumentNullException.ThrowIfNull(record);
|
||||
var company = Company(actor, companyCode);
|
||||
var store = DocumentStores.Of(record.Family);
|
||||
var pack = await _packs.PackOfAsync(company, cancellationToken).ConfigureAwait(false);
|
||||
long id;
|
||||
await using (var uow = await _factory.CreateAsync(DatabaseScope.Company(company), cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
@@ -221,7 +235,15 @@ public sealed class DocumentEngine
|
||||
throw new ValidationFailedException(errors);
|
||||
}
|
||||
|
||||
if (before is not null && definition.Kind == DocumentKind.Quotation && DocumentStateMachine.IsInForce(header.Status))
|
||||
{
|
||||
// A confirmed quotation that changes is a new revision of the same offer.
|
||||
header.Revision = before.Header.Revision + 1;
|
||||
}
|
||||
|
||||
var figures = await DocumentCalculation.ApplyAsync(uow, pack.TaxEngine, definition.Behavior, record, cancellationToken).ConfigureAwait(false);
|
||||
id = await WriteAsync(uow, store, record, before, actor.UserName, cancellationToken).ConfigureAwait(false);
|
||||
await DocumentCalculation.WriteAsync(uow, record.Family, id, figures, cancellationToken).ConfigureAwait(false);
|
||||
if (before is not null && DocumentStateMachine.IsInForce(header.Status))
|
||||
{
|
||||
await RefreshFulfillmentAsync(uow, record.Family, id, actor.UserName, cancellationToken).ConfigureAwait(false);
|
||||
@@ -435,7 +457,13 @@ public sealed class DocumentEngine
|
||||
return null;
|
||||
}
|
||||
|
||||
return new DocumentRecord { Family = family, Header = header, Lines = await store.LinesAsync(uow, id, cancellationToken).ConfigureAwait(false) };
|
||||
return new DocumentRecord
|
||||
{
|
||||
Family = family,
|
||||
Header = header,
|
||||
Lines = await store.LinesAsync(uow, id, cancellationToken).ConfigureAwait(false),
|
||||
Figures = await DocumentCalculation.ReadAsync(uow, family, id, cancellationToken).ConfigureAwait(false),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Checks a document against its definition; <paramref name="before"/> is the stored one.</summary>
|
||||
@@ -487,6 +515,11 @@ public sealed class DocumentEngine
|
||||
errors.Add(field + nameof(DocumentLine.Description) + ":" + DocumentErrors.LineDescriptionRequired);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(line.DiscountFormula) && !DiscountFormula.TryParse(line.DiscountFormula, out _))
|
||||
{
|
||||
errors.Add(field + nameof(DocumentLine.DiscountFormula) + ":" + DocumentErrors.LineDiscountInvalid);
|
||||
}
|
||||
|
||||
if (line.Kind == DocumentLineKind.Item && line.ItemId is null)
|
||||
{
|
||||
errors.Add(field + nameof(DocumentLine.ItemId) + ":" + DocumentErrors.LineItemRequired);
|
||||
@@ -710,6 +743,9 @@ public sealed class DocumentEngine
|
||||
header.Posted = stored.Posted;
|
||||
header.GlEntryId = stored.GlEntryId;
|
||||
header.InstallmentsGenerated = stored.InstallmentsGenerated;
|
||||
header.Printed = stored.Printed;
|
||||
header.Sent = stored.Sent;
|
||||
header.Revision = stored.Revision;
|
||||
header.IsDemo = stored.IsDemo;
|
||||
}
|
||||
|
||||
@@ -728,6 +764,9 @@ public sealed class DocumentEngine
|
||||
header.Posted = false;
|
||||
header.GlEntryId = null;
|
||||
header.InstallmentsGenerated = false;
|
||||
header.Printed = false;
|
||||
header.Sent = false;
|
||||
header.Revision = 0;
|
||||
}
|
||||
|
||||
private static string Company(Actor actor, string companyCode)
|
||||
|
||||
Reference in new issue
Block a user