Search in sources :

Example 56 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class ConsorsbankPDFExtractor method addQ42017DividendTransaction.

@SuppressWarnings("nls")
private void addQ42017DividendTransaction() {
    DocumentType type = new DocumentType("Dividendengutschrift");
    this.addDocumentTyp(type);
    Block block = new Block("Dividendengutschrift.*");
    type.addBlock(block);
    block.set(new Transaction<AccountTransaction>().subject(() -> {
        AccountTransaction t = new AccountTransaction();
        t.setType(AccountTransaction.Type.DIVIDENDS);
        return t;
    }).section("name", "wkn", "isin", // 
    "currency").find(// 
    "Wertpapierbezeichnung WKN ISIN").match(// 
    "(?<name>.*) (?<wkn>[^ ]*) (?<isin>[^ ]*)$").match("Dividende pro Stück ([\\d.]+,\\d+) (?<currency>\\w{3}+).*").assign((t, v) -> {
        t.setSecurity(getOrCreateSecurity(v));
    }).section("amount", // 
    "currency").match("Netto zugunsten IBAN (.*) (?<amount>[\\d.]+,\\d+) (?<currency>\\w{3}+)$").assign((t, v) -> {
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
        t.setAmount(asAmount(v.get("amount")));
    }).section(// 
    "shares").match(// 
    "(?<shares>[\\d.]+(,\\d+)?) Stück").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section(// 
    "date").match("Valuta (?<date>\\d+.\\d+.\\d{4}+).*").assign((t, v) -> t.setDateTime(asDate(v.get("date")))).section("exchangeRate", "fxAmount", "fxCurrency", "amount", "currency").optional().match("Brutto in (\\w{3}+) (?<fxAmount>[\\d.]+,\\d+) (?<fxCurrency>\\w{3}+)").match(// 
    "Devisenkurs (?<exchangeRate>[\\d.]+,\\d+) (\\w{3}+) / (\\w{3}+)").match(// 
    "Brutto in (\\w{3}+) (?<amount>[\\d.]+,\\d+) (?<currency>\\w{3}+)").assign((t, v) -> {
        BigDecimal rate = asExchangeRate(v.get("exchangeRate"));
        BigDecimal inverseRate = BigDecimal.ONE.divide(rate, 10, BigDecimal.ROUND_HALF_DOWN);
        type.getCurrentContext().put("exchangeRate", inverseRate.toPlainString());
        Money fxAmount = Money.of(asCurrencyCode(v.get("fxCurrency")), asAmount(v.get("fxAmount")));
        Money amount = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("amount")));
        if (!t.getCurrencyCode().equals(t.getSecurity().getCurrencyCode())) {
            Unit grossValue = new Unit(Unit.Type.GROSS_VALUE, amount, fxAmount, inverseRate);
            t.addUnit(grossValue);
        }
    }).section("tax", "currency").optional().match("abzgl. Quellensteuer .* (\\w{3}+) (?<tax>[\\d.]+,\\d+) (?<currency>\\w{3}+)").assign((t, v) -> {
        Money tax = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("tax")));
        if (tax.getCurrencyCode().equals(t.getCurrencyCode())) {
            t.addUnit(new Unit(Unit.Type.TAX, tax));
        } else if (type.getCurrentContext().containsKey("exchangeRate")) {
            BigDecimal exchangeRate = new BigDecimal(type.getCurrentContext().get("exchangeRate"));
            Money txTax = Money.of(t.getCurrencyCode(), BigDecimal.valueOf(tax.getAmount()).multiply(exchangeRate).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
            t.addUnit(new Unit(Unit.Type.TAX, txTax));
            // update gross value if necessary
            if (!t.getCurrencyCode().equals(t.getSecurity().getCurrencyCode())) {
                Unit grossValue = t.getUnit(Unit.Type.GROSS_VALUE).get();
                t.removeUnit(grossValue);
                t.addUnit(new Unit(Unit.Type.GROSS_VALUE, grossValue.getAmount().subtract(txTax), grossValue.getForex().subtract(tax), grossValue.getExchangeRate()));
            }
        }
    }).section("tax", "currency").optional().match("abzgl. Kapitalertragsteuer .* (\\w{3}+) (?<tax>[\\d.]+,\\d+) (?<currency>\\w{3}+)$").assign((t, v) -> {
        Money tax = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("tax")));
        t.addUnit(new Unit(Unit.Type.TAX, tax));
    }).section("tax", "currency").optional().match("abzgl. Solidaritätszuschlag .* (\\w{3}+) (?<tax>[\\d.]+,\\d+) (?<currency>\\w{3}+)$").assign((t, v) -> {
        Money tax = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("tax")));
        t.addUnit(new Unit(Unit.Type.TAX, tax));
    }).wrap(t -> t.getAmount() != 0 ? new TransactionItem(t) : null));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Type(name.abuchen.portfolio.model.Transaction.Unit.Type) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) Optional(java.util.Optional) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) RoundingMode(java.math.RoundingMode) Money(name.abuchen.portfolio.money.Money) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) BigDecimal(java.math.BigDecimal)

