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,185 @@
@page "/vendite/{Segment}"
@attribute [Authorize]
@using Khaemhat.Application.Documents
@using Khaemhat.Core.Documents
@using Khaemhat.Web.Documents
@inject ShellSession Session
@inject DocumentEngine Engine
@inject DocumentEditor Editor
@inject NavigationManager Navigation
@inject TimeProvider Clock
@* The radar of a kind of sales documents (SPEC §11.3): quotations, orders, delivery notes, invoices (with pro forma
and fees), credit and debit notes. Search on number, reference and customer; filters for state, type and year;
totals of the page. Enter or double click opens the document. *@
<KhPageTitle Text="@Title" Icon="@(_section?.Icon ?? "cart")" />
@if (!_loaded)
{
<p class="muted kh-pad">@T["Common.Loading"]</p>
}
else if (Session.Company is null)
{
<div class="kh-message"><KhIcon Name="building" Class="lg" /><h1>@T["Sales.NoCompany"]</h1></div>
}
else if (_section is null)
{
<div class="kh-message"><KhIcon Name="alert" Class="lg" /><h1>@T["Pages.NotFound.Title"]</h1></div>
}
else if (!CanRead)
{
<div class="kh-message">
<KhIcon Name="lock" Class="lg" />
<h1>@T["Pages.AccessDenied.Title"]</h1>
<p class="lead">@T["Pages.AccessDenied.Lead"]</p>
</div>
}
else
{
<KhRadar TItem="DocumentRow" @key="_section.Segment" Id="@("sales." + _section.Segment)" Columns="Columns" Source="ListAsync" KeyOf="r => r.Id" Selectable="false"
OnOpen="Open" QuickFilters="QuickFilters" RowClass="RowClassOf" SearchPlaceholder="@T["Sales.Radar.Search"]" ExportName="@_section.Segment"
Label="@Title">
<Toolbar>
@if (NewTypes.Count > 0)
{
<button type="button" class="btn primary" @onclick="New"><KhIcon Name="plus" /> @T["Sales.Sections." + _section.Key + ".New"]</button>
}
@if (_section.Segment == "fatture" && Session.Permissions.Allows("sales.invoice.write"))
{
<a class="btn" href="@SalesScreens.DeferredInvoicingUrl"><KhIcon Name="layers" /> @T["Sales.Deferred.Title"]</a>
}
</Toolbar>
</KhRadar>
<KhDialog Open="_choosing" Title="@T["Sales.NewDocument"]" Icon="plus" OnClose="() => _choosing = false">
<ChildContent>
<div class="kh-choice-list">
@foreach (var type in NewTypes)
{
<button type="button" class="btn kh-choice" @onclick="() => Create(type.Code)">
<span class="code">@type.Code</span>
<b>@type.Type.Description</b>
<span class="muted sm">@T["Sales.Hints.Kinds." + type.Kind]</span>
</button>
}
</div>
</ChildContent>
</KhDialog>
}
@code {
private SalesSection? _section;
private IReadOnlyList<DocumentDefinition> _definitions = [];
private bool _loaded;
private bool _choosing;
/// <summary>The radar segment, such as <c>fatture</c>.</summary>
[Parameter]
public string? Segment { get; set; }
private string Title => _section is null ? T["Shell.Menu.Sales"] : T["Sales.Sections." + _section.Key + ".Title"];
private bool CanRead => _section is not null && Session.Permissions.Allows(DocumentBehaviors.Of(_section.Kinds[0]).ReadPermission);
private DateOnly Today => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
// The types of the radar the user may create, in the order of the section.
private List<DocumentDefinition> NewTypes => _section is null ? [] :
[.. _section.NewTypes.Select(code => _definitions.FirstOrDefault(d => d.Code == code)).OfType<DocumentDefinition>()
.Where(d => Session.Permissions.Allows(d.Behavior.WritePermission))];
private IReadOnlyList<KhColumn<DocumentRow>> Columns =>
[
new("number", T["Sales.Fields.DocumentNumber"], r => r.DocumentNumber ?? T["Sales.DraftNumber"]) { Kind = KhColumnKind.Code, Width = "130px" },
new("date", T["Sales.Fields.DocumentDate"], r => r.Date) { Kind = KhColumnKind.Date, Width = "100px" },
new("type", T["Sales.Radar.Kind"], r => SalesScreens.KindName(T, r.Kind)) { Width = "150px", Sortable = false, Visible = _section?.Kinds.Count > 1 },
new("party", T["Sales.Fields.PartyId"], r => r.PartyName) { Sortable = false },
new("reference", T["Sales.Fields.ExternalReference"], r => r.Reference) { Width = "140px" },
new("validUntil", T["Sales.Fields.ValidUntil"], r => r.ValidUntil) { Kind = KhColumnKind.Date, Width = "100px", Visible = _section?.Segment == "preventivi" },
new("delivery", T["Sales.Fields.DeliveryDate"], r => r.DeliveryDate) { Kind = KhColumnKind.Date, Width = "100px", Visible = _section?.Segment is "ordini" or "ddt" },
new("taxable", T["Sales.Totals.Taxable"], r => r.Taxable) { Kind = KhColumnKind.Money, Width = "120px" },
new("total", T["Sales.Totals.Total"], r => r.Total) { Kind = KhColumnKind.Money, Width = "120px" },
new("status", T["Sales.Radar.State"], r => StateOf(r).Text)
{
Width = "130px",
Template = r => @<KhBadge Tone="StateOf(r).Tone">@StateOf(r).Text</KhBadge>,
},
];
private IReadOnlyList<KhQuickFilter> QuickFilters
{
get
{
var filters = new List<KhQuickFilter>
{
new("status", T["Sales.Radar.State"], [new(string.Empty, T["Sales.Radar.All"]), .. States.Select(s => new KhOption<string>(s.ToString(), T["Sales.States." + s]))]),
};
if (_section is { Kinds.Count: > 1 })
{
filters.Add(new("kind", T["Sales.Radar.Kind"], [new(string.Empty, T["Sales.Radar.All"]), .. _section.Kinds.Select(k => new KhOption<string>(k.ToString(), SalesScreens.KindName(T, k)))]));
}
var year = Today.Year;
filters.Add(new("year", T["Sales.Radar.Year"],
[new(string.Empty, T["Sales.Radar.All"]), .. Enumerable.Range(0, 3).Select(i => new KhOption<string>((year - i).ToString(T.Culture), (year - i).ToString(T.Culture)))]));
return filters;
}
}
// The states that make sense for the life cycle of the radar.
private IEnumerable<DocumentStatus> States => _section is null ? [] :
DocumentBehaviors.Of(_section.Kinds[0]).Lifecycle == DocumentLifecycle.Invoice && _section.Kinds.All(k => DocumentBehaviors.Of(k).Lifecycle == DocumentLifecycle.Invoice)
? [DocumentStatus.Draft, DocumentStatus.Issued, DocumentStatus.Cancelled]
: [DocumentStatus.Draft, DocumentStatus.Confirmed, DocumentStatus.PartiallyFulfilled, DocumentStatus.Fulfilled, DocumentStatus.Closed, DocumentStatus.Cancelled, DocumentStatus.Issued];
/// <inheritdoc />
protected override async Task OnParametersSetAsync()
{
await Session.EnsureLoadedAsync();
_section = SalesScreens.Find(Segment);
if (!_loaded && Session.Actor is { } actor && Session.Company?.Code is { } company)
{
_definitions = await Engine.DefinitionsAsync(actor, company);
}
_loaded = true;
}
private (string Text, KhTone Tone) StateOf(DocumentRow row) => SalesScreens.State(T, row.Kind, row.Status, row.ValidUntil, Today);
private static string? RowClassOf(DocumentRow row) => row.Status == DocumentStatus.Cancelled ? "muted" : null;
private Task<ListPage<DocumentRow>> ListAsync(ListQuery query, CancellationToken cancellationToken) =>
Session.Actor is { } actor && Session.Company?.Code is { } company && _section is not null
? Editor.ListAsync(actor, company, _section.Family, _section.Kinds, query, cancellationToken)
: Task.FromResult(new ListPage<DocumentRow>([], 0, new Dictionary<string, decimal>()));
private void Open(DocumentRow row)
{
if (SalesScreens.RecordUrl(row.Kind, row.Id) is { } url)
{
Navigation.NavigateTo(url);
}
}
private void New()
{
if (NewTypes.Count == 1)
{
Create(NewTypes[0].Code);
}
else
{
_choosing = true;
}
}
private void Create(string code)
{
_choosing = false;
if (_section is not null)
{
Navigation.NavigateTo(SalesScreens.NewUrl(_section, code));
}
}
}