using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Centron.BusinessLogic; using Centron.BusinessLogic.EmployeeArea; using Centron.BusinessLogic.Sales.Receipts.ArticleSearch; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.WebServices.Accounts.Campaigns; using Centron.BusinessLogic.WebServices.Accounts.ExtendedFilters; using Centron.BusinessLogic.WebServices.Administration.HourlySurchargeRates; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.BusinessLogic.WebServices.Sales.Receipts.ArticleSearch; using Centron.BusinessLogic.WebServices.Sales.Receipts.ReceiptSearch; using Centron.Core.Extensions; using Centron.DAO; using Centron.Data.Entities.Administration.Logins; using Centron.Data.Entities.Sales.Receipts.SupplierInvoices; using Centron.Data.WebServices.Sales.Receipts; using Centron.Data.WebServices.Sales.Receipts.Articles; using Centron.Data.WebServices.Sales.Receipts.DeliveryLists; using Centron.Data.WebServices.Sales.Receipts.Invoices; using Centron.Data.WebServices.Sales.Receipts.Offers; using Centron.Data.WebServices.Sales.Receipts.ReceiptSearch; using Centron.Interfaces; using Centron.Interfaces.Accounts.Campaigns; using Centron.Interfaces.Accounts.ExtendedFilters; using Centron.Interfaces.Administration.Environment; using Centron.Interfaces.BL; using Centron.Interfaces.Sales.Receipts; using Centron.Interfaces.Sales.Receipts.ReceiptSearch; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Accounts.Campaigns; using CentronSoftware.Centron.WebServices.Entities.Accounts.ExtendedFilters; using CentronSoftware.Centron.WebServices.Entities.Accounts.ExtendedSearch; using CentronSoftware.Centron.WebServices.Entities.Accounts.ExtendedSearch.Parts; using CentronSoftware.Centron.WebServices.Entities.Administration.HourlySurchargeRates; using CentronSoftware.Centron.WebServices.Entities.Sales.Receipts; using CentronSoftware.Centron.WebServices.Entities.Sales.Receipts.SupplierInvoices; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.ReceiptFreightArticle { public class ReceiptFreightArticleTest : EndToEndTest { public ReceiptFreightArticleTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { this.Verifier.Settings.IgnoreProperty(f => f.ChangedAt); this.Verifier.Settings.IgnoreProperty(f => f.CreatedAt); this.Verifier.Settings.IgnoreProperty(f => f.CreatedDate); //we are using the same articles for 3 receipts, so we ignore the changing amount this.Verifier.Settings.IgnoreProperty(f => f.Quantity); this.Verifier.Settings.IgnoreProperty(f => f.QuantityAvailable); this.Verifier.Settings.IgnoreProperty(f => f.Quantity); this.Verifier.Settings.IgnoreProperty(f => f.QuantityAvailable); this.Verifier.Settings.IgnoreProperty(f => f.CreatedDate); this.Verifier.Settings.IgnoreProperty(f => f.CreatedVersion); this.Verifier.Settings.IgnoreProperty(f => f.ChangedDate); this.Verifier.Settings.IgnoreProperty(f => f.ChangedVersion); } public override void Execute() { this.AddExtendedSearch(); var offer = this.AddOffer(); var deliveryList = this.AddDeliveryList(); var supplierInvoice = this.AddSupplierInvoice(); this.AddFreightSettings(); this.AddOffer(afterSettingsChange:true); this.AddDeliveryList(afterSettingsChange:true); this.AddSupplierInvoice(afterSettingsChange:true); } private void AddFreightSettings() { using (var session = new BLSession()) { //global settings var user = session.GetBL().GetAppUser(f => f.I3D == 11); var settings = session.GetBL().GetReceiptSettings(user); //dontInsert -> 0; //aksForInsert -> 1; //insertAlways -> 2; settings.InsertFreightArticleIntoOrder = 2; settings.FreightArticleArticlecode = "FRACHT"; settings.FreightArticleCostMinimum = (double) 1; settings.FreightArticleCostLimit = (double) 100; settings.FreightArticlePercentage = (double) 5; settings.FreightArticlePurchasePriceCalculationKind = 1; session.GetBL().UpdateReceiptSettings(settings, user); var allFiltersResult = session.GetBL().GetCustomFilters(new LoggedInUser(user), new AccountExtendedSearchFilter()); //durch verfifier ersetzten ? sollte eigentlich durch den Verifier beim erstellen abgedeckt sein if (allFiltersResult.Status != ResultStatus.Success) throw new Exception("error loading the custom filters for the freight article test"); if (allFiltersResult.Data.Count < 3) throw new Exception("couldn't load all custom filters for the freight article test"); #region Setting Search 0 var settingSearch0 = new FreightArticleSettingDTO() { Kind = FreightArticleSettingKind.ExtendedSearch, ExtendedAccountSearchI3D = allFiltersResult.Data[0].I3D, IsActive = true, PurchasePricePercentage = 54.12, PurchasePriceMaxCost = 500, PurchasePriceMinCost = 25.42, PurchasePriceUsePercentage = true, SalePricePercentage = 87.35, SalePriceMaxCost = 50, SalePriceMinCost = 42, SalePriceUsePercentage = true, SalesPriceCanBeChangedManually = false, SetSalesPriceToZero = false }; #endregion #region Setting Search 1 var settingSearch1 = new FreightArticleSettingDTO() { Kind = FreightArticleSettingKind.ExtendedSearch, ExtendedAccountSearchI3D = allFiltersResult.Data[1].I3D, IsActive = true, PurchasePricePercentage = 23, PurchasePriceMaxCost = 2021, PurchasePriceMinCost = 07, PurchasePriceUsePercentage = false, SalePricePercentage = 42, SalePriceMaxCost = 1553, SalePriceMinCost = 07, SalePriceUsePercentage = false, SalesPriceCanBeChangedManually = false, SetSalesPriceToZero = true }; #endregion #region Setting Search 2 var settingSearch2 = new FreightArticleSettingDTO() { Kind = FreightArticleSettingKind.ExtendedSearch, ExtendedAccountSearchI3D = allFiltersResult.Data[2].I3D, IsActive = true, PurchasePricePercentage = 99.99, PurchasePriceMaxCost = Double.MaxValue, PurchasePriceMinCost = Double.Epsilon, PurchasePriceUsePercentage = true, SalePricePercentage = 45.54, SalePriceMaxCost = 4554, SalePriceMinCost = 4455, SalePriceUsePercentage = true, SalesPriceCanBeChangedManually = false, SetSalesPriceToZero = false, }; #endregion #region Setting Customer 0 var settingCustomer0 = new FreightArticleSettingDTO() { Kind = FreightArticleSettingKind.Customer, CustomerI3D = 10023, IsActive = true, PurchasePricePercentage = 54.12, PurchasePriceMaxCost = 500, PurchasePriceMinCost = 25.42, PurchasePriceUsePercentage = true, SalePricePercentage = 87.35, SalePriceMaxCost = 50, SalePriceMinCost = 42, SalePriceUsePercentage = true, SalesPriceCanBeChangedManually = false, SetSalesPriceToZero = false }; #endregion #region Setting Customer 1 var settingCustomer1 = new FreightArticleSettingDTO() { Kind = FreightArticleSettingKind.Customer, CustomerI3D = 10022, IsActive = true, PurchasePricePercentage = 23, PurchasePriceMaxCost = 2021, PurchasePriceMinCost = 07, PurchasePriceUsePercentage = false, SalePricePercentage = 42, SalePriceMaxCost = 1553, SalePriceMinCost = 07, SalePriceUsePercentage = false, SalesPriceCanBeChangedManually = false, SetSalesPriceToZero = true }; #endregion #region Setting Customer 2 var settingCustomer2 = new FreightArticleSettingDTO() { Kind = FreightArticleSettingKind.Customer, CustomerI3D = 10024, IsActive = true, PurchasePricePercentage = 99.99, PurchasePriceMaxCost = Double.MaxValue, PurchasePriceMinCost = Double.Epsilon, PurchasePriceUsePercentage = true, SalePricePercentage = 45.54, SalePriceMaxCost = 4554, SalePriceMinCost = 4455, SalePriceUsePercentage = true, SalesPriceCanBeChangedManually = false, SetSalesPriceToZero = false, }; #endregion session.GetBL().SaveOrUpdateFreightArticleSetting(settingSearch0); session.GetBL().SaveOrUpdateFreightArticleSetting(settingSearch1); session.GetBL().SaveOrUpdateFreightArticleSetting(settingSearch2); session.GetBL().SaveOrUpdateFreightArticleSetting(settingCustomer0); session.GetBL().SaveOrUpdateFreightArticleSetting(settingCustomer1); session.GetBL().SaveOrUpdateFreightArticleSetting(settingCustomer2); var allFreightArticleSettings = session.GetBL().GetFreightArticleSettingByFilter(new FreightArticleSettingFilter()); this.Verifier.Verify("AllExtendedFreightArticleSettings", allFreightArticleSettings); } } private void AddExtendedSearch() { #region Search 0 ExtendedFilterAddressZipNode addressZipNode0 = new ExtendedFilterAddressZipNode() { From = 09249, To = 89249 }; ExtendedFilterNode filterNode0 = new ExtendedFilterAndNode() { Nodes = new List() {addressZipNode0} }; AccountSearchExtendedFilter extendedFilter0 = new AccountSearchExtendedFilter() { AccountCustomFilterI3D = null, HasMailing1 = null, HasMailing2 = null, IncludeOnlyAccountsWithNoAdvertisement = false, OnlyDefaultAddress = true, OnlyDefaultAddressContact = true, SearchSubstitute = false, FilterNode = filterNode0 }; AccountCustomFilterDTO customFilter0 = new AccountCustomFilterDTO() { Caption = "Sollte Fly Tech nicht finden", ChangedByI3D = 0, ChangedDate = DateTime.Now, ChangedVersion = null, CreatedByI3D = 0, CreatedDate = DateTime.Now, CreatedVersion = null, ExtendedFilter = null, I3D = 0, IsPublic = true, IsStatic = false, ObjectI3D = null, ObjectKind = null, OnlySearchResult = false }; #endregion Search 0 #region Search 1 ExtendedFilterAddressZipNode addressZipNode1 = new ExtendedFilterAddressZipNode() { From = 89249, To = 89251 }; ExtendedFilterNode filterNode1 = new ExtendedFilterAndNode() { Nodes = new List() {addressZipNode1} }; AccountSearchExtendedFilter extendedFilter1 = new AccountSearchExtendedFilter() { AccountCustomFilterI3D = null, HasMailing1 = null, HasMailing2 = null, IncludeOnlyAccountsWithNoAdvertisement = false, OnlyDefaultAddress = true, OnlyDefaultAddressContact = true, SearchSubstitute = false, FilterNode = filterNode1 }; AccountCustomFilterDTO customFilter1 = new AccountCustomFilterDTO() { Caption = "Sollte Fly Tech finden", ChangedByI3D = 0, ChangedDate = DateTime.Now, ChangedVersion = null, CreatedByI3D = 0, CreatedDate = DateTime.Now, CreatedVersion = null, ExtendedFilter = null, I3D = 0, IsPublic = true, IsStatic = false, ObjectI3D = null, ObjectKind = null, OnlySearchResult = false }; #endregion Search 1 #region Search 2 ExtendedFilterAddressZipNode addressZipNode2 = new ExtendedFilterAddressZipNode() { From = 12345, To = 89251 }; ExtendedFilterNode filterNode2 = new ExtendedFilterAndNode() { Nodes = new List() {addressZipNode2} }; AccountSearchExtendedFilter extendedFilter2 = new AccountSearchExtendedFilter() { AccountCustomFilterI3D = null, HasMailing1 = null, HasMailing2 = null, IncludeOnlyAccountsWithNoAdvertisement = false, OnlyDefaultAddress = true, OnlyDefaultAddressContact = true, SearchSubstitute = false, FilterNode = filterNode2 }; AccountCustomFilterDTO customFilter2 = new AccountCustomFilterDTO() { Caption = "Sollte Fly Tech mit vielen anderen finden", ChangedByI3D = 0, ChangedDate = DateTime.Now, ChangedVersion = null, CreatedByI3D = 0, CreatedDate = DateTime.Now, CreatedVersion = null, ExtendedFilter = null, I3D = 0, IsPublic = true, IsStatic = false, ObjectI3D = null, ObjectKind = null, OnlySearchResult = false }; #endregion Search 2 using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); session.GetBL().SaveCustomFilter(new LoggedInUser(user), customFilter0, extendedFilter0); session.GetBL().SaveCustomFilter(new LoggedInUser(user), customFilter1, extendedFilter1); session.GetBL().SaveCustomFilter(new LoggedInUser(user), customFilter2, extendedFilter2); var allSearches = session.GetBL().GetCustomFilters(new LoggedInUser(user), new AccountExtendedSearchFilter()); this.Verifier.Verify("SavedCustomFilter", allSearches); } } private ReceiptOfferDTO AddOffer(bool afterSettingsChange = false) { var offer = this.CreateNewOffer(afterSettingsChange); var articles = this.SearchArticles(); this.AddPositionsOrder(offer, articles); return this.SaveOffer(offer,afterSettingsChange); } private ReceiptSupplierInvoiceDTO AddSupplierInvoice(bool afterSettingsChange = false) { var supplierInvoice = this.CreateNewSupplierInvoice(afterSettingsChange); var articles = this.SearchArticles(); this.AddPositionsSupplierInvoice(supplierInvoice, articles, afterSettingsChange); return this.SaveSupplierInvoice(supplierInvoice, afterSettingsChange); } private ReceiptDeliveryListDTO AddDeliveryList(bool afterSettingsChange = false) { var deliveryList = CreateNewDeliveryList(afterSettingsChange); var articles = this.SearchArticles(); this.AddPositionsDeliveryList(deliveryList, articles, afterSettingsChange); return this.SaveDeliveryList(deliveryList,afterSettingsChange); } private IList SearchArticles() { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var result1 = session.GetBL().SearchArticles("L:Eigene UG086E", true, new OverriddenArticleSearchFilter {ExternalArticleSearches = new ExternalSearchedArticleKind[0]}, 10022, CentronObjectKindNumeric.OfferClass, null, new LoggedInUser(user)); var result2 = session.GetBL().SearchArticles("L:Eigene U6493PE", true, new OverriddenArticleSearchFilter {ExternalArticleSearches = new ExternalSearchedArticleKind[0]}, 10022, CentronObjectKindNumeric.OfferClass, null, new LoggedInUser(user)); return result1.Data.Articles.Concat(new[] {result2.Data.Articles[0]}).ToList(); } } #region Offer private ReceiptOfferDTO CreateNewOffer(bool afterSettingsChange) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var result = session.GetBL().CreateNewReceipt(CentronObjectKindNumeric.OfferClass, new LoggedInUser(user), 10023, new CreateReceiptData(), null, null, true, false, true); return (ReceiptOfferDTO) result.Data.Receipt; } } private void AddPositionsOrder(ReceiptOfferDTO receipt, IList articles) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); foreach (var article in articles) { var receiptItemsResult = session.GetBL().CreateArticleReceiptItems( receipt, article.IsExternalArticle, article.I3D, article.ExternalArticleKind, article.ExternalArticleId, -1, new Data.Entities.Administration.Logins.LoggedInUser(user)); receipt.Items.AddRange(receiptItemsResult.Data.ReceiptItems.Cast()); } } } private ReceiptOfferDTO SaveOffer(ReceiptOfferDTO receipt, bool afterSettingsChange = false) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var watch = Stopwatch.StartNew(); var saveResult = session.GetBL().SaveReceipt(receipt, user, new SaveReceiptData { IgnoreCallbacks = true, InsertFreightArticle = afterSettingsChange }, false, CreatedThroughApplication.CentronNet, null); watch.Stop(); this.TestOutputHelper.WriteLine($"Saving the offer took {watch.Elapsed}"); if(afterSettingsChange) this.Verifier.Verify("SaveOfferResultAfterSettings", saveResult); else this.Verifier.Verify("SaveOfferResult", saveResult); return (ReceiptOfferDTO) saveResult.Data.Receipt; } } #endregion #region DeliveryList private ReceiptDeliveryListDTO CreateNewDeliveryList(bool afterSettingsChange) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var result = session.GetBL().CreateNewReceipt(CentronObjectKindNumeric.DeliveryListClass, new LoggedInUser(user), 10023, new CreateReceiptData(), null, null, true, false, true); return (ReceiptDeliveryListDTO) result.Data.Receipt; } } private void AddPositionsDeliveryList(ReceiptDeliveryListDTO receipt, IList articles, bool afterSettingsChange) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); foreach (var article in articles) { var receiptItemsResult = session.GetBL().CreateArticleReceiptItems( receipt, article.IsExternalArticle, article.I3D, article.ExternalArticleKind, article.ExternalArticleId, -1, new Data.Entities.Administration.Logins.LoggedInUser(user)); if(!afterSettingsChange) this.Verifier.Verify($"ReceiptDeliveryListItemsResultFor{article.I3D}", receiptItemsResult); receipt.Items.AddRange(receiptItemsResult.Data.ReceiptItems.Cast()); } } } private ReceiptDeliveryListDTO SaveDeliveryList(ReceiptDeliveryListDTO receipt, bool afterSettingsChange = false) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var watch = Stopwatch.StartNew(); var saveResult = session.GetBL().SaveReceipt(receipt, user, new SaveReceiptData { IgnoreCallbacks = true, InsertFreightArticle = afterSettingsChange }, false, CreatedThroughApplication.CentronNet, null); watch.Stop(); this.TestOutputHelper.WriteLine($"Saving the DeliveryList took {watch.Elapsed}"); if(afterSettingsChange) this.Verifier.Verify("SaveDeliveryListResultAfterSettingsChange", saveResult); else this.Verifier.Verify("SaveDeliveryListResult", saveResult); return (ReceiptDeliveryListDTO) saveResult.Data.Receipt; } } #endregion #region SupplierInvoice private ReceiptSupplierInvoiceDTO CreateNewSupplierInvoice(bool afterSettingsChange) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var result = session.GetBL().CreateNewReceipt(CentronObjectKindNumeric.SupplierInvoice, new LoggedInUser(user), 70003, new CreateReceiptData(), null, null, true, false, true); return (ReceiptSupplierInvoiceDTO) result.Data.Receipt; } } private void AddPositionsSupplierInvoice(ReceiptSupplierInvoiceDTO receipt, IList articles, bool afterSettingsChange) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); foreach (var article in articles) { var receiptItemsResult = session.GetBL().CreateArticleReceiptItems( receipt, article.IsExternalArticle, article.I3D, article.ExternalArticleKind, article.ExternalArticleId, -1, new Data.Entities.Administration.Logins.LoggedInUser(user)); receipt.Items.AddRange(receiptItemsResult.Data.ReceiptItems.Cast()); } //hijack this test to check a new property for Ticket 137714 receipt.ReceiptLink = "How does The Rock pee? He Dwaynes his Johnson"; } } private ReceiptSupplierInvoiceDTO SaveSupplierInvoice(ReceiptSupplierInvoiceDTO receipt, bool afterSettingsChange) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var watch = Stopwatch.StartNew(); var saveResult = session.GetBL().SaveReceipt(receipt, user, new SaveReceiptData { IgnoreCallbacks = true, InsertFreightArticle = afterSettingsChange }, false, CreatedThroughApplication.CentronNet, null); watch.Stop(); this.TestOutputHelper.WriteLine($"Saving the SupplierInvoice took {watch.Elapsed}"); if (afterSettingsChange) { this.Verifier.Verify("SaveSupplierInvoiceResultAfterSettingsChange", saveResult); } else this.Verifier.Verify("SaveSupplierInvoiceResult", saveResult); return (ReceiptSupplierInvoiceDTO) saveResult.Data.Receipt; } } #endregion } }