Search in sources :

Example 31 with Unit

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

the class SBrokerPDFExtractor 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").find(// 
    "Gattungsbezeichnung ISIN").match("(?<name>.*) Inhaber-Anteile (?<isin>.*)").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section("date", "amount", // 
    "currency").find("Wert Konto-Nr. Betrag zu Ihren Lasten").match(// 
    "(?<date>\\d+.\\d+.\\d{4}) \\d{2}/\\d{4}/\\d{3} (?<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(// 
    "shares").match(// 
    "^STK (?<shares>\\d+,\\d+?) .*").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("fee", "currency").optional().match(// 
    ".* Orderentgelt (?<currency>\\w{3}+) (?<fee>[\\d.]+,\\d+)-").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fee")))))).section("fee", "currency").optional().match(// 
    ".* Börsengebühr (?<currency>\\w{3}+) (?<fee>[\\d.]+,\\d+)-").assign((t, v) -> t.getPortfolioTransaction().addUnit(new // 
    Unit(// 
    Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fee")))))).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) 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) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) 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)

Example 32 with Unit

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

the class UnicreditPDFExtractor method addBuyTransaction.

@SuppressWarnings("nls")
private void addBuyTransaction() {
    DocumentType type = new DocumentType("UniCredit");
    this.addDocumentTyp(type);
    Block block = new Block("W e r t p a p i e r - A b r e c h n u n g K a u f .*");
    type.addBlock(block);
    block.set(new Transaction<BuySellEntry>().subject(() -> {
        BuySellEntry entry = new BuySellEntry();
        entry.setType(PortfolioTransaction.Type.BUY);
        return entry;
    }).section("name", "wkn", "isin").find("Nennbetrag Wertpapierbezeichnung Wertpapierkennnummer/ISIN").match("^(?<name>.*) (?<wkn>[^ ]*)$").match("^.* (?<isin>[A-Z]{2}[A-Z\\d]{9}\\d)$").assign((t, v) -> t.setSecurity(getOrCreateSecurity(v))).section("shares").match("^ST *(?<shares>[\\.\\d]+[,\\d]*)$").assign((t, v) -> t.setShares(asShares(v.get("shares")))).section("amount", "currency").match("^Belastung.* (?<currency>\\w{3}) (?<amount>[\\d.]+,\\d{2})$").assign((t, v) -> {
        t.setCurrencyCode(asCurrencyCode(v.get("currency")));
        t.setAmount(asAmount(v.get("amount")));
    }).section("date").match("^Valuta (?<date>\\d+\\.\\d+\\.\\d{4}) .*").assign((t, v) -> t.setDate(asDate(v.get("date")))).section("fee", "currency").optional().match("^Brokerkommission.* (?<currency>\\w{3}) (?<fee>[\\d.]+,\\d{2})$").assign((t, v) -> t.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fee")))))).section("fee", "currency").optional().match("^Transaktionsentgelt.* (?<currency>\\w{3}) (?<fee>[\\d.]+,\\d{2})$").assign((t, v) -> t.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fee")))))).section("fee", "currency").optional().match("^Provision.* (?<currency>\\w{3}) (?<fee>[\\d.]+,\\d{2})$").assign((t, v) -> t.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fee")))))).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) Transaction(name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction) Unit(name.abuchen.portfolio.model.Transaction.Unit) DocumentType(name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType) IOException(java.io.IOException) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) 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 33 with Unit

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

the class CSVAccountTransactionExtractor method extract.

@Override
void extract(List<Item> items, String[] rawValues, Map<String, Column> field2column) throws ParseException {
    // check if we have a security
    Security security = getSecurity(rawValues, field2column, s -> s.setCurrencyCode(getCurrencyCode(Messages.CSVColumn_TransactionCurrency, rawValues, field2column)));
    // check for the transaction amount
    Money amount = getMoney(rawValues, field2column);
    // determine type (if not explicitly given by import)
    Type type = inferType(rawValues, field2column, security, amount);
    // extract remaining fields
    LocalDateTime date = getDate(Messages.CSVColumn_Date, rawValues, field2column);
    if (date == null)
        throw new ParseException(MessageFormat.format(Messages.CSVImportMissingField, Messages.CSVColumn_Date), 0);
    String note = getText(Messages.CSVColumn_Note, rawValues, field2column);
    Long shares = getShares(Messages.CSVColumn_Shares, rawValues, field2column);
    Long taxes = getAmount(Messages.CSVColumn_Taxes, rawValues, field2column);
    switch(type) {
        case TRANSFER_IN:
        case TRANSFER_OUT:
            AccountTransferEntry entry = new AccountTransferEntry();
            entry.setAmount(Math.abs(amount.getAmount()));
            entry.setCurrencyCode(amount.getCurrencyCode());
            entry.setDate(date);
            entry.setNote(note);
            items.add(new AccountTransferItem(entry, type == Type.TRANSFER_OUT));
            break;
        case BUY:
        case SELL:
            if (security == null)
                throw new ParseException(MessageFormat.format(Messages.CSVImportMissingSecurity, // $NON-NLS-1$
                new StringJoiner(", ").add(Messages.CSVColumn_ISIN).add(Messages.CSVColumn_TickerSymbol).add(Messages.CSVColumn_WKN).toString()), 0);
            if (shares == null)
                throw new ParseException(MessageFormat.format(Messages.CSVImportMissingField, Messages.CSVColumn_Shares), 0);
            BuySellEntry buySellEntry = new BuySellEntry();
            buySellEntry.setType(PortfolioTransaction.Type.valueOf(type.name()));
            buySellEntry.setAmount(Math.abs(amount.getAmount()));
            buySellEntry.setShares(Math.abs(shares));
            buySellEntry.setCurrencyCode(amount.getCurrencyCode());
            buySellEntry.setSecurity(security);
            buySellEntry.setDate(date);
            buySellEntry.setNote(note);
            items.add(new BuySellEntryItem(buySellEntry));
            break;
        case DIVIDENDS:
            if (security == null)
                throw new ParseException(MessageFormat.format(Messages.CSVImportMissingSecurity, // $NON-NLS-1$
                new StringJoiner(", ").add(Messages.CSVColumn_ISIN).add(Messages.CSVColumn_TickerSymbol).add(Messages.CSVColumn_WKN).toString()), 0);
        case DEPOSIT:
        case TAXES:
        case TAX_REFUND:
        case FEES:
        case FEES_REFUND:
        case INTEREST:
        case INTEREST_CHARGE:
        case REMOVAL:
            AccountTransaction t = new AccountTransaction();
            t.setType(type);
            t.setAmount(Math.abs(amount.getAmount()));
            t.setCurrencyCode(amount.getCurrencyCode());
            if (type == Type.DIVIDENDS || type == Type.TAX_REFUND)
                t.setSecurity(security);
            t.setDateTime(date);
            t.setNote(note);
            if (shares != null && type == Type.DIVIDENDS)
                t.setShares(Math.abs(shares));
            if (type == Type.DIVIDENDS && taxes != null && taxes.longValue() != 0)
                t.addUnit(new Unit(Unit.Type.TAX, Money.of(t.getCurrencyCode(), Math.abs(taxes))));
            items.add(new TransactionItem(t));
            break;
        default:
            throw new IllegalArgumentException(type.toString());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Money(name.abuchen.portfolio.money.Money) Type(name.abuchen.portfolio.model.AccountTransaction.Type) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) ParseException(java.text.ParseException) StringJoiner(java.util.StringJoiner)

Example 34 with Unit

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

the class CSVExporter method exportPortfolioTransactions.

public void exportPortfolioTransactions(File file, Portfolio portfolio) throws IOException {
    try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
        CSVPrinter printer = new CSVPrinter(writer);
        printer.setStrategy(STRATEGY);
        printer.println(new String[] { // 
        Messages.CSVColumn_Date, // 
        Messages.CSVColumn_Type, // 
        Messages.CSVColumn_Value, // 
        Messages.CSVColumn_TransactionCurrency, // 
        Messages.CSVColumn_GrossAmount, // 
        Messages.CSVColumn_CurrencyGrossAmount, // 
        Messages.CSVColumn_ExchangeRate, // 
        Messages.CSVColumn_Fees, // 
        Messages.CSVColumn_Taxes, // 
        Messages.CSVColumn_Shares, // 
        Messages.CSVColumn_ISIN, // 
        Messages.CSVColumn_WKN, // 
        Messages.CSVColumn_TickerSymbol, // 
        Messages.CSVColumn_SecurityName, Messages.CSVColumn_Note });
        for (PortfolioTransaction t : portfolio.getTransactions()) {
            printer.print(t.getDateTime().toString());
            printer.print(t.getType().toString());
            printer.print(Values.Amount.format(t.getType().isLiquidation() ? -t.getAmount() : t.getAmount()));
            printer.print(t.getCurrencyCode());
            // gross amount
            Optional<Unit> grossAmount = t.getUnit(Unit.Type.GROSS_VALUE);
            if (grossAmount.isPresent()) {
                Money forex = grossAmount.get().getForex();
                printer.print(Values.Amount.format(forex.getAmount()));
                printer.print(forex.getCurrencyCode());
                printer.print(Values.ExchangeRate.format(grossAmount.get().getExchangeRate()));
            } else {
                // $NON-NLS-1$
                printer.print("");
                // $NON-NLS-1$
                printer.print("");
                // $NON-NLS-1$
                printer.print("");
            }
            printer.print(Values.Amount.format(t.getUnitSum(Unit.Type.FEE).getAmount()));
            printer.print(Values.Amount.format(t.getUnitSum(Unit.Type.TAX).getAmount()));
            printer.print(Values.Share.format(t.getShares()));
            printSecurityInfo(printer, t);
            printer.print(escapeNull(t.getNote()));
            printer.println();
        }
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Money(name.abuchen.portfolio.money.Money) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Unit(name.abuchen.portfolio.model.Transaction.Unit) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 35 with Unit

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

the class CSVPortfolioTransactionExtractor method extractGrossAmount.

private Unit extractGrossAmount(String[] rawValues, Map<String, Column> field2column, Money amount) throws ParseException {
    Long grossAmount = getAmount(Messages.CSVColumn_GrossAmount, rawValues, field2column);
    String currencyCode = getCurrencyCode(Messages.CSVColumn_CurrencyGrossAmount, rawValues, field2column);
    BigDecimal exchangeRate = getBigDecimal(Messages.CSVColumn_ExchangeRate, rawValues, field2column);
    // amount currency equals the transaction currency, no unit is created
    if (currencyCode == null || amount.getCurrencyCode().equals(currencyCode))
        return null;
    // if no gross amount is given at all, no unit
    if (grossAmount == null || grossAmount.longValue() == 0)
        return null;
    // if no exchange rate is available, not unit to create
    if (exchangeRate == null || exchangeRate.compareTo(BigDecimal.ZERO) == 0)
        return null;
    Money forex = Money.of(currencyCode, Math.abs(grossAmount.longValue()));
    BigDecimal grossAmountConverted = exchangeRate.multiply(BigDecimal.valueOf(grossAmount));
    Money converted = Money.of(amount.getCurrencyCode(), Math.round(grossAmountConverted.doubleValue()));
    return new Unit(Unit.Type.GROSS_VALUE, converted, forex, exchangeRate);
}
Also used : Money(name.abuchen.portfolio.money.Money) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) 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