details:   https://code.openbravo.com/erp/devel/pi/rev/29bb5d02e0fe
changeset: 35234:29bb5d02e0fe
user:      Atul Gaware <atul.gaware <at> openbravo.com>
date:      Thu Dec 13 00:01:44 2018 +0530
summary:   Fixes Issue 0039710: Landed cost can be reactivated TWICE

** Inorder to avoid process or reactivate twice, processing flag
is used for locking mechanism. At the beginning check is done
whether the flag is checked, if yes error is shown else flag is
set as Yes and process or reactivate action proceeds. At the end
flag is reset as No.
** Checking of processed flag or document status (Draft) is also
done while reactivation process whether the landed cost is
reactivated by other process.

diffstat:

 src/org/openbravo/costing/LandedCostProcessHandler.java |  48 +++++++---------
 src/org/openbravo/costing/ReactivateLandedCost.java     |  28 +++++++++-
 2 files changed, 47 insertions(+), 29 deletions(-)

diffs (165 lines):

diff -r 4c022943ed23 -r 29bb5d02e0fe 
src/org/openbravo/costing/LandedCostProcessHandler.java
--- a/src/org/openbravo/costing/LandedCostProcessHandler.java   Wed Dec 12 
23:20:31 2018 +0000
+++ b/src/org/openbravo/costing/LandedCostProcessHandler.java   Thu Dec 13 
00:01:44 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) 2014 Openbravo SLU
+ * All portions are Copyright (C) 2014-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -20,16 +20,16 @@
 
 import java.util.Map;
 
