using Khaemhat.Application.Items; using Khaemhat.Core.Items; using Khaemhat.Core.Parties; using Khaemhat.Core.Tables; namespace Khaemhat.Application.Tests; /// The search of the price of a line (SPEC ยง11.3, ADR 0030): net price, list, base list, item; validity and derived lists. [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)); } }