One engine for sales, purchases and warehouse documents (ADR 0032). Each document type now carries a kind that decides its family of tables, life cycle, effects, accepted lines, the kinds it fulfils and its permissions, so company-defined types work without code. - Six families of header and line tables plus document_links; document_types gets document_kind (seeded types filled by the migration and the Italian template), document_numerators gets last_date. - DocumentStateMachine with the order, invoice and simple life cycles and the actions each state admits; issued invoices are immutable and are cancelled with a reason only before the file reaches the exchange system. - DocumentNumberingService numbers at confirmation inside the transaction under the numerator lock (advisory lock on PostgreSQL, write lock on SQLite): no duplicates, a rollback gives the number back, out-of-sequence dates refused on gap-free numerators, the new year's numerator created on first use, gap report. - DocumentEngine, the generic DocumentService<THeader, TLine> and DocumentCopyService: save, confirm or issue, cancel, close, reopen, delete, duplicate and transform; links are recorded at confirmation and removed at cancellation, and FulfillmentCalculator recomputes quantities and states. - Tests: rules in Core, concurrent numbering on SQLite and PostgreSQL (data tests, run on PostgreSQL 18 locally), order to delivery notes to deferred invoice on a real instance. Wiki (Vendite, Tabelle azienda), CHANGELOG, STATUS, DATA-MODEL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
4.1 KiB
C#
93 lines
4.1 KiB
C#
using Khaemhat.Application.Documents;
|
|
using Khaemhat.Application.Security;
|
|
using Khaemhat.Core.Tables;
|
|
using Khaemhat.Data.Migrations;
|
|
using Khaemhat.Data.Querying;
|
|
using Khaemhat.Data.Tests.Support;
|
|
|
|
namespace Khaemhat.Data.Tests;
|
|
|
|
/// <summary>Document numbering on both engines (issue #47): concurrent confirmations never share a number.</summary>
|
|
[TestClass]
|
|
public sealed class DocumentNumberingTests
|
|
{
|
|
private static readonly DateOnly Day = new(2026, 11, 12);
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task ConcurrentConfirmations_GetEveryNumberOnce_AndRollbacksGiveTheNumberBack(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
var id = await NumeratorAsync(db);
|
|
const int Workers = 24;
|
|
|
|
var results = await Task.WhenAll(Enumerable.Range(0, Workers).Select(i => Task.Run(async () =>
|
|
{
|
|
await using var uow = await db.OpenAsync();
|
|
await uow.BeginAsync();
|
|
var number = await DocumentNumberingService.NextAsync(uow, id, Day);
|
|
await Task.Yield();
|
|
if (i % 4 == 0)
|
|
{
|
|
await uow.RollbackAsync();
|
|
return (long?)null;
|
|
}
|
|
|
|
await uow.CommitAsync();
|
|
return number.Number;
|
|
})));
|
|
|
|
var given = results.OfType<long>().Order().ToArray();
|
|
CollectionAssert.AreEqual(Enumerable.Range(1, given.Length).Select(n => (long)n).ToArray(), given, "Each number once, none skipped.");
|
|
await using var check = await db.OpenAsync();
|
|
var numerator = await check.Repository<DocumentNumerator, long>().GetAsync(id);
|
|
Assert.AreEqual((long)given.Length, numerator!.LastNumber);
|
|
Assert.AreEqual(Day, numerator.LastDate);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task Numbering_RefusesDatesBeforeTheLast_AndStartsANewYearFromOne(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
var id = await NumeratorAsync(db);
|
|
await using var uow = await db.OpenAsync();
|
|
|
|
await uow.BeginAsync();
|
|
var first = await DocumentNumberingService.NextAsync(uow, id, Day);
|
|
Assert.AreEqual("FV-2026/0001", first.Text);
|
|
var refused = await Assert.ThrowsExactlyAsync<ValidationFailedException>(() => DocumentNumberingService.NextAsync(uow, id, Day.AddDays(-1)));
|
|
Assert.AreEqual(DocumentNumberingService.DateBeforeLast, refused.Errors.Single());
|
|
Assert.AreEqual("FV-2027/0001", await DocumentNumberingService.PreviewAsync(uow, id, new DateOnly(2027, 1, 2)));
|
|
|
|
// The first document of a new year creates the numerator of that year with the same rules.
|
|
var next = await DocumentNumberingService.NextAsync(uow, id, new DateOnly(2027, 1, 2));
|
|
Assert.AreEqual(1L, next.Number);
|
|
Assert.AreEqual("FV-2027/0001", next.Text);
|
|
Assert.AreNotEqual(id, next.Numerator.Id);
|
|
Assert.AreEqual(2, (await uow.Repository<DocumentNumerator, long>().ListAsync(Query.For<DocumentNumerator>().Where(n => n.Code == "FAT"))).Items.Count);
|
|
Assert.AreEqual(2L, (await DocumentNumberingService.NextAsync(uow, id, Day.AddDays(1))).Number, "2026 goes on by itself.");
|
|
await uow.CommitAsync();
|
|
|
|
// No document carries the number given in 2027: the check lists it as missing.
|
|
CollectionAssert.AreEqual(new long[] { 1 }, (await DocumentNumberingService.FindGapsAsync(uow, next.Numerator.Id)).ToArray());
|
|
}
|
|
|
|
private static async Task<long> NumeratorAsync(TestDatabase db)
|
|
{
|
|
await new MigrationRunner(db.Context).RunAsync(db.Target, MigrationCatalog.ForCompany());
|
|
await using var setup = await db.OpenAsync();
|
|
return await setup.Repository<DocumentNumerator, long>().InsertAsync(new DocumentNumerator
|
|
{
|
|
Code = "FAT",
|
|
Description = "Fatture",
|
|
Kind = NumeratorKind.SalesInvoice,
|
|
Year = 2026,
|
|
Prefix = "FV",
|
|
Format = "{prefix}-{year}/{number:D4}",
|
|
});
|
|
}
|
|
}
|