Search in sources :

Example 1 with TransactionOwner

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);
    }
}
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 2 with TransactionOwner

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());
}
Also used : BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner)

Example 3 with TransactionOwner

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);
}
Also used : Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) TableViewer(org.eclipse.jface.viewers.TableViewer) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) ToolBar(org.eclipse.swt.widgets.ToolBar) ColumnPixelData(org.eclipse.jface.viewers.ColumnPixelData) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner) ESelectionService(org.eclipse.e4.ui.workbench.modeling.ESelectionService) Composite(org.eclipse.swt.widgets.Composite) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) Account(name.abuchen.portfolio.model.Account) CTabFolder(org.eclipse.swt.custom.CTabFolder) ExchangeRateProviderFactory(name.abuchen.portfolio.money.ExchangeRateProviderFactory) ClientPerformanceSnapshot(name.abuchen.portfolio.snapshot.ClientPerformanceSnapshot) Security(name.abuchen.portfolio.model.Security) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) ShowHideColumnHelper(name.abuchen.portfolio.ui.util.viewers.ShowHideColumnHelper) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) SWT(org.eclipse.swt.SWT) TreeViewer(org.eclipse.jface.viewers.TreeViewer) SecuritySelection(name.abuchen.portfolio.ui.selection.SecuritySelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) GroupEarningsByAccount(name.abuchen.portfolio.snapshot.GroupEarningsByAccount) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Values(name.abuchen.portfolio.money.Values) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) Images(name.abuchen.portfolio.ui.Images) Image(org.eclipse.swt.graphics.Image) CrossEntry(name.abuchen.portfolio.model.CrossEntry) JFaceResources(org.eclipse.jface.resource.JFaceResources) Function(java.util.function.Function) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) MessageFormat(java.text.MessageFormat) Inject(javax.inject.Inject) Messages(name.abuchen.portfolio.ui.Messages) Font(org.eclipse.swt.graphics.Font) Portfolio(name.abuchen.portfolio.model.Portfolio) ClientFilterDropDown(name.abuchen.portfolio.ui.util.ClientFilterDropDown) Viewer(org.eclipse.jface.viewers.Viewer) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionPair(name.abuchen.portfolio.model.TransactionPair) SimpleAction(name.abuchen.portfolio.ui.util.SimpleAction) Column(name.abuchen.portfolio.ui.util.viewers.Column) ColumnViewerSorter(name.abuchen.portfolio.ui.util.viewers.ColumnViewerSorter) TreeViewerCSVExporter(name.abuchen.portfolio.ui.util.TreeViewerCSVExporter) AbstractDropDown(name.abuchen.portfolio.ui.util.AbstractDropDown) TableViewerCSVExporter(name.abuchen.portfolio.ui.util.TableViewerCSVExporter) CTabItem(org.eclipse.swt.custom.CTabItem) Unit(name.abuchen.portfolio.model.Transaction.Unit) IMenuManager(org.eclipse.jface.action.IMenuManager) TreeColumnLayout(org.eclipse.jface.layout.TreeColumnLayout) Control(org.eclipse.swt.widgets.Control) TransactionPair(name.abuchen.portfolio.model.TransactionPair) Account(name.abuchen.portfolio.model.Account) GroupEarningsByAccount(name.abuchen.portfolio.snapshot.GroupEarningsByAccount) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) Column(name.abuchen.portfolio.ui.util.viewers.Column) CrossEntry(name.abuchen.portfolio.model.CrossEntry) Image(org.eclipse.swt.graphics.Image) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner)

Example 4 with TransactionOwner

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;
}
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 5 with TransactionOwner

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);
}
Also used : Transaction(name.abuchen.portfolio.model.Transaction) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry)

Aggregations

Transaction (name.abuchen.portfolio.model.Transaction)6 TransactionOwner (name.abuchen.portfolio.model.TransactionOwner)6 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)4 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)4 Account (name.abuchen.portfolio.model.Account)3 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)3 Portfolio (name.abuchen.portfolio.model.Portfolio)3 PortfolioTransferEntry (name.abuchen.portfolio.model.PortfolioTransferEntry)3 TransactionPair (name.abuchen.portfolio.model.TransactionPair)3 MessageFormat (java.text.MessageFormat)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Inject (javax.inject.Inject)2 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)2 Client (name.abuchen.portfolio.model.Client)2 Security (name.abuchen.portfolio.model.Security)2 Unit (name.abuchen.portfolio.model.Transaction.Unit)2 CurrencyConverterImpl (name.abuchen.portfolio.money.CurrencyConverterImpl)2 ExchangeRateProviderFactory (name.abuchen.portfolio.money.ExchangeRateProviderFactory)2