Files
Khaemhat/tests/Khaemhat.Web.Tests/Documents/DemoSalesTests.cs
T
Alby96andClaude Opus 5.5 826042f828 feat: sales screens, deferred invoicing, printing and mail of documents (#49 #50 #51 #52)
Quotations, orders, delivery notes, invoices and credit notes get their radars and
one document screen (lines, header, transport, links, attachments) with the
transformations of the engine, so a quotation reaches the invoice without
retyping. The deferred invoicing groups the open delivery notes of a period by
customer, payment and destination into one invoice each.

Every document prints from one model: an HTML preview on screen and a PDF drawn
by the in-house writer, kept among the attachments as a new version. "Invia per
e-mail" proposes recipient, subject and text from the templates, attaches the
PDF and hands it to a background queue that talks SMTP itself (no external
library); the outcome goes to the log, section E-mail/PEC.

The demo company now has a year of sales in every state, made through the
engine, and run-dev.sh no longer writes to a stray B:\b folder under Git Bash.

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

77 lines
4.3 KiB
C#

using Khaemhat.Application.Documents;
using Khaemhat.Application.Instance;
using Khaemhat.Application.Querying;
using Khaemhat.Application.Security;
using Khaemhat.Application.Seeding;
using Khaemhat.Application.Tables;
using Khaemhat.Core.Documents;
using Khaemhat.Core.Security;
using Khaemhat.Web.Tests.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Khaemhat.Web.Tests.Documents;
/// <summary>The demo sales (SPEC §14, issue #53): every kind of sales document, in every state, made through the engine.</summary>
[TestClass]
public sealed class DemoSalesTests
{
private static readonly Actor Root = new(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null);
[TestMethod]
public async Task DemoSeed_MakesTheSalesOfTheYear_Once()
{
var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None);
await using (app)
{
await app.Services.GetRequiredService<SeedRunner>().RunAsync(company, SeedKind.Demo);
var seeder = app.Services.GetRequiredService<DemoSalesSeeder>();
var context = new CompanySeedContext(IsNew: false, Base: false, Demo: true);
await seeder.SeedAsync(company, context);
var editor = app.Services.GetRequiredService<DocumentEditor>();
var all = await AllAsync(editor, company);
Assert.IsTrue(all.All(r => r.Date.Year == DateTime.Now.Year && r.Date <= DateOnly.FromDateTime(DateTime.Now)), "Dates of this year, never in the future.");
int Count(DocumentKind kind, DocumentStatus status) => all.Count(r => r.Kind == kind && r.Status == status);
Assert.AreEqual(2, Count(DocumentKind.Quotation, DocumentStatus.Fulfilled), "Two quotations became orders.");
Assert.AreEqual(1, Count(DocumentKind.Quotation, DocumentStatus.Closed), "One was refused.");
Assert.AreEqual(1, Count(DocumentKind.Quotation, DocumentStatus.Draft));
Assert.AreEqual(1, Count(DocumentKind.SalesOrder, DocumentStatus.Confirmed), "An order still to deliver.");
Assert.AreEqual(3, Count(DocumentKind.SalesOrder, DocumentStatus.Fulfilled));
Assert.AreEqual(2, Count(DocumentKind.DeliveryNote, DocumentStatus.Fulfilled), "Two delivery notes already invoiced.");
Assert.AreEqual(3, Count(DocumentKind.DeliveryNote, DocumentStatus.Confirmed), "Three for the deferred invoicing.");
Assert.AreEqual(4, Count(DocumentKind.ImmediateInvoice, DocumentStatus.Issued));
Assert.AreEqual(1, Count(DocumentKind.ImmediateInvoice, DocumentStatus.Draft));
Assert.AreEqual(2, Count(DocumentKind.DeferredInvoice, DocumentStatus.Issued));
Assert.AreEqual(1, Count(DocumentKind.CreditNote, DocumentStatus.Issued));
Assert.AreEqual(1, all.Count(r => r.Kind == DocumentKind.ProForma));
Assert.IsTrue(all.Where(r => r.Status != DocumentStatus.Draft).All(r => r.DocumentNumber is not null && r.Total > 0m));
// The issued invoices and the note share one numerator, numbered in date order.
var numbered = all.Where(r => r.Kind is DocumentKind.ImmediateInvoice or DocumentKind.DeferredInvoice or DocumentKind.CreditNote && r.Status == DocumentStatus.Issued)
.OrderBy(r => r.Date).ThenBy(r => r.Id).Select(r => r.DocumentNumber).ToList();
Assert.HasCount(7, numbered);
CollectionAssert.AllItemsAreUnique(numbered);
var profile = await app.Services.GetRequiredService<CompanyProfileService>().GetAsync(Root, company);
Assert.AreEqual(DemoSalesSeeder.CompanyVatNumber, profile.VatNumber);
// Run again, nothing changes.
await seeder.SeedAsync(company, context);
Assert.HasCount(all.Count, await AllAsync(editor, company));
await app.StopAsync();
}
}
private static async Task<List<DocumentRow>> AllAsync(DocumentEditor editor, string company)
{
var all = new List<DocumentRow>();
foreach (var family in new[] { DocumentFamily.SaleQuotation, DocumentFamily.SaleOrder, DocumentFamily.SaleDocument })
{
all.AddRange((await editor.ListAsync(Root, company, family, [.. Enum.GetValues<DocumentKind>()], new ListQuery { Limit = 200 })).Items);
}
return all;
}
}