feat: identifier validators and the VIES client (#40)

Parties need their identifiers checked before they reach documents. IBAN, EAN
and e-mail checks are international and live in Core. The Italian VAT number,
fiscal code (with calculator, decoder and omocodia) and SDI recipient code are
Italian, so they sit in the Italian pack behind ITaxIdentifierRules, as the
non-negotiable rule of SPEC 1.10 wins over the class list of SPEC 21 (ADR 0025).
IBankDirectory reads ABI and CAB from an Italian IBAN; its bank list arrives
with the base data.

ViesClient calls the VIES REST service and never reports a number as invalid
when the service could not answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 21:53:37 +01:00
1 parent 49d2bb166b
commit 25f8a7ad30
10 files changed
+1086 -2

No files matched your search

@@ -0,0 +1,98 @@
namespace Khaemhat.Fiscal.Abstractions;
/// <summary>The sex written in a personal tax code.</summary>
public enum PersonSex
{
/// <summary>Male.</summary>
Male = 0,
/// <summary>Female.</summary>
Female = 1,
}
/// <summary>What a personal tax code is computed from.</summary>
/// <param name="Surname">The surname.</param>
/// <param name="GivenName">The given name (all given names, as in the registry).</param>
/// <param name="BirthDate">The date of birth.</param>
/// <param name="Sex">The sex.</param>
/// <param name="BirthPlaceCode">The code of the place of birth (Italy: cadastral code of the municipality, or Z... for a foreign country).</param>
public sealed record PersonalTaxCodeData(string Surname, string GivenName, DateOnly BirthDate, PersonSex Sex, string BirthPlaceCode);
/// <summary>What can be read back from a personal tax code.</summary>
/// <param name="BirthDate">The date of birth (century guessed from the reference date).</param>
/// <param name="Sex">The sex.</param>
/// <param name="BirthPlaceCode">The code of the place of birth.</param>
public sealed record PersonalTaxCodeInfo(DateOnly BirthDate, PersonSex Sex, string BirthPlaceCode);
/// <summary>Who receives an e-invoice, for the recipient code rules.</summary>
public enum RecipientKind
{
/// <summary>A business with its own channel, or without (then the default code).</summary>
Business = 0,
/// <summary>A public administration (Italy: office code of 6 characters).</summary>
PublicAdministration = 1,
/// <summary>A private consumer.</summary>
Consumer = 2,
/// <summary>A customer abroad.</summary>
Foreign = 3,
}
/// <summary>The country's tax identifiers: VAT number, personal tax code and e-invoice recipient code.</summary>
public interface ITaxIdentifierRules
{
/// <summary>The VAT number without spaces and without the country prefix.</summary>
/// <param name="vatNumber">The number as typed.</param>
/// <returns>The compact number.</returns>
string NormalizeVatNumber(string? vatNumber);
/// <summary>Whether a domestic VAT number is valid (format and check digit).</summary>
/// <param name="vatNumber">The number, with or without prefix.</param>
/// <returns><see langword="true"/> when valid.</returns>
bool IsValidVatNumber(string? vatNumber);
/// <summary>Whether a personal or company tax code is valid.</summary>
/// <param name="taxCode">The code.</param>
/// <returns><see langword="true"/> when valid.</returns>
bool IsValidTaxCode(string? taxCode);
/// <summary>Computes the personal tax code of a person.</summary>
/// <param name="data">The personal data.</param>
/// <returns>The code.</returns>
string ComputePersonalTaxCode(PersonalTaxCodeData data);
/// <summary>Reads birth date, sex and place back from a personal tax code.</summary>
/// <param name="taxCode">The code.</param>
/// <param name="today">The reference date that decides the century.</param>
/// <returns>The data, or <see langword="null"/> if the code is not a valid personal code.</returns>
PersonalTaxCodeInfo? DecodePersonalTaxCode(string? taxCode, DateOnly today);
/// <summary>The rules an e-invoice recipient code breaks for a kind of customer; empty when valid.</summary>
/// <param name="recipientCode">The code.</param>
/// <param name="kind">The kind of customer.</param>
/// <returns>Errors and warnings with field <c>RecipientCode</c>.</returns>
IReadOnlyList<FiscalProblem> CheckRecipientCode(string? recipientCode, RecipientKind kind);
/// <summary>The recipient code to propose for a kind of customer without one.</summary>
/// <param name="kind">The kind of customer.</param>
/// <returns>The default code, or <see langword="null"/> if the customer must give one.</returns>
string? DefaultRecipientCode(RecipientKind kind);
}
/// <summary>A bank found from its national code.</summary>
/// <param name="BankCode">The national bank code (Italy: ABI).</param>
/// <param name="Name">The name of the bank.</param>
/// <param name="Bic">The BIC, when known.</param>
/// <param name="BranchCode">The branch code (Italy: CAB), when the IBAN has one.</param>
public sealed record BankInfo(string BankCode, string Name, string? Bic, string? BranchCode);
/// <summary>The country's directory of banks, to fill bank name and BIC from an IBAN.</summary>
public interface IBankDirectory
{
/// <summary>The bank of a domestic IBAN.</summary>
/// <param name="iban">The IBAN.</param>
/// <returns>The bank, or <see langword="null"/> if the IBAN is foreign, invalid or the bank unknown.</returns>
BankInfo? FindByIban(string? iban);
}