@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%. *@ @if (!ReadOnly) { } @for (var i = 0; i < _lines.Count; i++) { var line = _lines[i]; var number = i + 1; }
# @T["CompanyTables.Lines.Days"] @T["CompanyTables.Lines.EndOfMonth"] @T["CompanyTables.Lines.FixedDay"] @T["CompanyTables.Lines.Percent"]
@number @if (!ReadOnly) { }
@T["CompanyTables.Lines.Total"] @ItalianNumber.Format(Total, 2, Italian)
@if (_error is not null) {

@_error

}
@code { private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it-IT"); private List _lines = []; private bool _modified; private bool _busy; private string? _error; /// The payment term. [Parameter, EditorRequired] public long PaymentTermId { get; set; } /// Whether the installments can only be read. [Parameter] public bool ReadOnly { get; set; } private decimal Total => _lines.Sum(l => l.Percent); /// 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; }