Files
Khaemhat/src/Khaemhat.Web/Documents/DocumentLookups.cs
T
Alby96andClaude Opus 5.5 f8b72f4d17 feat: purchase and warehouse documents on the document screens (#55 #56 #58)
The document radars and records now serve /acquisti and /magazzino too:
supplier orders, receipts, supplier invoices and credit notes, self-invoices
and warehouse movements, with the supplier in place of the customer, the
supplier's number and date, the protocol and VAT date of registered invoices,
and reason and warehouses of a movement. Supplier invoices take their
protocol from the purchase numbering, refuse a second document with the same
supplier number in the same year and show price differences against their
order or receipt. The Italian pack advises the self-invoice a received
invoice needs (TD16-TD19, TD28) with its deadline, and the purchase service
drafts it with the ordinary rate. A credit note of an invoice now moves its
goods back instead of being skipped as already moved.

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

129 lines
6.7 KiB
C#

using Khaemhat.Application.Security;
using Khaemhat.Application.Tables;
using Khaemhat.Core.Tables;
using Khaemhat.Web.Tables;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Documents;
/// <summary>
/// The choices of the document screens, read once per page from the company tables: conditions, transport, the VAT
/// codes and units of the lines. Values are ids, except the currencies (by code); texts are <c>code · description</c>.
/// </summary>
public sealed class DocumentLookups
{
/// <summary>No choices, before loading.</summary>
public static DocumentLookups Empty { get; } = new();
/// <summary>Payment terms.</summary>
public IReadOnlyList<KhOption<string>> PaymentTerms { get; private init; } = [];
/// <summary>Payment methods.</summary>
public IReadOnlyList<KhOption<string>> PaymentMethods { get; private init; } = [];
/// <summary>Price lists.</summary>
public IReadOnlyList<KhOption<string>> PriceLists { get; private init; } = [];
/// <summary>Salespeople.</summary>
public IReadOnlyList<KhOption<string>> Salespeople { get; private init; } = [];
/// <summary>The company bank accounts.</summary>
public IReadOnlyList<KhOption<string>> BankAccounts { get; private init; } = [];
/// <summary>Warehouses.</summary>
public IReadOnlyList<KhOption<string>> Warehouses { get; private init; } = [];
/// <summary>Warehouse reasons (causali di magazzino).</summary>
public IReadOnlyList<KhOption<string>> InventoryReasons { get; private init; } = [];
/// <summary>The ids of the reasons that move goods between two warehouses.</summary>
public IReadOnlySet<long> TransferReasons { get; private init; } = new HashSet<long>();
/// <summary>Carriers.</summary>
public IReadOnlyList<KhOption<string>> Carriers { get; private init; } = [];
/// <summary>Shipping terms (porto).</summary>
public IReadOnlyList<KhOption<string>> ShippingTerms { get; private init; } = [];
/// <summary>Reasons of transport.</summary>
public IReadOnlyList<KhOption<string>> TransportReasons { get; private init; } = [];
/// <summary>Appearances of the goods.</summary>
public IReadOnlyList<KhOption<string>> GoodsAppearances { get; private init; } = [];
/// <summary>VAT codes.</summary>
public IReadOnlyList<KhOption<string>> TaxCodes { get; private init; } = [];
/// <summary>Units of measure.</summary>
public IReadOnlyList<KhOption<string>> Units { get; private init; } = [];
/// <summary>Currencies, by code.</summary>
public IReadOnlyList<KhOption<string>> Currencies { get; private init; } = [];
/// <summary>Reads the choices.</summary>
/// <param name="tables">The tables.</param>
/// <param name="actor">Who reads.</param>
/// <param name="company">The company.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The choices.</returns>
public static async Task<DocumentLookups> ReadAsync(TableService tables, Actor actor, string company, CancellationToken cancellationToken = default) => new()
{
PaymentTerms = await ReadAsync<PaymentTerm>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
PaymentMethods = await ReadAsync<PaymentMethod>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
PriceLists = await ReadAsync<PriceList>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Salespeople = await ReadAsync<Salesperson>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
BankAccounts = await ReadAsync<BankAccount>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Warehouses = await ReadAsync<Warehouse>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
InventoryReasons = await ReadAsync<InventoryReason>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TransferReasons = (await tables.ActiveAsync<InventoryReason>(actor, company, cancellationToken).ConfigureAwait(false)).Where(r => r.IsTransfer).Select(r => r.Id).ToHashSet(),
Carriers = await ReadAsync<Carrier>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
ShippingTerms = await ReadAsync<ShippingTerm>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TransportReasons = await ReadAsync<TransportReason>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
GoodsAppearances = await ReadAsync<GoodsAppearance>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TaxCodes = await ReadAsync<TaxCode>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Units = await ReadAsync<UnitOfMeasure>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Currencies = await ReadAsync<Currency>(tables, actor, company, true, cancellationToken).ConfigureAwait(false),
};
/// <summary>The code of a choice (the part before <c> · </c>).</summary>
/// <param name="options">The choices.</param>
/// <param name="id">The id.</param>
/// <returns>The code, or <see langword="null"/>.</returns>
public static string? CodeOf(IReadOnlyList<KhOption<string>> options, long? id)
{
ArgumentNullException.ThrowIfNull(options);
if (id is null)
{
return null;
}
var value = id.Value.ToString(System.Globalization.CultureInfo.InvariantCulture);
var text = options.FirstOrDefault(o => o.Value == value)?.Text;
return text is null ? null : text.Split(" · ", 2)[0];
}
/// <summary>The id of the choice with a code, ignoring case.</summary>
/// <param name="options">The choices.</param>
/// <param name="code">The code typed.</param>
/// <returns>The id, or <see langword="null"/>.</returns>
public static long? IdOf(IReadOnlyList<KhOption<string>> options, string? code)
{
ArgumentNullException.ThrowIfNull(options);
if (string.IsNullOrWhiteSpace(code))
{
return null;
}
var wanted = code.Trim();
var match = options.FirstOrDefault(o => string.Equals(o.Text.Split(" · ", 2)[0], wanted, StringComparison.OrdinalIgnoreCase));
return match is null ? null : long.Parse(match.Value, System.Globalization.CultureInfo.InvariantCulture);
}
private static async Task<IReadOnlyList<KhOption<string>>> ReadAsync<T>(TableService tables, Actor actor, string company, bool byCode,
CancellationToken cancellationToken)
{
var choices = await TableValues.LookupAsync(tables, typeof(T), byCode, actor, company, cancellationToken).ConfigureAwait(false);
return [.. choices.Select(c => new KhOption<string>(c.Value, c.Text))];
}
}