From 57d77385fd2e875ed639ac2a3338c772a32cbd2e Mon Sep 17 00:00:00 2001 From: Andreas Buchen Date: Mon, 30 Sep 2019 07:15:03 +0200 Subject: [PATCH] Fixed creation of transaction unit if security and transaction currencies match Issue: #1234 --- .../datatransfer/pdf/JSONPDFExtractor.java | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/JSONPDFExtractor.java b/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/JSONPDFExtractor.java index e0db14803a..6fe8e6ca2f 100644 --- a/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/JSONPDFExtractor.java +++ b/name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/pdf/JSONPDFExtractor.java @@ -238,9 +238,10 @@ private Extractor.Item wrapBuySell(JTransaction t, PortfolioTransaction.Type txT entry.setShares(Values.Share.factorize(t.getShares())); - entry.setSecurity(convertToSecurity(t)); + Security security = convertToSecurity(t); + entry.setSecurity(security); - t.getUnits().map(u -> convertToUnit(t, u)).filter(Objects::nonNull) + t.getUnits().map(u -> convertToUnit(security, t, u)).filter(Objects::nonNull) .forEach(u -> entry.getPortfolioTransaction().addUnit(u)); BuySellEntryItem item = new BuySellEntryItem(entry); @@ -262,9 +263,10 @@ private Extractor.Item wrapDividend(JTransaction t) else tx.setDateTime(t.getDate().atStartOfDay()); - tx.setSecurity(convertToSecurity(t)); + Security security = convertToSecurity(t); + tx.setSecurity(security); - t.getUnits().map(u -> convertToUnit(t, u)).filter(Objects::nonNull).forEach(tx::addUnit); + t.getUnits().map(u -> convertToUnit(security, t, u)).filter(Objects::nonNull).forEach(tx::addUnit); TransactionItem item = new TransactionItem(tx); item.setData(t); @@ -282,7 +284,7 @@ private Security convertToSecurity(JTransaction t) return getOrCreateSecurity(values); } - private Transaction.Unit convertToUnit(JTransaction jtx, JTransactionUnit junit) + private Transaction.Unit convertToUnit(Security security, JTransaction jtx, JTransactionUnit junit) { Money amount = null; @@ -313,12 +315,27 @@ private Transaction.Unit convertToUnit(JTransaction jtx, JTransactionUnit junit) if (Strings.isNullOrEmpty(junit.getFxCurrency())) return null; - String fxCurrency = asCurrencyCode(jtx.getSecurity().getCurrency()); + String fxCurrency = asCurrencyCode(junit.getFxCurrency()); - if (jtx.getCurrency().equals(fxCurrency)) + // two reasons to not have a fx unit: + + // a) fx part of unit is not needed if currency of security and unit + // match + + boolean transactionAndUnitCurrencyMatch = jtx.getCurrency().equals(fxCurrency); + + // b) fx part of unit is not needed if the security is configured in the + // currency of the transactions. However, it is important to check + // against the resolved security as the parsed currency might differ + + boolean transactionAndSecurityCurrencyMatch = jtx.getCurrency().equals(security.getCurrencyCode()); + + if (transactionAndUnitCurrencyMatch || transactionAndSecurityCurrencyMatch) { - return junit.getType() != Transaction.Unit.Type.GROSS_VALUE ? new Transaction.Unit(junit.getType(), amount) - : null; + if (junit.getType() == Transaction.Unit.Type.GROSS_VALUE) + return null; + else + new Transaction.Unit(junit.getType(), amount); } // check forex amount