Package org.jallinone.sales.documents.java

Examples of org.jallinone.sales.documents.java.DetailSaleDocVO


   * @return <code>true</code> allows to go to EDIT mode, <code>false</code> the mode change is interrupted
   */
  public boolean beforeEditData(Form form) {
    if (!super.beforeEditData(form))
      return false;
    DetailSaleDocVO vo = (DetailSaleDocVO)frame.getHeaderFormPanel().getVOModel().getValueObject();
    return !vo.getDocStateDOC01().equals(ApplicationConsts.CLOSED);
  }
View Full Code Here


   * @return <code>true</code> allows the deleting to continue, <code>false</code> the deleting is interrupted
   */
  public boolean beforeDeleteData(Form form) {
    if (!super.beforeDeleteData(form))
      return false;
    DetailSaleDocVO vo = (DetailSaleDocVO)frame.getHeaderFormPanel().getVOModel().getValueObject();
    return !vo.getDocStateDOC01().equals(ApplicationConsts.CLOSED);
  }
View Full Code Here

   * Callback method called when the Form mode is changed.
   * @param currentMode current Form mode
   */
  public void modeChanged(int currentMode) {
    if (currentMode==Consts.INSERT) {
      DetailSaleDocVO vo = (DetailSaleDocVO)frame.getHeaderFormPanel().getVOModel().getValueObject();
      if (vo.getWarehouseCodeWar01DOC01()!=null)
        frame.getWarePanel().getWareController().forceValidate();
    }
  }
