feat: sales screens, deferred invoicing, printing and mail of documents (#49 #50 #51 #52)

Quotations, orders, delivery notes, invoices and credit notes get their radars and
one document screen (lines, header, transport, links, attachments) with the
transformations of the engine, so a quotation reaches the invoice without
retyping. The deferred invoicing groups the open delivery notes of a period by
customer, payment and destination into one invoice each.

Every document prints from one model: an HTML preview on screen and a PDF drawn
by the in-house writer, kept among the attachments as a new version. "Invia per
e-mail" proposes recipient, subject and text from the templates, attaches the
PDF and hands it to a background queue that talks SMTP itself (no external
library); the outcome goes to the log, section E-mail/PEC.

The demo company now has a year of sales in every state, made through the
engine, and run-dev.sh no longer writes to a stray B:\b folder under Git Bash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 17:20:25 +01:00
1 parent f12848865d
commit 826042f828
31 files changed
+5682 -16

No files matched your search

@@ -0,0 +1,252 @@
@page "/vendite/{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
@* 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. *@
<KhPageTitle Text="@Heading" Icon="printer" />
@if (_model is null)
{
<div class="kh-message">
<KhIcon Name="@(_missing ? "alert" : "printer")" Class="lg" />
<h1>@(_missing ? T["Sales.NotFound"] : T["Common.Loading"])</h1>
</div>
}
else
{
<div class="phead">
<div>
<div class="crumbs"><a href="@BackUrl">@_model.Title @_model.Number</a> › <b>@T["Sales.Print.Title"]</b></div>
<h1>@Heading</h1>
</div>
</div>
<KhPrintPreview PdfUrl="@PdfUrl(false)" PdfDownloadUrl="@PdfUrl(true)" OnSaveAttachment="SaveAsync" Saving="_saving">
<div class="kh-sheet-head">
<div class="kh-print-company">
@if (_model.LogoAttachmentId is { } logo)
{
<img class="kh-print-logo" src="@FileEndpoints.View(logo)" alt="" />
}
<div>
<div class="kh-sheet-company">@_model.Company.Name</div>
@foreach (var line in _model.Company.Lines)
{
<div class="kh-sheet-muted">@line</div>
}
</div>
</div>
<div class="kh-sheet-title">
<div class="k">@_model.Title</div>
<div class="kh-print-number">@_model.Number</div>
<div class="n">@_model.Date</div>
@if (_model.Watermark is { } mark)
{
<div class="kh-print-mark">@mark</div>
}
</div>
</div>
<div class="kh-print-top">
<dl class="kh-print-fields">
@foreach (var field in _model.Fields)
{
<dt>@field.Label</dt>
<dd>@field.Value</dd>
}
</dl>
<div class="kh-print-party">
<div class="kh-sheet-muted">@_model.PartyLabel <span class="mono">@_model.Party.Code</span></div>
<b>@_model.Party.Name</b>
@foreach (var line in _model.Party.Lines)
{
<div>@line</div>
}
</div>
</div>
<table class="kh-sheet-table">
<thead>
<tr>
@for (var i = 0; i < _model.LineHeaders.Count; i++)
{
<th class="@(i >= 3 ? "num" : null)">@_model.LineHeaders[i]</th>
}
</tr>
</thead>
<tbody>
@foreach (var line in _model.Lines)
{
if (line.IsDescription)
{
<tr class="kh-print-text"><td></td><td colspan="7">@line.Description</td></tr>
}
else
{
<tr>
<td class="mono">@line.Code</td>
<td>@line.Description</td>
<td>@line.Unit</td>
<td class="num">@line.Quantity</td>
<td class="num">@line.Price</td>
<td class="num">@line.Discount</td>
<td class="num">@line.Tax</td>
<td class="num">@line.Amount</td>
</tr>
}
}
</tbody>
</table>
@foreach (var note in _model.Notes)
{
<p class="kh-print-note">@note</p>
}
<div class="kh-print-sums">
<div>
@if (_model.Summary is { } summary)
{
@Table(summary)
}
@if (_model.Installments is { } installments)
{
@Table(installments)
}
</div>
<div class="kh-print-totals">
@foreach (var row in _model.Totals)
{
<span class="@(row.Strong ? "big" : null)">@row.Label</span>
<span class="@(row.Strong ? "big num" : "num")">@row.Value</span>
}
</div>
</div>
@if (_model.Transport.Count > 0)
{
<dl class="kh-print-transport">
@foreach (var field in _model.Transport)
{
<div><dt>@field.Label</dt><dd>@field.Value</dd></div>
}
</dl>
<div class="kh-print-signatures">
@foreach (var signature in _model.Signatures)
{
<div>@signature</div>
}
</div>
}
@if (_model.Footer is { } footer)
{
<p class="kh-sheet-foot">@footer</p>
}
</KhPrintPreview>
}
@code {
private SalesSection? _section;
private DocumentPrintModel? _model;
private bool _missing;
private bool _saving;
/// <summary>The radar segment, such as <c>fatture</c>.</summary>
[Parameter]
public string? Segment { get; set; }
/// <summary>The document.</summary>
[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.BasePath + "/" + _section.Segment + "/" + Id.ToString(T.Culture);
/// <inheritdoc />
protected override async Task OnParametersSetAsync()
{
await Session.EnsureLoadedAsync();
_section = SalesScreens.Find(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 =>
{
<div class="kh-print-block">
<div class="kh-print-block-title">@table.Title</div>
<table class="kh-sheet-table">
<thead>
<tr>
@for (var i = 0; i < table.Headers.Count; i++)
{
<th class="@(table.Numeric[i] ? "num" : null)">@table.Headers[i]</th>
}
</tr>
</thead>
<tbody>
@foreach (var row in table.Rows)
{
<tr>
@for (var i = 0; i < row.Count; i++)
{
<td class="@(table.Numeric[i] ? "num" : null)">@row[i]</td>
}
</tr>
}
</tbody>
</table>
</div>
};
}