src-db/database/model/triggers/C_BPARTNER_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Mon, 28 Apr 2008 09:28:08 +0000
changeset 785 8dba91261590
parent 781 d68cf344696f
child 1067 4a05c922c488
permissions -rw-r--r--
Fix last update of copyright years in database files
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER C_BPARTNER_TRG">
adrian@94
     3
    <trigger name="C_BPARTNER_TRG" table="C_BPARTNER" fires="after" insert="true" update="false" 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.
juanpablo@771
    19
  * Contributor(s): Openbravo SL
juanpablo@785
    20
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    21
  *
juanpablo@771
    22
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    23
  * file and version.
juanpablo@771
    24
  *************************************************************************
juanpablo@771
    25
  * $Id: C_BPartner_Trg.sql,v 1.4 2002/10/23 03:16:57 jjanke Exp $
juanpablo@771
    26
  ***
juanpablo@771
    27
  * Title: Business Partner new necord
juanpablo@771
    28
  * Description:
juanpablo@771
    29
  *    - create default Account records
juanpablo@771
    30
  *    - add treenode
juanpablo@771
    31
  ************************************************************************/
adrian@94
    32
adrian@94
    33
   TYPE RECORD IS REF CURSOR;
adrian@94
    34
   Cur_Defaults RECORD;
adrian@94
    35
   Cur_Defaults2 RECORD;
adrian@94
    36
adrian@94
    37
  v_xTree_ID        NUMBER;
adrian@94
    38
  v_xParent_ID      NUMBER;
adrian@94
    39
  v_BP_Acct_ID    NUMBER;
adrian@94
    40
adrian@94
    41
BEGIN
adrian@94
    42
    --  Default Accounts for all AcctSchema
adrian@94
    43
 IF (INSERTING) THEN
adrian@94
    44
     FOR Cur_Defaults IN (SELECT  *
adrian@94
    45
                            FROM  C_BP_Group_Acct d1
adrian@94
    46
                           WHERE  d1.C_BP_Group_ID=:new.C_BP_Group_ID) LOOP
adrian@94
    47
         --  Customer
adrian@94
    48
   Ad_Sequence_Next('C_BP_Customer_Acct', Cur_Defaults.C_AcctSchema_ID, v_BP_Acct_ID);
adrian@94
    49
   INSERT INTO C_BP_Customer_Acct
adrian@94
    50
             (C_BP_Customer_Acct_ID,C_BPartner_ID, C_AcctSchema_ID,
adrian@94
    51
             AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
    52
             C_Receivable_Acct, C_Prepayment_Acct, Status)
adrian@94
    53
         VALUES
adrian@94
    54
             (v_BP_Acct_ID,:new.C_BPartner_ID, Cur_Defaults.C_AcctSchema_ID,
antonio@735
    55
             :new.AD_Client_ID, :new.AD_Org_ID, 'Y', now(), :new.CreatedBy, now(), :new.UpdatedBy,
adrian@94
    56
             Cur_Defaults.C_Receivable_Acct, Cur_Defaults.C_Prepayment_Acct, Cur_Defaults.Status);
adrian@94
    57
   --  Vendor
adrian@94
    58
adrian@94
    59
   Ad_Sequence_Next('C_BP_Vendor_Acct', Cur_Defaults.C_AcctSchema_ID, v_BP_Acct_ID);
adrian@94
    60
   INSERT INTO C_BP_Vendor_Acct
adrian@94
    61
             (C_BP_Vendor_Acct_ID, C_BPartner_ID, C_AcctSchema_ID,
adrian@94
    62
             AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
    63
             V_Liability_Acct, V_Liability_Services_Acct, V_Prepayment_Acct, Status)
adrian@94
    64
         VALUES
adrian@94
    65
             (v_BP_Acct_ID,:new.C_BPartner_ID, Cur_Defaults.C_AcctSchema_ID,
antonio@735
    66
             :new.AD_Client_ID, :new.AD_Org_ID, 'Y', now(), :new.CreatedBy, now(), :new.UpdatedBy,
adrian@94
    67
             Cur_Defaults.V_Liability_Acct, Cur_Defaults.V_Liability_Services_Acct, Cur_Defaults.V_Prepayment_Acct, Cur_Defaults.Status);
