Files
Khaemhat/src/Khaemhat.Fiscal.Abstractions/FiscalPackCatalog.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

78 lines
3.1 KiB
C#

namespace Khaemhat.Fiscal.Abstractions;
/// <summary>
/// The country packs registered at start-up, found by country code. The composition root registers the concrete packs;
/// application code receives this catalog and never names a country's implementation.
/// </summary>
public sealed class FiscalPackCatalog
{
private readonly Dictionary<string, IFiscalPack> _byCountry;
/// <summary>Creates the catalog.</summary>
/// <param name="packs">The packs; two packs for the same country are a configuration error.</param>
public FiscalPackCatalog(IEnumerable<IFiscalPack> packs)
{
ArgumentNullException.ThrowIfNull(packs);
_byCountry = new Dictionary<string, IFiscalPack>(StringComparer.OrdinalIgnoreCase);
foreach (var pack in packs)
{
if (!_byCountry.TryAdd(pack.CountryCode, pack))
{
throw new ArgumentException($"Two fiscal packs for country '{pack.CountryCode}'.", nameof(packs));
}
}
Packs = [.. _byCountry.Values.OrderBy(p => p.CountryCode, StringComparer.Ordinal)];
}
/// <summary>Every registered pack, by country code.</summary>
public IReadOnlyList<IFiscalPack> Packs { get; }
/// <summary>The pack of a country.</summary>
/// <param name="countryCode">ISO 3166-1 alpha-2 code, case-insensitive.</param>
/// <returns>The pack.</returns>
/// <exception cref="FiscalPackNotFoundException">No pack for that country.</exception>
public IFiscalPack ForCountry(string? countryCode) =>
TryForCountry(countryCode, out var pack) ? pack : throw new FiscalPackNotFoundException(countryCode ?? string.Empty);
/// <summary>The pack of a country, if registered.</summary>
/// <param name="countryCode">ISO 3166-1 alpha-2 code, case-insensitive.</param>
/// <param name="pack">The pack, when found.</param>
/// <returns>Whether a pack was found.</returns>
public bool TryForCountry(string? countryCode, out IFiscalPack pack)
{
if (!string.IsNullOrWhiteSpace(countryCode) && _byCountry.TryGetValue(countryCode.Trim(), out var found))
{
pack = found;
return true;
}
pack = null!;
return false;
}
}
/// <summary>No fiscal pack is registered for the country of the company.</summary>
public sealed class FiscalPackNotFoundException : Exception
{
/// <summary>Creates the exception.</summary>
public FiscalPackNotFoundException()
: this(string.Empty)
{
}
/// <summary>Creates the exception.</summary>
/// <param name="countryCode">The country without a pack.</param>
public FiscalPackNotFoundException(string countryCode)
: base($"No fiscal pack for country '{countryCode}'.") => CountryCode = countryCode;
/// <summary>Creates the exception.</summary>
/// <param name="message">The message.</param>
/// <param name="innerException">The cause.</param>
public FiscalPackNotFoundException(string message, Exception innerException)
: base(message, innerException) => CountryCode = string.Empty;
/// <summary>The country without a pack.</summary>
public string CountryCode { get; }
}