using Khaemhat.Application.Tables;
using Khaemhat.Core.Tables;
using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Web.Tables;
/// How a field of a company table is edited.
public enum TableFieldKind
{
/// Free text.
Text = 0,
/// A short code in monospace, upper case.
Code = 1,
/// A number (decimal or integer property).
Number = 2,
/// A yes/no flag.
Bool = 3,
/// A date.
Date = 4,
/// A value of an enumeration.
Choice = 5,
/// A row of another company table, by id or by code.
Lookup = 6,
/// A code of a list of the fiscal pack (payment methods of the e-invoice, VAT natures).
CodeList = 7,
}
/// A field of the detail panel of a company table (mockup 076).
/// The property of the entity; its label is CompanyTables.Fields.<Property>.
/// How it is edited.
public sealed record TableField(string Property, TableFieldKind Kind = TableFieldKind.Text)
{
/// The columns of the form it takes, out of 12.
public int Span { get; init; } = 6;
/// The decimals of a number.
public int Decimals { get; init; }
/// The smallest number.
public decimal? Min { get; init; }
/// The largest number.
public decimal? Max { get; init; }
/// The longest text.
public int? MaxLength { get; init; }
/// Whether the field must be filled.
public bool Required { get; init; }
/// The table a lookup points to.
public Type? Lookup { get; init; }
/// Whether a lookup stores the code (a string property) instead of the id.
public bool ByCode { get; init; }
/// The list of the fiscal pack a field takes its codes from.
public Func? CodeList { get; init; }
/// Whether the field is also a column of the grid.
public bool InGrid { get; init; }
/// The width of the grid column.
public string? Width { get; init; }
/// Whether the value is computed and only shown.
public bool ReadOnly { get; init; }
/// The fixed values of a on a text property; their texts are CompanyTables.Values.<Property>.<Value>.
public IReadOnlyList? Values { get; init; }
/// The rows of a multi-line text.
public int Rows { get; init; }
/// The text key of the label.
public string LabelKey => "CompanyTables.Fields." + Property;
}
/// A company table as the interface shows it: address, icon, group and fields.
/// The last part of the address, such as codici-iva.
/// The key of the , such as tax-codes.
/// The entity.
/// The icon.
/// The group of the index page.
public sealed record TableScreen(string Slug, string Key, Type Entity, string Icon, TableGroup Group)
{
/// The fields after code and description, in order.
public IReadOnlyList Fields { get; init; } = [];
/// The address.
public string Href => TableScreens.BasePath + "/" + Slug;
/// The text key of the title.
public string TitleKey => "CompanyTables.Screens." + Key + ".Title";
/// The text key of the one-line description.
public string HintKey => "CompanyTables.Screens." + Key + ".Hint";
}
/// The groups of the index page of the company tables.
public enum TableGroup
{
/// VAT, payments, banks, currencies.
Fiscal = 0,
/// Documents, numbering, price lists, discounts.
Sales = 1,
/// Warehouses, reasons, units, transport.
Logistics = 2,
/// Countries, provinces, municipalities.
Geography = 3,
/// Categories, salespeople, cost centres, jobs.
Classification = 4,
}
///
/// The company tables of SPEC ยง11.1 with the fields of their detail panel. The page /tabelle/{slug} is generic
/// (KhTableEditor); the company profile, the fiscal years and the exchange rates have pages of their own.
///
public static class TableScreens
{
/// The address of the area.
public const string BasePath = "/tabelle";
/// Every screen, in the order of the index page.
public static IReadOnlyList 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.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.GapsAllowed), TableFieldKind.Bool) { Span = 8 },
],
},
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 },
],
},
];
/// The screen of an address.
/// The last part of the address.
/// The screen, or .
public static TableScreen? Find(string? slug) =>
All.FirstOrDefault(s => string.Equals(s.Slug, slug, StringComparison.OrdinalIgnoreCase));
/// The screen of an entity.
/// The entity.
/// The screen, or .
public static TableScreen? Of(Type entity) => All.FirstOrDefault(s => s.Entity == entity);
/// The text key of a value of an enumeration: CompanyTables.Enums.<Type>.<Value>.
/// The value.
/// The key.
public static string EnumKey(Enum value)
{
ArgumentNullException.ThrowIfNull(value);
return "CompanyTables.Enums." + value.GetType().Name + "." + value;
}
private static Func EInvoice(Func list) =>
pack => pack.GetFeature() is { } format ? list(format) : null;
}