using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Core.Documents;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Documents;
/// A radar of the documents menus (sales, purchases, warehouse) and the document types it holds.
/// The path segment, such as fatture.
/// The family of the documents.
/// The kinds shown.
/// The document types a user can create from the radar, the first being the usual one.
/// The icon.
/// The key of the texts under Sales.Sections.
/// The menu area, such as vendite or acquisti.
public sealed record SalesSection(
string Segment, DocumentFamily Family, IReadOnlyList Kinds, IReadOnlyList NewTypes, string Icon, string Key, string Area = SalesScreens.BasePath)
{
/// Whose documents: customers, suppliers or nobody.
public DocumentDirection Direction => DocumentBehaviors.Of(Kinds[0]).Direction;
}
///
/// Addresses and labels of the document screens (SPEC §11.3-§11.6, mockups 020-037): one radar per menu entry under
/// /vendite, /acquisti and /magazzino, 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).
///
public static class SalesScreens
{
/// The sales area.
public const string BasePath = "vendite";
/// The purchases area.
public const string PurchasesPath = "acquisti";
/// The warehouse area.
public const string InventoryPath = "magazzino";
/// The deferred invoicing of the delivery notes.
public const string DeferredInvoicingUrl = BasePath + "/fatturazione-differita";
/// The tab of the transport data, after Testata, for delivery notes and accompanying invoices.
public const string TransportTab = "trasporto";
/// The radars, in the order of the menu.
public static IReadOnlyList 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),
];
/// The sales radar of a path segment.
/// The segment.
/// The radar, or .
public static SalesSection? Find(string? segment) => Find(BasePath, segment);
/// The radar of an area and a path segment.
/// The area, such as acquisti.
/// The segment.
/// The radar, or .
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));
/// The area of a relative address: its first segment.
/// The address relative to the base, such as acquisti/fatture/3.
/// The area.
public static string AreaOf(string? relativePath)
{
var path = relativePath ?? string.Empty;
var end = path.IndexOfAny(['/', '?', '#']);
return end < 0 ? path : path[..end];
}
/// The key of the label of the party for a direction: customer, supplier.
/// The direction.
/// The key of the text.
public static string PartyLabel(DocumentDirection direction) => direction == DocumentDirection.Purchase ? "Purchases.Fields.Supplier" : "Sales.Fields.PartyId";
/// The radar that holds a kind of document.
/// The kind.
/// The radar, or for a kind outside sales.
public static SalesSection? Of(DocumentKind kind) => Sections.FirstOrDefault(s => s.Kinds.Contains(kind));
/// The address of a radar.
/// The radar.
/// The address.
public static string RadarUrl(SalesSection section)
{
ArgumentNullException.ThrowIfNull(section);
return section.Area + "/" + section.Segment;
}
/// The address of a document.
/// The kind.
/// The document.
/// The address, or for a kind outside sales.
public static string? RecordUrl(DocumentKind kind, long id) =>
Of(kind) is { } section ? RadarUrl(section) + "/" + id.ToString(CultureInfo.InvariantCulture) : null;
/// The address of a new document of a type.
/// The radar.
/// The document type.
/// The address.
public static string NewUrl(SalesSection section, string typeCode) => RadarUrl(section) + "/nuovo?tipo=" + Uri.EscapeDataString(typeCode);
/// The print preview of a document.
/// The radar.
/// The document.
/// The relative URL.
public static string PrintUrl(SalesSection section, long id)
{
ArgumentNullException.ThrowIfNull(section);
return RadarUrl(section) + "/" + id.ToString(System.Globalization.CultureInfo.InvariantCulture) + "/stampa";
}
/// The name of a kind, such as Fattura immediata.
/// The texts.
/// The kind.
/// The name.
public static string KindName(ITexts texts, DocumentKind kind)
{
ArgumentNullException.ThrowIfNull(texts);
return texts["Sales.Kinds." + kind];
}
///
/// 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).
///
/// The texts.
/// The document number.
/// The state.
/// The text.
public static string Number(ITexts texts, string? number, DocumentStatus status)
{
ArgumentNullException.ThrowIfNull(texts);
return number ?? texts[status == DocumentStatus.Draft ? "Sales.DraftNumber" : "Sales.NoNumber"];
}
/// The label and tone of a state; a quotation shows its outcome (accepted, refused, expired).
/// The texts.
/// The kind.
/// The state.
/// Until when a quotation is valid.
/// Today.
/// The label and the tone.
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);
}
/// The choices of an enumeration, labelled from Sales.{group}.{value}.
/// The enumeration.
/// The texts.
/// The group of texts.
/// The choices.
public static IReadOnlyList> Choices(ITexts texts, string group)
where TEnum : struct, Enum
{
ArgumentNullException.ThrowIfNull(texts);
return [.. Enum.GetValues().Select(v => new KhOption(v.ToString(), texts["Sales." + group + "." + v]))];
}
}