src-db/database/model/triggers/M_PRODUCT_TRG.xml
changeset 22744 f25d4926a29a
parent 22741 18e0d8ef18ce
child 22745 ca8c20bc2f67
equal deleted inserted replaced
22743:7e2d830d916b 22744:f25d4926a29a
    15   * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
    15   * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
    16   * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
    16   * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
    17   * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
    17   * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
    18   * All Rights Reserved.
    18   * All Rights Reserved.
    19   * Contributor(s): Openbravo SLU
    19   * Contributor(s): Openbravo SLU
    20   * Contributions are Copyright (C) 2001-2014 Openbravo, S.L.U.
    20   * Contributions are Copyright (C) 2001-2013 Openbravo, S.L.U.
    21   *
    21   *
    22   * Specifically, this derivative work is based upon the following Compiere
    22   * Specifically, this derivative work is based upon the following Compiere
    23   * file and version.
    23   * file and version.
    24   *************************************************************************/
    24   *************************************************************************/
    25 
    25 
    26    TYPE RECORD IS REF CURSOR;
    26    TYPE RECORD IS REF CURSOR;
    27    Cur_Defaults RECORD;
    27    Cur_Defaults RECORD;
    28    Cur_Product RECORD;
       
    29 
    28 
    30     v_xTree_ID    			           varchar2(32);
    29     v_xTree_ID    			           varchar2(32);
    31     v_xParent_ID  			           varchar2(32);
    30     v_xParent_ID  			           varchar2(32);
    32     v_NextNo      			           VARCHAR2(32);
    31     v_NextNo      			           VARCHAR2(32);
    33     v_ControlNo 	 	               NUMBER;
    32     v_ControlNo 	 	               NUMBER;
    45     v_P_Revenue_Return_Acct    VARCHAR2(32);
    44     v_P_Revenue_Return_Acct    VARCHAR2(32);
    46     v_seq_name				             VARCHAR2(32);
    45     v_seq_name				             VARCHAR2(32);
    47     v_Sequence_No			             VARCHAR2(32);
    46     v_Sequence_No			             VARCHAR2(32);
    48     v_C_AcctSchema_ID			         VARCHAR2(32);
    47     v_C_AcctSchema_ID			         VARCHAR2(32);
    49     v_Count 	 	               NUMBER;
    48     v_Count 	 	               NUMBER;
    50     v_QtyOnHand							   NUMBER;
    49     v_qtyin_hand 	 	       NUMBER;
    51     v_QtyReserved 						 NUMBER;
       
    52     v_QtyOrdered 							 NUMBER;
       
    53     v_Message                  VARCHAR(2000);
       
    54   			
    50   			
    55 BEGIN
    51 BEGIN
    56     IF AD_isTriggerEnabled()='N' THEN RETURN;
    52     IF AD_isTriggerEnabled()='N' THEN RETURN;
    57     END IF;
    53     END IF;
    58 
    54 
    64  -- or Change the attribute set
    60  -- or Change the attribute set
    65   IF ((:new.IsActive='N' AND :old.IsActive='Y')
    61   IF ((:new.IsActive='N' AND :old.IsActive='Y')
    66       OR (COALESCE(:new.m_attributeset_id,'null') <> COALESCE(:old.m_attributeset_id,'null'))
    62       OR (COALESCE(:new.m_attributeset_id,'null') <> COALESCE(:old.m_attributeset_id,'null'))
    67       OR (COALESCE(:new.c_uom_id,'null') <> COALESCE(:old.c_uom_id,'null'))
    63       OR (COALESCE(:new.c_uom_id,'null') <> COALESCE(:old.c_uom_id,'null'))
    68       OR (:old.isStocked='Y' AND :new.isStocked='N')) THEN
    64       OR (:old.isStocked='Y' AND :new.isStocked='N')) THEN
    69    SELECT  COALESCE(SUM(QtyOnHand)+SUM(QtyReserved)*.111+SUM(QtyOrdered)*999, 0) ,
    65    SELECT  COALESCE(SUM(QtyOnHand)+SUM(QtyReserved)*.111+SUM(QtyOrdered)*999, 0) INTO v_ControlNo
    70    COALESCE(sum(QtyOnHand),0), COALESCE(SUM(QtyReserved),0) , COALESCE(SUM(QtyOrdered),0)
       
    71    INTO v_ControlNo, v_QtyOnHand, v_QtyReserved, v_QtyOrdered
       
    72    FROM (SELECT QtyOnHand, 0 AS QtyReserved, 0 AS QtyOrdered
    66    FROM (SELECT QtyOnHand, 0 AS QtyReserved, 0 AS QtyOrdered
    73    FROM M_Storage_Detail s
    67    FROM M_Storage_Detail s
    74    WHERE s.M_Product_ID=:new.M_Product_ID
    68    WHERE s.M_Product_ID=:new.M_Product_ID
    75    UNION
    69    UNION
    76    SELECT 0 AS QtyOnHand, QtyReserved, QtyOrdered
    70    SELECT 0 AS QtyOnHand, QtyReserved, QtyOrdered
    77    FROM M_Storage_Pending s
    71    FROM M_Storage_Pending s
    78    WHERE s.M_Product_ID=:new.M_Product_ID) A;
    72    WHERE s.M_Product_ID=:new.M_Product_ID) A;
       
    73    select COALESCE(sum(QtyOnHand),0) Into v_qtyin_hand from (
       
    74    SELECT QtyOnHand
       
    75    FROM M_Storage_Detail s
       
    76    WHERE s.M_Product_ID=:new.M_Product_ID
       
    77    UNION
       
    78    SELECT 0 AS QtyOnHand
       
    79    FROM M_Storage_Pending s
       
    80    WHERE s.M_Product_ID=:new.M_Product_ID )A;
    79    IF (v_ControlNo <> 0) THEN
    81    IF (v_ControlNo <> 0) THEN
    80 		IF (v_QtyOnHand <> 0) THEN
    82     IF (COALESCE(:new.m_attributeset_id,'null') <> COALESCE(:old.m_attributeset_id,'null')) THEN
    81 			IF (COALESCE(:new.m_attributeset_id,'null') <> COALESCE(:old.m_attributeset_id,'null')) THEN
    83       RAISE_APPLICATION_ERROR(-20000, '@20633@');
    82 				RAISE_APPLICATION_ERROR(-20000, '@20633@');
    84     ELSIF (COALESCE(:new.c_uom_id,'null') <> COALESCE(:old.c_uom_id,'null')) THEN
    83 			ELSIF (COALESCE(:new.c_uom_id,'null') <> COALESCE(:old.c_uom_id,'null')) THEN
    85       RAISE_APPLICATION_ERROR(-20000, '@20634@');
    84 				RAISE_APPLICATION_ERROR(-20000, '@20634@');
    86     ELSIF (:old.isStocked = 'Y' AND :new.isStocked = 'N') THEN
    85 			ELSIF (:old.isStocked = 'Y' AND :new.isStocked = 'N') THEN
    87       RAISE_APPLICATION_ERROR(-20000, '@StockedProductWithMovements@');
    86 				RAISE_APPLICATION_ERROR(-20000, '@StockedProductWithMovements@');
    88     ELSIF (v_qtyin_hand=0) THEN
    87 			ELSE
    89       RAISE_APPLICATION_ERROR(-20000, '@ActivePoSo@');
    88 				RAISE_APPLICATION_ERROR(-20000, '@20400@');
    90     ELSE
    89 			END IF;
    91       RAISE_APPLICATION_ERROR(-20000, '@20400@');
    90 		END IF;
    92     END IF;
    91 		IF(v_QtyOrdered <> 0 OR v_QtyReserved <>0) THEN
       
    92 		v_Message:='';
       
    93 		FOR Cur_Product IN (
       
    94 			SELECT dt.name, o.documentno
       
    95 			from c_orderline ol
       
    96 			left join c_order o ON o.c_order_id = ol.c_order_id
       
    97 			left join c_doctype dt on dt.c_doctype_id = o.c_doctype_id
       
    98 			WHERE CASE WHEN o.issotrx = 'N'  THEN ol.qtyordered + ol.qtyreserved <> 0
       
    99 			ELSE ol.qtyordered - ol.qtydelivered <> 0 END
       
   100 			AND ol.m_product_id = :new.m_product_id
       
   101 			AND o.docstatus='CO'
       
   102 			AND o.processed='Y')
       
   103 		LOOP
       
   104 			v_Message:= v_Message || Cur_Product.name ||'-'|| Cur_Product.documentno||',';
       
   105 		END LOOP;
       
   106 			RAISE_APPLICATION_ERROR(-20000, v_Message||'@ActivePoSo@');
       
   107 		END IF;
       
   108    END IF;
    93    END IF;
   109   END IF;
    94   END IF;
   110 
    95 
   111   IF( COALESCE(:old.Name,'.') <> COALESCE(:new.Name,'.')  OR COALESCE(:old.documentnote,'.') <> COALESCE(:new.documentnote,'.') ) THEN   
    96   IF( COALESCE(:old.Name,'.') <> COALESCE(:new.Name,'.')  OR COALESCE(:old.documentnote,'.') <> COALESCE(:new.documentnote,'.') ) THEN   
   112     UPDATE M_PRODUCT_Trl
    97     UPDATE M_PRODUCT_Trl