src-db/database/model/triggers/M_PRODUCT_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 1067 4a05c922c488
child 2078 cf88ca44cdd2
permissions -rw-r--r--
Merge r2.5x intro trunk
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER M_PRODUCT_TRG">
adrian@94
     3
    <trigger name="M_PRODUCT_TRG" table="M_PRODUCT" fires="after" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
asier@799
     5
/*************************************************************************
asier@799
     6
  * The contents of this file are subject to the Compiere Public
asier@799
     7
  * License 1.1 ("License"); You may not use this file except in
asier@799
     8
  * compliance with the License. You may obtain a copy of the License in
asier@799
     9
  * the legal folder of your Openbravo installation.
asier@799
    10
  * Software distributed under the License is distributed on an
asier@799
    11
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
asier@799
    12
  * implied. See the License for the specific language governing rights
asier@799
    13
  * and limitations under the License.
asier@799
    14
  * The Original Code is  Compiere  ERP &  Business Solution
asier@799
    15
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
asier@799
    16
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
asier@799
    17
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
asier@799
    18
  * All Rights Reserved.
asier@799
    19
  * Contributor(s): Openbravo SL
asier@799
    20
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
asier@799
    21
  *
asier@799
    22
  * Specifically, this derivative work is based upon the following Compiere
asier@799
    23
  * file and version.
asier@799
    24
  *************************************************************************/
adrian@94
    25
adrian@94
    26
   TYPE RECORD IS REF CURSOR;
adrian@94
    27
   Cur_Defaults RECORD;
adrian@94
    28
juanpablo@1605
    29
    v_xTree_ID    varchar2(32);
juanpablo@1605
    30
    v_xParent_ID  varchar2(32);
juanpablo@1605
    31
    v_NextNo      VARCHAR2(32);
adrian@94
    32
    v_ControlNo NUMBER;
adrian@94
    33
adrian@94
    34
BEGIN
eduardo@1067
    35
eduardo@1067
    36
 IF (UPDATING) THEN
eduardo@1067
    37
  UPDATE M_PRODUCT_ACCT SET AD_ORG_ID = :new.AD_ORG_ID
eduardo@1067
    38
  WHERE M_PRODUCT_ID = :new.M_PRODUCT_ID;
adrian@94
    39
 -- Do not allow to de-activate products with OnHand Qty
adrian@94
    40
  IF (:new.IsActive='N' AND :old.IsActive='Y') THEN
eduardo@1067
    41
   SELECT  COALESCE(SUM(QtyOnHand)+SUM(QtyReserved)*.111+SUM(QtyOrdered)*999, 0) INTO v_ControlNo
adrian@94
    42
   FROM (SELECT QtyOnHand, 0 AS QtyReserved, 0 AS QtyOrdered
adrian@94
    43
   FROM M_Storage_Detail s
adrian@94
    44
   WHERE s.M_Product_ID=:new.M_Product_ID
adrian@94
    45
   UNION
adrian@94
    46
   SELECT 0 AS QtyOnHand, QtyReserved, QtyOrdered
adrian@94
    47
   FROM M_Storage_Pending s
adrian@94
    48
   WHERE s.M_Product_ID=:new.M_Product_ID) A;
gorkaion@239
    49
   IF (v_ControlNo <> 0) THEN
adrian@94
    50
    RAISE_APPLICATION_ERROR(-20400, 'Product has active Inventory');
adrian@94
    51
   END IF;
adrian@94
    52
  END IF;
antonio@737
    53
antonio@737
    54
  IF( COALESCE(:old.Name,'.') <> COALESCE(:new.Name,'.')
gorka@367
    55
  OR COALESCE(:old.documentnote,'.') <> COALESCE(:new.documentnote,'.') ) THEN
gorka@367
    56
    UPDATE M_PRODUCT_Trl
gorka@367
    57
      SET IsTranslated='N'
gorka@367
    58
    WHERE M_PRODUCT_ID=:new.M_PRODUCT_ID;
gorka@367
    59
  END IF;
gorka@367
    60
adrian@94
    61
  RETURN;
adrian@94
    62
 ELSIF (INSERTING) THEN
adrian@94
    63
  -- For all AcctSchema create ...
adrian@94
    64
  FOR Cur_Defaults IN (SELECT  *
adrian@94
    65
             FROM  M_Product_Category_Acct d
adrian@94
    66
        WHERE  d.M_Product_Category_ID=:new.M_Product_Category_ID) LOOP
adrian@94
    67
         --  Account
adrian@94
    68
         INSERT INTO M_Product_Acct
juanpablo@1605
    69
             (M_Product_Acct_ID, M_Product_ID, C_AcctSchema_ID,
adrian@94
    70
             AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
    71
             P_Revenue_Acct, P_Expense_Acct, P_Asset_Acct, P_COGS_Acct,
adrian@94
    72
    P_PurchasePriceVariance_Acct, P_InvoicePriceVariance_Acct,
adrian@94
    73
    P_TradeDiscountRec_Acct, P_TradeDiscountGrant_Acct)
