Files
Khaemhat/src/Khaemhat.Fiscal.Abstractions/Taxes.cs
T
Alby96andClaude Opus 5.5 49d2bb166b feat: fiscal pack abstractions and the Italian pack (#37)
The rest of the product needs tax codes, natures, code lists and VAT rules
without knowing the country. IFiscalPack exposes the parts every country has
(tax code catalog, tax engine, calendar) and optional features by type, so a
module can tell a missing feature from a broken one. FiscalPackCatalog finds
the pack by country; only the composition root names the Italian pack, as an
architecture test now checks.

The Italian pack carries the FatturaPA 1.9.1 code lists, VAT natures and base
tax codes with their rules, summary rounding on the total per rate, the stamp
duty rule, VAT deadlines with the August and holiday shifts, withholding kinds,
basic depreciation rules and F24 codes. ADR 0025 records the design.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 21:48:09 +01:00

110 lines
5.2 KiB
C#

using Khaemhat.Core.Tax;
namespace Khaemhat.Fiscal.Abstractions;
/// <summary>
/// The fiscal content of a tax code (SPEC §11.1, mockup 076): the parts the country's rules check. The table
/// <c>tax_codes</c> adds validity, accounts and the other company data.
/// </summary>
/// <param name="Code">The company's code, such as <c>22</c> or <c>NI8</c>.</param>
/// <param name="Description">The description shown on documents.</param>
/// <param name="Rate">The rate in percent, such as 22.</param>
/// <param name="Nature">The country's nature code for zero-rate operations (Italy: <c>N1</c>...<c>N7</c>).</param>
/// <param name="Exigibility">When the tax becomes due.</param>
/// <param name="NonDeductiblePercent">Share of the purchase tax that cannot be deducted, 0-100.</param>
/// <param name="LegalReference">The law the operation is based on, printed on the invoice.</param>
public sealed record TaxCodeDefinition(
string Code,
string Description,
decimal Rate,
string? Nature = null,
TaxExigibility Exigibility = TaxExigibility.Immediate,
decimal NonDeductiblePercent = 0m,
string? LegalReference = null);
/// <summary>The country's tax natures and base tax codes, and the rules a tax code must follow.</summary>
public interface ITaxCodeCatalog
{
/// <summary>The nature codes for operations without tax.</summary>
CodeList Natures { get; }
/// <summary>The tax codes loaded by the base data (SPEC §14).</summary>
IReadOnlyList<TaxCodeDefinition> BaseCodes { get; }
/// <summary>The kind of operation a rate and nature describe.</summary>
/// <param name="rate">The rate in percent.</param>
/// <param name="nature">The nature code, if any.</param>
/// <returns>The kind.</returns>
TaxCodeKind KindOf(decimal rate, string? nature);
/// <summary>The rules a tax code breaks; empty when it is valid.</summary>
/// <param name="code">The tax code.</param>
/// <returns>Errors and warnings.</returns>
IReadOnlyList<FiscalProblem> Validate(TaxCodeDefinition code);
}
/// <summary>An amount of a document subject to one tax code.</summary>
/// <param name="Amount">The taxable amount, already net of discounts.</param>
/// <param name="Rate">The rate in percent.</param>
/// <param name="Nature">The nature code for zero-rate operations.</param>
/// <param name="Exigibility">When the tax becomes due.</param>
/// <param name="NonDeductiblePercent">Share of the tax that cannot be deducted (purchases).</param>
public sealed record TaxableAmount(
decimal Amount,
decimal Rate,
string? Nature = null,
TaxExigibility Exigibility = TaxExigibility.Immediate,
decimal NonDeductiblePercent = 0m);
/// <summary>One line of the tax summary of a document (castelletto IVA).</summary>
/// <param name="Rate">The rate in percent.</param>
/// <param name="Nature">The nature code, if any.</param>
/// <param name="Exigibility">When the tax becomes due.</param>
/// <param name="NonDeductiblePercent">Share of the tax that cannot be deducted.</param>
/// <param name="Taxable">The sum of the taxable amounts.</param>
/// <param name="Tax">The tax, rounded to the cent.</param>
/// <param name="NonDeductibleTax">The part of <paramref name="Tax"/> that cannot be deducted.</param>
public sealed record TaxSummaryLine(
decimal Rate,
string? Nature,
TaxExigibility Exigibility,
decimal NonDeductiblePercent,
decimal Taxable,
decimal Tax,
decimal NonDeductibleTax);
/// <summary>Whether a document needs stamp duty, and how much.</summary>
/// <param name="Required">The document must carry the duty.</param>
/// <param name="Amount">The duty, zero when not required.</param>
/// <param name="ExemptTotal">The sum of the amounts without tax that the rule counts.</param>
/// <param name="Threshold">The amount above which the duty is due.</param>
public sealed record StampDutyAssessment(bool Required, decimal Amount, decimal ExemptTotal, decimal Threshold);
/// <summary>Tax computation with the country's rounding rules.</summary>
public interface ITaxEngine
{
/// <summary>Rounds an amount of money as the country's tax rules require.</summary>
/// <param name="amount">The amount.</param>
/// <returns>The amount rounded to the cent.</returns>
decimal Round(decimal amount);
/// <summary>The tax on a taxable amount.</summary>
/// <param name="taxable">The taxable amount.</param>
/// <param name="rate">The rate in percent.</param>
/// <returns>The tax, rounded.</returns>
decimal ComputeTax(decimal taxable, decimal rate);
/// <summary>
/// The tax summary of a document: one line per rate, nature, exigibility and non-deductible share, with the tax
/// computed on the total of the line (not summed from the rows).
/// </summary>
/// <param name="amounts">The taxable amounts of the document rows.</param>
/// <returns>The summary lines, by rate (highest first) and then by nature.</returns>
IReadOnlyList<TaxSummaryLine> Summarize(IEnumerable<TaxableAmount> amounts);
/// <summary>Whether the document needs stamp duty.</summary>
/// <param name="summary">The tax summary of the document.</param>
/// <returns>The assessment.</returns>
StampDutyAssessment AssessStampDuty(IEnumerable<TaxSummaryLine> summary);
}