From 5900a6e6e452a9c1151772a8137e5d32639e5082 Mon Sep 17 00:00:00 2001 From: Bob Hodgetts Date: Fri, 27 Jun 2025 09:02:22 +0100 Subject: [PATCH] Bug fixes in converting invoice to gbp method --- .../Logic/Account/CurrencyService.cs | 14 +++++++------- src/bnhtrade.Core/Test/Account/Account.cs | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/bnhtrade.Core/Logic/Account/CurrencyService.cs b/src/bnhtrade.Core/Logic/Account/CurrencyService.cs index 5fc5f53..e58b1fb 100644 --- a/src/bnhtrade.Core/Logic/Account/CurrencyService.cs +++ b/src/bnhtrade.Core/Logic/Account/CurrencyService.cs @@ -113,14 +113,14 @@ namespace bnhtrade.Core.Logic.Account // convert line items var lineWeighting = new List<(int, decimal)>(); var lineCalculatedTotalByWeighting = new List<(int, decimal)>(); - decimal newLineSum = 0; + decimal convertedLineListSum = 0; int i = 0; foreach (var line in invoice.InvoiceLineList) { decimal weighting = line.LineTotalAmount / invoiceOriginalTotalAmount; - decimal lineCalculatedTotal = invoiceOriginalTotalAmount * weighting; + decimal lineConvertedTotal = existingRate.ConvertToGbp(invoiceOriginalTotalAmount * weighting); lineWeighting.Add(new(i, weighting)); - lineCalculatedTotalByWeighting.Add(new(i, lineCalculatedTotal)); + lineCalculatedTotalByWeighting.Add(new(i, lineConvertedTotal)); // edit line if (line.TaxAmountAdjust != 0) @@ -128,17 +128,17 @@ namespace bnhtrade.Core.Logic.Account line.TaxAmountAdjust = existingRate.ConvertToGbp(line.TaxAmountAdjust); } line.UnitAmount = null; - line.SetUnitAmountByLineTotal(decimal.Round(lineCalculatedTotal, 2)); - newLineSum += line.LineTotalAmount; + line.SetUnitAmountByLineTotal(decimal.Round(lineConvertedTotal, 2)); + convertedLineListSum += line.LineTotalAmount; i++; } // there may be rounding errors // section untested - may have to fix some bugs in here when I've got some invoice to test on - if (invoiceOriginalTotalAmount != newLineSum) + if (invoice.InvoiceTotalAmount != convertedLineListSum) { - decimal amountRemainingToDistribute = invoiceOriginalTotalAmount - newLineSum; + decimal amountRemainingToDistribute = invoice.InvoiceTotalAmount.Value - convertedLineListSum; lineWeighting = lineWeighting.OrderByDescending(i => i.Item2).ToList(); foreach (var line in lineWeighting) diff --git a/src/bnhtrade.Core/Test/Account/Account.cs b/src/bnhtrade.Core/Test/Account/Account.cs index 9579e1f..4180fc6 100644 --- a/src/bnhtrade.Core/Test/Account/Account.cs +++ b/src/bnhtrade.Core/Test/Account/Account.cs @@ -14,7 +14,7 @@ namespace bnhtrade.Core.Test.Account { public Account() { - UpdateHmrcExchageRates(); + InoivceCurrencyConversion(); } public void UpdateHmrcExchageRates() @@ -41,5 +41,21 @@ namespace bnhtrade.Core.Test.Account read.AccountJournalId = new List { 123, 300, 324, 5678, 22 }; var result = read.Read(); } + + public void InoivceCurrencyConversion() + { + var invoiceService = new bnhtrade.Core.Logic.Export.AccountInvoice.QueueService(); + var invoiceList = invoiceService.ReadInvoiceById(new List { 349, 371, 375, 379 }); + var currencyService = new bnhtrade.Core.Logic.Account.CurrencyService(); + + foreach (var invoice in invoiceList.Values.ToList()) + { + if (currencyService.ConvertInvoiceToGbp(invoice) == false) + { + throw new Exception(currencyService.ErrorMessage); + } + } + int i = 3; + } } }