using Khaemhat.Application.Documents; using Khaemhat.Application.Instance; using Khaemhat.Application.Inventory; using Khaemhat.Application.Purchases; using Khaemhat.Application.Querying; using Khaemhat.Application.Security; using Khaemhat.Application.Seeding; using Khaemhat.Core.Documents; using Khaemhat.Core.Inventory; using Khaemhat.Core.Security; using Khaemhat.Web.Tests.Hosting; using Microsoft.Extensions.DependencyInjection; namespace Khaemhat.Web.Tests.Inventory; /// /// The demo purchases and warehouse (SPEC ยง14, issue #61): the opening stock, the orders, the receipts and the invoices /// made through the engine, a self-invoice still to issue, items under their minimum and a stock take being counted. /// [TestClass] public sealed class DemoPurchasesTests { private static readonly Actor Root = new(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null); [TestMethod] public async Task DemoSeed_MakesPurchasesAndStock_Once() { var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None); await using (app) { await app.Services.GetRequiredService().RunAsync(company, SeedKind.Demo); var context = new CompanySeedContext(IsNew: false, Base: false, Demo: true); await app.Services.GetRequiredService().SeedAsync(company, context); var seeder = app.Services.GetRequiredService(); await seeder.SeedAsync(company, context); var editor = app.Services.GetRequiredService(); var all = await AllAsync(editor, company); int Count(DocumentKind kind, DocumentStatus status) => all.Count(r => r.Kind == kind && r.Status == status); Assert.AreEqual(1, Count(DocumentKind.PurchaseOrder, DocumentStatus.Fulfilled), "One order received in full."); Assert.AreEqual(1, all.Count(r => r.Kind == DocumentKind.PurchaseOrder && r.Status is DocumentStatus.Confirmed or DocumentStatus.PartiallyFulfilled), "One order still partly on its way."); Assert.AreEqual(2, all.Count(r => r.Kind == DocumentKind.PurchaseDeliveryNote)); Assert.AreEqual(3, all.Count(r => r.Kind == DocumentKind.PurchaseInvoice && r.Status != DocumentStatus.Draft)); Assert.AreEqual(1, all.Count(r => r.Kind == DocumentKind.InventoryEntry)); 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."); // The German invoice waits for its self-invoice; the stock has items under the minimum to reorder. var pending = await app.Services.GetRequiredService().PendingSelfInvoicesAsync(Root, company); Assert.HasCount(1, pending); var inventory = app.Services.GetRequiredService(); Assert.IsNotEmpty(await inventory.StockAsync(Root, company, new StockFilter())); Assert.IsNotEmpty(await app.Services.GetRequiredService().ProposalsAsync(Root, company)); var take = (await app.Services.GetRequiredService().ListAsync(Root, company)).Single(); Assert.AreEqual(StockTakeStatus.Open, take.Status); // 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.PurchaseOrder, DocumentFamily.PurchaseDocument, DocumentFamily.InventoryDocument }) { all.AddRange((await editor.ListAsync(Root, company, family, [.. Enum.GetValues()], new ListQuery { Limit = 200 })).Items); } return all; } }