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

+53 -16
View File
@@ -8,10 +8,17 @@ using Khaemhat.Infrastructure.Logging;
namespace Khaemhat.Web.Hosting;
/// <summary>What <c>seed</c> was asked to do.</summary>
/// <param name="Demo">Add the demo data too, after the base data, to the tables still empty.</param>
/// <param name="Scale">How many times the demo catalogues are repeated.</param>
/// <param name="Company">The company, or <see langword="null"/> for every active one.</param>
public sealed record SeedOptions(bool Demo, int Scale, string? Company);
/// <summary>
/// The command line of <c>scripts/seed.sh</c> (SPEC §4, §14): <c>seed --base [--company CODE]</c> runs the start
/// sequence without serving anything (migrations, first company), then adds the missing base data to one company or to
/// every active one, and prints what it added. Rows already there are never changed (ADR 0026).
/// The command line of <c>scripts/seed.sh</c> (SPEC §4, §14): <c>seed --base|--demo [--scale N] [--company CODE]</c>
/// runs the start sequence without serving anything (migrations, first company), then adds the missing base data, and
/// with <c>--demo</c> the demo data, to one company or to every active one, and prints what it added. Rows already there
/// are never changed (ADR 0026); each demo seeder fills only its tables still empty.
/// </summary>
public static class SeedCommand
{
@@ -21,6 +28,9 @@ public static class SeedCommand
/// <summary>Exit code of a wrong command line.</summary>
public const int UsageError = 64;
/// <summary>The largest <c>--scale</c>.</summary>
public const int MaxScale = 100;
/// <summary>Whether the arguments ask for this command.</summary>
/// <param name="args">The arguments.</param>
/// <returns><see langword="true"/> for <c>seed ...</c>.</returns>
@@ -28,15 +38,18 @@ public static class SeedCommand
/// <summary>Reads the options after the verb.</summary>
/// <param name="args">The arguments, verb included.</param>
/// <param name="company">The company, or <see langword="null"/> for every active one.</param>
/// <param name="options">What to do, when the line is valid.</param>
/// <param name="error">The problem, or <see langword="null"/>.</param>
/// <returns><see langword="true"/> when the line is valid.</returns>
public static bool TryParse(string[] args, out string? company, out string? error)
public static bool TryParse(string[] args, out SeedOptions? options, out string? error)
{
ArgumentNullException.ThrowIfNull(args);
company = null;
options = null;
error = null;
var wantsBase = false;
var wantsDemo = false;
int? scale = null;
string? company = null;
for (var i = 1; i < args.Length; i++)
{
switch (args[i])
@@ -44,9 +57,13 @@ public static class SeedCommand
case "--base":
wantsBase = true;
break;
case "--demo" or "--scale":
error = "seed --demo: i dati dimostrativi arrivano con anagrafiche e articoli (M4, issue #41 e #42).";
return false;
case "--demo":
wantsDemo = true;
break;
case "--scale" when i + 1 < args.Length && int.TryParse(args[i + 1], NumberStyles.None, CultureInfo.InvariantCulture, out var n) && n is >= 1 and <= MaxScale:
scale = n;
i++;
break;
case "--company" when i + 1 < args.Length && CompanyCodes.IsValid(CompanyCodes.Normalize(args[i + 1])):
company = CompanyCodes.Normalize(args[++i]);
break;
@@ -56,12 +73,20 @@ public static class SeedCommand
}
}
if (!wantsBase)
if (!wantsBase && !wantsDemo)
{
error = "Uso: seed --base [--company CODICE]";
error = "Uso: seed --base | --demo [--scale N] [--company CODICE]";
return false;
}
return error is null;
if (scale is not null && !wantsDemo)
{
error = "--scale vale solo con --demo.";
return false;
}
options = new SeedOptions(wantsDemo, scale ?? 1, company);
return true;
}
/// <summary>Runs the command.</summary>
@@ -69,7 +94,7 @@ public static class SeedCommand
/// <returns>The exit code: 0 done, 1 failed, 64 wrong command line.</returns>
public static async Task<int> RunAsync(string[] args)
{
if (!TryParse(args, out var company, out var error))
if (!TryParse(args, out var options, out var error) || options is null)
{
await Console.Error.WriteLineAsync(error).ConfigureAwait(false);
return UsageError;
@@ -84,6 +109,7 @@ public static class SeedCommand
{
await services.GetRequiredService<StartupOrchestrator>().RunAsync().ConfigureAwait(false);
var registry = services.GetRequiredService<ICompanyRegistry>();
var company = options.Company;
IReadOnlyList<string> codes = company is null
? [.. (await registry.ListAsync().ConfigureAwait(false)).Where(c => c.Status == CompanyStatus.Active).Select(c => c.Code)]
: await registry.FindAsync(company).ConfigureAwait(false) is { } found ? [found.Code] : [];
@@ -96,11 +122,13 @@ public static class SeedCommand
var runner = services.GetRequiredService<SeedRunner>();
foreach (var code in codes)
{
// The demo data need the base tables: --demo runs the base seeders first.
var report = await runner.RunAsync(code, SeedKind.Base).ConfigureAwait(false);
Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $"{code}: dati di base {report.DataVersion}, {report.Total} righe aggiunte."));
foreach (var (table, count) in report.Added.Where(a => a.Value > 0).OrderBy(a => a.Key, StringComparer.Ordinal))
Print(code, "dati di base " + report.DataVersion, report);
if (options.Demo)
{
Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $" {table}: {count}"));
Print(code, "dati dimostrativi, scala " + options.Scale.ToString(CultureInfo.InvariantCulture),
await runner.RunAsync(code, SeedKind.Demo, options.Scale).ConfigureAwait(false));
}
}
@@ -112,4 +140,13 @@ public static class SeedCommand
}
}
}
private static void Print(string company, string what, SeedReport report)
{
Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $"{company}: {what}, {report.Total} righe aggiunte."));
foreach (var (table, count) in report.Added.Where(a => a.Value > 0).OrderBy(a => a.Key, StringComparer.Ordinal))
{
Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $" {table}: {count}"));
}
}
}