@page "/fatturazione-elettronica/conservazione" @attribute [Authorize] @using Khaemhat.Application.EInvoicing @using Khaemhat.Application.Security @using Khaemhat.Core.EInvoicing @using Khaemhat.Web.EInvoicing @inject ShellSession Session @inject ConservationExportService Conservation @inject ToastService Toasts @inject IJSRuntime JS @inject TimeProvider Clock @* The packages for substitute conservation (SPEC §11.9, issue #79): the files issued and received in a period, their notifications, an index with the fingerprint of each file, to hand to the conservation provider. Khaemhat is not a conservation system: it prepares what goes to one, and remembers what went out. *@
@T["Shell.Menu.EInvoicing"] / @T["EInvoicing.Conservation.Title"]

@T["EInvoicing.Conservation.Heading"]

@T["EInvoicing.Conservation.Hint"]

@T["EInvoicing.Conservation.Contents"]

@if (_exports.Count == 0) {

@T["EInvoicing.Conservation.None"]

} else {
@foreach (var x in _exports) { }
@T["EInvoicing.Conservation.Period"] @T["EInvoicing.Conservation.File"] @T["EInvoicing.Conservation.Files"] @T["EInvoicing.Conservation.Size"] @T["EInvoicing.Conservation.Hash"]
@T.Date(x.FromDate) – @T.Date(x.ToDate) @x.FileName @x.FileCount @Size(x.Size) @x.Hash[..16]…
}
@T["EInvoicing.Conservation.Footer"]
@code { private IReadOnlyList _exports = []; private DateOnly? _from; private DateOnly? _to; private bool _busy; private string Size(long bytes) => bytes >= 1024 * 1024 ? T.Format("EInvoicing.Conservation.MegaBytes", bytes / 1048576m) : T.Format("EInvoicing.Conservation.KiloBytes", Math.Max(1, bytes / 1024)); /// protected override async Task OnInitializedAsync() { await Session.EnsureLoadedAsync(); var today = DateOnly.FromDateTime(Clock.GetLocalNow().DateTime); var previous = new DateOnly(today.Year, today.Month, 1).AddMonths(-1); _from = previous; _to = previous.AddMonths(1).AddDays(-1); if (EInvoiceGuard.Allowed(Session, EInvoicePermissions.Read)) { await LoadAsync(); } } private async Task LoadAsync() { if (Session.Actor is { } actor && Session.Company?.Code is { } company) { _exports = await Conservation.ListAsync(actor, company); } } private async Task ExportAsync() { if (Session.Actor is not { } actor || Session.Company?.Code is not { } company || _from is not { } from || _to is not { } to) { return; } _busy = true; try { var package = await Conservation.ExportAsync(actor, company, from, to); await EInvoicingScreens.DownloadAsync(JS, package.Export.FileName, "application/zip", package.Content); Toasts.Show(ToastKind.Success, T["EInvoicing.Conservation.Done"], T.Format("EInvoicing.Conservation.DoneText", package.Export.FileCount)); await LoadAsync(); } catch (ValidationFailedException ex) { Toasts.Show(ToastKind.Error, T["EInvoicing.NotDone"], EInvoicingScreens.Errors(T, ex)); } finally { _busy = false; } } }