feat: stock take, reorder proposal and warehouse rebuild (#60)

A physical stock take freezes the book quantity and cost of a warehouse at a
date, takes counts typed or read from a scanner file, and posts the gains and
losses as INVP/INVN movements at that date. The reorder proposal lists the
items whose available and ordered quantity no longer covers the reorder point
and turns the accepted lines into draft supplier orders, one per supplier.
The warehouse can be rebuilt from its documents on demand and every Sunday
night, as a safety net after imports or corrections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 21:35:34 +01:00
1 parent f8b72f4d17
commit eb301fa570
13 files changed
+975 -2

No files matched your search

@@ -314,6 +314,37 @@ public sealed class InventoryService
return [.. rows.OrderByDescending(r => r.Level).ThenBy(r => r.Available - (r.MinimumStock ?? r.ReorderPoint ?? 0m)).ThenBy(r => r.Code, StringComparer.Ordinal).Take(limit)];
}
/// <summary>
/// Rebuilds the warehouse of the company from its documents (SPEC §11.6): every movement is written again and every
/// item computed again, in one transaction.
/// </summary>
/// <param name="actor">Who rebuilds: needs <c>inventory.entry.post</c>.</param>
/// <param name="companyCode">The company.</param>
/// <param name="progress">How far it is.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The documents posted and the items computed.</returns>
public async Task<(int Documents, int Items)> RebuildAsync(Actor actor, string companyCode, IProgress<RebuildProgress>? progress = null, CancellationToken cancellationToken = default)
{
var company = await CompanyGuard.DemandAsync(_permissions, actor, companyCode, PostPermission, cancellationToken).ConfigureAwait(false);
return await RebuildCompanyAsync(_factory, company, progress, cancellationToken).ConfigureAwait(false);
}
/// <summary>Rebuilds the warehouse of a company, without checks (the scheduled job).</summary>
/// <param name="factory">The units of work.</param>
/// <param name="company">The company.</param>
/// <param name="progress">How far it is.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The documents posted and the items computed.</returns>
internal static async Task<(int Documents, int Items)> RebuildCompanyAsync(
IUnitOfWorkFactory factory, string company, IProgress<RebuildProgress>? progress, CancellationToken cancellationToken)
{
await using var uow = await factory.CreateAsync(DatabaseScope.Company(company), cancellationToken).ConfigureAwait(false);
await uow.BeginAsync(cancellationToken).ConfigureAwait(false);
var result = await InventoryPoster.RebuildAsync(uow, progress, cancellationToken).ConfigureAwait(false);
await uow.CommitAsync(cancellationToken).ConfigureAwait(false);
return result;
}
/// <summary>
/// The level of an item: out when nothing is available (or less than nothing), under stock below the minimum,
/// to reorder when what is available and on order no longer covers the reorder point.