Files
Khaemhat/src/Khaemhat.Web/Items/ItemScreens.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

121 lines
4.7 KiB
C#

using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Core.Items;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Items;
/// <summary>
/// Addresses, tabs and choices of the item screens (SPEC §11.2, mockups 017 and 018): the radar
/// <c>/anagrafiche/articoli</c>, the record of an item under it, and the price grid <c>/anagrafiche/listini</c>.
/// </summary>
public static class ItemScreens
{
/// <summary>The radar of the items.</summary>
public const string RadarUrl = "anagrafiche/articoli";
/// <summary>A new item.</summary>
public const string NewUrl = RadarUrl + "/nuovo";
/// <summary>The price grid.</summary>
public const string PriceListsUrl = "anagrafiche/listini";
/// <summary>The table of the item categories, where <c>/anagrafiche/categorie</c> leads.</summary>
public const string CategoriesUrl = "tabelle/categorie-articoli";
/// <summary>The tab ids of the record page, in order.</summary>
public static class Tabs
{
/// <summary>Main data.</summary>
public const string Main = "dati";
/// <summary>Alternative units.</summary>
public const string Units = "um";
/// <summary>Alternative codes.</summary>
public const string Barcodes = "codici";
/// <summary>Prices of the lists and net prices of the customers.</summary>
public const string Prices = "listini";
/// <summary>Suppliers.</summary>
public const string Suppliers = "fornitori";
/// <summary>Substitutes.</summary>
public const string Substitutes = "sostitutivi";
/// <summary>Notes and attachments.</summary>
public const string Notes = "note";
/// <summary>History.</summary>
public const string History = "storico";
}
/// <summary>The record of an item.</summary>
/// <param name="id">The item.</param>
/// <returns>The address, relative to the base.</returns>
public static string RecordUrl(long id) => RadarUrl + "/" + id.ToString(CultureInfo.InvariantCulture);
/// <summary>The tab that holds a field of the service (<c>Prices.0.Price</c>, <c>Suppliers.1.PartyId</c>).</summary>
/// <param name="field">The field.</param>
/// <returns>The tab id.</returns>
public static string TabOf(string? field)
{
var head = (field ?? string.Empty).Split('.')[0];
return head switch
{
"Units" => Tabs.Units,
"Barcodes" => Tabs.Barcodes,
"Prices" => Tabs.Prices,
"Suppliers" => Tabs.Suppliers,
"Substitutes" => Tabs.Substitutes,
nameof(Item.Notes) => Tabs.Notes,
_ => Tabs.Main,
};
}
/// <summary>The choices of an enum, with the texts <c>Items.&lt;group&gt;.&lt;value&gt;</c>.</summary>
/// <typeparam name="TEnum">The enum.</typeparam>
/// <param name="texts">The texts.</param>
/// <param name="group">The group of texts.</param>
/// <returns>The choices.</returns>
public static IReadOnlyList<KhOption<string>> Choices<TEnum>(ITexts texts, string group)
where TEnum : struct, Enum
{
ArgumentNullException.ThrowIfNull(texts);
return [.. Enum.GetValues<TEnum>().Select(v => new KhOption<string>(v.ToString(), texts["Items." + group + "." + v]))];
}
/// <summary>The margin on the price as text, such as <c>45%</c>; none without both values.</summary>
/// <param name="price">The price.</param>
/// <param name="cost">The cost.</param>
/// <param name="culture">The culture of the texts.</param>
/// <returns>The text.</returns>
public static string? MarginText(decimal? price, decimal? cost, CultureInfo culture) =>
PriceRounding.Margin(price, cost) is { } margin ? margin.ToString("0.#", culture) + "%" : null;
/// <summary>Reads a price typed in a cell: empty means no price, otherwise a number not below zero.</summary>
/// <param name="text">The text typed.</param>
/// <param name="culture">The culture of the texts.</param>
/// <param name="price">The price, or <see langword="null"/> for none.</param>
/// <returns>Whether the text is valid.</returns>
public static bool TryParsePrice(string? text, CultureInfo culture, out decimal? price)
{
ArgumentNullException.ThrowIfNull(culture);
price = null;
var clean = (text ?? string.Empty).Replace("€", string.Empty, StringComparison.Ordinal).Trim();
if (clean.Length == 0)
{
return true;
}
if (!decimal.TryParse(clean, NumberStyles.Number, culture, out var value) || value < 0)
{
return false;
}
price = Math.Round(value, 4, MidpointRounding.AwayFromZero);
return true;
}
}