@page "/sviluppo/documento/stampa"
@attribute [Authorize]
@using System.Globalization
@using Khaemhat.Application.Security
@using Khaemhat.Web.Hosting
@inject IWebHostEnvironment Environment
@inject ShellSession Session
@inject FileGenerationService Generation
@inject ToastService Toasts
@inject TimeProvider Clock
@* The print preview of the test document (issue #34, mockup 025): the same lines the PDF generator writes, as an A4
sheet in HTML; Stampa prints the sheet only, the PDF comes from DemoPdfGenerator. Development only. *@
@if (!Environment.IsDevelopment())
{
@T["Pages.Catalog.DevOnly"]
}
else
{
@T["Pages.Print.Crumb"] › @T["Pages.Print.Title"]
@T.Format("Pages.Print.Heading", DemoDocument.Title, Number)
@(Session.Company?.Name ?? Company)
@T.Format("Pages.Print.CompanyLine", Company)
@DemoDocument.Title
@T.Format("Pages.Print.Number", Number, T.Date(Today))
@T.Format("Pages.Print.Intro", Company, Session.Actor?.UserName ?? string.Empty)
@for (var i = 0; i < DemoDocument.Headers.Count; i++)
{
| @DemoDocument.Headers[i] |
}
@foreach (var line in DemoDocument.Lines)
{
| @line.Code |
@line.Description |
@line.Quantity.ToString("#,##0.##", T.Culture) |
@DemoDocument.Euro(line.UnitPrice, T.Culture) |
@DemoDocument.Euro(DemoDocument.Amount(line), T.Culture) |
}
| @T["Pages.Print.Total"] |
@DemoDocument.Euro(DemoDocument.Total, T.Culture) |
}
@code {
private bool _saving;
private static string Number => SampleDocumentPage.SampleId.ToString(CultureInfo.InvariantCulture);
private string Company => Session.Actor?.CompanyCode ?? string.Empty;
private DateOnly Today => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
///
protected override Task OnInitializedAsync() => Session.EnsureLoadedAsync();
private string? PdfUrl(bool download) =>
Session.Actor?.CompanyCode is null ? null : FileEndpoints.Generated(DemoDocument.Entity, SampleDocumentPage.SampleId, DemoPdfGenerator.GeneratorCode, download);
private async Task SaveAsync()
{
if (Session.Actor is not { CompanyCode: { } company } actor)
{
return;
}
_saving = true;
try
{
var saved = await Generation.GenerateAsync(actor, company, DemoDocument.Entity, SampleDocumentPage.SampleId, DemoPdfGenerator.GeneratorCode);
Toasts.Show(ToastKind.Success, T["Ui.Attachments.GeneratedToast"], saved.FileName);
}
catch (Exception ex) when (ex is AccessDeniedException or ValidationFailedException)
{
Toasts.Show(ToastKind.Error, T["Ui.Print.SaveAttachment"], T["Ui.Attachments.Denied"]);
}
finally
{
_saving = false;
}
}
}