Files
Khaemhat/tests/Khaemhat.Web.Tests/Inventory/DemoPurchasesTests.cs
T
Alby96andClaude Opus 5.5 ced689b297 feat: warehouse and purchase screens, e-invoice import, demo purchases (#55 #56 #57 #58 #60)
Stock, stock take and reorder screens under /magazzino, with valuation, rebuild and the
movements of an item; the understock dashboard widget. Supplier e-invoices (XML, p7m, ZIP)
become draft purchase invoices with supplier, items and tax codes matched, the file attached
and duplicates refused; the TD code mapping stays in the Italian fiscal pack. Purchase
invoices show price differences against the order and the self-invoice still to issue,
with a pending list on the self-invoice screen. The opening stock import now writes an
opening movement. The demo company gets purchases, an opening stock, a pending
self-invoice and a stock take being counted.

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

75 lines
4.0 KiB
C#

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;
/// <summary>
/// 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.
/// </summary>
[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<SeedRunner>().RunAsync(company, SeedKind.Demo);
var context = new CompanySeedContext(IsNew: false, Base: false, Demo: true);
await app.Services.GetRequiredService<DemoSalesSeeder>().SeedAsync(company, context);
var seeder = app.Services.GetRequiredService<DemoPurchasesSeeder>();
await seeder.SeedAsync(company, context);
var editor = app.Services.GetRequiredService<DocumentEditor>();
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<PurchaseService>().PendingSelfInvoicesAsync(Root, company);
Assert.HasCount(1, pending);
var inventory = app.Services.GetRequiredService<InventoryService>();
Assert.IsNotEmpty(await inventory.StockAsync(Root, company, new StockFilter()));
Assert.IsNotEmpty(await app.Services.GetRequiredService<ReorderService>().ProposalsAsync(Root, company));
var take = (await app.Services.GetRequiredService<StockTakeService>().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<List<DocumentRow>> AllAsync(DocumentEditor editor, string company)
{
var all = new List<DocumentRow>();
foreach (var family in new[] { DocumentFamily.PurchaseOrder, DocumentFamily.PurchaseDocument, DocumentFamily.InventoryDocument })
{
all.AddRange((await editor.ListAsync(Root, company, family, [.. Enum.GetValues<DocumentKind>()], new ListQuery { Limit = 200 })).Items);
}
return all;
}
}