using Khaemhat.Core.Documents;
using Khaemhat.Core.Inventory;
using Khaemhat.Core.Tables;
namespace Khaemhat.Core.Tests.Inventory;
/// Which documents move the stock and how the weighted average values it (issue #59, ADR 0035).
[TestClass]
public sealed class StockRulesTests
{
private static readonly StockReason Load = new(StockEffect.Load, AffectsValue: true, IsTransfer: false);
private static readonly StockReason Unload = new(StockEffect.Unload, AffectsValue: true, IsTransfer: false);
private static readonly StockReason Transfer = new(StockEffect.None, AffectsValue: false, IsTransfer: true);
[TestMethod]
public void Movements_FollowTheReason_AndOnlyPurchasesCarryTheirCost()
{
var line = new StockLineInput(7, 100, 5m, 1, 12.5m);
var receipt = StockEffectMatrix.Movements(DocumentKind.PurchaseDeliveryNote, Load, null, [line]).Single();
Assert.AreEqual(5m, receipt.Quantity);
Assert.IsTrue(receipt.Valued);
Assert.AreEqual(12.5m, receipt.UnitCost);
var delivery = StockEffectMatrix.Movements(DocumentKind.DeliveryNote, Unload, null, [line]).Single();
Assert.AreEqual(-5m, delivery.Quantity);
Assert.IsFalse(delivery.Valued, "A sale is valued at the average, not at its price.");
// A customer return loads, at the average.
var back = StockEffectMatrix.Movements(DocumentKind.CreditNote, Load, null, [line]).Single();
Assert.AreEqual(5m, back.Quantity);
Assert.IsFalse(back.Valued);
// Orders and quotations never move the stock; without a reason nothing moves.
Assert.IsEmpty(StockEffectMatrix.Movements(DocumentKind.PurchaseOrder, Load, null, [line]));
Assert.IsEmpty(StockEffectMatrix.Movements(DocumentKind.Quotation, Unload, null, [line]));
Assert.IsEmpty(StockEffectMatrix.Movements(DocumentKind.ImmediateInvoice, null, null, [line]));
Assert.IsTrue(StockEffectMatrix.Commits(DocumentKind.SalesOrder));
Assert.IsTrue(StockEffectMatrix.Orders(DocumentKind.PurchaseOrder));
Assert.IsTrue(StockEffectMatrix.IsOpenOrder(DocumentStatus.PartiallyFulfilled));
Assert.IsFalse(StockEffectMatrix.IsOpenOrder(DocumentStatus.Closed));
}
[TestMethod]
public void Transfer_UnloadsTheOriginAndLoadsTheDestination_WithoutValue()
{
var moves = StockEffectMatrix.Movements(DocumentKind.InventoryEntry, Transfer, 2, [new StockLineInput(1, 100, 3m, 1, 10m)]);
Assert.HasCount(2, moves);
Assert.AreEqual((1L, -3m), (moves[0].WarehouseId, moves[0].Quantity));
Assert.AreEqual((2L, 3m), (moves[1].WarehouseId, moves[1].Quantity));
Assert.IsFalse(moves.Any(m => m.Valued));
Assert.ThrowsExactly(() => StockEffectMatrix.Movements(DocumentKind.InventoryEntry, Transfer, null, [new StockLineInput(1, 100, 3m, 1, null)]));
Assert.ThrowsExactly(() => StockEffectMatrix.Movements(DocumentKind.InventoryEntry, Transfer, 1, [new StockLineInput(1, 100, 3m, 1, null)]));
}
[TestMethod]
public void WeightedAverage_MovesWithEveryValuedLoad_AndValuesTheRestAtItsMoment()
{
var day = new DateOnly(2026, 3, 1);
var history = StockCostCalculator.Run(
[
new CostedMovement(1, day, 1, 10m, true, 10m),
new CostedMovement(2, day.AddDays(1), 1, -4m, false, 0m),
new CostedMovement(3, day.AddDays(2), 2, 6m, true, 13m),
new CostedMovement(4, day.AddDays(3), 1, -2m, false, 0m),
]);
// 6 left at 10, then 6 more at 13: (60 + 78) / 12 = 11.5.
Assert.AreEqual(10m, history.UnitCosts[2]);
Assert.AreEqual(11.5m, history.UnitCosts[4]);
Assert.AreEqual(11.5m, history.AverageCost);
Assert.AreEqual(13m, history.LastCost);
Assert.AreEqual(10m, history.OnHand);
Assert.AreEqual(4m, history.Warehouses[1].OnHand);
Assert.AreEqual(6m, history.Warehouses[2].OnHand);
Assert.AreEqual(day.AddDays(3), history.Warehouses[1].LastUnload);
Assert.AreEqual(day, history.Warehouses[1].LastLoad);
Assert.AreEqual(-23m, StockCostCalculator.Value(-2m, 11.5m));
}
[TestMethod]
public void WeightedAverage_RestartsFromTheLoad_WhenTheStockWasNegative()
{
var day = new DateOnly(2026, 3, 1);
var history = StockCostCalculator.Run(
[
new CostedMovement(1, day, 1, -3m, false, 0m),
new CostedMovement(2, day, 1, 5m, true, 8m),
],
initialCost: 7m);
Assert.AreEqual(7m, history.UnitCosts[1], "Before any purchase the standard cost values the unload.");
Assert.AreEqual(8m, history.AverageCost);
Assert.AreEqual(2m, history.OnHand);
}
[TestMethod]
public void Reason_ComesFromTheRowOfTheTable()
{
var reason = StockReason.Of(new InventoryReason { Code = "TRA", Description = "Trasferimento", Effect = StockEffect.None, AffectsValue = false, IsTransfer = true });
Assert.AreEqual(Transfer, reason);
}
}