src-db/database/model/triggers/C_VALIDCOMBINATION_TRG.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 756 ae11e4610537
child 1610 84e498902fbd
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER C_VALIDCOMBINATION_TRG">
adrian@94
     3
    <trigger name="C_VALIDCOMBINATION_TRG" table="C_VALIDCOMBINATION" fires="before" insert="true" update="true" delete="false" 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_Elements RECORD;
adrian@94
    28
adrian@94
    29
adrian@94
    30
 v_Combination      VARCHAR(255) := '';
adrian@94
    31
 v_Description      VARCHAR(255) := '';
adrian@94
    32
 v_IsFullyQualified CHAR(1) := 'Y';
adrian@94
    33
 v_Separator      CHAR(1);
adrian@94
    34
 v_TempValue   VARCHAR(255) := '_';
adrian@94
    35
 v_TempName   VARCHAR(255) := '_';
adrian@94
    36
adrian@94
    37
 Not_Fully_Qualified Exception;
adrian@94
    38
adrian@94
    39
 --  Individual Statements
adrian@94
    40
 CURSOR Cur_OO IS
adrian@94
    41
  SELECT Value, Name
adrian@94
    42
  FROM AD_Org WHERE AD_Org_ID=:new.AD_Org_ID AND IsActive='Y';
adrian@94
    43
 CURSOR Cur_AC IS
adrian@94
    44
  SELECT Value, Name
adrian@94
    45
  FROM C_ElementValue WHERE C_ElementValue_ID=:new.Account_ID AND IsActive = 'Y';
adrian@94
    46
 CURSOR Cur_PR IS
adrian@94
    47
  SELECT Value, Name
adrian@94
    48
  FROM M_Product WHERE M_Product_ID=:new.M_Product_ID AND IsActive='Y';
adrian@94
    49
 CURSOR Cur_BP IS
adrian@94
    50
  SELECT Value, Name
adrian@94
    51
  FROM C_BPartner WHERE C_BPartner_ID=:new.C_BPartner_ID AND IsActive = 'Y';
adrian@94
    52
 CURSOR Cur_TO IS
adrian@94
    53
  SELECT Value, Name
adrian@94
    54
  FROM AD_Org WHERE AD_Org_ID=:new.AD_OrgTrx_ID AND IsActive='Y';
adrian@94
    55
 CURSOR Cur_LF IS
adrian@94
    56
  SELECT Postal, City
adrian@94
    57
  FROM C_Location WHERE C_Location_ID=:new.C_LocFrom_ID AND IsActive='Y';
adrian@94
    58
 CURSOR Cur_LT IS
adrian@94
    59
  SELECT Postal, City
adrian@94
    60
  FROM C_Location WHERE C_Location_ID=:new.C_LocTo_ID AND IsActive='Y';
adrian@94
    61
 CURSOR Cur_SR IS
adrian@94
    62
  SELECT Value, Name
adrian@94
    63
  FROM C_SalesRegion WHERE C_SalesRegion_ID=:new.C_SalesRegion_ID AND IsActive='Y';
adrian@94
    64
 CURSOR Cur_PJ IS
adrian@94
    65
  SELECT Value, Name
adrian@94
    66
  FROM C_Project WHERE C_Project_ID=:new.C_Project_ID AND IsActive='Y';
adrian@94
    67
 CURSOR Cur_MC IS
adrian@94
    68
  SELECT Value, Name
adrian@94
    69
  FROM C_Campaign WHERE C_Campaign_ID=:new.C_Campaign_ID AND IsActive='Y';
adrian@94
    70
 CURSOR Cur_AY IS
adrian@94
    71
  SELECT Value, Name
adrian@94
    72
  FROM C_Activity WHERE C_Activity_ID=:new.C_Activity_ID AND IsActive='Y';
adrian@94
    73
 CURSOR Cur_U1 IS
adrian@94
    74
  SELECT Value, Name
adrian@94
    75
  FROM C_ElementValue WHERE C_ElementValue_ID=:new.User1_ID AND IsActive = 'Y';
adrian@94
    76
 CURSOR Cur_U2 IS
adrian@94
    77
  SELECT Value, Name
adrian@94
    78
  FROM C_ElementValue WHERE C_ElementValue_ID=:new.User2_ID AND IsActive = 'Y';
adrian@94
    79
 --
adrian@94
    80
 v_change    BOOLEAN;
adrian@94
    81
BEGIN
adrian@94
    82
 --  Load C_AcctSchema Info (if not valid: 01403 no data found)
