Files
Khaemhat/src/Khaemhat.Core/Tables/BaseTables.cs
T
Alby96andClaude Opus 5.5 34e196424f feat: company tables, profile, fiscal years and base data (#38, #39, #45)
Adds the company tables of SPEC 11.1 behind one generic service so that every
table gets the same rules, history, cache and CSV import/export, plus the
company profile, fiscal years with monthly lock and the base-data seeder.

- 26 coded tables, exchange rates, company profile, fiscal years and periods:
  entities, maps and migrations 010-014
- TableService + TableCatalog: unique codes, per-table rules (VAT codes are
  checked by the fiscal pack), delete or disable when in use, single default,
  CSV export/import all-or-nothing, cache with cross-instance invalidation
- CompanyProfileService (Italian identifiers through the pack),
  FiscalYearService with the date check used by every dated record
- EcbRatesClient and the ECB rates system task; CsvReader
- ISeeder/SeedRunner with the data version in company settings; base tables
  from the pack (IBaseTablesTemplate) and an embedded ISO country list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 22:33:37 +01:00

470 lines
15 KiB
C#

using Khaemhat.Core.Entities;
using Khaemhat.Core.Tax;
namespace Khaemhat.Core.Tables;
/// <summary>A row of a company table with a code the users type and a description (SPEC §11.1).</summary>
public interface ICodedRecord : IEntity<long>
{
/// <summary>The code, unique in the table.</summary>
string Code { get; set; }
/// <summary>The description (or the name).</summary>
string Description { get; set; }
/// <summary>Hidden from choices on new records; kept for old ones.</summary>
bool Disabled { get; set; }
}
/// <summary>Base class of the coded tables.</summary>
public abstract class CodedEntity : Entity<long>, ICodedRecord, IDemoMarkable
{
/// <inheritdoc />
public string Code { get; set; } = string.Empty;
/// <inheritdoc />
public string Description { get; set; } = string.Empty;
/// <inheritdoc />
public bool Disabled { get; set; }
/// <inheritdoc />
public bool IsDemo { get; set; }
}
/// <summary>A country (ISO 3166-1).</summary>
public sealed class Country : CodedEntity
{
/// <summary>ISO 3166-1 alpha-3 code.</summary>
public string? Iso3 { get; set; }
/// <summary>Member of the European Union (VIES, Intrastat, intra-EU VAT).</summary>
public bool EuMember { get; set; }
/// <summary>The prefix of VAT numbers, when different from the code (Greece: <c>EL</c>).</summary>
public string? VatPrefix { get; set; }
/// <summary>The usual currency (ISO 4217).</summary>
public string? CurrencyCode { get; set; }
}
/// <summary>An Italian province (code = the two-letter abbreviation).</summary>
public sealed class Province : CodedEntity
{
/// <summary>The region.</summary>
public string? Region { get; set; }
}
/// <summary>An Italian municipality (code = cadastral code, used by the fiscal code).</summary>
public sealed class Municipality : CodedEntity
{
/// <summary>The province abbreviation.</summary>
public string ProvinceCode { get; set; } = string.Empty;
/// <summary>The region.</summary>
public string? Region { get; set; }
/// <summary>The ISTAT code (6 digits).</summary>
public string? IstatCode { get; set; }
/// <summary>The postal codes, comma separated (one for most municipalities, a range for large cities).</summary>
public string? PostalCodes { get; set; }
}
/// <summary>A currency (ISO 4217).</summary>
public sealed class Currency : CodedEntity
{
/// <summary>The symbol, such as €.</summary>
public string? Symbol { get; set; }
/// <summary>Decimals of amounts.</summary>
public int Decimals { get; set; } = 2;
}
/// <summary>An exchange rate: units of the currency for one euro on a day (ECB reference rate or manual).</summary>
public sealed class ExchangeRate : Entity<long>
{
/// <summary>The currency (ISO 4217).</summary>
public string CurrencyCode { get; set; } = string.Empty;
/// <summary>The day.</summary>
public DateOnly RateDate { get; set; }
/// <summary>Units of the currency for one euro.</summary>
public decimal Rate { get; set; }
/// <summary>Where the rate comes from: <c>ECB</c> or <c>MANUAL</c>.</summary>
public string Source { get; set; } = "MANUAL";
}
/// <summary>A unit of measure.</summary>
public sealed class UnitOfMeasure : CodedEntity
{
/// <summary>Decimals allowed in quantities.</summary>
public int Decimals { get; set; }
/// <summary>The text written in <c>UnitaMisura</c> of the e-invoice, when different from the code.</summary>
public string? EInvoiceCode { get; set; }
}
/// <summary>A VAT code (mockup 076).</summary>
public sealed class TaxCode : CodedEntity
{
/// <summary>The rate in percent.</summary>
public decimal Rate { get; set; }
/// <summary>The nature for zero-rate operations (N1...N7).</summary>
public string? Nature { get; set; }
/// <summary>The kind of operation, from rate and nature.</summary>
public TaxCodeKind Kind { get; set; }
/// <summary>When the tax becomes due.</summary>
public TaxExigibility Exigibility { get; set; }
/// <summary>Share of the purchase tax that cannot be deducted.</summary>
public decimal NonDeductiblePercent { get; set; }
/// <summary>The law printed on the invoice.</summary>
public string? LegalReference { get; set; }
/// <summary>First day of validity.</summary>
public DateOnly? ValidFrom { get; set; }
/// <summary>Last day of validity.</summary>
public DateOnly? ValidTo { get; set; }
/// <summary>The VAT account for sales (ledger, M7).</summary>
public string? SalesAccountCode { get; set; }
/// <summary>The VAT account for purchases (ledger, M7).</summary>
public string? PurchaseAccountCode { get; set; }
}
/// <summary>A payment method (MP01...MP23).</summary>
public sealed class PaymentMethod : CodedEntity
{
/// <summary>The FatturaPA code, such as <c>MP05</c>.</summary>
public string EInvoiceCode { get; set; } = "MP05";
/// <summary>Whether a bank of the customer (RiBa, direct debit) or of the company (transfer) is needed.</summary>
public bool NeedsBank { get; set; }
}
/// <summary>Payment terms with their installments (lines).</summary>
public sealed class PaymentTerm : CodedEntity
{
/// <summary>The payment method.</summary>
public long? PaymentMethodId { get; set; }
/// <summary>The FatturaPA condition: TP01 installments, TP02 full, TP03 advance.</summary>
public string Condition { get; set; } = "TP02";
/// <summary>All the VAT on the first installment.</summary>
public bool VatOnFirstInstallment { get; set; }
}
/// <summary>An installment of payment terms.</summary>
public sealed class PaymentTermLine : Entity<long>
{
/// <summary>The payment terms.</summary>
public long PaymentTermId { get; set; }
/// <summary>The order of the installment, from 1.</summary>
public int Number { get; set; }
/// <summary>Days from the document date.</summary>
public int Days { get; set; }
/// <summary>Move the due date to the end of the month.</summary>
public bool EndOfMonth { get; set; }
/// <summary>A fixed day of the month after the end-of-month move, such as 10.</summary>
public int? FixedDay { get; set; }
/// <summary>Share of the amount, in percent.</summary>
public decimal Percent { get; set; }
}
/// <summary>A bank account of the company.</summary>
public sealed class BankAccount : CodedEntity
{
/// <summary>The bank.</summary>
public string? BankName { get; set; }
/// <summary>The IBAN, compact.</summary>
public string? Iban { get; set; }
/// <summary>The BIC.</summary>
public string? Bic { get; set; }
/// <summary>The SIA code for RiBa files.</summary>
public string? SiaCode { get; set; }
/// <summary>The CBI CUC code.</summary>
public string? CucCode { get; set; }
/// <summary>The ledger account (M7).</summary>
public string? AccountCode { get; set; }
/// <summary>The account proposed on documents.</summary>
public bool IsDefault { get; set; }
}
/// <summary>A sales agent (code + name in <see cref="CodedEntity.Description"/>).</summary>
public sealed class Salesperson : CodedEntity
{
/// <summary>The e-mail address.</summary>
public string? Email { get; set; }
/// <summary>The phone.</summary>
public string? Phone { get; set; }
/// <summary>The usual commission, in percent.</summary>
public decimal CommissionPercent { get; set; }
/// <summary>Subject to ENASARCO contributions.</summary>
public bool Enasarco { get; set; }
}
/// <summary>A carrier (code + name).</summary>
public sealed class Carrier : CodedEntity
{
/// <summary>The VAT number.</summary>
public string? VatNumber { get; set; }
/// <summary>The address.</summary>
public string? Address { get; set; }
/// <summary>The town.</summary>
public string? City { get; set; }
/// <summary>The phone.</summary>
public string? Phone { get; set; }
/// <summary>The e-mail address.</summary>
public string? Email { get; set; }
/// <summary>The registration number in the haulage register.</summary>
public string? RegisterNumber { get; set; }
}
/// <summary>Who pays the transport (porto: franco, assegnato...).</summary>
public sealed class ShippingTerm : CodedEntity;
/// <summary>Who carries the goods (mittente, destinatario, vettore).</summary>
public sealed class TransportMean : CodedEntity;
/// <summary>The appearance of the goods on the delivery note (cartoni, bancali...).</summary>
public sealed class GoodsAppearance : CodedEntity;
/// <summary>An Incoterms rule.</summary>
public sealed class DeliveryTerm : CodedEntity;
/// <summary>The reason of the transport (vendita, conto visione...).</summary>
public sealed class TransportReason : CodedEntity;
/// <summary>What a stock movement does to the quantity on hand.</summary>
public enum StockEffect
{
/// <summary>No effect on quantities.</summary>
None = 0,
/// <summary>Increases the stock (load).</summary>
Load = 1,
/// <summary>Decreases the stock (unload).</summary>
Unload = 2,
}
/// <summary>A warehouse movement reason.</summary>
public sealed class InventoryReason : CodedEntity
{
/// <summary>The effect on the quantity.</summary>
public StockEffect Effect { get; set; }
/// <summary>Whether it changes the stock value (purchases, sales) or only quantities (transfers).</summary>
public bool AffectsValue { get; set; } = true;
/// <summary>Whether it is a transfer between warehouses (needs a destination).</summary>
public bool IsTransfer { get; set; }
}
/// <summary>A warehouse.</summary>
public sealed class Warehouse : CodedEntity
{
/// <summary>The address.</summary>
public string? Address { get; set; }
/// <summary>The town.</summary>
public string? City { get; set; }
/// <summary>The province abbreviation.</summary>
public string? ProvinceCode { get; set; }
/// <summary>The warehouse proposed on documents.</summary>
public bool IsDefault { get; set; }
}
/// <summary>A category of items, on up to three levels.</summary>
public sealed class ItemCategory : CodedEntity
{
/// <summary>The parent category.</summary>
public long? ParentId { get; set; }
/// <summary>The level, 1 to 3.</summary>
public int Level { get; set; } = 1;
}
/// <summary>A category of customers and suppliers.</summary>
public sealed class PartyCategory : CodedEntity;
/// <summary>A price list (the prices are in <c>item_prices</c>, mockup 018).</summary>
public sealed class PriceList : CodedEntity
{
/// <summary>The currency (ISO 4217).</summary>
public string CurrencyCode { get; set; } = "EUR";
/// <summary>Prices include VAT (retail).</summary>
public bool VatIncluded { get; set; }
/// <summary>First day of validity.</summary>
public DateOnly? ValidFrom { get; set; }
/// <summary>Last day of validity.</summary>
public DateOnly? ValidTo { get; set; }
/// <summary>The list this one derives from.</summary>
public long? BasePriceListId { get; set; }
/// <summary>Change applied to the base list, in percent (negative for a discount).</summary>
public decimal MarkupPercent { get; set; }
/// <summary>Rounding step of derived prices, such as 0.05; zero for none.</summary>
public decimal RoundingStep { get; set; }
}
/// <summary>A discount policy: a chain of discounts such as <c>10+5</c>.</summary>
public sealed class DiscountPolicy : CodedEntity
{
/// <summary>The chain of discounts in percent, separated by +.</summary>
public string Formula { get; set; } = string.Empty;
}
/// <summary>The family of a document numerator.</summary>
public enum NumeratorKind
{
/// <summary>Quotations.</summary>
Quotation = 0,
/// <summary>Customer orders.</summary>
SalesOrder = 1,
/// <summary>Delivery notes.</summary>
DeliveryNote = 2,
/// <summary>Sales invoices and credit notes (no gaps).</summary>
SalesInvoice = 3,
/// <summary>Supplier orders.</summary>
PurchaseOrder = 4,
/// <summary>Purchase protocol.</summary>
PurchaseInvoice = 5,
/// <summary>Warehouse movements.</summary>
Inventory = 6,
/// <summary>Journal entries (no gaps).</summary>
Journal = 7,
/// <summary>Anything else.</summary>
Other = 8,
}
/// <summary>A document numerator for a year (assignment at confirmation arrives with the documents, M5).</summary>
public sealed class DocumentNumerator : CodedEntity
{
/// <summary>The family.</summary>
public NumeratorKind Kind { get; set; }
/// <summary>The section (sezionale), such as <c>A</c>; empty for the main one.</summary>
public string? Section { get; set; }
/// <summary>The year.</summary>
public int Year { get; set; }
/// <summary>The prefix.</summary>
public string? Prefix { get; set; }
/// <summary>The format, such as <c>{prefix}/{year}/{number:D6}</c>.</summary>
public string Format { get; set; } = "{number}/{year}";
/// <summary>The last number assigned.</summary>
public long LastNumber { get; set; }
/// <summary>Whether gaps are allowed (never for invoices, credit notes and final entries).</summary>
public bool GapsAllowed { get; set; }
}
/// <summary>A type of document: the links between a document and its numerator, warehouse reason and SDI type.</summary>
public sealed class DocumentType : CodedEntity
{
/// <summary>The family of document.</summary>
public NumeratorKind Kind { get; set; }
/// <summary>The numerator.</summary>
public long? NumeratorId { get; set; }
/// <summary>The warehouse reason.</summary>
public long? InventoryReasonId { get; set; }
/// <summary>The SDI document type (TD01...).</summary>
public string? EInvoiceType { get; set; }
/// <summary>The journal template (ledger, M7).</summary>
public string? JournalTemplateCode { get; set; }
/// <summary>The VAT register (ledger, M7).</summary>
public string? VatRegisterCode { get; set; }
/// <summary>The print template.</summary>
public string? PrintTemplate { get; set; }
}
/// <summary>A cost centre.</summary>
public sealed class CostCenter : CodedEntity
{
/// <summary>The parent cost centre.</summary>
public long? ParentId { get; set; }
}
/// <summary>The state of a job.</summary>
public enum JobStatus
{
/// <summary>Open.</summary>
Open = 0,
/// <summary>Suspended.</summary>
Suspended = 1,
/// <summary>Closed.</summary>
Closed = 2,
}
/// <summary>A job (commessa).</summary>
public sealed class Job : CodedEntity
{
/// <summary>The customer, when the job is for one.</summary>
public long? PartyId { get; set; }
/// <summary>The start.</summary>
public DateOnly? StartDate { get; set; }
/// <summary>The end.</summary>
public DateOnly? EndDate { get; set; }
/// <summary>The state.</summary>
public JobStatus Status { get; set; }
}