adrian@94
    74
         VALUES
juanpablo@1605
    75
             (get_uuid(), :new.M_Product_ID, Cur_Defaults.C_AcctSchema_ID,
antonio@735
    76
             :new.AD_Client_ID, :new.AD_ORG_ID, 'Y', now(), :new.CreatedBy, now(), :new.UpdatedBy,
adrian@94
    77
             Cur_Defaults.P_Revenue_Acct, Cur_Defaults.P_Expense_Acct, Cur_Defaults.P_Asset_Acct, Cur_Defaults.P_COGS_Acct,
adrian@94
    78
    Cur_Defaults.P_PurchasePriceVariance_Acct, Cur_Defaults.P_InvoicePriceVariance_Acct,
adrian@94
    79
    Cur_Defaults.P_TradeDiscountRec_Acct, Cur_Defaults.P_TradeDiscountGrant_Acct);
adrian@94
    80
asier@799
    81
   END LOOP;
adrian@94
    82
adrian@94
    83
adrian@94
    84
     --  Create Translation Row
adrian@94
    85
     INSERT INTO M_Product_Trl
juanpablo@1605
    86
         (M_Product_Trl_ID, M_Product_ID, AD_Language, AD_Client_ID, AD_Org_ID,
adrian@94
    87
         IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
    88
         Name, DocumentNote, IsTranslated)
juanpablo@1605
    89
     SELECT get_uuid(), :new.M_Product_ID, AD_Language, :new.AD_Client_ID, :new.AD_Org_ID,
adrian@94
    90
         :new.IsActive, :new.Created, :new.CreatedBy, :new.Updated, :new.UpdatedBy,
adrian@94
    91
         :new.Name, :new.DocumentNote, 'N'
adrian@94
    92
     FROM    AD_Language
adrian@94
    93
     WHERE IsActive = 'Y' AND IsSystemLanguage = 'Y'
adrian@94
    94
    AND EXISTS (SELECT * FROM AD_Client
adrian@94
    95
     WHERE AD_Client_ID=:new.AD_Client_ID AND IsMultiLingualDocument='Y');
adrian@94
    96
adrian@94
    97
adrian@94
    98
     --  Create TreeNode --
adrian@94
    99
     --  get AD_Tree_ID + ParentID
adrian@94
   100
     SELECT  c.AD_Tree_Product_ID, n.Node_ID INTO v_xTree_ID, v_xParent_ID
adrian@94
   101
     FROM    AD_ClientInfo c, AD_TreeNodePR n
adrian@94
   102
     WHERE   c.AD_Tree_Product_ID=n.AD_Tree_ID
adrian@94
   103
         AND n.Parent_ID IS NULL
adrian@94
   104
         AND c.AD_Client_ID=:new.AD_Client_ID;
adrian@94
   105
adrian@94
   106
     --  DBMS_OUTPUT.PUT_LINE('Tree='||v_xTree_ID||'  Node='||:new.M_Product_ID||'  Parent='||v_xParent_ID);
adrian@94
   107
adrian@94
   108
     --  Insert into TreeNode
adrian@94
   109
     INSERT INTO AD_TreeNodePR
juanpablo@1605
   110
         (AD_TreeNodePR_ID, AD_Client_ID, AD_Org_ID,
adrian@94
   111
         IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
   112
         AD_Tree_ID, Node_ID,
adrian@94
   113
         Parent_ID, SeqNo)
adrian@94
   114
     VALUES
juanpablo@1605
   115
         (get_uuid(), :new.AD_Client_ID, :new.AD_Org_ID,
adrian@94
   116
         :new.IsActive, :new.Created, :new.CreatedBy, :new.Updated, :new.UpdatedBy,
adrian@94
   117
         v_xTree_ID, :new.M_Product_ID,
adrian@94
   118
         v_xParent_ID, (CASE :new.IsSummary WHEN 'Y' THEN 100 ELSE 999 END));     -- Summary Nodes first
adrian@94
   119
 ELSIF (DELETING) THEN
adrian@94
   120
    DELETE FROM AD_TreeNodePR WHERE AD_Client_ID = :old.AD_Client_ID AND Node_ID = :old.M_Product_ID;
adrian@94
   121
 END IF;
adrian@94
   122
EXCEPTION
adrian@94
   123
 WHEN NO_DATA_FOUND THEN
antonio@735
   124
  RAISE_APPLICATION_ERROR(-20009, 'M_Product InsertTrigger Error: No ClientInfo or parent TreeNode');
adrian@94
   125
antonio@735
   126
END M_PRODUCT_TRG
gorkaion@239
   127
]]></body>
adrian@94
   128
    </trigger>
adrian@94
   129
  </database>