feat: company table screens, profile and fiscal years, seed command (#38, #39, #45)

Gives the company tables of the previous commit their screens, so a company
can be configured from the browser, and makes the base data runnable outside
the web server.

- one generic editor for the 26 coded tables (/tabelle/<slug>), described in
  TableScreens: grid with active/disabled filter, detail with errors and
  warnings, disable instead of delete, CSV export and all-or-nothing import,
  "update from base tables"; index page and menu entries
- company profile in tabs with the fiscal pack checks, fiscal years with
  month lock/unlock, exchange rates with manual rates
- PaymentTermService: installments saved together, percentages must sum to 100
- a new profile proposes the registered company name instead of its code
- seed verb in Khaemhat.Web and scripts/seed.sh --base [--company CODE],
  shipped in the image under /app/scripts
- ADR 0026 (base data only adds missing codes; ISTAT and ABI/CAB lists come
  as separate datasets), wiki, CHANGELOG, STATUS

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 23:15:09 +01:00
1 parent 34e196424f
commit 9bcf8c1082
33 files changed
+3272 -43

No files matched your search

@@ -0,0 +1,159 @@
using Khaemhat.Application.Audit;
using Khaemhat.Application.Security;
using Khaemhat.Core.Caching;
using Khaemhat.Core.Tables;
using Khaemhat.Data;
using Khaemhat.Data.Querying;
namespace Khaemhat.Application.Tables;
/// <summary>
/// The installments of a payment term (SPEC §11.1): days from the document date, end of month, fixed day and share of
/// the amount. They are saved all together, because only the whole set has to add up to 100%.
/// </summary>
public sealed class PaymentTermService
{
/// <summary>The installments do not add up to 100%.</summary>
public const string PercentTotal = "tables.payment-term.percent-total";
/// <summary>A payment term needs at least one installment.</summary>
public const string LinesRequired = "tables.payment-term.lines-required";
/// <summary>The days are negative or beyond a year and a half.</summary>
public const string DaysRange = "tables.payment-term.days";
/// <summary>The fixed day is not a day of the month.</summary>
public const string FixedDayRange = "tables.payment-term.fixed-day";
/// <summary>The field the errors of the installments are reported on.</summary>
public const string LinesField = "Lines";
private const int MaxDays = 540;
private readonly IUnitOfWorkFactory _factory;
private readonly PermissionService _permissions;
private readonly AuditTrail _audit;
private readonly ICacheProvider _cache;
/// <summary>Creates the service.</summary>
/// <param name="factory">The units of work.</param>
/// <param name="permissions">The permissions.</param>
/// <param name="audit">The audit trail.</param>
/// <param name="cache">The cache of the tables.</param>
public PaymentTermService(IUnitOfWorkFactory factory, PermissionService permissions, AuditTrail audit, ICacheProvider cache)
{
ArgumentNullException.ThrowIfNull(factory);
ArgumentNullException.ThrowIfNull(permissions);
ArgumentNullException.ThrowIfNull(audit);
ArgumentNullException.ThrowIfNull(cache);
_factory = factory;
_permissions = permissions;
_audit = audit;
_cache = cache;
}
/// <summary>The installments of a payment term, in order.</summary>
/// <param name="actor">Who reads.</param>
/// <param name="companyCode">The company.</param>
/// <param name="paymentTermId">The payment term.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The installments.</returns>
public async Task<IReadOnlyList<PaymentTermLine>> LinesAsync(Actor actor, string companyCode, long paymentTermId, CancellationToken cancellationToken = default)
{
var company = await CompanyGuard.DemandAsync(_permissions, actor, companyCode, TableService.ReadPermission, cancellationToken).ConfigureAwait(false);
await using var uow = await _factory.CreateAsync(DatabaseScope.Company(company), cancellationToken).ConfigureAwait(false);
return await ReadAsync(uow, paymentTermId, cancellationToken).ConfigureAwait(false);
}
/// <summary>Checks a set of installments.</summary>
/// <param name="lines">The installments.</param>
/// <returns>The errors, on <see cref="LinesField"/>.</returns>
public static IReadOnlyList<TableProblem> Validate(IReadOnlyList<PaymentTermLine> lines)
{
ArgumentNullException.ThrowIfNull(lines);
var problems = new List<TableProblem>();
if (lines.Count == 0)
{
problems.Add(new(LinesField, LinesRequired));
return problems;
}
if (lines.Any(l => l.Days < 0 || l.Days > MaxDays))
{
problems.Add(new(LinesField, DaysRange));
}
if (lines.Any(l => l.FixedDay is < 1 or > 31))
{
problems.Add(new(LinesField, FixedDayRange));
}
if (lines.Any(l => l.Percent <= 0) || lines.Sum(l => l.Percent) != 100m)
{
problems.Add(new(LinesField, PercentTotal));
}
return problems;
}
/// <summary>Replaces the installments of a payment term, numbering them 1, 2, 3 in the given order.</summary>
/// <param name="actor">Who saves.</param>
/// <param name="companyCode">The company.</param>
/// <param name="paymentTermId">The payment term.</param>
/// <param name="lines">The installments.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The saved installments.</returns>
/// <exception cref="ValidationFailedException">The installments are not valid (<c>Lines:code</c>).</exception>
public async Task<IReadOnlyList<PaymentTermLine>> SaveLinesAsync(Actor actor, string companyCode, long paymentTermId, IReadOnlyList<PaymentTermLine> lines,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(lines);
var company = await CompanyGuard.DemandAsync(_permissions, actor, companyCode, TableService.WritePermission, cancellationToken).ConfigureAwait(false);
var problems = Validate(lines);
if (problems.Count > 0)
{
throw new ValidationFailedException([.. problems.Select(p => p.Field + ":" + p.Code)]);
}
IReadOnlyList<PaymentTermLine> saved;
await using (var uow = await _factory.CreateAsync(DatabaseScope.Company(company), cancellationToken).ConfigureAwait(false))
{
await uow.BeginAsync(cancellationToken).ConfigureAwait(false);
_ = await uow.Repository<PaymentTerm, long>().GetAsync(paymentTermId, cancellationToken).ConfigureAwait(false)
?? throw new KeyNotFoundException(TableService.NotFound);
var repository = uow.Repository<PaymentTermLine, long>();
foreach (var old in await ReadAsync(uow, paymentTermId, cancellationToken).ConfigureAwait(false))
{
await repository.DeleteAsync(old.Id, cancellationToken).ConfigureAwait(false);
await _audit.RecordAsync(uow, AuditAction.Delete, old.Id, old, null, actor.UserName, cancellationToken).ConfigureAwait(false);
}
var number = 0;
foreach (var line in lines)
{
var row = new PaymentTermLine
{
PaymentTermId = paymentTermId,
Number = ++number,
Days = line.Days,
EndOfMonth = line.EndOfMonth,
FixedDay = line.FixedDay,
Percent = line.Percent,
};
var id = await repository.InsertAsync(row, cancellationToken).ConfigureAwait(false);
await _audit.RecordAsync(uow, AuditAction.Insert, id, null, row, actor.UserName, cancellationToken).ConfigureAwait(false);
}
saved = await ReadAsync(uow, paymentTermId, cancellationToken).ConfigureAwait(false);
await uow.CommitAsync(cancellationToken).ConfigureAwait(false);
}
await _cache.PublishInvalidationAsync(CacheKeys.Prefix(company, "tables"), cancellationToken).ConfigureAwait(false);
return saved;
}
private static async Task<IReadOnlyList<PaymentTermLine>> ReadAsync(IUnitOfWork uow, long paymentTermId, CancellationToken cancellationToken) =>
(await uow.Repository<PaymentTermLine, long>()
.ListAsync(Query.For<PaymentTermLine>().Where(x => x.PaymentTermId == paymentTermId).OrderBy(x => x.Number), cancellationToken)
.ConfigureAwait(false)).Items;
}