Files
Khaemhat/src/Khaemhat.Web/Components/Tables/KhPaymentTermLines.razor
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

146 lines
5.8 KiB
Plaintext

@using System.Globalization
@using Khaemhat.Application.Security
@using Khaemhat.Application.Tables
@using Khaemhat.Core.Tables
@inject ShellSession Session
@inject PaymentTermService Terms
@inject ToastService Toasts
@* The installments of a payment term (SPEC §11.1): days, end of month, fixed day, share. They are saved together,
because only the whole set has to add up to 100%. *@
<KhPanel Title="@T["CompanyTables.Lines.Title"]" Icon="calendar">
<Actions>
@if (!ReadOnly)
{
<button type="button" class="btn" @onclick="Add"><KhIcon Name="plus" /> @T["CompanyTables.Lines.Add"]</button>
<button type="button" class="btn primary" disabled="@(!_modified || _busy)" @onclick="SaveAsync"><KhIcon Name="save" /> @T["CompanyTables.Lines.Save"]</button>
}
</Actions>
<ChildContent>
<table class="kh-lines" aria-label="@T["CompanyTables.Lines.Title"]">
<thead>
<tr>
<th class="n">#</th>
<th class="n">@T["CompanyTables.Lines.Days"]</th>
<th>@T["CompanyTables.Lines.EndOfMonth"]</th>
<th class="n">@T["CompanyTables.Lines.FixedDay"]</th>
<th class="n">@T["CompanyTables.Lines.Percent"]</th>
<th></th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < _lines.Count; i++)
{
var line = _lines[i];
var number = i + 1;
<tr>
<td class="n">@number</td>
<td class="n">
<input class="in num" inputmode="numeric" value="@line.Days" disabled="@ReadOnly" aria-label="@T["CompanyTables.Lines.Days"]"
@onchange="e => Set(() => line.Days = Integer(e.Value) ?? 0)" />
</td>
<td>
<input type="checkbox" checked="@line.EndOfMonth" disabled="@ReadOnly" aria-label="@T["CompanyTables.Lines.EndOfMonth"]"
@onchange="e => Set(() => line.EndOfMonth = e.Value is true)" />
</td>
<td class="n">
<input class="in num" inputmode="numeric" value="@line.FixedDay" disabled="@ReadOnly" aria-label="@T["CompanyTables.Lines.FixedDay"]"
@onchange="e => Set(() => line.FixedDay = Integer(e.Value))" />
</td>
<td class="n">
<input class="in num" inputmode="decimal" value="@ItalianNumber.Format(line.Percent, 2, Italian)" disabled="@ReadOnly"
aria-label="@T["CompanyTables.Lines.Percent"]" @onchange="e => Set(() => line.Percent = Decimal(e.Value) ?? 0m)" />
</td>
<td>
@if (!ReadOnly)
{
<KhIconButton Icon="trash" Text="@T["CompanyTables.Lines.Remove"]" Danger="true" OnClick="() => Remove(line)" />
}
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="4">@T["CompanyTables.Lines.Total"]</td>
<td class="n @(Total == 100m ? null : "err")">@ItalianNumber.Format(Total, 2, Italian)</td>
<td></td>
</tr>
</tfoot>
</table>
@if (_error is not null)
{
<p class="err kh-pad">@_error</p>
}
</ChildContent>
</KhPanel>
@code {
private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it-IT");
private List<PaymentTermLine> _lines = [];
private bool _modified;
private bool _busy;
private string? _error;
/// <summary>The payment term.</summary>
[Parameter, EditorRequired]
public long PaymentTermId { get; set; }
/// <summary>Whether the installments can only be read.</summary>
[Parameter]
public bool ReadOnly { get; set; }
private decimal Total => _lines.Sum(l => l.Percent);
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
if (Session.Actor is { } actor && Session.Company?.Code is { } company)
{
_lines = [.. await Terms.LinesAsync(actor, company, PaymentTermId)];
}
}
private void Set(Action change)
{
change();
_modified = true;
_error = null;
}
private void Add() => Set(() => _lines.Add(new PaymentTermLine { Days = _lines.Count == 0 ? 30 : _lines[^1].Days + 30, EndOfMonth = _lines.LastOrDefault()?.EndOfMonth ?? false,
Percent = Math.Max(0m, 100m - Total) }));
private void Remove(PaymentTermLine line) => Set(() => _lines.Remove(line));
private async Task SaveAsync()
{
if (Session.Actor is not { } actor || Session.Company?.Code is not { } company)
{
return;
}
_busy = true;
try
{
_lines = [.. await Terms.SaveLinesAsync(actor, company, PaymentTermId, _lines)];
_modified = false;
Toasts.Show(ToastKind.Success, T["CompanyTables.Lines.Saved"]);
}
catch (ValidationFailedException ex)
{
_error = string.Join(" ", ex.Errors.Select(e => T[e[(e.IndexOf(':', StringComparison.Ordinal) + 1)..]]));
}
finally
{
_busy = false;
}
}
private static int? Integer(object? value) =>
int.TryParse(value?.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var number) ? number : null;
private static decimal? Decimal(object? value) => ItalianNumber.TryParse(value?.ToString(), out var number) ? number : null;
}