@FXML
  @Override
  public void handleEnterAction() {
    if (validateForm()) {
      if (modTrans == null) { // new transaction
        Transaction newTrans = buildTransaction();

        ReconcileManager.reconcileTransaction(
            accountProperty.get(), newTrans, getReconciledState());

        newTrans = attachmentPane.buildTransaction(newTrans); // chain the transaction build

        final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

        if (engine != null) {
          engine.addTransaction(newTrans);
        }
      } else {
        Transaction newTrans = buildTransaction();

        newTrans.setDateEntered(modTrans.getDateEntered());

        // restore the reconciled state of the previous old transaction
        for (final Account a : modTrans.getAccounts()) {
          if (!a.equals(accountProperty.get())) {
            ReconcileManager.reconcileTransaction(a, newTrans, modTrans.getReconciled(a));
          }
        }

        /*
         * Reconcile the modified transaction for this account.
         * This must be performed last to ensure consistent results per the ReconcileManager rules
         */
        ReconcileManager.reconcileTransaction(
            accountProperty.get(), newTrans, getReconciledState());

        newTrans = attachmentPane.buildTransaction(newTrans); // chain the transaction build

        final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

        if (engine != null && engine.removeTransaction(modTrans)) {
          engine.addTransaction(newTrans);
        }
      }
      clearForm();

      if (payeeTextField != null) {
        payeeTextField.requestFocus();
      } else {
        memoTextField.requestFocus();
      }
    }
  }
  @FXML
  private void convertAction() {
    final Optional<Account> accountOptional =
        StaticAccountsMethods.selectAccount(null, accountProperty.get());
    if (accountOptional.isPresent()) {
      final Account opp = accountOptional.get();

      final Transaction t = new Transaction();

      t.setDate(datePicker.getValue());
      t.setNumber(numberComboBox.getValue());
      t.setPayee(payeeTextField.getText());

      final TransactionEntry entry = new TransactionEntry();
      entry.setMemo(memoTextField.getText());

      if (amountField.getDecimal().signum() >= 0) {
        entry.setCreditAccount(accountProperty.get());
        entry.setDebitAccount(opp);
      } else {
        entry.setDebitAccount(accountProperty.get());
        entry.setCreditAccount(opp);
      }

      entry.setCreditAmount(amountField.getDecimal().abs());
      entry.setDebitAmount(amountField.getDecimal().abs().negate());

      t.addTransactionEntry(entry);

      ReconcileManager.reconcileTransaction(accountProperty.get(), t, getReconciledState());

      TransactionDialog.showAndWait(
          accountProperty.get(),
          t,
          optional -> {
            if (optional.isPresent()) {
              final Transaction tran = optional.get();
              final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
              Objects.requireNonNull(engine);

              if (engine.removeTransaction(modTrans)) {
                engine.addTransaction(tran);
              }
              clearForm();
            }
          });
    }
  }
Example #3
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;
  }
Example #4
0
  /** TODO: The memo payee and number should be user configurable by the user */
  @Override
  protected Transaction buildTransaction() {
    String number = "TRAN";

    String payee = rb.getString("Tab.Transfer");

    Transaction transaction;

    final int signum = amountField.getDecimal().signum();

    if (panelType == PanelType.DECREASE && signum >= 0 || signum == -1) {
      if (hasEqualCurrencies()) {
        transaction =
            TransactionFactory.generateDoubleEntryTransaction(
                accountPanel.getSelectedAccount(),
                getAccount(),
                amountField.getDecimal().abs(),
                datePanel.getDate(),
                memoField.getText(),
                payee,
                number);
      } else {
        transaction =
            TransactionFactory.generateDoubleEntryTransaction(
                accountPanel.getSelectedAccount(),
                getAccount(),
                accountPanel.getExchangedAmount().abs(),
                amountField.getDecimal().abs().negate(),
                datePanel.getDate(),
                memoField.getText(),
                payee,
                number);
      }
    } else {
      if (hasEqualCurrencies()) {
        transaction =
            TransactionFactory.generateDoubleEntryTransaction(
                getAccount(),
                accountPanel.getSelectedAccount(),
                amountField.getDecimal().abs(),
                datePanel.getDate(),
                memoField.getText(),
                payee,
                number);
      } else {
        transaction =
            TransactionFactory.generateDoubleEntryTransaction(
                getAccount(),
                accountPanel.getSelectedAccount(),
                amountField.getDecimal().abs(),
                accountPanel.getExchangedAmount().abs().negate(),
                datePanel.getDate(),
                memoField.getText(),
                payee,
                number);
      }
    }

    ReconcileManager.reconcileTransaction(getAccount(), transaction, reconciledButton.isSelected());

    return transaction;
  }