using Centron.BusinessLogic; using Centron.BusinessLogic.Accounting; using Centron.BusinessLogic.Finances.OnlineBanking; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.WebServices.Administration.CentronConfigDb; using Centron.BusinessLogic.WebServices.Finances.OnlineBanking; using Centron.BusinessLogic.WebServices.PasswordManager; using Centron.BusinessLogic.WebServices.Sales.Customers; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.BusinessLogic.WebServices.Sales.Receipts.ReceiptSearch; using Centron.Data.Entities.Finances.OnlineBanking; using Centron.Data.WebServices.Sales.Customers; using Centron.Data.WebServices.Sales.Receipts.Invoices; using Centron.Interfaces; using Centron.Interfaces.Accounting; using Centron.Interfaces.BL; using Centron.Interfaces.Finances.OnlineBanking; using Centron.Interfaces.Sales.Receipts.ReceiptSearch; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Accounts.Accounting; using CentronSoftware.Centron.WebServices.Entities.Finances.OnlineBanking; using CentronSoftware.Centron.WebServices.Entities.PasswordManager; using CentronSoftware.Centron.WebServices.RestRequests.OnlineBanking; using DevExpress.Xpo; using FastReport; using FluentNHibernate.Conventions; using Microsoft.Graph.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.OnlineBanking { public class OnlineBankingAccountTransactionTest : EndToEndTest { private int _configI3D; public OnlineBankingAccountTransactionTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { this.PrepareDatabase(); this.CreateAndSaveAccountTransactions(); // Also checks if the SaveMethod prevents saving duplicates with the same data // TODO: The new save method also auto checks for complete status. Is this really the right approach? var accountTransactions1 = this.LoadAccountTransactions(); var accountTransactions2 = this.LoadAccountTransactions(); this.AddCustomerAndAssignmentsManually(accountTransactions1); this.AddCustomerAndAssignmentsAutomatically(accountTransactions2); this.TestCheckForUnknownIbans(); } private void PrepareDatabase() { var user = this.GetLoggedInUser(); // Use file instead of config DB this.WithSession(f => f.GetBL().UpdatePasswordManagerSettings(user, new PasswordManagerSettingsDTO { IsPasswordManagerGuidelineManagementActive = false, SecondsUntilNewAuthentificationPasswordManagerAccess = 0, MasterPasswordSaveLocation = MasterPasswordSaveLocation.SecureFile, MasterPasswordSecureFileName = "TestMasterKey.txt" })); var masterkeySaveresult = this.WithSession(f => f.GetBL().SetHotlineMasterKey(user, "1234ABC567")).ThrowIfError(); Assert.True(masterkeySaveresult); var config = new OnlineBankingConfigurationFinApiDTO() { Name = "Test", BranchI3D = 0 }; var configSaveResult = this.WithSession(f => f.GetBL().SaveOnlineBankingConfiguration(config, user)).ThrowIfError(); this._configI3D = configSaveResult.I3D; base.Sql(@" UPDATE RechKopf SET Bezahlt = 52.7 WHERE Nummer = 40233"); } private void CreateAndSaveAccountTransactions() { using var session = new BLSession(); var accountTransactionsWebServiceBL = session.GetBL(); var user = this.GetLoggedInUser(); var transaction1 = new OnlineBankingAccountTransactionDTO() { OnlineBankingConfigurationI3D = this._configI3D, TypeCode = "103", BookingDate = new DateTime(new DateOnly(2024, 03,20), new TimeOnly(15, 00)), Amount = 190.5m, Currency = "EUR", Description = "Rechnung 40233, 40282", BookingText = "Überweisung", BIC = "ABCDEFG", AccountIBAN = "DE12345678901234567890", Sender = "Martin Herzog", RelatedCustomerI3D = 10000, CustomerMatchingRate = 0.75, IsCompleted = true, IsVisible = true, }; var transaction2 = new OnlineBankingAccountTransactionDTO() { OnlineBankingConfigurationI3D = 22, TypeCode = "103", BookingDate = new DateTime(new DateOnly(2024, 03, 21), new TimeOnly(09, 08)), Amount = 1671.4m, Currency = "EUR", Description = "Bestbeschreibung 2", BookingText = "Abbuchung", BIC = "HIJKLMN", AccountIBAN = "DE26930901000012345678", Sender = "Test Sender", RelatedCustomerI3D = 10012, CustomerMatchingRate = 0.01, IsCompleted = false, IsVisible = false, }; var savedAccountTransactions1 = accountTransactionsWebServiceBL.SaveOnlineBankingAccountTransactions( new List() { transaction1, transaction2 }, user).ThrowIfError(); var duplicatesSaveResult = accountTransactionsWebServiceBL.SaveOnlineBankingAccountTransactions( new List() { transaction2, transaction1 }, user); if (duplicatesSaveResult.Status != ResultStatus.Warning || duplicatesSaveResult.Data.Count > 0) throw new Exception("Account transactions with the same data were saved twice. There should be no data duplicates in the table!"); } private IList LoadAccountTransactions() { using var session = new BLSession(); var accountTransactionsWebServiceBL = session.GetBL(); var user = this.GetLoggedInUser(); var accountTransactions = accountTransactionsWebServiceBL.GetOnlineBankingAccountTransactionsByFilter( new OnlineBankingAccountTransactionsFilter(), user).ThrowIfError(); this.Verifier.Verify($"OnlineBankingAccountTransactions", accountTransactions.ToList()); return accountTransactions; } private void AddCustomerAndAssignmentsManually(IList accountTransactions) { using var session = new BLSession(); var accountTransactionsWebServiceBL = session.GetBL(); var customerWebServiceBL = session.GetBL(); var receiptWebServiceBL = session.GetBL(); var user = this.GetLoggedInUser(); var customers = customerWebServiceBL.GetCustomersByIDs(new List() { 10000, 10003 }); var filter1 = new ReceiptSearchFilter() { ReceiptI3Ds = new List() { 1318, 1372 } }; var receipts1 = receiptWebServiceBL.SearchReceipts(user, filter1, 1, int.MaxValue).ThrowIfError(); var filter2 = new ReceiptSearchFilter() { ReceiptI3Ds = new List() { 6553, 6565 } }; var receipts2 = receiptWebServiceBL.SearchReceipts(user, filter2, 1, int.MaxValue).ThrowIfError(); if (accountTransactions.Count != 2 || customers.Count != 2 || receipts1.Count != 2 || receipts2.Count != 2) throw new Exception("Expected two objects each for AccountTransactions, Customers and Receipts!"); for(int i=0; i<2; i++) { var accountTransaction = accountTransactions[i]; var customer = customers[i]; var receiptSearchResult = i == 0 ? receipts1 : receipts2; var receipt1 = receiptSearchResult.Result[0]; var receipt2 = receiptSearchResult.Result[1]; accountTransaction.RelatedCustomerI3D = customer.I3D; accountTransaction.RelatedCustomerName = customer.Name; accountTransaction.TransactionAssignments.Add(new OnlineBankingTransactionAssignmentDTO() { AccountTransactionI3D = accountTransaction.I3D, ReceiptI3D = receipt1.I3D, ReceiptObjectKind = (CentronObjectKindNumeric) receipt1.ObjectKind, ReceiptMatchingRate = 0.5, BookedReceiptDemandedGrossAmount = receipt1.NetPriceComplete + receipt1.TaxPriceComplete, ReceiptPaidGrossAmount = receipt1.PaidPrice, ReceiptOpenGrossAmount = receipt1.OpenPrice, AssignedAmount = 0m, ReceiptMatchingHeuristic = OnlineBankingReceiptMatchingHeuristic.ManuallySet, ReceiptDate = receipt1.DueDate.Value, ReceiptNumber = receipt1.Number, ReceiptCaption = receipt1.Caption, IsBooked = true, BookedByEmployeeI3D = 44, BookedDate = new DateTime(new DateOnly(2024, 03, 22), new TimeOnly(10, 00)), }); accountTransaction.TransactionAssignments.Add(new OnlineBankingTransactionAssignmentDTO() { AccountTransactionI3D = accountTransaction.I3D, ReceiptI3D = receipt2.I3D, ReceiptObjectKind = (CentronObjectKindNumeric) receipt2.ObjectKind, ReceiptMatchingRate = 0.5, BookedReceiptDemandedGrossAmount = receipt2.NetPriceComplete + receipt2.TaxPriceComplete, ReceiptPaidGrossAmount = receipt2.PaidPrice, ReceiptOpenGrossAmount = receipt2.OpenPrice, AssignedAmount = receipt2.OpenPrice, ReceiptMatchingHeuristic = OnlineBankingReceiptMatchingHeuristic.ManuallySet, ReceiptDate = receipt2.DueDate.Value, ReceiptNumber = receipt2.Number, ReceiptCaption = receipt2.Caption, IsBooked = false, BookedByEmployeeI3D = null, BookedDate = null }); } var savedAccountTransactions = accountTransactionsWebServiceBL.SaveOnlineBankingAccountTransactions(accountTransactions.ToList() , user).ThrowIfError(); this.Verifier.Verify($"AccountTransactionsWithManuallyAddedAssignments", accountTransactions.ToList()); } private void AddCustomerAndAssignmentsAutomatically(IList accountTransactions) { using var session = new BLSession(); var accountTransactionsWebServiceBL = session.GetBL(); var user = this.GetLoggedInUser(); var request = new AutoCompleteAccountTransactionsRequest() { OnlineBankingAccountTransactions = accountTransactions.ToList() }; var updated = accountTransactionsWebServiceBL.AutoCompleteAccountTransacitons(request, user).ThrowIfError(); this.Verifier.Verify($"AccountTransactionsWithAutomaticallyAddedAssignments", updated.ToList()); } private void TestCheckForUnknownIbans() { using var session = new BLSession(); var accountTransactionsBL = session.GetBL(); var user = this.GetLoggedInUser(); var request1 = new CheckForUnknownIbansRequest { IncludeCompleted = true }; var result1 = accountTransactionsBL.CheckForUnknownIbans(request1, user).ThrowIfError(); this.Verifier.Verify($"CheckForUnknownIbans_IncludeCompleted", result1.ToList()); var request2 = new CheckForUnknownIbansRequest { IncludeCompleted = false, DateFrom = new DateTime(2024, 03, 01), DateTo = new DateTime(2024, 03, 31) }; var result2 = accountTransactionsBL.CheckForUnknownIbans(request2, user).ThrowIfError(); this.Verifier.Verify($"CheckForUnknownIbans_WithDateRange", result2.ToList()); } } }