@page "/contabilita/bollo" @attribute [Authorize] @using Khaemhat.Application.Ledger @using Khaemhat.Application.Security @using Khaemhat.Web.Ledger @inject ShellSession Session @inject TaxService Taxes @inject ToastService Toasts @inject IJSRuntime JS @inject TimeProvider Clock @* The virtual stamp duty on e-invoices (SPEC §11.7, mockup 060): the invoices of a quarter with the duty, the amount, the payment code and the due date (a small first quarter goes with the second, the first two with the third), the F24 line, its data file and its payment. *@
@T["Shell.Menu.Ledger"] / @T["Ledger.StampDuty.Title"]

@T["Ledger.StampDuty.Heading"]

@T["Ledger.StampDuty.Hint"]
@if (Current is { } q) { @T.Format("Ledger.StampDuty.QuarterBadge", q.Quarter, q.Year) } @if (CanConfirm) { }
@if (Current is { } current) { } @if (_documents.Count == 0) {

@T["Ledger.StampDuty.None"]

} else { @foreach (var d in _documents) { }
@T["Ledger.Fields.Document"] @T["Ledger.Fields.DocumentDate"] @T["Ledger.Fields.Party"] @T["Ledger.Fields.Taxable"] @T["Ledger.StampDuty.Duty"] @T["Ledger.StampDuty.Charge"]
@d.DocumentNumber @T.Date(d.DocumentDate) @d.PartyName @T.Money(d.Taxable) @T.Money(d.StampDuty) @T[d.Charged ? "Ledger.StampDuty.Charged" : "Ledger.StampDuty.Borne"]
}
@T["Ledger.StampDuty.Footer"]
@foreach (var q in _quarters) { }
@T["Ledger.StampDuty.Quarter"] @T["Ledger.StampDuty.Invoices"] @T["Ledger.Fields.Amount"] @T["Ledger.Settlement.DueDate"] @T["Ledger.Fields.Status"]
@T.Format("Ledger.StampDuty.QuarterBadge", q.Quarter, q.Year) @q.Documents @T.Money(q.Amount) @T.Date(q.DueDate) @if (q.Payment is { PaidOn: { } paid }) { @T.Format("Ledger.StampDuty.PaidOn", T.Date(paid)) } else if (q.Payment is not null) { @T["Ledger.StampDuty.InF24"] } else if (q.Amount > 0m) { @T["Ledger.StampDuty.ToRecord"] }
@code { private int _year; private int _quarter; private IReadOnlyList _quarters = []; private IReadOnlyList _documents = []; private bool _busy; private DateOnly Today => DateOnly.FromDateTime(Clock.GetLocalNow().DateTime); private bool CanConfirm => Session.Permissions.Allows(LedgerPermissions.SettlementConfirm); private StampDutyQuarter? Current => _quarters.FirstOrDefault(q => q.Quarter == _quarter); private IReadOnlyList> YearChoices => [.. Enumerable.Range(Today.Year - 3, 5).Select(y => new KhOption(y, y.ToString(System.Globalization.CultureInfo.InvariantCulture)))]; private IReadOnlyList> QuarterChoices => [.. Enumerable.Range(1, 4).Select(q => new KhOption(q, T.Format("Ledger.StampDuty.QuarterBadge", q, _year)))]; private IReadOnlyList Stats(StampDutyQuarter q) { var previous = q.Quarter > 1 ? _quarters.FirstOrDefault(p => p.Quarter == q.Quarter - 1) : null; return [ new(T["Ledger.StampDuty.Invoices"], T.Number(q.Documents)), new(T["Ledger.Fields.Amount"], T.Money(q.Amount)), new(T["Ledger.Settlement.PaymentCode"], q.Payment?.PaymentCode ?? "-"), new(T["Ledger.Settlement.DueDate"], T.Date(q.DueDate)), new(T["Ledger.StampDuty.Previous"], previous is null ? "-" : T.Money(previous.Amount) + (previous.Payment?.PaidOn is { } paid ? " · " + T.Format("Ledger.StampDuty.PaidOn", T.Date(paid)) : string.Empty), previous?.Payment?.PaidOn is not null ? KhTone.Ok : KhTone.Neutral), ]; } /// protected override async Task OnInitializedAsync() { await Session.EnsureLoadedAsync(); _year = Today.Year; _quarter = ((Today.Month - 1) / 3) + 1; if (Session.Actor is { } && Session.Company is { } && Session.Permissions.Allows(LedgerPermissions.SettlementRead) && Session.Licenses.IsModuleActive(Khaemhat.Application.Modules.ModuleCodes.Accounting)) { await LoadAsync(); } } private async Task LoadAsync() { if (Session.Actor is not { } actor || Session.Company?.Code is not { } company) { return; } _quarters = await Taxes.StampDutyAsync(actor, company, _year); _documents = await Taxes.StampDutyDocumentsAsync(actor, company, _year, _quarter); } private async Task SetYearAsync(int year) { _year = year; await LoadAsync(); } private async Task SetQuarterAsync(int quarter) { _quarter = quarter; await LoadAsync(); } private async Task RecordAsync() => await RunAsync(async (actor, company) => { var payment = await Taxes.StampDutyPaymentAsync(actor, company, _year, _quarter); Toasts.Show(ToastKind.Success, payment is null ? T["Ledger.StampDuty.NoneDue"] : T.Format("Ledger.StampDuty.Recorded", T.Money(payment.Amount), T.Date(payment.DueDate))); await LoadAsync(); }); private async Task ExportAsync() => await RunAsync(async (actor, company) => { if (Current?.Payment is { } payment) { await LedgerScreens.DownloadAsync(JS, await Taxes.ExportFormAsync(actor, company, payment.DueDate)); } }); private async Task RunAsync(Func action) { if (Session.Actor is not { } actor || Session.Company?.Code is not { } company) { return; } _busy = true; try { await action(actor, company); } catch (ValidationFailedException ex) { Toasts.Show(ToastKind.Error, T["Ledger.NotDone"], LedgerScreens.Errors(T, ex)); } finally { _busy = false; } } }