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);
}
}
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());
}
}
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;
}
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));
}
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));
}
Aggregations