Files
Khaemhat/src/Khaemhat.Web/Tables/TableOptions.cs
T
Alby96andClaude Opus 5.5 9bcf8c1082 feat: company table screens, profile and fiscal years, seed command (#38, #39, #45)
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>
2026-11-11 23:15:09 +01:00

99 lines
4.7 KiB
C#

using System.Globalization;
using System.Reflection;
using Khaemhat.Application.Localization;
using Khaemhat.Application.Security;
using Khaemhat.Application.Tables;
using Khaemhat.Core.Entities;
using Khaemhat.Fiscal.Abstractions;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Tables;
/// <summary>The choices of the fields of a page: rows of other tables, lists of the fiscal pack, enumerations, fixed values.</summary>
public sealed class TableOptions
{
private readonly Dictionary<string, IReadOnlyList<KhOption<string>>> _lookups = new(StringComparer.Ordinal);
/// <summary>The fiscal pack of the company.</summary>
public IFiscalPack? Pack { get; private set; }
/// <summary>Reads the pack of the company and the rows every lookup field points to.</summary>
/// <param name="tables">The tables.</param>
/// <param name="fiscal">The fiscal context.</param>
/// <param name="fields">The fields.</param>
/// <param name="actor">Who reads.</param>
/// <param name="company">The company.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
public async Task LoadAsync(TableService tables, CompanyFiscalContext fiscal, IEnumerable<TableField> fields, Actor actor, string company,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(fiscal);
ArgumentNullException.ThrowIfNull(fields);
Pack = await fiscal.PackOfAsync(company, cancellationToken).ConfigureAwait(false);
foreach (var field in fields.Where(f => f is { Kind: TableFieldKind.Lookup, Lookup: not null }))
{
var choices = await TableValues.LookupAsync(tables, field.Lookup!, field.ByCode, actor, company, cancellationToken).ConfigureAwait(false);
_lookups[field.Property] = [.. choices.Select(c => new KhOption<string>(c.Value, c.Text))];
}
}
/// <summary>The choices of a field for a row.</summary>
/// <param name="field">The field.</param>
/// <param name="property">Its property.</param>
/// <param name="row">The row, to keep an obsolete code or leave out the row itself.</param>
/// <param name="texts">The texts.</param>
/// <returns>The choices.</returns>
public IReadOnlyList<KhOption<string>> For(TableField field, PropertyInfo property, object? row, ITexts texts)
{
ArgumentNullException.ThrowIfNull(field);
ArgumentNullException.ThrowIfNull(property);
ArgumentNullException.ThrowIfNull(texts);
var current = row is null ? null : TableValues.GetText(row, property);
switch (field.Kind)
{
case TableFieldKind.Choice when field.Values is { } values:
return [.. values.Select(v => new KhOption<string>(v, texts[ValueKey(field, v)]))];
case TableFieldKind.Choice:
return [.. TableValues.EnumChoices(property, v => texts[TableScreens.EnumKey(v)]).Select(c => new KhOption<string>(c.Value, c.Text))];
case TableFieldKind.CodeList:
var list = Pack is null ? null : field.CodeList?.Invoke(Pack);
if (list is null)
{
return current is null ? [] : [new(current, current)];
}
return [.. list.Entries.Where(e => !e.Obsolete || e.Code == current).Select(e => new KhOption<string>(e.Code, e.Code + " · " + e.Description))];
default:
var options = _lookups.GetValueOrDefault(field.Property) ?? [];
// A self reference never points to the row itself.
return field.Lookup == row?.GetType() && row is Entity<long> { Id: > 0 } entity
? [.. options.Where(o => o.Value != entity.Id.ToString(CultureInfo.InvariantCulture))]
: options;
}
}
/// <summary>The code shown in the grid for the value of a lookup.</summary>
/// <param name="field">The field.</param>
/// <param name="value">The stored value.</param>
/// <returns>The code.</returns>
public string? CodeOf(TableField field, string? value)
{
ArgumentNullException.ThrowIfNull(field);
return value is null ? null : _lookups.GetValueOrDefault(field.Property)?.FirstOrDefault(o => o.Value == value)?.Text.Split(" · ")[0] ?? value;
}
/// <summary>The text key of a fixed value: <c>CompanyTables.Values.&lt;Property&gt;.&lt;Value&gt;</c>.</summary>
/// <param name="field">The field.</param>
/// <param name="value">The value.</param>
/// <returns>The key.</returns>
public static string ValueKey(TableField field, string value)
{
ArgumentNullException.ThrowIfNull(field);
return "CompanyTables.Values." + field.Property + "." + value;
}
}