private static void collectAccountTransactions(
     Client client, Date start, Date end, List<Transaction> transactions) {
   for (Account account : client.getAccounts()) {
     for (AccountTransaction t : account.getTransactions()) {
       if (t.getDate().getTime() > start.getTime() && t.getDate().getTime() <= end.getTime()) {
         switch (t.getType()) {
           case DEPOSIT:
           case REMOVAL:
           case TRANSFER_IN:
           case TRANSFER_OUT:
             transactions.add(t);
             break;
           case BUY:
           case SELL:
           case FEES:
           case TAXES:
           case DIVIDENDS:
           case INTEREST:
           case TAX_REFUND:
             break;
           default:
             throw new UnsupportedOperationException();
         }
       }
     }
   }
 }
  @Test
  public void testThatNotTheSameAccountIsMatched() {
    Account second = new Account();
    client.addAccount(second);

    account.addTransaction(
        new AccountTransaction(
            Dates.today(),
            security, //
            AccountTransaction.Type.TRANSFER_IN,
            2));

    AccountTransaction umatched =
        new AccountTransaction(
            Dates.today(),
            security, //
            AccountTransaction.Type.TRANSFER_OUT,
            2);
    account.addTransaction(umatched);

    second.addTransaction(
        new AccountTransaction(
            Dates.today(),
            security, //
            AccountTransaction.Type.TRANSFER_OUT,
            2));

    List<Issue> issues = new CrossEntryCheck().execute(client);

    assertThat(issues.size(), is(1));
    assertThat(issues.get(0), is(MissingAccountTransferIssue.class));

    assertThat(account.getTransactions(), hasItem(umatched));
    assertThat(second.getTransactions().get(0).getCrossEntry(), notNullValue());
    assertThat(second.getTransactions().get(0).getType(), is(AccountTransaction.Type.TRANSFER_OUT));

    applyFixes(client, issues);
  }
 private void adaptAccountTransactions(
     Account account, ReadOnlyAccount pseudoAccount, Set<Security> usedSecurities) {
   for (AccountTransaction t : account.getTransactions()) {
     switch (t.getType()) {
       case BUY:
         if (portfolios.contains(t.getCrossEntry().getCrossOwner(t)))
           pseudoAccount.internalAddTransaction(t);
         else pseudoAccount.internalAddTransaction(convertTo(t, AccountTransaction.Type.REMOVAL));
         break;
       case SELL:
         if (portfolios.contains(t.getCrossEntry().getCrossOwner(t)))
           pseudoAccount.internalAddTransaction(t);
         else pseudoAccount.internalAddTransaction(convertTo(t, AccountTransaction.Type.DEPOSIT));
         break;
       case TRANSFER_IN:
         if (accounts.contains(t.getCrossEntry().getCrossOwner(t)))
           pseudoAccount.internalAddTransaction(t);
         else pseudoAccount.internalAddTransaction(convertTo(t, AccountTransaction.Type.DEPOSIT));
         break;
       case TRANSFER_OUT:
         if (accounts.contains(t.getCrossEntry().getCrossOwner(t)))
           pseudoAccount.internalAddTransaction(t);
         else pseudoAccount.internalAddTransaction(convertTo(t, AccountTransaction.Type.REMOVAL));
         break;
       case DIVIDENDS:
       case TAX_REFUND:
         if (t.getSecurity() == null || usedSecurities.contains(t.getSecurity()))
           pseudoAccount.internalAddTransaction(t);
         else pseudoAccount.internalAddTransaction(convertTo(t, AccountTransaction.Type.DEPOSIT));
         break;
       case DEPOSIT:
       case REMOVAL:
       case INTEREST:
       case INTEREST_CHARGE:
       case TAXES:
       case FEES:
         pseudoAccount.internalAddTransaction(t);
         break;
       default:
         throw new UnsupportedOperationException();
     }
   }
 }
  @Test
  public void testThatMatchingBuySellEntriesAreFixed() {
    portfolio.addTransaction(
        new PortfolioTransaction(
            Dates.today(),
            security, //
            PortfolioTransaction.Type.SELL,
            1,
            1,
            1));

    account.addTransaction(
        new AccountTransaction(
            Dates.today(),
            security, //
            AccountTransaction.Type.SELL,
            1));

    assertThat(new CrossEntryCheck().execute(client).size(), is(0));
    assertThat(portfolio.getTransactions().get(0).getCrossEntry(), notNullValue());
    assertThat(account.getTransactions().get(0).getCrossEntry(), notNullValue());
  }
  private static void extractSecurityRelatedAccountTransactions(
      Account account, ReportingPeriod period, Map<Security, SecurityPerformanceRecord> records) {
    for (AccountTransaction t : account.getTransactions()) {
      if (t.getSecurity() == null) continue;

      if (!period.containsTransaction().test(t)) continue;

      if (t.getType() == AccountTransaction.Type.DIVIDENDS //
          || t.getType() == AccountTransaction.Type.INTEREST) {
        DividendTransaction dt = new DividendTransaction();
        dt.setDate(t.getDate());
        dt.setSecurity(t.getSecurity());
        dt.setAccount(account);
        dt.setCurrencyCode(t.getCurrencyCode());
        dt.setAmount(t.getAmount());
        dt.setShares(t.getShares());
        dt.setNote(t.getNote());
        records.get(t.getSecurity()).addTransaction(dt);
      } else if (t.getType() == AccountTransaction.Type.TAX_REFUND) {
        records.get(t.getSecurity()).addTransaction(t);
      }
    }
  }