@page "/vendite/{Segment}/{Id:long}/stampa" @page "/acquisti/{Segment}/{Id:long}/stampa" @page "/magazzino/{Segment}/{Id:long}/stampa" @attribute [Authorize] @using Khaemhat.Application.Documents @using Khaemhat.Application.Security @using Khaemhat.Web.Documents @using Khaemhat.Web.Hosting @inject ShellSession Session @inject DocumentPrintService Prints @inject DocumentEngine Engine @inject FileGenerationService Generation @inject ToastService Toasts @inject NavigationManager Navigation @* The print preview of a sales document (SPEC §9.4, issue #52, mockup 025): the A4 sheet in HTML from the same model as the PDF, with Stampa (the print of the browser), the PDF made by Khaemhat and "Salva come allegato", which keeps the PDF among the attachments of the document as a new version. *@ @if (_model is null) {

@(_missing ? T["Sales.NotFound"] : T["Common.Loading"])

} else {
@_model.Title @_model.Number › @T["Sales.Print.Title"]

@Heading

@if (_model.LogoAttachmentId is { } logo) { }
@_model.Company.Name
@foreach (var line in _model.Company.Lines) {
@line
}
@_model.Title
@_model.Number
@_model.Date
@if (_model.Watermark is { } mark) {
@mark
}
@foreach (var field in _model.Fields) {
@field.Label
@field.Value
}
@_model.PartyLabel @_model.Party.Code
@_model.Party.Name @foreach (var line in _model.Party.Lines) {
@line
}
@for (var i = 0; i < _model.LineHeaders.Count; i++) { } @foreach (var line in _model.Lines) { if (line.IsDescription) { } else { } }
@_model.LineHeaders[i]
@line.Description
@line.Code @line.Description @line.Unit @line.Quantity @line.Price @line.Discount @line.Tax @line.Amount
@foreach (var note in _model.Notes) {

@note

}
@if (_model.Summary is { } summary) { @Table(summary) } @if (_model.Installments is { } installments) { @Table(installments) }
@foreach (var row in _model.Totals) { @row.Label @row.Value }
@if (_model.Transport.Count > 0) {
@foreach (var field in _model.Transport) {
@field.Label
@field.Value
}
@foreach (var signature in _model.Signatures) {
@signature
}
} @if (_model.Footer is { } footer) {

@footer

}
} @code { private SalesSection? _section; private DocumentPrintModel? _model; private bool _missing; private bool _saving; /// The radar segment, such as fatture. [Parameter] public string? Segment { get; set; } /// The document. [Parameter] public long Id { get; set; } private string Heading => _model is null ? T["Sales.Print.Title"] : T.Format("Sales.Print.Heading", _model.Title, _model.Number); private string BackUrl => _section is null ? SalesScreens.BasePath : SalesScreens.RadarUrl(_section) + "/" + Id.ToString(T.Culture); /// protected override async Task OnParametersSetAsync() { await Session.EnsureLoadedAsync(); _section = SalesScreens.Find(SalesScreens.AreaOf(Navigation.ToBaseRelativePath(Navigation.Uri)), Segment); if (_section is null || Session.Actor is not { } actor || Session.Company?.Code is not { } company) { _missing = true; return; } try { _model = await Prints.BuildAsync(actor, company, _section.Family, Id); } catch (Exception ex) when (ex is KeyNotFoundException or AccessDeniedException) { _missing = true; } } private string? PdfUrl(bool download) => _section is null ? null : FileEndpoints.Generated(DocumentPrintService.EntityOf(_section.Family), Id, DocumentPdfGenerator.GeneratorCode, download); private async Task SaveAsync() { if (_section is null || Session.Actor is not { } actor || Session.Company?.Code is not { } company) { return; } _saving = true; try { var saved = await Generation.GenerateAsync(actor, company, DocumentPrintService.EntityOf(_section.Family), Id, DocumentPdfGenerator.GeneratorCode); await Engine.MarkPrintedAsync(actor, company, _section.Family, Id); Toasts.Show(ToastKind.Success, T["Ui.Attachments.GeneratedToast"], saved.FileName); } catch (Exception ex) when (ex is AccessDeniedException or ValidationFailedException or KeyNotFoundException) { Toasts.Show(ToastKind.Error, T["Ui.Print.SaveAttachment"], T["Ui.Attachments.Denied"]); } finally { _saving = false; } } private static RenderFragment Table(PrintTable table) => __builder => {
@table.Title
@for (var i = 0; i < table.Headers.Count; i++) { } @foreach (var row in table.Rows) { @for (var i = 0; i < row.Count; i++) { } }
@table.Headers[i]
@row[i]
}; }