Files
Khaemhat/src/Khaemhat.Core/Tables/BaseTables.cs
T
Alby96andClaude Opus 5.5 3dbbaba86e feat: items and price lists with dated prices, derived lists and demo catalogue (#42)
Items get their own archive and record (mockup 017): codes from the
per-category mask, EAN and NC8 checks, alternative units and bar codes,
suppliers with the usual one, substitutes, notes and history. Only goods
can be stocked; stock itself comes with the warehouse in M6. Costs are
hidden from users without items.cost.read and kept as they were when
those users save. A used item is disabled instead of deleted.

Prices live in one item_prices table, either for a price list or as a
customer's net price, each valid from a date, so documents keep the price
of their day. Derived lists compute a price from their base list with
markup and rounding when they have none of their own. The price grid
(mockup 018) edits every list at one validity date, keeps changes pending
and highlighted until saved, and computes mass updates as a preview.
PriceListService.QuoteAsync gives documents (M5) the price search order
net, list, base, item price with its source.

Parties get the "Articoli e codici" tab: the codes a customer or supplier
uses for our items, found by the item search in that party's documents.

Demo data adds 120 items of a plumbing wholesaler, the PUBB and RIV lists,
last year's prices, suppliers, units, codes and a customer's net prices;
seed --demo [--scale N] loads them from the command line and repeats the
catalogue for performance tests.

ADR 0030 records the decisions; wiki, data model, CHANGELOG and STATUS
are updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 11:06:57 +01:00

473 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>The base price list of the company: the third step of the price search (SPEC §11.3, ADR 0030).</summary>
public bool IsBase { 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; }
}