Search in sources :

Example 1 with Type

use of name.abuchen.portfolio.model.PortfolioTransaction.Type in project portfolio by buchen.

the class CSVPortfolioTransactionExtractor method extract.

@Override
void extract(List<Item> items, String[] rawValues, Map<String, Column> field2column) throws ParseException {
    // if forex gross amount is available then assume that is the currency
    // of the security to be created
    // check if we have a security
    Security security = getSecurity(rawValues, field2column, s -> {
        String currency = getText(Messages.CSVColumn_CurrencyGrossAmount, rawValues, field2column);
        if (currency == null || currency.isEmpty())
            currency = getText(Messages.CSVColumn_TransactionCurrency, rawValues, field2column);
        if (currency != null) {
            CurrencyUnit unit = CurrencyUnit.getInstance(currency.trim());
            s.setCurrencyCode(unit == null ? getClient().getBaseCurrency() : unit.getCurrencyCode());
        }
    });
    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);
    // check for the transaction amount
    Money amount = getMoney(rawValues, field2column);
    // determine type (if not explicitly given by import)
    Type type = inferType(rawValues, field2column, amount);
    // determine remaining fields
    LocalDateTime date = getDate(Messages.CSVColumn_Date, rawValues, field2column);
    if (date == null)
        throw new ParseException(MessageFormat.format(Messages.CSVImportMissingField, Messages.CSVColumn_Date), 0);
    Long shares = getShares(Messages.CSVColumn_Shares, rawValues, field2column);
    Long fees = getAmount(Messages.CSVColumn_Fees, rawValues, field2column);
    Long taxes = getAmount(Messages.CSVColumn_Taxes, rawValues, field2column);
    String note = getText(Messages.CSVColumn_Note, rawValues, field2column);
    Unit grossAmount = extractGrossAmount(rawValues, field2column, amount);
    switch(type) {
        case BUY:
        case SELL:
            items.add(createBuySell(rawValues, field2column, type, security, amount, fees, taxes, date, note, shares, grossAmount));
            break;
        case TRANSFER_IN:
        case TRANSFER_OUT:
            items.add(createTransfer(security, amount, fees, taxes, date, note, shares, grossAmount));
            break;
        case DELIVERY_INBOUND:
        case DELIVERY_OUTBOUND:
            items.add(createDelivery(rawValues, field2column, type, security, amount, fees, taxes, date, note, shares, grossAmount));
            break;
        default:
            throw new IllegalArgumentException(type.toString());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Money(name.abuchen.portfolio.money.Money) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Type(name.abuchen.portfolio.model.PortfolioTransaction.Type) ParseException(java.text.ParseException) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) StringJoiner(java.util.StringJoiner)

Example 2 with Type

use of name.abuchen.portfolio.model.PortfolioTransaction.Type in project portfolio by buchen.

the class InsertActionTest method testPortfolioTransactionAttributes.

@Test
public void testPortfolioTransactionAttributes() throws IntrospectionException {
    // This test is only a "marker" that fails if the PortfolioTransaction
    // is structurally changed. If it is changed, then the ImportAction
    // needs to change too.
    BeanInfo info = Introspector.getBeanInfo(PortfolioTransaction.class);
    Set<String> properties = Arrays.stream(info.getPropertyDescriptors()).filter(p -> p.getWriteMethod() != null).map(p -> p.getName()).collect(Collectors.toSet());
    assertThat(properties, hasItem("security"));
    assertThat(properties, hasItem("monetaryAmount"));
    assertThat(properties, hasItem("currencyCode"));
    assertThat(properties, hasItem("amount"));
    assertThat(properties, hasItem("shares"));
    assertThat(properties, hasItem("dateTime"));
    assertThat(properties, hasItem("type"));
    assertThat(properties, hasItem("note"));
    assertThat(properties.size(), is(8));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) Collectors(java.util.stream.Collectors) IntrospectionException(java.beans.IntrospectionException) Assert.assertThat(org.junit.Assert.assertThat) Introspector(java.beans.Introspector) IsCollectionContaining.hasItem(org.hamcrest.core.IsCollectionContaining.hasItem) Unit(name.abuchen.portfolio.model.Transaction.Unit) BeanInfo(java.beans.BeanInfo) Type(name.abuchen.portfolio.model.PortfolioTransaction.Type) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) BeanInfo(java.beans.BeanInfo) Test(org.junit.Test)

Aggregations

LocalDateTime (java.time.LocalDateTime)2 Type (name.abuchen.portfolio.model.PortfolioTransaction.Type)2 Security (name.abuchen.portfolio.model.Security)2 Unit (name.abuchen.portfolio.model.Transaction.Unit)2 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)2 Money (name.abuchen.portfolio.money.Money)2 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 Introspector (java.beans.Introspector)1 ParseException (java.text.ParseException)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 StringJoiner (java.util.StringJoiner)1 Collectors (java.util.stream.Collectors)1 Account (name.abuchen.portfolio.model.Account)1 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)1 Client (name.abuchen.portfolio.model.Client)1 Portfolio (name.abuchen.portfolio.model.Portfolio)1 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1