feat: attachments panel, file previews, print preview and history (#34)

Documents need to show, upload and generate their files from the interface,
and users need to see who changed a record. This adds the attachment list
(upload, preview, download, lock, trash and restore, older versions, ZIP,
generate/regenerate), in-app previews for PDF, images, XML, text, CSV and
XLSX, an A4 print preview with "save as attachment", and an audit history
list read through a permission-checked AuditTrailService.

Files are served by /files endpoints that require a signed-in user and the
company permissions; only PDF and images open inline, with their own CSP
that allows framing by this site only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 19:59:37 +01:00
1 parent f415dc37a8
commit 8198e0a754
20 files changed
+1786 -24

No files matched your search

@@ -0,0 +1,110 @@
@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. *@
<KhPageTitle Text="@T["Pages.Print.Title"]" Icon="printer" />
@if (!Environment.IsDevelopment())
{
<div class="kh-message"><h1>@T["Pages.Catalog.DevOnly"]</h1></div>
}
else
{
<div class="phead">
<div>
<div class="crumbs">@T["Pages.Print.Crumb"] › <b>@T["Pages.Print.Title"]</b></div>
<h1>@T.Format("Pages.Print.Heading", DemoDocument.Title, Number)</h1>
</div>
</div>
<KhPrintPreview PdfUrl="@PdfUrl(false)" PdfDownloadUrl="@PdfUrl(true)" OnSaveAttachment="SaveAsync" Saving="_saving">
<div class="kh-sheet-head">
<div>
<div class="kh-sheet-company">@(Session.Company?.Name ?? Company)</div>
<div class="kh-sheet-muted">@T.Format("Pages.Print.CompanyLine", Company)</div>
</div>
<div class="kh-sheet-title">
<div class="k">@DemoDocument.Title</div>
<div class="n">@T.Format("Pages.Print.Number", Number, T.Date(Today))</div>
</div>
</div>
<p class="kh-sheet-intro">@T.Format("Pages.Print.Intro", Company, Session.Actor?.UserName ?? string.Empty)</p>
<table class="kh-sheet-table">
<thead>
<tr>
@for (var i = 0; i < DemoDocument.Headers.Count; i++)
{
<th class="@(i >= 2 ? "num" : null)">@DemoDocument.Headers[i]</th>
}
</tr>
</thead>
<tbody>
@foreach (var line in DemoDocument.Lines)
{
<tr>
<td class="mono">@line.Code</td>
<td>@line.Description</td>
<td class="num">@line.Quantity.ToString("#,##0.##", T.Culture)</td>
<td class="num">@DemoDocument.Euro(line.UnitPrice, T.Culture)</td>
<td class="num">@DemoDocument.Euro(DemoDocument.Amount(line), T.Culture)</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="4" class="num">@T["Pages.Print.Total"]</td>
<td class="num"><b>@DemoDocument.Euro(DemoDocument.Total, T.Culture)</b></td>
</tr>
</tfoot>
</table>
<p class="kh-sheet-foot">@T["Pages.Print.Footer"]</p>
</KhPrintPreview>
}
@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);
/// <inheritdoc />
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;
}
}
}