using System; using System.Collections.Generic; using Centron.BusinessLogic; using Centron.BusinessLogic.Sales.Receipts; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.Sales.Receipts.Internal; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.Data.Entities.Sales.Receipts.ContractLists; using Centron.Data.Entities.Sales.Receipts.Invoices; using Centron.Interfaces; using Centron.Interfaces.BL; using Centron.Interfaces.Sales.Receipts; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Sales.Receipts; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.ReceiptProvisionApplyCurrent; public class ReceiptProvisionApplyCurrentTests : EndToEndTest { public ReceiptProvisionApplyCurrentTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } private static readonly DateTime From = new(2013, 10, 1); private static readonly DateTime To = new(2013, 10, 31); public override void Execute() { // When we already had a default provision-schema, that was applied to existing receipts // And now we changed things up, there is no default provision-schema anymore // If the user now calls the ApplyCurrentSchemaToOpenReceiptsWithoutProvision method, the receipts would not get a EMPTY provision, but keep their old ones // That's incorrect tho, the receipts should get a empty provision in this situation this.PrepareDatabase(); this.RemoveDefaultProvisionSchema(); this.ApplyCurrentSchemas(); // Now the ReceiptProvisionItems-Table should be empty again this.TestApplyCurrentSchemaWorkForContracts(); this.TestInvoiceWithCreditVoucher(); } private void PrepareDatabase() { // Create a default schema using (var session = new BLSession()) { var schema = new ReceiptProvisionSchemaDTO { Name = "Test-Schema", Items = new List { new() { Receiver = ReceiptProvisionReceiver.CustomerAdviser1, Source = ReceiptProvisionSource.All, Value = ReceiptProvisionValue.Auto, SharePercentage = 100, ProvisionPercentage = 50, } } }; var savedSchema = session.GetBL().SaveProvisionSchema(schema, this.GetLoggedInUser()); var receiptSettings = session.GetBL().GetReceiptSettings(this.GetLoggedInUser().User); receiptSettings.GlobalReceiptProvisionSchemaI3D = savedSchema.I3D; session.GetBL().UpdateReceiptSettings(receiptSettings, this.GetLoggedInUser().User); } // Apply it to some existing receipts using (var session = new BLSession()) { var updateResult = session.GetBL().ApplyCurrentSchemaToOpenReceiptsWithoutProvision( new List { CentronObjectKindNumeric.InvoiceClass }, null, includeClosedReceipts:true, forceOverwriteProvision:true, From, To, branchI3Ds:null, alreadyUpdatedReceipts:null, this.GetLoggedInUser()); this.Verifier.VerifyTable("ReceiptProvisionItems_Before", "ReceiptProvisionItems"); } } private void RemoveDefaultProvisionSchema() { using var session = new BLSession(); var settings = session.GetBL().GetReceiptSettings(this.GetLoggedInUser().User); settings.GlobalReceiptProvisionSchemaI3D = null; session.GetBL().UpdateReceiptSettings(settings, this.GetLoggedInUser().User); } private void ApplyCurrentSchemas() { using var session = new BLSession(); var updateResult = session.GetBL().ApplyCurrentSchemaToOpenReceiptsWithoutProvision( new List { CentronObjectKindNumeric.InvoiceClass }, null, includeClosedReceipts:true, forceOverwriteProvision:true, From, To, null, alreadyUpdatedReceipts:null, this.GetLoggedInUser()); this.Verifier.VerifyTable("ReceiptProvisionItems_After", "ReceiptProvisionItems"); } private void TestApplyCurrentSchemaWorkForContracts() { const int ContractI3D = 35; // There is invoice 6586 in the database, which was created from the contract above // This invoice has a receipt-date of 2017-10-30, so use that for the filter var invoiceFrom = new DateTime(2017, 10, 30); var invoiceTo = new DateTime(2020, 10, 30); // Right now there is no provision at all in the database // So lets add some to the contract, and then use the ApplyCurrentSchemaToOpenReceiptsWithoutProvision method to apply the provision to the invoice using (var session = new BLSession()) { var contract = session.GetBL().GetReceiptByI3D(ContractI3D, this.GetLoggedInUser()); contract.Provision = new List { new() { Receiver = ReceiptProvisionReceiver.CustomerAdviser1, Source = ReceiptProvisionSource.All, Value = ReceiptProvisionValue.Auto, SharePercentage = 100, ProvisionPercentage = 50, } }; var saveReceiptData = new SaveReceiptData { IgnoreCallbacks = true }; var saveReceiptResultBuilder = new SaveReceiptResultBuilder(); session.GetBL().SaveProvision(contract, null, saveReceiptData, saveReceiptResultBuilder); saveReceiptResultBuilder.GetResult().ThrowIfError(); } this.Verifier.VerifyTable("ReceiptProvisionItems_OnlyContract", "ReceiptProvisionItems"); // And now lets update some invoices from that contract using (var session = new BLSession()) { var result = session.GetBL().ApplyCurrentSchemaToOpenReceiptsWithoutProvision( new List { CentronObjectKindNumeric.InvoiceClass }, null, includeClosedReceipts:true, forceOverwriteProvision:true, invoiceFrom, invoiceTo, null, alreadyUpdatedReceipts:null, this.GetLoggedInUser()); } this.Verifier.VerifyTable("ReceiptProvisionItems_InvoiceFromContract", "ReceiptProvisionItems"); this.Sql("DELETE FROM dbo.ReceiptProvisionItems"); } private void TestInvoiceWithCreditVoucher() { const int InvoiceI3D = 222; //This invoice was forwarded to credit-voucher 20, so has no current-quantity using (var session = new BLSession()) { var contract = session.GetBL().GetReceiptByI3D(InvoiceI3D, this.GetLoggedInUser()); contract.Provision = new List { new() { Receiver = ReceiptProvisionReceiver.CustomerAdviser1, Source = ReceiptProvisionSource.All, Value = ReceiptProvisionValue.Auto, SharePercentage = 100, ProvisionPercentage = 50, } }; var saveReceiptData = new SaveReceiptData { IgnoreCallbacks = true }; var saveReceiptResultBuilder = new SaveReceiptResultBuilder(); session.GetBL().SaveProvision(contract, null, saveReceiptData, saveReceiptResultBuilder); saveReceiptResultBuilder.GetResult().ThrowIfError(); } this.Verifier.VerifyTable("ReceiptProvisionItems_InvoiceWithCreditVoucher", "ReceiptProvisionItems"); } }