/**
   * ************************************************************************ Save Multiple records
   * - Clone a record and assign new values to each clone for a specific column.
   *
   * @param ctx context
   * @param tableName Table Name
   * @param columnName Column for which value need to be changed
   * @param recordId Record to clone
   * @param values Values to be assigned to clones for the specified column
   * @param trxName Transaction
   * @throws Exception If error is occured when loading the PO or saving clones
   * @author ashley
   */
  protected void saveMultipleRecords(
      Properties ctx,
      String tableName,
      String columnName,
      int recordId,
      Integer[] values,
      String trxName)
      throws Exception {
    if (values == null) {
      return;
    }

    int oldRow = gridTab.getCurrentRow();
    GridField lineField = gridTab.getField("Line");

    for (int i = 0; i < values.length; i++) {
      if (!gridTab.dataNew(true)) {
        throw new IllegalStateException("Could not clone tab");
      }

      gridTab.setValue(columnName, values[i]);

      if (lineField != null) {
        gridTab.setValue(lineField, 0);
      }

      if (!gridTab.dataSave(false)) {
        throw new IllegalStateException("Could not update tab");
      }

      gridTab.setCurrentRow(oldRow);
    }
  }
  public String priceBeco(
      Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value, Object oldValue) {
    System.out.println("pricebeco ... ");
    try {
      BigDecimal precio = (BigDecimal) mField.getValue();

      String priceRuleSQL =
          "select xx_lowrank,xx_highrank,xx_termination,xx_increase,xx_infinitevalue from xx_vme_pricerule order by (xx_lowrank)";
      PreparedStatement priceRulePstmt = DB.prepareStatement(priceRuleSQL, null);
      ResultSet priceRuleRs = priceRulePstmt.executeQuery();

      Integer precioInt = precio.intValue();
      BigDecimal precioBig = new BigDecimal(precioInt);
      while (priceRuleRs.next()) {
        if (precioBig.compareTo(priceRuleRs.getBigDecimal("xx_lowrank")) >= 0
            && precioBig.compareTo(priceRuleRs.getBigDecimal("xx_highrank")) <= 0) {
          Integer incremento = priceRuleRs.getInt("xx_increase");

          for (Integer i = priceRuleRs.getInt("xx_lowrank") - 1;
              i <= priceRuleRs.getInt("xx_highrank");
              i = i + incremento) {
            BigDecimal var = new BigDecimal(i);

            if (precioBig.compareTo(var) <= 0) {
              BigDecimal beco = var;

              BigDecimal terminacion = priceRuleRs.getBigDecimal("xx_termination");
              if (terminacion.intValue() == 0) {
                beco = var.add(terminacion);
              } else {
                var = var.divide(new BigDecimal(10));
                Integer aux = var.intValue() * 10;
                beco = new BigDecimal(aux).add(terminacion);
              }
              // mTab.setValue("PriceList", beco);
              priceRuleRs.close();
              priceRulePstmt.close();

              if (beco.compareTo(precio) == 0) {
                return priceBandBeco(ctx, WindowNo, mTab, mField, value, oldValue);
                // return"";
              } else {
                mTab.setValue("PriceList", beco);
                return "";
                // return "Precio Beco Sugerido "+beco.toString();
              }
            }
          }
        }
      }
      priceRuleRs.close();
      priceRulePstmt.close();

      return priceBandBeco(ctx, WindowNo, mTab, mField, value, oldValue);
      // return "";
    } catch (Exception e) {
      return e.getMessage();
    }
  }
