Search in sources :

Example 6 with AccountTransferEntry

use of name.abuchen.portfolio.model.AccountTransferEntry in project portfolio by buchen.

the class AccountTransferModel method applyChanges.

@Override
public void applyChanges() {
    if (sourceAccount == null)
        throw new UnsupportedOperationException(Messages.MsgAccountFromMissing);
    if (targetAccount == null)
        throw new UnsupportedOperationException(Messages.MsgAccountToMissing);
    AccountTransferEntry t;
    if (source != null && sourceAccount.equals(source.getOwner(source.getSourceTransaction())) && targetAccount.equals(source.getOwner(source.getTargetTransaction()))) {
        // transaction stays in same accounts
        t = source;
    } else {
        if (source != null) {
            @SuppressWarnings("unchecked") TransactionOwner<Transaction> owner = (TransactionOwner<Transaction>) source.getOwner(source.getSourceTransaction());
            owner.deleteTransaction(source.getSourceTransaction(), client);
            source = null;
        }
        t = new AccountTransferEntry(sourceAccount, targetAccount);
        t.getSourceTransaction().setCurrencyCode(sourceAccount.getCurrencyCode());
        t.getTargetTransaction().setCurrencyCode(targetAccount.getCurrencyCode());
        t.insert();
    }
    t.setDate(date.atStartOfDay());
    t.setNote(note);
    // if source and target account have the same currencies, no forex data
    // needs to be stored
    AccountTransaction sourceTransaction = t.getSourceTransaction();
    sourceTransaction.clearUnits();
    if (sourceAccount.getCurrencyCode().equals(targetAccount.getCurrencyCode())) {
        sourceTransaction.setAmount(amount);
        t.getTargetTransaction().setAmount(amount);
    } else {
        // TODO improve naming of fields: the source amount is called
        // 'fxAmount' while the target amount is just called 'amount' but
        // then the source account holds the 'forex' which is switched
        sourceTransaction.setAmount(fxAmount);
        t.getTargetTransaction().setAmount(amount);
        Transaction.Unit forex = new // 
        Transaction.Unit(// 
        Transaction.Unit.Type.GROSS_VALUE, // 
        Money.of(sourceAccount.getCurrencyCode(), fxAmount), // 
        Money.of(targetAccount.getCurrencyCode(), amount), getInverseExchangeRate());
        sourceTransaction.addUnit(forex);
    }
}
Also used : Transaction(name.abuchen.portfolio.model.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner)

Example 7 with AccountTransferEntry

use of name.abuchen.portfolio.model.AccountTransferEntry 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 8 with AccountTransferEntry

use of name.abuchen.portfolio.model.AccountTransferEntry in project portfolio by buchen.

the class TransactionCurrencyCheck method execute.

