Fixes Issue 24473: In BOM Production part products could be produced
authorPandeeswari Ramakrishnan <pandeeswari.ramakrishnan@openbravo.com>
Wed, 14 Aug 2013 18:42:36 +0530
changeset 20980 57708817383a
parent 20979 d6ba90dc64fa
child 20985 3fbf3113169e
Fixes Issue 24473: In BOM Production part products could be produced
src-db/database/model/functions/M_PRODUCTION_RUN.xml
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/event/ProductionLineEventHandler.java
--- a/src-db/database/model/functions/M_PRODUCTION_RUN.xml	Tue Aug 13 11:58:10 2013 +0530
+++ b/src-db/database/model/functions/M_PRODUCTION_RUN.xml	Wed Aug 14 18:42:36 2013 +0530
@@ -45,6 +45,7 @@
   -- Parameter
   TYPE RECORD IS REF CURSOR;
     Cur_Parameter RECORD;
+    CUR_ProductionPlan RECORD;
     CUR_PP RECORD;
     CUR_PLineBOM RECORD;
     CUR_BOM_PRODUCT RECORD;
@@ -456,6 +457,58 @@
       v_Message:='@Product@'||' "' ||v_Product_Name ||'" ' || '@ProductWithoutAttributeSet@';
       RAISE_APPLICATION_ERROR(-20000, v_Message);
     END IF;
