Files
Khaemhat/tests/Khaemhat.Fiscal.Italy.Tests/YearEndRulesTests.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

196 lines
8.7 KiB
C#

using System.Text;
using Khaemhat.Core.Accounting;
using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Fiscal.Italy.Tests;
[TestClass]
public sealed class CeeSchemaItTests
{
private static readonly CeeSchemaIt Schema = new();
[TestMethod]
public void Ordinary_HasTheLinesOfTheCivilCode()
{
var assets = Schema.Lines(FinancialStatementKind.Assets, FinancialStatementForm.Ordinary);
var liabilities = Schema.Lines(FinancialStatementKind.Liabilities, FinancialStatementForm.Ordinary);
var income = Schema.Lines(FinancialStatementKind.IncomeStatement, FinancialStatementForm.Ordinary);
Assert.IsTrue(assets.Any(l => l.Code == "C.II.5-quater"));
Assert.IsTrue(liabilities.Single(l => l.Code == "A.IX").CreditNormal);
Assert.AreEqual("21", income[^1].Code);
CollectionAssert.AreEqual(new[] { "A", "B", "C", "D", "20" }, income[^1].Totals!.ToArray());
Assert.IsFalse(income.Single(l => l.Code == "C.17").CreditNormal);
Assert.IsTrue(income.Single(l => l.Code == "C.17-bis").CreditNormal);
}
[TestMethod]
public void Lines_HaveUniqueCodesAndKnownParents()
{
foreach (var kind in Enum.GetValues<FinancialStatementKind>())
{
foreach (var form in Enum.GetValues<FinancialStatementForm>())
{
var lines = Schema.Lines(kind, form);
var codes = lines.Select(l => l.Code).ToHashSet(StringComparer.Ordinal);
Assert.HasCount(lines.Count, codes, $"{kind} {form}");
Assert.IsTrue(lines.All(l => l.ParentCode is null || codes.Contains(l.ParentCode)), $"{kind} {form}");
}
}
}
[TestMethod]
[DataRow("B.II.4", FinancialStatementKind.Assets, "B.II")]
[DataRow("C.II.5-bis", FinancialStatementKind.Assets, "C.II")]
[DataRow("D.7", FinancialStatementKind.Liabilities, "D")]
[DataRow("A.IX", FinancialStatementKind.Liabilities, "A.IX")]
[DataRow("B.9.a", FinancialStatementKind.IncomeStatement, "B.9")]
[DataRow("B.10.d", FinancialStatementKind.IncomeStatement, "B.10")]
[DataRow("C.17-bis", FinancialStatementKind.IncomeStatement, "C.17-bis")]
public void Abbreviated_CollapsesToTheRomanNumerals(string code, FinancialStatementKind kind, string expected)
{
Assert.AreEqual(expected, Schema.Collapse(code, kind, FinancialStatementForm.Abbreviated));
Assert.AreEqual(expected, Schema.Collapse(code, kind, FinancialStatementForm.Micro));
Assert.AreEqual(code, Schema.Collapse(code, kind, FinancialStatementForm.Ordinary));
}
[TestMethod]
public void Collapse_OfAnUnknownCodeIsNull() =>
Assert.IsNull(Schema.Collapse("Z.9", FinancialStatementKind.Assets, FinancialStatementForm.Ordinary));
[TestMethod]
public void EveryLineOfTheChart_IsInTheSchema()
{
var template = new ItalianLedgerTemplate();
var missing = template.Accounts
.Where(a => !string.IsNullOrEmpty(a.StatementLine))
.Where(a => Schema.Collapse(a.StatementLine!, KindOf(a.Nature), FinancialStatementForm.Ordinary) is null)
.Select(a => a.Code + " " + a.StatementLine)
.ToList();
Assert.IsEmpty(missing, string.Join(", ", missing));
}
[TestMethod]
public void Reclassifications_ReferToKnownLines()
{
foreach (var kind in Enum.GetValues<ReclassificationKind>())
{
var lines = Schema.Reclassification(kind);
var codes = new HashSet<string>(StringComparer.Ordinal);
foreach (var line in lines)
{
var schema = Schema.Lines(line.Source, FinancialStatementForm.Ordinary).Select(l => l.Code).ToHashSet();
Assert.IsTrue(line.Lines.All(schema.Contains), line.Code);
Assert.IsTrue((line.Totals ?? []).All(t => codes.Contains(t.TrimStart('-'))), line.Code);
codes.Add(line.Code);
}
}
}
private static FinancialStatementKind KindOf(AccountNature nature) => nature switch
{
AccountNature.Asset => FinancialStatementKind.Assets,
AccountNature.Liability => FinancialStatementKind.Liabilities,
_ => FinancialStatementKind.IncomeStatement,
};
}
[TestClass]
public sealed class IntrastatExporterItTests
{
private static readonly IntrastatExporterIt Exporter = new();
[TestMethod]
[DataRow(49_999.99, 0, IntrastatPeriodicity.Quarterly, IntrastatPeriodicity.Quarterly)]
[DataRow(50_000, 0, IntrastatPeriodicity.Monthly, IntrastatPeriodicity.Quarterly)]
[DataRow(0, 60_000, IntrastatPeriodicity.Quarterly, IntrastatPeriodicity.Monthly)]
public void Sales_AreMonthlyFrom50000PerQuarter(double goods, double services, IntrastatPeriodicity g, IntrastatPeriodicity s)
{
var due = Exporter.Due(IntrastatFlow.Sales, (decimal)goods, (decimal)services);
Assert.AreEqual(g, due.Goods);
Assert.AreEqual(s, due.Services);
}
[TestMethod]
public void Purchases_AreDueOnlyAboveTheThresholds()
{
var below = Exporter.Due(IntrastatFlow.Purchases, 349_999m, 99_999m);
var goods = Exporter.Due(IntrastatFlow.Purchases, 350_000m, 0m);
var services = Exporter.Due(IntrastatFlow.Purchases, 0m, 100_000m);
Assert.AreEqual(IntrastatPeriodicity.NotDue, below.List);
Assert.AreEqual(IntrastatPeriodicity.Monthly, goods.Goods);
Assert.AreEqual(IntrastatPeriodicity.NotDue, goods.Services);
Assert.AreEqual(IntrastatPeriodicity.Monthly, services.List);
}
[TestMethod]
[DataRow("73043100", true)]
[DataRow("7304310", false)]
[DataRow("7304310A", false)]
[DataRow(null, false)]
public void Nomenclature_IsEightDigits(string? code, bool valid) => Assert.AreEqual(valid, Exporter.ValidNomenclature(code));
[TestMethod]
public void MemberStates_AreTheTwentySevenWithGreeceAsGR()
{
Assert.HasCount(27, Exporter.MemberStates);
Assert.Contains("GR", Exporter.MemberStates);
Assert.DoesNotContain("NO", Exporter.MemberStates);
}
[TestMethod]
public void File_HasAFrontispieceAndOneFixedRecordPerIncludedLine()
{
var list = new IntrastatList { Flow = IntrastatFlow.Sales, Year = 2026, Period = 10, Periodicity = IntrastatPeriodicity.Monthly, Progressive = 3 };
IntrastatListLine[] lines =
[
new() { Id = 1, Section = IntrastatSection.Goods, CountryCode = "AT", VatNumber = "U12345678", Amount = 12_400.40m, NomenclatureCode = "73043100", TransactionNature = "11", NetMass = 820m, DeliveryTerms = "DAP", TransportMode = "3", Province = "TO" },
new() { Id = 2, Section = IntrastatSection.Services, CountryCode = "DE", VatNumber = "123456789", Amount = 900m, NomenclatureCode = "711210", SupplyMode = "I", PaymentMode = "B", DocumentNumber = "FV 2026/0101", DocumentDate = new DateOnly(2026, 10, 12) },
new() { Id = 3, Section = IntrastatSection.Goods, CountryCode = "NO", Amount = 5_000m, Check = IntrastatCheck.OutsideEu },
];
var text = Encoding.ASCII.GetString(Exporter.Write(new IntrastatReturn(list, lines, "01234567890")));
var records = text.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
Assert.HasCount(3, records);
Assert.IsTrue(records.All(r => r.Length == IntrastatExporterIt.RecordLength));
Assert.StartsWith("EUROX01234567890000003000000C26M10", records[0]);
Assert.Contains("000010000000012400", records[0]);
Assert.StartsWith("EUROX01234567890000003100001AT", records[1]);
Assert.StartsWith("EUROX01234567890000003300002DE", records[2]);
Assert.AreEqual("SCAMBI.CEE", Exporter.FileName(new IntrastatReturn(list, lines, "01234567890")));
}
}
[TestClass]
public sealed class MinisterialCoefficientsTests
{
[TestMethod]
public void Coefficients_UseAccountsOfTheChart()
{
var accounts = new ItalianLedgerTemplate().Accounts.Select(a => a.Code).ToHashSet(StringComparer.Ordinal);
var missing = MinisterialCoefficients.All
.SelectMany(c => new[] { c.AssetAccount, c.FundAccount, c.DepreciationAccount })
.Where(code => !accounts.Contains(code))
.ToList();
Assert.IsEmpty(missing, string.Join(", ", missing));
Assert.HasCount(MinisterialCoefficients.All.Count, MinisterialCoefficients.All.Select(c => c.Code).Distinct().ToList());
}
[TestMethod]
public void Cars_HaveTheTaxLimits()
{
var car = MinisterialCoefficients.All.Single(c => c.Code == "AUV");
Assert.AreEqual(25m, car.Rate);
Assert.AreEqual(18_075.99m, car.CostLimit);
Assert.AreEqual(20m, car.DeductiblePercent);
Assert.AreSame(MinisterialCoefficients.All, new FixedAssetRulesIt().Coefficients);
}
}