adrian@94
    83
 SELECT Separator
adrian@94
    84
   INTO v_Separator
adrian@94
    85
 FROM C_AcctSchema
adrian@94
    86
 WHERE C_AcctSchema_ID=:new.C_AcctSchema_ID;
adrian@94
    87
adrian@94
    88
 --  Loop for C_AcctSchema_Elements
adrian@94
    89
 FOR Cur_Elements IN (SELECT *
adrian@94
    90
               FROM C_AcctSchema_Element
adrian@94
    91
       WHERE C_AcctSchema_ID=:new.C_AcctSchema_ID
adrian@94
    92
       AND IsActive='Y'
adrian@94
    93
       ORDER BY SeqNo) LOOP
adrian@94
    94
  IF (Cur_Elements.ElementType = 'OO') THEN
adrian@94
    95
   OPEN Cur_OO;
adrian@94
    96
   FETCH Cur_OO INTO v_TempValue, v_TempName;
adrian@94
    97
   IF (Cur_OO%NOTFOUND) THEN -- mandatory
adrian@94
    98
    v_IsFullyQualified := 'N';
adrian@94
    99
   END IF;
adrian@94
   100
   CLOSE Cur_OO;
adrian@94
   101
  ELSIF (Cur_Elements.ElementType = 'AC') THEN
adrian@94
   102
   OPEN Cur_AC;
adrian@94
   103
   FETCH Cur_AC INTO v_TempValue, v_TempName;
adrian@94
   104
   IF (Cur_AC%NOTFOUND) THEN -- mandatory
adrian@94
   105
    v_IsFullyQualified := 'N';
adrian@94
   106
   ELSE
adrian@94
   107
    :new.Alias := v_TempValue; --Alias is the account no
adrian@94
   108
   END IF;
adrian@94
   109
   CLOSE Cur_AC;
adrian@94
   110
  ELSIF (Cur_Elements.ElementType = 'PR') THEN
adrian@94
   111
   OPEN Cur_PR;
adrian@94
   112
   FETCH Cur_PR INTO v_TempValue, v_TempName;
