Fixes Issue 0037934: G/l item drop down in add details popup showing
authorAtul Gaware <atul.gaware@openbravo.com>
Wed, 28 Feb 2018 23:31:15 +0530
changeset 33617 da52de15f453
parent 33616 1b9efd28dc91
child 33618 0ded97bd7628
Fixes Issue 0037934: G/l item drop down in add details popup showing
different in payment in and in transaction window of financial account.

Implement orgnization where clause for GL Item selection not only
in financial account window but also other window for eg:-
Sales/Purchase Order, Sales/Purchase Invoice, Payment In/Out, etc
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/GLItemSelectorFilterExpression.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/GLItemSelectorFilterExpression.java	Thu Mar 01 11:32:50 2018 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/GLItemSelectorFilterExpression.java	Wed Feb 28 23:31:15 2018 +0530
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2012-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2012-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -22,32 +22,56 @@
 import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.client.application.FilterExpression;
-import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
-
 import org.openbravo.erpCommon.utility.Utility;
-
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class GLItemSelectorFilterExpression implements FilterExpression {
-  final static String FINANCIAL_ACCOUNT_WINDOW = "94EAA455D2644E04AB25D93BE5157B6D";
+  private static final String INPAD_ORG_ID_PARAM = "inpadOrgId";
+  private static final String AD_ORG_ID_PARAM = "ad_org_id";
+  private static final Logger log = LoggerFactory.getLogger(GLItemSelectorFilterExpression.class);
 
   @Override
   public String getExpression(Map<String, String> requestMap) {
-    if (!FINANCIAL_ACCOUNT_WINDOW.equals(requestMap.get("inpwindowId"))) {
-      return "";
+    try {
+      final JSONObject context = new JSONObject(requestMap);
+      final String orgId = getOrganizationIdFromContext(context);
+      String orgList = Utility.getInStrSet(OBContext.getOBContext()
+          .getOrganizationStructureProvider().getNaturalTree(orgId));
+      if (!orgList.isEmpty()) {
+        StringBuilder whereClause = new StringBuilder();
+        whereClause.append("e.organization.id in (" + orgList + ")");
+        return whereClause.toString();
+      } else {
+        return null;
+      }
+    } catch (Exception e) {
+      log.error("Error trying to get organization where clause for GL Item: ", e);
+      return null;
     }
-    StringBuilder whereClause = new StringBuilder();
-    String orgId = (String) RequestContext.get().getSession()
-        .getAttribute(FINANCIAL_ACCOUNT_WINDOW + "|AD_ORG_ID");
-    if(StringUtils.isEmpty(orgId)) {
-    	orgId = requestMap.get("inpadOrgId");
+  }
+
+  private String getOrganizationIdFromContext(final JSONObject context) throws JSONException {
+    if (contextHasADOrgIDParam(context)) {
+      return context.getString(AD_ORG_ID_PARAM);
+    } else if (contextHasInpADOrgIDParam(context)) {
+      return context.getString(INPAD_ORG_ID_PARAM);
+    } else {
+      return null;
     }
-    String orgList = Utility.getInStrSet(OBContext.getOBContext()
-        .getOrganizationStructureProvider().getNaturalTree(orgId));
-    if (!orgList.isEmpty()) {
-      whereClause.append("e.organization.id in (" + orgList + ")");
-    }
-    return whereClause.toString();
+  }
+
+  private boolean contextHasInpADOrgIDParam(final JSONObject context) throws JSONException {
+    return context.has(INPAD_ORG_ID_PARAM) && context.get(INPAD_ORG_ID_PARAM) != JSONObject.NULL
+        && StringUtils.isNotEmpty(context.getString(INPAD_ORG_ID_PARAM));
+  }
+
+  private boolean contextHasADOrgIDParam(final JSONObject context) throws JSONException {
+    return context.has(AD_ORG_ID_PARAM) && context.get(AD_ORG_ID_PARAM) != JSONObject.NULL
+        && StringUtils.isNotEmpty(context.getString(AD_ORG_ID_PARAM));
   }
 }