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; /// The demo sales (SPEC ยง14, issue #53): every kind of sales document, in every state, made through the engine. [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().RunAsync(company, SeedKind.Demo); var seeder = app.Services.GetRequiredService(); var context = new CompanySeedContext(IsNew: false, Base: false, Demo: true); await seeder.SeedAsync(company, context); var editor = app.Services.GetRequiredService(); 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().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> AllAsync(DocumentEditor editor, string company) { var all = new List(); foreach (var family in new[] { DocumentFamily.SaleQuotation, DocumentFamily.SaleOrder, DocumentFamily.SaleDocument }) { all.AddRange((await editor.ListAsync(Root, company, family, [.. Enum.GetValues()], new ListQuery { Limit = 200 })).Items); } return all; } }