View Full Code Here

   * The method can pre-set some v.o. attributes, so that some input controls will have a predefined value associated.
   * @param persistentObject new value object
   */
  public void createPersistentObject(ValueObject persistentObject) throws Exception {
    DetailSaleDocRowVO vo = (DetailSaleDocRowVO)persistentObject;
    DetailSaleDocVO parentVO = panel.getParentVO();
    Calendar cal = Calendar.getInstance();
    vo.setCompanyCodeSys01DOC02(parentVO.getCompanyCodeSys01DOC01());
    vo.setDocYearDOC02(parentVO.getDocYearDOC01());
    vo.setDocTypeDOC02(parentVO.getDocTypeDOC01());
    vo.setDocNumberDOC02(parentVO.getDocNumberDOC01());
    vo.setDeliveryDateDOC02(new java.sql.Date(System.currentTimeMillis()));
    vo.setCurrencyCodeReg03DOC01(panel.getParentVO().getCurrencyCodeReg03DOC01());
    vo.setPricelistCodeSal01DOC02(parentVO.getPricelistCodeSal01DOC01());
    vo.setPricelistDescriptionDOC02(parentVO.getPricelistDescriptionDOC01());
    vo.setVariantCodeItm11DOC02("*");
    vo.setVariantCodeItm12DOC02("*");
    vo.setVariantCodeItm13DOC02("*");
    vo.setVariantCodeItm14DOC02("*");
    vo.setVariantCodeItm15DOC02("*");
 
View Full Code Here

    actPanel.setEnabled(enabled);
  }


  public void loadDataCompleted(boolean error,SaleDocPK pk) {
    DetailSaleDocVO vo = (DetailSaleDocVO)headerFormPanel.getVOModel().getValueObject();

    this.setTitle(ClientSettings.getInstance().getResources().getResource("sale invoice")+(vo.getDocSequenceDOC01()!=null?" - "+vo.getDocYearDOC01()+"/"+vo.getDocSequenceDOC01():"")+" - "+vo.getName_1REG04()+" "+(vo.getName_2REG04()==null?"":vo.getName_2REG04()));

    saleTotalsPanel1.getControlTaxableIncome().setDecimals(vo.getDecimalsREG03().intValue());
    saleTotalsPanel1.getControlTaxableIncome().setCurrencySymbol(vo.getCurrencySymbolREG03());
    saleTotalsPanel1.getControlTaxableIncome().setDecimalSymbol(vo.getDecimalSymbolREG03().charAt(0));
    saleTotalsPanel1.getControlTaxableIncome().setGroupingSymbol(vo.getThousandSymbolREG03().charAt(0));

    saleTotalsPanel1.getControlTotal().setDecimals(vo.getDecimalsREG03().intValue());
    saleTotalsPanel1.getControlTotal().setCurrencySymbol(vo.getCurrencySymbolREG03());
    saleTotalsPanel1.getControlTotal().setDecimalSymbol(vo.getDecimalSymbolREG03().charAt(0));
    saleTotalsPanel1.getControlTotal().setGroupingSymbol(vo.getThousandSymbolREG03().charAt(0));

    saleTotalsPanel1.getControlTotalVat().setDecimals(vo.getDecimalsREG03().intValue());
    saleTotalsPanel1.getControlTotalVat().setCurrencySymbol(vo.getCurrencySymbolREG03());
    saleTotalsPanel1.getControlTotalVat().setDecimalSymbol(vo.getDecimalSymbolREG03().charAt(0));
    saleTotalsPanel1.getControlTotalVat().setGroupingSymbol(vo.getThousandSymbolREG03().charAt(0));


    saleTotalsPanel1.getControlAllowance().setDecimals(vo.getDecimalsREG03().intValue());
    saleTotalsPanel1.getControlAllowance().setCurrencySymbol(vo.getCurrencySymbolREG03());
    saleTotalsPanel1.getControlAllowance().setDecimalSymbol(vo.getDecimalSymbolREG03().charAt(0));
    saleTotalsPanel1.getControlAllowance().setGroupingSymbol(vo.getThousandSymbolREG03().charAt(0));

    saleTotalsPanel1.getControlDeposit().setDecimals(vo.getDecimalsREG03().intValue());
    saleTotalsPanel1.getControlDeposit().setCurrencySymbol(vo.getCurrencySymbolREG03());
    saleTotalsPanel1.getControlDeposit().setDecimalSymbol(vo.getDecimalSymbolREG03().charAt(0));
    saleTotalsPanel1.getControlDeposit().setGroupingSymbol(vo.getThousandSymbolREG03().charAt(0));

    rowsPanel.setParentVO(vo);
    rowsPanel.getGrid().getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
    rowsPanel.getGrid().reloadData();

    discPanel.setParentVO(vo);
    discPanel.getGrid().getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
    discPanel.getGrid().reloadData();

    chargePanel.setParentVO(vo);
    chargePanel.getGrid().getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
    chargePanel.getGrid().reloadData();

    actPanel.setParentVO(vo);
    actPanel.getGrid().getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
    actPanel.getGrid().reloadData();

    if (vo.getDocStateDOC01().equals(ApplicationConsts.CONFIRMED) ||
        vo.getDocStateDOC01().equals(ApplicationConsts.CLOSED)) {
      confirmButton.setEnabled(false);
      setButtonsEnabled(false);
      rowsPanel.setButtonsEnabled(false);
    }
    if (!vo.getDocStateDOC01().equals(ApplicationConsts.HEADER_BLOCKED)) {
      confirmButton.setEnabled(false);
    }
    if (vo.getDocStateDOC01().equals(ApplicationConsts.HEADER_BLOCKED)) {
      confirmButton.setEnabled(true);
    }

    if (vo.getDocStateDOC01().equals(ApplicationConsts.CONFIRMED) ||
        vo.getDocStateDOC01().equals(ApplicationConsts.CLOSED) ||
        vo.getDocStateDOC01().equals(ApplicationConsts.HEADER_BLOCKED)) {
      printButton.setEnabled(true);
    }
    else {
      printButton.setEnabled(false);
    }
View Full Code Here

  /**
   * Recalculate total on changing allowance/deposit values.
   */
  private void calculateTotals() {
    DetailSaleDocVO vo = (DetailSaleDocVO)form.getVOModel().getValueObject();
    if (vo.getAllowanceDOC01()==null)
      vo.setAllowanceDOC01(new BigDecimal(0));
    if (vo.getDepositDOC01()==null)
      vo.setDepositDOC01(new BigDecimal(0));
    if (vo.getTotalDOC01()==null)
      vo.setTotalDOC01(new BigDecimal(0));
    if (vo.getTotalVatDOC01()==null)
      vo.setTotalVatDOC01(new BigDecimal(0));
    if (vo.getTaxableIncomeDOC01()==null)
      vo.setTaxableIncomeDOC01(new BigDecimal(0));

    vo.setTotalDOC01(
        vo.getTaxableIncomeDOC01().
        add(vo.getTotalVatDOC01().
        subtract(vo.getAllowanceDOC01()).
        subtract(vo.getDepositDOC01()))
    );

    if (vo.getTotalDOC01().doubleValue()<0) {
      JOptionPane.showMessageDialog(
          ClientUtils.getParentFrame(this),
          ClientSettings.getInstance().getResources().getResource("document total cannot be less than zero!"),
          ClientSettings.getInstance().getResources().getResource("Attention"),
          JOptionPane.WARNING_MESSAGE
      );
      vo.setAllowanceDOC01(new BigDecimal(0));
      vo.setDepositDOC01(new BigDecimal(0));
      vo.setTotalDOC01(
          vo.getTaxableIncomeDOC01().
          add(vo.getTotalVatDOC01().
          subtract(vo.getAllowanceDOC01()).
          subtract(vo.getDepositDOC01()))
      );
    }

    form.pull();
  }
View Full Code Here

   * @return <code>true</code> if no policy is defined in the form/grid for the specified button, <code>false</code> if there exists a disabilitation policy for the specified button (through addButtonsNotEnabledOnState form/grid method)
   */
  public boolean isButtonDisabled(GenericButton button) {
    if (headerFormPanel.getVOModel()==null)
      return false;
    DetailSaleDocVO vo = (DetailSaleDocVO)headerFormPanel.getVOModel().getValueObject();
    if (vo!=null && vo.getDocStateDOC01()!=null && vo.getDocStateDOC01().equals(ApplicationConsts.CLOSED))
      return true;
    else
      return false;
  }
View Full Code Here

        headerFormPanel.executeReload();
        if (getInvoices()!=null)
          getInvoices().reloadCurrentBlockOfData();

          // check if there is only one instalment and this instalment has 0 instalment days
          DetailSaleDocVO vo = (DetailSaleDocVO)headerFormPanel.getVOModel().getValueObject();
          res = ClientUtils.getData("validatePaymentCode",new LookupValidationParams(vo.getPaymentCodeReg10DOC01(),new HashMap()));
          if (res.isError()) {
            OptionPane.showMessageDialog(
                  ClientUtils.getParentFrame(this),
                  res.getErrorMessage(),
                  ClientSettings.getInstance().getResources().getResource("invoice closing"),
                  JOptionPane.ERROR_MESSAGE
            );
          }
          else {
            PaymentVO payVO = (PaymentVO)((VOListResponse)res).getRows().get(0);
            if (payVO.getInstalmentNumberREG10().intValue()==1 &&
                payVO.getStepREG10().intValue()==0 &&
                payVO.getFirstInstalmentDaysREG10().intValue()==0 &&
                payVO.getStartDayREG10().equals(ApplicationConsts.START_DAY_INVOICE_DATE)) {
              // there is only one instalment and this instalment has 0 instalment days:
              // prompt user for an immediate payment...
              if (OptionPane.showConfirmDialog(
                    ClientUtils.getParentFrame(this),
                    "create payment immediately",
                    ClientSettings.getInstance().getResources().getResource("invoice closing"),
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE
              )==JOptionPane.YES_OPTION) {
                res = ClientUtils.getData("payImmediately",new Object[]{
                        vo.getCompanyCodeSys01DOC01(),
                        vo.getDocTypeDOC01(),
                        vo.getDocYearDOC01(),
                        vo.getDocNumberDOC01(),
                        vo.getDocSequenceDOC01()
                });

                if (res.isError())
                  OptionPane.showMessageDialog(
                        ClientUtils.getParentFrame(this),
View Full Code Here

  public void enabledConfirmButton() {
    confirmButton.setEnabled(true);
  }

  void printButton_actionPerformed(ActionEvent e) {
    DetailSaleDocVO vo = (DetailSaleDocVO)headerFormPanel.getVOModel().getValueObject();

    HashMap params = new HashMap();
    params.put("COMPANY_CODE",vo.getCompanyCodeSys01DOC01());
    params.put("DOC_TYPE",vo.getDocTypeDOC01());
    params.put("DOC_YEAR",vo.getDocYearDOC01());
    params.put("DOC_NUMBER",vo.getDocNumberDOC01());

    HashMap map = new HashMap();
    map.put(ApplicationConsts.COMPANY_CODE_SYS01,vo.getCompanyCodeSys01DOC01());
    map.put(ApplicationConsts.FUNCTION_CODE_SYS06,headerFormPanel.getFunctionId());
    map.put(ApplicationConsts.EXPORT_PARAMS,params);
    Response res = ClientUtils.getData("getJasperReport",map);
    if (!res.isError()) {
      JasperPrint print = (JasperPrint)((VOResponse)res).getVo();
View Full Code Here

      vatRegisterAction.setConn(conn); // use same transaction...
      parsBean.setConn(conn); // use same transaction...
      loadSaleDocRowBean.setConn(conn);

      // retrieve document header...
      DetailSaleDocVO docVO = docAction.loadSaleDoc(pk,serverLanguageId,username,new ArrayList());

      if (docVO.getTotalDOC01()==null || docVO.getTotalDOC01().doubleValue()==0)
        throw new Exception(t16);

      // retrieve company currency code and currency conversion factor...
      String companyCurrencyCode = CurrencyConversionUtils.getCompanyCurrencyCode(docVO.getCompanyCodeSys01DOC01(),conn);
      BigDecimal conv = CurrencyConversionUtils.getConversionFactor(docVO.getDocDateDOC01(),docVO.getCurrencyCodeReg03DOC01(),companyCurrencyCode,conn);

      // retrieve document item rows...
      GridParams gridParams = new GridParams();
      gridParams.getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
      Response res = rowsAction.loadSaleDocRows(variant1Descriptions,variant2Descriptions,variant3Descriptions,variant4Descriptions,variant5Descriptions,gridParams,serverLanguageId,username);
      if (res.isError())
        throw new Exception(res.getErrorMessage());
      ArrayList rows = new ArrayList(((VOListResponse)res).getRows());
      ArrayList itemRows = rows;


      // retrieve payment instalments...
      ArrayList compList = new ArrayList();
      compList.add(docVO.getCompanyCodeSys01DOC01());
      res = payAction.validatePaymentCode(new LookupValidationParams(docVO.getPaymentCodeReg10DOC01(),new HashMap()),serverLanguageId,username,new ArrayList(),compList);
      if (res.isError()) {
        throw new Exception(res.getErrorMessage());
      }
      PaymentVO payVO = (PaymentVO)((VOListResponse)res).getRows().get(0);

      gridParams = new GridParams();
      gridParams.getOtherGridParams().put(ApplicationConsts.COMPANY_CODE_SYS01,docVO.getCompanyCodeSys01DOC01());
      gridParams.getOtherGridParams().put(ApplicationConsts.PAYMENT_CODE_REG10,docVO.getPaymentCodeReg10DOC01());
      res = payAction.loadPaymentInstalments(gridParams,serverLanguageId,username);
      if (res.isError()) {
        throw new Exception(res.getErrorMessage());
      }
      ArrayList paymentInstallments = new ArrayList(((VOListResponse)res).getRows());


      if (pk.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {

        // there must be only one instalment and this instalment has 0 instalment days
        if (paymentInstallments.size()>1 || ((PaymentInstalmentVO)paymentInstallments.get(0)).getInstalmentDaysREG17().intValue()>0) {
          throw new Exception(t17);
       }

        // check if all items are available...
        GridSaleDocRowVO vo = null;
        DetailSaleDocRowVO detailVO = null;
        gridParams = new GridParams();
        BookedItemQtyVO availVO = null;
        java.util.List availRows = null;
        for(int i=0;i<rows.size();i++) {
          vo = (GridSaleDocRowVO)rows.get(i);
          gridParams.getOtherGridParams().put(ApplicationConsts.WAREHOUSE_CODE,docVO.getWarehouseCodeWar01DOC01());
          gridParams.getOtherGridParams().put(ApplicationConsts.ITEM_PK,new ItemPK(vo.getCompanyCodeSys01DOC02(),vo.getItemCodeItm01DOC02()));
          res = availAction.loadBookedItems(variant1Descriptions,variant2Descriptions,variant3Descriptions,variant4Descriptions,variant5Descriptions,gridParams,serverLanguageId,username,companiesList);
          if (res.isError()) {
            throw new Exception(res.getErrorMessage());
          }
          availRows = ((VOListResponse)res).getRows();
          if (availRows.size()>0) {
            availVO = (BookedItemQtyVO) availRows.get(0);
            if (availVO.getAvailableQtyWAR03().doubleValue()>=vo.getQtyDOC02().doubleValue()) {
              // unload item from the specified warehouse...
              res = loadSaleDocRowBean.loadSaleDocRow(
                  variant1Descriptions,variant2Descriptions,variant3Descriptions,variant4Descriptions,variant5Descriptions,
                  new SaleDocRowPK(
                    vo.getCompanyCodeSys01DOC02(),
                    vo.getDocTypeDOC02(),
                    vo.getDocYearDOC02(),
                    vo.getDocNumberDOC02(),
                    vo.getItemCodeItm01DOC02(),
                    vo.getVariantTypeItm06DOC02(),
                    vo.getVariantCodeItm11DOC02(),
                    vo.getVariantTypeItm07DOC02(),
                    vo.getVariantCodeItm12DOC02(),
                    vo.getVariantTypeItm08DOC02(),
                    vo.getVariantCodeItm13DOC02(),
                    vo.getVariantTypeItm09DOC02(),
                    vo.getVariantCodeItm14DOC02(),
                    vo.getVariantTypeItm10DOC02(),
                    vo.getVariantCodeItm15DOC02()

                  ),
                  serverLanguageId,
                  username

              );
              if (res.isError()) {
                throw new Exception(res.getErrorMessage());
              }
              detailVO = (DetailSaleDocRowVO)((VOResponse)res).getVo();
              WarehouseMovementVO movVO = new WarehouseMovementVO(
                  detailVO.getProgressiveHie01DOC02(),
                  vo.getQtyDOC02(),
                  vo.getCompanyCodeSys01DOC02(),
                  docVO.getWarehouseCodeWar01DOC01(),
                  vo.getItemCodeItm01DOC02(),
                  ApplicationConsts.WAREHOUSE_MOTIVE_DIRECTLY_UNLOAD,
                  ApplicationConsts.ITEM_GOOD,
                  t2+" "+docVO.getDocTypeDOC01()+"/"+docVO.getDocNumberDOC01()+"/"+docVO.getDocYearDOC01(),
                  detailVO.getSerialNumbers(),

                  vo.getVariantCodeItm11DOC02(),
                  vo.getVariantCodeItm12DOC02(),
                  vo.getVariantCodeItm13DOC02(),
                  vo.getVariantCodeItm14DOC02(),
                  vo.getVariantCodeItm15DOC02(),
                  vo.getVariantTypeItm06DOC02(),
                  vo.getVariantTypeItm07DOC02(),
                  vo.getVariantTypeItm08DOC02(),
                  vo.getVariantTypeItm09DOC02(),
                  vo.getVariantTypeItm10DOC02()

              );
              res = movBean.addWarehouseMovement(movVO,t15,serverLanguageId,username);
              if (res.isError()) {
                throw new Exception(res.getErrorMessage());
              }
            }
            else {
              throw new Exception(t1);
            }
          }
          else {
            throw new Exception(t1);
          }
        }
      } // end if SALE_DESK_DOC_TYPE (check item availabilities...)



      // check if this document is a sale invoice and has a linked sale document:
      // if this is the case, then the linked document will be updated...
      if ((docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
           docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE)) &&
        docVO.getDocNumberDoc01DOC01()!=null
      ) {
        SaleDocPK refPK = new SaleDocPK(
          docVO.getCompanyCodeSys01Doc01DOC01(),
          docVO.getDocTypeDoc01DOC01(),
          docVO.getDocYearDoc01DOC01(),
          docVO.getDocNumberDoc01DOC01()
        );

        // retrieve ref. document item rows...
        GridSaleDocRowVO vo = null;
        DetailSaleDocRowVO refDetailVO = null;
        BigDecimal qty = null;
        BigDecimal invoiceQty = null;
        String docType = null;
        BigDecimal docYear = null;
        BigDecimal docNumber = null;
        BigDecimal rowNumber = null;
        for(int i=0;i<rows.size();i++) {
          vo = (GridSaleDocRowVO)rows.get(i);
          res = loadSaleDocRowBean.loadSaleDocRow(
              variant1Descriptions,variant2Descriptions,variant3Descriptions,variant4Descriptions,variant5Descriptions,
              new SaleDocRowPK(
                docVO.getCompanyCodeSys01Doc01DOC01(),
                docVO.getDocTypeDoc01DOC01(),
                docVO.getDocYearDoc01DOC01(),
                docVO.getDocNumberDoc01DOC01(),
                vo.getItemCodeItm01DOC02(),
                vo.getVariantTypeItm06DOC02(),
                vo.getVariantCodeItm11DOC02(),
                vo.getVariantTypeItm07DOC02(),
                vo.getVariantCodeItm12DOC02(),
                vo.getVariantTypeItm08DOC02(),
                vo.getVariantCodeItm13DOC02(),
                vo.getVariantTypeItm09DOC02(),
                vo.getVariantCodeItm14DOC02(),
                vo.getVariantTypeItm10DOC02(),
                vo.getVariantCodeItm15DOC02()
              ),
              serverLanguageId,
              username

          );
          if (res.isError()) {
            throw new Exception(res.getErrorMessage());
          }
          refDetailVO = (DetailSaleDocRowVO)((VOResponse)res).getVo();
          refDetailVO.setInvoiceQtyDOC02(
              refDetailVO.getInvoiceQtyDOC02().add(vo.getQtyDOC02()).setScale(docVO.getDecimalsREG03().intValue(),BigDecimal.ROUND_HALF_UP)
          );
          if (refDetailVO.getInvoiceQtyDOC02().doubleValue()>refDetailVO.getQtyDOC02().doubleValue())
            refDetailVO.setInvoiceQtyDOC02( refDetailVO.getQtyDOC02() );

            // update ref. item row...
          pstmt = conn.prepareStatement(
            "update DOC02_SELLING_ITEMS set INVOICE_QTY=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=?  where "+
            "COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and ITEM_CODE_ITM01=? and "+
            "VARIANT_TYPE_ITM06=? and VARIANT_CODE_ITM11=? and "+
            "VARIANT_TYPE_ITM07=? and VARIANT_CODE_ITM12=? and "+
            "VARIANT_TYPE_ITM08=? and VARIANT_CODE_ITM13=? and "+
            "VARIANT_TYPE_ITM09=? and VARIANT_CODE_ITM14=? and "+
            "VARIANT_TYPE_ITM10=? and VARIANT_CODE_ITM15=? "
          );
          pstmt.setBigDecimal(1,refDetailVO.getInvoiceQtyDOC02());
          pstmt.setString(2,username);
          pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
          pstmt.setString(4,refDetailVO.getCompanyCodeSys01DOC02());
          pstmt.setString(5,refDetailVO.getDocTypeDOC02());
          pstmt.setBigDecimal(6,refDetailVO.getDocYearDOC02());
          pstmt.setBigDecimal(7,refDetailVO.getDocNumberDOC02());
          pstmt.setString(8,refDetailVO.getItemCodeItm01DOC02());

          pstmt.setString(9,refDetailVO.getVariantTypeItm06DOC02());
          pstmt.setString(10,refDetailVO.getVariantCodeItm11DOC02());
          pstmt.setString(11,refDetailVO.getVariantTypeItm07DOC02());
          pstmt.setString(12,refDetailVO.getVariantCodeItm12DOC02());
          pstmt.setString(13,refDetailVO.getVariantTypeItm08DOC02());
          pstmt.setString(14,refDetailVO.getVariantCodeItm13DOC02());
          pstmt.setString(15,refDetailVO.getVariantTypeItm09DOC02());
          pstmt.setString(16,refDetailVO.getVariantCodeItm14DOC02());
          pstmt.setString(17,refDetailVO.getVariantTypeItm10DOC02());
          pstmt.setString(18,refDetailVO.getVariantCodeItm15DOC02());

          pstmt.execute();
          pstmt.close();

          // update ref. item row in the out delivery note...
          pstmt2 = conn.prepareStatement(
            "select QTY,INVOICE_QTY,DOC_TYPE,DOC_YEAR,DOC_NUMBER,ROW_NUMBER from DOC10_OUT_DELIVERY_NOTE_ITEMS where "+
            "COMPANY_CODE_SYS01=? and DOC_TYPE_DOC01=? and DOC_YEAR_DOC01=? and DOC_NUMBER_DOC01=? and ITEM_CODE_ITM01=? and INVOICE_QTY<QTY and "+
            "VARIANT_TYPE_ITM06=? and VARIANT_CODE_ITM11=? and "+
            "VARIANT_TYPE_ITM07=? and VARIANT_CODE_ITM12=? and "+
            "VARIANT_TYPE_ITM08=? and VARIANT_CODE_ITM13=? and "+
            "VARIANT_TYPE_ITM09=? and VARIANT_CODE_ITM14=? and "+
            "VARIANT_TYPE_ITM10=? and VARIANT_CODE_ITM15=? "
          );
          qty = null;
          invoiceQty = null;

          pstmt2.setString(1,refDetailVO.getCompanyCodeSys01DOC02());
          pstmt2.setString(2,refDetailVO.getDocTypeDOC02());
          pstmt2.setBigDecimal(3,refDetailVO.getDocYearDOC02());
          pstmt2.setBigDecimal(4,refDetailVO.getDocNumberDOC02());
          pstmt2.setString(5,refDetailVO.getItemCodeItm01DOC02());

          pstmt2.setString(6,refDetailVO.getVariantTypeItm06DOC02());
          pstmt2.setString(7,refDetailVO.getVariantCodeItm11DOC02());
          pstmt2.setString(8,refDetailVO.getVariantTypeItm07DOC02());
          pstmt2.setString(9,refDetailVO.getVariantCodeItm12DOC02());
          pstmt2.setString(10,refDetailVO.getVariantTypeItm08DOC02());
          pstmt2.setString(11,refDetailVO.getVariantCodeItm13DOC02());
          pstmt2.setString(12,refDetailVO.getVariantTypeItm09DOC02());
          pstmt2.setString(13,refDetailVO.getVariantCodeItm14DOC02());
          pstmt2.setString(14,refDetailVO.getVariantTypeItm10DOC02());
          pstmt2.setString(15,refDetailVO.getVariantCodeItm15DOC02());

          rset = pstmt2.executeQuery();

          // it only updates one row, that matches the where clause...
          if(rset.next()) {
            qty = rset.getBigDecimal(1);
            invoiceQty = rset.getBigDecimal(2);
            docType = rset.getString(3);
            docYear = rset.getBigDecimal(4);
            docNumber = rset.getBigDecimal(5);
            rowNumber = rset.getBigDecimal(6);
          }
          rset.close();
          pstmt2.close();

          if (qty!=null && invoiceQty!=null) {
            if (invoiceQty.doubleValue()+vo.getQtyDOC02().doubleValue()<=qty.doubleValue())
              qty = invoiceQty.add(vo.getQtyDOC02());

            pstmt = conn.prepareStatement(
              "update DOC10_OUT_DELIVERY_NOTE_ITEMS set INVOICE_QTY=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=?  where "+
              "COMPANY_CODE_SYS01=? and DOC_TYPE_DOC01=? and DOC_YEAR_DOC01=? and DOC_NUMBER_DOC01=? and ITEM_CODE_ITM01=? and "+
              "DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and ROW_NUMBER=? and "+
              "VARIANT_TYPE_ITM06=? and VARIANT_CODE_ITM11=? and "+
              "VARIANT_TYPE_ITM07=? and VARIANT_CODE_ITM12=? and "+
              "VARIANT_TYPE_ITM08=? and VARIANT_CODE_ITM13=? and "+
              "VARIANT_TYPE_ITM09=? and VARIANT_CODE_ITM14=? and "+
              "VARIANT_TYPE_ITM10=? and VARIANT_CODE_ITM15=? "
            );
            pstmt.setBigDecimal(1,qty);
            pstmt.setString(2,username);
            pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
            pstmt.setString(4,refDetailVO.getCompanyCodeSys01DOC02());
            pstmt.setString(5,refDetailVO.getDocTypeDOC02());
            pstmt.setBigDecimal(6,refDetailVO.getDocYearDOC02());
            pstmt.setBigDecimal(7,refDetailVO.getDocNumberDOC02());
            pstmt.setString(8,refDetailVO.getItemCodeItm01DOC02());
            pstmt.setString(9,docType);
            pstmt.setBigDecimal(10,docYear);
            pstmt.setBigDecimal(11,docNumber);
            pstmt.setBigDecimal(12,rowNumber);

            pstmt.setString(13,refDetailVO.getVariantTypeItm06DOC02());
            pstmt.setString(14,refDetailVO.getVariantCodeItm11DOC02());
            pstmt.setString(15,refDetailVO.getVariantTypeItm07DOC02());
            pstmt.setString(16,refDetailVO.getVariantCodeItm12DOC02());
            pstmt.setString(17,refDetailVO.getVariantTypeItm08DOC02());
            pstmt.setString(18,refDetailVO.getVariantCodeItm13DOC02());
            pstmt.setString(19,refDetailVO.getVariantTypeItm09DOC02());
            pstmt.setString(20,refDetailVO.getVariantCodeItm14DOC02());
            pstmt.setString(21,refDetailVO.getVariantTypeItm10DOC02());
            pstmt.setString(22,refDetailVO.getVariantCodeItm15DOC02());

            pstmt.execute();
            pstmt.close();
          }
        } // end for (used to update invoice qtys in referred docs...)

        // retrieve document value charges...
        gridParams = new GridParams();
        gridParams.getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
        res = rowsAction.loadSaleDocCharges(gridParams,serverLanguageId,username);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }

        SaleDocChargeVO chargeVO = null;
        rows = new ArrayList(((VOListResponse)res).getRows());
        BigDecimal valueDOC03,invoicedValueDOC03 = new BigDecimal(0);
        for(int i=0;i<rows.size();i++) {
          chargeVO = (SaleDocChargeVO)rows.get(i);
          if (chargeVO.getValueDOC03()!=null) {

            pstmt = conn.prepareStatement(
              "select VALUE,INVOICED_VALUE from DOC03_SELLING_CHARGES where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and CHARGE_CODE_SAL06=?"
            );
            pstmt.setString(1,refPK.getCompanyCodeSys01DOC01());
            pstmt.setString(2,refPK.getDocTypeDOC01());
            pstmt.setBigDecimal(3,refPK.getDocYearDOC01());
            pstmt.setBigDecimal(4,refPK.getDocNumberDOC01());
            pstmt.setString(5,chargeVO.getChargeCodeSal06DOC03());
            rset = pstmt.executeQuery();
            if (rset.next()) {
              valueDOC03 = rset.getBigDecimal(1);
              invoicedValueDOC03 = rset.getBigDecimal(2);

              invoicedValueDOC03 = invoicedValueDOC03.add(chargeVO.getValueDOC03()).setScale(docVO.getDecimalsREG03().intValue(),BigDecimal.ROUND_HALF_UP);
              if (invoicedValueDOC03.doubleValue()>valueDOC03.doubleValue())
                invoicedValueDOC03 = valueDOC03;

            }
            rset.next();
            pstmt.close();

            pstmt = conn.prepareStatement(
              "update DOC03_SELLING_CHARGES set INVOICED_VALUE=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=?  where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and CHARGE_CODE_SAL06=?"
            );
            pstmt.setBigDecimal(1,invoicedValueDOC03);
            pstmt.setString(2,username);
            pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
            pstmt.setString(4,refPK.getCompanyCodeSys01DOC01());
            pstmt.setString(5,refPK.getDocTypeDOC01());
            pstmt.setBigDecimal(6,refPK.getDocYearDOC01());
            pstmt.setBigDecimal(7,refPK.getDocNumberDOC01());
            pstmt.setString(8,chargeVO.getChargeCodeSal06DOC03());
            pstmt.execute();
            pstmt.close();

          }
        } // end for (used to update invoice qtys in referred docs...)

        // retrieve document activities...
        gridParams = new GridParams();
        gridParams.getOtherGridParams().put(ApplicationConsts.SALE_DOC_PK,pk);
        res = rowsAction.loadSaleDocActivities(gridParams,serverLanguageId,username);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }

        BigDecimal valueDOC13,invoicedValueDOC13 = new BigDecimal(0);
        SaleDocActivityVO actVO = null;
        rows = new ArrayList(((VOListResponse)res).getRows());
        for(int i=0;i<rows.size();i++) {
          actVO = (SaleDocActivityVO)rows.get(i);
          pstmt = conn.prepareStatement(
            "select VALUE,INVOICED_VALUE from DOC13_SELLING_ACTIVITIES where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and ACTIVITY_CODE_SAL09=?"
          );
          pstmt.setString(1,refPK.getCompanyCodeSys01DOC01());
          pstmt.setString(2,refPK.getDocTypeDOC01());
          pstmt.setBigDecimal(3,refPK.getDocYearDOC01());
          pstmt.setBigDecimal(4,refPK.getDocNumberDOC01());
          pstmt.setString(5,actVO.getActivityCodeSal09DOC13());
          rset = pstmt.executeQuery();
          if (rset.next()) {
            valueDOC13 = rset.getBigDecimal(1);
            invoicedValueDOC13 = rset.getBigDecimal(2);

            invoicedValueDOC13 = invoicedValueDOC03.add(actVO.getValueDOC13()).setScale(docVO.getDecimalsREG03().intValue(),BigDecimal.ROUND_HALF_UP);
            if (invoicedValueDOC13.doubleValue()>valueDOC13.doubleValue())
              invoicedValueDOC13 = valueDOC13;

          }
          rset.next();
          pstmt.close();

          pstmt = conn.prepareStatement(
            "update DOC13_SELLING_ACTIVITIES set INVOICED_VALUE=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=?  where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and ACTIVITY_CODE_SAL09=?"
          );
          pstmt.setBigDecimal(1,invoicedValueDOC13);
          pstmt.setString(2,username);
          pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
          pstmt.setString(4,refPK.getCompanyCodeSys01DOC01());
          pstmt.setString(5,refPK.getDocTypeDOC01());
          pstmt.setBigDecimal(6,refPK.getDocYearDOC01());
          pstmt.setBigDecimal(7,refPK.getDocNumberDOC01());
          pstmt.setString(8,actVO.getActivityCodeSal09DOC13());
          pstmt.execute();
          pstmt.close();
        } // end for (used to update invoice qtys in referred docs...)

        // check if linked document can be closed...
        boolean canCloseLinkedDoc = true;
        pstmt = conn.prepareStatement(
          "select QTY from DOC02_SELLING_ITEMS where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and QTY-INVOICE_QTY>0"
        );
        pstmt.setString(1,refPK.getCompanyCodeSys01DOC01());
        pstmt.setString(2,refPK.getDocTypeDOC01());
        pstmt.setBigDecimal(3,refPK.getDocYearDOC01());
        pstmt.setBigDecimal(4,refPK.getDocNumberDOC01());
        rset = pstmt.executeQuery();
        if (rset.next())
          canCloseLinkedDoc = false;
        rset.close();
        pstmt.close();

        if (canCloseLinkedDoc) {
          pstmt = conn.prepareStatement(
            "select VALUE from DOC13_SELLING_ACTIVITIES where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and VALUE-INVOICED_VALUE>0"
          );
          pstmt.setString(1,refPK.getCompanyCodeSys01DOC01());
          pstmt.setString(2,refPK.getDocTypeDOC01());
          pstmt.setBigDecimal(3,refPK.getDocYearDOC01());
          pstmt.setBigDecimal(4,refPK.getDocNumberDOC01());
          rset = pstmt.executeQuery();
          if (rset.next())
            canCloseLinkedDoc = false;
          rset.close();
          pstmt.close();

          if (canCloseLinkedDoc) {
            pstmt = conn.prepareStatement(
              "select VALUE from DOC03_SELLING_CHARGES where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=? and VALUE is not null and VALUE-INVOICED_VALUE>0"
            );
            pstmt.setString(1,refPK.getCompanyCodeSys01DOC01());
            pstmt.setString(2,refPK.getDocTypeDOC01());
            pstmt.setBigDecimal(3,refPK.getDocYearDOC01());
            pstmt.setBigDecimal(4,refPK.getDocNumberDOC01());
            rset = pstmt.executeQuery();
            if (rset.next())
              canCloseLinkedDoc = false;
            rset.close();
            pstmt.close();

            if (canCloseLinkedDoc) {
              // the linked document can be closed...
              pstmt = conn.prepareStatement("update DOC01_SELLING set DOC_STATE=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=?  where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=?");
              pstmt.setString(1,ApplicationConsts.CLOSED);
              pstmt.setString(2,username);
              pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
              pstmt.setString(4,refPK.getCompanyCodeSys01DOC01());
              pstmt.setString(5,refPK.getDocTypeDOC01());
              pstmt.setBigDecimal(6,refPK.getDocYearDOC01());
              pstmt.setBigDecimal(7,refPK.getDocNumberDOC01());
              pstmt.execute();
            }
          }
        }

      } // end if (check if doc is of type SALE_INVOICE_FROM_DN_DOC_TYPE or SALE_INVOICE_FROM_SD_DOC_TYPE...)



      // generate progressive for doc. sequence...
      if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE) ||
          docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
          docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE)) {
        // invoice...
        pstmt = conn.prepareStatement(
          "select max(DOC_SEQUENCE) from DOC01_SELLING where COMPANY_CODE_SYS01=? and DOC_TYPE in (?,?,?) and DOC_YEAR=? and DOC_SEQUENCE is not null"
        );
        pstmt.setString(1,pk.getCompanyCodeSys01DOC01());
        pstmt.setString(2,ApplicationConsts.SALE_INVOICE_DOC_TYPE);
        pstmt.setString(3,ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE);
        pstmt.setString(4,ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE);
        pstmt.setBigDecimal(5,pk.getDocYearDOC01());

        Response sectRes = parsBean.loadCompanyParams(docVO.getCompanyCodeSys01DOC01(),serverLanguageId,username);
        if (!res.isError()) {
          CompanyParametersVO parsVO = (CompanyParametersVO)((VOResponse)sectRes).getVo();
          docVO.setSectionalDOC01(parsVO.getSaleSectionalDOC01());
        }

      }
      else {
        // other sale document (e.g. credit note)...
        pstmt = conn.prepareStatement(
          "select max(DOC_SEQUENCE) from DOC01_SELLING where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_SEQUENCE is not null"
        );
        pstmt.setString(1,pk.getCompanyCodeSys01DOC01());
        pstmt.setString(2,pk.getDocTypeDOC01());
        pstmt.setBigDecimal(3,pk.getDocYearDOC01());
      }
      rset = pstmt.executeQuery();
      int docSequenceDOC01 = 1;
      if (rset.next())
        docSequenceDOC01 = rset.getInt(1)+1;
      rset.close();
      pstmt.close();
      if (docVO.getDocSequenceDOC01() == null || docVO.getDocSequenceDOC01().intValue() == 0) {
        docVO.setDocSequenceDOC01(new BigDecimal(docSequenceDOC01));
      } else {
        docSequenceDOC01 = docVO.getDocSequenceDOC01().intValue();
      }


/*
      // 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 (paymentInstallments.size()>1 || (paymentInstallments.size()==1 && ((PaymentInstalmentVO)paymentInstallments.get(0)).getInstalmentDaysREG17().intValue()>0 )) {
*/


      HashMap map = new HashMap();
      map.put(ApplicationConsts.COMPANY_CODE_SYS01,docVO.getCompanyCodeSys01DOC01());
      map.put(ApplicationConsts.PARAM_CODE,ApplicationConsts.ROUNDING_PROCEEDS_CODE);
      res = parsBean.loadUserParam(map,serverLanguageId,username);
      if (res.isError()) {
        throw new Exception(res.getErrorMessage());
      }
      String roundingAccountCode = ((VOResponse)res).getVo().toString();

      // create ALWAYS expirations in DOC19...
      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,DESCRIPTION,CUSTOMER_SUPPLIER_CODE,PROGRESSIVE_REG04,"+
         "CURRENCY_CODE_REG03,PAYMENT_TYPE_CODE_REG11,PAYED,REAL_PAYMENT_TYPE_CODE_REG11,PAYED_DATE,PAYED_VALUE,"+
         "REAL_ACCOUNT_CODE_ACC02,ROUNDING_ACCOUNT_CODE_ACC02,ALREADY_PAYED,CREATE_USER,CREATE_DATE) "+
         "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
      );
      long startTime = docVO.getDocDateDOC01().getTime(); // invoice date...
      if (payVO.getStartDayREG10().equals(ApplicationConsts.START_DAY_END_MONTH)) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(startTime);
        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;
      BigDecimal progressiveDOC19 = null;

      pstmt3 = conn.prepareStatement(
        "INSERT INTO DOC27_PAYMENTS(COMPANY_CODE_SYS01,PROGRESSIVE,PAYMENT_DATE,PAYMENT_VALUE,CUSTOMER_SUPPLIER_CODE,"+
        "ACCOUNT_CODE_ACC02,PAYMENT_TYPE_CODE_REG11,CURRENCY_CODE_REG03,PROGRESSIVE_REG04,CREATE_USER,CREATE_DATE) "+
        "VALUES (?,?,?,?,?,?,?,?,?,?,?)"
      );
      pstmt2 = conn.prepareStatement(
        "INSERT INTO DOC28_PAYMENT_DISTRIBUTION(COMPANY_CODE_SYS01,PROGRESSIVE_DOC27,PROGRESSIVE_DOC19,"+
        "PAYMENT_VALUE,PAYED,CREATE_USER,CREATE_DATE) VALUES (?,?,?,?,?,?,?)"
      );

      for(int i=0;i<paymentInstallments.size();i++) {
        progressiveDOC19 = CompanyProgressiveUtils.getInternalProgressive(docVO.getCompanyCodeSys01DOC01(),"DOC19_EXPIRATIONS","PROGRESSIVE",conn);

        inVO = (PaymentInstalmentVO)paymentInstallments.get(i);
        pstmt.setString(1,docVO.getCompanyCodeSys01DOC01());
        pstmt.setString(2,docVO.getDocTypeDOC01());
        pstmt.setBigDecimal(3,docVO.getDocYearDOC01());
        pstmt.setBigDecimal(4,docVO.getDocNumberDOC01());
        pstmt.setBigDecimal(5,docVO.getDocSequenceDOC01());
        pstmt.setBigDecimal(6,progressiveDOC19);
        pstmt.setDate(7,docVO.getDocDateDOC01());
        pstmt.setDate(8,new java.sql.Date(startTime + inVO.getInstalmentDaysREG17().longValue()*86400*1000)); // expiration date
        pstmt.setString(9,docVO.getName_1REG04());
        pstmt.setString(10,docVO.getName_2REG04());

        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
          amount = docVO.getTotalDOC01().multiply(inVO.getPercentageREG17()).divide(new BigDecimal(-100),BigDecimal.ROUND_HALF_UP).setScale(docVO.getDecimalsREG03().intValue(),BigDecimal.ROUND_HALF_UP); // value
        else
          amount = docVO.getTotalDOC01().multiply(inVO.getPercentageREG17()).divide(new BigDecimal(100),BigDecimal.ROUND_HALF_UP).setScale(docVO.getDecimalsREG03().intValue(),BigDecimal.ROUND_HALF_UP); // value

        pstmt.setBigDecimal(11,CurrencyConversionUtils.convertCurrencyToCurrency(amount,conv));
        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE))
          pstmt.setString(12,t3+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+t8+" "+t9+" "+(i+1)+" - "+inVO.getPaymentTypeDescriptionSYS10()); // description
        else if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
          pstmt.setString(12,t7+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+t8+" "+t9+" "+(i+1)+" - "+inVO.getPaymentTypeDescriptionSYS10()); // description
        else
          pstmt.setString(12,t10+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+t8+" "+t9+" "+(i+1)+" - "+inVO.getPaymentTypeDescriptionSYS10()); // description

        pstmt.setString(13,docVO.getCustomerCodeSAL07());
        pstmt.setBigDecimal(14,docVO.getProgressiveReg04DOC01());
        pstmt.setString(15,companyCurrencyCode);
        pstmt.setString(16,payVO.getPaymentTypeCodeReg11REG10());

        if (pk.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {
          pstmt.setString(17,"Y");
          pstmt.setString(18,payVO.getPaymentTypeCodeReg11REG10());
          pstmt.setDate(19,new java.sql.Date(startTime + inVO.getInstalmentDaysREG17().longValue()*86400*1000));
          pstmt.setBigDecimal(20,CurrencyConversionUtils.convertCurrencyToCurrency(amount,conv));
          pstmt.setString(21,payVO.getAccountCodeAcc02REG11());
          pstmt.setString(22,roundingAccountCode);
          pstmt.setBigDecimal(23,CurrencyConversionUtils.convertCurrencyToCurrency(amount,conv));
          pstmt.setString(24,username);
          pstmt.setTimestamp(25,new java.sql.Timestamp(System.currentTimeMillis()));
        }
        else {
          pstmt.setString(17,"N");
          pstmt.setString(18,null);
          pstmt.setDate(19,null);
          pstmt.setBigDecimal(20,null);
          pstmt.setString(21,payVO.getAccountCodeAcc02REG11());
          pstmt.setString(22,roundingAccountCode);
          pstmt.setBigDecimal(23,new BigDecimal(0));
          pstmt.setString(24,username);
          pstmt.setTimestamp(25,new java.sql.Timestamp(System.currentTimeMillis()));
        }

        pstmt.execute();

        if (pk.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {

          // insert record in DOC27...
          BigDecimal progressiveDOC27 = CompanyProgressiveUtils.getInternalProgressive(
             docVO.getCompanyCodeSys01DOC01(),
             "DOC27_PAYMENTS",
             "PROGRESSIVE",
             conn
          );
          pstmt3.setString(1,docVO.getCompanyCodeSys01DOC01());
          pstmt3.setBigDecimal(2,progressiveDOC27);
          pstmt3.setDate(3,docVO.getDocDateDOC01());
          pstmt3.setBigDecimal(4,docVO.getTotalDOC01());
          pstmt3.setString(5,docVO.getCustomerCodeSAL07());
          pstmt3.setString(6,payVO.getAccountCodeAcc02REG11());
          pstmt3.setString(7,payVO.getPaymentTypeCodeReg11REG10());
          pstmt3.setString(8,docVO.getCurrencyCodeReg03DOC01());
          pstmt3.setBigDecimal(9,docVO.getProgressiveReg04DOC01());
          pstmt3.setString(10,username);
          pstmt3.setTimestamp(11,new java.sql.Timestamp(System.currentTimeMillis()));
          pstmt3.execute();

          // insert record in DOC28...
          pstmt2.setString(1,docVO.getCompanyCodeSys01DOC01());
          pstmt2.setBigDecimal(2,progressiveDOC27);
          pstmt2.setBigDecimal(3,progressiveDOC19);
          pstmt2.setBigDecimal(4,docVO.getTotalDOC01());
          pstmt2.setString(5,"Y");
          pstmt2.setString(6,username);
          pstmt2.setTimestamp(7,new java.sql.Timestamp(System.currentTimeMillis()));
          pstmt2.execute();

        } // end if on sale desk




      }
      pstmt.close();
//      }



      // change doc state to close...
      pstmt = conn.prepareStatement("update DOC01_SELLING set DOC_STATE=?,DOC_SEQUENCE=?,SECTIONAL=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=?  where COMPANY_CODE_SYS01=? and DOC_TYPE=? and DOC_YEAR=? and DOC_NUMBER=?");
      pstmt.setString(1,ApplicationConsts.CLOSED);
      pstmt.setInt(2,docSequenceDOC01);
      pstmt.setString(3,docVO.getSectionalDOC01());
      pstmt.setString(4,username);
      pstmt.setTimestamp(5,new java.sql.Timestamp(System.currentTimeMillis()));
      pstmt.setString(6,pk.getCompanyCodeSys01DOC01());
      pstmt.setString(7,pk.getDocTypeDOC01());
      pstmt.setBigDecimal(8,pk.getDocYearDOC01());
      pstmt.setBigDecimal(9,pk.getDocNumberDOC01());
      int rowNum = pstmt.executeUpdate();



      // if the document is a retail sale doc then
      // export the document in a text file format and call an external application to manage it...
      if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {
      res = expAction.exportToFile(t11,t12,t13,t14,pk,docVO,itemRows,serverLanguageId,username);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }
      }



      // if the document is a sale invoice or a sale retail document or a credit note, then generate an accounting item...
      if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
          docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE) ||
          docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE) ||
          docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE) ||
          docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {
        JournalHeaderVO jhVO = new JournalHeaderVO();
        jhVO.setCompanyCodeSys01ACC05(docVO.getCompanyCodeSys01DOC01());
        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE))
          jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_SALE_RETAIL);
        else
          jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_SALE_INVOICE);
        jhVO.setItemDateACC05(new java.sql.Date(System.currentTimeMillis()));
        jhVO.setItemYearACC05(new BigDecimal(Calendar.getInstance().get(Calendar.YEAR)));
        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE))
          jhVO.setDescriptionACC05(
              t3+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()
          );
        else if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
          jhVO.setDescriptionACC05(
              t4+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+
              t5+" "+docVO.getName_1REG04()+" "+(docVO.getName_2REG04()==null?"":docVO.getName_2REG04())
          );
        else
          jhVO.setDescriptionACC05(
              t6+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+
              t5+" "+docVO.getName_1REG04()+" "+(docVO.getName_2REG04()==null?"":docVO.getName_2REG04())
          );

        // determine account codes defined for the current customer...
        pstmt = conn.prepareStatement(
          "select CREDIT_ACCOUNT_CODE_ACC02,ITEMS_ACCOUNT_CODE_ACC02,ACTIVITIES_ACCOUNT_CODE_ACC02,CHARGES_ACCOUNT_CODE_ACC02 "+
          "from SAL07_CUSTOMERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?"
        );
        pstmt.setString(1,pk.getCompanyCodeSys01DOC01());
        pstmt.setBigDecimal(2,docVO.getProgressiveReg04DOC01());
        rset = pstmt.executeQuery();
        if (!rset.next()) {
          rset.close();
          throw new Exception("customer not found");
        }
        String creditAccountCodeAcc02 = rset.getString(1);
        String itemsAccountCodeAcc02 = rset.getString(2);
        String actAccountCodeAcc02 = rset.getString(3);
        String chargesAccountCodeAcc02 = rset.getString(4);
        rset.close();
        pstmt.close();

        // determine account code defined for vat in sale vat register...
        pstmt = conn.prepareStatement(
          "select ACCOUNT_CODE_ACC02 from ACC04_VAT_REGISTERS where COMPANY_CODE_SYS01=? and REGISTER_CODE=?"
        );
        pstmt.setString(1,pk.getCompanyCodeSys01DOC01());
        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE))
          pstmt.setString(2,ApplicationConsts.VAT_REGISTER_RETAIL);
        else
          pstmt.setString(2,ApplicationConsts.VAT_REGISTER_SELLING);
        rset = pstmt.executeQuery();
        if (!rset.next()) {
          rset.close();
          throw new Exception("vat register not found");
        }
        String vatAccountCodeAcc02 = rset.getString(1);
        rset.close();
        pstmt.close();

        // calculate taxable income rows, grouped by vat code...
        res = taxableIncomeAction.updateTaxableIncomes(
            variant1Descriptions,
            variant2Descriptions,
            variant3Descriptions,
            variant4Descriptions,
            variant5Descriptions,
            pk, serverLanguageId, username);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }

        // add taxable income rows to the accounting item...
        java.util.List taxableIncomes = ((VOListResponse)res).getRows();
        TaxableIncomeVO tVO = null;
        JournalRowVO jrVO = null;
        BigDecimal totalVat = new BigDecimal(0);
        Hashtable vats = new Hashtable(); // collections of: vat code,VatRowVO
        VatRowVO vatVO = null;
        for(int i=0;i<taxableIncomes.size();i++) {
          tVO = (TaxableIncomeVO)taxableIncomes.get(i);
          totalVat = totalVat.add(tVO.getVatValue());
          jrVO = new JournalRowVO();
          jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
          if (tVO.getRowType()==tVO.ITEM_ROW_TYPE) {
            jrVO.setAccountCodeAcc02ACC06(itemsAccountCodeAcc02);
            jrVO.setAccountCodeACC06(itemsAccountCodeAcc02);
          }
          else if (tVO.getRowType()==tVO.ACTIVITY_ROW_TYPE) {
            jrVO.setAccountCodeAcc02ACC06(actAccountCodeAcc02);
            jrVO.setAccountCodeACC06(actAccountCodeAcc02);
          }
          else if (tVO.getRowType()==tVO.CHARGE_ROW_TYPE) {
            jrVO.setAccountCodeAcc02ACC06(chargesAccountCodeAcc02);
            jrVO.setAccountCodeACC06(chargesAccountCodeAcc02);
          }
          jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);

          if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
            jrVO.setCreditAmountACC06(tVO.getTaxableIncome().negate());
          else
            jrVO.setCreditAmountACC06(tVO.getTaxableIncome());
          jrVO.setCreditAmountACC06(CurrencyConversionUtils.convertCurrencyToCurrency(jrVO.getCreditAmountACC06(),conv));

          jrVO.setDescriptionACC06("");
          jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
          jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
          jhVO.addJournalRow(jrVO);

          // prepare vat row for sale vat register...
          vatVO = (VatRowVO)vats.get(tVO.getVatCode());
          if (vatVO==null) {
            vatVO = new VatRowVO();
            vatVO.setCompanyCodeSys01ACC07(docVO.getCompanyCodeSys01DOC01());
            if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE))
              vatVO.setRegisterCodeAcc04ACC07(ApplicationConsts.VAT_REGISTER_RETAIL);
            else
              vatVO.setRegisterCodeAcc04ACC07(ApplicationConsts.VAT_REGISTER_SELLING);
            vatVO.setTaxableIncomeACC07(tVO.getTaxableIncome());
            vatVO.setTaxableIncomeACC07(CurrencyConversionUtils.convertCurrencyToCurrency(vatVO.getTaxableIncomeACC07(),conv));
            vatVO.setVatCodeACC07(tVO.getVatCode());
            vatVO.setVatDateACC07(new java.sql.Date(System.currentTimeMillis()));
            vatVO.setVatDescriptionACC07(tVO.getVatDescription());

            if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
              vatVO.setVatValueACC07(tVO.getVatValue().negate());
            else
              vatVO.setVatValueACC07(tVO.getVatValue());
            vatVO.setVatValueACC07(CurrencyConversionUtils.convertCurrencyToCurrency(vatVO.getVatValueACC07(),conv));

            vatVO.setVatYearACC07(new BigDecimal(Calendar.getInstance().get(Calendar.YEAR)));
          }
          else {
            vatVO.setTaxableIncomeACC07(vatVO.getTaxableIncomeACC07().add(tVO.getTaxableIncome()));
            vatVO.setTaxableIncomeACC07(CurrencyConversionUtils.convertCurrencyToCurrency(vatVO.getTaxableIncomeACC07(),conv));
            if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
              vatVO.setVatValueACC07(vatVO.getVatValueACC07().add(tVO.getVatValue()).negate());
            else
              vatVO.setVatValueACC07(vatVO.getVatValueACC07().add(tVO.getVatValue()));
            vatVO.setVatValueACC07(CurrencyConversionUtils.convertCurrencyToCurrency(vatVO.getVatValueACC07(),conv));
          }
          vats.put(tVO.getVatCode(),vatVO);
        }

        // add total vat value to the accounting item...
        jrVO = new JournalRowVO();
        jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
        jrVO.setAccountCodeAcc02ACC06(vatAccountCodeAcc02);
        jrVO.setAccountCodeACC06(vatAccountCodeAcc02);
        jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);

        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
          jrVO.setCreditAmountACC06(totalVat.negate());
        else
          jrVO.setCreditAmountACC06(totalVat);
        jrVO.setCreditAmountACC06(CurrencyConversionUtils.convertCurrencyToCurrency(jrVO.getCreditAmountACC06(),conv));

        jrVO.setDescriptionACC06("");
        jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
        jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
        jhVO.addJournalRow(jrVO);

        // add total credit value to the accounting item...
        jrVO = new JournalRowVO();
        jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
        jrVO.setAccountCodeAcc02ACC06(creditAccountCodeAcc02);
        jrVO.setAccountCodeACC06(docVO.getCustomerCodeSAL07());
        jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_CUSTOMER);

        if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
          jrVO.setDebitAmountACC06(docVO.getTotalDOC01().negate());
        else
          jrVO.setDebitAmountACC06(docVO.getTotalDOC01());
        jrVO.setDebitAmountACC06(CurrencyConversionUtils.convertCurrencyToCurrency(jrVO.getDebitAmountACC06(),conv));

        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());
        }

        // insert vat rows in sale vat register...
        ArrayList vatRows = new ArrayList();
        Enumeration en = vats.keys();
        while(en.hasMoreElements())
          vatRows.add(vats.get(en.nextElement()));
        res = vatRegisterAction.insertVatRows(vatRows,serverLanguageId,username);
        if (res.isError()) {
          throw new Exception(res.getErrorMessage());
        }