Example 57 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class DABPDFExtractor method addSellTransaction.

@SuppressWarnings("nls")
private void addSellTransaction() {
    DocumentType type = new DocumentType("Verkauf");
    this.addDocumentTyp(type);
    Block block = new Block("^Verkauf .*$");
    type.addBlock(block);
    Transaction<BuySellEntry> pdfTransaction = new Transaction<>();
    pdfTransaction.subject(() -> {
        BuySellEntry entry = new BuySellEntry();
        entry.setType(PortfolioTransaction.Type.SELL);
        return entry;
    });
    block.set(pdfTransaction);
    // 
    pdfTransaction.section("isin", "name", "currency").find(// 
    "Gattungsbezeichnung ISIN").match("^(?<name>.*) (?<isin>[^ ]*)$").match("STK [\\d.]+(,\\d+)? (?<currency>\\w{3}+) ([\\d.]+,\\d+)$").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section(// 
    "shares").find(// 
    "Nominal Kurs").match("^STK (?<shares>[\\d.]+(,\\d+)?) (\\w{3}+) ([\\d.]+,\\d+)$").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("amount", "currency").optional().find("Wert Konto-Nr. Betrag zu Ihren Gunsten").match("^(\\d+.\\d+.\\d{4}+) ([0-9]*) (?<currency>\\w{3}+) (?<amount>[\\d.]+,\\d+)$").assign((t, v) -> {
        t.setAmount(asAmount(v.get("amount")));
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
    }).section("amount", "currency", "exchangeRate", "forex", "forexCurrency").optional().find(".* Ausmachender Betrag (?<forexCurrency>\\w{3}+) (?<forex>[\\d.]+,\\d+)").find("Wert Konto-Nr. Devisenkurs Betrag zu Ihren Gunsten").match("^(\\d+.\\d+.\\d{4}+) ([0-9]*) .../... (?<exchangeRate>[\\d.]+,\\d+) (?<currency>\\w{3}+) (?<amount>[\\d.]+,\\d+)$").assign((t, v) -> {
        Money amount = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("amount")));
        t.setMonetaryAmount(amount);
        BigDecimal exchangeRate = // 
        BigDecimal.ONE.divide(asExchangeRate(v.get("exchangeRate")), 10, BigDecimal.ROUND_HALF_DOWN);
        Money forex = Money.of(asCurrencyCode(v.get("forexCurrency")), asAmount(v.get("forex")));
        Unit grossValue = new Unit(Unit.Type.GROSS_VALUE, amount, forex, exchangeRate);
        t.getPortfolioTransaction().addUnit(grossValue);
    }).section(// 
    "date").match("^Handelstag (?<date>\\d+.\\d+.\\d{4}+) .*$").assign((t, v) -> t.setDate(asDate(v.get("date")))).section("fees", "currency").optional().match("^.*Provision (?<currency>\\w{3}+) (?<fees>[\\d.]+,\\d+)-$").assign((t, v) -> {
        String currency = asCurrencyCode(v.get("currency"));
        if (currency.equals(t.getAccountTransaction().getCurrencyCode())) {
            t.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, Money.of(currency, asAmount(v.get("fees")))));
        }
    }).wrap(t -> {
        if (t.getPortfolioTransaction().getAmount() == 0L)
            throw new IllegalArgumentException("No amount found");
        return new BuySellEntryItem(t);
    });
    addTaxesSectionsTransaction(type, pdfTransaction);
}
Also used : BigDecimal(java.math.BigDecimal) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Map(java.util.Map) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Money(name.abuchen.portfolio.money.Money) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) Unit(name.abuchen.portfolio.model.Transaction.Unit) BigDecimal(java.math.BigDecimal)

Example 58 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class DegiroPDFExtractor method addBankAccountTransactions.

