src-db/database/model/triggers/AD_ORG_TRG.xml
author Harikrishnan Raja <harikrishnan.raja@openbravo.com>
Tue, 14 Sep 2010 13:32:07 +0530
changeset 8389 97a50a6985e3
parent 6701 690d2af86089
child 8897 6f95ff3ad91e
permissions -rw-r--r--
Fixes Issue 14229: Delete Parent Organizations
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER AD_ORG_TRG">
eduardo@6019
     3
    <trigger name="AD_ORG_TRG" table="AD_ORG" fires="after" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
juanpablo@771
     5
  /*************************************************************************
juanpablo@771
     6
  * The contents of this file are subject to the Compiere Public
juanpablo@771
     7
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
     8
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
     9
  * the legal folder of your Openbravo installation.
juanpablo@771
    10
  * Software distributed under the License is distributed on an
juanpablo@771
    11
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
juanpablo@771
    12
  * implied. See the License for the specific language governing rights
juanpablo@771
    13
  * and limitations under the License.
juanpablo@778
    14
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    15
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
juanpablo@771
    16
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
juanpablo@771
    17
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
juanpablo@771
    18
  * All Rights Reserved.
ggi@6701
    19
  * Contributor(s): Openbravo SLU
ggi@6701
    20
  * Contributions are Copyright (C) 2001-2009 Openbravo, S.L.U.
juanpablo@771
    21
  *
juanpablo@771
    22
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    23
  * file and version.
juanpablo@774
    24
  *************************************************************************/
juanpablo@1605
    25
  v_xTree_ID VARCHAR2(32);
juanpablo@1605
    26
  v_xParent_ID VARCHAR2(32);
asier@1929
    27
  TYPE RECORD IS REF CURSOR;
asier@1929
    28
    CUR_PeriodControl RECORD;
harikrishnan@8389
    29
    v_count NUMBER;
asier@1929
    30
  
asier@2084
    31
    
adrian@94
    32
BEGIN
asier@2084
    33
    
asier@2084
    34
    IF AD_isTriggerEnabled()='N' THEN RETURN;
asier@2078
    35
    END IF;
asier@2078
    36
asier@2078
    37
adrian@94
    38
  IF(INSERTING) THEN
adrian@94
    39
    -- Add to all roles of the client
adrian@94
    40
    INSERT
adrian@94
    41
    INTO AD_Role_OrgAccess
adrian@94
    42
      (
david@1775
    43
        AD_Role_OrgAccess_ID, AD_Role_ID, AD_Client_ID, AD_Org_ID,
adrian@94
    44
        IsActive, Created, CreatedBy,
adrian@94
    45
        Updated, UpdatedBy
adrian@94
    46
      )
david@1775
    47
    SELECT get_uuid(), AD_Role_ID, :new.AD_Client_ID, :new.AD_Org_ID,
antonio@735
    48
       'Y', now(), :new.CreatedBy,
antonio@735
    49
      now(), :new.CreatedBy
adrian@94
    50
    FROM AD_Role
asier@5126
    51
    WHERE AD_Client_ID=:new.AD_Client_ID
asier@5126
    52
      AND IsManual='N';
asier@5126
    53
      
adrian@94
    54
    --  Create TreeNode --
adrian@94
    55
    --  get AD_Tree_ID + ParentID
adrian@94
    56
    SELECT c.AD_Tree_Org_ID,
adrian@94
    57
      n.Node_ID
adrian@94
    58
    INTO v_xTree_ID,
adrian@94
    59
      v_xParent_ID
adrian@94
    60
    FROM AD_ClientInfo c,
adrian@94
    61
      AD_TreeNode n
adrian@94
    62
    WHERE c.AD_Tree_Org_ID=n.AD_Tree_ID
adrian@94
    63
      AND n.Parent_ID IS NULL
adrian@94
    64
      AND c.AD_Client_ID=:new.AD_Client_ID;
adrian@94
    65
    -- DBMS_OUTPUT.PUT_LINE('Tree='||v_xTree_ID||'  Node='||:new.AD_Org_ID||'  Parent='||v_xParent_ID);
