M7 adds the double-entry ledger so that the documents of M5 and M6 reach the accounts without retyping, and the Italian VAT obligations can be prepared from the same data. - Chart of accounts on three levels with the Italian "PMI standard" model in the fiscal pack, journal templates with proposed lines, VAT registers; the chart import now writes the accounts and exports them (#62, ADR 0036). - Journal entries: draft, posted, final with the libro giornale number, reversal; recurring entries with the nightly ledger.recurring job (#63). - Documents post their entry inside the document transaction (PostingEngine, LedgerPoster) with VAT lines, open items and withholdings; a change rewrites the same entry, a cancellation removes or reverses it (#64, ADR 0037). - VAT registers with final print, monthly or quarterly settlement with pro rata, interest, minimum and advance, confirmation with the LIQ entry, period lock and F24 line, LIPE file and F24 data (#65). - Operated and suffered withholdings, CU data, stamp duty by quarter with the 5,000 euro threshold, F24 summary with its payment entry (#66). - Party ledgers, account ledgers, trial balance and libro giornale (#67). - Demo accounting with the settlements of the past months confirmed and checked by hand in a test; wiki Contabilita, IVA, Fiscalita italiana completed with screenshots; CHANGELOG 0.7.0, STATUS, ADR 0036-0037 (#68). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
300 lines
13 KiB
C#
300 lines
13 KiB
C#
using Khaemhat.Core.Accounting;
|
|
using Khaemhat.Core.Tax;
|
|
|
|
namespace Khaemhat.Core.Tests.Accounting;
|
|
|
|
/// <summary>
|
|
/// The pure rules of the ledger (issues #62-#65): the checks of an entry, the account codes, the posting of each kind of
|
|
/// document, the VAT settlement with pro rata, carried amounts, interest and minimum, and the December advance.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class LedgerRulesTests
|
|
{
|
|
private const long Customer = 1;
|
|
private const long Supplier = 2;
|
|
private const long Revenue = 3;
|
|
private const long OtherRevenue = 4;
|
|
private const long Cost = 5;
|
|
private const long SalesVat = 6;
|
|
private const long PurchaseVat = 7;
|
|
private const long Stamp = 8;
|
|
private const long Fund = 9;
|
|
|
|
private static readonly PostingAccounts SaleAccounts = new(Customer, Revenue, SalesVat, PurchaseVat, Stamp, Fund);
|
|
private static readonly PostingAccounts PurchaseAccounts = new(Supplier, Cost, SalesVat, PurchaseVat, Stamp);
|
|
|
|
private static readonly Dictionary<long, AccountRule> Accounts = new()
|
|
{
|
|
[Customer] = new(Customer, Postable: true, Disabled: false, SubLedger: true),
|
|
[Revenue] = new(Revenue, Postable: true, Disabled: false, SubLedger: false),
|
|
[Cost] = new(Cost, Postable: false, Disabled: false, SubLedger: false),
|
|
[SalesVat] = new(SalesVat, Postable: true, Disabled: true, SubLedger: false),
|
|
};
|
|
|
|
[TestMethod]
|
|
public void Validator_AcceptsABalancedEntry_AndAcceptsUnbalancedDrafts()
|
|
{
|
|
EntryLineCheck[] lines = [new(Customer, 10, 122m, 0m), new(Revenue, null, 0m, 122m)];
|
|
Assert.IsEmpty(JournalEntryValidator.Check("Fattura", lines, 0, false, Accounts, posting: true));
|
|
|
|
EntryLineCheck[] half = [new(Revenue, null, 0m, 100m)];
|
|
Assert.IsEmpty(JournalEntryValidator.Check("Bozza", half, 0, false, Accounts, posting: false));
|
|
CollectionAssert.Contains(JournalEntryValidator.Check("Bozza", half, 0, false, Accounts, posting: true).ToList(), "Lines:" + LedgerErrors.Unbalanced);
|
|
Assert.AreEqual(-100m, JournalEntryValidator.Difference(half));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Validator_ReportsEveryProblemOfTheLines()
|
|
{
|
|
EntryLineCheck[] lines =
|
|
[
|
|
new(null, null, 10m, 0m),
|
|
new(99, null, 10m, 0m),
|
|
new(Cost, null, 10m, 0m),
|
|
new(SalesVat, null, 0m, 10m),
|
|
new(Customer, null, 0m, 10m),
|
|
new(Revenue, null, -5m, 0m),
|
|
new(Revenue, null, 5m, 5m),
|
|
new(Revenue, null, 0m, 0m),
|
|
];
|
|
|
|
var errors = JournalEntryValidator.Check(" ", lines, 1, false, Accounts, posting: true);
|
|
|
|
CollectionAssert.IsSubsetOf(
|
|
new[]
|
|
{
|
|
"Description:" + LedgerErrors.DescriptionRequired,
|
|
"Lines.0.AccountId:" + LedgerErrors.AccountRequired,
|
|
"Lines.1.AccountId:" + LedgerErrors.AccountMissing,
|
|
"Lines.2.AccountId:" + LedgerErrors.AccountNotPostable,
|
|
"Lines.3.AccountId:" + LedgerErrors.AccountDisabled,
|
|
"Lines.4.PartyId:" + LedgerErrors.PartyRequired,
|
|
"Lines.5.Debit:" + LedgerErrors.Negative,
|
|
"Lines.6.Credit:" + LedgerErrors.BothSides,
|
|
"Lines.7.Debit:" + LedgerErrors.AmountRequired,
|
|
"VatRegisterId:" + LedgerErrors.VatRegisterRequired,
|
|
},
|
|
errors.ToList());
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("05", 1)]
|
|
[DataRow("05.01", 2)]
|
|
[DataRow("05.01.001", 3)]
|
|
[DataRow("05.01.001.1", 0)]
|
|
[DataRow("05..001", 0)]
|
|
[DataRow("", 0)]
|
|
[DataRow("05.0-1", 0)]
|
|
public void AccountCodes_HaveUpToThreeLevels(string code, int level) => Assert.AreEqual(level, AccountCodes.LevelOf(code));
|
|
|
|
[TestMethod]
|
|
public void AccountCodes_KnowTheirParent()
|
|
{
|
|
Assert.AreEqual("05.01", AccountCodes.ParentOf("05.01.001"));
|
|
Assert.AreEqual("05", AccountCodes.ParentOf("05.01"));
|
|
Assert.IsNull(AccountCodes.ParentOf("05"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Sale_DebitsTheCustomer_CreditsRevenuesStampAndVat()
|
|
{
|
|
var input = new PostingInput(
|
|
PostingKind.Sale, false, 10,
|
|
[new PostingLineInput(Revenue, 600m), new PostingLineInput(OtherRevenue, 400m), new PostingLineInput(Revenue, 0m)],
|
|
[Tax(1000m, 22m, 220m)],
|
|
Total: 1222m, StampDuty: 2m);
|
|
|
|
var result = PostingEngine.Post(input, SaleAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
Assert.AreEqual(1222m, Line(result, PostingRole.Customer).Debit);
|
|
Assert.AreEqual(10L, Line(result, PostingRole.Customer).PartyId);
|
|
Assert.AreEqual(600m, result.Lines.Single(l => l.AccountId == Revenue).Credit);
|
|
Assert.AreEqual(400m, result.Lines.Single(l => l.AccountId == OtherRevenue).Credit);
|
|
Assert.AreEqual(2m, Line(result, PostingRole.StampDuty).Credit);
|
|
Assert.AreEqual(220m, Line(result, PostingRole.SalesVat).Credit);
|
|
Assert.AreEqual(VatRegisterKind.Sales, result.Vat.Single().RegisterKind);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Sale_GivesTheFooterDiscountToTheLargestLine()
|
|
{
|
|
// Lines for 1000, a footer discount of 10: the summary says 990.
|
|
var input = new PostingInput(PostingKind.Sale, false, 10,
|
|
[new PostingLineInput(Revenue, 700m), new PostingLineInput(OtherRevenue, 300m)], [Tax(990m, 22m, 217.8m)], Total: 1207.8m);
|
|
|
|
var result = PostingEngine.Post(input, SaleAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
Assert.AreEqual(690m, result.Lines.Single(l => l.AccountId == Revenue).Credit);
|
|
Assert.AreEqual(300m, result.Lines.Single(l => l.AccountId == OtherRevenue).Credit);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Sale_WithSplitPayment_TakesTheVatBackFromTheCustomer()
|
|
{
|
|
var input = new PostingInput(PostingKind.Sale, false, 10, [new PostingLineInput(Revenue, 1000m)],
|
|
[Tax(1000m, 22m, 220m) with { Exigibility = TaxExigibility.SplitPayment }], Total: 1220m);
|
|
|
|
var result = PostingEngine.Post(input, SaleAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
var customer = result.Lines.Where(l => l.Role == PostingRole.Customer).Sum(l => l.Debit - l.Credit);
|
|
Assert.AreEqual(1000m, customer, "The public body pays the taxable amount only.");
|
|
Assert.AreEqual(220m, Line(result, PostingRole.SplitPaymentVat).Debit);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Fee_CreditsTheSocialSecurityCharged()
|
|
{
|
|
var input = new PostingInput(PostingKind.Sale, false, 10, [new PostingLineInput(Revenue, 1000m)], [Tax(1040m, 22m, 228.8m)],
|
|
Total: 1268.8m, SocialSecurity: 40m);
|
|
|
|
var result = PostingEngine.Post(input, SaleAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
Assert.AreEqual(40m, Line(result, PostingRole.SocialSecurity).Credit);
|
|
Assert.AreEqual(1000m, Line(result, PostingRole.Revenue).Credit);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Purchase_PutsTheNonDeductibleVatOnTheCost()
|
|
{
|
|
var input = new PostingInput(PostingKind.Purchase, false, 20, [new PostingLineInput(Cost, 500m, TaxCodeId: 77)],
|
|
[new PostingTaxInput(77, 22m, null, TaxExigibility.Immediate, 500m, 110m, 66m)], Total: 610m);
|
|
|
|
var result = PostingEngine.Post(input, PurchaseAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
Assert.AreEqual(610m, Line(result, PostingRole.Supplier).Credit);
|
|
Assert.AreEqual(566m, Line(result, PostingRole.Cost).Debit);
|
|
Assert.AreEqual(44m, Line(result, PostingRole.PurchaseVat).Debit);
|
|
Assert.AreEqual(VatRegisterKind.Purchases, result.Vat.Single().RegisterKind);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CreditNote_ReversesEverySide_AndTheVat()
|
|
{
|
|
var input = new PostingInput(PostingKind.Sale, true, 10, [new PostingLineInput(Revenue, 100m)], [Tax(100m, 22m, 22m)], Total: 122m);
|
|
|
|
var result = PostingEngine.Post(input, SaleAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
Assert.AreEqual(122m, Line(result, PostingRole.Customer).Credit);
|
|
Assert.AreEqual(100m, Line(result, PostingRole.Revenue).Debit);
|
|
Assert.AreEqual(22m, Line(result, PostingRole.SalesVat).Debit);
|
|
Assert.AreEqual(-22m, result.Vat.Single().Tax.Tax);
|
|
Assert.AreEqual(-100m, result.Vat.Single().Tax.Taxable);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SelfInvoice_WritesTheVatInBothRegisters()
|
|
{
|
|
var input = new PostingInput(PostingKind.SelfInvoice, false, null, [], [Tax(1000m, 22m, 220m)], Total: 220m);
|
|
|
|
var result = PostingEngine.Post(input, PurchaseAccounts);
|
|
|
|
Assert.AreEqual(result.Debit, result.Credit);
|
|
Assert.AreEqual(220m, Line(result, PostingRole.PurchaseVat).Debit);
|
|
Assert.AreEqual(220m, Line(result, PostingRole.SalesVat).Credit);
|
|
CollectionAssert.AreEquivalent(new[] { VatRegisterKind.Sales, VatRegisterKind.Purchases }, result.Vat.Select(v => v.RegisterKind).ToList());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Posting_WithoutAnAccount_SaysWhichOne()
|
|
{
|
|
var input = new PostingInput(PostingKind.Sale, false, 10, [new PostingLineInput(Revenue, 100m)], [Tax(100m, 22m, 22m)], Total: 122m);
|
|
|
|
var missing = Assert.ThrowsExactly<PostingAccountMissingException>(() => PostingEngine.Post(input, SaleAccounts with { SalesVat = null }));
|
|
|
|
Assert.AreEqual(PostingRole.SalesVat, missing.Role);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Settlement_PaysTheDifference_WithQuarterlyInterest()
|
|
{
|
|
VatRegisterTotals[] registers =
|
|
[
|
|
new(1, VatRegisterKind.Sales, 10, 20000m, 4400m, 0m, 0m),
|
|
new(2, VatRegisterKind.Receipts, 3, 1000m, 220m, 0m, 0m),
|
|
new(3, VatRegisterKind.Purchases, 8, 10000m, 2200m, 200m, 0m),
|
|
];
|
|
|
|
var result = VatSettlementCalculator.Compute(registers, new VatSettlementTerms(PreviousCredit: 300m, InterestPercent: 1m, MinimumPayment: 25.82m));
|
|
|
|
Assert.AreEqual(4620m, result.SalesTax);
|
|
Assert.AreEqual(2000m, result.PurchaseTax);
|
|
Assert.AreEqual(200m, result.NonDeductibleTax);
|
|
Assert.AreEqual(2320m, result.Balance); // 4620 - 2000 - 300
|
|
Assert.AreEqual(23.2m, result.Interest);
|
|
Assert.AreEqual(2343.2m, result.AmountDue);
|
|
Assert.AreEqual(0m, result.CreditCarried);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Settlement_InCredit_CarriesTheCredit()
|
|
{
|
|
VatRegisterTotals[] registers = [new(1, VatRegisterKind.Sales, 1, 1000m, 220m, 0m, 0m), new(3, VatRegisterKind.Purchases, 1, 5000m, 1100m, 0m, 0m)];
|
|
|
|
var result = VatSettlementCalculator.Compute(registers, new VatSettlementTerms(InterestPercent: 1m, MinimumPayment: 25.82m));
|
|
|
|
Assert.AreEqual(0m, result.AmountDue);
|
|
Assert.AreEqual(880m, result.CreditCarried);
|
|
Assert.AreEqual(0m, result.Interest);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Settlement_BelowTheMinimum_CarriesTheDebit()
|
|
{
|
|
VatRegisterTotals[] registers = [new(1, VatRegisterKind.Sales, 1, 100m, 22m, 0m, 0m)];
|
|
|
|
var result = VatSettlementCalculator.Compute(registers, new VatSettlementTerms(MinimumPayment: 25.82m));
|
|
|
|
Assert.AreEqual(0m, result.AmountDue);
|
|
Assert.AreEqual(22m, result.DebitCarried);
|
|
|
|
var next = VatSettlementCalculator.Compute(registers, new VatSettlementTerms(PreviousDebit: result.DebitCarried, MinimumPayment: 25.82m));
|
|
Assert.AreEqual(44m, next.AmountDue);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Settlement_LeavesTheDeferredVatForLater_AndDeductsTheAdvance()
|
|
{
|
|
VatRegisterTotals[] registers = [new(1, VatRegisterKind.Sales, 2, 2000m, 440m, 0m, 220m)];
|
|
|
|
var result = VatSettlementCalculator.Compute(registers, new VatSettlementTerms(AdvancePaid: 100m));
|
|
|
|
Assert.AreEqual(220m, result.SalesTax);
|
|
Assert.AreEqual(220m, result.DeferredTax);
|
|
Assert.AreEqual(120m, result.AmountDue);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ProRata_RoundsUp_AndLimitsTheDeduction()
|
|
{
|
|
Assert.AreEqual(100m, ProRataCalculator.Percent(1000m, 0m));
|
|
Assert.AreEqual(67m, ProRataCalculator.Percent(2000m, 1000m)); // 66.67 rounded up
|
|
Assert.AreEqual(670m, ProRataCalculator.Deductible(1000m, 67m));
|
|
|
|
VatRegisterTotals[] registers = [new(1, VatRegisterKind.Sales, 1, 10000m, 2200m, 0m, 0m), new(3, VatRegisterKind.Purchases, 1, 5000m, 1000m, 0m, 0m)];
|
|
var result = VatSettlementCalculator.Compute(registers, new VatSettlementTerms(ProRataPercent: 67m));
|
|
Assert.AreEqual(670m, result.PurchaseTax);
|
|
Assert.AreEqual(330m, result.NonDeductibleTax);
|
|
Assert.AreEqual(1530m, result.AmountDue);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Advance_IsEightyEightPercent_AndNothingBelowTheMinimum()
|
|
{
|
|
Assert.AreEqual(880m, VatAdvanceCalculator.Compute(VatAdvanceMethod.Historical, 1000m, 88m, 103.29m));
|
|
Assert.AreEqual(440m, VatAdvanceCalculator.Compute(VatAdvanceMethod.Forecast, 500m, 88m, 103.29m));
|
|
Assert.AreEqual(500m, VatAdvanceCalculator.Compute(VatAdvanceMethod.Actual, 500m, 88m, 103.29m));
|
|
Assert.AreEqual(0m, VatAdvanceCalculator.Compute(VatAdvanceMethod.Historical, 100m, 88m, 103.29m));
|
|
Assert.AreEqual(0m, VatAdvanceCalculator.Compute(VatAdvanceMethod.Actual, -10m, 88m, 103.29m));
|
|
}
|
|
|
|
private static PostingTaxInput Tax(decimal taxable, decimal rate, decimal tax) => new(1, rate, null, TaxExigibility.Immediate, taxable, tax, 0m);
|
|
|
|
private static PostedLine Line(PostingResult result, PostingRole role) => result.Lines.Single(l => l.Role == role);
|
|
}
|