/**
   * Insert a new accounting item in ACC05/ACC06 tables.
   *
   * @return JournalHeaderVO object with the progressive attribute filled
   */
  public VOResponse insertJournalItem(JournalHeaderVO vo, String serverLanguageId, String username)
      throws Throwable {
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
      if (this.conn == null) conn = getConn();
      else conn = this.conn;

      // generate progressive for journal item number...
      vo.setProgressiveACC05(
          CompanyProgressiveUtils.getConsecutiveProgressive(
              vo.getCompanyCodeSys01ACC05(),
              "ACC05_JOURNAL_HEADER_YEAR=" + vo.getItemYearACC05(),
              "PROGRESSIVE",
              conn));

      // insert into ACC05...
      Map attribute2dbField = new HashMap();
      attribute2dbField.put("companyCodeSys01ACC05", "COMPANY_CODE_SYS01");
      attribute2dbField.put("accountingMotiveCodeAcc03ACC05", "ACCOUNTING_MOTIVE_CODE_ACC03");
      attribute2dbField.put("progressiveACC05", "PROGRESSIVE");
      attribute2dbField.put("itemYearACC05", "ITEM_YEAR");
      attribute2dbField.put("itemDateACC05", "ITEM_DATE");
      attribute2dbField.put("descriptionACC05", "DESCRIPTION");
      Response res =
          org.jallinone.commons.server.QueryUtilExtension.insertTable(
              conn,
              new UserSessionParameters(username),
              vo,
              "ACC05_JOURNAL_HEADER",
              attribute2dbField,
              "Y",
              "N",
              null,
              true);
      if (res.isError()) throw new Exception(res.getErrorMessage());

      // insert into ACC06...
      attribute2dbField.clear();

      attribute2dbField.put("companyCodeSys01ACC06", "COMPANY_CODE_SYS01");
      attribute2dbField.put("progressiveAcc05ACC06", "PROGRESSIVE_ACC05");
      attribute2dbField.put("itemYearAcc05ACC06", "ITEM_YEAR_ACC05");
      attribute2dbField.put("progressiveACC06", "PROGRESSIVE");
      attribute2dbField.put("debitAmountACC06", "DEBIT_AMOUNT");
      attribute2dbField.put("creditAmountACC06", "CREDIT_AMOUNT");
      attribute2dbField.put("accountCodeTypeACC06", "ACCOUNT_CODE_TYPE");
      attribute2dbField.put("accountCodeACC06", "ACCOUNT_CODE");
      attribute2dbField.put("accountCodeAcc02ACC06", "ACCOUNT_CODE_ACC02");
      attribute2dbField.put("descriptionACC06", "DESCRIPTION");
      JournalRowVO rowVO = null;
      for (int i = 0; i < vo.getJournalRows().size(); i++) {
        rowVO = (JournalRowVO) vo.getJournalRows().get(i);
        rowVO.setProgressiveAcc05ACC06(vo.getProgressiveACC05());
        rowVO.setProgressiveACC06(
            CompanyProgressiveUtils.getConsecutiveProgressive(
                vo.getCompanyCodeSys01ACC05(), "ACC06_JOURNAL_ROWS", "PROGRESSIVE", conn));
        res =
            org.jallinone.commons.server.QueryUtilExtension.insertTable(
                conn,
                new UserSessionParameters(username),
                rowVO,
                "ACC06_JOURNAL_ROWS",
                attribute2dbField,
                "Y",
                "N",
                null,
                true);
        if (res.isError()) throw new Exception(res.getErrorMessage());
      }

      return new VOResponse(vo);
    } catch (Throwable ex) {
      Logger.error(
          username,
          this.getClass().getName(),
          "insertItem",
          "Error while inserting a new item in the journal",
          ex);
      try {
        if (this.conn == null && conn != null)
          // rollback only local connection
          conn.rollback();
      } catch (Exception ex3) {
      }
      throw new Exception(ex.getMessage());
    } finally {
      try {
        pstmt.close();
      } catch (Exception ex2) {
      }

      try {
        if (this.conn == null && conn != null) {
          // close only local connection
          conn.commit();
          conn.close();
        }

      } catch (Exception exx) {
      }
    }
  }
  /** Business logic to execute. */
  public VOListResponse updateExpirations(
      String t1,
      String t2,
      ArrayList oldVOs,
      ArrayList newVOs,
      String serverLanguageId,
      String username)
      throws Throwable {
    PreparedStatement pstmt = null;

    Connection conn = null;
    try {
      if (this.conn == null) conn = getConn();
      else conn = this.conn;
      insJornalItemAction.setConn(conn); // use same transaction...
      userParamAction.setConn(conn); // use same transaction...

      ExpirationVO oldVO = null;
      ExpirationVO newVO = null;
      Response res = null;

      HashSet pkAttrs = new HashSet();
      pkAttrs.add("companyCodeSys01DOC19");
      pkAttrs.add("progressiveDOC19");

      HashMap attribute2dbField = new HashMap();
      attribute2dbField.put("progressiveDOC19", "PROGRESSIVE");
      attribute2dbField.put("companyCodeSys01DOC19", "COMPANY_CODE_SYS01");
      attribute2dbField.put("docTypeDOC19", "DOC_TYPE");
      attribute2dbField.put("docNumberDOC19", "DOC_NUMBER");
      attribute2dbField.put("docYearDOC19", "DOC_YEAR");
      attribute2dbField.put("docSequenceDOC19", "DOC_SEQUENCE");
      attribute2dbField.put("name_1DOC19", "NAME_1");
      attribute2dbField.put("name_2DOC19", "NAME_2");
      attribute2dbField.put("descriptionDOC19", "DESCRIPTION");
      attribute2dbField.put("valueDOC19", "VALUE");
      attribute2dbField.put("payedDOC19", "PAYED");
      attribute2dbField.put("docDateDOC19", "DOC_DATE");
      attribute2dbField.put("expirationDateDOC19", "EXPIRATION_DATE");
      attribute2dbField.put("progressiveReg04DOC19", "PROGRESSIVE_REG04");
      attribute2dbField.put("customerSupplierCodeDOC19", "CUSTOMER_SUPPLIER_CODE");

      attribute2dbField.put("payedDateDOC19", "PAYED_DATE");
      attribute2dbField.put("payedValueDOC19", "PAYED_VALUE");
      attribute2dbField.put("realPaymentTypeCodeReg11DOC19", "REAL_PAYMENT_TYPE_CODE_REG11");
      attribute2dbField.put("paymentTypeCodeReg11DOC19", "PAYMENT_TYPE_CODE_REG11");

      JournalHeaderVO jhVO = null;
      HashMap map = new HashMap();
      String bankAccountCode = null;

      for (int i = 0; i < oldVOs.size(); i++) {
        oldVO = (ExpirationVO) oldVOs.get(i);
        newVO = (ExpirationVO) newVOs.get(i);

        res =
            new QueryUtil()
                .updateTable(
                    conn,
                    new UserSessionParameters(username),
                    pkAttrs,
                    oldVO,
                    newVO,
                    "DOC19_EXPIRATIONS",
                    attribute2dbField,
                    "Y",
                    "N",
                    null,
                    true);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }

        map.put(ApplicationConsts.COMPANY_CODE_SYS01, newVO.getCompanyCodeSys01DOC19());
        map.put(ApplicationConsts.PARAM_CODE, ApplicationConsts.BANK_ACCOUNT);
        res = userParamAction.loadUserParam(map, serverLanguageId, username);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }
        bankAccountCode = ((VOResponse) res).getVo().toString();

        // generate an accounting item if the row has been payed...
        if (!oldVO.getPayedDOC19().booleanValue() && newVO.getPayedDOC19().booleanValue()) {

          jhVO = new JournalHeaderVO();
          jhVO.setCompanyCodeSys01ACC05(newVO.getCompanyCodeSys01DOC19());
          String creditDebitAccountCode = null;
          String accountCodeTypeACC06 = null;
          if (newVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)
              || newVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE)
              || newVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE)
              || newVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE)
              || newVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE)
              || newVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_INVOICE_PROCEEDS);
            jhVO.setDescriptionACC05(
                newVO.getDescriptionDOC19()
                    + " - "
                    + t1
                    + " "
                    + newVO.getName_1DOC19()
                    + " "
                    + (newVO.getName_2DOC19() == null ? "" : newVO.getName_2DOC19()));

            // determine account codes defined for the current customer...
            pstmt =
                conn.prepareStatement(
                    "select CREDIT_ACCOUNT_CODE_ACC02 from SAL07_CUSTOMERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?");
            pstmt.setString(1, newVO.getCompanyCodeSys01DOC19());
            pstmt.setBigDecimal(2, newVO.getProgressiveReg04DOC19());
            ResultSet rset = pstmt.executeQuery();
            if (!rset.next()) {
              rset.close();
              throw new Exception("customer not found");
            }
            creditDebitAccountCode = rset.getString(1);
            rset.close();
            pstmt.close();

            accountCodeTypeACC06 = ApplicationConsts.ACCOUNT_TYPE_CUSTOMER;
          } else {
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_PURCHASE_INVOICE_PAYED);
            jhVO.setDescriptionACC05(
                newVO.getDescriptionDOC19()
                    + " - "
                    + t2
                    + " "
                    + newVO.getName_1DOC19()
                    + " "
                    + (newVO.getName_2DOC19() == null ? "" : newVO.getName_2DOC19()));

            // determine account codes defined for the current supplier...
            pstmt =
                conn.prepareStatement(
                    "select DEBIT_ACCOUNT_CODE_ACC02 from PUR01_SUPPLIERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?");
            pstmt.setString(1, newVO.getCompanyCodeSys01DOC19());
            pstmt.setBigDecimal(2, newVO.getProgressiveReg04DOC19());
            ResultSet rset = pstmt.executeQuery();
            if (!rset.next()) {
              rset.close();
              throw new Exception("supplier not found");
            }
            creditDebitAccountCode = rset.getString(1);
            rset.close();
            pstmt.close();

            accountCodeTypeACC06 = ApplicationConsts.ACCOUNT_TYPE_SUPPLIER;
          }
          jhVO.setItemDateACC05(newVO.getPayedDateDOC19());
          Calendar cal = Calendar.getInstance();
          cal.setTime(newVO.getPayedDateDOC19());
          jhVO.setItemYearACC05(new BigDecimal(cal.get(Calendar.YEAR)));

          JournalRowVO jrVO = new JournalRowVO();
          jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
          jrVO.setAccountCodeAcc02ACC06(creditDebitAccountCode);
          jrVO.setAccountCodeACC06(newVO.getCustomerSupplierCodeDOC19());
          jrVO.setAccountCodeTypeACC06(accountCodeTypeACC06);
          jrVO.setCreditAmountACC06(newVO.getPayedValueDOC19());
          jrVO.setDescriptionACC06("");
          jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
          jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
          jhVO.addJournalRow(jrVO);

          jrVO = new JournalRowVO();
          jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
          jrVO.setAccountCodeAcc02ACC06(bankAccountCode);
          jrVO.setAccountCodeACC06(bankAccountCode);
          jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);
          jrVO.setDebitAmountACC06(newVO.getPayedValueDOC19());
          jrVO.setDescriptionACC06("");
          jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
          jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
          jhVO.addJournalRow(jrVO);

          res = insJornalItemAction.insertJournalItem(jhVO, serverLanguageId, username);
          if (res.isError()) {
            throw new Exception(res.getErrorMessage());
          }
        }
      }

      return new VOListResponse(newVOs, false, newVOs.size());
    } catch (Throwable ex) {
      Logger.error(
          username,
          this.getClass().getName(),
          "executeCommand",
          "Error while updating existing expirations",
          ex);
      try {
        if (this.conn == null && conn != null)
          // rollback only local connection
          conn.rollback();
      } catch (Exception ex3) {
      }
      throw new Exception(ex.getMessage());
    } finally {
      try {
        if (pstmt != null) pstmt.close();
      } catch (Exception ex1) {
      }

      try {
        insJornalItemAction.setConn(null);
        userParamAction.setConn(null);
      } catch (Exception ex) {
      }
      try {
        if (this.conn == null && conn != null) {
          // close only local connection
          conn.commit();
          conn.close();
        }

      } catch (Exception exx) {
      }
    }
  }
  public VOResponse payImmediately(
      String companyCode,
      String docType,
      BigDecimal docYear,
      BigDecimal docNumber,
      BigDecimal docSequence,
      String t1,
      String t2,
      String serverLanguageId,
      String username)
      throws Throwable {
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    Connection conn = null;
    try {
      if (this.conn == null) conn = getConn();
      else conn = this.conn;
      insJornalItemAction.setConn(conn); // use same transaction...
      userParamAction.setConn(conn); // use same transaction...

      pstmt =
          conn.prepareStatement(
              "UPDATE DOC19_EXPIRATIONS SET PAYED_VALUE=VALUE,PAYED_DATE=EXPIRATION_DATE,PAYED='Y',REAL_PAYMENT_TYPE_CODE_REG11=PAYMENT_TYPE_CODE_REG11 WHERE "
                  + "COMPANY_CODE_SYS01=? AND DOC_TYPE=? AND DOC_YEAR=? AND DOC_NUMBER=? ");
      pstmt.setString(1, companyCode);
      pstmt.setString(2, docType);
      pstmt.setBigDecimal(3, docYear);
      pstmt.setBigDecimal(4, docNumber);
      int rows = pstmt.executeUpdate();
      if (rows != 1) throw new Exception("invalid number of payments");
      pstmt.close();

      pstmt =
          conn.prepareStatement(
              "SELECT DESCRIPTION,NAME_1,NAME_2,PROGRESSIVE_REG04,VALUE,CUSTOMER_SUPPLIER_CODE "
                  + "FROM DOC19_EXPIRATIONS WHERE "
                  + "COMPANY_CODE_SYS01=? AND DOC_TYPE=? AND DOC_YEAR=? AND DOC_NUMBER=?");
      pstmt.setString(1, companyCode);
      pstmt.setString(2, docType);
      pstmt.setBigDecimal(3, docYear);
      pstmt.setBigDecimal(4, docNumber);
      rset = pstmt.executeQuery();
      rset.next();
      String descriptionDOC19 = rset.getString(1);
      String name_1DOC19 = rset.getString(2);
      String name_2DOC19 = rset.getString(3);
      BigDecimal progressiveReg04DOC19 = rset.getBigDecimal(4);
      BigDecimal valueDOC19 = rset.getBigDecimal(5);
      String customerSupplierCodeDOC19 = rset.getString(6);
      rset.close();
      pstmt.close();

      HashMap map = new HashMap();
      map.put(ApplicationConsts.COMPANY_CODE_SYS01, companyCode);
      map.put(ApplicationConsts.PARAM_CODE, ApplicationConsts.BANK_ACCOUNT);
      Response res = userParamAction.loadUserParam(map, serverLanguageId, username);
      if (res.isError()) {
        throw new Exception(res.getErrorMessage());
      }
      String bankAccountCode = ((VOResponse) res).getVo().toString();

      // generate an accounting item, since the row has been payed...
      JournalHeaderVO jhVO = new JournalHeaderVO();
      jhVO.setCompanyCodeSys01ACC05(companyCode);
      String creditDebitAccountCode = null;
      String accountCodeTypeACC06 = null;
      if (docType.equals(ApplicationConsts.SALE_DESK_DOC_TYPE)
          || docType.equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE)
          || docType.equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE)
          || docType.equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE)
          || docType.equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE)
          || docType.equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
        jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_INVOICE_PROCEEDS);
        jhVO.setDescriptionACC05(
            descriptionDOC19
                + " - "
                + t1
                + " "
                + name_1DOC19
                + " "
                + (name_2DOC19 == null ? "" : name_2DOC19));

        // determine account codes defined for the current customer...
        pstmt =
            conn.prepareStatement(
                "select CREDIT_ACCOUNT_CODE_ACC02 from SAL07_CUSTOMERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?");
        pstmt.setString(1, companyCode);
        pstmt.setBigDecimal(2, progressiveReg04DOC19);
        rset = pstmt.executeQuery();
        if (!rset.next()) {
          rset.close();
          throw new Exception("customer not found");
        }
        creditDebitAccountCode = rset.getString(1);
        rset.close();
        pstmt.close();

        accountCodeTypeACC06 = ApplicationConsts.ACCOUNT_TYPE_CUSTOMER;
      } else {
        jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_PURCHASE_INVOICE_PAYED);
        jhVO.setDescriptionACC05(
            descriptionDOC19
                + " - "
                + t2
                + " "
                + name_1DOC19
                + " "
                + (name_2DOC19 == null ? "" : name_2DOC19));

        // determine account codes defined for the current supplier...
        pstmt =
            conn.prepareStatement(
                "select DEBIT_ACCOUNT_CODE_ACC02 from PUR01_SUPPLIERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?");
        pstmt.setString(1, companyCode);
        pstmt.setBigDecimal(2, progressiveReg04DOC19);
        rset = pstmt.executeQuery();
        if (!rset.next()) {
          rset.close();
          throw new Exception("supplier not found");
        }
        creditDebitAccountCode = rset.getString(1);
        rset.close();
        pstmt.close();

        accountCodeTypeACC06 = ApplicationConsts.ACCOUNT_TYPE_SUPPLIER;
      }
      jhVO.setItemDateACC05(new java.sql.Date(System.currentTimeMillis()));
      jhVO.setItemYearACC05(new BigDecimal(Calendar.getInstance().get(Calendar.YEAR)));

      JournalRowVO jrVO = new JournalRowVO();
      jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
      jrVO.setAccountCodeAcc02ACC06(creditDebitAccountCode);
      jrVO.setAccountCodeACC06(customerSupplierCodeDOC19);
      jrVO.setAccountCodeTypeACC06(accountCodeTypeACC06);
      jrVO.setCreditAmountACC06(valueDOC19);
      jrVO.setDescriptionACC06("");
      jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
      jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
      jhVO.addJournalRow(jrVO);

      jrVO = new JournalRowVO();
      jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
      jrVO.setAccountCodeAcc02ACC06(bankAccountCode);
      jrVO.setAccountCodeACC06(bankAccountCode);
      jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);
      jrVO.setDebitAmountACC06(valueDOC19);
      jrVO.setDescriptionACC06("");
      jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
      jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
      jhVO.addJournalRow(jrVO);

      res = insJornalItemAction.insertJournalItem(jhVO, serverLanguageId, username);
      if (res.isError()) {
        throw new Exception(res.getErrorMessage());
      }

      return new VOResponse(Boolean.TRUE);
    } catch (Throwable ex) {
      Logger.error(
          username,
          this.getClass().getName(),
          "executeCommand",
          "Error while updating existing expiration",
          ex);
      try {
        if (this.conn == null && conn != null)
          // rollback only local connection
          conn.rollback();
      } catch (Exception ex3) {
      }
      throw new Exception(ex.getMessage());
    } finally {
      try {
        if (rset != null) rset.close();
      } catch (Exception ex1) {
      }
      try {
        if (pstmt != null) pstmt.close();
      } catch (Exception ex1) {
      }

      try {
        insJornalItemAction.setConn(null);
        userParamAction.setConn(null);
      } catch (Exception ex) {
      }
      try {
        if (this.conn == null && conn != null) {
          // close only local connection
          conn.commit();
          conn.close();
        }

      } catch (Exception exx) {
      }
    }
  }
  /** Business logic to execute. */
  public final Response executeCommand(
      Object inputPar,
      UserSessionParameters userSessionPars,
      HttpServletRequest request,
      HttpServletResponse response,
      HttpSession userSession,
      ServletContext context) {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = ConnectionManager.getConnection(context);

      // fires the GenericEvent.CONNECTION_CREATED event...
      EventsManager.getInstance()
          .processEvent(
              new GenericEvent(
                  this,
                  getRequestName(),
                  GenericEvent.CONNECTION_CREATED,
                  (JAIOUserSessionParameters) userSessionPars,
                  request,
                  response,
                  userSession,
                  context,
                  conn,
                  inputPar,
                  null));

      Response responseVO =
          bean.insertItem(
              conn,
              (JournalHeaderVO) inputPar,
              userSessionPars,
              request,
              response,
              userSession,
              context);
      if (responseVO.isError()) {
        conn.rollback();
        return responseVO;
      }

      if (inputPar instanceof JournalHeaderWithVatVO) {
        JournalHeaderWithVatVO vo = (JournalHeaderWithVatVO) inputPar;

        // insert vat rows in the specified vat register...
        Response regRes =
            vatRegisterAction.insertVatRows(
                conn, vo.getVats(), userSessionPars, request, response, userSession, context);
        if (regRes.isError()) {
          conn.rollback();
          return regRes;
        }

        // retrieve payment instalments...
        Response payRes =
            payAction.executeCommand(
                new LookupValidationParams(vo.getPaymentCodeREG10(), new HashMap()),
                userSessionPars,
                request,
                response,
                userSession,
                context);
        if (payRes.isError()) {
          conn.rollback();
          return payRes;
        }
        PaymentVO payVO = (PaymentVO) ((VOListResponse) payRes).getRows().get(0);
        GridParams gridParams = new GridParams();
        gridParams
            .getOtherGridParams()
            .put(ApplicationConsts.PAYMENT_CODE_REG10, vo.getPaymentCodeREG10());
        payRes =
            paysAction.executeCommand(
                gridParams, userSessionPars, request, response, userSession, context);
        if (payRes.isError()) {
          conn.rollback();
          return payRes;
        }
        java.util.List rows = ((VOListResponse) payRes).getRows();

        // create expirations in DOC19 ONLY if:
        // - there are more than one instalment OR
        // - there is only one instalment and this instalment has more than 0 instalment days
        if (rows.size() > 1
            || (rows.size() == 1
                && ((PaymentInstalmentVO) rows.get(0)).getInstalmentDaysREG17().intValue() > 0)) {

          // retrieve internationalization settings (Resources object)...
          ServerResourcesFactory factory =
              (ServerResourcesFactory) context.getAttribute(Controller.RESOURCES_FACTORY);
          Resources resources = factory.getResources(userSessionPars.getLanguageId());

          PaymentInstalmentVO inVO = null;
          pstmt =
              conn.prepareStatement(
                  "insert into DOC19_EXPIRATIONS(COMPANY_CODE_SYS01,DOC_TYPE,DOC_YEAR,DOC_NUMBER,DOC_SEQUENCE,PROGRESSIVE,DOC_DATE,EXPIRATION_DATE,NAME_1,NAME_2,VALUE,PAYED,DESCRIPTION,CUSTOMER_SUPPLIER_CODE,PROGRESSIVE_REG04,CURRENCY_CODE_REG03) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
          long startTime = vo.getItemDateACC05().getTime(); // item date...
          if (payVO.getStartDayREG10().equals(ApplicationConsts.START_DAY_END_MONTH)) {
            Calendar cal = Calendar.getInstance();
            if (cal.get(cal.MONTH) == 10
                || cal.get(cal.MONTH) == 3
                || cal.get(cal.MONTH) == 5
                || cal.get(cal.MONTH) == 8) cal.set(cal.DAY_OF_MONTH, 30);
            else if (cal.get(cal.MONTH) == 1) {
              if (cal.get(cal.YEAR) % 4 == 0) cal.set(cal.DAY_OF_MONTH, 29);
              else cal.set(cal.DAY_OF_MONTH, 28);
            } else cal.set(cal.DAY_OF_MONTH, 31);
            startTime = cal.getTime().getTime();
          }
          BigDecimal amount = null;

          for (int i = 0; i < rows.size(); i++) {
            inVO = (PaymentInstalmentVO) rows.get(i);
            pstmt.setString(1, vo.getCompanyCodeSys01ACC05());
            pstmt.setString(2, vo.getDocTypeDOC19());
            pstmt.setBigDecimal(3, vo.getItemYearACC05());
            pstmt.setBigDecimal(4, null);
            pstmt.setBigDecimal(5, vo.getDocSequenceDOC19());
            pstmt.setBigDecimal(
                6,
                ProgressiveUtils.getConsecutiveProgressive(
                    "DOC19_EXPIRATIONS", "PROGRESSIVE", conn));
            pstmt.setDate(7, vo.getItemDateACC05());
            pstmt.setDate(
                8,
                new java.sql.Date(
                    startTime
                        + inVO.getInstalmentDaysREG17().longValue()
                            * 86400
                            * 1000)); // expiration date
            pstmt.setString(9, vo.getName_1REG04());
            pstmt.setString(10, vo.getName_2REG04());
            amount =
                vo.getTotalValue()
                    .multiply(inVO.getPercentageREG17())
                    .divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP)
                    .setScale(vo.getTotalValue().scale(), BigDecimal.ROUND_HALF_UP); // value

            pstmt.setBigDecimal(11, amount);
            pstmt.setString(12, "N");

            if (vo.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE))
              pstmt.setString(
                  13,
                  resources.getResource("sale generic document")
                      + " "
                      + vo.getDocSequenceDOC19()
                      + "/"
                      + vo.getItemYearACC05()
                      + " - "
                      + resources.getResource("valueREG01")
                      + " "
                      + resources.getResource("rateNumberREG17")
                      + " "
                      + (i + 1)
                      + " - "
                      + inVO.getPaymentTypeDescriptionSYS10()); // description
            else
              pstmt.setString(
                  13,
                  resources.getResource("purchase generic document")
                      + " "
                      + vo.getDocSequenceDOC19()
                      + "/"
                      + vo.getItemYearACC05()
                      + " - "
                      + resources.getResource("valueREG01")
                      + " "
                      + resources.getResource("rateNumberREG17")
                      + " "
                      + (i + 1)
                      + " - "
                      + inVO.getPaymentTypeDescriptionSYS10()); // description
            pstmt.setString(14, vo.getCustomerCodeSAL07());
            pstmt.setBigDecimal(15, vo.getProgressiveREG04());
            pstmt.setString(16, vo.getCurrencyCodeREG01());
            pstmt.execute();
          }
          pstmt.close();
        }

        // create an item registration for proceeds, according to expiration settings (e.g. retail
        // selling):
        // there must be only one instalment and this instalment has 0 instalment days
        if (rows.size() == 1
            && ((PaymentInstalmentVO) rows.get(0)).getInstalmentDaysREG17().intValue() == 0) {

          // retrieve internationalization settings (Resources object)...
          ServerResourcesFactory factory =
              (ServerResourcesFactory) context.getAttribute(Controller.RESOURCES_FACTORY);
          Resources resources = factory.getResources(userSessionPars.getLanguageId());

          HashMap map = new HashMap();
          map.put(ApplicationConsts.COMPANY_CODE_SYS01, vo.getCompanyCodeSys01ACC05());
          map.put(ApplicationConsts.PARAM_CODE, ApplicationConsts.CASE_ACCOUNT);
          Response res =
              userParamAction.executeCommand(
                  map, userSessionPars, request, response, userSession, context);
          if (res.isError()) {
            conn.rollback();
            return res;
          }
          String caseAccountCode = ((VOResponse) res).getVo().toString();

          JournalHeaderVO jhVO = new JournalHeaderVO();
          jhVO.setCompanyCodeSys01ACC05(vo.getCompanyCodeSys01ACC05());
          if (vo.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
            jhVO.setDescriptionACC05(
                resources.getResource("sale generic document")
                    + " "
                    + vo.getDocSequenceDOC19()
                    + "/"
                    + vo.getItemYearACC05()
                    + " - "
                    + resources.getResource("customer")
                    + " "
                    + vo.getName_1REG04()
                    + " "
                    + (vo.getName_2REG04() == null ? "" : vo.getName_2REG04()));
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_INVOICE_PROCEEDS);
          } else {
            jhVO.setDescriptionACC05(
                resources.getResource("purchase generic document")
                    + " "
                    + vo.getDocSequenceDOC19()
                    + "/"
                    + vo.getItemYearACC05()
                    + " - "
                    + resources.getResource("supplier")
                    + " "
                    + vo.getName_1REG04()
                    + " "
                    + (vo.getName_2REG04() == null ? "" : vo.getName_2REG04()));
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_PURCHASE_INVOICE_PAYED);
          }

          jhVO.setItemDateACC05(new java.sql.Date(System.currentTimeMillis()));
          jhVO.setItemYearACC05(new BigDecimal(Calendar.getInstance().get(Calendar.YEAR)));

          JournalRowVO jrVO = new JournalRowVO();
          jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
          if (vo.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
            jrVO.setAccountCodeAcc02ACC06(vo.getCreditAccountCodeAcc02SAL07());
            jrVO.setAccountCodeACC06(vo.getCustomerCodeSAL07());
            jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_CUSTOMER);
            jrVO.setCreditAmountACC06(vo.getTotalValue());
          } else {
            jrVO.setAccountCodeAcc02ACC06(vo.getDebitAccountCodeAcc02PUR01());
            jrVO.setAccountCodeACC06(vo.getSupplierCodePUR01());
            jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_SUPPLIER);
            jrVO.setDebitAmountACC06(vo.getTotalValue());
          }
          jrVO.setDescriptionACC06("");
          jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
          jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
          jhVO.addJournalRow(jrVO);

          jrVO = new JournalRowVO();
          jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
          jrVO.setAccountCodeAcc02ACC06(caseAccountCode);
          jrVO.setAccountCodeACC06(caseAccountCode);
          jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);
          if (vo.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
            jrVO.setDebitAmountACC06(vo.getTotalValue());
          } else {
            jrVO.setCreditAmountACC06(vo.getTotalValue());
          }
          jrVO.setDescriptionACC06("");
          jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
          jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
          jhVO.addJournalRow(jrVO);
          Response proceedsRes =
              bean.insertItem(conn, jhVO, userSessionPars, request, response, userSession, context);
          if (proceedsRes.isError()) {
            conn.rollback();
            return proceedsRes;
          }
        }
      }

      Response answer = responseVO;

      // fires the GenericEvent.BEFORE_COMMIT event...
      EventsManager.getInstance()
          .processEvent(
              new GenericEvent(
                  this,
                  getRequestName(),
                  GenericEvent.BEFORE_COMMIT,
                  (JAIOUserSessionParameters) userSessionPars,
                  request,
                  response,
                  userSession,
                  context,
                  conn,
                  inputPar,
                  answer));

      conn.commit();

      // fires the GenericEvent.AFTER_COMMIT event...
      EventsManager.getInstance()
          .processEvent(
              new GenericEvent(
                  this,
                  getRequestName(),
                  GenericEvent.AFTER_COMMIT,
                  (JAIOUserSessionParameters) userSessionPars,
                  request,
                  response,
                  userSession,
                  context,
                  conn,
                  inputPar,
                  answer));

      return answer;
    } catch (Throwable ex) {
      Logger.error(
          userSessionPars.getUsername(),
          this.getClass().getName(),
          "executeCommand",
          "Error while inserting a new item in the journal",
          ex);
      try {
        conn.rollback();
      } catch (Exception ex3) {
      }
      return new ErrorResponse(ex.getMessage());
    } finally {
      try {
        pstmt.close();
      } catch (Exception ex2) {
      }
      try {
        ConnectionManager.releaseConnection(conn, context);
      } catch (Exception ex1) {
      }
    }
  }