[levelPricing] add new hook in SL_Order_Amt.java
authorGuillermo Alvarez de Eulate <guillermo.alvarez@openbravo.com>
Mon, 14 Oct 2013 11:22:51 +0200
changeset 21276 2a737579aafc
parent 21275 38ab65d07468
child 21277 15ddcf502947
[levelPricing] add new hook in SL_Order_Amt.java
src/org/openbravo/common/hooks/OrderLineQtyChangedHook.java
src/org/openbravo/common/hooks/OrderLineQtyChangedHookManager.java
src/org/openbravo/common/hooks/OrderLineQtyChangedHookObject.java
src/org/openbravo/erpCommon/ad_callouts/SL_Order_Amt.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/common/hooks/OrderLineQtyChangedHook.java	Mon Oct 14 11:22:51 2013 +0200
@@ -0,0 +1,34 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2012 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+
+package org.openbravo.common.hooks;
+
+public interface OrderLineQtyChangedHook {
+
+  public void exec(OrderLineQtyChangedHookObject hookObject) throws Exception;
+}
+
+// Example of a hook:
+// import javax.enterprise.context.ApplicationScoped;
+//
+// import org.openbravo.model.common.invoice.Invoice;
+// import org.openbravo.model.common.order.Order;
+// import org.openbravo.model.materialmgmt.transaction.ShipmentInOut;
+//
+// @ApplicationScoped
+// public class OrderLoaderHookTest implements OrderLoaderHook {
+//
+// @Override
+// public void exec(JSONObject jsonorder, Order order, ShipmentInOut shipment, Invoice invoice)
+// throws Exception {
+// // TODO Auto-generated method stub
+// System.out.println("somebody is calling me");
+// }
+//
+// }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/common/hooks/OrderLineQtyChangedHookManager.java	Mon Oct 14 11:22:51 2013 +0200
@@ -0,0 +1,20 @@
+package org.openbravo.common.hooks;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+@ApplicationScoped
+public class OrderLineQtyChangedHookManager {
+  @Inject
+  @Any
+  private Instance<OrderLineQtyChangedHook> orderLineQtyChangedHooks;
+
+  public void executeHooks(OrderLineQtyChangedHookObject hookObject) throws Exception {
+    for (OrderLineQtyChangedHook hook : orderLineQtyChangedHooks) {
+      hook.exec(hookObject);
+    }
+  }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/common/hooks/OrderLineQtyChangedHookObject.java	Mon Oct 14 11:22:51 2013 +0200
@@ -0,0 +1,66 @@
+package org.openbravo.common.hooks;
+
+import java.math.BigDecimal;
+
+public class OrderLineQtyChangedHookObject {
+
+  private String productId;
+  private String orderId;
+  private BigDecimal qty;
+  private BigDecimal price;
+  private String changed;
+  private int pricePrecision;
+
+  public int getPricePrecision() {
+    return pricePrecision;
+  }
+
+  public void setPricePrecision(int pricePrecision) {
+    this.pricePrecision = pricePrecision;
+  }
+
+  public String getChanged() {
+    return changed;
+  }
+
+  public void setChanged(String changed) {
+    this.changed = changed;
+  }
+
+  public String getOrderId() {
+    return orderId;
+  }
+
+  public void setOrderId(String orderId) {
+    this.orderId = orderId;
+  }
+
+  public void setProductId(String productId) {
+    this.productId = productId;
+  }
+
+  public String getProductId() {
+    return productId;
+  }
+
+  public void setProduct(String productid) {
+    this.productId = productid;
+  }
+
+  public BigDecimal getQty() {
+    return qty;
+  }
+
+  public void setQty(BigDecimal qty) {
+    this.qty = qty;
+  }
+
+  public BigDecimal getPrice() {
+    return price;
+  }
+
+  public void setPrice(BigDecimal price) {
+    this.price = price;
+  }
+
+}
--- a/src/org/openbravo/erpCommon/ad_callouts/SL_Order_Amt.java	Mon Oct 14 11:15:59 2013 +0200
+++ b/src/org/openbravo/erpCommon/ad_callouts/SL_Order_Amt.java	Mon Oct 14 11:22:51 2013 +0200
@@ -30,6 +30,9 @@
 
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
 import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.base.weld.WeldUtils;
+import org.openbravo.common.hooks.OrderLineQtyChangedHookManager;
+import org.openbravo.common.hooks.OrderLineQtyChangedHookObject;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.erpCommon.businessUtility.PriceAdjustment;
 import org.openbravo.erpCommon.utility.Utility;
@@ -97,6 +100,7 @@
       String strTaxBaseAmt, String strGrossBaseUnitPrice) throws IOException, ServletException {
     XmlDocument xmlDocument = xmlEngine.readXmlTemplate(
         "org/openbravo/erpCommon/ad_callouts/CallOut").createXmlDocument();
+
     SLOrderAmtData[] data = SLOrderAmtData.select(this, strCOrderId);
     SLOrderStockData[] data1 = SLOrderStockData.select(this, strProduct);
     String strPrecision = "0", strPricePrecision = "0";
@@ -109,7 +113,7 @@
     boolean isTaxIncludedPriceList = OBDal.getInstance().get(PriceList.class, data[0].mPricelistId)
         .isPriceIncludesTax();
     boolean isGrossUnitPriceChanged = strChanged.equals("inpgrossUnitPrice");
-
+    boolean forceSetPriceStd = false;
     if (data1 != null && data1.length > 0) {
       strStockSecurity = data1[0].stock;
       strEnforceAttribute = data1[0].enforceAttribute;
@@ -139,11 +143,46 @@
         pricePrecision, BigDecimal.ROUND_HALF_UP);
     BigDecimal grossUnitPrice = (strGrossUnitPrice.equals("") ? ZERO : new BigDecimal(
         strGrossUnitPrice).setScale(pricePrecision, BigDecimal.ROUND_HALF_UP));
+
     BigDecimal grossPriceList = (strGrossPriceList.equals("") ? ZERO : new BigDecimal(
         strGrossPriceList).setScale(pricePrecision, BigDecimal.ROUND_HALF_UP));
     BigDecimal grossBaseUnitPrice = (strGrossBaseUnitPrice.equals("") ? ZERO : new BigDecimal(
         strGrossBaseUnitPrice).setScale(pricePrecision, BigDecimal.ROUND_HALF_UP));
 
+    // A hook has been created. This hook will be raised when the qty is changed having selected a
+    // product
+    if (!strProduct.equals("") && strChanged.equals("inpqtyordered")) {
+      try {
+        OrderLineQtyChangedHookObject hookObject = new OrderLineQtyChangedHookObject();
+        hookObject.setProductId(strProduct);
+        hookObject.setQty(qtyOrdered);
+        hookObject.setOrderId(strCOrderId);
+        hookObject.setPricePrecision(pricePrecision);
+        if (isTaxIncludedPriceList) {
+          hookObject.setPrice(grossPriceList);
+        } else {
+          hookObject.setPrice(netPriceList);
+        }
+
+        hookObject.setChanged(strChanged);
+        WeldUtils.getInstanceFromStaticBeanManager(OrderLineQtyChangedHookManager.class)
+            .executeHooks(hookObject);
+        if (isTaxIncludedPriceList) {
+          if (grossBaseUnitPrice.compareTo(hookObject.getPrice()) != 0) {
+            grossBaseUnitPrice = hookObject.getPrice();
+            isGrossUnitPriceChanged = true;
+          }
+        } else {
+          if (priceStd.compareTo(hookObject.getPrice()) != 0) {
+            priceStd = hookObject.getPrice();
+            forceSetPriceStd = true;
+          }
+        }
+      } catch (Exception e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      }
+    }
     StringBuffer resultado = new StringBuffer();
     resultado.append("var calloutName='SL_Order_Amt';\n\n");
     resultado.append("var respuesta = new Array(");
@@ -173,7 +212,8 @@
       resultado.append("new Array(\"inppriceactual\", " + priceActual + "),");
     }
     // Calculating prices for offers...
-    if (strChanged.equals("inppriceactual") || strChanged.equals("inplinenetamt")) {
+    if (strChanged.equals("inppriceactual") || strChanged.equals("inplinenetamt")
+        || forceSetPriceStd) {
       log4j.debug("priceActual:" + priceActual.toString());
       if (!cancelPriceAd) {
         priceStd = PriceAdjustment.calculatePriceStd(order, product, qtyOrdered, priceActual);
@@ -238,7 +278,7 @@
     // calculating discount
     if (strChanged.equals("inppricelist") || strChanged.equals("inppriceactual")
         || strChanged.equals("inplinenetamt") || strChanged.equals("inpgrosspricelist")
-        || strChanged.equals("inpgrossUnitPrice")) {
+        || strChanged.equals("inpgrossUnitPrice") || strChanged.equals("inpqtyordered")) {
       BigDecimal priceList = BigDecimal.ZERO;
       BigDecimal unitPrice = BigDecimal.ZERO;
       BigDecimal discount;
@@ -392,4 +432,5 @@
     out.println(xmlDocument.print());
     out.close();
   }
+
 }