using System; using System.Collections.Generic; using System.Linq; using Centron.BusinessLogic.Finances.IncomingPayments; using Centron.BusinessLogic.WebServices.Finances.Payments; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.BusinessLogic.WebServices.Sales.Receipts.ReceiptSearch; using Centron.Core; using Centron.Data.WebServices.Sales.Receipts.ReceiptSearch; using Centron.Interfaces; using Centron.Interfaces.BL; using Centron.Interfaces.Sales.Receipts.ReceiptSearch; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Finances.Payments.IncomingPayments; using Xunit; using Xunit.Abstractions; using NotImplementedException = System.NotImplementedException; namespace Centron.Tests.EndToEnd.Tests.IncomingPayment; public class IncomingPaymentTest: EndToEndTest { private int invoiceHeadI3D_2 = 6586; private int invoiceHeadI3D_1 = 6576; public IncomingPaymentTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { var createdI3Ds = this.CreateIncomingPayments(); this.DeleteIncomingPayments(createdI3Ds); this.TestSearchFilter(); } private List CreateIncomingPayments() { this.CreateIncomingPayments_1(); return this.CreateIncomingPayments_2(); } private void CreateIncomingPayments_1() { var loggedInUser = this.GetLoggedInUser(); var receipt = this.GetInvoice(this.invoiceHeadI3D_1); var newAmountPaid = 5; var changedReceipt = this.WithSession(f => f.GetBL().UpdateReceiptIsPaid( CentronObjectKindNumeric.InvoiceClass, this.invoiceHeadI3D_1, true, (newAmountPaid + receipt.PaidPrice) * receipt.CurrencyFactor, null, null, loggedInUser.User)).ThrowIfError(); this.Verifier.Verify("05_paidReceipt", changedReceipt); IncomingPaymentDTO incomingPayment = new() { Amount = newAmountPaid, Number = receipt.Number, CreatedAt = new DateTime(1970,1,1), InvoiceAmount = receipt.TaxPriceComplete + receipt.NetPriceComplete, PaymentDate = DateTime.Now, Comment = "What do you call a vampire with Alzheimer’s? A nos forgot to.", ResidualAmount = (receipt.TaxPriceComplete + receipt.NetPriceComplete) - (receipt.PaidPrice + newAmountPaid), AccountStatementDate = null, AccountStatementNumber = 1234, EmployeeI3D = loggedInUser.User.Employee.I3D, InvoiceHeadI3D = receipt.I3D, IsClosed = true }; var savedIncomingPayment = this.WithSession(f => f.GetBL().SaveIncomingPayment(incomingPayment)).ThrowIfError(); this.Verifier.Verify("05_incomingPaymentForPaidReceipt", savedIncomingPayment); } private List CreateIncomingPayments_2() { var loggedInUser = this.GetLoggedInUser(); var receipt = this.GetInvoice(invoiceHeadI3D_2); var tithe = receipt.OpenPrice % 10; var paidPrice = tithe * receipt.CurrencyFactor; var changedReceiptOne = this.WithSession(f => f.GetBL().UpdateReceiptIsPaid( CentronObjectKindNumeric.InvoiceClass, this.invoiceHeadI3D_1, null, paidPrice, null, null, loggedInUser.User)).ThrowIfError(); this.Verifier.Verify("01_partlyPaidReceipt", changedReceiptOne); IncomingPaymentDTO incomingPaymentOne = new() { Amount = paidPrice, Number = receipt.Number, CreatedAt = new DateTime(1970,1,1), InvoiceAmount = receipt.TaxPriceComplete + receipt.NetPriceComplete, PaymentDate = DateTime.Now, Comment = "What do you call a ship full of buddies? A friendship.", ResidualAmount = (receipt.TaxPriceComplete + receipt.NetPriceComplete) - (receipt.PaidPrice + tithe), AccountStatementDate = null, AccountStatementNumber = 1234, EmployeeI3D = loggedInUser.User.Employee.I3D, InvoiceHeadI3D = receipt.I3D, IsClosed = false }; var savedIncomingPaymentOne = this.WithSession(f => f.GetBL().SaveIncomingPayment(incomingPaymentOne)).ThrowIfError(); this.Verifier.Verify("01_incomingPaymentForPartlyPaidReceipt", savedIncomingPaymentOne); paidPrice += (tithe * 2) * receipt.CurrencyFactor; var changedReceiptTwo = this.WithSession(f => f.GetBL().UpdateReceiptIsPaid( CentronObjectKindNumeric.InvoiceClass, this.invoiceHeadI3D_1, null, paidPrice, null, null, loggedInUser.User)).ThrowIfError(); this.Verifier.Verify("02_partlyPaidReceipt", changedReceiptTwo); IncomingPaymentDTO incomingPaymentTwo = new() { Amount = paidPrice, Number = receipt.Number, CreatedAt = new DateTime(1970,2,2), InvoiceAmount = receipt.TaxPriceComplete + receipt.NetPriceComplete, PaymentDate = DateTime.Now, Comment = "Why did the grape cross the road? No raisin.", ResidualAmount = (receipt.TaxPriceComplete + receipt.NetPriceComplete) - (receipt.PaidPrice + tithe), AccountStatementDate = null, AccountStatementNumber = 1234, EmployeeI3D = loggedInUser.User.Employee.I3D, InvoiceHeadI3D = receipt.I3D, IsClosed = false }; var savedIncomingPaymentTwo = this.WithSession(f => f.GetBL().SaveIncomingPayment(incomingPaymentTwo)).ThrowIfError(); this.Verifier.Verify("02_incomingPaymentForPartlyPaidReceipt", savedIncomingPaymentTwo); paidPrice += (tithe * 3) * receipt.CurrencyFactor; var changedReceiptThree = this.WithSession(f => f.GetBL().UpdateReceiptIsPaid( CentronObjectKindNumeric.InvoiceClass, this.invoiceHeadI3D_1, null, paidPrice, null, null, loggedInUser.User)).ThrowIfError(); this.Verifier.Verify("03_partlyPaidReceipt", changedReceiptThree); IncomingPaymentDTO incomingPaymentThree = new() { Amount = paidPrice, Number = receipt.Number, CreatedAt = new DateTime(1970,3,3), InvoiceAmount = receipt.TaxPriceComplete + receipt.NetPriceComplete, PaymentDate = DateTime.Now, Comment = "My friend just sprayed some deodorant in my mouth. Now I have some weird Axe scent.", ResidualAmount = (receipt.TaxPriceComplete + receipt.NetPriceComplete) - (receipt.PaidPrice + tithe), AccountStatementDate = null, AccountStatementNumber = 1234, EmployeeI3D = loggedInUser.User.Employee.I3D, InvoiceHeadI3D = receipt.I3D, IsClosed = false }; var savedIncomingPaymentThree = this.WithSession(f => f.GetBL().SaveIncomingPayment(incomingPaymentThree)).ThrowIfError(); this.Verifier.Verify("03_incomingPaymentForPartlyPaidReceipt", savedIncomingPaymentThree); paidPrice += (tithe * 4) * receipt.CurrencyFactor; var changedReceiptFour = this.WithSession(f => f.GetBL().UpdateReceiptIsPaid( CentronObjectKindNumeric.InvoiceClass, this.invoiceHeadI3D_1, null, paidPrice, null, null, loggedInUser.User)).ThrowIfError(); this.Verifier.Verify("04_partlyPaidReceiptWhichIsNowPaid", changedReceiptFour); IncomingPaymentDTO incomingPaymentFour = new() { Amount = paidPrice, Number = receipt.Number, CreatedAt = new DateTime(1970,4,4), InvoiceAmount = receipt.TaxPriceComplete + receipt.NetPriceComplete, PaymentDate = DateTime.Now, Comment = "What do you call a Buddhist Squirrel? A chipmunk.", ResidualAmount = (receipt.TaxPriceComplete + receipt.NetPriceComplete) - (receipt.PaidPrice + tithe), AccountStatementDate = null, AccountStatementNumber = 1234, EmployeeI3D = loggedInUser.User.Employee.I3D, InvoiceHeadI3D = receipt.I3D, IsClosed = true }; var savedIncomingPaymentFour = this.WithSession(f => f.GetBL().SaveIncomingPayment(incomingPaymentFour)).ThrowIfError(); this.Verifier.Verify("04_incomingPaymentForPartlyPaidReceipt", savedIncomingPaymentFour); return new List() { savedIncomingPaymentOne.I3D, savedIncomingPaymentTwo.I3D, savedIncomingPaymentThree.I3D, savedIncomingPaymentFour.I3D }; } private void DeleteIncomingPayments(List incomingPaymentI3Ds) { this.DeleteIncomingPayments_1(); this.DeleteIncomingPayments_2(incomingPaymentI3Ds); this.DeleteIncomingPayments_3(); } private void DeleteIncomingPayments_2(List incomingPaymentI3Ds) { Guard.Not(incomingPaymentI3Ds.Count != 4, "something went wrong with the incomingPaymentI3Ds"); var incomingPaymentsFilter = new IncomingPaymentsFilter() { IncomingPaymentI3Ds = incomingPaymentI3Ds }; var incomingPayments = this.WithSession(f => f.GetBL().GetIncomingPayments(incomingPaymentsFilter)).ThrowIfError(); this.Verifier.Verify("07_incomingPayment_BeforeDeletingByPaymentI3D", incomingPayments); var receiptBeforeDeleting = this.GetInvoice(invoiceHeadI3D_2); var deleteIncomingPaymentFilter= new DeleteIncomingPaymentFilter() { IncomingPaymentI3Ds = new List { incomingPaymentI3Ds[0], incomingPaymentI3Ds[3] }, DontChangeInvoice = false, }; this.WithSession(f => f.GetBL().DeleteIncomingPayment(deleteIncomingPaymentFilter, this.GetLoggedInUser())).ThrowIfError(); var incomingPaymentsAfterDeleting = this.WithSession(f => f.GetBL().GetIncomingPayments(incomingPaymentsFilter)).ThrowIfError(); this.Verifier.Verify("07_incomingPayment_AfterDeletingByPaymentI3D", incomingPaymentsAfterDeleting); var receiptAfterDeleting = this.GetInvoice(invoiceHeadI3D_2); Assert.NotEqual(receiptBeforeDeleting.PaidPrice, receiptAfterDeleting.PaidPrice); this.Verifier.Verify("07_receipt_AfterDeleting", receiptBeforeDeleting); } private void DeleteIncomingPayments_1() { var incomingPaymentsFilter = new IncomingPaymentsFilter() { InvoiceHeadI3Ds = new List(){invoiceHeadI3D_1} }; var incomingPaymentsBeforeDeleting = this.WithSession(f => f.GetBL().GetIncomingPayments(incomingPaymentsFilter)).ThrowIfError(); this.Verifier.Verify("08_incomingPayment_BeforeDeletingByInvoiceI3D", incomingPaymentsBeforeDeleting); var receiptBeforeDeleting = this.GetInvoice(invoiceHeadI3D_2); var deleteIncomingPaymentFilter = new DeleteIncomingPaymentFilter() { InvoiceHeadI3Ds = new List() { invoiceHeadI3D_1 } }; this.WithSession(f => f.GetBL().DeleteIncomingPayment(deleteIncomingPaymentFilter, this.GetLoggedInUser())).ThrowIfError(); var incomingPaymentsAfterDeleting = this.WithSession(f => f.GetBL().GetIncomingPayments(incomingPaymentsFilter)).ThrowIfError(); this.Verifier.Verify("08_incomingPaymnets_AfterDeletingByInvoiceI3D", incomingPaymentsAfterDeleting); var receiptAfterDeleting = this.GetInvoice(invoiceHeadI3D_1); Assert.NotEqual(receiptBeforeDeleting.PaidPrice, receiptAfterDeleting.PaidPrice); this.Verifier.Verify("08_receipt_AfterDeletingByInvoiceI3D", receiptBeforeDeleting); } private void DeleteIncomingPayments_3() { var incomingPaymentsFilter = new IncomingPaymentsFilter(); var incomingPaymentsBeforeDeleting = this.WithSession(f => f.GetBL().GetIncomingPayments(incomingPaymentsFilter)).ThrowIfError(); this.Verifier.Verify("09_incomingPayment_BeforeDeletingByInvoiceNumber", incomingPaymentsBeforeDeleting); var receiptBeforeDeleting = this.GetInvoice(invoiceHeadI3D_2); //this should deletes entries with the invoiceHeadI3D 6586 (the receipt has the number 40286) var deleteIncomingPaymentFilter= new DeleteIncomingPaymentFilter() { InvoiceNumbers = new List { 40286 }, }; this.WithSession(f => f.GetBL().DeleteIncomingPayment(deleteIncomingPaymentFilter, this.GetLoggedInUser())).ThrowIfError(); var incomingPaymentsAfterDeleting = this.WithSession(f => f.GetBL().GetIncomingPayments(incomingPaymentsFilter)).ThrowIfError(); this.Verifier.Verify("09_incomingPayments_AfterDeletingByInvoiceNumber", incomingPaymentsAfterDeleting); var receiptAfterDeleting = this.GetInvoice(invoiceHeadI3D_1); Assert.NotEqual(receiptBeforeDeleting.PaidPrice, receiptAfterDeleting.PaidPrice); } private void TestSearchFilter() { var filter = new IncomingPaymentsFilter(); filter.IncomingPaymentI3Ds = new List(); filter.InvoiceHeadI3Ds = new List(); for (int i = 0; i < 2500; i++) { filter.IncomingPaymentI3Ds.Add(i); filter.InvoiceHeadI3Ds.Add(i); } //we don't care about the result, the method just need to finish, to see if the sql throws an error about the more than 2100 parameters this.WithSession(f => f.GetBL().GetIncomingPayments(filter)).ThrowIfError(); } private ReceiptSearchItemDTO GetInvoice(int invoiceHeadI3D) { var filter = new ReceiptSearchFilter() { ReceiptI3Ds = new List() { invoiceHeadI3D }, ReceiptKinds = new List() { CentronObjectKindNumeric.InvoiceClass }, IncludeClosedReceipts = true }; var searchedReceipt = this.WithSession(f => f.GetBL().SearchReceipts(this.GetLoggedInUser(), filter, 1, int.MaxValue)).ThrowIfError(); return searchedReceipt.Result.First(); } }