using System.Linq; using Centron.BusinessLogic.Administration.Company; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.Data.WebServices.Sales.Receipts.Invoices; using Centron.Interfaces; using Centron.Interfaces.Administration.Environment; using Centron.Interfaces.BL; using Centron.Tests.EndToEnd.Infrastructure; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.TicketTests.Ticket138418; public class Ticket138418Tests : EndToEndTest { public Ticket138418Tests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { this.Verifier.Settings.IgnoreColumn("Date"); } public override void Execute() { // In this ticket the problem was, that in the ContractItemsContingent table, it would save the old incorrect invoice-number // Not the correct one from the 0,00 € invoice-number-group that was actually used for the invoice this.PrepareDatabase(); var invoice = this.CreateInvoice(); Assert.Equal(40287, invoice.Number); //New invoice should have a number from the default number-group var savedInvoice = this.SaveInvoice(invoice); Assert.Equal(22001, savedInvoice.Number); //When it's saved we should use a number from the 0,00 € invoice-number-group this.Verifier.VerifyTable("Items", "ContractItemsContingent", $"AssetI3D = {savedInvoice.I3D} AND AssetKind = 4"); } private ReceiptInvoiceDTO SaveInvoice(ReceiptInvoiceDTO invoice) { var savedInvoice = (ReceiptInvoiceDTO)this.WithSession(s => s.GetBL().SaveReceipt( invoice, this.GetLoggedInUser().User, new SaveReceiptData(), autoLockIfNewReceipt: false, CreatedThroughApplication.WebServices, receiptTemplateFolderI3D: null)).ThrowIfError().Receipt; return savedInvoice; } private ReceiptInvoiceDTO CreateInvoice() { // Create new invoice var invoice = (ReceiptInvoiceDTO)this.WithSession(s => s.GetBL().CreateNewReceipt( CentronObjectKindNumeric.InvoiceClass, this.GetLoggedInUser(), customerI3D: 10000, new CreateReceiptData { IgnoreCallbacks = true, }, addressI3D: null, contactPersonI3D: null, insertSalutationAndAgreement: false, ignoreDefaultContract: false, insertCustomerDiscount: false)).ThrowIfError().Receipt; // Assign contingent-contract to the invoice invoice.ContractI3D = 35; // Create items var items = this.WithSession(s => s.GetBL().CreateArticleReceiptItems( invoice, isExternalArticle: false, articleI3D: 5, externalArticleKind: null, externalArticleId: null, warehouseI3D: -1, this.GetLoggedInUser())).ThrowIfError(); invoice.Items.AddRange(items.ReceiptItems.Cast()); return invoice; } private void PrepareDatabase() { // Setup 0,00 € invoice-number-group this.WithSession(s => s.GetBL().RefreshAllNumberGroups()); this.Sql("UPDATE dbo.Nummernkreis SET BereichVon = 22000, BereichBis = 22999, Aktuell = 22000, Intervall = 1 WHERE I3D = 56"); // Configure the 0,00 € invoice-number-group // Reset contingent-settings to work for all contingent-contracts and service-articles this.Sql($"DELETE FROM dbo.GroupToContingent"); this.Sql($"INSERT INTO dbo.GroupToContingent (ContractI3D, Kind, ElementI3D) VALUES (0, 1, 3)"); } }