using System; using System.Collections.Generic; using System.Linq; using Centron.BusinessLogic; using Centron.BusinessLogic.Administration.Company; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.WebServices.Administration.Company; using Centron.BusinessLogic.WebServices.Administration.MasterData; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.Data.WebServices.Administration.Company; using Centron.Data.WebServices.Administration.MandatoryArea; using Centron.Interfaces; using Centron.Interfaces.BL; using Centron.Interfaces.Sales.Receipts.InsertReceipts; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.BranchArea; using CentronSoftware.Centron.WebServices.EntitiesWrongPlace.Sales.Receipts.DataAndResults; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.TicketTests.Ticket143661; public class Ticket143661Tests : EndToEndTest { public Ticket143661Tests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public const int DeliveryListI3D = 5176; public const int CustomerI3D = 10000; public const int CurrentInvoiceNumber = 9999000; public override void Execute() { // The issue in this ticket is as follows: // 1. There's a delivery list in the main branch, with a number according to main-branch number-groups // 2. A user from a different branch, which uses different number-groups, forwards the delivery list to an invoice // 3. The invoice keeps the BranchI3D and BranchOrigin from the delivery list (good!) // 5. However, the invoice number was incorrect, as it incorrectly used the user's branch this.PrepareDatabase(); this.TestNewInvoice(); this.TestForwardDeliveryList(); } private void TestNewInvoice() { var invoice = this.WithSession(s => s.GetBL().CreateNewReceipt( CentronObjectKindNumeric.InvoiceClass, this.GetLoggedInUser(), CustomerI3D, new CreateReceiptData { IgnoreCallbacks = true, }, addressI3D: null, contactPersonI3D: null, insertSalutationAndAgreement: false, ignoreDefaultContract: false, insertCustomerDiscount: false)).ThrowIfError().Receipt; Assert.Equal(CurrentInvoiceNumber + 1, invoice.Number); } private void TestForwardDeliveryList() { var invoice = this.WithSession(s => s.GetBL().ForwardReceipt( CentronObjectKindNumeric.InvoiceClass, this.GetLoggedInUser().User, new ForwardReceiptData { IgnoreCallbacks = true, CreateReceiptData = new CreateReceiptData { IgnoreCallbacks = true, }, CreateReceiptItemsFromExistingItemsData = new List { new CreateReceiptItemsFromExistingItemsData { IgnoreCallbacks = true, }, }, }, new List { new ReceiptToForward { ReceiptKind = CentronObjectKindNumeric.DeliveryListClass, ReceiptI3D = DeliveryListI3D, }, }, null, InsertReceiptTakeoverOptions.Reference, onlyTakeoverAvailableQuantity: false, ignoreDefaultContract: true)).ThrowIfError().Receipt; // The Invoice should NOT have a number from that new number-group // Because the delivery-list was created for the main-branch, and thus it should get a number from the main-branch number-groups Assert.NotEqual(CurrentInvoiceNumber + 1, invoice.Number); } private void PrepareDatabase() { // We need a second branch with separate number-groups var mandantI3D = this.CreateNewMandant(); var branchI3D = this.CreateNewBranch(mandantI3D); var numberGroups = this.GetNumberGroups(mandantI3D, branchI3D); var invoiceNumberGroup = numberGroups.Single(f => f.NumberKind == (int)NumberGroupEnum.Invoice); invoiceNumberGroup.Current = CurrentInvoiceNumber; invoiceNumberGroup.Interval = 1; this.WithSession(s => s.GetBL().SaveNumberGroups(numberGroups).ThrowIfError()); // And now we need to assign the current user to that branch this.Sql($"UPDATE dbo.Personal SET FilialI3D = {branchI3D} WHERE I3D = (SELECT Personal FROM dbo.Sichbenu WHERE I3D = 11)"); // Is the default user from this.GetLoggedInUser() } private int CreateNewMandant() { using (var session = new BLSession()) { var newMandant = new MandatoryExtendedDTO { Mandator = "Ticket 143661", State = 1 }; var mandant = session.GetBL().SaveMandatoryExtended(newMandant).ThrowIfError(); return mandant.I3D; } } private int CreateNewBranch(int mandantI3D) { using (var session = new BLSession()) { var country = session.GetBL().GetDefaultCountry(); var federalState = session.GetBL().GetFederalStates().FirstOrDefault(); var newBranch = new BranchDTO { Name = "Branch for Ticket 143661", MandatorI3D = mandantI3D, CountryI3D = country.I3D, FederalState = federalState, CreatedDate = DateTime.Now, IsDefault = 0, Status = 1, }; var branch = session.GetBL().SaveBranchDetails(newBranch).ThrowIfError(); return branch.I3D; } } private List GetNumberGroups(int mandantI3D, int branchI3D) { using (var session = new BLSession()) { session.GetBL().CreateNumberGroups(mandantI3D, branchI3D).ThrowIfError(); return session.GetBL().GetNumberGroupsForBranch(mandantI3D, branchI3D).ThrowIfError().ToList(); } } }