Files
Khaemhat/tests/Khaemhat.Application.Tests/PriceBookTests.cs
T
Alby96andClaude Opus 5.5 3dbbaba86e feat: items and price lists with dated prices, derived lists and demo catalogue (#42)
Items get their own archive and record (mockup 017): codes from the
per-category mask, EAN and NC8 checks, alternative units and bar codes,
suppliers with the usual one, substitutes, notes and history. Only goods
can be stocked; stock itself comes with the warehouse in M6. Costs are
hidden from users without items.cost.read and kept as they were when
those users save. A used item is disabled instead of deleted.

Prices live in one item_prices table, either for a price list or as a
customer's net price, each valid from a date, so documents keep the price
of their day. Derived lists compute a price from their base list with
markup and rounding when they have none of their own. The price grid
(mockup 018) edits every list at one validity date, keeps changes pending
and highlighted until saved, and computes mass updates as a preview.
PriceListService.QuoteAsync gives documents (M5) the price search order
net, list, base, item price with its source.

Parties get the "Articoli e codici" tab: the codes a customer or supplier
uses for our items, found by the item search in that party's documents.

Demo data adds 120 items of a plumbing wholesaler, the PUBB and RIV lists,
last year's prices, suppliers, units, codes and a customer's net prices;
seed --demo [--scale N] loads them from the command line and repeats the
catalogue for performance tests.

ADR 0030 records the decisions; wiki, data model, CHANGELOG and STATUS
are updated.

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

86 lines
4.9 KiB
C#

using Khaemhat.Application.Items;
using Khaemhat.Core.Items;
using Khaemhat.Core.Parties;
using Khaemhat.Core.Tables;
namespace Khaemhat.Application.Tests;
/// <summary>The search of the price of a line (SPEC §11.3, ADR 0030): net price, list, base list, item; validity and derived lists.</summary>
[TestClass]
public sealed class PriceBookTests
{
private static readonly DateOnly Jan = new(2026, 1, 1);
private static readonly DateOnly Jun = new(2026, 6, 1);
private static readonly PriceList Base = new() { Id = 1, Code = "BASE", Description = "Base", IsBase = true };
private static readonly PriceList Public = new() { Id = 2, Code = "PUBB", Description = "Pubblico", BasePriceListId = 1, MarkupPercent = 45m, RoundingStep = 0.10m };
private static readonly PriceList Resellers = new() { Id = 3, Code = "RIV", Description = "Rivenditori", BasePriceListId = 1, MarkupPercent = -10m };
private static readonly PriceList Expired = new() { Id = 4, Code = "OLD", Description = "Scaduto", ValidTo = new DateOnly(2025, 12, 31) };
private static readonly Item Valve = new() { Id = 10, Code = "VALV-0001", Description = "Valvola", Price = 30m };
private static readonly Item Service = new() { Id = 11, Code = "SERV-0001", Description = "Manodopera", Price = 38m };
private static PriceBook Book() => new(
[Base, Public, Resellers, Expired],
[
new ItemPrice { ItemId = 10, PriceListId = 1, Price = 30.70m, ValidFrom = new DateOnly(2025, 1, 1) },
new ItemPrice { ItemId = 10, PriceListId = 1, Price = 32m, ValidFrom = Jan },
new ItemPrice { ItemId = 10, PriceListId = 1, Price = 34m, ValidFrom = Jun },
new ItemPrice { ItemId = 10, PriceListId = 3, Price = 27.50m, ValidFrom = Jan },
new ItemPrice { ItemId = 10, PriceListId = 4, Price = 1m },
new ItemPrice { ItemId = 10, PartyId = 99, Price = 30.40m, ValidFrom = Jan },
]);
[TestMethod]
public void ListPrice_IsTheLatestStartNotAfterTheDate()
{
var book = Book();
Assert.AreEqual(30.70m, book.ListPrice(10, 1, new DateOnly(2025, 6, 1)).Price);
Assert.AreEqual(32m, book.ListPrice(10, 1, new DateOnly(2026, 5, 31)).Price);
Assert.AreEqual(new PriceCell(34m, false, Jun), book.ListPrice(10, 1, new DateOnly(2026, 7, 1)));
Assert.IsNull(book.ListPrice(10, 1, new DateOnly(2024, 12, 31)).Price, "Nothing before the first price.");
}
[TestMethod]
public void DerivedList_TakesTheBaseWithMarkupAndRounding_UnlessItHasItsOwn()
{
var book = Book();
Assert.AreEqual(new PriceCell(46.40m, true, Jan), book.ListPrice(10, 2, new DateOnly(2026, 3, 1)), "32 + 45% = 46.40.");
Assert.AreEqual(49.30m, book.ListPrice(10, 2, Jun).Price, "34 + 45% = 49.30.");
Assert.AreEqual(new PriceCell(27.50m, false, Jan), book.ListPrice(10, 3, Jun), "Its own price wins over the derived one.");
Assert.AreEqual(PriceCell.Empty, book.ListPrice(11, 2, Jun), "No base price, nothing to derive.");
}
[TestMethod]
public void Quote_SearchesNet_ThenList_ThenBase_ThenItem()
{
var book = Book();
Assert.AreEqual(new PriceQuote(30.40m, PriceSource.PartyNet, null, false, Jan), book.Quote(Valve, 99, 2, Jun));
Assert.AreEqual(PriceSource.PartyPriceList, book.Quote(Valve, 5, 3, Jun).Source);
Assert.AreEqual(27.50m, book.Quote(Valve, 5, 3, Jun).Price);
Assert.AreEqual(new PriceQuote(34m, PriceSource.BasePriceList, 1, false, Jun), book.Quote(Valve, 5, 4, Jun), "An expired list is skipped.");
Assert.AreEqual(new PriceQuote(38m, PriceSource.Item, null, false, null), book.Quote(Service, null, null, Jun));
Assert.AreEqual(PriceQuote.None, book.Quote(new Item { Id = 12 }, null, null, Jun));
Assert.AreEqual(Base, book.Base);
}
[TestMethod]
public void BaseOf_IsTheMarkedList_ElseTheCodeBase()
{
Assert.AreEqual(2, PriceBook.BaseOf([new PriceList { Id = 1, Code = "BASE" }, new PriceList { Id = 2, Code = "A", IsBase = true }])!.Id);
Assert.AreEqual(1, PriceBook.BaseOf([new PriceList { Id = 1, Code = "BASE" }, new PriceList { Id = 2, Code = "A" }])!.Id);
Assert.IsNull(PriceBook.BaseOf([new PriceList { Id = 1, Code = "BASE", Disabled = true }]));
}
[TestMethod]
public void Discounts_ComeFromTheTerms_ButNotOnANetPrice()
{
var terms = new PartySalesTerms { Discount1 = 10m, Discount2 = 5m };
var fromList = new PriceQuote(100m, PriceSource.BasePriceList, 1, false, Jan);
var discounts = PriceBook.Discounts(fromList, terms);
Assert.AreEqual("10+5", discounts.Formula);
Assert.AreEqual(14.5m, discounts.Equivalent);
Assert.AreEqual(DiscountQuote.None, PriceBook.Discounts(fromList with { Source = PriceSource.PartyNet }, terms));
Assert.AreEqual(DiscountQuote.None, PriceBook.Discounts(fromList, new PartySalesTerms()));
Assert.AreEqual(DiscountQuote.None, PriceBook.Discounts(fromList, null));
}
}