Ejemplo n.º 1
0
  private Transaction generateTransaction(
      final String transactionClass, final Map<String, String> elementMap) {
    // logger.finest("Being generateTransaction");
    switch (transactionClass) {
      case "SplitTransaction":
        logger.finest("Found SplitTransaction");
        splitList.add(elementMap);
        return null;
      case "SplitEntryTransaction":
        logger.finest("Found SplitEntryTransaction");
        splitEntryList.add(elementMap);
        return null;
    }

    // logger.finest("Building base transation");

    Transaction transaction = null;

    Date actDate = decodeDate(elementMap.get("actTransDate"));
    Date date = decodeDate(elementMap.get("voucherDate"));
    String memo = elementMap.get("memo");
    String payee = elementMap.get("payee");
    String number = elementMap.get("number");
    CurrencyNode node = decodeCurrency(elementMap.get("commodity"));

    // logger.finest(transactionClass);

    if (transactionClass.equals("AddXTransaction")
        || transactionClass.equals("RemoveXTransaction")) {
      BigDecimal price = new BigDecimal(elementMap.get("price"));
      BigDecimal quantity = new BigDecimal(elementMap.get("quantity"));
      Account investmentAccount = accountMap.get(elementMap.get("investmentAccount"));
      boolean reconciled = Boolean.parseBoolean(elementMap.get("reconciled"));
      SecurityNode sNode =
          EngineFactory.getEngine(EngineFactory.DEFAULT).getSecurity(elementMap.get("security"));

      transaction = new InvestmentTransaction();

      AbstractInvestmentTransactionEntry entry;

      if (transactionClass.equals("AddXTransaction")) {
        entry = new TransactionEntryAddX(investmentAccount, sNode, price, quantity);
      } else {
        entry = new TransactionEntryRemoveX(investmentAccount, sNode, price, quantity);
      }

      entry.setMemo(memo);

      transaction.setDate(date);
      transaction.setDateEntered(actDate);
      transaction.setPayee(payee);
      transaction.addTransactionEntry(entry);

      transaction.setReconciled(
          investmentAccount,
          reconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
    }

    if (transactionClass.equals("SplitXTransaction")
        || transactionClass.equals("MergeXTransaction")) {
      BigDecimal price = new BigDecimal(elementMap.get("price"));
      BigDecimal quantity = new BigDecimal(elementMap.get("quantity"));
      Account investmentAccount = accountMap.get(elementMap.get("investmentAccount"));
      boolean reconciled = Boolean.parseBoolean(elementMap.get("reconciled"));
      SecurityNode sNode =
          EngineFactory.getEngine(EngineFactory.DEFAULT).getSecurity(elementMap.get("security"));

      transaction = new InvestmentTransaction();

      AbstractInvestmentTransactionEntry entry;

      if (transactionClass.equals("SplitXTransaction")) {
        entry = new TransactionEntrySplitX(investmentAccount, sNode, price, quantity);
      } else {
        entry = new TransactionEntryMergeX(investmentAccount, sNode, price, quantity);
      }

      entry.setMemo(memo);

      transaction.setDate(date);
      transaction.setDateEntered(actDate);
      transaction.setPayee(payee);
      transaction.addTransactionEntry(entry);

      transaction.setReconciled(
          investmentAccount,
          reconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
    }

    if (transactionClass.equals("BuyXTransaction") || transactionClass.equals("SellXTransaction")) {
      BigDecimal fees = new BigDecimal(elementMap.get("fees"));
      BigDecimal price = new BigDecimal(elementMap.get("price"));
      BigDecimal quantity = new BigDecimal(elementMap.get("quantity"));
      Account investmentAccount = accountMap.get(elementMap.get("investmentAccount"));
      Account account = accountMap.get(elementMap.get("account"));
      boolean accountReconciled = Boolean.parseBoolean(elementMap.get("accountReconciled"));
      boolean investmentAccountReconciled =
          Boolean.parseBoolean(elementMap.get("investmentAccountReconciled"));
      SecurityNode sNode =
          EngineFactory.getEngine(EngineFactory.DEFAULT).getSecurity(elementMap.get("security"));

      BigDecimal exchangeRate = BigDecimal.ONE;

      if (sNode != null) {

        if (transactionClass.equals("BuyXTransaction")) {
          transaction =
              TransactionFactory.import1xBuyXTransaction(
                  account,
                  investmentAccount,
                  sNode,
                  price,
                  quantity,
                  exchangeRate,
                  fees,
                  date,
                  memo);
        } else {
          transaction =
              TransactionFactory.import1xSellXTransaction(
                  account,
                  investmentAccount,
                  sNode,
                  price,
                  quantity,
                  exchangeRate,
                  fees,
                  date,
                  memo);
        }

        transaction.setDateEntered(actDate);
        transaction.setPayee(payee);

        transaction.setReconciled(
            investmentAccount,
            investmentAccountReconciled
                ? ReconciledState.RECONCILED
                : ReconciledState.NOT_RECONCILED);
        transaction.setReconciled(
            account,
            accountReconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
      }
    }

    if (transactionClass.equals("DividendTransaction")) {

      BigDecimal amount = new BigDecimal(elementMap.get("amount"));
      Account investmentAccount = accountMap.get(elementMap.get("investmentAccount"));
      Account account = accountMap.get(elementMap.get("account"));
      boolean accountReconciled = Boolean.parseBoolean(elementMap.get("accountReconciled"));
      boolean investmentAccountReconciled =
          Boolean.parseBoolean(elementMap.get("investmentAccountReconciled"));
      SecurityNode sNode =
          EngineFactory.getEngine(EngineFactory.DEFAULT).getSecurity(elementMap.get("security"));

      transaction =
          TransactionFactory.generateDividendXTransaction(
              investmentAccount,
              investmentAccount,
              account,
              sNode,
              amount,
              amount.negate(),
              amount,
              date,
              memo,
              accountReconciled);

      ReconcileManager.reconcileTransaction(
          investmentAccount,
          transaction,
          investmentAccountReconciled
              ? ReconciledState.RECONCILED
              : ReconciledState.NOT_RECONCILED);

      ReconcileManager.reconcileTransaction(
          account,
          transaction,
          accountReconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
    }

    if (transactionClass.equals("ReinvestDivTransaction")) {
      BigDecimal fees = new BigDecimal(elementMap.get("fees"));
      BigDecimal price = new BigDecimal(elementMap.get("price"));
      BigDecimal quantity = new BigDecimal(elementMap.get("quantity"));
      SecurityNode sNode =
          EngineFactory.getEngine(EngineFactory.DEFAULT).getSecurity(elementMap.get("security"));
      Account investmentAccount = accountMap.get(elementMap.get("investmentAccount"));
      boolean reconciled = Boolean.parseBoolean(elementMap.get("reconciled"));

      transaction = new InvestmentTransaction();

      AbstractInvestmentTransactionEntry entry =
          new TransactionEntryReinvestDivX(investmentAccount, sNode, price, quantity);

      entry.setMemo(memo);

      transaction.setDate(date);
      transaction.setDateEntered(actDate);
      transaction.setPayee(payee);

      transaction.addTransactionEntry(entry);

      if (fees.compareTo(BigDecimal.ZERO) > 0) {
        TransactionEntry fTran = new TransactionEntry(investmentAccount, fees.negate());
        fTran.setMemo(memo);
        fTran.setTransactionTag(TransactionTag.INVESTMENT_FEE);

        transaction.addTransactionEntry(fTran);
      }

      transaction.setReconciled(
          investmentAccount,
          reconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
    }
    switch (transactionClass) {
      case "SingleEntryTransaction":
        {
          BigDecimal amount = new BigDecimal(elementMap.get("amount"));
          Account account = accountMap.get(elementMap.get("account"));
          boolean reconciled = Boolean.parseBoolean(elementMap.get("reconciled"));
          transaction =
              TransactionFactory.generateSingleEntryTransaction(
                  account, amount, date, reconciled, memo, payee, number);
          transaction.setDateEntered(actDate);
          break;
        }
      case "DoubleEntryTransaction":
        {
          Account creditAccount = accountMap.get(elementMap.get("creditAccount"));
          Account debitAccount = accountMap.get(elementMap.get("debitAccount"));
          BigDecimal amount = new BigDecimal(elementMap.get("amount"));
          boolean creditReconciled = Boolean.parseBoolean(elementMap.get("creditReconciled"));
          boolean debitReconciled = Boolean.parseBoolean(elementMap.get("debitReconciled"));
          transaction = new Transaction();
          transaction.setDate(date);
          transaction.setDateEntered(actDate);
          transaction.setNumber(number);
          transaction.setPayee(payee);
          TransactionEntry entry = new TransactionEntry();
          entry.setMemo(memo);
          entry.setCreditAccount(creditAccount);
          entry.setDebitAccount(debitAccount);
          if (creditAccount.getCurrencyNode().equals(node)) {
            entry.setCreditAmount(amount);
          } else {
            BigDecimal exchangeRate = new BigDecimal(elementMap.get("exchangeRate"));
            entry.setCreditAmount(amount.multiply(exchangeRate));
          }
          if (debitAccount.getCurrencyNode().equals(node)) {
            entry.setDebitAmount(amount.negate());
          } else {
            BigDecimal exchangeRate = new BigDecimal(elementMap.get("exchangeRate"));
            entry.setDebitAmount(amount.multiply(exchangeRate).negate());
          }
          transaction.addTransactionEntry(entry);
          transaction.setReconciled(
              creditAccount,
              creditReconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
          transaction.setReconciled(
              debitAccount,
              debitReconciled ? ReconciledState.RECONCILED : ReconciledState.NOT_RECONCILED);
          break;
        }
    }
    return transaction;
  }