@SuppressWarnings("nls")
private void addBankAccountTransactions() {
    DocumentType type = new DocumentType("Kontoauszug");
    this.addDocumentTyp(type);
    // 02-08-2017 00:00 Einzahlung EUR 350,00 EUR 350,00
    Block blockDeposit = new Block(".* Einzahlung .*");
    type.addBlock(blockDeposit);
    blockDeposit.set(new Transaction<AccountTransaction>().subject(() -> {
        AccountTransaction t = new AccountTransaction();
        t.setType(AccountTransaction.Type.DEPOSIT);
        return t;
    }).section("date", "currency", "amount").match("(?<date>\\d+-\\d+-\\d{4}) \\d+:\\d+ Einzahlung (?<currency>\\w{3}) (?<amount>[\\d.]+,\\d{2}) .*").assign((t, v) -> {
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
        t.setDateTime(asDate(v.get("date")));
        t.setAmount(asAmount(v.get("amount")));
    }).wrap(t -> new TransactionItem(t)));
    // 03-08-2017 15:32 BERKSHIRE HATHAWAY INC US0846707026 Währungswechsel USD 177,50 USD 0,00
    // (Einbuchung)
    // 03-08-2017 15:32 BERKSHIRE HATHAWAY INC US0846707026 Währungswechsel 1,1851 EUR -149,92 EUR 36,52
    // (Ausbuchung)
    // 03-08-2017 15:32 BERKSHIRE HATHAWAY INC US0846707026 Transaktionsgebühr EUR -0,50 EUR 186,44
    // 03-08-2017 15:32 BERKSHIRE HATHAWAY INC US0846707026 Kauf 1 zu je 177,5 USD -177,50 USD -177,50
    Block blockBuyForex = new Block(".*Einbuchung.*");
    type.addBlock(blockBuyForex);
    blockBuyForex.set(new Transaction<BuySellEntry>().subject(() -> {
        BuySellEntry entry = new BuySellEntry();
        entry.setType(PortfolioTransaction.Type.BUY);
        return entry;
    }).section("date", "isin", "name", "shares", "currency", "amount").match("(?<date>\\d+-\\d+-\\d{4} \\d+:\\d+) (?<name>.*) (?<isin>[^ ]+) Kauf (?<shares>[.\\d]+[,\\d]*) zu je [.\\d]+[,\\d]* (?<currency>\\w{3}) -(?<amount>[.\\d]+,\\d{2}) .*").assign((t, v) -> {
        t.setSecurity(getOrCreateSecurity(v));
        t.setDate(asDate(v.get("date")));
        t.setShares(asShares(v.get("shares")));
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
        t.setAmount(asAmount(v.get("amount")));
    }).section("exchangeRate", "currency", "amount").optional().match(".* Währungswechsel (?<exchangeRate>[.\\d]+,\\d+) (?<currency>\\w{3}) -(?<amount>[.\\d]+,\\d{2}) .*").assign((t, v) -> {
        // in fx
        Money currentMonetaryAmount = t.getAccountTransaction().getMonetaryAmount();
        // exchange rate in fx/EUR
        BigDecimal exchangeRate = asExchangeRate(v.get("exchangeRate"));
        BigDecimal accountMoneyValue = BigDecimal.valueOf(t.getAccountTransaction().getAmount()).divide(exchangeRate, // in EUR
        RoundingMode.HALF_DOWN);
        // "EUR"
        String currencyCode = asCurrencyCode(v.get("currency"));
        // in EUR
        Money accountMoney = Money.of(currencyCode, Math.round(accountMoneyValue.doubleValue()));
        // change total amount to amount in EUR
        // "EUR"
        t.setCurrencyCode(currencyCode);
        // in EUR
        t.setMonetaryAmount(accountMoney);
        // replace BRUTTO (which is in foreign currency) with the value in transaction currency
        BigDecimal inverseRate = BigDecimal.ONE.divide(exchangeRate, 10, BigDecimal.ROUND_HALF_DOWN);
        Unit grossValue = new Unit(Unit.Type.GROSS_VALUE, accountMoney, currentMonetaryAmount, inverseRate);
        t.getPortfolioTransaction().addUnit(grossValue);
        // the difference between the accountMoney and the amount after changing the currency
        // are the fees and taxes which had to be paid in Fx
        // in EUR
        Money feesAndTaxes = Money.of(currencyCode, asAmount(v.get("amount"))).subtract(accountMoney);
        BigDecimal feesAndTaxesFxValue = BigDecimal.valueOf(feesAndTaxes.getAmount()).multiply(// in USD
        exchangeRate);
        Money feesAndTaxesFx = Money.of(t.getPortfolioTransaction().getSecurity().getCurrencyCode(), // in USD
        Math.round(feesAndTaxesFxValue.doubleValue()));
        // add fee in fx currency and to total amount
        t.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, feesAndTaxes, feesAndTaxesFx, inverseRate));
        t.setMonetaryAmount(accountMoney.add(feesAndTaxes));
    }).section("fee", "currency").optional().match(".* Transaktionsgebühr (?<currency>\\w{3}) -(?<fee>[.\\d]+,\\d{2}) .*").assign((t, v) -> {
        // set fee
        Money feeAmount = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fee")));
        t.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, feeAmount));
        // add fee to total amount
        Money currentMonetaryAmount = t.getAccountTransaction().getMonetaryAmount();
        t.setMonetaryAmount(currentMonetaryAmount.add(feeAmount));
    }).wrap(t -> new BuySellEntryItem(t)));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) LocalDateTime(java.time.LocalDateTime) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) Locale(java.util.Locale) DateTimeFormatter(java.time.format.DateTimeFormatter) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) RoundingMode(java.math.RoundingMode) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Money(name.abuchen.portfolio.money.Money) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) BigDecimal(java.math.BigDecimal)

