using System; using System.Collections.Generic; using System.Linq; using Centron.BusinessLogic.Sales.CustomerAssets.Shared; using Centron.BusinessLogic.Sales.Customers; using Centron.BusinessLogic.Sales.Receipts.Internal; using Centron.BusinessLogic.WebServices.Accounts.SpecialPrices; using Centron.BusinessLogic.WebServices.Sales.CustomerAssets.Shared; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.Data.WebServices.Warehousing; using Centron.Interfaces; using Centron.Interfaces.BL; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Accounts.SpecialPrices; using CentronSoftware.Centron.WebServices.Entities.Warehousing; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.TicketTests.Ticket136129; public class Ticket136129Test : EndToEndTest { public Ticket136129Test(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public const int ArticleI3D = 12; public const int CustomerI3D = 10000; public override void Execute() { // In this ticket the problem was, that the globally configured default project-prices (SpecialAgreement in code) // for offers/orders/etc were not used in all cases // They were only used, when the project-price was for specific articles // Now it seems like, it should also work, if it's for all articles // // I talked with Steve Marek about it, and we decided on the following logic // 1. If the project-price is assigned to a customer, then it should only be used, if the article is in the project-price // 2. If the project-price is setup as a default one for offers/orders/etc, then it should always be used // // That's it, nothing more // There was a 3. level to it, all project-prices that had the "Is Global" flag set, but we don't want that anymore this.ExecuteMatrix( new[] { false, true }, new[] { ProjectPriceAssignment.None, ProjectPriceAssignment.Customer, ProjectPriceAssignment.GlobalDefault, ProjectPriceAssignment.IsGlobal }, (includeArticleInProjectPrice, assignment) => { bool expectProjectPriceToBeUsed = (includeArticleInProjectPrice, assignment) switch { (_, ProjectPriceAssignment.GlobalDefault) => true, (true, ProjectPriceAssignment.Customer) => true, _ => false, }; SetupProjectPrices(includeArticleInProjectPrice, assignment); this.WithSession(s => { var customer = s.GetBL().GetCustomer(CustomerI3D); var projectPrice = s.GetBL().GetSpecialAgreement(ArticleI3D, customer, CentronObjectKindNumeric.OfferClass); if (expectProjectPriceToBeUsed) { Assert.NotNull(projectPrice); } else { Assert.Null(projectPrice); } }); }); } private void SetupProjectPrices(bool includeArticleInProjectPrice, ProjectPriceAssignment assignment) { ClearAllProjectPrices(); var projectPrice = CreateProjectPrice(); TryAssignProjectPriceToCustomer(projectPrice); TryAssignProjectPriceGlobally(projectPrice); void ClearAllProjectPrices() { // Drop all those tables to reset everything about project-prices this.Sql($"DELETE FROM KundenSonderpreise"); this.Sql($"DELETE FROM SondervereinbarungsArtikel"); this.Sql($"DELETE FROM Sondervereinbarung"); var receiptSettings = this.WithSession(s => s.GetBL().GetReceiptSettings(this.GetLoggedInUser().User)); receiptSettings.OfferHasGlobalSpecialAgreement = false; receiptSettings.OfferGlobalSpecialAgreementI3D = 0; receiptSettings.OrderHasGlobalSpecialAgreement = false; receiptSettings.OrderGlobalSpecialAgreementI3D = 0; receiptSettings.DeliveryListHasGlobalSpecialAgreement = false; receiptSettings.DeliveryListGlobalSpecialAgreementI3D = 0; receiptSettings.InvoiceHasGlobalSpecialAgreement = false; receiptSettings.InvoiceGlobalSpecialAgreementI3D = 0; this.WithSession(s => s.GetBL().UpdateReceiptSettings(receiptSettings, this.GetLoggedInUser().User)).ThrowIfError(); } SpecialAgreementDTO CreateProjectPrice() { var newProjectPrice = this.WithSession(s => s.GetBL().CreateNew(this.GetLoggedInUser().User)); newProjectPrice.Text = "Test Project Price"; newProjectPrice.Number = "123"; newProjectPrice.IsProjectPrice = true; newProjectPrice.Articles = new List(); newProjectPrice.IsGlobal = assignment is ProjectPriceAssignment.IsGlobal; if (includeArticleInProjectPrice) { newProjectPrice.Articles.Add(new SpecialAgreementArticleDTO { Article = new ArticleDTO { I3D = ArticleI3D }, State = 1, }); } return this.WithSession(s => s.GetBL().SaveOrUpdate(newProjectPrice, this.GetLoggedInUser().User)).ThrowIfError(); } void TryAssignProjectPriceToCustomer(SpecialAgreementDTO projectPrice) { if (assignment is not ProjectPriceAssignment.Customer) return; var accountSpecialPrice = new AccountSpecialPriceDTO { ObjectKind = CentronObjectKindNumeric.CustomerClass, ObjectI3D = CustomerI3D, SpecialAgreementI3D = projectPrice.I3D, SpecialAgreementCaption = projectPrice.Text, }; this.WithSession(s => s.GetBL().SaveAccountSpecialPrice(this.GetLoggedInUser().User, accountSpecialPrice)); } void TryAssignProjectPriceGlobally(SpecialAgreementDTO projectPrice) { if (assignment is not ProjectPriceAssignment.GlobalDefault) return; var receiptSettings = this.WithSession(s => s.GetBL().GetReceiptSettings(this.GetLoggedInUser().User)); receiptSettings.OfferHasGlobalSpecialAgreement = true; receiptSettings.OfferGlobalSpecialAgreementI3D = projectPrice.I3D; this.WithSession(s => s.GetBL().UpdateReceiptSettings(receiptSettings, this.GetLoggedInUser().User)).ThrowIfError(); } } public enum ProjectPriceAssignment { None, Customer, GlobalDefault, IsGlobal, } }