/**
   * Called from ProcessMiscCashReceipts, this Method validates the data from the MiscCashReceipts
   * form. If a validation error occurs, the error is stored in the errors collection. Creation
   * date: (8/13/2002 3:41:28 PM)
   */
  public void validateData(fdms.ui.struts.form.MiscCashReceiptsForm form, ActionErrors errors) {

    try {

      // AmountOfTran is Required
      try {
        if (form.getAmountOfTran() == null || form.getAmountOfTran().trim().equals("")) {
          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullAmountOfTran"));
          formErrors.add("amountOfTran");
        } else {
          if (FormatCurrency.convertToCurrency(form.getAmountOfTran()) == 0) {
            errors.add(
                ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.invalidAmountOfTran"));
            formErrors.add("amountOfTran");
          }
        }
      } catch (Exception e) {
        errors.add(
            ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.invalidAmountOfTran"));
        formErrors.add("amountOfTran");
      }

      // ArAcct
      if (form.getArAcct() == null || form.getArAcct().trim().equals("")) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullArAcct"));
        formErrors.add("arAcct");
      }

      // DateOfTran
      if (form.getDateOfTran() == null || form.getDateOfTran().trim().equals("")) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullDateOfTran"));
        formErrors.add("dateOfTran");
      } else {
        try {
          FormatDate.convertToDate(form.getDateOfTran());
        } catch (Exception de) {
          errors.add(
              ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.invalidDateOfTran"));
          formErrors.add("dateOfTran");
        }
      }

      // Description
      if (form.getDescription() == null || form.getDescription().trim().equals("")) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullDescription"));
        formErrors.add("description");
      }

      // GlAcct
      if (form.getGlAcct() == null || form.getGlAcct().trim().equals("")) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullGlAcct"));
        formErrors.add("glAcct");
      }

      // LocationId
      if (form.getLocationId() == null || form.getLocationId().trim().equals("")) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullLocationId"));
        formErrors.add("locationId");
      }

      // PayMethod
      if (form.getPayMethod() == null || form.getPayMethod().trim().equals("")) {
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.nullPayMethod"));
        formErrors.add("payMethod");
      }

      // ReceiptNo is not required but must be numeric
      try {
        if (form.getReceiptNumber() != null && (!form.getReceiptNumber().trim().equals(""))) {
          Integer.parseInt(form.getReceiptNumber());
        }
      } catch (Exception ne) {
        errors.add(
            ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.invalidReceiptNumber"));
        formErrors.add("receiptNumber");
      }

    } catch (Exception e) {
      logger.error("Catching errors in ProcessMiscCashReceipts.validateData. ", e);
    }
  }
  /**
   * Called from ProcessMiscCashReceipts, this Method sets the History record from the
   * MiscCashReceipts form values. If am error occurs, the error is stored in the errors collection.
   * Creation date: (8/13/2002 3:41:43 PM)
   */
  public void setHistory(
      DatabaseTransaction t,
      DbUserSession sessionUser,
      DbHistory dbHistory,
      fdms.ui.struts.form.MiscCashReceiptsForm form,
      ActionErrors errors) {

    String errorField = new String();
    String formField = null;
    DbLocale userlocale = null;
    DatabaseTransaction tlocale = null;
    int receiptNumber = 0;

    try {

      errorField = "VitalsMasterKey";
      dbHistory.setLMainKey(0);

      errorField = "ARAcct";
      formField = "arAcct";
      dbHistory.setCHistARacct(form.getArAcct());

      errorField = "Date";
      formField = "dateOfTran";
      dbHistory.setCHistDate(
          new java.sql.Date(FormatDate.convertToDate(form.getDateOfTran()).getTime()));

      errorField = "Description";
      formField = "description";
      dbHistory.setCHistDesc(form.getDescription());

      errorField = "GLAcct";
      formField = "glAcct";
      dbHistory.setCHistGLAcct(form.getGlAcct());

      errorField = "ManualReceipt";
      formField = "manualReceiptNo";
      dbHistory.setCHistManualReceipt(form.getManualReceiptNo());

      errorField = "OriginalPosting";
      formField = "";
      dbHistory.setCHistOriginalPosting('N');

      errorField = "PayMethod";
      formField = "getPayMethod";
      dbHistory.setCHistPayMethod(form.getPayMethod());

      errorField = "Posted";
      formField = "";
      dbHistory.setCHistPosted('N');

      errorField = "SPF";
      formField = "";
      dbHistory.setCHistSPF('R');

      // Set Receipt Number from locale
      try {
        tlocale = (DatabaseTransaction) DatabaseTransaction.getTransaction(sessionUser);
        userlocale = FdmsDb.getInstance().getLocaleWithLock(tlocale, sessionUser.getRegion());
        if (userlocale == null) {
          throw new java.sql.SQLException("No locale for user region.");
        }
        receiptNumber = userlocale.getNextReceiptNo();
        userlocale.setNextReceiptNo(receiptNumber + 1);
        tlocale.addPersistent(userlocale);
        tlocale.save();
        // AppLog.trace("ProcessMiscCashReceipts: next receipt number = " +receiptNumber);

      } catch (java.sql.SQLException e) {
        if (e.getErrorCode() == 1205) {
          // AppLog.warning("ProcessMiscCashReceipts - DbLocale locked from updating."
          // +sessionUser.getRegion());
          throw new PersistenceException("User locale temporarily locked. Try again.");
        }
        // AppLog.warning("ProcessMiscCashReceipts - invalid region for user "
        // +sessionUser.getUserName());
        throw new PersistenceException("Invalid region for user", e);
      } finally {
        if (t != null) {
          t.closeConnection();
        }
      }

      errorField = "ReceiptNo";
      dbHistory.setLHistReceiptNo(receiptNumber);

      errorField = "Amount";
      dbHistory.setLHistAmount(
          FormatNumber.parseInteger(
              String.valueOf(FormatCurrency.convertToCurrency(form.getAmountOfTran()))));

      errorField = "LocationId";
      dbHistory.setLocationId(Integer.parseInt(form.getLocationId()));

      t.addPersistent(dbHistory);

    } catch (Exception e) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.receipts.set" + errorField));
      formErrors.add(formField);
    }

    return;
  }