using Khaemhat.Application.Documents; using Khaemhat.Core.Documents; using Khaemhat.Core.Tax; using Khaemhat.Fiscal.Italy; namespace Khaemhat.Application.Tests; /// /// Reference cases of the totals (SPEC ยง11.3, issue #48): cascaded discounts, footer discount, expenses, tax summary, /// stamp duty, social security and withholding, split payment, prices with VAT, rounding to the cent, installments. /// [TestClass] public sealed class DocumentTotalsTests { private static readonly ItalianTaxEngine Engine = new(); private static readonly TotalsTax Vat22 = new(1, 22m); private static readonly TotalsTax Exempt = new(2, 0m, "N4", LegalReference: "Esente art. 10 DPR 633/72"); private static readonly TotalsTax Excluded = new(3, 0m, "N1", LegalReference: "Escluso art. 15 DPR 633/72"); private static TotalsLine Item(decimal quantity, decimal price, TotalsTax tax, string? discount = null) => new(DocumentLineKind.Item, quantity, price, discount, tax); [TestMethod] public void Invoice_with_discounts_expenses_and_exempt_lines_carries_the_stamp_duty() { var totals = DocumentTotalsCalculator.Compute( [ Item(3m, 12.35m, Vat22, "10+5"), Item(1m, 100m, Vat22), new(DocumentLineKind.Description, 0m, 0m, null, null), new(DocumentLineKind.Expense, 0m, 10m, null, Vat22), Item(2m, 50m, Exempt), ], new TotalsOptions { StampDutyApplies = true, StampDutyCharged = true }, Engine); CollectionAssert.AreEqual(new[] { 31.68m, 100m, 0m, 10m, 100m }, totals.LineAmounts.ToArray()); Assert.HasCount(2, totals.Summary); Assert.AreEqual(141.68m, totals.Summary[0].Taxable); Assert.AreEqual(31.17m, totals.Summary[0].TaxAmount); Assert.AreEqual("N4", totals.Summary[1].Tax.Nature); Assert.AreEqual(100m, totals.Summary[1].Taxable); Assert.AreEqual(241.68m, totals.Taxable); Assert.AreEqual(31.17m, totals.Tax); Assert.AreEqual(10m, totals.Expenses); Assert.AreEqual(2m, totals.StampDuty); Assert.AreEqual(274.85m, totals.Total); Assert.AreEqual(274.85m, totals.Net); } [TestMethod] public void Stamp_duty_not_charged_stays_out_of_the_total_and_below_the_threshold_is_not_due() { var notCharged = DocumentTotalsCalculator.Compute([Item(1m, 100m, Exempt)], new TotalsOptions { StampDutyApplies = true }, Engine); Assert.AreEqual(2m, notCharged.StampDuty); Assert.AreEqual(100m, notCharged.Total); var below = DocumentTotalsCalculator.Compute([Item(1m, 77.47m, Exempt)], new TotalsOptions { StampDutyApplies = true, StampDutyCharged = true }, Engine); Assert.AreEqual(0m, below.StampDuty); Assert.AreEqual(77.47m, below.Total); } [TestMethod] public void Footer_discount_applies_to_goods_and_not_to_expenses() { var totals = DocumentTotalsCalculator.Compute( [Item(3m, 12.35m, Vat22, "10+5"), Item(1m, 100m, Vat22), new(DocumentLineKind.Expense, 1m, 10m, null, Vat22)], new TotalsOptions { FooterDiscount = "5" }, Engine); Assert.AreEqual(31.68m, totals.LineAmounts[0], "the line keeps its own amount"); Assert.AreEqual(6.59m, totals.FooterDiscountAmount); Assert.AreEqual(135.09m, totals.Taxable); Assert.AreEqual(29.72m, totals.Tax); } [TestMethod] public void Fee_with_social_security_withheld_and_withholding_tax() { var totals = DocumentTotalsCalculator.Compute( [new(DocumentLineKind.Free, 1m, 1000m, null, Vat22), new(DocumentLineKind.Expense, 1m, 50m, null, Excluded, WithholdingExcluded: true)], new TotalsOptions { SocialSecurityRate = 4m, SocialSecurityWithheld = true, WithholdingRate = 20m }, Engine); Assert.AreEqual(40m, totals.SocialSecurity); Assert.AreEqual(1040m, totals.Summary[0].Taxable); Assert.AreEqual(228.80m, totals.Tax); Assert.AreEqual(1090m, totals.Taxable); Assert.AreEqual(1318.80m, totals.Total); Assert.AreEqual(208m, totals.Withholding); Assert.AreEqual(1110.80m, totals.Net); } [TestMethod] public void Agent_commissions_withhold_on_half_and_enasarco() { var totals = DocumentTotalsCalculator.Compute( [new(DocumentLineKind.Free, 1m, 2000m, null, Vat22)], new TotalsOptions { WithholdingRate = 23m, WithholdingBasePercent = 50m, EnasarcoRate = 8.5m }, Engine); Assert.AreEqual(230m, totals.Withholding); Assert.AreEqual(170m, totals.Enasarco); Assert.AreEqual(2440m - 230m - 170m, totals.Net); } [TestMethod] public void Split_payment_leaves_the_vat_out_of_the_amount_to_pay() { var totals = DocumentTotalsCalculator.Compute([Item(1m, 1000m, Vat22)], new TotalsOptions { SplitPayment = true }, Engine); Assert.AreEqual(TaxExigibility.SplitPayment, totals.Summary[0].Tax.Exigibility); Assert.AreEqual(1220m, totals.Total); Assert.AreEqual(220m, totals.SplitPaymentTax); Assert.AreEqual(1000m, totals.Net); } [TestMethod] public void Prices_with_vat_are_split_back_per_group_and_discount_lines_subtract() { var gross = DocumentTotalsCalculator.Compute([Item(1m, 122m, Vat22), Item(2m, 10.99m, Vat22)], new TotalsOptions { PricesIncludeTax = true }, Engine); Assert.AreEqual(118.02m, gross.Taxable); Assert.AreEqual(25.96m, gross.Tax); Assert.AreEqual(143.98m, gross.Total); var discounted = DocumentTotalsCalculator.Compute([Item(1m, 100m, Vat22), new(DocumentLineKind.Discount, 0m, 10m, null, Vat22)], new TotalsOptions(), Engine); Assert.AreEqual(-10m, discounted.LineAmounts[1]); Assert.AreEqual(90m, discounted.Taxable); Assert.AreEqual(19.80m, discounted.Tax); } [TestMethod] public void Halves_round_away_from_zero_on_lines_and_on_the_tax_of_the_group() { var totals = DocumentTotalsCalculator.Compute([Item(1m, 0.125m, Vat22), Item(1m, 2.125m, Vat22)], new TotalsOptions(), Engine); CollectionAssert.AreEqual(new[] { 0.13m, 2.13m }, totals.LineAmounts.ToArray()); Assert.AreEqual(2.26m, totals.Taxable); Assert.AreEqual(0.50m, totals.Tax, "22% of 2.26 is 0.4972, rounded once on the group"); } [TestMethod] public void Installments_move_to_end_of_month_and_carry_the_vat_on_the_first() { var dues = InstallmentGenerator.Generate(new DateOnly(2026, 1, 15), 1220m, 220m, [new(30, true, null, 50m), new(60, true, null, 50m)], vatOnFirst: true, null, Engine.Round); Assert.HasCount(2, dues); Assert.AreEqual(new DateOnly(2026, 2, 28), dues[0].DueDate); Assert.AreEqual(720m, dues[0].Amount); Assert.AreEqual(new DateOnly(2026, 3, 31), dues[1].DueDate); Assert.AreEqual(500m, dues[1].Amount); } [TestMethod] public void Installments_with_fixed_day_excluded_months_and_rounding_on_the_last() { Assert.AreEqual(new DateOnly(2026, 4, 10), InstallmentGenerator.DueDate(new DateOnly(2026, 1, 15), new(60, true, 10, 100m), null)); Assert.AreEqual(new DateOnly(2026, 2, 5), InstallmentGenerator.DueDate(new DateOnly(2026, 1, 20), new(0, false, 5, 100m), null)); Assert.AreEqual(new DateOnly(2026, 9, 10), InstallmentGenerator.DueDate(new DateOnly(2026, 7, 1), new(60, true, null, 100m), new ExcludedMonths(8, 12))); var thirds = InstallmentGenerator.Generate(new DateOnly(2026, 1, 1), 100m, 0m, [new(30, false, null, 33.33m), new(60, false, null, 33.33m), new(90, false, null, 33.34m)], false, null, Engine.Round); CollectionAssert.AreEqual(new[] { 33.33m, 33.33m, 33.34m }, thirds.Select(d => d.Amount).ToArray()); var single = InstallmentGenerator.Generate(new DateOnly(2026, 8, 3), 50m, 0m, [], false, new ExcludedMonths(8, null, 15), Engine.Round); Assert.AreEqual(new DateOnly(2026, 9, 15), single[0].DueDate); Assert.IsEmpty(InstallmentGenerator.Generate(new DateOnly(2026, 1, 1), 0m, 0m, [], false, null, Engine.Round)); } }