Example 59 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class DeutscheBankPDFExtractor method addSellTransaction.

@SuppressWarnings("nls")
private void addSellTransaction() {
    DocumentType type = new DocumentType("Verkauf von Wertpapieren");
    this.addDocumentTyp(type);
    Block block = new Block("Abrechnung: Verkauf von Wertpapieren");
    type.addBlock(block);
    block.set(new Transaction<BuySellEntry>().subject(() -> {
        BuySellEntry entry = new BuySellEntry();
        entry.setType(PortfolioTransaction.Type.SELL);
        return entry;
    }).section("wkn", "isin", "name", "currency").find(// 
    "Filialnummer Depotnummer Wertpapierbezeichnung Seite").match(// 
    "^.{15}(?<name>.*)$").match(// 
    "^WKN (?<wkn>[^ ]*) (.*)$").match(// 
    "^ISIN (?<isin>[^ ]*) Kurs (?<currency>\\w{3}+) (.*)$").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section(// 
    "shares").match("^WKN [^ ]* Nominal ST (?<shares>\\d+(,\\d+)?)").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("date", "amount", "currency").match("Buchung auf Kontonummer [\\d ]* mit Wertstellung (?<date>\\d+.\\d+.\\d{4}+) (?<currency>\\w{3}+) (?<amount>[\\d.]+,\\d+)").assign((t, v) -> {
        t.setDate(asDate(v.get("date")));
        t.setAmount(asAmount(v.get("amount")));
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
    }).section("tax", // 
    "currency").optional().match("Kapitalertragsteuer (?<currency>\\w{3}+) (?<tax>[\\d.-]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("tax")))))).section("soli", // 
    "currency").optional().match("Solidaritätszuschlag auf Kapitalertragsteuer (?<currency>\\w{3}+) (?<soli>[\\d.-]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("soli")))))).section("churchtax", // 
    "currency").optional().match("Kirchensteuer auf Kapitalertragsteuer (?<currency>\\w{3}+) (?<churchtax>[\\d.-]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("churchtax")))))).section("provision", // 
    "currency").optional().match("Provision.*(?<currency>\\w{3}+) -(?<provision>[\\d.]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("provision")))))).section("charges", // 
    "currency").optional().match("Fremde Spesen und Auslagen (?<currency>\\w{3}+) -(?<charges>[\\d.]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("charges")))))).section("additional", // 
    "currency").optional().match("Weitere Provision der Bank bei der börslichen Orderausf.*hrung (?<currency>\\w{3}+) -(?<additional>[\\d.]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("additional")))))).section("xetra", // 
    "currency").optional().match("XETRA-Kosten (?<currency>\\w{3}+) -(?<xetra>[\\d.]+,\\d+)").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("xetra")))))).wrap(t -> new BuySellEntryItem(t)));
}
Also used : BigDecimal(java.math.BigDecimal) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) MutableMoney(name.abuchen.portfolio.money.MutableMoney) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) Unit(name.abuchen.portfolio.model.Transaction.Unit)

Example 60 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class DeutscheBankPDFExtractor method addDividendTransaction.

