use of name.abuchen.portfolio.model.TransactionOwner 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.TransactionOwner in project portfolio by buchen.
the class BuySellModel method applyChanges.
@Override
public void applyChanges() {
if (security == null)
throw new UnsupportedOperationException(Messages.MsgMissingSecurity);
if (account == null)
throw new UnsupportedOperationException(Messages.MsgMissingReferenceAccount);
BuySellEntry entry;
if (source != null && source.getOwner(source.getPortfolioTransaction()).equals(portfolio) && source.getOwner(source.getAccountTransaction()).equals(account)) {
entry = source;
} else {
if (source != null) {
@SuppressWarnings("unchecked") TransactionOwner<Transaction> owner = (TransactionOwner<Transaction>) source.getOwner(source.getPortfolioTransaction());
owner.deleteTransaction(source.getPortfolioTransaction(), client);
source = null;
}
entry = new BuySellEntry(portfolio, account);
entry.setCurrencyCode(account.getCurrencyCode());
entry.insert();
}
entry.setDate(LocalDateTime.of(date, time));
entry.setCurrencyCode(account.getCurrencyCode());
entry.setSecurity(security);
entry.setShares(shares);
entry.setAmount(total);
entry.setType(type);
entry.setNote(note);
writeToTransaction(entry.getPortfolioTransaction());
}
use of name.abuchen.portfolio.model.TransactionOwner in project portfolio by buchen.
the class PerformanceView method addAccountColumn.
private void addAccountColumn(ShowHideColumnHelper support) {
Column column = new Column(Messages.ColumnAccount, SWT.LEFT, 100);
Function<Object, Account> getAccount = element -> {
TransactionPair<?> pair = (TransactionPair<?>) element;
if (pair.getOwner() instanceof Account)
return (Account) pair.getOwner();
CrossEntry crossEntry = pair.getTransaction().getCrossEntry();
if (crossEntry == null)
return null;
TransactionOwner<?> other = crossEntry.getCrossOwner(pair.getTransaction());
return other instanceof Account ? ((Account) other) : null;
};
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
Account account = getAccount.apply(element);
return account != null ? account.getName() : null;
}
@Override
public Image getImage(Object element) {
Account account = getAccount.apply(element);
return account != null ? Images.ACCOUNT.image() : null;
}
});
column.setSorter(ColumnViewerSorter.create(e -> {
Account account = getAccount.apply(e);
return account != null ? account.getName() : null;
}));
support.addColumn(column);
}
use of name.abuchen.portfolio.model.TransactionOwner 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.TransactionOwner in project portfolio by buchen.
the class SecurityTransferModel method applyChanges.
@Override
public void applyChanges() {
if (security == null)
throw new UnsupportedOperationException(Messages.MsgMissingSecurity);
if (sourcePortfolio == null)
throw new UnsupportedOperationException(Messages.MsgPortfolioFromMissing);
if (targetPortfolio == null)
throw new UnsupportedOperationException(Messages.MsgPortfolioToMissing);
PortfolioTransferEntry t;
if (source != null && sourcePortfolio.equals(source.getOwner(source.getSourceTransaction())) && targetPortfolio.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 PortfolioTransferEntry(sourcePortfolio, targetPortfolio);
t.insert();
}
t.setSecurity(security);
t.setDate(LocalDateTime.of(date, time));
t.setShares(shares);
t.setAmount(amount);
t.setCurrencyCode(security.getCurrencyCode());
t.setNote(note);
}
Aggregations