feat: parties with radar, record, checks, VIES and demo data (#41)

Customers, suppliers, prospects and leads live in one parties archive
with a kind and a subject type (ADR 0029). A customer that also supplies
carries is_also_supplier, so it shows in both radars with one code.

- Three radars (/anagrafiche/clienti, /fornitori, /contatti) with search,
  quick filters and bulk disable/enable; the record has eight tabs
  (main data, addresses, contacts, terms, fiscal and SDI, banks, notes and
  attachments, history) on the MasterDataPage base.
- PartyService saves the whole record in one transaction, checks it with
  the fiscal pack (VAT number, tax code, recipient code, country), treats
  a duplicate VAT number or a consumer without tax code as warnings, fills
  the default recipient code, runs VIES after commit for EU companies, and
  converts prospects/leads (new customer code, old one kept as
  previous_code) and suppliers (become customers that also supply).
- Delete falls back to disable when the party is referenced.
- DemoPartiesSeeder adds 3 salespeople and 21 parties with fictitious but
  formally valid identifiers; SeedAsync now runs demo seeders when asked.
- Test host accepts service overrides so VIES can be stubbed.

Docs: ADR 0029, wiki Anagrafiche and demo data pages, regenerated data
model, CHANGELOG and STATUS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 10:06:33 +01:00
1 parent a9eda510f2
commit 3401271483
28 files changed
+4600 -85

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
using Khaemhat.Application.Security;
using Khaemhat.Application.Tables;
using Khaemhat.Core.Tables;
using Khaemhat.Web.Tables;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Parties;
/// <summary>The company tables a party points to, read once per page as choices (<c>code · description</c>).</summary>
public sealed class PartyLookups
{
/// <summary>Nothing loaded.</summary>
public static PartyLookups Empty { get; } = new();
/// <summary>Payment terms, by id.</summary>
public IReadOnlyList<KhOption<string>> PaymentTerms { get; private init; } = [];
/// <summary>Price lists, by id.</summary>
public IReadOnlyList<KhOption<string>> PriceLists { get; private init; } = [];
/// <summary>Salespeople, by id.</summary>
public IReadOnlyList<KhOption<string>> Salespeople { get; private init; } = [];
/// <summary>Party categories, by id.</summary>
public IReadOnlyList<KhOption<string>> Categories { get; private init; } = [];
/// <summary>Carriers, by id.</summary>
public IReadOnlyList<KhOption<string>> Carriers { get; private init; } = [];
/// <summary>Shipping terms, by id.</summary>
public IReadOnlyList<KhOption<string>> ShippingTerms { get; private init; } = [];
/// <summary>Warehouses, by id.</summary>
public IReadOnlyList<KhOption<string>> Warehouses { get; private init; } = [];
/// <summary>Company bank accounts, by id.</summary>
public IReadOnlyList<KhOption<string>> BankAccounts { get; private init; } = [];
/// <summary>VAT codes, by id.</summary>
public IReadOnlyList<KhOption<string>> TaxCodes { get; private init; } = [];
/// <summary>Countries, by code.</summary>
public IReadOnlyList<KhOption<string>> Countries { get; private init; } = [];
/// <summary>Currencies, by code.</summary>
public IReadOnlyList<KhOption<string>> Currencies { get; private init; } = [];
/// <summary>Reads the choices the radar needs.</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<PartyLookups> ForRadarAsync(TableService tables, Actor actor, string company, CancellationToken cancellationToken = default) => new()
{
PaymentTerms = await ReadAsync<PaymentTerm>(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),
Categories = await ReadAsync<PartyCategory>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
};
/// <summary>Reads every choice of the record page.</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<PartyLookups> ForRecordAsync(TableService tables, Actor actor, string company, CancellationToken cancellationToken = default) => new()
{
PaymentTerms = await ReadAsync<PaymentTerm>(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),
Categories = await ReadAsync<PartyCategory>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Carriers = await ReadAsync<Carrier>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
ShippingTerms = await ReadAsync<ShippingTerm>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Warehouses = await ReadAsync<Warehouse>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
BankAccounts = await ReadAsync<BankAccount>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
TaxCodes = await ReadAsync<TaxCode>(tables, actor, company, false, cancellationToken).ConfigureAwait(false),
Countries = await ReadAsync<Country>(tables, actor, company, true, cancellationToken).ConfigureAwait(false),
Currencies = await ReadAsync<Currency>(tables, actor, company, true, cancellationToken).ConfigureAwait(false),
};
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))];
}
}
+172
View File
@@ -0,0 +1,172 @@
using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Application.Parties;
using Khaemhat.Core.Parties;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Parties;
/// <summary>
/// Addresses, tabs and choices of the party screens (SPEC §11.2, mockups 012-016): the radars
/// <c>/anagrafiche/clienti</c>, <c>/fornitori</c>, <c>/contatti</c> and the record of a party under each of them.
/// </summary>
public static class PartyScreens
{
/// <summary>The menu area.</summary>
public const string BasePath = "/anagrafiche";
/// <summary>The tab ids of the record page, in order.</summary>
public static class Tabs
{
/// <summary>Main data.</summary>
public const string Main = "dati";
/// <summary>Other addresses.</summary>
public const string Addresses = "indirizzi";
/// <summary>Contact people.</summary>
public const string Contacts = "contatti";
/// <summary>Sales and purchase terms.</summary>
public const string Terms = "condizioni";
/// <summary>Fiscal data and e-invoice.</summary>
public const string Fiscal = "fiscale";
/// <summary>Bank accounts.</summary>
public const string Banks = "banche";
/// <summary>Notes and attachments.</summary>
public const string Notes = "note";
/// <summary>History.</summary>
public const string History = "storico";
}
/// <summary>The path segment of an archive.</summary>
/// <param name="archive">The archive.</param>
/// <returns><c>clienti</c>, <c>fornitori</c> or <c>contatti</c>.</returns>
public static string SegmentOf(PartyArchive archive) => archive switch
{
PartyArchive.Suppliers => "fornitori",
PartyArchive.Contacts => "contatti",
_ => "clienti",
};
/// <summary>The archive of an address such as <c>/anagrafiche/fornitori/12</c>.</summary>
/// <param name="path">The path, absolute or relative, with or without query.</param>
/// <returns>The archive; customers when the segment is not recognised.</returns>
public static PartyArchive ArchiveOf(string? path)
{
var segments = (path ?? string.Empty).Split('?', '#')[0].Split('/', StringSplitOptions.RemoveEmptyEntries);
var index = Array.IndexOf(segments, BasePath.TrimStart('/'));
return (index >= 0 && index + 1 < segments.Length ? segments[index + 1] : null) switch
{
"fornitori" => PartyArchive.Suppliers,
"contatti" => PartyArchive.Contacts,
_ => PartyArchive.Customers,
};
}
/// <summary>The archive a party belongs to first.</summary>
/// <param name="kind">The kind.</param>
/// <returns>The archive.</returns>
public static PartyArchive ArchiveOf(PartyKind kind) => kind switch
{
PartyKind.Supplier => PartyArchive.Suppliers,
PartyKind.Prospect or PartyKind.Lead => PartyArchive.Contacts,
_ => PartyArchive.Customers,
};
/// <summary>The kind of a new party of an archive.</summary>
/// <param name="archive">The archive.</param>
/// <returns>The kind.</returns>
public static PartyKind NewKindOf(PartyArchive archive) => archive switch
{
PartyArchive.Suppliers => PartyKind.Supplier,
PartyArchive.Contacts => PartyKind.Prospect,
_ => PartyKind.Customer,
};
/// <summary>The radar of an archive.</summary>
/// <param name="archive">The archive.</param>
/// <returns>The address, relative to the base.</returns>
public static string RadarUrl(PartyArchive archive) => "anagrafiche/" + SegmentOf(archive);
/// <summary>The record of a party.</summary>
/// <param name="archive">The archive it is opened from.</param>
/// <param name="id">The party.</param>
/// <returns>The address, relative to the base.</returns>
public static string RecordUrl(PartyArchive archive, long id) => RadarUrl(archive) + "/" + id.ToString(CultureInfo.InvariantCulture);
/// <summary>A new party of an archive.</summary>
/// <param name="archive">The archive.</param>
/// <param name="kind">The kind, when not the default of the archive (a lead among the contacts).</param>
/// <returns>The address, relative to the base.</returns>
public static string NewUrl(PartyArchive archive, PartyKind? kind = null) =>
RadarUrl(archive) + "/nuovo" + (kind is { } k && k != NewKindOf(archive) ? "?tipo=" + k.ToString().ToLowerInvariant() : string.Empty);
/// <summary>The kind asked by <c>?tipo=</c>, when it fits the archive.</summary>
/// <param name="archive">The archive.</param>
/// <param name="value">The value of the query.</param>
/// <returns>The kind.</returns>
public static PartyKind KindOf(PartyArchive archive, string? value) =>
Enum.TryParse<PartyKind>(value, ignoreCase: true, out var kind) && ArchiveOf(kind) == archive ? kind : NewKindOf(archive);
/// <summary>The tab that holds a field of the service (<c>Fiscal.RecipientCode</c>, <c>Contacts.0.Email</c>).</summary>
/// <param name="field">The field.</param>
/// <returns>The tab id.</returns>
public static string TabOf(string? field)
{
var head = (field ?? string.Empty).Split('.')[0];
return head switch
{
"Addresses" => Tabs.Addresses,
"Contacts" => Tabs.Contacts,
"BankAccounts" => Tabs.Banks,
"Sales" or "Purchase" => Tabs.Terms,
"Fiscal" => Tabs.Fiscal,
nameof(Party.Notes) => Tabs.Notes,
_ => Tabs.Main,
};
}
/// <summary>The choices of an enum, with the texts <c>Parties.&lt;group&gt;.&lt;value&gt;</c>.</summary>
/// <typeparam name="TEnum">The enum.</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["Parties." + group + "." + v]))];
}
/// <summary>An id as the value of a select.</summary>
/// <param name="id">The id.</param>
/// <returns>The text.</returns>
public static string? Text(long? id) => id?.ToString(CultureInfo.InvariantCulture);
/// <summary>The value of a select as an id.</summary>
/// <param name="value">The text.</param>
/// <returns>The id.</returns>
public static long? Id(string? value) => long.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var id) ? id : null;
/// <summary>The code part of a lookup text (<c>RB60 · Ricevuta bancaria 60 gg</c>).</summary>
/// <param name="options">The choices.</param>
/// <param name="id">The id.</param>
/// <param name="full">Whether to keep the description.</param>
/// <returns>The text, or <see langword="null"/>.</returns>
public static string? Describe(IReadOnlyList<KhOption<string>> options, long? id, bool full = false)
{
ArgumentNullException.ThrowIfNull(options);
if (Text(id) is not { } value || options.FirstOrDefault(o => o.Value == value) is not { } option)
{
return null;
}
var parts = option.Text.Split(" · ", 2);
return full && parts.Length > 1 ? parts[1] : parts[0];
}
}