/*
        // 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 (paymentInstallments.size()==1 && ((PaymentInstalmentVO)paymentInstallments.get(0)).getInstalmentDaysREG17().intValue()==0) {
*/

         if (pk.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {

          map = new HashMap();
          map.put(ApplicationConsts.COMPANY_CODE_SYS01,docVO.getCompanyCodeSys01DOC01());
          map.put(ApplicationConsts.PARAM_CODE,ApplicationConsts.CASE_ACCOUNT);
          res = parsBean.loadUserParam(map,serverLanguageId,username);
          if (res.isError()) {
            throw new Exception(res.getErrorMessage());
          }
          String caseAccountCode = ((VOResponse)res).getVo().toString();


          jhVO = new JournalHeaderVO();
          jhVO.setCompanyCodeSys01ACC05(docVO.getCompanyCodeSys01DOC01());
          if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_DESK_DOC_TYPE)) {
            jhVO.setDescriptionACC05(
                t3+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()
            );
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_SALE_RETAIL_PROCEEDS);
          }
          else if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE)) {
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_INVOICE_PROCEEDS);
            jhVO.setDescriptionACC05(
                t4+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+
                t5+" "+docVO.getName_1REG04()+" "+(docVO.getName_2REG04()==null?"":docVO.getName_2REG04())
            );
          }
          else {
            jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_INVOICE_PROCEEDS);
            jhVO.setDescriptionACC05(
                t6+" "+docVO.getDocSequenceDOC01()+"/"+docVO.getDocYearDOC01()+" - "+
                t5+" "+docVO.getName_1REG04()+" "+(docVO.getName_2REG04()==null?"":docVO.getName_2REG04())
            );
          }

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

          jrVO = new JournalRowVO();
          jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
          jrVO.setAccountCodeAcc02ACC06(creditAccountCodeAcc02);
          jrVO.setAccountCodeACC06(docVO.getCustomerCodeSAL07());
          jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_CUSTOMER);

          if (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
            jrVO.setCreditAmountACC06(docVO.getTotalDOC01().negate());
          else
            jrVO.setCreditAmountACC06(docVO.getTotalDOC01());
          jrVO.setCreditAmountACC06(CurrencyConversionUtils.convertCurrencyToCurrency(jrVO.getCreditAmountACC06(),conv));

          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 (docVO.getDocTypeDOC01().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE))
            jrVO.setDebitAmountACC06(docVO.getTotalDOC01().negate());
          else
            jrVO.setDebitAmountACC06(docVO.getTotalDOC01());
          jrVO.setDebitAmountACC06(CurrencyConversionUtils.convertCurrencyToCurrency(jrVO.getDebitAmountACC06(),conv));

          jrVO.setDescriptionACC06("");
          jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
          jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
View Full Code Here

TOP

Related Classes of org.jallinone.sales.documents.java.DetailSaleDocVO

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.