+
+    SELECT COUNT(M_ProductionPlan_ID)
+    INTO v_count
+    FROM M_ProductionPlan pp
+    WHERE pp.M_Production_ID = v_Record_ID
+    AND NOT EXISTS (SELECT 1 FROM M_ProductionLine WHERE M_ProductionPlan_ID = pp.M_ProductionPlan_ID AND MovementQty > 0);
+
+    v_Message := NULL;      
+    IF(v_count <> 0) THEN
+      FOR CUR_ProductionPlan IN (
+	SELECT DISTINCT(pp.line)
+	FROM M_ProductionPlan pp
+	WHERE pp.M_Production_ID = v_Record_ID
+	AND NOT EXISTS (SELECT 1 FROM M_ProductionLine WHERE M_ProductionPlan_ID = pp.M_ProductionPlan_ID AND MovementQty > 0)
+	ORDER BY Line
+        )
+      LOOP
+        IF v_Message IS NULL THEN
+          v_Message := CUR_ProductionPlan.line;
+        ELSE
+	  v_Message := v_Message || ', ' || CUR_ProductionPlan.line;
+	END IF;
+      END LOOP;
+      v_Message := '@ProducedProductWithNegativeQty@' || ' @ReferProductionPlanLines@' || ': ' || v_Message;
+      RAISE_APPLICATION_ERROR(-20000, v_Message);
+    END IF;   
+
+    SELECT COUNT(M_ProductionPlan_ID)
+    INTO v_count
+    FROM M_ProductionPlan pp
+    WHERE pp.M_Production_ID = v_Record_ID
+    AND (SELECT COUNT(M_ProductionLine_ID) FROM M_ProductionLine WHERE M_ProductionPlan_ID = pp.M_ProductionPlan_ID AND movementQty > 0) > 1;
+    
+    IF (v_count <> 0) THEN
+      FOR CUR_ProductionPlan IN (
+	SELECT DISTINCT(pp.line)
+	FROM M_ProductionPlan pp
+	WHERE pp.M_Production_ID = v_Record_ID
+	AND (SELECT COUNT(M_ProductionLine_ID) FROM M_ProductionLine WHERE M_ProductionPlan_ID = pp.M_ProductionPlan_ID AND movementQty > 0) > 1
+	ORDER BY Line
+      )
+      LOOP
+        IF v_Message IS NULL THEN
+          v_Message := CUR_ProductionPlan.line;
+        ELSE
+	  v_Message := v_Message || ', ' || CUR_ProductionPlan.line;
+	END IF;
+      END LOOP;
+      v_Message:= '@ConsumedProductWithPostiveQty@' || ' @ReferProductionPlanLines@' || ': ' || v_Message;
+      RAISE_APPLICATION_ERROR(-20000, v_Message);
+    END IF;
+    
     SELECT count(*) INTO v_count
     FROM dual
     WHERE EXISTS (
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Tue Aug 13 11:58:10 2013 +0530
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Aug 14 18:42:36 2013 +0530
@@ -16908,6 +16908,18 @@
 <!--1DCBDE7B658A4735A04DDE9F79B4DAC2-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--1DCBDE7B658A4735A04DDE9F79B4DAC2--></AD_MESSAGE>
 
+<!--1DDD7506273E4408820998C00EE2AC1D--><AD_MESSAGE>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <AD_MESSAGE_ID><![CDATA[1DDD7506273E4408820998C00EE2AC1D]]></AD_MESSAGE_ID>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <VALUE><![CDATA[ConsumedProductWithPostiveQty]]></VALUE>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <MSGTEXT><![CDATA[Consumed products is not allowed to have positive quantities.]]></MSGTEXT>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--1DDD7506273E4408820998C00EE2AC1D-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--1DDD7506273E4408820998C00EE2AC1D--></AD_MESSAGE>
+
 <!--1E0F1799B5F144778669B2D23FD9BA9E--><AD_MESSAGE>
 <!--1E0F1799B5F144778669B2D23FD9BA9E-->  <AD_MESSAGE_ID><![CDATA[1E0F1799B5F144778669B2D23FD9BA9E]]></AD_MESSAGE_ID>
 <!--1E0F1799B5F144778669B2D23FD9BA9E-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
@@ -18466,6 +18478,18 @@
 <!--47B524D00C99444EBB128AFF52F66225-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--47B524D00C99444EBB128AFF52F66225--></AD_MESSAGE>
 
+<!--4824BDCE0FDA46ADA4D79BA676791DFF--><AD_MESSAGE>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <AD_MESSAGE_ID><![CDATA[4824BDCE0FDA46ADA4D79BA676791DFF]]></AD_MESSAGE_ID>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <VALUE><![CDATA[ProducedProductWithNegativeQty]]></VALUE>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <MSGTEXT><![CDATA[Produced products is not allowed to have negative quantities.]]></MSGTEXT>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--4824BDCE0FDA46ADA4D79BA676791DFF--></AD_MESSAGE>
+
 <!--48352ADE760D4352AFA5F36873BFB4C1--><AD_MESSAGE>
 <!--48352ADE760D4352AFA5F36873BFB4C1-->  <AD_MESSAGE_ID><![CDATA[48352ADE760D4352AFA5F36873BFB4C1]]></AD_MESSAGE_ID>
 <!--48352ADE760D4352AFA5F36873BFB4C1-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
@@ -20361,6 +20385,18 @@
 <!--804D6F47FC0041C09F86F21DDE1FFF4C-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--804D6F47FC0041C09F86F21DDE1FFF4C--></AD_MESSAGE>
 
+<!--805825FD800246FAA023E5D75B62919B--><AD_MESSAGE>
+<!--805825FD800246FAA023E5D75B62919B-->  <AD_MESSAGE_ID><![CDATA[805825FD800246FAA023E5D75B62919B]]></AD_MESSAGE_ID>
+<!--805825FD800246FAA023E5D75B62919B-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--805825FD800246FAA023E5D75B62919B-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--805825FD800246FAA023E5D75B62919B-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--805825FD800246FAA023E5D75B62919B-->  <VALUE><![CDATA[ReferProductionPlanLines]]></VALUE>
+<!--805825FD800246FAA023E5D75B62919B-->  <MSGTEXT><![CDATA[Refer production plan line(s)]]></MSGTEXT>
+<!--805825FD800246FAA023E5D75B62919B-->  <MSGTYPE><![CDATA[I]]></MSGTYPE>
+<!--805825FD800246FAA023E5D75B62919B-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--805825FD800246FAA023E5D75B62919B-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--805825FD800246FAA023E5D75B62919B--></AD_MESSAGE>
+
 <!--80F0ED31B28B4523A83293151EF87EA5--><AD_MESSAGE>
 <!--80F0ED31B28B4523A83293151EF87EA5-->  <AD_MESSAGE_ID><![CDATA[80F0ED31B28B4523A83293151EF87EA5]]></AD_MESSAGE_ID>
 <!--80F0ED31B28B4523A83293151EF87EA5-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/event/ProductionLineEventHandler.java	Wed Aug 14 18:42:36 2013 +0530
@@ -0,0 +1,87 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2013 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.event;
+
+import java.math.BigDecimal;
+
+import javax.enterprise.event.Observes;
+
+import org.apache.log4j.Logger;
+import org.hibernate.criterion.Restrictions;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.model.Property;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.client.kernel.event.EntityUpdateEvent;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.database.ConnectionProvider;
+import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.model.materialmgmt.transaction.ProductionLine;
+import org.openbravo.model.materialmgmt.transaction.ProductionPlan;
+import org.openbravo.service.db.DalConnectionProvider;
+
+public class ProductionLineEventHandler extends EntityPersistenceEventObserver {
+
+  private static Entity[] entities = { ModelProvider.getInstance().getEntity(
+      ProductionLine.ENTITY_NAME) };
+  protected Logger logger = Logger.getLogger(this.getClass());
+
+  @Override
+  protected Entity[] getObservedEntities() {
+    return entities;
+  }
+
+  public void onUpdate(@Observes
+  EntityUpdateEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    final Entity productionLineEntity = ModelProvider.getInstance().getEntity(
+        ProductionLine.ENTITY_NAME);
+    final BigDecimal ONE = new BigDecimal("1");
+    final BigDecimal ZERO = new BigDecimal("0");
+    final Property productionPlanProperty = productionLineEntity
+        .getProperty(ProductionLine.PROPERTY_PRODUCTIONPLAN);
+    final Property movementQtyProperty = productionLineEntity
+        .getProperty(ProductionLine.PROPERTY_MOVEMENTQUANTITY);
+    final ProductionPlan productionPlan = (ProductionPlan) event
+        .getCurrentState(productionPlanProperty);
+    final BigDecimal currentMovementQty = (BigDecimal) event.getCurrentState(movementQtyProperty);
+    final BigDecimal previousMovementQty = (BigDecimal) event.getPreviousState(movementQtyProperty);
+    OBCriteria<ProductionLine> productionLineCriteria = OBDal.getInstance().createCriteria(
+        ProductionLine.class);
+    productionLineCriteria.add(Restrictions.eq(ProductionLine.PROPERTY_PRODUCTIONPLAN,
+        productionPlan));
+    productionLineCriteria.add(Restrictions.gt(ProductionLine.PROPERTY_MOVEMENTQUANTITY, ONE));
+    if (productionLineCriteria.count() > 0 && previousMovementQty != currentMovementQty
+        && currentMovementQty.compareTo(ZERO) == 1) {
+      String language = OBContext.getOBContext().getLanguage().getLanguage();
+      ConnectionProvider conn = new DalConnectionProvider(false);
+      throw new OBException(Utility.messageBD(conn, "@ConsumedProductWithPostiveQty@", language));
+    } else if (productionLineCriteria.count() == 1 && previousMovementQty != currentMovementQty
+        && currentMovementQty.compareTo(ZERO) == -1) {
+      String language = OBContext.getOBContext().getLanguage().getLanguage();
+      ConnectionProvider conn = new DalConnectionProvider(false);
+      throw new OBException(Utility.messageBD(conn, "@ProducedProductWithNegativeQty@", language));
+    }
+  }
+}