Fixes issue 31912: It is not possible to delete a product with a permanent cost
authorMark <markmm82@gmail.com>
Mon, 09 May 2016 10:27:19 -0400
changeset 29452 719ef21233f6
parent 29451 25cda173d4d8
child 29453 4c7b579487bd
Fixes issue 31912: It is not possible to delete a product with a permanent cost

When a product is removed, the M_COSTING trigger is fired in cascade to delete the related costs, and it don't allow to remove the existing permanents costings associated to the product. To allow to remove the product with permanent costs was modified the M_COSTING_TRG to remove the permanent costs if the product have not related documents.
src-db/database/model/triggers/M_COSTING_TRG.xml
--- a/src-db/database/model/triggers/M_COSTING_TRG.xml	Mon May 09 14:06:13 2016 +0200
+++ b/src-db/database/model/triggers/M_COSTING_TRG.xml	Mon May 09 10:27:19 2016 -0400
@@ -15,11 +15,12 @@
 * 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 Openbravo SLU
+* All portions are Copyright (C) 2012-2016 Openbravo SLU
 * All Rights Reserved.
 * Contributor(s):  ______________________________________.
 ************************************************************************/
 v_count NUMBER;
+v_relateddocs BOOLEAN;
 BEGIN
   IF AD_isTriggerEnabled()='N' THEN RETURN;
   END IF;
@@ -32,11 +33,20 @@
       RAISE_APPLICATION_ERROR(-20000, '@CostMigratedInstance@');
     END IF;
   ELSIF (DELETING) THEN
-    IF (:OLD.ISPERMANENT = 'Y') THEN
+    SELECT EXISTS ( SELECT 1 FROM C_OrderLine WHERE m_product_id = :OLD.m_product_id )
+       OR EXISTS ( SELECT 1 FROM C_InvoiceLine WHERE m_product_id = :OLD.m_product_id )
+       OR EXISTS ( SELECT 1 FROM M_InventoryLine WHERE m_product_id = :OLD.m_product_id )
+       OR EXISTS ( SELECT 1 FROM M_MovementLine WHERE m_product_id = :OLD.m_product_id )
+       OR EXISTS ( SELECT 1 FROM M_InoutLine WHERE m_product_id = :OLD.m_product_id )
+       OR EXISTS ( SELECT 1 FROM M_ProductionLine WHERE m_product_id = :OLD.m_product_id )
+       OR EXISTS ( SELECT 1 FROM M_Internal_ConsumptionLine WHERE m_product_id = :OLD.m_product_id )
+    INTO v_relateddocs;
+
+    IF (:OLD.ISPERMANENT = 'Y' AND v_relateddocs = 't') THEN
       RAISE_APPLICATION_ERROR(-20000, '@CannotDeletePermanentCost@');
     END IF;
   ELSIF (UPDATING) THEN
-    IF (:OLD.ISPERMANENT = 'Y' 
+    IF (:OLD.ISPERMANENT = 'Y'
         AND (:OLD.COST <> :NEW.COST
              OR :OLD.DATEFROM <> :NEW.DATEFROM)) THEN
       RAISE_APPLICATION_ERROR(-20000, '@CannotModifyPermanentCost@');