Files
Khaemhat/tests/Khaemhat.Fiscal.Italy.Tests/ItalyFiscalPackTests.cs
T
Alby96andClaude Opus 5.5 51007d68ca
CI / Build and test (SQLite) (pull_request) Successful in 4m35s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Successful in 1m24s
feat: fixed assets, Intrastat, intent declarations, year-end closing and statements (#82 #83 #84 #85 #86 #87)
M10 gives the ledger its year end. Fixed assets get categories with the
ministerial coefficients, a civil and a fiscal plan, depreciation posted
per category and undoable, disposals with gain or loss and the register
print (ADR 0041). Intrastat lists are generated from confirmed invoices
with the due periodicity and the SCAMBI.CEE file; declarations of intent
get their own register and the N3.5 ceiling is checked when an invoice is
confirmed (ADR 0042). Accruals, deferrals and exchange differences feed a
nine-step guided closing that can repeat or undo its last step until the
final prints, reopens the next year and locks the closed one; the CEE
statements (ordinary, abbreviated, micro), the reclassified statements,
the ratios and the accountant export read the year's balances (ADR 0043).

The demo seed now opens the year with assets, stock, bank and capital, so
the integration test closes the demo year from the checks to the lock on
SQLite and PostgreSQL. Docs: wiki pages with screenshots, ADR 0041-0043,
MODULES, STATUS, DATA-MODEL and the 0.10.0 changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:31:26 +01:00

356 lines
14 KiB
C#

using Khaemhat.Core.Tax;
using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Fiscal.Italy.Tests;
[TestClass]
public sealed class ItalyFiscalPackTests
{
private static readonly ItalyFiscalPack Pack = new();
[TestMethod]
public void Pack_IsItalianWithItsDataVersion()
{
Assert.AreEqual("IT", Pack.CountryCode);
Assert.AreEqual("IT 2026.3", Pack.DataVersion);
}
[TestMethod]
public void Pack_DeliversTheFeaturesOfThisMilestone()
{
Assert.IsTrue(Pack.Supports<IEInvoiceFormat>());
Assert.IsTrue(Pack.Supports<IWithholdingRules>());
Assert.IsTrue(Pack.Supports<IFixedAssetRules>());
Assert.IsTrue(Pack.Supports<ITaxPaymentFormExporter>());
Assert.IsTrue(Pack.Supports<IFinancialStatementsSchema>());
Assert.IsTrue(Pack.Supports<IIntrastatExporter>());
Assert.AreSame(Pack.TaxCodes, Pack.GetFeature<ITaxCodeCatalog>());
Assert.AreSame(Pack.Calendar, Pack.Require<IFiscalCalendar>());
}
[TestMethod]
public void Require_OfAFeatureNotYetDelivered_SaysWhichOne()
{
var ex = Assert.ThrowsExactly<FiscalFeatureNotAvailableException>(() => Pack.Require<IFutureFeature>());
Assert.AreEqual("IT", ex.CountryCode);
Assert.AreEqual(nameof(IFutureFeature), ex.Feature);
}
[TestMethod]
public void Catalog_FindsThePackByCountryWithoutCase()
{
var catalog = new FiscalPackCatalog([Pack]);
Assert.AreSame(Pack, catalog.ForCountry("it"));
Assert.AreSame(Pack, catalog.ForCountry(" IT "));
Assert.IsFalse(catalog.TryForCountry("FR", out _));
Assert.ThrowsExactly<FiscalPackNotFoundException>(() => catalog.ForCountry(null));
}
[TestMethod]
public void Catalog_RefusesTwoPacksForOneCountry() =>
Assert.ThrowsExactly<ArgumentException>(() => new FiscalPackCatalog([Pack, new ItalyFiscalPack()]));
/// <summary>A feature no pack delivers.</summary>
public interface IFutureFeature;
}
[TestClass]
public sealed class FatturaPaCodeListsTests
{
[TestMethod]
public void SpecVersion_IsTheLatestChecked() => Assert.AreEqual("1.9.1", new FatturaPaFormat().SpecVersion);
[TestMethod]
public void DocumentTypes_RunFromTD01ToTD29WithoutTheRemovedOnes()
{
var codes = FatturaPaCodeLists.DocumentTypes.Entries.Select(e => e.Code).ToList();
Assert.HasCount(23, codes);
Assert.AreEqual("TD01", codes[0]);
Assert.AreEqual("TD29", codes[^1]);
Assert.IsNull(FatturaPaCodeLists.DocumentTypes.Find("TD10"));
Assert.IsTrue(FatturaPaCodeLists.DocumentTypes.IsActive("td24"));
}
[TestMethod]
public void PaymentMethods_AreMP01ToMP23()
{
var codes = FatturaPaCodeLists.PaymentMethods.Entries.Select(e => e.Code).ToList();
CollectionAssert.AreEqual(Enumerable.Range(1, 23).Select(n => $"MP{n:00}").ToList(), codes);
}
[TestMethod]
public void TaxRegimes_KeepRF03OnlyAsObsolete()
{
Assert.HasCount(18, FatturaPaCodeLists.TaxRegimes.Active.ToList());
Assert.IsFalse(FatturaPaCodeLists.TaxRegimes.IsActive("RF03"));
Assert.IsTrue(FatturaPaCodeLists.TaxRegimes.IsActive("RF19"));
}
[TestMethod]
public void SocialSecurityFunds_AreTC01ToTC22() =>
Assert.HasCount(22, FatturaPaCodeLists.SocialSecurityFunds.Entries);
[TestMethod]
public void CodeList_RefusesDuplicates() =>
Assert.ThrowsExactly<ArgumentException>(() => new CodeList("X", [new("A", "a"), new("a", "b")]));
}
[TestClass]
public sealed class ItalianTaxCodeCatalogTests
{
private static readonly ItalianTaxCodeCatalog Catalog = new();
[TestMethod]
public void Natures_AcceptOnlyTheSubCodesOfN2N3N6()
{
Assert.IsFalse(Catalog.Natures.IsActive("N2"));
Assert.IsFalse(Catalog.Natures.IsActive("N3"));
Assert.IsFalse(Catalog.Natures.IsActive("N6"));
Assert.IsTrue(Catalog.Natures.IsActive("N6.9"));
Assert.HasCount(21, Catalog.Natures.Active.ToList());
}
[TestMethod]
public void BaseCodes_HaveNoErrorsAndUniqueCodes()
{
var errors = Catalog.BaseCodes
.SelectMany(c => Catalog.Validate(c).Where(p => p.Severity == FiscalProblemSeverity.Error).Select(p => $"{c.Code}: {p.Code}"))
.ToList();
Assert.IsEmpty(errors, string.Join(", ", errors));
Assert.HasCount(Catalog.BaseCodes.Count, Catalog.BaseCodes.Select(c => c.Code).Distinct(StringComparer.OrdinalIgnoreCase).ToList());
}
[TestMethod]
public void BaseCodes_CoverTheCodesOfSpecSection14()
{
string[] expected = ["22", "10", "5", "4", "ESE10", "NI8", "NI41", "NI71", "NI8C", "FC7", "ESC15", "RC17", "NS", "22I50", "22I100", "FORF"];
var codes = Catalog.BaseCodes.Select(c => c.Code).ToHashSet(StringComparer.Ordinal);
Assert.IsEmpty(expected.Where(c => !codes.Contains(c)).ToList());
Assert.IsTrue(Catalog.BaseCodes.Any(c => c.Exigibility == TaxExigibility.SplitPayment));
}
[TestMethod]
[DataRow(0, null, "Nature", "fiscal.tax.nature-required")]
[DataRow(22, "N4", "Nature", "fiscal.tax.nature-with-rate")]
[DataRow(0, "N9", "Nature", "fiscal.tax.nature-unknown")]
[DataRow(0, "N2", "Nature", "fiscal.tax.nature-obsolete")]
[DataRow(-1, null, "Rate", "fiscal.tax.rate-range")]
public void Validate_ReportsTheBrokenRule(int rate, string? nature, string field, string code)
{
var problems = Catalog.Validate(new TaxCodeDefinition("X", "x", rate, nature, LegalReference: "Art. 1"));
Assert.IsTrue(problems.Any(p => p.Field == field && p.Code == code && p.Severity == FiscalProblemSeverity.Error),
string.Join(", ", problems));
}
[TestMethod]
public void Validate_WarnsWithoutLegalReferenceAndOnUnusualRates()
{
var noReference = Catalog.Validate(new TaxCodeDefinition("ESE", "Esente", 0m, "N4"));
var unusual = Catalog.Validate(new TaxCodeDefinition("AGR", "Compensazione", 7.3m));
Assert.AreEqual(FiscalProblemSeverity.Warning, noReference.Single().Severity);
Assert.AreEqual("fiscal.tax.rate-unusual", unusual.Single().Code);
Assert.AreEqual(FiscalProblemSeverity.Warning, unusual.Single().Severity);
}
[TestMethod]
public void Validate_RefusesSplitWithoutTaxAndReverseChargeNotImmediate()
{
var split = Catalog.Validate(new TaxCodeDefinition("S", "s", 0m, "N4", TaxExigibility.SplitPayment, LegalReference: "x"));
var reverse = Catalog.Validate(new TaxCodeDefinition("R", "r", 0m, "N6.3", TaxExigibility.Deferred, LegalReference: "x"));
Assert.IsTrue(split.Any(p => p.Code == "fiscal.tax.split-needs-rate"));
Assert.IsTrue(reverse.Any(p => p.Code == "fiscal.tax.reverse-charge-immediate"));
}
[TestMethod]
[DataRow(null, TaxCodeKind.Taxable)]
[DataRow("N1", TaxCodeKind.Excluded)]
[DataRow("N2.1", TaxCodeKind.NotSubject)]
[DataRow("N3.5", TaxCodeKind.NonTaxable)]
[DataRow("N4", TaxCodeKind.Exempt)]
[DataRow("N5", TaxCodeKind.Margin)]
[DataRow("n6.4", TaxCodeKind.ReverseCharge)]
[DataRow("N7", TaxCodeKind.OtherEuVat)]
public void KindOf_FollowsTheNature(string? nature, TaxCodeKind kind) => Assert.AreEqual(kind, Catalog.KindOf(0m, nature));
}
[TestClass]
public sealed class ItalianTaxEngineTests
{
private static readonly ItalianTaxEngine Engine = new();
[TestMethod]
public void Round_TakesHalvesAwayFromZero()
{
Assert.AreEqual(0.13m, Engine.Round(0.125m));
Assert.AreEqual(-0.13m, Engine.Round(-0.125m));
Assert.AreEqual(2.2m, Engine.ComputeTax(10m, 22m));
}
[TestMethod]
public void Summarize_ComputesTheTaxOnTheTotalOfEachRate()
{
// Three rows of 0.07 at 22%: per row the tax would be 0.02 each (0.06); on the total it is 0.21 * 22% = 0.0462 -> 0.05.
var summary = Engine.Summarize(
[
new(0.07m, 22m), new(0.07m, 22m), new(0.07m, 22m),
new(100m, 10m),
new(50m, 0m, "N4"),
]);
Assert.HasCount(3, summary);
Assert.AreEqual(22m, summary[0].Rate);
Assert.AreEqual(0.21m, summary[0].Taxable);
Assert.AreEqual(0.05m, summary[0].Tax);
Assert.AreEqual(10m, summary[1].Tax);
Assert.AreEqual("N4", summary[2].Nature);
Assert.AreEqual(0m, summary[2].Tax);
}
[TestMethod]
public void Summarize_KeepsSplitPaymentAndNonDeductibleApart()
{
var summary = Engine.Summarize(
[
new(100m, 22m),
new(100m, 22m, Exigibility: TaxExigibility.SplitPayment),
new(100m, 22m, NonDeductiblePercent: 50m),
]);
Assert.HasCount(3, summary);
Assert.AreEqual(11m, summary.Single(l => l.NonDeductiblePercent == 50m).NonDeductibleTax);
}
[TestMethod]
public void StampDuty_IsDueAboveTheThresholdOfExemptAmounts()
{
var exempt = Engine.AssessStampDuty(Engine.Summarize([new(77.48m, 0m, "N4")]));
var atThreshold = Engine.AssessStampDuty(Engine.Summarize([new(77.47m, 0m, "N4")]));
var mixed = Engine.AssessStampDuty(Engine.Summarize([new(1000m, 22m), new(50m, 0m, "N2.2"), new(30m, 0m, "N3.5")]));
Assert.IsTrue(exempt.Required);
Assert.AreEqual(2.00m, exempt.Amount);
Assert.IsFalse(atThreshold.Required);
Assert.IsTrue(mixed.Required);
Assert.AreEqual(80m, mixed.ExemptTotal);
}
[TestMethod]
[DataRow("N3.1")]
[DataRow("N3.2")]
[DataRow("N6.3")]
public void StampDuty_IsNotDueOnExportsIntraEuAndReverseCharge(string nature) =>
Assert.IsFalse(Engine.AssessStampDuty(Engine.Summarize([new(1000m, 0m, nature)])).Required);
}
[TestClass]
public sealed class ItalianFiscalCalendarTests
{
private static readonly ItalianFiscalCalendar Calendar = new();
[TestMethod]
[DataRow(2026, 1, 2026, 2, 16)] // Monday 16 February
[DataRow(2026, 4, 2026, 5, 18)] // 16 May 2026 is a Saturday
[DataRow(2026, 7, 2026, 8, 20)] // August deadlines move to the 20th
[DataRow(2026, 12, 2027, 1, 18)] // 16 January 2027 is a Saturday
public void MonthlyVat_IsDueOnThe16thOfTheNextMonth(int year, int month, int y, int m, int d) =>
Assert.AreEqual(new DateOnly(y, m, d), Calendar.VatPaymentDue(TaxPeriod.Of(year, month, VatPeriodicity.Monthly)));
[TestMethod]
[DataRow(1, 2026, 5, 18)]
[DataRow(2, 2026, 8, 20)]
[DataRow(3, 2026, 11, 16)]
[DataRow(4, 2027, 3, 16)]
public void QuarterlyVat_IsDueOnThe16thOfTheSecondMonth(int quarter, int y, int m, int d) =>
Assert.AreEqual(new DateOnly(y, m, d), Calendar.VatPaymentDue(TaxPeriod.Of(2026, quarter, VatPeriodicity.Quarterly)));
[TestMethod]
public void QuarterlyVat_CarriesOnePercentInterest()
{
Assert.AreEqual(1m, Calendar.VatInterestRate(VatPeriodicity.Quarterly));
Assert.AreEqual(0m, Calendar.VatInterestRate(VatPeriodicity.Monthly));
}
[TestMethod]
public void Holidays_IncludeEasterMondayAndTheFixedOnes()
{
Assert.AreEqual(new DateOnly(2026, 4, 5), ItalianFiscalCalendar.EasterSunday(2026));
Assert.AreEqual(new DateOnly(2027, 3, 28), ItalianFiscalCalendar.EasterSunday(2027));
Assert.IsTrue(ItalianFiscalCalendar.IsPublicHoliday(new DateOnly(2026, 4, 6)));
Assert.IsTrue(ItalianFiscalCalendar.IsPublicHoliday(new DateOnly(2026, 6, 2)));
Assert.IsTrue(ItalianFiscalCalendar.IsPublicHoliday(new DateOnly(2026, 10, 4)));
Assert.IsFalse(ItalianFiscalCalendar.IsPublicHoliday(new DateOnly(2025, 10, 4)));
Assert.AreEqual(new DateOnly(2026, 12, 28), Calendar.NextWorkingDay(new DateOnly(2026, 12, 25)));
}
[TestMethod]
public void TaxPeriod_CoversTheMonthsOfTheQuarter()
{
var q2 = TaxPeriod.Containing(new DateOnly(2026, 5, 31), VatPeriodicity.Quarterly);
Assert.AreEqual(2, q2.Number);
Assert.AreEqual(new DateOnly(2026, 4, 1), q2.Start);
Assert.AreEqual(new DateOnly(2026, 6, 30), q2.End);
Assert.HasCount(12, TaxPeriod.OfYear(2026, VatPeriodicity.Monthly));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => TaxPeriod.Of(2026, 5, VatPeriodicity.Quarterly));
}
}
[TestClass]
public sealed class ItalianRulesTests
{
[TestMethod]
public void Withholding_OfAgents_AppliesTheRateToHalfTheCommission()
{
var rules = new WithholdingRulesIt();
Assert.AreEqual(115m, rules.Compute(1000m, rules.Kinds.Single(k => k.Code == "AGEN")));
Assert.AreEqual(46m, rules.Compute(1000m, rules.Kinds.Single(k => k.Code == "AGEND")));
Assert.AreEqual(200m, rules.Compute(1000m, rules.Kinds.Single(k => k.Code == "PROF")));
}
[TestMethod]
public void Withholding_KindsUseActiveCodes()
{
var rules = new WithholdingRulesIt();
foreach (var kind in rules.Kinds)
{
Assert.IsTrue(rules.InvoiceTypes.IsActive(kind.InvoiceType), kind.Code);
Assert.IsTrue(rules.PaymentReasons.IsActive(kind.PaymentReason), kind.Code);
}
}
[TestMethod]
public void FixedAssets_HalveTheFirstYearAndStopAtTheCost()
{
var rules = new FixedAssetRulesIt();
Assert.AreEqual(1000m, rules.YearlyDepreciation(10_000m, 20m, firstYear: true, 0m));
Assert.AreEqual(2000m, rules.YearlyDepreciation(10_000m, 20m, firstYear: false, 1000m));
Assert.AreEqual(1000m, rules.YearlyDepreciation(10_000m, 20m, firstYear: false, 9000m));
}
[TestMethod]
public void F24_CodesFollowThePeriod()
{
var f24 = new F24Exporter();
Assert.AreEqual("6001", f24.VatCode(TaxPeriod.Of(2026, 1, VatPeriodicity.Monthly)));
Assert.AreEqual("6012", f24.VatCode(TaxPeriod.Of(2026, 12, VatPeriodicity.Monthly)));
Assert.AreEqual("6033", f24.VatCode(TaxPeriod.Of(2026, 3, VatPeriodicity.Quarterly)));
Assert.AreEqual("6035", f24.VatAdvanceCode(VatPeriodicity.Quarterly));
Assert.AreEqual("2524", f24.StampDutyCode(4));
Assert.IsTrue(f24.Codes.Any(c => c.Code == "6099"));
Assert.HasCount(f24.Codes.Count, f24.Codes.Select(c => c.Code).Distinct().ToList());
}
}