using System.Collections.Generic; using System.Linq; using Centron.BusinessLogic; using Centron.BusinessLogic.EmployeeArea; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.Warehousing; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.BusinessLogic.WebServices.Warehousing; using Centron.Core.Extensions; using Centron.Data.Entities.Administration.Logins; using Centron.Data.WebServices.Sales.Receipts; using Centron.Interfaces; using Centron.Interfaces.Administration.Environment; using Centron.Interfaces.BL; using Centron.Interfaces.Sales.Receipts; using Centron.Interfaces.Sales.Receipts.InsertReceipts; using Centron.Interfaces.Warehousing; using Centron.Tests.EndToEnd.Infrastructure; using Centron.Tests.EndToEnd.Infrastructure.Verifier; using CentronSoftware.Centron.WebServices.EntitiesWrongPlace.Sales.Receipts.DataAndResults; using CentronSoftware.Centron.WebServices.Extensions; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.TicketTests.Ticket113237 { public class Ticket113237Test : EndToEndTest { public const string Barcode = "100072017"; public const int BarcodeI3D = 3678; public const int ArticleI3D = 1256; public Ticket113237Test(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { this.PrepareDatabase(); //=================================================================// // Test the full chain, order -> delivery-list -> invoice and back // //=================================================================// var orderI3D = this.CreateReceiptWithBarcode(CentronObjectKindNumeric.OrderClass); this.VerifyBarcodeTables("1_AfterOrderCreated"); //barcode is in order now var deliveryListI3D = this.ForwardReceipt(CentronObjectKindNumeric.OrderClass, orderI3D, CentronObjectKindNumeric.DeliveryListClass); this.VerifyBarcodeTables("2_AfterForwardToDeliveryList"); //barcode is in delivery-list now var invoiceI3D = this.ForwardReceipt(CentronObjectKindNumeric.DeliveryListClass, deliveryListI3D, CentronObjectKindNumeric.InvoiceClass); this.VerifyBarcodeTables("3_AfterForwardToInvoice"); //barcode is in invoice now this.CreateNewVersion(CentronObjectKindNumeric.OrderClass, orderI3D); this.VerifyBarcodeTables("4_AfterNewOrderVersion"); //barcode is still in invoice this.ClearReceipt(CentronObjectKindNumeric.InvoiceClass, invoiceI3D); this.VerifyBarcodeTables("5_AfterClearInvoice"); //barcode is in delivery-list now this.ClearReceipt(CentronObjectKindNumeric.DeliveryListClass, deliveryListI3D); this.VerifyBarcodeTables("6_AfterClearDeliveryList"); //barcode is in order now this.ClearReceipt(CentronObjectKindNumeric.OrderClass, orderI3D); this.VerifyBarcodeTables("7_AfterClearOrder"); //barcode is completely free again //======================================================================// // Test creating just a delivery-list, and removing it from there again // //======================================================================// deliveryListI3D = this.CreateReceiptWithBarcode(CentronObjectKindNumeric.DeliveryListClass); this.VerifyBarcodeTables("8_AfterNewDeliveryListCreated"); //barcode is in delivery-list now this.ClearReceipt(CentronObjectKindNumeric.DeliveryListClass, deliveryListI3D); this.VerifyBarcodeTables("9_AfterClearNewDeliveryList"); //barcode is completely free again //=======================================================// // Test creating a order -> invoice, and back into order // //=======================================================// orderI3D = this.CreateReceiptWithBarcode(CentronObjectKindNumeric.OrderClass); this.VerifyBarcodeTables("10_AfterNewOrderCreated"); //barcode is in order now invoiceI3D = this.ForwardReceipt(CentronObjectKindNumeric.OrderClass, orderI3D, CentronObjectKindNumeric.InvoiceClass); this.VerifyBarcodeTables("11_AfterNewOrderForwardedToInvoice"); //barcode is in invoice now this.ClearReceipt(CentronObjectKindNumeric.InvoiceClass, invoiceI3D); this.VerifyBarcodeTables("12_AfterClearNewInvoice"); //barcode is back in order } private void PrepareDatabase() { this.Sql("ALTER TABLE RechKopf DROP COLUMN LcmStatus"); this.Sql("ALTER TABLE GutKopf DROP COLUMN LcmStatus"); // There is no Bankverbindung for the customer 10022 - but we are creating invoices for him // So we just move the existing Bankverbindung to customer 10022, so he can use it for this test this.Sql("UPDATE dbo.Bankverbindungen SET ObjectI3D = 10022"); } private int CreateReceiptWithBarcode(CentronObjectKindNumeric receiptKind) { ReceiptBaseDTO receipt; using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var result = session.GetBL().CreateNewReceipt( receiptKind, new LoggedInUser(user), customerI3D: 10022, new CreateReceiptData(), addressI3D: null, contactPersonI3D: null, insertSalutationAndAgreement: true, ignoreDefaultContract: false, insertCustomerDiscount: true); var receiptItem = session.GetBL().CreateArticleReceiptItems( result.Data.Receipt, isExternalArticle: false, ArticleI3D, externalArticleKind: null, externalArticleId: null, warehouseI3D: -1, new LoggedInUser(user)); receipt = result.Data.Receipt; var allReceiptItems = receipt.GetReceiptItems(); allReceiptItems.AddRange(receiptItem.Data.ReceiptItems); receipt.SetReceiptItems(allReceiptItems); var barcodeFilter = new BarcodeFilter { ArticleI3Ds = new List { ArticleI3D } }; var barcodes = session.GetBL().GetBarcodesThroughPaging(barcodeFilter, 1, 100); var articlePosition = receipt.GetReceiptItems().Single(f => f.ArticleI3D == ArticleI3D); var barcode = barcodes.Result.Single(f => f.Name == Barcode); articlePosition.Get((IReceiptItemWithBarcodes f) => f.Barcodes).Add(new ReceiptItemBarcode { WarehouseI3D = barcode.SecondaryStockI3D, WarehouseCaption = "Doesn't matter", CurrentBarcodeState = (BarcodeState)barcode.State, BarcodeCaption = barcode.Name, BarcodeI3D = barcode.I3D, IsActive = true }); } using (var saveSession = new BLSession()) { var user = saveSession.GetBL().GetAppUser(f => f.I3D == 11); var saveResult = saveSession.GetBL().SaveReceipt( receipt, user, new SaveReceiptData { IgnoreCallbacks = true }, autoLockIfNewReceipt: false, CreatedThroughApplication.CentronNet, receiptTemplateFolderI3D: null); return saveResult.Data.Receipt.I3D; } } private int ForwardReceipt(CentronObjectKindNumeric receiptKind, int receiptI3D, CentronObjectKindNumeric toReceiptKind) { ReceiptBaseDTO newReceipt; using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var forwardResult = session.GetBL().ForwardReceipt( toReceiptKind, user, new ForwardReceiptData { CreateReceiptData = new CreateReceiptData { IgnoreCallbacks = true, }, CreateReceiptItemsFromExistingItemsData = new List { new() { IgnoreCallbacks = true, }, }, IgnoreCallbacks = true }, new List { new ReceiptToForward { ReceiptKind = receiptKind, ReceiptI3D = receiptI3D } }, receiptProjectLayoutItemI3DsToForward:null, InsertReceiptTakeoverOptions.Reference, onlyTakeoverAvailableQuantity: true, ignoreDefaultContract: false).ThrowIfError(); newReceipt = forwardResult.Receipt; } using (var saveSession = new BLSession()) { var user = saveSession.GetBL().GetAppUser(f => f.I3D == 11); var saveReceiptResult = saveSession.GetBL().SaveReceipt( newReceipt, user, new SaveReceiptData { IgnoreCallbacks = false, InsertFreightArticle = false }, autoLockIfNewReceipt: false, CreatedThroughApplication.CentronNet, receiptTemplateFolderI3D: null).ThrowIfError(); return saveReceiptResult.Receipt.I3D; } } private void CreateNewVersion(CentronObjectKindNumeric receiptKind, int receiptI3D) { ReceiptBaseDTO newVersion; using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var createNewVersionResult = session.GetBL().CreateNewVersion( receiptKind, receiptI3D, version: null, newContactPersonI3D: null, user, new CreateNewVersionData { IgnoreCallbacks = true }).ThrowIfError(); newVersion = createNewVersionResult.Receipt; } using (var saveSession = new BLSession()) { var user = saveSession.GetBL().GetAppUser(f => f.I3D == 11); var saveVersionResult = saveSession.GetBL().SaveReceipt( newVersion, user, new SaveReceiptData { IgnoreCallbacks = false, InsertFreightArticle = false }, autoLockIfNewReceipt: false, CreatedThroughApplication.CentronNet, receiptTemplateFolderI3D: null).ThrowIfError(); } } private void ClearReceipt(CentronObjectKindNumeric receiptKind, int invoiceI3D) { ReceiptBaseDTO newVersion; using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var createNewVersionResult = session.GetBL().CreateNewVersion( receiptKind, invoiceI3D, version: null, newContactPersonI3D: null, user, new CreateNewVersionData { IgnoreCallbacks = true }).ThrowIfError(); newVersion = createNewVersionResult.Receipt; } //Remove all receipt-items newVersion.SetReceiptItems(new List()); using (var saveSession = new BLSession()) { var user = saveSession.GetBL().GetAppUser(f => f.I3D == 11); var saveVersionResult = saveSession.GetBL().SaveReceipt( newVersion, user, new SaveReceiptData { IgnoreCallbacks = false, InsertFreightArticle = false }, autoLockIfNewReceipt: false, CreatedThroughApplication.CentronNet, receiptTemplateFolderI3D: null).ThrowIfError(); } } private void VerifyBarcodeTables(string name) { var settings = new CentronVerifierSettings(); settings.IgnoreColumn("Datum"); this.Verifier.VerifyTable($"{name}_Table_Barcode", tableName:"Barcode", where:$"I3D = {BarcodeI3D}"); this.Verifier.VerifyTable($"{name}_Table_SeriennummerToPosition", "SeriennummerToPosition", $"SNI3D = {BarcodeI3D}", settings); this.Verifier.VerifyTable($"{name}_Table_BarcodeHistory", "BarcodeHistory", $"BarcodeI3D = {BarcodeI3D}", settings); } } }