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:
Alby96andClaude Opus 5.5 committed 2026-11-12 15:55:58 +01:00
1 parent ddd2273f7e
commit f12848865d
21 files changed
+1916 -31

No files matched your search

@@ -0,0 +1,110 @@
namespace Khaemhat.Application.Documents;
/// <summary>An installment of payment terms.</summary>
/// <param name="Days">Days from the document date.</param>
/// <param name="EndOfMonth">Move to the end of that month.</param>
/// <param name="FixedDay">A fixed day: of the next month after the end-of-month move, else the next such day.</param>
/// <param name="Percent">The share of the amount, percent.</param>
public sealed record InstallmentRule(int Days, bool EndOfMonth, int? FixedDay, decimal Percent);
/// <summary>Months without due dates (mesi esclusi): a due date in them moves to a day of the next month.</summary>
/// <param name="First">The first month (1-12).</param>
/// <param name="Second">The second month.</param>
/// <param name="Day">The day of the next month.</param>
public sealed record ExcludedMonths(int? First, int? Second, int Day = 10)
{
/// <summary>Whether a month is excluded.</summary>
/// <param name="month">The month.</param>
/// <returns>Whether it is.</returns>
public bool Contains(int month) => First == month || Second == month;
}
/// <summary>A computed installment.</summary>
/// <param name="Number">The order, from 1.</param>
/// <param name="DueDate">The due date.</param>
/// <param name="Amount">The amount.</param>
public sealed record InstallmentDue(int Number, DateOnly DueDate, decimal Amount);
/// <summary>
/// The installments of a document from its payment terms (SPEC §11.3, issue #48): days, end of month, fixed day,
/// excluded months, percentages, all the VAT on the first installment; the rounding goes on the last one.
/// </summary>
public static class InstallmentGenerator
{
/// <summary>Computes the installments.</summary>
/// <param name="date">The document date.</param>
/// <param name="amount">The amount to pay.</param>
/// <param name="tax">The VAT included in <paramref name="amount"/> (for <paramref name="vatOnFirst"/>).</param>
/// <param name="rules">The installments of the terms; none means one installment on the document date.</param>
/// <param name="vatOnFirst">All the VAT is paid with the first installment.</param>
/// <param name="excluded">The months without due dates.</param>
/// <param name="round">Rounding to the cent.</param>
/// <returns>The installments, empty for a zero amount.</returns>
public static IReadOnlyList<InstallmentDue> Generate(
DateOnly date, decimal amount, decimal tax, IReadOnlyList<InstallmentRule> rules, bool vatOnFirst, ExcludedMonths? excluded, Func<decimal, decimal> round)
{
ArgumentNullException.ThrowIfNull(rules);
ArgumentNullException.ThrowIfNull(round);
if (amount == 0m)
{
return [];
}
if (rules.Count == 0)
{
return [new InstallmentDue(1, Move(date, excluded), amount)];
}
var share = vatOnFirst && rules.Count > 1 ? tax : 0m;
var spread = amount - share;
var percents = rules.Sum(r => r.Percent);
var result = new List<InstallmentDue>(rules.Count);
var assigned = 0m;
for (var i = 0; i < rules.Count; i++)
{
var rule = rules[i];
var part = i == rules.Count - 1
? spread - assigned
: round(spread * (percents == 0m ? 1m / rules.Count : rule.Percent / percents));
assigned += part;
result.Add(new InstallmentDue(i + 1, DueDate(date, rule, excluded), part + (i == 0 ? share : 0m)));
}
return result;
}
/// <summary>The due date of an installment.</summary>
/// <param name="date">The document date.</param>
/// <param name="rule">The installment.</param>
/// <param name="excluded">The months without due dates.</param>
/// <returns>The due date.</returns>
public static DateOnly DueDate(DateOnly date, InstallmentRule rule, ExcludedMonths? excluded)
{
ArgumentNullException.ThrowIfNull(rule);
var due = date.AddDays(rule.Days);
if (rule.EndOfMonth)
{
due = new DateOnly(due.Year, due.Month, DateTime.DaysInMonth(due.Year, due.Month));
}
if (rule.FixedDay is { } day and >= 1 and <= 31)
{
due = rule.EndOfMonth || due.Day > day ? Day(due.AddMonths(1), day) : Day(due, day);
}
return Move(due, excluded);
}
private static DateOnly Move(DateOnly due, ExcludedMonths? excluded)
{
for (var guard = 0; excluded is not null && excluded.Contains(due.Month) && guard < 12; guard++)
{
due = Day(due.AddMonths(1), excluded.Day);
}
return due;
}
private static DateOnly Day(DateOnly month, int day) =>
new(month.Year, month.Month, Math.Min(day, DateTime.DaysInMonth(month.Year, month.Month)));
}