adrian@94
    68
  END LOOP;
adrian@94
    69
  FOR Cur_Defaults2 IN (SELECT * FROM C_AcctSchema_Default d2
adrian@94
    70
                WHERE EXISTS (SELECT * FROM AD_ClientInfo c2
adrian@94
    71
                   WHERE d2.C_AcctSchema_ID IN (c2.C_AcctSchema1_ID, c2.C_AcctSchema2_ID, c2.C_AcctSchema3_ID)
adrian@94
    72
           AND AD_Client_ID=:new.AD_Client_ID)) LOOP
adrian@94
    73
   --  Employee
adrian@94
    74
         INSERT INTO C_BP_Employee_Acct
adrian@94
    75
             (C_BPartner_ID, C_AcctSchema_ID,
adrian@94
    76
             AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
    77
             E_Expense_Acct, E_Prepayment_Acct)
adrian@94
    78
         VALUES
adrian@94
    79
             (:new.C_BPartner_ID, Cur_Defaults2.C_AcctSchema_ID,
antonio@735
    80
             :new.AD_Client_ID, :new.AD_Org_ID, 'Y', now(), :new.CreatedBy, now(), :new.UpdatedBy,
adrian@94
    81
             Cur_Defaults2.E_Expense_Acct, Cur_Defaults2.E_Prepayment_Acct);
adrian@94
    82
     END LOOP;
adrian@94
    83
adrian@94
    84
     --  Create TreeNode --
adrian@94
    85
     --  get AD_Tree_ID + ParentID
adrian@94
    86
     SELECT  c.AD_Tree_BPartner_ID, n.Node_ID
adrian@94
    87
    INTO  v_xTree_ID, v_xParent_ID
adrian@94
    88
     FROM    AD_ClientInfo c, AD_TreeNodeBP n
adrian@94
    89
     WHERE   c.AD_Tree_BPartner_ID=n.AD_Tree_ID
adrian@94
    90
         AND n.Parent_ID IS NULL
adrian@94
    91
         AND c.AD_Client_ID=:new.AD_Client_ID;
adrian@94
    92
adrian@94
    93
     --  DBMS_OUTPUT.PUT_LINE('Tree='||v_xTree_ID||'  Node='||:new.AD_Menu_ID||'  Parent='||v_xParent_ID);
adrian@94
    94
adrian@94
    95
     --  Insert into TreeNode
adrian@94
    96
     INSERT INTO AD_TreeNodeBP
adrian@94
    97
         (AD_Client_ID, AD_Org_ID,
adrian@94
    98
         IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
    99
         AD_Tree_ID, Node_ID,
adrian@94
   100
         Parent_ID, SeqNo)
adrian@94
   101
     VALUES
adrian@94
   102
         (:new.AD_Client_ID, :new.AD_Org_ID,
adrian@94
   103
         :new.IsActive, :new.Created, :new.CreatedBy, :new.Updated, :new.UpdatedBy,
adrian@94
   104
         v_xTree_ID, :new.C_BPartner_ID,
adrian@94
   105
         v_xParent_ID, (CASE :new.IsSummary WHEN 'Y' THEN 100 ELSE 999 END));     -- Summary Nodes first
adrian@94
   106
     ELSIF (DELETING) THEN
adrian@94
   107
     DELETE FROM AD_TreeNodeBP WHERE AD_Client_ID = :old.AD_Client_ID AND Node_ID = :old.C_BPartner_ID;
adrian@94
   108
  END IF;
adrian@94
   109
EXCEPTION
adrian@94
   110
 WHEN NO_DATA_FOUND THEN
antonio@735
   111
  RAISE_APPLICATION_ERROR(-20004, 'C_BPartner InsertTrigger Error: No ClientInfo or parent TreeNode');
adrian@94
   112
antonio@735
   113
END C_BPARTNER_TRG
gorkaion@239
   114
]]></body>
adrian@94
   115
    </trigger>
adrian@94
   116
  </database>