Files
Khaemhat/src/Khaemhat.Web/Documents/SalesScreens.cs
T
Alby96andClaude Opus 5.5 cd4cb85562 fix: tidy the warehouse screens and the supplier document labels (#55 #60)
Seen while checking M6 in the browser: the stock take toolbar crowded the panel title,
the reorder and stock take tables ran past their panel, and the movements dialog cut
the balance column. Confirmed supplier receipts have no number of their own and showed
"Bozza"; they now read "Senza numero". The self-invoice advice no longer doubles the
full stop, and a single price difference reads in the singular.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 22:24:43 +01:00

200 lines
11 KiB
C#

using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Core.Documents;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Documents;
/// <summary>A radar of the documents menus (sales, purchases, warehouse) 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>
/// <param name="Area">The menu area, such as <c>vendite</c> or <c>acquisti</c>.</param>
public sealed record SalesSection(
string Segment, DocumentFamily Family, IReadOnlyList<DocumentKind> Kinds, IReadOnlyList<string> NewTypes, string Icon, string Key, string Area = SalesScreens.BasePath)
{
/// <summary>Whose documents: customers, suppliers or nobody.</summary>
public DocumentDirection Direction => DocumentBehaviors.Of(Kinds[0]).Direction;
}
/// <summary>
/// Addresses and labels of the document screens (SPEC §11.3-§11.6, mockups 020-037): one radar per menu entry under
/// <c>/vendite</c>, <c>/acquisti</c> and <c>/magazzino</c>, the record of a document under it, and the deferred
/// invoicing. The screens are the same for every area; the labels follow the direction (customer or supplier).
/// </summary>
public static class SalesScreens
{
/// <summary>The sales area.</summary>
public const string BasePath = "vendite";
/// <summary>The purchases area.</summary>
public const string PurchasesPath = "acquisti";
/// <summary>The warehouse area.</summary>
public const string InventoryPath = "magazzino";
/// <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"),
new("ordini", DocumentFamily.PurchaseOrder, [DocumentKind.PurchaseOrder], ["OF"], "file-check", "PurchaseOrders", PurchasesPath),
new("ricevimenti", DocumentFamily.PurchaseDocument, [DocumentKind.PurchaseDeliveryNote], ["DDTF"], "truck", "Receipts", PurchasesPath),
new("fatture", DocumentFamily.PurchaseDocument, [DocumentKind.PurchaseInvoice, DocumentKind.PurchaseCreditNote], ["FF", "NCF"], "euro", "PurchaseInvoices", PurchasesPath),
new("autofatture", DocumentFamily.SaleDocument, [DocumentKind.SelfInvoice],
["AF16", "AF17", "AF18", "AF19", "AF20", "AF21", "AF22", "AF23", "AF28", "AF29"], "refresh", "SelfInvoices", PurchasesPath),
new("movimenti", DocumentFamily.InventoryDocument, [DocumentKind.InventoryEntry], ["MM"], "layers", "Movements", InventoryPath),
];
/// <summary>The sales 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) => Find(BasePath, segment);
/// <summary>The radar of an area and a path segment.</summary>
/// <param name="area">The area, such as <c>acquisti</c>.</param>
/// <param name="segment">The segment.</param>
/// <returns>The radar, or <see langword="null"/>.</returns>
public static SalesSection? Find(string? area, string? segment) =>
Sections.FirstOrDefault(s => string.Equals(s.Area, area, StringComparison.OrdinalIgnoreCase) && string.Equals(s.Segment, segment, StringComparison.OrdinalIgnoreCase));
/// <summary>The area of a relative address: its first segment.</summary>
/// <param name="relativePath">The address relative to the base, such as <c>acquisti/fatture/3</c>.</param>
/// <returns>The area.</returns>
public static string AreaOf(string? relativePath)
{
var path = relativePath ?? string.Empty;
var end = path.IndexOfAny(['/', '?', '#']);
return end < 0 ? path : path[..end];
}
/// <summary>The key of the label of the party for a direction: customer, supplier.</summary>
/// <param name="direction">The direction.</param>
/// <returns>The key of the text.</returns>
public static string PartyLabel(DocumentDirection direction) => direction == DocumentDirection.Purchase ? "Purchases.Fields.Supplier" : "Sales.Fields.PartyId";
/// <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 section.Area + "/" + 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 number shown for a document: its own number, else "Bozza" for a draft and "Senza numero" for a confirmed
/// document of a type without numerator (a supplier delivery note, which keeps the supplier's number).
/// </summary>
/// <param name="texts">The texts.</param>
/// <param name="number">The document number.</param>
/// <param name="status">The state.</param>
/// <returns>The text.</returns>
public static string Number(ITexts texts, string? number, DocumentStatus status)
{
ArgumentNullException.ThrowIfNull(texts);
return number ?? texts[status == DocumentStatus.Draft ? "Sales.DraftNumber" : "Sales.NoNumber"];
}
/// <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]))];
}
}