Search in sources :

Example 66 with AccountTransaction

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

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

the class ClientClassificationFilter method addSecurityRelatedAccountT.

private void addSecurityRelatedAccountT(CalculationState state, Account account, AccountTransaction t) {
    int accountWeight = state.getWeight(account);
    int securityWeight = state.getWeight(t.getSecurity());
    long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), securityWeight);
    long amount = value(t.getAmount(), securityWeight);
    state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), amount + taxes, t.getSecurity(), t.getType()));
    long accountAmount = value(t.getAmount(), accountWeight);
    long delta = accountAmount - amount - taxes;
    if (delta != 0) {
        AccountTransaction.Type deltaType = delta > 0 ^ t.getType().isDebit() ? AccountTransaction.Type.DEPOSIT : AccountTransaction.Type.REMOVAL;
        state.asReadOnly(account).internalAddTransaction(new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), Math.abs(delta), null, deltaType));
    }
}
Also used : AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 68 with AccountTransaction

use of name.abuchen.portfolio.model.AccountTransaction 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)

Example 69 with AccountTransaction

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

the class PortfolioClientFilter method convertTo.

private AccountTransaction convertTo(AccountTransaction t, AccountTransaction.Type type) {
    AccountTransaction clone = new AccountTransaction();
    clone.setType(type);
    clone.setDateTime(t.getDateTime());
    clone.setCurrencyCode(t.getCurrencyCode());
    // no security for REMOVAL or DEPOSIT
    clone.setSecurity(null);
    clone.setAmount(t.getAmount());
    clone.setShares(t.getShares());
    // do *not* copy units as REMOVAL and DEPOSIT have never units
    return clone;
}
Also used : AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 70 with AccountTransaction

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

the class AccountPerformanceTaxRefundTestCase method testAccountPerformanceTaxRefund.

/**
 * Feature: when calculating the performance of an account, do include taxes
 * and tax refunds but only those that are not paid for a security.
 */
@Test
public void testAccountPerformanceTaxRefund() throws IOException {
    Client client = ClientFactory.load(SecurityTestCase.class.getResourceAsStream("account_performance_tax_refund.xml"));
    Account account = client.getAccounts().get(0);
    ReportingPeriod period = new ReportingPeriod.FromXtoY(LocalDate.parse("2013-12-06"), LocalDate.parse("2014-12-06"));
    AccountTransaction deposit = account.getTransactions().get(0);
    // no changes in holdings, ttwror must be:
    double startValue = deposit.getAmount();
    double endValue = account.getCurrentAmount(LocalDateTime.of(2016, 1, 1, 10, 00));
    double ttwror = (endValue / startValue) - 1;
    List<Exception> warnings = new ArrayList<>();
    CurrencyConverter converter = new TestCurrencyConverter();
    PerformanceIndex accountPerformance = PerformanceIndex.forAccount(client, converter, account, period, warnings);
    assertThat(warnings, empty());
    double calculatedTtwror = accountPerformance.getFinalAccumulatedPercentage();
    assertThat(calculatedTtwror, closeTo(ttwror, 0.0001));
    // if the tax_refund is for a security, it must not be included in the
    // performance of the account
    AccountTransaction taxRefund = account.getTransactions().get(1);
    assertThat(taxRefund.getType(), is(AccountTransaction.Type.TAX_REFUND));
    taxRefund.setSecurity(new Security());
    accountPerformance = PerformanceIndex.forAccount(client, converter, account, period, warnings);
    assertThat(warnings, empty());
    assertThat(accountPerformance.getFinalAccumulatedPercentage(), lessThan(calculatedTtwror));
}
Also used : Account(name.abuchen.portfolio.model.Account) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) IOException(java.io.IOException) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Aggregations

AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)218 Client (name.abuchen.portfolio.model.Client)169 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)152 Money (name.abuchen.portfolio.money.Money)148 IOException (java.io.IOException)141 Unit (name.abuchen.portfolio.model.Transaction.Unit)135 Test (org.junit.Test)133 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)131 Security (name.abuchen.portfolio.model.Security)129 ArrayList (java.util.ArrayList)125 Values (name.abuchen.portfolio.money.Values)117 List (java.util.List)110 LocalDateTime (java.time.LocalDateTime)109 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)103 CoreMatchers.is (org.hamcrest.CoreMatchers.is)102 Assert.assertThat (org.junit.Assert.assertThat)102 Item (name.abuchen.portfolio.datatransfer.Extractor.Item)99 SecurityItem (name.abuchen.portfolio.datatransfer.Extractor.SecurityItem)99 TransactionItem (name.abuchen.portfolio.datatransfer.Extractor.TransactionItem)99 Optional (java.util.Optional)87