Esempio n. 3
0
 public static void bind(DataModel data, GridTab curTab) {
   Set<String> keys = data.getData().keySet();
   for (String key : keys) {
     if (curTab.getField(key) != null) {
       // ?? wat abt callouts?
       curTab.setValue(key, data.get(key));
     }
   }
 }
  public String priceBecoGlobal(
      Ctx ctx,
      int WindowNo,
      GridTab mTab,
      GridField mField,
      Object value,
      Object oldValue,
      int option,
      BigDecimal auxValue) {
    // System.out.println("priceBecoGlobal");
    try {
      BigDecimal precio;

      if (option == 1) precio = (BigDecimal) mTab.getValue("XX_SalePricePlusTax");
      else precio = auxValue;

      // VME_PriceProductCallout banda = new VME_PriceProductCallout();

      String priceRuleSQL =
          "select xx_lowrank,xx_highrank,xx_termination,xx_increase,xx_infinitevalue from xx_vme_pricerule order by (xx_lowrank)";
      PreparedStatement priceRulePstmt = DB.prepareStatement(priceRuleSQL, null);
      ResultSet priceRuleRs = priceRulePstmt.executeQuery();

      Integer precioInt = precio.intValue();
      BigDecimal precioBig = new BigDecimal(precioInt);
      while (priceRuleRs.next()) {
        if (precioBig.compareTo(priceRuleRs.getBigDecimal("xx_lowrank")) >= 0
            && precioBig.compareTo(priceRuleRs.getBigDecimal("xx_highrank")) <= 0) {
          Integer incremento = priceRuleRs.getInt("xx_increase");

          for (Integer i = priceRuleRs.getInt("xx_lowrank") - 1;
              i <= priceRuleRs.getInt("xx_highrank");
              i = i + incremento) {
            BigDecimal var = new BigDecimal(i);

            if (precioBig.compareTo(var) <= 0) {
              BigDecimal beco = var;

              BigDecimal terminacion = priceRuleRs.getBigDecimal("xx_termination");
              if (terminacion.intValue() == 0) {
                beco = var.add(terminacion);
              } else {
                var = var.divide(new BigDecimal(10));
                Integer aux = var.intValue() * 10;
                beco = new BigDecimal(aux).add(terminacion);
              }
              // mTab.setValue("PriceList", beco);
              priceRuleRs.close();
              priceRulePstmt.close();

              if (beco.compareTo(precio) == 0 && option != 5) {
                return ""; // ModifyPVP(ctx, WindowNo, mTab, mField,value);
              } else {
                // Jpires
                // lo llamo desde XX_CreateDiscountPDA. Si es 5 es q lo estoy llamando desde algo
                // que no es un callout x eso no lo seteo en el mTab
                if (option != 5) mTab.setValue("XX_SalePricePlusTax", beco);

                if (option == 1) {
                  // return banda.priceBandBeco(ctx, WindowNo, mTab, mField, value,
                  // oldValue);//banda.priceBandBeco(ctx, WindowNo, mTab, mField, value, oldValue);
                  return priceBandBeco(
                      ctx, WindowNo, mTab, mField, value,
                      oldValue); // banda.priceBandBeco(ctx, WindowNo, mTab, mField, value,
                  // oldValue);
                } else {
                  return beco.toString();
                }
              }
            }
          }
        }
      }
      priceRuleRs.close();
      priceRulePstmt.close();

      return ""; // ModifyPVP(ctx, WindowNo, mTab, mField,value);
    } catch (Exception e) {
      return e.getMessage();
    }
  }
  public String priceBandBeco(
      Ctx ctx, int WindowNo, GridTab mTab, GridField mField, Object value, Object oldValue) {
    System.out.println("pricebandBeco");

    PreparedStatement pstmtC = null;
    ResultSet rsC = null;
    try {

      MVMRPOLineRefProv lineRefProv =
          new MVMRPOLineRefProv(ctx, (Integer) mTab.getValue("XX_VMR_PO_LineRefProv_ID"), null);
      MOrder order = new MOrder(ctx, lineRefProv.getC_Order_ID(), null);

      // String conceptoProducto = ctx.getContext(WindowNo, "XX_ConceptValue_ID");
      Integer conceptoProducto = lineRefProv.getXX_VME_ConceptValue_ID();

      // String seccionProducto = ctx.getContext(WindowNo,"XX_SECTION_ID");
      Integer seccionProducto = lineRefProv.getXX_VMR_Section_ID();

      // String lineaProducto = ctx.getContext(WindowNo,"XX_LINE_ID");
      Integer lineaProducto = lineRefProv.getXX_VMR_Line_ID();

      // String departamentoProducto = ctx.getContext(WindowNo,"XX_VMR_DEPARTMENT_ID");
      Integer departamentoProducto = order.getXX_VMR_DEPARTMENT_ID();

      /** Agregado por Javier Pino, para que funcione en distribucion */
      if (mTab.getValue("M_Product_ID") != null) {
        MProduct producto = new MProduct(ctx, (Integer) mTab.getValue("M_Product_ID"), null);
        conceptoProducto = producto.getXX_VME_ConceptValue_ID();
        departamentoProducto = producto.getXX_VMR_Department_ID();
        seccionProducto = producto.getXX_VMR_Section_ID();
        lineaProducto = producto.getXX_VMR_Line_ID();
      }
      /** Fin agregado Javier Pino */
      String conceptoComparar = null;

      String SQLA =
          "select P.xx_comparisonvalue_id "
              + "from xx_vme_priceband P , xx_vme_conceptvalue C "
              + "where C.XX_VME_ConceptValue_ID = "
              + conceptoProducto
              + " and "
              +
              // "where C.XX_VME_ConceptValue_ID = 1000002 and " +
              // "where C.XX_VME_ConceptValue_ID = 1000002 and " +
              "C.XX_VME_ConceptValue_id = P.xx_conceptvalue_id";

      // System.out.println("sqla "+SQLA);

      PreparedStatement pstmtA = DB.prepareStatement(SQLA, null);
      ResultSet rsA = pstmtA.executeQuery();

      if (rsA.next()) {
        conceptoComparar = rsA.getString("xx_comparisonvalue_id");
        // conceptoComparar="1000002";
      }
      rsA.close();
      pstmtA.close();

      Double promedioSeccion = 0.0;
      Double promedioLinea = 0.0;
      Double promedioDepartamento = 0.0;

      String SQL =
          "SELECT AVG(AUX.A) valor FROM "
              + "(SELECT MAX(P.XX_SALEPRICE) A, P.M_PRODUCT_ID "
              + "FROM XX_VMR_PRICECONSECUTIVE P, M_PRODUCT M "
              + "WHERE ";

      if (conceptoComparar == null) {
        SQL += "M.XX_VME_ConceptValue_ID IS NULL and ";
      } else {
        SQL += "M.XX_VME_ConceptValue_ID=" + conceptoComparar + " and ";
      }

      SQL +=
          "M.XX_VMR_Section_ID = "
              + seccionProducto
              + " "
              +
              //			"P.m_product_id <> "+ctx.getContext(WindowNo, "M_Product_ID")+" and "+
              "AND P.M_PRODUCT_ID = M.M_PRODUCT_ID "
              + "group by P.M_PRODUCT_ID) AUX";

      PreparedStatement pstmt = DB.prepareStatement(SQL, null);
      ResultSet rs = pstmt.executeQuery();

      // System.out.println("sql "+SQL);

      if (rs.next()) {
        promedioSeccion = rs.getDouble("valor");
      }
      rs.close();
      pstmt.close();

      if (promedioSeccion == 0.0) {
        String SQL2 =
            "SELECT AVG(AUX.A) valor FROM "
                + "(SELECT MAX(P.XX_SALEPRICE) A, P.M_PRODUCT_ID "
                + "FROM XX_VMR_PRICECONSECUTIVE P, M_PRODUCT M "
                + "WHERE ";

        if (conceptoComparar == null) {
          SQL2 += "M.XX_VME_ConceptValue_ID IS NULL and ";
        } else {
          SQL2 += "M.XX_VME_ConceptValue_ID=" + conceptoComparar + " and ";
        }

        SQL2 +=
            "M.XX_VMR_Line_ID = "
                + lineaProducto
                + " "
                +
                //				 			  "P.m_product_id <> "+ctx.getContext(WindowNo, "M_Product_ID")+" and "+
                "AND P.M_PRODUCT_ID = M.M_PRODUCT_ID "
                + "group by P.M_PRODUCT_ID) AUX";

        PreparedStatement pstmt2 = DB.prepareStatement(SQL2, null);
        ResultSet rs2 = pstmt2.executeQuery();
        // System.out.println("sql2"+SQL2);

        if (rs2.next()) {
          promedioLinea = rs2.getDouble("valor");
        }
        rs2.close();
        pstmt2.close();

        if (promedioLinea == 0.0) {
          String SQL3 =
              "SELECT AVG(AUX.A) valor FROM "
                  + "(SELECT MAX(P.XX_SALEPRICE) A, P.M_PRODUCT_ID "
                  + "FROM XX_VMR_PRICECONSECUTIVE P, m_product M "
                  + "WHERE ";

          if (conceptoComparar == null) {
            SQL2 += "M.XX_VME_ConceptValue_ID IS NULL and ";
          } else {
            SQL2 += "M.XX_VME_ConceptValue_ID=" + conceptoComparar + " and ";
          }

          SQL3 +=
              "M.XX_VMR_Department_ID = "
                  + departamentoProducto
                  + " "
                  +
                  //		 			  			 "P.m_product_id <> "+ctx.getContext(WindowNo, "M_Product_ID")+" and
                  // "+
                  "AND P.M_PRODUCT_ID = M.M_PRODUCT_ID "
                  + "group by P.M_PRODUCT_ID) AUX";

          PreparedStatement pstmt3 = DB.prepareStatement(SQL3, null);
          ResultSet rs3 = pstmt3.executeQuery();
          // System.out.println("TRES");
          if (rs3.next()) {
            promedioDepartamento = rs3.getDouble("valor");
          }
          rs3.close();
          pstmt3.close();
        }
      }

      Double promedio = promedioSeccion + promedioLinea + promedioDepartamento;

      System.out.println("Promedio -> " + promedio);

      // Si no tiene precio en depart, linea y seccion
      if (promedio == 0.0) {
        return "";
      }

      String SQLC =
          "SELECT * "
              + "FROM xx_vme_priceband P , xx_vme_conceptvalue C "
              + "WHERE C.xx_vme_conceptvalue_id = "
              + conceptoProducto
              + " AND "
              + "C.xx_vme_conceptvalue_id = P.xx_conceptvalue_id";

      pstmtC = DB.prepareStatement(SQLC, null);
      rsC = pstmtC.executeQuery();

      System.out.println(SQLC);

      if (rsC.next()) {
        Double precioProducto = new Double(ctx.getContext(WindowNo, "XX_SalePricePlusTax"));
        Double low = rsC.getDouble("xx_lowrank");
        Double high = rsC.getDouble("xx_highrank");

        // if((low-rsC.getDouble("xx_percentagevalue")) < 0 )
        // {
        //	low = new Double(0);
        // }

        Double percentage = rsC.getDouble("xx_percentagevalue");

        Double incrementaBanda = promedio * (percentage / 100);

        String operador = rsC.getString("xx_operating");

        DB.closeStatement(pstmtC);
        DB.closeResultSet(rsC);

        // Menor que 10000012
        // Mayor que 10000013

        C_OrderCallout precio = new C_OrderCallout();

        System.out.println(operador);

        if (operador.equals("Minor")) {
          // Double bandaMayor = (promedio - promedio*(low/100))+incrementaBanda;
          // Double bandaMenor = (promedio - promedio*(high/(100)))-incrementaBanda;

          BigDecimal doubleAux =
              new BigDecimal((promedio - promedio * (low / 100)) + incrementaBanda);
          doubleAux = doubleAux.setScale(2, BigDecimal.ROUND_HALF_UP);
          Double bandaMayor = doubleAux.doubleValue();

          doubleAux = new BigDecimal((promedio - promedio * (high / (100))) - incrementaBanda);
          doubleAux = doubleAux.setScale(2, BigDecimal.ROUND_HALF_UP);
          Double bandaMenor = doubleAux.doubleValue();

          if ((precioProducto <= bandaMayor) && (precioProducto >= bandaMenor)) {
            System.out.println(1);
            mTab.setValue("XX_CanSetDefinitive", "Y");
            return "";
          } else {
            // Si el precio no esta entre las bandas entonces no lo dejo colocar el precio como
            // definitivo
            mTab.setValue("XX_CanSetDefinitive", "N");
            return "Advertencia, el precio debe estar entre las bandas "
                + bandaMenor
                + " y "
                + bandaMayor
                + " Precio BECO bandas "
                + precio.priceBeco(new BigDecimal(bandaMenor))
                + " y "
                + PrecioBecoRebaja(new BigDecimal(bandaMayor));
          }
        }

        if (operador.equals("higher")) {
          // Double bandaMayor = (promedio + promedio*(high/(100))+incrementaBanda);
          // Double bandaMenor = (promedio + promedio*(low/100))-incrementaBanda;

          BigDecimal doubleAux =
              new BigDecimal((promedio + promedio * (high / (100)) + incrementaBanda));
          doubleAux = doubleAux.setScale(2, BigDecimal.ROUND_HALF_UP);
          Double bandaMayor = doubleAux.doubleValue();

          doubleAux = new BigDecimal((promedio + promedio * (low / 100)) - incrementaBanda);
          doubleAux = doubleAux.setScale(2, BigDecimal.ROUND_HALF_UP);
          Double bandaMenor = doubleAux.doubleValue();

          if ((precioProducto <= bandaMayor) && (precioProducto >= bandaMenor)) {
            System.out.println(2);
            mTab.setValue("XX_CanSetDefinitive", "Y");
            return "";
          } else {
            // Si el precio no esta entre las bandas entonces no lo dejo colocar el precio como
            // definitivo
            mTab.setValue("XX_CanSetDefinitive", "N");

            String message =
                "Advertencia, el precio deberia estar entre las bandas "
                    + ""
                    + bandaMenor
                    + " y "
                    + bandaMayor
                    + "Precio BECO bandas";
            // +precio.priceBeco(new BigDecimal(bandaMenor))+" y ";
            // + PrecioBecoRebaja(new BigDecimal(bandaMayor));

            return message;
          }
        }
      }

      return "";
    } catch (Exception e) {
      System.out.println("Error an la base de datos " + e.getMessage());
    } finally {
      DB.closeStatement(pstmtC);
      DB.closeResultSet(rsC);
    }

    return "";
  }
  /** Start dialog */
  private void cmd_dialog() {
    //
    Integer oldValue = (Integer) getValue();
    int oldValueInt = oldValue == null ? 0 : oldValue.intValue();
    int M_AttributeSetInstance_ID = oldValueInt;
    int M_Product_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "M_Product_ID");
    int M_ProductBOM_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "M_ProductBOM_ID");

    log.config(
        "M_Product_ID="
            + M_Product_ID
            + "/"
            + M_ProductBOM_ID
            + ",M_AttributeSetInstance_ID="
            + M_AttributeSetInstance_ID
            + ", AD_Column_ID="
            + gridField.getAD_Column_ID());

    //	M_Product.M_AttributeSetInstance_ID = 8418
    boolean productWindow = (gridField.getAD_Column_ID() == 8418); // 	HARDCODED

    //	Exclude ability to enter ASI
    boolean exclude = true;

    if (M_Product_ID != 0) {
      MProduct product = MProduct.get(Env.getCtx(), M_Product_ID);
      int M_AttributeSet_ID = Services.get(IProductBL.class).getM_AttributeSet_ID(product);
      if (M_AttributeSet_ID != 0) {
        final IAttributeExcludeBL excludeBL = Services.get(IAttributeExcludeBL.class);
        final I_M_AttributeSet attributeSet =
            InterfaceWrapperHelper.create(
                Env.getCtx(), M_AttributeSet_ID, I_M_AttributeSet.class, ITrx.TRXNAME_None);
        final I_M_AttributeSetExclude asExclude =
            excludeBL.getAttributeSetExclude(
                attributeSet, gridField.getAD_Column_ID(), Env.isSOTrx(Env.getCtx(), m_WindowNo));
        if ((null == asExclude) || (!excludeBL.isFullExclude(asExclude))) {
          exclude = false;
        }
      }
    }

    boolean changed = false;
    if (M_ProductBOM_ID != 0) // 	Use BOM Component
    M_Product_ID = M_ProductBOM_ID;
    //
    if (!productWindow && (M_Product_ID == 0 || exclude)) {
      changed = true;
      getComponent().setText(null);
      M_AttributeSetInstance_ID = 0;
    } else {
      WPAttributeDialog vad =
          new WPAttributeDialog(
              M_AttributeSetInstance_ID,
              M_Product_ID,
              m_C_BPartner_ID,
              productWindow,
              gridField.getAD_Column_ID(),
              m_WindowNo);
      if (vad.isChanged()) {
        getComponent().setText(vad.getM_AttributeSetInstanceName());
        M_AttributeSetInstance_ID = vad.getM_AttributeSetInstance_ID();
        if (m_GridTab != null && !productWindow && vad.getM_Locator_ID() > 0)
          m_GridTab.setValue("M_Locator_ID", vad.getM_Locator_ID());
        changed = true;
      }
    }
    /**
     * Selection { // Get Model MAttributeSetInstance masi = MAttributeSetInstance.get(Env.getCtx(),
     * M_AttributeSetInstance_ID, M_Product_ID); if (masi == null) { log.log(Level.SEVERE, "No Model
     * for M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID + ", M_Product_ID=" +
     * M_Product_ID); } else { Env.setContext(Env.getCtx(), m_WindowNo, "M_AttributeSet_ID",
     * masi.getM_AttributeSet_ID()); // Get Attribute Set MAttributeSet as =
     * masi.getMAttributeSet(); // Product has no Attribute Set if (as == null)
     * ADialog.error(m_WindowNo, this, "PAttributeNoAttributeSet"); // Product has no Instance
     * Attributes else if (!as.isInstanceAttribute()) ADialog.error(m_WindowNo, this,
     * "PAttributeNoInstanceAttribute"); else { int M_Warehouse_ID = Env.getContextAsInt (Env.getCtx
     * (), m_WindowNo, "M_Warehouse_ID"); int M_Locator_ID = Env.getContextAsInt (Env.getCtx (),
     * m_WindowNo, "M_Locator_ID"); String title = ""; PAttributeInstance pai = new
     * PAttributeInstance ( Env.getFrame(this), title, M_Warehouse_ID, M_Locator_ID, M_Product_ID,
     * m_C_BPartner_ID); if (pai.getM_AttributeSetInstance_ID() != -1) {
     * m_text.setText(pai.getM_AttributeSetInstanceName()); M_AttributeSetInstance_ID =
     * pai.getM_AttributeSetInstance_ID(); changed = true; } } } }
     */

    //	Set Value
    if (changed) {
      log.finest("Changed M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
      m_value = new Object(); // 	force re-query display
      if (M_AttributeSetInstance_ID == 0) setValue(null);
      else setValue(new Integer(M_AttributeSetInstance_ID));

      ValueChangeEvent vce =
          new ValueChangeEvent(this, gridField.getColumnName(), new Object(), getValue());
      fireValueChange(vce);
      if (M_AttributeSetInstance_ID == oldValueInt && m_GridTab != null && gridField != null) {
        //  force Change - user does not realize that embedded object is already saved.
        m_GridTab.processFieldChange(gridField);
      }
    } //	change
  } //  cmd_file