adrian@94
   113
   IF (Cur_PR%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   114
    v_IsFullyQualified := 'N';
adrian@94
   115
   END IF;
adrian@94
   116
   CLOSE Cur_PR;
adrian@94
   117
  ELSIF (Cur_Elements.ElementType = 'BP') THEN
adrian@94
   118
   OPEN Cur_BP;
adrian@94
   119
   FETCH Cur_BP INTO v_TempValue, v_TempName;
adrian@94
   120
   IF (Cur_BP%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   121
    v_IsFullyQualified := 'N';
adrian@94
   122
   END IF;
adrian@94
   123
   CLOSE Cur_BP;
adrian@94
   124
  ELSIF (Cur_Elements.ElementType = 'TO') THEN
adrian@94
   125
   OPEN Cur_TO;
adrian@94
   126
   FETCH Cur_TO INTO v_TempValue, v_TempName;
adrian@94
   127
   IF (Cur_TO%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   128
    v_IsFullyQualified := 'N';
adrian@94
   129
   END IF;
adrian@94
   130
   CLOSE Cur_TO;
adrian@94
   131
  ELSIF (Cur_Elements.ElementType = 'LF') THEN
adrian@94
   132
   OPEN Cur_LF;
adrian@94
   133
   FETCH Cur_LF INTO v_TempValue, v_TempName;
adrian@94
   134
   IF (Cur_LF%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   135
    v_IsFullyQualified := 'N';
adrian@94
   136
   END IF;
adrian@94
   137
   CLOSE Cur_LF;
adrian@94
   138
  ELSIF (Cur_Elements.ElementType = 'LT') THEN
adrian@94
   139
   OPEN Cur_LT;
adrian@94
   140
   FETCH Cur_LT INTO v_TempValue, v_TempName;
adrian@94
   141
   IF (Cur_LT%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   142
    v_IsFullyQualified := 'N';
adrian@94
   143
   END IF;
adrian@94
   144
   CLOSE Cur_LT;
adrian@94
   145
  ELSIF (Cur_Elements.ElementType = 'SR') THEN
adrian@94
   146
   OPEN Cur_SR;
adrian@94
   147
   FETCH Cur_SR INTO v_TempValue, v_TempName;
adrian@94
   148
   IF (Cur_SR%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   149
    v_IsFullyQualified := 'N';
adrian@94
   150
   END IF;
adrian@94
   151
   CLOSE Cur_SR;
adrian@94
   152
  ELSIF (Cur_Elements.ElementType = 'PJ') THEN
adrian@94
   153
   OPEN Cur_PJ;
adrian@94
   154
   FETCH Cur_PJ INTO v_TempValue, v_TempName;
adrian@94
   155
   IF (Cur_PJ%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   156
    v_IsFullyQualified := 'N';
adrian@94
   157
   END IF;
adrian@94
   158
   CLOSE Cur_PJ;
adrian@94
   159
  ELSIF (Cur_Elements.ElementType = 'MC') THEN
adrian@94
   160
   OPEN Cur_MC;
adrian@94
   161
   FETCH Cur_MC INTO v_TempValue, v_TempName;
adrian@94
   162
   IF (Cur_MC%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   163
    v_IsFullyQualified := 'N';
adrian@94
   164
   END IF;
adrian@94
   165
   CLOSE Cur_MC;
adrian@94
   166
  ELSIF (Cur_Elements.ElementType = 'AY') THEN
adrian@94
   167
   OPEN Cur_AY;
adrian@94
   168
   FETCH Cur_AY INTO v_TempValue, v_TempName;
adrian@94
   169
   IF (Cur_AY%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   170
    v_IsFullyQualified := 'N';
adrian@94
   171
   END IF;
adrian@94
   172
   CLOSE Cur_AY;
adrian@94
   173
  ELSIF (Cur_Elements.ElementType = 'U1') THEN
adrian@94
   174
   OPEN Cur_U1;
adrian@94
   175
   FETCH Cur_U1 INTO v_TempValue, v_TempName;
adrian@94
   176
   IF (Cur_U1%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   177
    v_IsFullyQualified := 'N';
adrian@94
   178
   END IF;
adrian@94
   179
   CLOSE Cur_U1;
adrian@94
   180
  ELSIF (Cur_Elements.ElementType = 'U2') THEN
adrian@94
   181
   OPEN Cur_U2;
adrian@94
   182
   FETCH Cur_U2 INTO v_TempValue, v_TempName;
adrian@94
   183
   IF (Cur_U2%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   184
    v_IsFullyQualified := 'N';
adrian@94
   185
   END IF;
adrian@94
   186
   CLOSE Cur_U2;
adrian@94
   187
  END IF;
adrian@94
   188
adrian@94
   189
  -- Construct Info Fields
adrian@94
   190
  v_Combination := COALESCE(v_Combination,'') || COALESCE(v_TempValue,'') || COALESCE(v_Separator,'');
adrian@94
   191
  v_Description := COALESCE(v_Description,'') || COALESCE(v_TempName,'') || COALESCE(v_Separator,'');
adrian@94
   192
  v_TempValue := '_';
adrian@94
   193
  v_TempName := '_';
adrian@94
   194
 END LOOP;
adrian@94
   195
adrian@95
   196
 --  Fully Qualified
adrian@94
   197
 IF NOT DELETING THEN
adrian@94
   198
  IF (:new.IsFullyQualified='Y' AND v_IsFullyQualified='N') THEN
adrian@94
   199
   RAISE Not_Fully_Qualified;
adrian@94
   200
  END IF;
adrian@94
   201
 END IF;
adrian@94
   202
adrian@94
   203
 --  Finish
adrian@94
   204
 :new.Combination := SUBSTR(v_Combination,1,LENGTH(v_Combination)-1);
adrian@94
   205
 :new.Description := SUBSTR(v_Description,1,LENGTH(v_Description)-1);
adrian@94
   206
 :new.IsFullyQualified := v_IsFullyQualified;
adrian@94
   207
 IF NOT INSERTING THEN
adrian@94
   208
  IF NOT (:new.Combination=:old.Combination AND :new.Description=:old.Description) THEN
adrian@94
   209
    DBMS_OUTPUT.PUT_LINE(:new.Combination || ' - ' || :new.Description);
adrian@94
   210
  END IF;
adrian@94
   211
 END IF;
adrian@94
   212
adrian@94
   213
EXCEPTION
adrian@94
   214
 WHEN Not_Fully_Qualified THEN
antonio@735
   215
  RAISE_APPLICATION_ERROR(-20101, 'Not fully Qualified: ' || v_Combination || '(' || :new.C_ValidCombination_ID || ')');
adrian@94
   216
antonio@735
   217
END C_VALIDCOMBINATION_TRG
gorkaion@239
   218
]]></body>
adrian@94
   219
    </trigger>
adrian@94
   220
  </database>