# HG changeset patch
# User Miguel de Juana <miguel.dejuana@openbravo.com>
# Date 1460471128 -7200
#      Tue Apr 12 16:25:28 2016 +0200
# Node ID 33c0f58c706e902af3086ef8d3d0993c72729857
# Parent  f594a124ffa104b9a04dc808537d6526e87fe550
Fixed issue 0032652: WebPOS is Loading all ProductPrices even if MultiPricelist is not activated

- If preference is disabled, do not load prices lits and product prices

diff --git a/src/org/openbravo/retail/posterminal/master/PriceList.java b/src/org/openbravo/retail/posterminal/master/PriceList.java
--- a/src/org/openbravo/retail/posterminal/master/PriceList.java
+++ b/src/org/openbravo/retail/posterminal/master/PriceList.java
@@ -19,11 +19,14 @@
 import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 
+import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.client.kernel.ComponentProvider.Qualifier;
 import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.erpCommon.businessUtility.Preferences;
+import org.openbravo.erpCommon.utility.PropertyException;
 import org.openbravo.mobile.core.model.HQLPropertyList;
 import org.openbravo.mobile.core.model.ModelExtension;
 import org.openbravo.mobile.core.model.ModelExtensionUtils;
@@ -33,6 +36,7 @@
 
 public class PriceList extends ProcessHQLQuery {
   public static final String priceListPropertyExtension = "OBPOS_PriceListExtension";
+  public static final Logger log = Logger.getLogger(Discount.class);
 
   @Inject
   @Any
@@ -56,17 +60,28 @@
 
   @Override
   protected List<String> getQuery(JSONObject jsonsent) throws JSONException {
+    boolean multiPrices = false;
     List<String> hqlQueries = new ArrayList<String>();
 
     HQLPropertyList priceListHQLProperties = ModelExtensionUtils.getPropertyExtensions(extensions);
+    try {
+      multiPrices = "Y".equals(Preferences.getPreferenceValue("OBPOS_EnableMultiPriceList", true,
+          OBContext.getOBContext().getCurrentClient(), OBContext.getOBContext()
+              .getCurrentOrganization(), OBContext.getOBContext().getUser(), OBContext
+              .getOBContext().getRole(), null));
+    } catch (PropertyException e1) {
+      log.error("Error getting Preference: " + e1.getMessage(), e1);
+    }
 
-    hqlQueries
-        .add("select "
-            + priceListHQLProperties.getHqlSelect()
-            + " from PricingPriceList pl "
-            + "where pl.id in (select distinct priceList.id from BusinessPartner where customer = 'Y') "
-            + "and pl.id <> (:priceList) "
-            + "and $naturalOrgCriteria and $readableClientCriteria and ($incrementalUpdateCriteria)");
+    if (multiPrices) {
+      hqlQueries
+          .add("select "
+              + priceListHQLProperties.getHqlSelect()
+              + " from PricingPriceList pl "
+              + "where pl.id in (select distinct priceList.id from BusinessPartner where customer = 'Y') "
+              + "and pl.id <> (:priceList) "
+              + "and $naturalOrgCriteria and $readableClientCriteria and ($incrementalUpdateCriteria)");
+    }
 
     return hqlQueries;
   }
diff --git a/src/org/openbravo/retail/posterminal/master/ProductPrice.java b/src/org/openbravo/retail/posterminal/master/ProductPrice.java
--- a/src/org/openbravo/retail/posterminal/master/ProductPrice.java
+++ b/src/org/openbravo/retail/posterminal/master/ProductPrice.java
@@ -18,11 +18,14 @@
 import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 
+import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.client.kernel.ComponentProvider.Qualifier;
 import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.erpCommon.businessUtility.Preferences;
+import org.openbravo.erpCommon.utility.PropertyException;
 import org.openbravo.mobile.core.model.HQLPropertyList;
 import org.openbravo.mobile.core.model.ModelExtension;
 import org.openbravo.mobile.core.model.ModelExtensionUtils;
@@ -34,6 +37,7 @@
 
 public class ProductPrice extends ProcessHQLQuery {
   public static final String productPricePropertyExtension = "OBPOS_ProductPriceExtension";
+  public static final Logger log = Logger.getLogger(Discount.class);
 
   @Inject
   @Any
@@ -68,12 +72,22 @@
   @Override
   protected List<String> getQuery(JSONObject jsonsent) throws JSONException {
     String orgId = OBContext.getOBContext().getCurrentOrganization().getId();
+    boolean multiPrices = false;
+    OBRETCOProductList productList = POSUtils.getProductListByOrgId(orgId);
 
-    OBRETCOProductList productList = POSUtils.getProductListByOrgId(orgId);
     if (productList == null) {
       throw new JSONException("Product list not found");
     }
 
+    try {
+      multiPrices = "Y".equals(Preferences.getPreferenceValue("OBPOS_EnableMultiPriceList", true,
+          OBContext.getOBContext().getCurrentClient(), OBContext.getOBContext()
+              .getCurrentOrganization(), OBContext.getOBContext().getUser(), OBContext
+              .getOBContext().getRole(), null));
+    } catch (PropertyException e1) {
+      log.error("Error getting Preference: " + e1.getMessage(), e1);
+    }
+
     Date terminalDate = OBMOBCUtils.calculateServerDate(jsonsent.getJSONObject("parameters")
         .getString("terminalTime"),
         jsonsent.getJSONObject("parameters").getJSONObject("terminalTimeOffset").getLong("value"));
@@ -81,17 +95,18 @@
     List<String> hqlQueries = new ArrayList<String>();
 
     HQLPropertyList priceListHQLProperties = ModelExtensionUtils.getPropertyExtensions(extensions);
-
-    hqlQueries
-        .add("select "
-            + priceListHQLProperties.getHqlSelect()
-            + "from OBRETCO_Prol_Product as pli, PricingProductPrice ppp "
-            + "where pli.product.id = ppp.product.id and pli.obretcoProductlist = '"
-            + productList.getId()
-            + "' and ppp.priceListVersion.id in ("
-            + PriceList.getSelectPriceListVersionIds(orgId, terminalDate)
-            + ") and $filtersCriteria AND $hqlCriteria "
-            + "and pli.$naturalOrgCriteria and pli.$readableClientCriteria and (ppp.$incrementalUpdateCriteria)");
+    if (multiPrices) {
+      hqlQueries
+          .add("select "
+              + priceListHQLProperties.getHqlSelect()
+              + "from OBRETCO_Prol_Product as pli, PricingProductPrice ppp "
+              + "where pli.product.id = ppp.product.id and pli.obretcoProductlist = '"
+              + productList.getId()
+              + "' and ppp.priceListVersion.id in ("
+              + PriceList.getSelectPriceListVersionIds(orgId, terminalDate)
+              + ") and $filtersCriteria AND $hqlCriteria "
+              + "and pli.$naturalOrgCriteria and pli.$readableClientCriteria and (ppp.$incrementalUpdateCriteria)");
+    }
 
     return hqlQueries;
   }
