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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 11:06:57 +01:00
1 parent c8241a414e
commit 3dbbaba86e
46 files changed
+5318 -144

No files matched your search

+234
View File
@@ -0,0 +1,234 @@
using Khaemhat.Core.Entities;
namespace Khaemhat.Core.Items;
/// <summary>What an item is (SPEC §11.2).</summary>
public enum ItemKind
{
/// <summary>Goods bought or sold; kept in stock when <see cref="Item.Stocked"/>.</summary>
Goods = 0,
/// <summary>A service, such as installation or hours of assistance.</summary>
Service = 1,
/// <summary>An ancillary expense on invoices (transport, collection).</summary>
Expense = 2,
/// <summary>A recurring descriptive line.</summary>
Text = 3,
/// <summary>A fixed asset (linked to the asset register with the ASSET module).</summary>
Asset = 4,
}
/// <summary>Where the price proposed on a document line comes from (SPEC §11.3).</summary>
public enum PriceSource
{
/// <summary>No price found.</summary>
None = 0,
/// <summary>The net price agreed with the party for the item.</summary>
PartyNet = 1,
/// <summary>The price list of the party (or of the document).</summary>
PartyPriceList = 2,
/// <summary>The base price list of the company.</summary>
BasePriceList = 3,
/// <summary>The price written on the item.</summary>
Item = 4,
}
/// <summary>An item (<c>items</c>, mockup 017).</summary>
public sealed class Item : Entity<long>, IDemoMarkable
{
/// <summary>The code, automatic (ADR 0027, mask <c>{categoria}-{seq:0000}</c>).</summary>
public string Code { get; set; } = string.Empty;
/// <summary>What the item is.</summary>
public ItemKind Kind { get; set; }
/// <summary>The description used on documents.</summary>
public string Description { get; set; } = string.Empty;
/// <summary>A longer description.</summary>
public string? ExtendedDescription { get; set; }
/// <summary>The category (<c>item_categories</c>).</summary>
public long? CategoryId { get; set; }
/// <summary>The base unit of measure (<c>units_of_measure</c>).</summary>
public long? UnitOfMeasureId { get; set; }
/// <summary>The VAT code of sales (<c>tax_codes</c>).</summary>
public long? SalesTaxCodeId { get; set; }
/// <summary>The VAT code of purchases (<c>tax_codes</c>).</summary>
public long? PurchaseTaxCodeId { get; set; }
/// <summary>The main bar code (EAN/GTIN).</summary>
public string? Barcode { get; set; }
/// <summary>The combined nomenclature code (NC8, Intrastat).</summary>
public string? CustomsCode { get; set; }
/// <summary>The manufacturer.</summary>
public string? Manufacturer { get; set; }
/// <summary>The brand.</summary>
public string? Brand { get; set; }
/// <summary>Net weight in kilograms.</summary>
public decimal? NetWeight { get; set; }
/// <summary>Gross weight in kilograms.</summary>
public decimal? GrossWeight { get; set; }
/// <summary>The item is kept in stock (movements, balances, reorder from M6).</summary>
public bool Stocked { get; set; }
/// <summary>The usual warehouse (<c>warehouses</c>).</summary>
public long? WarehouseId { get; set; }
/// <summary>The minimum stock.</summary>
public decimal? MinimumStock { get; set; }
/// <summary>The reorder point: below it the item is proposed for an order.</summary>
public decimal? ReorderPoint { get; set; }
/// <summary>The reorder lot: orders are proposed in multiples of it.</summary>
public decimal? ReorderLot { get; set; }
/// <summary>The price written on the item, the last fallback of the price search.</summary>
public decimal? Price { get; set; }
/// <summary>The standard cost, set by hand.</summary>
public decimal? StandardCost { get; set; }
/// <summary>The last purchase cost (updated by the receipts, M6).</summary>
public decimal? LastCost { get; set; }
/// <summary>The weighted average cost (updated by the receipts, M6).</summary>
public decimal? AverageCost { get; set; }
/// <summary>The revenue account (chart of accounts, M7).</summary>
public string? RevenueAccountCode { get; set; }
/// <summary>The cost account (chart of accounts, M7).</summary>
public string? CostAccountCode { get; set; }
/// <summary>Free notes.</summary>
public string? Notes { get; set; }
/// <summary>Disabled: no longer proposed in new documents.</summary>
public bool Disabled { get; set; }
/// <inheritdoc />
public bool IsDemo { get; set; }
}
/// <summary>An alternative unit of measure of an item (<c>item_uoms</c>): a box of 10 pieces has factor 10.</summary>
public sealed class ItemUnit : Entity<long>
{
/// <summary>The item.</summary>
public long ItemId { get; set; }
/// <summary>The unit (<c>units_of_measure</c>).</summary>
public long UnitOfMeasureId { get; set; }
/// <summary>How many base units one of this unit holds.</summary>
public decimal Factor { get; set; } = 1m;
/// <summary>The bar code of the package.</summary>
public string? Barcode { get; set; }
}
/// <summary>An alternative code of an item (<c>item_barcodes</c>): another EAN or an internal code.</summary>
public sealed class ItemBarcode : Entity<long>
{
/// <summary>The item.</summary>
public long ItemId { get; set; }
/// <summary>The code.</summary>
public string Code { get; set; } = string.Empty;
/// <summary>What the code is, such as <i>confezione</i> or <i>codice interno</i>.</summary>
public string? Description { get; set; }
}
/// <summary>
/// A price of an item (<c>item_prices</c>): in a price list, or net for a party. A price is valid from a date (none:
/// always); the price at a date is the one with the latest start not after it, so old prices stay for old documents.
/// </summary>
public sealed class ItemPrice : Entity<long>
{
/// <summary>The item.</summary>
public long ItemId { get; set; }
/// <summary>The price list (<c>price_lists</c>), or none for a net price of a party.</summary>
public long? PriceListId { get; set; }
/// <summary>The party of a net price (<c>parties</c>), or none for a price list.</summary>
public long? PartyId { get; set; }
/// <summary>The price.</summary>
public decimal Price { get; set; }
/// <summary>The price is valid from this day (none: always).</summary>
public DateOnly? ValidFrom { get; set; }
}
/// <summary>A supplier of an item (<c>item_suppliers</c>) with its code and price; one is the usual one.</summary>
public sealed class ItemSupplier : Entity<long>
{
/// <summary>The item.</summary>
public long ItemId { get; set; }
/// <summary>The supplier (<c>parties</c>).</summary>
public long PartyId { get; set; }
/// <summary>The code of the item at the supplier.</summary>
public string? SupplierCode { get; set; }
/// <summary>The purchase price.</summary>
public decimal? Price { get; set; }
/// <summary>The lead time in days.</summary>
public int? LeadTimeDays { get; set; }
/// <summary>The minimum quantity of an order.</summary>
public decimal? MinimumQuantity { get; set; }
/// <summary>The usual supplier, used by the reorder proposal.</summary>
public bool IsDefault { get; set; }
}
/// <summary>An equivalent item proposed when this one is missing (<c>item_substitutes</c>).</summary>
public sealed class ItemSubstitute : Entity<long>
{
/// <summary>The item.</summary>
public long ItemId { get; set; }
/// <summary>The substitute (<c>items</c>).</summary>
public long SubstituteId { get; set; }
/// <summary>Notes, such as the differences.</summary>
public string? Notes { get; set; }
}
/// <summary>The code a customer or a supplier uses for one of our items (<c>party_item_codes</c>).</summary>
public sealed class PartyItemCode : Entity<long>
{
/// <summary>The party.</summary>
public long PartyId { get; set; }
/// <summary>The item.</summary>
public long ItemId { get; set; }
/// <summary>The code of the party.</summary>
public string Code { get; set; } = string.Empty;
/// <summary>The description of the party.</summary>
public string? Description { get; set; }
}
+60
View File
@@ -0,0 +1,60 @@
namespace Khaemhat.Core.Items;
/// <summary>How a price is rounded to a step.</summary>
public enum RoundingDirection
{
/// <summary>To the nearest step, halves away from zero.</summary>
Nearest = 0,
/// <summary>Up to the next step (per eccesso).</summary>
Up = 1,
/// <summary>Down to the previous step (per difetto).</summary>
Down = 2,
}
/// <summary>The arithmetic of prices: percent changes and rounding to a step such as 0,10, 0,50 or 1,00.</summary>
public static class PriceRounding
{
/// <summary>Rounds a price to a step; with no step, to the cent.</summary>
/// <param name="value">The price.</param>
/// <param name="step">The step, such as 0.10; zero or less means cents.</param>
/// <param name="direction">Nearest, up or down.</param>
/// <returns>The rounded price.</returns>
public static decimal Round(decimal value, decimal step, RoundingDirection direction = RoundingDirection.Nearest)
{
if (step <= 0)
{
step = 0.01m;
}
var units = value / step;
units = direction switch
{
RoundingDirection.Up => decimal.Ceiling(Math.Round(units, 10)),
RoundingDirection.Down => decimal.Floor(Math.Round(units, 10)),
_ => Math.Round(units, MidpointRounding.AwayFromZero),
};
return Normalize(units * step);
}
/// <summary>Applies a percent change: +10 adds a tenth, -5 takes off a twentieth.</summary>
/// <param name="value">The price.</param>
/// <param name="percent">The change in percent.</param>
/// <returns>The changed price, not rounded.</returns>
public static decimal Change(decimal value, decimal percent) => value * (1m + (percent / 100m));
/// <summary>The margin of a price over a cost, in percent of the price.</summary>
/// <param name="price">The price.</param>
/// <param name="cost">The cost.</param>
/// <returns>The margin, or none without a positive price or a cost.</returns>
public static decimal? Margin(decimal? price, decimal? cost) =>
price is > 0 && cost is { } c ? Math.Round((price.Value - c) / price.Value * 100m, 1, MidpointRounding.AwayFromZero) : null;
// Four decimals at most, and two when the rest are zeros (12.3000 becomes 12.30).
private static decimal Normalize(decimal value)
{
var rounded = Math.Round(value, 4, MidpointRounding.AwayFromZero);
return rounded == Math.Round(rounded, 2) ? Math.Round(rounded, 2) : rounded;
}
}
+3
View File
@@ -342,6 +342,9 @@ public sealed class PriceList : CodedEntity
/// <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>