using Khaemhat.Application.Security;
using Khaemhat.Application.Tables;
using Khaemhat.Core.Tables;
using Khaemhat.Web.Tables;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Documents;
///
/// 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 code · description.
///
public sealed class DocumentLookups
{
/// No choices, before loading.
public static DocumentLookups Empty { get; } = new();
/// Payment terms.
public IReadOnlyList> PaymentTerms { get; private init; } = [];
/// Payment methods.
public IReadOnlyList> PaymentMethods { get; private init; } = [];
/// Price lists.
public IReadOnlyList> PriceLists { get; private init; } = [];
/// Salespeople.
public IReadOnlyList> Salespeople { get; private init; } = [];
/// The company bank accounts.
public IReadOnlyList> BankAccounts { get; private init; } = [];
/// Warehouses.
public IReadOnlyList> Warehouses { get; private init; } = [];
/// Warehouse reasons (causali di magazzino).
public IReadOnlyList> InventoryReasons { get; private init; } = [];
/// The ids of the reasons that move goods between two warehouses.
public IReadOnlySet TransferReasons { get; private init; } = new HashSet();
/// Carriers.
public IReadOnlyList> Carriers { get; private init; } = [];
/// Shipping terms (porto).
public IReadOnlyList> ShippingTerms { get; private init; } = [];
/// Reasons of transport.
public IReadOnlyList> TransportReasons { get; private init; } = [];
/// Appearances of the goods.
public IReadOnlyList> GoodsAppearances { get; private init; } = [];
/// VAT codes.
public IReadOnlyList> TaxCodes { get; private init; } = [];
/// Units of measure.
public IReadOnlyList> Units { get; private init; } = [];
/// Currencies, by code.
public IReadOnlyList> Currencies { get; private init; } = [];
/// Reads the choices.
/// The tables.
/// Who reads.
/// The company.
/// Cancellation.
/// The choices.
public static async Task ReadAsync(TableService tables, Actor actor, string company, CancellationToken cancellationToken = default) => new()
{
PaymentTerms = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
PaymentMethods = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
PriceLists = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Salespeople = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
BankAccounts = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Warehouses = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
InventoryReasons = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TransferReasons = (await tables.ActiveAsync(actor, company, cancellationToken).ConfigureAwait(false)).Where(r => r.IsTransfer).Select(r => r.Id).ToHashSet(),
Carriers = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
ShippingTerms = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TransportReasons = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
GoodsAppearances = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TaxCodes = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Units = await ReadAsync(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Currencies = await ReadAsync(tables, actor, company, true, cancellationToken).ConfigureAwait(false),
};
/// The code of a choice (the part before · ).
/// The choices.
/// The id.
/// The code, or .
public static string? CodeOf(IReadOnlyList> 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];
}
/// The id of the choice with a code, ignoring case.
/// The choices.
/// The code typed.
/// The id, or .
public static long? IdOf(IReadOnlyList> 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>> ReadAsync(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(c.Value, c.Text))];
}
}