@Override
public List<Issue> execute(Client client) {
    Set<Object> transactions = new HashSet<Object>();
    for (Account account : client.getAccounts()) {
        account.getTransactions().stream().filter(t -> t.getCurrencyCode() == null).forEach(t -> transactions.add(t.getCrossEntry() != null ? t.getCrossEntry() : new TransactionPair<AccountTransaction>(account, t)));
    }
    for (Portfolio portfolio : client.getPortfolios()) {
        portfolio.getTransactions().stream().filter(t -> t.getCurrencyCode() == null).forEach(t -> transactions.add(t.getCrossEntry() != null ? t.getCrossEntry() : new TransactionPair<PortfolioTransaction>(portfolio, t)));
    }
    List<Issue> issues = new ArrayList<Issue>();
    for (Object t : transactions) {
        if (t instanceof TransactionPair<?>) {
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = (TransactionPair<Transaction>) t;
            issues.add(new TransactionMissingCurrencyIssue(client, pair));
        } else if (t instanceof BuySellEntry) {
            // attempt to fix it if both currencies are identical. If a fix
            // involves currency conversion plus exchange rates, just offer
            // to delete the transaction.
            BuySellEntry entry = (BuySellEntry) t;
            String accountCurrency = entry.getAccount().getCurrencyCode();
            String securityCurrency = entry.getPortfolioTransaction().getSecurity().getCurrencyCode();
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = new TransactionPair<Transaction>((TransactionOwner<Transaction>) entry.getOwner(entry.getAccountTransaction()), entry.getAccountTransaction());
            issues.add(new TransactionMissingCurrencyIssue(client, pair, Objects.equals(accountCurrency, securityCurrency)));
        } else if (t instanceof AccountTransferEntry) {
            // same story as with purchases: only offer to fix if currencies
            // match
            AccountTransferEntry entry = (AccountTransferEntry) t;
            String sourceCurrency = entry.getSourceAccount().getCurrencyCode();
            String targetCurrency = entry.getTargetAccount().getCurrencyCode();
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = new TransactionPair<Transaction>((TransactionOwner<Transaction>) entry.getOwner(entry.getSourceTransaction()), entry.getSourceTransaction());
            issues.add(new TransactionMissingCurrencyIssue(client, pair, Objects.equals(sourceCurrency, targetCurrency)));
        } else if (t instanceof PortfolioTransferEntry) {
            // transferring a security involves no currency change because
            // the currency is defined the security itself
            PortfolioTransferEntry entry = (PortfolioTransferEntry) t;
            @SuppressWarnings("unchecked") TransactionPair<Transaction> pair = new TransactionPair<Transaction>((TransactionOwner<Transaction>) entry.getOwner(entry.getSourceTransaction()), entry.getSourceTransaction());
            issues.add(new TransactionMissingCurrencyIssue(client, pair));
        } else {
            throw new IllegalArgumentException();
        }
    }
    return issues;
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionPair(name.abuchen.portfolio.model.TransactionPair) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Set(java.util.Set) QuickFix(name.abuchen.portfolio.checks.QuickFix) Messages(name.abuchen.portfolio.Messages) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Issue(name.abuchen.portfolio.checks.Issue) HashSet(java.util.HashSet) Objects(java.util.Objects) Check(name.abuchen.portfolio.checks.Check) List(java.util.List) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner) LocalDate(java.time.LocalDate) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) TransactionPair(name.abuchen.portfolio.model.TransactionPair) Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Issue(name.abuchen.portfolio.checks.Issue) Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) HashSet(java.util.HashSet) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry)

Example 9 with AccountTransferEntry

use of name.abuchen.portfolio.model.AccountTransferEntry in project portfolio by buchen.

the class PortfolioClientFilterTest method testCrossAccountTransfersAreConvertedIfAccountIsNotIncluded.

