Search in sources :

Example 16 with Unit

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

the class ComdirectPDFExtractor method addSellTransaction.

@SuppressWarnings("nls")
private void addSellTransaction() {
    DocumentType type = new DocumentType("Wertpapierverkauf");
    this.addDocumentTyp(type);
    Block block = new Block("^(\\* )?Wertpapierverkauf *.*");
    type.addBlock(block);
    Transaction<BuySellEntry> pdfTransaction = new Transaction<BuySellEntry>().subject(() -> {
        BuySellEntry entry = new BuySellEntry();
        entry.setType(PortfolioTransaction.Type.SELL);
        return entry;
    }).section(// 
    "date").match(// 
    "Geschäftstag *: (?<date>\\d+.\\d+.\\d{4}+) .*").assign((t, v) -> t.setDate(asDate(v.get("date")))).section("isin", "name", // 
    "wkn").find(// 
    "Wertpapier-Bezeichnung *WPKNR/ISIN *").match(// 
    "^(?<name>(\\S{1,} )*) *(?<wkn>\\S*) *$").match(// 
    "(\\S{1,} )* *(?<isin>\\S*) *$").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section("shares").optional().match(// 
    "^St\\. *(?<shares>[\\d.]+(,\\d+)?) .*").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("shares").optional().match(// 
    "^ Summe *St\\. *(?<shares>[\\d.]+(,\\d+)?) .*").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("amount", // 
    "currency").find(// 
    ".*Zu Ihren Gunsten vor Steuern *").match(// 
    ".* \\d+.\\d+.\\d{4}+ *(?<currency>\\w{3}+) *(?<amount>[\\d.]+,\\d+).*").assign((t, v) -> {
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
        t.setAmount(asAmount(v.get("amount")));
    }).section("tax").optional().match(// 
    "^a *b *g *e *f *ü *h *r *t *e *S *t *e *u *e *r *n *(?<tax>.*)$").assign((t, v) -> {
        Unit unit = createTaxUnit(v.get("tax"));
        if (unit == null || unit.getAmount().isZero())
            return;
        t.getPortfolioTransaction().addUnit(unit);
        MutableMoney total = MutableMoney.of(t.getPortfolioTransaction().getCurrencyCode());
        total.add(t.getPortfolioTransaction().getMonetaryAmount());
        total.subtract(unit.getAmount());
        t.setMonetaryAmount(total.toMoney());
    }).wrap(BuySellEntryItem::new);
    addFeesSection(pdfTransaction);
    block.set(pdfTransaction);
    addTaxRefunds(type, "^(\\* )?Wertpapierverkauf *.*");
}
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) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) Messages(name.abuchen.portfolio.Messages) MutableMoney(name.abuchen.portfolio.money.MutableMoney) Matcher(java.util.regex.Matcher) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Pattern(java.util.regex.Pattern) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) MutableMoney(name.abuchen.portfolio.money.MutableMoney) 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) Unit(name.abuchen.portfolio.model.Transaction.Unit)

Example 17 with Unit

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

the class ComdirectPDFExtractor method createTaxUnit.

