From 91c4d4f334b9eebdcd82695d924235fe091758a8 Mon Sep 17 00:00:00 2001 From: Andreas Buchen Date: Wed, 4 Oct 2023 14:14:30 +0200 Subject: [PATCH] Use collected sale/tax transactions to merge taxes into sale Issue: #3555 --- .../pdf/ComdirectPDFExtractor.java | 76 +++++-------------- 1 file changed, 21 insertions(+), 55 deletions(-) diff --git a/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/ComdirectPDFExtractor.java b/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/ComdirectPDFExtractor.java index 7ab1673d1d..a6550cc330 100644 --- a/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/ComdirectPDFExtractor.java +++ b/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/ComdirectPDFExtractor.java @@ -1809,14 +1809,6 @@ public List postProcessing(List items) .equals((((AccountTransaction) i.getSubject()).getType()))) // .collect(Collectors.toList()); - // Group sale and taxes transactions together and group by date and security - Map>> saleTaxesTransactions; - if (!saleTransactionList.isEmpty()) - saleTaxesTransactions = Stream.concat(saleTransactionList.stream(), taxesTransactionList.stream()) - .collect(Collectors.groupingBy(item -> item.getDate().toLocalDate(), Collectors.groupingBy(Item::getSecurity))); - else - saleTaxesTransactions = Collections.emptyMap(); - // Group dividend and taxes transactions together and group by date and security Map>> dividendTaxesTransactions; if (!dividendTransactionList.isEmpty()) @@ -1827,59 +1819,33 @@ public List postProcessing(List items) var saleTaxPairs = matchSaleAndTaxTransactions(saleTransactionList, taxesTransactionList); fixMissingCurrencyConversionForTaxesTransactions(saleTaxPairs); + + for (SaleTaxPair pair : saleTaxPairs) + { + BuySellEntry saleTransaction = (BuySellEntry) pair.sale.getSubject(); + AccountTransaction taxesTransaction = (AccountTransaction) pair.tax.getSubject(); - saleTaxesTransactions.forEach((k, v) -> { - v.forEach((security, transactions) -> { + if (taxesTransaction.getType() != AccountTransaction.Type.TAXES) + continue; - if (transactions.size() == 2) - { - BuySellEntry saleTransaction = null; - AccountTransaction taxesTransaction = null; + // Subtract the tax from the taxes transaction from the total amount + saleTransaction.setMonetaryAmount(saleTransaction.getPortfolioTransaction().getMonetaryAmount() + .subtract(taxesTransaction.getMonetaryAmount())); - // Which transaction is the taxes and which the sale? - if (transactions.get(0).getSubject() instanceof BuySellEntry - && transactions.get(1).getSubject() instanceof AccountTransaction) - { - saleTransaction = (BuySellEntry) transactions.get(0).getSubject(); - taxesTransaction = (AccountTransaction) transactions.get(1).getSubject(); - } - else if (transactions.get(1).getSubject() instanceof BuySellEntry - && transactions.get(0).getSubject() instanceof AccountTransaction) - { - saleTransaction = (BuySellEntry) transactions.get(1).getSubject(); - taxesTransaction = (AccountTransaction) transactions.get(0).getSubject(); - } + // Add taxes as tax unit + saleTransaction.getPortfolioTransaction() + .addUnit(new Unit(Unit.Type.TAX, taxesTransaction.getMonetaryAmount())); - // Check if not null and there is a sale transaction and a taxes transaction - if (saleTransaction != null && taxesTransaction != null - && AccountTransaction.Type.TAXES.equals(taxesTransaction.getType()) - && PortfolioTransaction.Type.SELL.equals(saleTransaction.getPortfolioTransaction().getType())) - { - // Subtract the tax from the taxes transaction from the total amount - saleTransaction.setMonetaryAmount(saleTransaction.getPortfolioTransaction().getMonetaryAmount() - .subtract(taxesTransaction.getMonetaryAmount())); + // Combine at sources file (One or two files?) + if (!saleTransaction.getSource().equals(taxesTransaction.getSource())) + saleTransaction.setSource(saleTransaction.getSource() + "; " + taxesTransaction.getSource()); - // Add taxes as tax unit - saleTransaction.getPortfolioTransaction() - .addUnit(new Unit(Unit.Type.TAX, taxesTransaction.getMonetaryAmount())); + // Combine at notes + saleTransaction.setNote(concat(saleTransaction.getNote(), taxesTransaction.getNote())); - // Combine at sources file (One or two files?) - if (!saleTransaction.getSource().equals(taxesTransaction.getSource())) - saleTransaction.setSource(saleTransaction.getSource() + "; " + taxesTransaction.getSource()); - - // Combine at notes - saleTransaction.setNote(concat(saleTransaction.getNote(), taxesTransaction.getNote())); - - // Add the taxes treatment to the hash table, which can be deleted - TaxesTransactionToBeDelete.add(taxesTransaction); - } - else - { - // do nothing because no taxes transaction is present - } - } - }); - }); + // delete the taxes treatment from the result list + items.remove(pair.tax()); + } dividendTaxesTransactions.forEach((k, v) -> { v.forEach((security, transactions) -> {