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

+144
View File
@@ -0,0 +1,144 @@
using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Core.Documents;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Documents;
/// <summary>A radar of the sales menu and the document types it holds.</summary>
/// <param name="Segment">The path segment, such as <c>fatture</c>.</param>
/// <param name="Family">The family of the documents.</param>
/// <param name="Kinds">The kinds shown.</param>
/// <param name="NewTypes">The document types a user can create from the radar, the first being the usual one.</param>
/// <param name="Icon">The icon.</param>
/// <param name="Key">The key of the texts under <c>Sales.Sections</c>.</param>
public sealed record SalesSection(string Segment, DocumentFamily Family, IReadOnlyList<DocumentKind> Kinds, IReadOnlyList<string> NewTypes, string Icon, string Key);
/// <summary>
/// Addresses and labels of the sales screens (SPEC §11.3, mockups 020-030): one radar per menu entry under
/// <c>/vendite</c>, the record of a document under it, and the deferred invoicing.
/// </summary>
public static class SalesScreens
{
/// <summary>The menu area.</summary>
public const string BasePath = "vendite";
/// <summary>The deferred invoicing of the delivery notes.</summary>
public const string DeferredInvoicingUrl = BasePath + "/fatturazione-differita";
/// <summary>The tab of the transport data, after Testata, for delivery notes and accompanying invoices.</summary>
public const string TransportTab = "trasporto";
/// <summary>The radars, in the order of the menu.</summary>
public static IReadOnlyList<SalesSection> Sections { get; } =
[
new("preventivi", DocumentFamily.SaleQuotation, [DocumentKind.Quotation], ["PRE"], "file", "Quotes"),
new("ordini", DocumentFamily.SaleOrder, [DocumentKind.SalesOrder], ["OC"], "file-check", "Orders"),
new("ddt", DocumentFamily.SaleDocument, [DocumentKind.DeliveryNote], ["DDT"], "truck", "DeliveryNotes"),
new("fatture", DocumentFamily.SaleDocument,
[DocumentKind.ImmediateInvoice, DocumentKind.AccompanyingInvoice, DocumentKind.DeferredInvoice, DocumentKind.DownPaymentInvoice, DocumentKind.Fee, DocumentKind.ProForma],
["FI", "FA", "FD", "FACC", "PAR", "PF"], "euro", "Invoices"),
new("note-credito", DocumentFamily.SaleDocument, [DocumentKind.CreditNote, DocumentKind.DebitNote], ["NC", "ND"], "undo", "CreditNotes"),
];
/// <summary>The radar of a path segment.</summary>
/// <param name="segment">The segment.</param>
/// <returns>The radar, or <see langword="null"/>.</returns>
public static SalesSection? Find(string? segment) => Sections.FirstOrDefault(s => string.Equals(s.Segment, segment, StringComparison.OrdinalIgnoreCase));
/// <summary>The radar that holds a kind of document.</summary>
/// <param name="kind">The kind.</param>
/// <returns>The radar, or <see langword="null"/> for a kind outside sales.</returns>
public static SalesSection? Of(DocumentKind kind) => Sections.FirstOrDefault(s => s.Kinds.Contains(kind));
/// <summary>The address of a radar.</summary>
/// <param name="section">The radar.</param>
/// <returns>The address.</returns>
public static string RadarUrl(SalesSection section)
{
ArgumentNullException.ThrowIfNull(section);
return BasePath + "/" + section.Segment;
}
/// <summary>The address of a document.</summary>
/// <param name="kind">The kind.</param>
/// <param name="id">The document.</param>
/// <returns>The address, or <see langword="null"/> for a kind outside sales.</returns>
public static string? RecordUrl(DocumentKind kind, long id) =>
Of(kind) is { } section ? RadarUrl(section) + "/" + id.ToString(CultureInfo.InvariantCulture) : null;
/// <summary>The address of a new document of a type.</summary>
/// <param name="section">The radar.</param>
/// <param name="typeCode">The document type.</param>
/// <returns>The address.</returns>
public static string NewUrl(SalesSection section, string typeCode) => RadarUrl(section) + "/nuovo?tipo=" + Uri.EscapeDataString(typeCode);
/// <summary>The print preview of a document.</summary>
/// <param name="section">The radar.</param>
/// <param name="id">The document.</param>
/// <returns>The relative URL.</returns>
public static string PrintUrl(SalesSection section, long id)
{
ArgumentNullException.ThrowIfNull(section);
return RadarUrl(section) + "/" + id.ToString(System.Globalization.CultureInfo.InvariantCulture) + "/stampa";
}
/// <summary>The name of a kind, such as <c>Fattura immediata</c>.</summary>
/// <param name="texts">The texts.</param>
/// <param name="kind">The kind.</param>
/// <returns>The name.</returns>
public static string KindName(ITexts texts, DocumentKind kind)
{
ArgumentNullException.ThrowIfNull(texts);
return texts["Sales.Kinds." + kind];
}
/// <summary>The label and tone of a state; a quotation shows its outcome (accepted, refused, expired).</summary>
/// <param name="texts">The texts.</param>
/// <param name="kind">The kind.</param>
/// <param name="status">The state.</param>
/// <param name="validUntil">Until when a quotation is valid.</param>
/// <param name="today">Today.</param>
/// <returns>The label and the tone.</returns>
public static (string Text, KhTone Tone) State(ITexts texts, DocumentKind kind, DocumentStatus status, DateOnly? validUntil, DateOnly today)
{
ArgumentNullException.ThrowIfNull(texts);
if (kind == DocumentKind.Quotation)
{
switch (status)
{
case DocumentStatus.Fulfilled or DocumentStatus.PartiallyFulfilled:
return (texts["Sales.Outcomes.Accepted"], KhTone.Ok);
case DocumentStatus.Closed:
return (texts["Sales.Outcomes.Refused"], KhTone.Error);
case DocumentStatus.Confirmed when validUntil is { } until && until < today:
return (texts["Sales.Outcomes.Expired"], KhTone.Warning);
case DocumentStatus.Confirmed:
return (texts["Sales.Outcomes.Sent"], KhTone.Info);
}
}
var tone = status switch
{
DocumentStatus.Draft => KhTone.Neutral,
DocumentStatus.Confirmed or DocumentStatus.Issued => KhTone.Info,
DocumentStatus.PartiallyFulfilled => KhTone.Warning,
DocumentStatus.Fulfilled or DocumentStatus.SdiDelivered => KhTone.Ok,
DocumentStatus.Cancelled or DocumentStatus.SdiRejected or DocumentStatus.SdiNotDelivered => KhTone.Error,
_ => KhTone.Accent,
};
return (texts["Sales.States." + status], tone);
}
/// <summary>The choices of an enumeration, labelled from <c>Sales.{group}.{value}</c>.</summary>
/// <typeparam name="TEnum">The enumeration.</typeparam>
/// <param name="texts">The texts.</param>
/// <param name="group">The group of texts.</param>
/// <returns>The choices.</returns>
public static IReadOnlyList<KhOption<string>> Choices<TEnum>(ITexts texts, string group)
where TEnum : struct, Enum
{
ArgumentNullException.ThrowIfNull(texts);
return [.. Enum.GetValues<TEnum>().Select(v => new KhOption<string>(v.ToString(), texts["Sales." + group + "." + v]))];
}
}