Search in sources :

Example 6 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CheckCurrenciesBuySellEntryTest method testBuySellEntry.

@Test
public void testBuySellEntry() {
    Account account = new Account();
    account.setCurrencyCode("EUR");
    Security security = new Security();
    security.setCurrencyCode("USD");
    Portfolio portfolio = new Portfolio();
    BuySellEntry entry = new BuySellEntry();
    entry.setType(PortfolioTransaction.Type.BUY);
    entry.setSecurity(security);
    entry.setMonetaryAmount(Money.of("EUR", 100_00));
    entry.getPortfolioTransaction().addUnit(new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 80_00), Money.of("USD", 100_00), BigDecimal.valueOf(0.8)));
    assertThat(action.process(entry, account, portfolio).getCode(), is(Status.Code.OK));
}
Also used : Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Example 7 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CheckCurrenciesPortfolioTransactionTest method testTransactionHasGrossValueMatchingSecurityCurrency.

@Test
public void testTransactionHasGrossValueMatchingSecurityCurrency() {
    Portfolio portfolio = new Portfolio();
    Security security = new Security("", "USD");
    Unit unit = new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 1_00), Money.of("USD", 2_00), BigDecimal.valueOf(0.5));
    PortfolioTransaction t = new PortfolioTransaction();
    t.setType(Type.DELIVERY_INBOUND);
    t.setMonetaryAmount(Money.of("EUR", 1_00));
    t.setSecurity(security);
    t.addUnit(unit);
    assertThat(action.process(t, portfolio).getCode(), is(Status.Code.OK));
    t.removeUnit(unit);
    assertThat(action.process(t, portfolio).getCode(), is(Status.Code.ERROR));
    Unit other = new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 1_00), Money.of("JPY", 2_00), BigDecimal.valueOf(0.5));
    t.addUnit(other);
    assertThat(action.process(t, portfolio).getCode(), is(Status.Code.ERROR));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Example 8 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CheckCurrenciesPortfolioTransactionTest method testTransactionForexTaxesAndFees.

@Test
public void testTransactionForexTaxesAndFees() {
    Portfolio portfolio = new Portfolio();
    Security security = new Security("", "USD");
    Unit grossValue = new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 10_00), Money.of("USD", 20_00), BigDecimal.valueOf(0.5));
    Unit tax = new Unit(Unit.Type.TAX, Money.of("EUR", 5_00), Money.of("USD", 10_00), BigDecimal.valueOf(0.5));
    Unit tax2 = new Unit(Unit.Type.TAX, Money.of("EUR", 1_00));
    Unit fee = new Unit(Unit.Type.FEE, Money.of("EUR", 5_00), Money.of("USD", 10_00), BigDecimal.valueOf(0.5));
    PortfolioTransaction t = new PortfolioTransaction();
    t.setType(Type.DELIVERY_OUTBOUND);
    t.setMonetaryAmount(Money.of("EUR", 20_00));
    t.setSecurity(security);
    t.addUnit(grossValue);
    t.addUnit(fee);
    t.addUnit(tax);
    t.addUnit(tax2);
    assertThat(action.process(t, portfolio).getCode(), is(Status.Code.OK));
    t.removeUnit(fee);
    t.addUnit(new Unit(Unit.Type.FEE, Money.of("EUR", 5_00), Money.of("JPY", 10_00), BigDecimal.valueOf(0.5)));
    assertThat(action.process(t, portfolio).getCode(), is(Status.Code.ERROR));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Example 9 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CrossEntryTest method testBuySellEntry.

@Test
public void testBuySellEntry() {
    Portfolio portfolio = client.getPortfolios().get(0);
    Account account = client.getAccounts().get(0);
    Security security = client.getSecurities().get(0);
    BuySellEntry entry = new BuySellEntry(portfolio, account);
    entry.setCurrencyCode(CurrencyUnit.EUR);
    LocalDateTime date = LocalDateTime.now();
    entry.setDate(date);
    entry.setSecurity(security);
    entry.setShares(1 * Values.Share.factor());
    entry.getPortfolioTransaction().addUnit(new Unit(Unit.Type.FEE, Money.of(CurrencyUnit.EUR, 10)));
    entry.getPortfolioTransaction().addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, 11)));
    entry.setAmount(1000 * Values.Amount.factor());
    entry.setType(PortfolioTransaction.Type.BUY);
    entry.insert();
    assertThat(portfolio.getTransactions().size(), is(1));
    assertThat(account.getTransactions().size(), is(1));
    PortfolioTransaction pt = portfolio.getTransactions().get(0);
    AccountTransaction pa = account.getTransactions().get(0);
    assertThat(pt.getSecurity(), is(security));
    assertThat(pa.getSecurity(), is(security));
    assertThat(pt.getAmount(), is(pa.getAmount()));
    assertThat(pt.getDateTime(), is(date));
    assertThat(pa.getDateTime(), is(date));
    assertThat(pt.getUnitSum(Unit.Type.FEE), is(Money.of(CurrencyUnit.EUR, 10L)));
    assertThat(pt.getUnitSum(Unit.Type.TAX), is(Money.of(CurrencyUnit.EUR, 11L)));
    // check cross entity identification
    assertThat(entry.getCrossOwner(pt), is((Object) account));
    assertThat(entry.getCrossTransaction(pt), is((Transaction) pa));
    assertThat(entry.getCrossOwner(pa), is((Object) portfolio));
    assertThat(entry.getCrossTransaction(pa), is((Transaction) pt));
    // check cross editing
    pa.setDateTime(LocalDateTime.of(2013, Month.MARCH, 16, 0, 0));
    entry.updateFrom(pa);
    assertThat(pt.getDateTime(), is(pa.getDateTime()));
    // check deletion
    portfolio.deleteTransaction(pt, client);
    assertThat(portfolio.getTransactions().size(), is(0));
    assertThat(account.getTransactions().size(), is(0));
}
Also used : LocalDateTime(java.time.LocalDateTime) Unit(name.abuchen.portfolio.model.Transaction.Unit) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Test(org.junit.Test)

Example 10 with Unit

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

Aggregations

Unit (name.abuchen.portfolio.model.Transaction.Unit)75 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)58 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)51 Money (name.abuchen.portfolio.money.Money)51 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)50 Client (name.abuchen.portfolio.model.Client)50 IOException (java.io.IOException)44 Block (name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block)37 DocumentType (name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType)37 Transaction (name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction)37 BigDecimal (java.math.BigDecimal)32 Security (name.abuchen.portfolio.model.Security)26 Matcher (java.util.regex.Matcher)24 Pattern (java.util.regex.Pattern)24 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)24 Test (org.junit.Test)24 Map (java.util.Map)23 Values (name.abuchen.portfolio.money.Values)18 ArrayList (java.util.ArrayList)13 LocalDateTime (java.time.LocalDateTime)12