@SuppressWarnings("nls")
private void addDividendTransaction(String nameOfTransaction) {
    DocumentType type = new DocumentType(nameOfTransaction);
    this.addDocumentTyp(type);
    Block block = new Block(nameOfTransaction);
    type.addBlock(block);
    block.set(new Transaction<AccountTransaction>().subject(() -> {
        AccountTransaction transaction = new AccountTransaction();
        transaction.setType(AccountTransaction.Type.DIVIDENDS);
        return transaction;
    }).section("wkn", "isin", "name", // 
    "currency").find(// 
    "Stück WKN ISIN").match(// 
    "(\\d+,\\d*) (?<wkn>\\S*) (?<isin>\\S*)").match(// 
    "^(?<name>.*)$").match(// 
    "Bruttoertrag ([\\d.]+,\\d+) (?<currency>\\w{3}+).*").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section(// 
    "shares").match("(?<shares>\\d+,\\d*) (\\S*) (\\S*)").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("date", "amount", "currency").match("Gutschrift mit Wert (?<date>\\d+.\\d+.\\d{4}+) (?<amount>[\\d.]+,\\d+) (?<currency>\\w{3}+)").assign((t, v) -> {
        t.setDateTime(asDate(v.get("date")));
        t.setAmount(asAmount(v.get("amount")));
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
    }).section("grossValue", // 
    "currency").optional().match("Bruttoertrag (?<grossValue>[\\d.]+,\\d+) (?<currency>\\w{3}+)").assign((t, v) -> {
        Money grossValue = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("grossValue")));
        // calculating taxes as the difference between gross
        // value and transaction amount
        Money taxes = MutableMoney.of(t.getCurrencyCode()).add(grossValue).subtract(t.getMonetaryAmount()).toMoney();
        if (!taxes.isZero())
            t.addUnit(new Unit(Unit.Type.TAX, taxes));
    }).section("forexSum", "forexCurrency", "grossValue", "currency", // 
    "exchangeRate").optional().match("Bruttoertrag (?<forexSum>[\\d.]+,\\d+) (?<forexCurrency>\\w{3}+) (?<grossValue>[\\d.]+,\\d+) (?<currency>\\w{3}+)").match(// 
    "Umrechnungskurs (\\w{3}+) zu (\\w{3}+) (?<exchangeRate>[\\d.]+,\\d+)").assign((t, v) -> {
        Money grossValue = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("grossValue")));
        Money forex = Money.of(asCurrencyCode(v.get("forexCurrency")), asAmount(v.get("forexSum")));
        BigDecimal exchangeRate = // 
        BigDecimal.ONE.divide(asExchangeRate(v.get("exchangeRate")), 10, BigDecimal.ROUND_HALF_DOWN);
        Unit unit = new Unit(Unit.Type.GROSS_VALUE, grossValue, forex, exchangeRate);
        // security actually matches
        if (unit.getForex().getCurrencyCode().equals(t.getSecurity().getCurrencyCode()))
            t.addUnit(unit);
        // calculating taxes as the difference between gross
        // value and transaction amount
        Money taxes = MutableMoney.of(t.getCurrencyCode()).add(grossValue).subtract(t.getMonetaryAmount()).toMoney();
        if (!taxes.isZero())
            t.addUnit(new Unit(Unit.Type.TAX, taxes));
    }).wrap(t -> new TransactionItem(t)));
}
Also used : BigDecimal(java.math.BigDecimal) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) MutableMoney(name.abuchen.portfolio.money.MutableMoney) Money(name.abuchen.portfolio.money.Money) MutableMoney(name.abuchen.portfolio.money.MutableMoney) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) BigDecimal(java.math.BigDecimal)

Aggregations

Unit (name.abuchen.portfolio.model.Transaction.Unit)75 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)58 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)51 Money (name.abuchen.portfolio.money.Money)51 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)50 Client (name.abuchen.portfolio.model.Client)50 IOException (java.io.IOException)44 Block (name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block)37 DocumentType (name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType)37 Transaction (name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction)37 BigDecimal (java.math.BigDecimal)32 Security (name.abuchen.portfolio.model.Security)26 Matcher (java.util.regex.Matcher)24 Pattern (java.util.regex.Pattern)24 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)24 Test (org.junit.Test)24 Map (java.util.Map)23 Values (name.abuchen.portfolio.money.Values)18 ArrayList (java.util.ArrayList)13 LocalDateTime (java.time.LocalDateTime)12