-import org.codehaus.jettison.json.JSONException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.client.kernel.BaseActionHandler;
+import org.openbravo.dal.core.SessionHandler;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.materialmgmt.cost.LandedCost;
 import org.openbravo.service.db.DbUtility;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class LandedCostProcessHandler extends BaseActionHandler {
   private static final Logger log = LogManager.getLogger();
@@ -40,37 +40,31 @@
     try {
       JSONObject jsonContent = new JSONObject(content);
       final String strLandedCostId = jsonContent.getString("M_Landedcost_ID");
-      final LandedCost landedCost = OBDal.getInstance().get(LandedCost.class, 
strLandedCostId);
+      LandedCost landedCost = OBDal.getInstance().get(LandedCost.class, 
strLandedCostId);
+      // lock Landed Cost
+      if (landedCost.isProcessNow()) {
+        throw new 
OBException(OBMessageUtils.parseTranslation("@OtherProcessActive@"));
+      }
 
+      landedCost.setProcessNow(true);
+      if (SessionHandler.isSessionHandlerPresent()) {
+        SessionHandler.getInstance().commitAndStart();
+      }
       JSONObject message = LandedCostProcess.doProcessLandedCost(landedCost);
+      landedCost = OBDal.getInstance().get(LandedCost.class, strLandedCostId);
+      landedCost.setProcessNow(false);
+
       jsonResponse.put("message", message);
 
-    } catch (OBException e) {
-      OBDal.getInstance().rollbackAndClose();
-      log.error("Error in LandedCostProcessHandler: " + e.getMessage(), e);
-      try {
-        JSONObject message = new JSONObject();
-        message.put("severity", "error");
-        message.put("title", OBMessageUtils.messageBD("Error"));
-        message.put("text", e.getMessage());
-        jsonResponse.put("message", message);
-      } catch (JSONException ignore) {
-      }
-    } catch (JSONException e) {
-      OBDal.getInstance().rollbackAndClose();
-      log.error("Error parsing JSONObject: " + e.getMessage(), e);
-      try {
-        JSONObject errorMessage = new JSONObject();
-        errorMessage.put("severity", "error");
-        errorMessage.put("title", OBMessageUtils.messageBD("Error"));
-        errorMessage.put("text", e.getMessage());
-        jsonResponse.put("message", errorMessage);
-      } catch (JSONException ignore) {
-      }
     } catch (Exception e) {
       OBDal.getInstance().rollbackAndClose();
       log.error("Error in LandedCostProcessHandler: " + e.getMessage(), e);
       try {
+        JSONObject jsonContent = new JSONObject(content);
+        final String strLandedCostId = 
jsonContent.getString("M_Landedcost_ID");
+        final LandedCost landedCost = 
OBDal.getInstance().get(LandedCost.class, strLandedCostId);
+        landedCost.setProcessNow(false);
+
         Throwable ex = DbUtility.getUnderlyingSQLException(e);
         String strMessage = 
OBMessageUtils.translateError(ex.getMessage()).getMessage();
         JSONObject errorMessage = new JSONObject();
diff -r 4c022943ed23 -r 29bb5d02e0fe 
src/org/openbravo/costing/ReactivateLandedCost.java
--- a/src/org/openbravo/costing/ReactivateLandedCost.java       Wed Dec 12 
23:20:31 2018 +0000
+++ b/src/org/openbravo/costing/ReactivateLandedCost.java       Thu Dec 13 
00:01:44 2018 +0530
@@ -22,6 +22,9 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.codec.binary.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.base.exception.OBException;
@@ -29,14 +32,13 @@
 import org.openbravo.base.weld.WeldUtils;
 import org.openbravo.client.kernel.BaseActionHandler;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.core.SessionHandler;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.materialmgmt.cost.LCDistributionAlgorithm;
 import org.openbravo.model.materialmgmt.cost.LandedCost;
 import org.openbravo.model.materialmgmt.cost.LandedCostCost;
 import org.openbravo.service.db.DbUtility;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
 
 public class ReactivateLandedCost extends BaseActionHandler {
   private static final Logger log = LogManager.getLogger();
@@ -50,12 +52,23 @@
       String lcId = jsonData.getString("inpmLandedcostId");
       LandedCost landedCost = OBDal.getInstance().get(LandedCost.class, lcId);
       doChecks(landedCost);
+      landedCost.setProcessNow(true);
+      if (SessionHandler.isSessionHandlerPresent()) {
+        SessionHandler.getInstance().commitAndStart();
+      }
       JSONObject message = doReactivateLandedCost(landedCost);
+      landedCost = OBDal.getInstance().get(LandedCost.class, lcId);
+      landedCost.setProcessNow(false);
       result.put("message", message);
     } catch (Exception e) {
       OBDal.getInstance().rollbackAndClose();
       log.error(e.getMessage(), e);
       try {
+        JSONObject jsonContent = new JSONObject(data);
+        final String strLandedCostId = 
jsonContent.getString("inpmLandedcostId");
+        final LandedCost landedCost = 
OBDal.getInstance().get(LandedCost.class, strLandedCostId);
+        landedCost.setProcessNow(false);
+
         Throwable ex = DbUtility.getUnderlyingSQLException(e);
         String message = 
OBMessageUtils.translateError(ex.getMessage()).getMessage();
         errorMessage = new JSONObject();
@@ -75,6 +88,13 @@
     String strLCostId = localLandedCost.getId();
     JSONObject message = null;
 
+    if (StringUtils.equals(localLandedCost.getDocumentStatus(), "DR")
+        || !localLandedCost.isProcessed()) {
+      message = new JSONObject();
+      message.put("severity", "error");
+      message.put("title", OBMessageUtils.messageBD("DocumentProcessed"));
+      return message;
+    }
     // Cancel cost adjustment only if exists
     if (localLandedCost.getCostAdjustment() != null) {
       message = 
CancelCostAdjustment.doCancelCostAdjustment(localLandedCost.getCostAdjustment());
@@ -155,6 +175,10 @@
       log.error("Document Posted");
       throw new OBException(errorMsg);
     }
+    // lock Landed Cost
+    if (landedCost.isProcessNow()) {
+      throw new 
OBException(OBMessageUtils.parseTranslation("@OtherProcessActive@"));
+    }
     for (LandedCostCost lcc : landedCost.getLandedCostCostList()) {
       if ("Y".equals(lcc.getPosted())) {
         String errorMsg = OBMessageUtils.messageBD("DocumentPosted");


_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to