@Test
public void testCrossAccountTransfersAreConvertedIfAccountIsNotIncluded() {
    Account accountA = client.getAccounts().get(0);
    Account accountB = client.getAccounts().get(1);
    AccountTransferEntry entry = new AccountTransferEntry(accountA, accountB);
    entry.setDate(LocalDateTime.parse("2016-04-01T00:00"));
    entry.setAmount(Values.Amount.factorize(10));
    entry.setCurrencyCode(accountA.getCurrencyCode());
    entry.insert();
    Client result = new PortfolioClientFilter(Collections.emptyList(), Arrays.asList(accountA)).filter(client);
    assertThat(result.getPortfolios(), empty());
    assertThat(result.getAccounts().size(), is(1));
    Account account = result.getAccounts().get(0);
    // check that the 4 transactions are transformed:
    // - buy -> removal
    // - deposit
    // - dividend -> deposit
    // - transfer -> removal
    assertThat(account.getTransactions().size(), is(4));
    assertThat(// 
    account.getTransactions().stream().filter(// 
    t -> t.getType() == AccountTransaction.Type.REMOVAL).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-02-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    account.getTransactions().stream().filter(// 
    t -> t.getType() == AccountTransaction.Type.DEPOSIT).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-01-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    account.getTransactions().stream().filter(// 
    t -> t.getType() == AccountTransaction.Type.DEPOSIT).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-03-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    account.getTransactions().stream().filter(// 
    t -> t.getType() == AccountTransaction.Type.REMOVAL).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
    // check other account
    result = new PortfolioClientFilter(Collections.emptyList(), Arrays.asList(accountB)).filter(client);
    assertThat(// 
    result.getAccounts().get(0).getTransactions().stream().filter(// 
    t -> t.getType() == AccountTransaction.Type.DEPOSIT).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Account(name.abuchen.portfolio.model.Account) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 10 with AccountTransferEntry

use of name.abuchen.portfolio.model.AccountTransferEntry in project portfolio by buchen.

the class ReviewExtractedItemsPage method addColumns.

private void addColumns(TableViewer viewer, TableColumnLayout layout) {
    TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
    column.getColumn().setText(Messages.ColumnStatus);
    column.setLabelProvider(new // NOSONAR
    FormattedLabelProvider() {

        @Override
        public Image getImage(ExtractedEntry element) {
            Images image = null;
            switch(element.getMaxCode()) {
                case WARNING:
                    image = Images.WARNING;
                    break;
                case ERROR:
                    image = Images.ERROR;
                    break;
                case OK:
                default:
            }
            return image != null ? image.image() : null;
        }

        @Override
        public String getText(ExtractedEntry entry) {
            // $NON-NLS-1$
            return "";
        }
    });
    layout.setColumnData(column.getColumn(), new ColumnPixelData(22, true));
    column = new TableViewerColumn(viewer, SWT.NONE);
    column.getColumn().setText(Messages.ColumnDate);
    column.setLabelProvider(new // NOSONAR
    FormattedLabelProvider() {

        @Override
        public String getText(ExtractedEntry entry) {
            LocalDateTime date = entry.getItem().getDate();
            return date != null ? Values.DateTime.format(date) : null;
        }
    });
    layout.setColumnData(column.getColumn(), new ColumnPixelData(80, true));
    column = new TableViewerColumn(viewer, SWT.NONE);
    column.getColumn().setText(Messages.ColumnTransactionType);
    column.setLabelProvider(new // NOSONAR
    FormattedLabelProvider() {

        @Override
        public String getText(ExtractedEntry entry) {
            return entry.getItem().getTypeInformation();
        }

        @Override
        public Image getImage(ExtractedEntry entry) {
            Annotated subject = entry.getItem().getSubject();
            if (subject instanceof AccountTransaction)
                return Images.ACCOUNT.image();
            else if (subject instanceof PortfolioTransaction)
                return Images.PORTFOLIO.image();
            else if (subject instanceof Security)
                return Images.SECURITY.image();
            else if (subject instanceof BuySellEntry)
                return Images.PORTFOLIO.image();
            else if (subject instanceof AccountTransferEntry)
                return Images.ACCOUNT.image();
            else if (subject instanceof PortfolioTransferEntry)
                return Images.PORTFOLIO.image();
            else
                return null;
        }
    });
    layout.setColumnData(column.getColumn(), new ColumnPixelData(100, true));
    column = new TableViewerColumn(viewer, SWT.RIGHT);
    column.getColumn().setText(Messages.ColumnAmount);
    column.setLabelProvider(new // NOSONAR
    FormattedLabelProvider() {

        @Override
        public String getText(ExtractedEntry entry) {
            Money amount = entry.getItem().getAmount();
            return amount != null ? Values.Money.format(amount) : null;
        }
    });
    layout.setColumnData(column.getColumn(), new ColumnPixelData(80, true));
    column = new TableViewerColumn(viewer, SWT.RIGHT);
    column.getColumn().setText(Messages.ColumnShares);
    column.setLabelProvider(new // NOSONAR
    FormattedLabelProvider() {

        @Override
        public String getText(ExtractedEntry entry) {
            return Values.Share.formatNonZero(entry.getItem().getShares());
        }
    });
    layout.setColumnData(column.getColumn(), new ColumnPixelData(80, true));
    column = new TableViewerColumn(viewer, SWT.NONE);
    column.getColumn().setText(Messages.ColumnSecurity);
    column.setLabelProvider(new // NOSONAR
    FormattedLabelProvider() {

        @Override
        public String getText(ExtractedEntry entry) {
            Security security = entry.getItem().getSecurity();
            return security != null ? security.getName() : null;
        }
    });
    layout.setColumnData(column.getColumn(), new ColumnPixelData(250, true));
}
Also used : LocalDateTime(java.time.LocalDateTime) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Images(name.abuchen.portfolio.ui.Images) ColumnPixelData(org.eclipse.jface.viewers.ColumnPixelData) StyledString(org.eclipse.jface.viewers.StyledString) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Image(org.eclipse.swt.graphics.Image) Security(name.abuchen.portfolio.model.Security) Annotated(name.abuchen.portfolio.model.Annotated) Money(name.abuchen.portfolio.money.Money) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry)

Aggregations

AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)10 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)9 Account (name.abuchen.portfolio.model.Account)5 Client (name.abuchen.portfolio.model.Client)5 LocalDateTime (java.time.LocalDateTime)4 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)4 PortfolioTransferEntry (name.abuchen.portfolio.model.PortfolioTransferEntry)4 Security (name.abuchen.portfolio.model.Security)4 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)4 Money (name.abuchen.portfolio.money.Money)4 Test (org.junit.Test)4 LocalDate (java.time.LocalDate)3 AccountBuilder (name.abuchen.portfolio.AccountBuilder)3 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)3 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)3 Portfolio (name.abuchen.portfolio.model.Portfolio)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2