adrian@94
    66
    --  Insert into TreeNode
adrian@94
    67
    INSERT
adrian@94
    68
    INTO AD_TreeNode
adrian@94
    69
      (
juanpablo@1605
    70
        ad_treeNode_Id, AD_Client_ID, AD_Org_ID, IsActive,
adrian@94
    71
        Created, CreatedBy, Updated,
adrian@94
    72
        UpdatedBy, AD_Tree_ID, Node_ID,
adrian@94
    73
        Parent_ID, SeqNo
adrian@94
    74
      )
adrian@94
    75
      VALUES
adrian@94
    76
      (
juanpablo@1605
    77
        get_uuid(), :new.AD_Client_ID, :new.AD_Org_ID, :new.IsActive,
adrian@94
    78
        :new.Created, :new.CreatedBy, :new.Updated,
adrian@94
    79
        :new.UpdatedBy, v_xTree_ID, :new.AD_Org_ID,
adrian@94
    80
        v_xParent_ID,(
adrian@94
    81
        CASE :new.IsSummary
adrian@94
    82
          WHEN 'Y'
adrian@94
    83
          THEN 100
adrian@94
    84
          ELSE 999
adrian@94
    85
        END
adrian@94
    86
        )
adrian@94
    87
      )
adrian@94
    88
      ;
adrian@94
    89
    -- Summary Nodes first
adrian@94
    90
    -- Org Info
adrian@94
    91
    INSERT
adrian@94
    92
    INTO AD_OrgInfo
adrian@94
    93
      (
adrian@94
    94
        AD_Org_ID, AD_Client_ID, IsActive,
adrian@94
    95
        Created, CreatedBy, Updated,
adrian@94
    96
        UpdatedBy, C_Location_ID, Duns,
asier@799
    97
        TaxID
adrian@94
    98
      )
adrian@94
    99
      VALUES
adrian@94
   100
      (
adrian@94
   101
        :new.AD_Org_ID, :new.AD_Client_ID, 'Y',
antonio@735
   102
        now(), :new.CreatedBy, now(),
adrian@94
   103
        :new.CreatedBy, NULL, '?',
asier@799
   104
         '?'
adrian@94
   105
      )
adrian@94
   106
      ;
asier@1929
   107
        
adrian@94
   108
  ELSIF(DELETING) THEN
adrian@94
   109
    --  Delete TreeNode --
adrian@94
   110
    --  get AD_Tree_ID
adrian@94
   111
    SELECT c.AD_Tree_Org_ID
adrian@94
   112
    INTO v_xTree_ID
adrian@94
   113
    FROM AD_ClientInfo c
adrian@94
   114
    WHERE c.AD_Client_ID=:old.AD_Client_ID;
harikrishnan@8389
   115
    select COUNT(*) INTO v_count from AD_TREENODE where parent_id=:old.AD_ORG_ID
harikrishnan@8389
   116
  	and AD_TREE_ID=v_xTree_ID;
harikrishnan@8389
   117
    IF(v_count>0) THEN
harikrishnan@8389
   118
    	RAISE_APPLICATION_ERROR(-20000, '@ChildOrgCheck@') ;
harikrishnan@8389
   119
    END IF;
adrian@94
   120
    DELETE
adrian@94
   121
    FROM AD_TREENODE
adrian@94
   122
    WHERE AD_CLIENT_ID=:old.AD_Client_ID
adrian@94
   123
      AND AD_Tree_ID=v_xTree_ID
victor@3099
   124
      AND Node_ID=:old.AD_Org_ID;     
adrian@94
   125
  END IF;
adrian@94
   126
  -- Deleting
adrian@94
   127
EXCEPTION
adrian@94
   128
WHEN NO_DATA_FOUND THEN
adrian@94
   129
  RAISE_APPLICATION_ERROR(-20014, 'AD_Org InsertTrigger Error: No ClientInfo or parent TreeNode') ;
antonio@735
   130
END AD_ORG_TRG
gorkaion@239
   131
]]></body>
adrian@94
   132
    </trigger>
adrian@94
   133
  </database>