Search in sources :

Example 6 with Account

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

the class DetectDuplicatesActionTest method account.

private Account account(AccountTransaction t) {
    Account a = new Account();
    a.setCurrencyCode(t.getCurrencyCode());
    a.addTransaction(t);
    return a;
}
Also used : Account(name.abuchen.portfolio.model.Account)

Example 7 with Account

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

the class ClientIndex method collectTransferalsAndTaxes.

private void collectTransferalsAndTaxes(Interval interval) {
    for (Account account : getClient().getAccounts()) {
        // 
        account.getTransactions().stream().filter(t -> !t.getDateTime().toLocalDate().isBefore(interval.getStart()) && !t.getDateTime().toLocalDate().isAfter(interval.getEnd())).forEach(t -> {
            // NOSONAR
            LocalDate d = t.getDateTime().toLocalDate();
            switch(t.getType()) {
                case DEPOSIT:
                    addValue(inboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case REMOVAL:
                    addValue(outboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case TAXES:
                    addValue(taxes, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case TAX_REFUND:
                    addValue(taxes, t.getCurrencyCode(), -t.getAmount(), interval, d);
                    break;
                case DIVIDENDS:
                    addValue(taxes, t.getCurrencyCode(), t.getUnitSum(Unit.Type.TAX).getAmount(), interval, d);
                    addValue(dividends, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case INTEREST:
                    addValue(interest, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case INTEREST_CHARGE:
                    addValue(interest, t.getCurrencyCode(), -t.getAmount(), interval, d);
                    addValue(interestCharge, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                default:
                    // do nothing
                    break;
            }
        });
    }
    for (Portfolio portfolio : getClient().getPortfolios()) {
        // 
        portfolio.getTransactions().stream().filter(t -> !t.getDateTime().toLocalDate().isBefore(interval.getStart()) && !t.getDateTime().toLocalDate().isAfter(interval.getEnd())).forEach(t -> {
            LocalDate d = t.getDateTime().toLocalDate();
            // collect taxes
            addValue(// 
            taxes, // 
            t.getCurrencyCode(), // 
            t.getUnitSum(Unit.Type.TAX).getAmount(), interval, d);
            // collect transferals
            switch(t.getType()) {
                case DELIVERY_INBOUND:
                    addValue(inboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                case DELIVERY_OUTBOUND:
                    addValue(outboundTransferals, t.getCurrencyCode(), t.getAmount(), interval, d);
                    break;
                default:
                    break;
            }
        });
    }
}
Also used : Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) FormatStyle(java.time.format.FormatStyle) Messages(name.abuchen.portfolio.Messages) MessageFormat(java.text.MessageFormat) List(java.util.List) Dates(name.abuchen.portfolio.util.Dates) Unit(name.abuchen.portfolio.model.Transaction.Unit) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Interval(name.abuchen.portfolio.util.Interval) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) LocalDate(java.time.LocalDate)

Example 8 with Account

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

the class ClientPerformanceSnapshot method addEarnings.

private void addEarnings() {
    MutableMoney mEarnings = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mOtherEarnings = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mFees = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mTaxes = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mDeposits = MutableMoney.of(converter.getTermCurrency());
    MutableMoney mRemovals = MutableMoney.of(converter.getTermCurrency());
    Map<Security, MutableMoney> earningsBySecurity = new HashMap<>();
    for (Account account : client.getAccounts()) {
        for (AccountTransaction t : account.getTransactions()) {
            if (!period.containsTransaction().test(t))
                continue;
            switch(t.getType()) {
                case DIVIDENDS:
                case INTEREST:
                    addEarningTransaction(account, t, mEarnings, mOtherEarnings, mTaxes, earningsBySecurity);
                    break;
                case INTEREST_CHARGE:
                    Money charged = t.getMonetaryAmount().with(converter.at(t.getDateTime()));
                    mEarnings.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    earnings.add(new TransactionPair<AccountTransaction>(account, t));
                    mOtherEarnings.subtract(charged);
                    break;
                case DEPOSIT:
                    mDeposits.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case REMOVAL:
                    mRemovals.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case FEES:
                    mFees.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    fees.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case FEES_REFUND:
                    mFees.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    fees.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case TAXES:
                    mTaxes.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    taxes.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case TAX_REFUND:
                    mTaxes.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    taxes.add(new TransactionPair<AccountTransaction>(account, t));
                    break;
                case BUY:
                case SELL:
                case TRANSFER_IN:
                case TRANSFER_OUT:
                    // no operation
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }
    }
    for (Portfolio portfolio : client.getPortfolios()) {
        for (PortfolioTransaction t : portfolio.getTransactions()) {
            if (!period.containsTransaction().test(t))
                continue;
            Money unit = t.getUnitSum(Unit.Type.FEE, converter);
            if (!unit.isZero()) {
                mFees.add(unit);
                fees.add(new TransactionPair<PortfolioTransaction>(portfolio, t));
            }
            unit = t.getUnitSum(Unit.Type.TAX, converter);
            if (!unit.isZero()) {
                mTaxes.add(unit);
                taxes.add(new TransactionPair<PortfolioTransaction>(portfolio, t));
            }
            switch(t.getType()) {
                case DELIVERY_INBOUND:
                    mDeposits.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case DELIVERY_OUTBOUND:
                    mRemovals.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
                    break;
                case BUY:
                case SELL:
                case TRANSFER_IN:
                case TRANSFER_OUT:
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }
    }
    Category earningsCategory = categories.get(CategoryType.EARNINGS);
    earningsCategory.valuation = mEarnings.toMoney();
    earningsCategory.positions = earningsBySecurity.entrySet().stream().filter(entry -> !entry.getValue().isZero()).map(entry -> new Position(entry.getKey(), entry.getValue().toMoney())).sorted(// 
    (p1, p2) -> p1.getLabel().compareToIgnoreCase(p2.getLabel())).collect(Collectors.toList());
    if (!mOtherEarnings.isZero())
        earningsCategory.positions.add(new Position(Messages.LabelInterest, mOtherEarnings.toMoney()));
    categories.get(CategoryType.FEES).valuation = mFees.toMoney();
    categories.get(CategoryType.TAXES).valuation = mTaxes.toMoney();
    categories.get(CategoryType.TRANSFERS).valuation = mDeposits.toMoney().subtract(mRemovals.toMoney());
    categories.get(CategoryType.TRANSFERS).positions.add(new Position(Messages.LabelDeposits, mDeposits.toMoney()));
    categories.get(CategoryType.TRANSFERS).positions.add(new Position(Messages.LabelRemovals, mRemovals.toMoney()));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Account(name.abuchen.portfolio.model.Account) EnumMap(java.util.EnumMap) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionPair(name.abuchen.portfolio.model.TransactionPair) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Messages(name.abuchen.portfolio.Messages) MutableMoney(name.abuchen.portfolio.money.MutableMoney) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Unit(name.abuchen.portfolio.model.Transaction.Unit) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) MoneyCollectors(name.abuchen.portfolio.money.MoneyCollectors) LocalDate(java.time.LocalDate) Map(java.util.Map) Collections(java.util.Collections) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) HashMap(java.util.HashMap) Portfolio(name.abuchen.portfolio.model.Portfolio) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) Money(name.abuchen.portfolio.money.Money) MutableMoney(name.abuchen.portfolio.money.MutableMoney) MutableMoney(name.abuchen.portfolio.money.MutableMoney) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction)

Example 9 with Account

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

the class ClientClassificationFilter method addTransferT.

private void addTransferT(CalculationState state, Account inboundAccount, AccountTransaction t) {
    Account outboundAccount = (Account) t.getCrossEntry().getCrossOwner(t);
    int inboundWeight = state.getWeight(inboundAccount);
    int outboundWeight = state.getWeight(outboundAccount);
    if (inboundWeight == outboundWeight && inboundWeight == Classification.ONE_HUNDRED_PERCENT) {
        state.asReadOnly(inboundAccount).internalAddTransaction(t);
        state.asReadOnly(outboundAccount).internalAddTransaction((AccountTransaction) t.getCrossEntry().getCrossTransaction(t));
    } else if (inboundWeight == outboundWeight) {
        AccountTransferEntry entry = createTransferEntry((AccountTransferEntry) t.getCrossEntry(), inboundWeight);
        // entry#insert does not work with ReadOnlyAccount
        state.asReadOnly(inboundAccount).internalAddTransaction(entry.getTargetTransaction());
        state.asReadOnly(outboundAccount).internalAddTransaction(entry.getSourceTransaction());
    } else if (inboundWeight < outboundWeight) {
        AccountTransferEntry entry = createTransferEntry((AccountTransferEntry) t.getCrossEntry(), inboundWeight);
        // entry#insert does not work with ReadOnlyAccount
        state.asReadOnly(inboundAccount).internalAddTransaction(entry.getTargetTransaction());
        state.asReadOnly(outboundAccount).internalAddTransaction(entry.getSourceTransaction());
        AccountTransaction ot = (AccountTransaction) t.getCrossEntry().getCrossTransaction(t);
        state.asReadOnly(outboundAccount).internalAddTransaction(new AccountTransaction(ot.getDateTime(), ot.getCurrencyCode(), value(ot.getAmount(), outboundWeight - inboundWeight), null, AccountTransaction.Type.REMOVAL));
    } else // inboundWeight > outboundWeight
    {
        AccountTransferEntry entry = createTransferEntry((AccountTransferEntry) t.getCrossEntry(), outboundWeight);
        // entry#insert does not work with ReadOnlyAccount
        state.asReadOnly(inboundAccount).internalAddTransaction(entry.getTargetTransaction());
        state.asReadOnly(outboundAccount).internalAddTransaction(entry.getSourceTransaction());
        state.asReadOnly(inboundAccount).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), value(t.getAmount(), inboundWeight - outboundWeight), null, AccountTransaction.Type.DEPOSIT));
    }
}
Also used : Account(name.abuchen.portfolio.model.Account) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 10 with Account

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

the class ClientClassificationFilter method addBuySellT.

private void addBuySellT(CalculationState state, Portfolio portfolio, PortfolioTransaction t) {
    int securityWeight = state.getWeight(t.getSecurity());
    long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), securityWeight);
    long securityAmount = value(t.getAmount(), securityWeight);
    securityAmount = t.getType() == PortfolioTransaction.Type.BUY ? securityAmount - taxes : securityAmount + taxes;
    Account account = (Account) t.getCrossEntry().getCrossOwner(t);
    int accountWeight = state.getWeight(account);
    long accountAmount = value(t.getAmount(), accountWeight);
    long commonAmount = Math.min(securityAmount, accountAmount);
    int commonWeight = (int) Math.round(((double) commonAmount / (double) securityAmount) * securityWeight);
    // create a buy/sell transactions with the amount shared by the account
    // assignment and the security assignment
    BuySellEntry copy = new BuySellEntry(state.asReadOnly(portfolio), state.account2readonly.get(account));
    copy.setDate(t.getDateTime());
    copy.setCurrencyCode(t.getCurrencyCode());
    copy.setSecurity(t.getSecurity());
    copy.setType(t.getType());
    copy.setNote(t.getNote());
    copy.setShares(value(t.getShares(), commonWeight));
    copy.setAmount(commonAmount);
    // copy all units (except for taxes) over to the new transaction
    t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.getPortfolioTransaction().addUnit(value(u, commonWeight)));
    state.asReadOnly(portfolio).internalAddTransaction(copy.getPortfolioTransaction());
    state.asReadOnly(account).internalAddTransaction(copy.getAccountTransaction());
    if (accountAmount - commonAmount > 0) {
        AccountTransaction ta = new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), accountAmount - commonAmount, null, t.getType() == PortfolioTransaction.Type.BUY ? AccountTransaction.Type.REMOVAL : AccountTransaction.Type.DEPOSIT);
        state.asReadOnly(account).internalAddTransaction(ta);
    }
    if (securityAmount - commonAmount > 0) {
        PortfolioTransaction tp = new PortfolioTransaction();
        tp.setDateTime(t.getDateTime());
        tp.setCurrencyCode(t.getCurrencyCode());
        tp.setSecurity(t.getSecurity());
        tp.setShares(value(t.getShares(), securityWeight - commonWeight));
        tp.setType(t.getType() == PortfolioTransaction.Type.BUY ? PortfolioTransaction.Type.DELIVERY_INBOUND : PortfolioTransaction.Type.DELIVERY_OUTBOUND);
        tp.setAmount(securityAmount - commonAmount);
        t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> tp.addUnit(value(u, securityWeight - commonWeight)));
        state.asReadOnly(portfolio).internalAddTransaction(tp);
    }
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Set(java.util.Set) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Classification(name.abuchen.portfolio.model.Classification) Visitor(name.abuchen.portfolio.model.Taxonomy.Visitor) HashSet(java.util.HashSet) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Assignment(name.abuchen.portfolio.model.Classification.Assignment) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Aggregations

Account (name.abuchen.portfolio.model.Account)75 Security (name.abuchen.portfolio.model.Security)39 Client (name.abuchen.portfolio.model.Client)38 Portfolio (name.abuchen.portfolio.model.Portfolio)37 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)35 Test (org.junit.Test)31 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)25 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)22 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)21 ArrayList (java.util.ArrayList)17 LocalDate (java.time.LocalDate)16 AccountBuilder (name.abuchen.portfolio.AccountBuilder)14 Unit (name.abuchen.portfolio.model.Transaction.Unit)14 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)13 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)12 Money (name.abuchen.portfolio.money.Money)12 Collections (java.util.Collections)11 List (java.util.List)11 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)11 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)11