@SuppressWarnings("nls")
private Unit createTaxUnit(String taxString) {
    String tax = taxString.replaceAll("[_ ]*", "");
    Pattern pattern = Pattern.compile("(?<currency>\\w{3}+)-?(?<amount>[\\d.]+,\\d+)");
    Matcher matcher = pattern.matcher(tax);
    if (!matcher.matches())
        return null;
    return new Unit(Unit.Type.TAX, Money.of(asCurrencyCode(matcher.group("currency")), asAmount(matcher.group("amount"))));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Unit(name.abuchen.portfolio.model.Transaction.Unit)

Example 18 with Unit

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

the class ComdirectPDFExtractor method addTaxRefunds.

@SuppressWarnings("nls")
private void addTaxRefunds(DocumentType type, String blockMarker) {
    // tax refunds --> separate transaction
    Block block = new Block(blockMarker);
    type.addBlock(block);
    block.set(new Transaction<AccountTransaction>().subject(() -> {
        AccountTransaction t = new AccountTransaction();
        t.setType(AccountTransaction.Type.TAX_REFUND);
        return t;
    }).section(// 
    "date").match(// 
    "Geschäftstag *: (?<date>\\d+.\\d+.\\d{4}+) .*").assign((t, v) -> t.setDateTime(asDate(v.get("date")))).section("isin", "name", // 
    "wkn").find(// 
    "Wertpapier-Bezeichnung *WPKNR/ISIN *").match(// 
    "^(?<name>(\\S{1,} )*) *(?<wkn>\\S*) *$").match(// 
    "(\\S{1,} )* *(?<isin>\\S*) *$").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section("tax").optional().match(// 
    "^e *r *s *t *a *t *t *e *t *e *S *t *e *u *e *r *n *(?<tax>.*)$").assign((t, v) -> {
        Unit unit = createTaxUnit(v.get("tax"));
        if (unit == null || unit.getAmount().isZero())
            return;
        t.setMonetaryAmount(unit.getAmount());
    }).wrap(t -> t.getAmount() == 0L ? null : new TransactionItem(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) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) Messages(name.abuchen.portfolio.Messages) MutableMoney(name.abuchen.portfolio.money.MutableMoney) Matcher(java.util.regex.Matcher) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Pattern(java.util.regex.Pattern) Block(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Unit(name.abuchen.portfolio.model.Transaction.Unit)

Example 19 with Unit

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

the class ConsorsbankPDFExtractor method newDividendTransaction.

@SuppressWarnings("nls")
private Transaction<AccountTransaction> newDividendTransaction(DocumentType type) {
    return new Transaction<AccountTransaction>().subject(() -> {
        AccountTransaction t = new AccountTransaction();
        t.setType(AccountTransaction.Type.DIVIDENDS);
        return t;
    }).section("amount", // 
    "currency").match(// 
    "BRUTTO *(?<currency>\\w{3}+) *(?<amount>[\\d.]+,\\d+) *").assign((t, v) -> {
        t.setAmount(asAmount(v.get("amount")));
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
    }).section("wkn", "name", // 
    "shares").match(// 
    "ST *(?<shares>[\\d.]+(,\\d+)?) *WKN: *(?<wkn>\\S*) *").match(// 
    "^(?<name>.*)$").assign((t, v) -> {
        // reuse currency from transaction when creating a
        // new security upon import
        v.put("currency", t.getCurrencyCode());
        t.setSecurity(getOrCreateSecurity(v));
        t.setShares(asShares(v.get("shares")));
    }).section("rate", "amount", "currency").optional().match(// 
    "UMGER.ZUM DEV.-KURS *(?<rate>[\\d.]+,\\d+) *(?<currency>\\w{3}+) *(?<amount>[\\d.]+,\\d+) *").assign((t, v) -> {
        Money currentMonetaryAmount = t.getMonetaryAmount();
        BigDecimal rate = asExchangeRate(v.get("rate"));
        type.getCurrentContext().put("exchangeRate", rate.toPlainString());
        BigDecimal accountMoneyValue = BigDecimal.valueOf(t.getAmount()).divide(rate, RoundingMode.HALF_DOWN);
        String currencyCode = asCurrencyCode(v.get("currency"));
        t.setMonetaryAmount(Money.of(currencyCode, asAmount(v.get("amount"))));
        // currencies -> add gross value
        if (!t.getCurrencyCode().equals(t.getSecurity().getCurrencyCode())) {
            Money accountMoney = Money.of(currencyCode, Math.round(accountMoneyValue.doubleValue()));
            // replace BRUTTO (which is in foreign currency)
            // with the value in transaction currency
            BigDecimal inverseRate = BigDecimal.ONE.divide(rate, 10, BigDecimal.ROUND_HALF_DOWN);
            Unit grossValue = new Unit(Unit.Type.GROSS_VALUE, accountMoney, currentMonetaryAmount, inverseRate);
            t.addUnit(grossValue);
        }
    }).section("kapst", "currency").optional().match("KAPST .*(?<currency>\\w{3}+) *(?<kapst>[\\d.]+,\\d+) *").assign((t, v) -> t.addUnit(new Unit(Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("kapst")))))).section("solz", "currency").optional().match(// 
    "SOLZ .*(?<currency>\\w{3}+) *(?<solz>[\\d.]+,\\d+) *").assign((t, v) -> {
        String currency = asCurrencyCode(v.get("currency"));
        if (currency.equals(t.getCurrencyCode())) {
            t.addUnit(new Unit(Unit.Type.TAX, Money.of(asCurrencyCode(currency), asAmount(v.get("solz")))));
        }
    }).section("qust", "currency", "forexcurrency", "forex").optional().match("QUST [\\d.]+,\\d+ *% *(?<currency>\\w{3}+) *(?<qust>[\\d.]+,\\d+) *(?<forexcurrency>\\w{3}+) *(?<forex>[\\d.]+,\\d+) *").assign((t, v) -> {
        Optional<Unit> grossValueOption = t.getUnit(Type.GROSS_VALUE);
        Money money = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("qust")));
        if (grossValueOption.isPresent()) {
            Money forex = Money.of(asCurrencyCode(v.get("forexcurrency")), asAmount(v.get("forex")));
            t.addUnit(new Unit(Unit.Type.TAX, money, forex, grossValueOption.get().getExchangeRate()));
        } else {
            t.addUnit(new Unit(Unit.Type.TAX, money));
        }
    }).section(// 
    "date").match("WERT (?<date>\\d+.\\d+.\\d{4}+).*").assign((t, v) -> t.setDateTime(asDate(v.get("date")))).section("currency", "amount").optional().match("WERT \\d+.\\d+.\\d{4}+ *(?<currency>\\w{3}+) *(?<amount>[\\d.]+,\\d+) *").assign((t, v) -> {
        String currencyCode = asCurrencyCode(v.get("currency"));
        Money money = Money.of(currencyCode, asAmount(v.get("amount")));
        t.setMonetaryAmount(money);
    }).section("currency", "forexpenses").optional().match(// 
    "FREMDE SPESEN *(?<currency>\\w{3}+) *(?<forexpenses>[\\d.]+,\\d+) *").assign((t, v) -> {
        Optional<Unit> grossValueOption = t.getUnit(Type.GROSS_VALUE);
        long forexAmount = asAmount(v.get("forexpenses"));
        if (grossValueOption.isPresent()) {
            BigDecimal exchangeRate = grossValueOption.get().getExchangeRate();
            long convertedMoney = Math.round(exchangeRate.multiply(BigDecimal.valueOf(forexAmount)).doubleValue());
            Money money = Money.of(t.getCurrencyCode(), convertedMoney);
            Money forex = Money.of(asCurrencyCode(v.get("currency")), forexAmount);
            t.addUnit(new Unit(Unit.Type.TAX, money, forex, grossValueOption.get().getExchangeRate()));
        } else {
            BigDecimal exchangeRate = new BigDecimal(type.getCurrentContext().get("exchangeRate"));
            long convertedMoney = BigDecimal.valueOf(forexAmount).divide(exchangeRate, RoundingMode.HALF_UP).longValue();
            Money money = Money.of(t.getCurrencyCode(), convertedMoney);
            t.addUnit(new Unit(Unit.Type.TAX, money));
        }
    }).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) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Optional(java.util.Optional) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) BigDecimal(java.math.BigDecimal)

Example 20 with Unit

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

the class DABPDFExtractor method addBuyTransaction.

@SuppressWarnings("nls")
private void addBuyTransaction() {
    DocumentType type = new DocumentType("Kauf");
    this.addDocumentTyp(type);
    Block block = new Block("^Kauf .*$");
    type.addBlock(block);
    block.set(new Transaction<BuySellEntry>().subject(() -> {
        BuySellEntry entry = new BuySellEntry();
        entry.setType(PortfolioTransaction.Type.BUY);
        return entry;
    }).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 Lasten").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 Lasten").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"));
        // FIXME forex fees must update gross value
        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);
    }));
}
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)

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