Files
Khaemhat/tests/Khaemhat.Fiscal.Italy.Tests/ItalianLedgerTests.cs
T
Alby96andClaude Opus 5.5 9e3ba92993 feat: general ledger, automatic posting, VAT settlement, withholdings and F24 (#62 #63 #64 #65 #66 #67 #68)
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>
2026-11-13 01:41:58 +01:00

159 lines
7.2 KiB
C#

using System.Text;
using System.Xml.Linq;
using Khaemhat.Core.Accounting;
using Khaemhat.Core.Tax;
using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Fiscal.Italy.Tests;
/// <summary>
/// The Italian ledger (issues #62, #65, #66): the model chart of accounts and its templates, the F24 codes and due dates
/// of VAT, withholdings and stamp duty, the LIPE file and the F24 data file.
/// </summary>
[TestClass]
public sealed class ItalianLedgerTests
{
private static readonly ItalyFiscalPack Pack = new();
private static readonly ILedgerTemplate Template = Pack.Require<ILedgerTemplate>();
private static readonly IVatSettlementRules Rules = Pack.Require<IVatSettlementRules>();
[TestMethod]
public void Chart_HasValidCodes_WithTheirParentsFirst()
{
var seen = new HashSet<string>(StringComparer.Ordinal);
Assert.IsGreaterThan(100, Template.Accounts.Count);
foreach (var account in Template.Accounts)
{
Assert.AreNotEqual(0, account.Level, account.Code);
Assert.IsTrue(seen.Add(account.Code), "Duplicate " + account.Code);
if (account.ParentCode is { } parent)
{
Assert.Contains(parent, seen, account.Code + " comes before its parent " + parent);
}
}
}
[TestMethod]
public void Templates_UseAccountsOfTheChart_AndTheRolesHaveSubAccounts()
{
var accounts = Template.Accounts.ToDictionary(a => a.Code, StringComparer.Ordinal);
var registers = Template.VatRegisters.Select(r => r.Code).ToHashSet(StringComparer.Ordinal);
foreach (var template in Template.JournalTemplates)
{
Assert.IsTrue(template.Reverses || template.Lines.Count > 0, template.Code + " proposes its lines (only the reversal copies them)");
if (template.VatRegisterCode is { } register)
{
Assert.Contains(register, registers, template.Code);
}
foreach (var line in template.Lines.Where(l => l.AccountCode is not null))
{
Assert.IsTrue(accounts.TryGetValue(line.AccountCode!, out var account), template.Code + " " + line.AccountCode);
Assert.AreEqual(3, account.Level, template.Code + " posts on a sub-account");
}
}
foreach (var role in Enum.GetValues<PostingRole>().Where(r => r != PostingRole.Account))
{
if (Template.AccountOf(role) is { } code)
{
Assert.AreEqual(3, accounts[code].Level, role.ToString());
}
}
Assert.IsTrue(accounts[ItalianLedgerTemplate.Customers].SubLedger);
Assert.IsTrue(accounts[ItalianLedgerTemplate.Suppliers].SubLedger);
var templates = Template.JournalTemplates.Select(t => t.Code).ToHashSet(StringComparer.Ordinal);
foreach (var posting in Template.DocumentPostings)
{
Assert.Contains(posting.JournalTemplateCode, templates, posting.DocumentTypeCode);
}
}
[TestMethod]
public void Templates_ForTheSettlementAndThePayments_Exist()
{
var codes = Template.JournalTemplates.Select(t => t.Code).ToList();
CollectionAssert.IsSubsetOf(new[] { "FV", "NCV", "FA", "NCA", "LIQ", "F24", "RIT", "RITS" }, codes);
}
[TestMethod]
[DataRow(VatPeriodicity.Monthly, 1, "6001", null)]
[DataRow(VatPeriodicity.Monthly, 12, "6012", null)]
[DataRow(VatPeriodicity.Quarterly, 1, "6031", null)]
[DataRow(VatPeriodicity.Quarterly, 3, "6033", null)]
[DataRow(VatPeriodicity.Quarterly, 4, "6099", "0101")]
public void VatPaymentCodes(VatPeriodicity periodicity, int number, string code, string? period)
{
var tax = TaxPeriod.Of(2026, number, periodicity);
Assert.AreEqual(code, Rules.PaymentCode(tax));
Assert.AreEqual(period, Rules.PaymentPeriod(tax));
}
[TestMethod]
public void TheRules_HaveTheItalianAmounts()
{
Assert.AreEqual(25.82m, Rules.MinimumPayment);
Assert.AreEqual(88m, Rules.AdvancePercent);
Assert.AreEqual(103.29m, Rules.AdvanceMinimum);
}
[TestMethod]
public void DueDates_MoveToTheNextWorkingDay_AndAugustToThe20th()
{
// The withholdings of March 2026 by 16 April (a Thursday); of July by 20 August.
Assert.AreEqual(new DateOnly(2026, 4, 16), Rules.WithholdingDue(2026, 3));
Assert.AreEqual(new DateOnly(2026, 8, 20), Rules.WithholdingDue(2026, 7));
// 16 May 2026 is a Saturday: Monday 18.
Assert.AreEqual(new DateOnly(2026, 5, 18), Rules.WithholdingDue(2026, 4));
// The stamp duty of the second quarter by 30 September, of the first by 31 May (a Sunday: 1 June).
Assert.AreEqual(new DateOnly(2026, 9, 30), Rules.StampDutyDue(2026, 2));
Assert.AreEqual(new DateOnly(2026, 6, 1), Rules.StampDutyDue(2026, 1));
// The advance by 27 December (a Sunday in 2026: Monday 28).
Assert.AreEqual(new DateOnly(2026, 12, 28), Rules.AdvanceDue(2026));
}
[TestMethod]
public void Lipe_WritesOneModulePerPeriod_WithCommaAmounts()
{
var data = new PeriodicVatReturn(2026, 3, "01234567890", "01234567890", null, null, new DateOnly(2026, 11, 13),
[
new PeriodicVatReturnModule(TaxPeriod.Of(2026, 3, VatPeriodicity.Quarterly), 20000m, 10000m, 4400m, 2200m, 0m, 300m, 19m, 0m, 1919m),
]);
var writer = Pack.Require<IPeriodicVatReturnWriter>();
var xml = XDocument.Parse(Encoding.UTF8.GetString(writer.Write(data)));
XNamespace iv = LipeWriter.Namespace;
var module = xml.Descendants(iv + "Modulo").Single();
Assert.AreEqual("IT01234567890_LI_2026_T3.xml", writer.FileName(data));
Assert.AreEqual("IVP18", xml.Descendants(iv + "CodiceFornitura").Single().Value);
Assert.AreEqual("3", module.Element(iv + "Trimestre")!.Value);
Assert.AreEqual("4400,00", module.Element(iv + "IvaEsigibile")!.Value);
Assert.AreEqual("2200,00", module.Element(iv + "IvaDovuta")!.Value);
Assert.AreEqual("300,00", module.Element(iv + "CreditoPeriodoPrecedente")!.Value);
Assert.AreEqual("1919,00", module.Element(iv + "ImportoDaVersare")!.Value);
Assert.IsNull(module.Element(iv + "Acconto"), "Zero amounts are left out.");
Assert.AreEqual("1234,50", LipeWriter.FormatAmount(1234.5m));
}
[TestMethod]
public void F24_ExportsTheErarioLines_AndTheBalance()
{
var form = new TaxPaymentForm(new DateOnly(2026, 11, 16), "01234567890", "Demo; S.r.l.",
[
new TaxPaymentFormLine("6033", null, 2026, 1919m, 0m, "IVA terzo trimestre"),
new TaxPaymentFormLine("1040", "10", 2026, 400m, 0m, "Ritenute lavoro autonomo"),
]);
var exporter = Pack.Require<ITaxPaymentFormExporter>();
var lines = Encoding.UTF8.GetString(exporter.Export(form)).Split('\n', StringSplitOptions.RemoveEmptyEntries);
Assert.AreEqual("F24;16/11/2026;01234567890;Demo, S.r.l.", lines[0]);
Assert.AreEqual("ER;6033;;2026;1919,00;;IVA terzo trimestre", lines[2]);
Assert.AreEqual("ER;1040;10;2026;400,00;;Ritenute lavoro autonomo", lines[3]);
Assert.AreEqual("Saldo;;;;2319,00;;", lines[4]);
Assert.AreEqual(2319m, form.Balance);
}
}