Search in sources :

Example 1 with PortfolioTransferEntry

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

the class CSVPortfolioTransactionExtractor method createTransfer.

private Item createTransfer(Security security, Money amount, Long fees, Long taxes, LocalDateTime date, String note, Long shares, Unit grossAmount) {
    PortfolioTransferEntry entry = new PortfolioTransferEntry();
    entry.setSecurity(security);
    entry.setDate(date);
    entry.setAmount(Math.abs(amount.getAmount()));
    entry.setCurrencyCode(amount.getCurrencyCode());
    entry.setShares(shares);
    entry.setNote(note);
    return new PortfolioTransferItem(entry);
}
Also used : PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry)

Example 2 with PortfolioTransferEntry

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

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

the class CSVPortfolioTransactionExtractorTest method testTransferTransaction.

@Test
public void testTransferTransaction() throws ParseException {
    Client client = new Client();
    Security security = new Security();
    security.setTickerSymbol("SAP.DE");
    client.addSecurity(security);
    CSVExtractor extractor = new CSVPortfolioTransactionExtractor(client);
    List<Exception> errors = new ArrayList<Exception>();
    List<Item> results = extractor.extract(0, Arrays.<String[]>asList(new String[] { "2013-01-01", "DE0007164600", "SAP.DE", "", "SAP SE", "100", "EUR", "11", "10", "", "", "", "1,2", "TRANSFER_IN", "Notiz" }), buildField2Column(extractor), errors);
    assertThat(errors, empty());
    assertThat(results.size(), is(1));
    new AssertImportActions().check(results, CurrencyUnit.EUR);
    PortfolioTransferEntry entry = (PortfolioTransferEntry) results.stream().filter(i -> i instanceof PortfolioTransferItem).findAny().get().getSubject();
    PortfolioTransaction source = entry.getSourceTransaction();
    assertThat(source.getType(), is(PortfolioTransaction.Type.TRANSFER_OUT));
    assertThat(source.getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 100_00)));
    assertThat(source.getNote(), is("Notiz"));
    assertThat(source.getDateTime(), is(LocalDateTime.parse("2013-01-01T00:00")));
    assertThat(source.getShares(), is(Values.Share.factorize(1.2)));
    assertThat(source.getSecurity(), is(security));
    // security transfers do not support fees and taxes at the moment
    assertThat(source.getUnitSum(Unit.Type.FEE), is(Money.of("EUR", 0)));
    assertThat(source.getUnitSum(Unit.Type.TAX), is(Money.of("EUR", 0)));
    PortfolioTransaction target = entry.getTargetTransaction();
    assertThat(target.getType(), is(PortfolioTransaction.Type.TRANSFER_IN));
    assertThat(target.getUnitSum(Unit.Type.FEE), is(Money.of("EUR", 0)));
    assertThat(target.getUnitSum(Unit.Type.TAX), is(Money.of("EUR", 0)));
}
Also used : ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) ParseException(java.text.ParseException) AssertImportActions(name.abuchen.portfolio.datatransfer.actions.AssertImportActions) PortfolioTransferItem(name.abuchen.portfolio.datatransfer.Extractor.PortfolioTransferItem) Item(name.abuchen.portfolio.datatransfer.Extractor.Item) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) TransactionItem(name.abuchen.portfolio.datatransfer.Extractor.TransactionItem) BuySellEntryItem(name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Client(name.abuchen.portfolio.model.Client) PortfolioTransferItem(name.abuchen.portfolio.datatransfer.Extractor.PortfolioTransferItem) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Test(org.junit.Test)

Example 4 with PortfolioTransferEntry

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

the class PortfolioClientFilterTest method testCrossPortfolioTransfersAreKept.

@Test
public void testCrossPortfolioTransfersAreKept() {
    Portfolio portfolioA = client.getPortfolios().get(0);
    Portfolio portfolioB = client.getPortfolios().get(1);
    PortfolioTransferEntry entry = new PortfolioTransferEntry(portfolioA, portfolioB);
    entry.setDate(LocalDateTime.parse("2016-04-01T00:00"));
    entry.setAmount(Values.Amount.factorize(10));
    entry.setShares(Values.Share.factorize(1));
    entry.setSecurity(client.getSecurities().get(0));
    entry.insert();
    Client result = new PortfolioClientFilter(Arrays.asList(portfolioA, portfolioB), Collections.emptyList()).filter(client);
    assertThat(result.getPortfolios().size(), is(2));
    assertThat(result.getAccounts().size(), is(2));
    Portfolio portfolio = result.getPortfolios().get(0);
    // check that the 4 transactions are transformed:
    // - buy -> inbound delivery
    // - transfer must exist
    assertThat(portfolio.getTransactions().size(), is(2));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-02-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.TRANSFER_OUT).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Test(org.junit.Test)

Example 5 with PortfolioTransferEntry

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

the class PortfolioClientFilterTest method testCrossPortfolioTransfersAreConvertedToDeliveries.

@Test
public void testCrossPortfolioTransfersAreConvertedToDeliveries() {
    Portfolio portfolioA = client.getPortfolios().get(0);
    Portfolio portfolioB = client.getPortfolios().get(1);
    PortfolioTransferEntry entry = new PortfolioTransferEntry(portfolioA, portfolioB);
    entry.setDate(LocalDateTime.parse("2016-04-01T00:00"));
    entry.setAmount(Values.Amount.factorize(10));
    entry.setShares(Values.Share.factorize(1));
    entry.setSecurity(client.getSecurities().get(0));
    entry.insert();
    Client result = new PortfolioClientFilter(Arrays.asList(portfolioA), Collections.emptyList()).filter(client);
    assertThat(result.getPortfolios().size(), is(1));
    assertThat(result.getAccounts().size(), is(1));
    Portfolio portfolio = result.getPortfolios().get(0);
    // check that the 4 transactions are transformed:
    // - buy -> inbound delivery
    // - transfer -> outbound delivery
    assertThat(portfolio.getTransactions().size(), is(2));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-02-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
    // check the other portfolio
    result = new PortfolioClientFilter(Arrays.asList(portfolioB), Collections.emptyList()).filter(client);
    assertThat(// 
    result.getPortfolios().get(0).getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Test(org.junit.Test)

Aggregations

PortfolioTransferEntry (name.abuchen.portfolio.model.PortfolioTransferEntry)8 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)6 Client (name.abuchen.portfolio.model.Client)5 Security (name.abuchen.portfolio.model.Security)5 LocalDate (java.time.LocalDate)4 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)4 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)4 Test (org.junit.Test)4 LocalDateTime (java.time.LocalDateTime)3 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)3 Account (name.abuchen.portfolio.model.Account)3 Portfolio (name.abuchen.portfolio.model.Portfolio)3 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)3 Money (name.abuchen.portfolio.money.Money)3 CoreMatchers.is (org.hamcrest.CoreMatchers.is)3 Assert.assertThat (org.junit.Assert.assertThat)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 AccountBuilder (name.abuchen.portfolio.AccountBuilder)2