Files
Khaemhat/src/Khaemhat.Web/Tables/TableScreens.cs
T
Alby96andClaude Opus 5.5 ddd2273f7e feat: document engine with kinds, states, atomic numbering and fulfilment (#47)
One engine for sales, purchases and warehouse documents (ADR 0032). Each document
type now carries a kind that decides its family of tables, life cycle, effects,
accepted lines, the kinds it fulfils and its permissions, so company-defined types
work without code.

- Six families of header and line tables plus document_links; document_types gets
  document_kind (seeded types filled by the migration and the Italian template),
  document_numerators gets last_date.
- DocumentStateMachine with the order, invoice and simple life cycles and the
  actions each state admits; issued invoices are immutable and are cancelled with
  a reason only before the file reaches the exchange system.
- DocumentNumberingService numbers at confirmation inside the transaction under
  the numerator lock (advisory lock on PostgreSQL, write lock on SQLite): no
  duplicates, a rollback gives the number back, out-of-sequence dates refused on
  gap-free numerators, the new year's numerator created on first use, gap report.
- DocumentEngine, the generic DocumentService<THeader, TLine> and
  DocumentCopyService: save, confirm or issue, cancel, close, reopen, delete,
  duplicate and transform; links are recorded at confirmation and removed at
  cancellation, and FulfillmentCalculator recomputes quantities and states.
- Tests: rules in Core, concurrent numbering on SQLite and PostgreSQL (data tests,
  run on PostgreSQL 18 locally), order to delivery notes to deferred invoice on a
  real instance. Wiki (Vendite, Tabelle azienda), CHANGELOG, STATUS, DATA-MODEL.

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

363 lines
19 KiB
C#

using Khaemhat.Application.Tables;
using Khaemhat.Core.Tables;
using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Web.Tables;
/// <summary>How a field of a company table is edited.</summary>
public enum TableFieldKind
{
/// <summary>Free text.</summary>
Text = 0,
/// <summary>A short code in monospace, upper case.</summary>
Code = 1,
/// <summary>A number (decimal or integer property).</summary>
Number = 2,
/// <summary>A yes/no flag.</summary>
Bool = 3,
/// <summary>A date.</summary>
Date = 4,
/// <summary>A value of an enumeration.</summary>
Choice = 5,
/// <summary>A row of another company table, by id or by code.</summary>
Lookup = 6,
/// <summary>A code of a list of the fiscal pack (payment methods of the e-invoice, VAT natures).</summary>
CodeList = 7,
}
/// <summary>A field of the detail panel of a company table (mockup 076).</summary>
/// <param name="Property">The property of the entity; its label is <c>CompanyTables.Fields.&lt;Property&gt;</c>.</param>
/// <param name="Kind">How it is edited.</param>
public sealed record TableField(string Property, TableFieldKind Kind = TableFieldKind.Text)
{
/// <summary>The columns of the form it takes, out of 12.</summary>
public int Span { get; init; } = 6;
/// <summary>The decimals of a number.</summary>
public int Decimals { get; init; }
/// <summary>The smallest number.</summary>
public decimal? Min { get; init; }
/// <summary>The largest number.</summary>
public decimal? Max { get; init; }
/// <summary>The longest text.</summary>
public int? MaxLength { get; init; }
/// <summary>Whether the field must be filled.</summary>
public bool Required { get; init; }
/// <summary>The table a lookup points to.</summary>
public Type? Lookup { get; init; }
/// <summary>Whether a lookup stores the code (a string property) instead of the id.</summary>
public bool ByCode { get; init; }
/// <summary>The list of the fiscal pack a <see cref="TableFieldKind.CodeList"/> field takes its codes from.</summary>
public Func<IFiscalPack, CodeList?>? CodeList { get; init; }
/// <summary>Whether the field is also a column of the grid.</summary>
public bool InGrid { get; init; }
/// <summary>The width of the grid column.</summary>
public string? Width { get; init; }
/// <summary>Whether the value is computed and only shown.</summary>
public bool ReadOnly { get; init; }
/// <summary>The fixed values of a <see cref="TableFieldKind.Choice"/> on a text property; their texts are <c>CompanyTables.Values.&lt;Property&gt;.&lt;Value&gt;</c>.</summary>
public IReadOnlyList<string>? Values { get; init; }
/// <summary>The rows of a multi-line text.</summary>
public int Rows { get; init; }
/// <summary>The text key of the label.</summary>
public string LabelKey => "CompanyTables.Fields." + Property;
}
/// <summary>A company table as the interface shows it: address, icon, group and fields.</summary>
/// <param name="Slug">The last part of the address, such as <c>codici-iva</c>.</param>
/// <param name="Key">The key of the <see cref="TableDefinition"/>, such as <c>tax-codes</c>.</param>
/// <param name="Entity">The entity.</param>
/// <param name="Icon">The icon.</param>
/// <param name="Group">The group of the index page.</param>
public sealed record TableScreen(string Slug, string Key, Type Entity, string Icon, TableGroup Group)
{
/// <summary>The fields after code and description, in order.</summary>
public IReadOnlyList<TableField> Fields { get; init; } = [];
/// <summary>The address.</summary>
public string Href => TableScreens.BasePath + "/" + Slug;
/// <summary>The text key of the title.</summary>
public string TitleKey => "CompanyTables.Screens." + Key + ".Title";
/// <summary>The text key of the one-line description.</summary>
public string HintKey => "CompanyTables.Screens." + Key + ".Hint";
}
/// <summary>The groups of the index page of the company tables.</summary>
public enum TableGroup
{
/// <summary>VAT, payments, banks, currencies.</summary>
Fiscal = 0,
/// <summary>Documents, numbering, price lists, discounts.</summary>
Sales = 1,
/// <summary>Warehouses, reasons, units, transport.</summary>
Logistics = 2,
/// <summary>Countries, provinces, municipalities.</summary>
Geography = 3,
/// <summary>Categories, salespeople, cost centres, jobs.</summary>
Classification = 4,
}
/// <summary>
/// The company tables of SPEC §11.1 with the fields of their detail panel. The page <c>/tabelle/{slug}</c> is generic
/// (<c>KhTableEditor</c>); the company profile, the fiscal years and the exchange rates have pages of their own.
/// </summary>
public static class TableScreens
{
/// <summary>The address of the area.</summary>
public const string BasePath = "/tabelle";
/// <summary>Every screen, in the order of the index page.</summary>
public static IReadOnlyList<TableScreen> All { get; } =
[
new("codici-iva", "tax-codes", typeof(TaxCode), "euro", TableGroup.Fiscal)
{
Fields =
[
new(nameof(TaxCode.Rate), TableFieldKind.Number) { Decimals = 2, Min = 0, Max = 100, InGrid = true, Width = "80px", Span = 3 },
new(nameof(TaxCode.Nature), TableFieldKind.CodeList) { CodeList = p => p.TaxCodes.Natures, InGrid = true, Width = "90px", Span = 3 },
new(nameof(TaxCode.Kind), TableFieldKind.Choice) { ReadOnly = true, InGrid = true, Width = "150px", Span = 3 },
new(nameof(TaxCode.Exigibility), TableFieldKind.Choice) { Span = 3 },
new(nameof(TaxCode.NonDeductiblePercent), TableFieldKind.Number) { Decimals = 2, Min = 0, Max = 100, Span = 3 },
new(nameof(TaxCode.ValidFrom), TableFieldKind.Date) { Span = 3 },
new(nameof(TaxCode.ValidTo), TableFieldKind.Date) { Span = 3 },
new(nameof(TaxCode.LegalReference)) { Span = 12, InGrid = true },
new(nameof(TaxCode.SalesAccountCode), TableFieldKind.Code),
new(nameof(TaxCode.PurchaseAccountCode), TableFieldKind.Code),
],
},
new("modalita-pagamento", "payment-methods", typeof(PaymentMethod), "coins", TableGroup.Fiscal)
{
Fields =
[
new(nameof(PaymentMethod.EInvoiceCode), TableFieldKind.CodeList) { CodeList = EInvoice(f => f.PaymentMethods), Required = true, InGrid = true, Width = "90px" },
new(nameof(PaymentMethod.NeedsBank), TableFieldKind.Bool) { InGrid = true, Width = "90px" },
],
},
new("pagamenti", "payment-terms", typeof(PaymentTerm), "calendar", TableGroup.Fiscal)
{
Fields =
[
new(nameof(PaymentTerm.PaymentMethodId), TableFieldKind.Lookup) { Lookup = typeof(PaymentMethod), InGrid = true, Width = "160px" },
new(nameof(PaymentTerm.Condition), TableFieldKind.CodeList) { CodeList = EInvoice(f => f.PaymentConditions), Required = true, InGrid = true, Width = "90px" },
new(nameof(PaymentTerm.VatOnFirstInstallment), TableFieldKind.Bool) { Span = 12 },
],
},
new("banche", "bank-accounts", typeof(BankAccount), "building", TableGroup.Fiscal)
{
Fields =
[
new(nameof(BankAccount.BankName)) { Span = 12, InGrid = true },
new(nameof(BankAccount.Iban), TableFieldKind.Code) { Span = 8, MaxLength = 34, InGrid = true, Width = "260px" },
new(nameof(BankAccount.Bic), TableFieldKind.Code) { Span = 4, MaxLength = 11 },
new(nameof(BankAccount.SiaCode), TableFieldKind.Code) { Span = 4, MaxLength = 5 },
new(nameof(BankAccount.CucCode), TableFieldKind.Code) { Span = 4, MaxLength = 8 },
new(nameof(BankAccount.AccountCode), TableFieldKind.Code) { Span = 4 },
new(nameof(BankAccount.IsDefault), TableFieldKind.Bool) { Span = 12, InGrid = true, Width = "90px" },
],
},
new("valute", "currencies", typeof(Currency), "euro", TableGroup.Fiscal)
{
Fields =
[
new(nameof(Currency.Symbol)) { MaxLength = 5, InGrid = true, Width = "80px", Span = 3 },
new(nameof(Currency.Decimals), TableFieldKind.Number) { Min = 0, Max = 4, InGrid = true, Width = "80px", Span = 3 },
],
},
new("tipi-documento", "document-types", typeof(DocumentType), "file", TableGroup.Sales)
{
Fields =
[
new(nameof(DocumentType.Kind), TableFieldKind.Choice) { Required = true, InGrid = true, Width = "170px" },
new(nameof(DocumentType.DocumentKind), TableFieldKind.Choice) { Required = true, InGrid = true, Width = "190px" },
new(nameof(DocumentType.NumeratorId), TableFieldKind.Lookup) { Lookup = typeof(DocumentNumerator), InGrid = true, Width = "120px" },
new(nameof(DocumentType.InventoryReasonId), TableFieldKind.Lookup) { Lookup = typeof(InventoryReason) },
new(nameof(DocumentType.EInvoiceType), TableFieldKind.CodeList) { CodeList = EInvoice(f => f.DocumentTypes), InGrid = true, Width = "90px" },
new(nameof(DocumentType.VatRegisterCode), TableFieldKind.Code) { Span = 4 },
new(nameof(DocumentType.JournalTemplateCode), TableFieldKind.Code) { Span = 4 },
new(nameof(DocumentType.PrintTemplate), TableFieldKind.Code) { Span = 4 },
],
},
new("numeratori", "numerators", typeof(DocumentNumerator), "list", TableGroup.Sales)
{
Fields =
[
new(nameof(DocumentNumerator.Year), TableFieldKind.Number) { Min = 2000, Max = 2100, Required = true, InGrid = true, Width = "70px", Span = 3 },
new(nameof(DocumentNumerator.Kind), TableFieldKind.Choice) { Required = true, InGrid = true, Width = "170px", Span = 5 },
new(nameof(DocumentNumerator.Section), TableFieldKind.Code) { MaxLength = 10, InGrid = true, Width = "80px", Span = 4 },
new(nameof(DocumentNumerator.Prefix), TableFieldKind.Code) { MaxLength = 10, Span = 4 },
new(nameof(DocumentNumerator.Format)) { Required = true, Span = 8, InGrid = true, Width = "180px" },
new(nameof(DocumentNumerator.LastNumber), TableFieldKind.Number) { Min = 0, InGrid = true, Width = "100px", Span = 4 },
new(nameof(DocumentNumerator.LastDate), TableFieldKind.Date) { Span = 4 },
new(nameof(DocumentNumerator.GapsAllowed), TableFieldKind.Bool) { Span = 4 },
],
},
new("listini", "price-lists", typeof(PriceList), "tag", TableGroup.Sales)
{
Fields =
[
new(nameof(PriceList.CurrencyCode), TableFieldKind.Lookup) { Lookup = typeof(Currency), ByCode = true, Required = true, InGrid = true, Width = "80px", Span = 4 },
new(nameof(PriceList.VatIncluded), TableFieldKind.Bool) { InGrid = true, Width = "90px", Span = 4 },
new(nameof(PriceList.IsBase), TableFieldKind.Bool) { InGrid = true, Width = "90px", Span = 4 },
new(nameof(PriceList.ValidFrom), TableFieldKind.Date),
new(nameof(PriceList.ValidTo), TableFieldKind.Date),
new(nameof(PriceList.BasePriceListId), TableFieldKind.Lookup) { Lookup = typeof(PriceList) },
new(nameof(PriceList.MarkupPercent), TableFieldKind.Number) { Decimals = 2, Span = 3 },
new(nameof(PriceList.RoundingStep), TableFieldKind.Number) { Decimals = 4, Min = 0, Span = 3 },
],
},
new("sconti", "discount-policies", typeof(DiscountPolicy), "tag", TableGroup.Sales)
{
Fields = [new(nameof(DiscountPolicy.Formula), TableFieldKind.Code) { Required = true, InGrid = true, Width = "140px" }],
},
new("agenti", "salespeople", typeof(Salesperson), "user", TableGroup.Sales)
{
Fields =
[
new(nameof(Salesperson.Email)) { InGrid = true },
new(nameof(Salesperson.Phone)),
new(nameof(Salesperson.CommissionPercent), TableFieldKind.Number) { Decimals = 2, Min = 0, Max = 100, InGrid = true, Width = "90px" },
new(nameof(Salesperson.Enasarco), TableFieldKind.Bool),
],
},
new("depositi", "warehouses", typeof(Warehouse), "box", TableGroup.Logistics)
{
Fields =
[
new(nameof(Warehouse.Address)) { Span = 12 },
new(nameof(Warehouse.City)) { Span = 8, InGrid = true },
new(nameof(Warehouse.ProvinceCode), TableFieldKind.Code) { MaxLength = 2, Span = 4, InGrid = true, Width = "60px" },
new(nameof(Warehouse.IsDefault), TableFieldKind.Bool) { Span = 12, InGrid = true, Width = "90px" },
],
},
new("causali-magazzino", "inventory-reasons", typeof(InventoryReason), "activity", TableGroup.Logistics)
{
Fields =
[
new(nameof(InventoryReason.Effect), TableFieldKind.Choice) { InGrid = true, Width = "110px", Span = 4 },
new(nameof(InventoryReason.AffectsValue), TableFieldKind.Bool) { InGrid = true, Width = "90px", Span = 4 },
new(nameof(InventoryReason.IsTransfer), TableFieldKind.Bool) { InGrid = true, Width = "90px", Span = 4 },
],
},
new("unita-di-misura", "units", typeof(UnitOfMeasure), "grid", TableGroup.Logistics)
{
Fields =
[
new(nameof(UnitOfMeasure.Decimals), TableFieldKind.Number) { Min = 0, Max = 6, InGrid = true, Width = "80px", Span = 3 },
new(nameof(UnitOfMeasure.EInvoiceCode), TableFieldKind.Code) { MaxLength = 10, InGrid = true, Width = "100px", Span = 3 },
],
},
new("vettori", "carriers", typeof(Carrier), "truck", TableGroup.Logistics)
{
Fields =
[
new(nameof(Carrier.VatNumber), TableFieldKind.Code) { InGrid = true, Width = "130px" },
new(nameof(Carrier.RegisterNumber), TableFieldKind.Code),
new(nameof(Carrier.Address)) { Span = 12 },
new(nameof(Carrier.City)) { InGrid = true },
new(nameof(Carrier.Phone)),
new(nameof(Carrier.Email)) { Span = 12 },
],
},
new("porto", "shipping-terms", typeof(ShippingTerm), "truck", TableGroup.Logistics),
new("trasporto-a-cura", "transport-means", typeof(TransportMean), "truck", TableGroup.Logistics),
new("causali-trasporto", "transport-reasons", typeof(TransportReason), "truck", TableGroup.Logistics),
new("aspetto-beni", "goods-appearances", typeof(GoodsAppearance), "box", TableGroup.Logistics),
new("rese", "delivery-terms", typeof(DeliveryTerm), "globe", TableGroup.Logistics),
new("nazioni", "countries", typeof(Country), "globe", TableGroup.Geography)
{
Fields =
[
new(nameof(Country.Iso3), TableFieldKind.Code) { MaxLength = 3, InGrid = true, Width = "70px", Span = 3 },
new(nameof(Country.VatPrefix), TableFieldKind.Code) { MaxLength = 2, InGrid = true, Width = "80px", Span = 3 },
new(nameof(Country.CurrencyCode), TableFieldKind.Lookup) { Lookup = typeof(Currency), ByCode = true, InGrid = true, Width = "80px", Span = 3 },
new(nameof(Country.EuMember), TableFieldKind.Bool) { InGrid = true, Width = "70px", Span = 3 },
],
},
new("province", "provinces", typeof(Province), "pin", TableGroup.Geography)
{
Fields = [new(nameof(Province.Region)) { InGrid = true }],
},
new("comuni", "municipalities", typeof(Municipality), "pin", TableGroup.Geography)
{
Fields =
[
new(nameof(Municipality.ProvinceCode), TableFieldKind.Code) { MaxLength = 2, Required = true, InGrid = true, Width = "60px", Span = 3 },
new(nameof(Municipality.IstatCode), TableFieldKind.Code) { MaxLength = 6, InGrid = true, Width = "90px", Span = 3 },
new(nameof(Municipality.Region)) { InGrid = true },
new(nameof(Municipality.PostalCodes)) { Span = 12, InGrid = true, Width = "160px" },
],
},
new("categorie-articoli", "item-categories", typeof(ItemCategory), "layers", TableGroup.Classification)
{
Fields =
[
new(nameof(ItemCategory.ParentId), TableFieldKind.Lookup) { Lookup = typeof(ItemCategory), InGrid = true, Width = "140px" },
new(nameof(ItemCategory.Level), TableFieldKind.Number) { ReadOnly = true, InGrid = true, Width = "70px", Span = 3 },
],
},
new("categorie-soggetti", "party-categories", typeof(PartyCategory), "users", TableGroup.Classification),
new("centri-di-costo", "cost-centers", typeof(CostCenter), "target", TableGroup.Classification)
{
Fields = [new(nameof(CostCenter.ParentId), TableFieldKind.Lookup) { Lookup = typeof(CostCenter), InGrid = true, Width = "140px" }],
},
new("commesse", "jobs", typeof(Job), "folder", TableGroup.Classification)
{
Fields =
[
new(nameof(Job.Status), TableFieldKind.Choice) { InGrid = true, Width = "110px", Span = 4 },
new(nameof(Job.StartDate), TableFieldKind.Date) { InGrid = true, Width = "100px", Span = 4 },
new(nameof(Job.EndDate), TableFieldKind.Date) { Span = 4 },
],
},
];
/// <summary>The screen of an address.</summary>
/// <param name="slug">The last part of the address.</param>
/// <returns>The screen, or <see langword="null"/>.</returns>
public static TableScreen? Find(string? slug) =>
All.FirstOrDefault(s => string.Equals(s.Slug, slug, StringComparison.OrdinalIgnoreCase));
/// <summary>The screen of an entity.</summary>
/// <param name="entity">The entity.</param>
/// <returns>The screen, or <see langword="null"/>.</returns>
public static TableScreen? Of(Type entity) => All.FirstOrDefault(s => s.Entity == entity);
/// <summary>The text key of a value of an enumeration: <c>CompanyTables.Enums.&lt;Type&gt;.&lt;Value&gt;</c>.</summary>
/// <param name="value">The value.</param>
/// <returns>The key.</returns>
public static string EnumKey(Enum value)
{
ArgumentNullException.ThrowIfNull(value);
return "CompanyTables.Enums." + value.GetType().Name + "." + value;
}
private static Func<IFiscalPack, CodeList?> EInvoice(Func<IEInvoiceFormat, CodeList> list) =>
pack => pack.GetFeature<IEInvoiceFormat>() is { } format ? list(format) : null;
}