Search in sources :

Example 1 with Type

use of name.abuchen.portfolio.model.AccountTransaction.Type 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)

Aggregations

ParseException (java.text.ParseException)1 LocalDateTime (java.time.LocalDateTime)1 StringJoiner (java.util.StringJoiner)1 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)1 Type (name.abuchen.portfolio.model.AccountTransaction.Type)1 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)1 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)1 Security (name.abuchen.portfolio.model.Security)1 Unit (name.abuchen.portfolio.model.Transaction.Unit)1 Money (name.abuchen.portfolio.money.Money)1