Gives the company tables of the previous commit their screens, so a company can be configured from the browser, and makes the base data runnable outside the web server. - one generic editor for the 26 coded tables (/tabelle/<slug>), described in TableScreens: grid with active/disabled filter, detail with errors and warnings, disable instead of delete, CSV export and all-or-nothing import, "update from base tables"; index page and menu entries - company profile in tabs with the fiscal pack checks, fiscal years with month lock/unlock, exchange rates with manual rates - PaymentTermService: installments saved together, percentages must sum to 100 - a new profile proposes the registered company name instead of its code - seed verb in Khaemhat.Web and scripts/seed.sh --base [--company CODE], shipped in the image under /app/scripts - ADR 0026 (base data only adds missing codes; ISTAT and ABI/CAB lists come as separate datasets), wiki, CHANGELOG, STATUS Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
34e196424f
commit
9bcf8c1082
33 files changed
+3272
-43
No files matched your search
@@ -0,0 +1,115 @@
|
||||
using System.Globalization;
|
||||
using Khaemhat.Application.Instance;
|
||||
using Khaemhat.Application.Seeding;
|
||||
using Khaemhat.Core.Instance;
|
||||
using Khaemhat.Data;
|
||||
using Khaemhat.Data.Companies;
|
||||
using Khaemhat.Infrastructure.Logging;
|
||||
|
||||
namespace Khaemhat.Web.Hosting;
|
||||
|
||||
/// <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).
|
||||
/// </summary>
|
||||
public static class SeedCommand
|
||||
{
|
||||
/// <summary>The first argument that selects the command.</summary>
|
||||
public const string Verb = "seed";
|
||||
|
||||
/// <summary>Exit code of a wrong command line.</summary>
|
||||
public const int UsageError = 64;
|
||||
|
||||
/// <summary>Whether the arguments ask for this command.</summary>
|
||||
/// <param name="args">The arguments.</param>
|
||||
/// <returns><see langword="true"/> for <c>seed ...</c>.</returns>
|
||||
public static bool Matches(string[] args) => args is [Verb, ..];
|
||||
|
||||
/// <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="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)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(args);
|
||||
company = null;
|
||||
error = null;
|
||||
var wantsBase = false;
|
||||
for (var i = 1; i < args.Length; i++)
|
||||
{
|
||||
switch (args[i])
|
||||
{
|
||||
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 "--company" when i + 1 < args.Length && CompanyCodes.IsValid(CompanyCodes.Normalize(args[i + 1])):
|
||||
company = CompanyCodes.Normalize(args[++i]);
|
||||
break;
|
||||
default:
|
||||
error = "Argomento non valido: " + args[i];
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!wantsBase)
|
||||
{
|
||||
error = "Uso: seed --base [--company CODICE]";
|
||||
}
|
||||
|
||||
return error is null;
|
||||
}
|
||||
|
||||
/// <summary>Runs the command.</summary>
|
||||
/// <param name="args">The arguments, verb included.</param>
|
||||
/// <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))
|
||||
{
|
||||
await Console.Error.WriteLineAsync(error).ConfigureAwait(false);
|
||||
return UsageError;
|
||||
}
|
||||
|
||||
// The configuration comes from the KHAEMHAT_* variables, as for the service; no web server is started.
|
||||
var app = KhaemhatApp.Build([]);
|
||||
await using (app.ConfigureAwait(false))
|
||||
{
|
||||
var services = app.Services;
|
||||
try
|
||||
{
|
||||
await services.GetRequiredService<StartupOrchestrator>().RunAsync().ConfigureAwait(false);
|
||||
var registry = services.GetRequiredService<ICompanyRegistry>();
|
||||
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] : [];
|
||||
if (codes.Count == 0)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(company is null ? "Nessuna azienda attiva." : "Azienda sconosciuta: " + company).ConfigureAwait(false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
var runner = services.GetRequiredService<SeedRunner>();
|
||||
foreach (var code in codes)
|
||||
{
|
||||
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))
|
||||
{
|
||||
Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $" {table}: {count}"));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await services.GetRequiredService<DbLogSink>().DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user