database/model/triggers/C_VALIDCOMBINATION_TRG.xml
author Adrián Romero <adrian.romero@openbravo.com>
Tue, 20 Nov 2007 16:27:14 +0000
changeset 94 4e7ba7b8a4ad
parent 59 85aab40b7d24
child 95 308b692fac65
permissions -rw-r--r--
fix: Changed check constraint names and foreign key names to fit in to
30 characteres.
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">
adrian@94
     4
      <body>
adrian@94
     5
adrian@94
     6
adrian@94
     7
adrian@94
     8
    /*************************************************************************
adrian@94
     9
    * The contents of this file are subject to the Openbravo  Public  License
adrian@94
    10
    * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
adrian@94
    11
    * Version 1.1  with a permitted attribution clause; you may not  use this
adrian@94
    12
    * file except in compliance with the License. You  may  obtain  a copy of
adrian@94
    13
    * the License at http://www.openbravo.com/legal/license.html
adrian@94
    14
    * Software distributed under the License  is  distributed  on  an "AS IS"
adrian@94
    15
    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
adrian@94
    16
    * License for the specific  language  governing  rights  and  limitations
adrian@94
    17
    * under the License.
adrian@94
    18
    * The Original Code is Openbravo ERP.
adrian@94
    19
    * The Initial Developer of the Original Code is Openbravo SL
adrian@94
    20
    * All portions are Copyright (C) 2001-2006 Openbravo SL
adrian@94
    21
    * All Rights Reserved.
adrian@94
    22
    * Contributor(s):  ______________________________________.
adrian@94
    23
    ************************************************************************/
adrian@94
    24
adrian@94
    25
   TYPE RECORD IS REF CURSOR;
adrian@94
    26
   Cur_Elements RECORD;
adrian@94
    27
adrian@94
    28
adrian@94
    29
 v_Combination      VARCHAR(255) := '';
adrian@94
    30
 v_Description      VARCHAR(255) := '';
adrian@94
    31
 v_IsFullyQualified CHAR(1) := 'Y';
adrian@94
    32
 v_Separator      CHAR(1);
adrian@94
    33
 v_TempValue   VARCHAR(255) := '_';
adrian@94
    34
 v_TempName   VARCHAR(255) := '_';
adrian@94
    35
adrian@94
    36
 Not_Fully_Qualified Exception;
adrian@94
    37
adrian@94
    38
 --  Individual Statements
adrian@94
    39
 CURSOR Cur_OO IS
adrian@94
    40
  SELECT Value, Name
adrian@94
    41
  FROM AD_Org WHERE AD_Org_ID=:new.AD_Org_ID AND IsActive='Y';
adrian@94
    42
 CURSOR Cur_AC IS
adrian@94
    43
  SELECT Value, Name
adrian@94
    44
  FROM C_ElementValue WHERE C_ElementValue_ID=:new.Account_ID AND IsActive = 'Y';
adrian@94
    45
 CURSOR Cur_PR IS
adrian@94
    46
  SELECT Value, Name
adrian@94
    47
  FROM M_Product WHERE M_Product_ID=:new.M_Product_ID AND IsActive='Y';
adrian@94
    48
 CURSOR Cur_BP IS
adrian@94
    49
  SELECT Value, Name
adrian@94
    50
  FROM C_BPartner WHERE C_BPartner_ID=:new.C_BPartner_ID AND IsActive = 'Y';
adrian@94
    51
 CURSOR Cur_TO IS
adrian@94
    52
  SELECT Value, Name
adrian@94
    53
  FROM AD_Org WHERE AD_Org_ID=:new.AD_OrgTrx_ID AND IsActive='Y';
adrian@94
    54
 CURSOR Cur_LF IS
adrian@94
    55
  SELECT Postal, City
adrian@94
    56
  FROM C_Location WHERE C_Location_ID=:new.C_LocFrom_ID AND IsActive='Y';
adrian@94
    57
 CURSOR Cur_LT IS
adrian@94
    58
  SELECT Postal, City
adrian@94
    59
  FROM C_Location WHERE C_Location_ID=:new.C_LocTo_ID AND IsActive='Y';
adrian@94
    60
 CURSOR Cur_SR IS
adrian@94
    61
  SELECT Value, Name
adrian@94
    62
  FROM C_SalesRegion WHERE C_SalesRegion_ID=:new.C_SalesRegion_ID AND IsActive='Y';
adrian@94
    63
 CURSOR Cur_PJ IS
adrian@94
    64
  SELECT Value, Name
adrian@94
    65
  FROM C_Project WHERE C_Project_ID=:new.C_Project_ID AND IsActive='Y';
adrian@94
    66
 CURSOR Cur_MC IS
adrian@94
    67
  SELECT Value, Name
adrian@94
    68
  FROM C_Campaign WHERE C_Campaign_ID=:new.C_Campaign_ID AND IsActive='Y';
adrian@94
    69
 CURSOR Cur_AY IS
adrian@94
    70
  SELECT Value, Name
adrian@94
    71
  FROM C_Activity WHERE C_Activity_ID=:new.C_Activity_ID AND IsActive='Y';
adrian@94
    72
 CURSOR Cur_U1 IS
adrian@94
    73
  SELECT Value, Name
adrian@94
    74
  FROM C_ElementValue WHERE C_ElementValue_ID=:new.User1_ID AND IsActive = 'Y';
adrian@94
    75
 CURSOR Cur_U2 IS
adrian@94
    76
  SELECT Value, Name
adrian@94
    77
  FROM C_ElementValue WHERE C_ElementValue_ID=:new.User2_ID AND IsActive = 'Y';
adrian@94
    78
 --
adrian@94
    79
 v_change    BOOLEAN;
adrian@94
    80
BEGIN
adrian@94
    81
 --  Load C_AcctSchema Info (if not valid: 01403 no data found)
adrian@94
    82
 SELECT Separator
adrian@94
    83
   INTO v_Separator
adrian@94
    84
 FROM C_AcctSchema
adrian@94
    85
 WHERE C_AcctSchema_ID=:new.C_AcctSchema_ID;
adrian@94
    86
adrian@94
    87
 --  Loop for C_AcctSchema_Elements
adrian@94
    88
 FOR Cur_Elements IN (SELECT *
adrian@94
    89
               FROM C_AcctSchema_Element
adrian@94
    90
       WHERE C_AcctSchema_ID=:new.C_AcctSchema_ID
adrian@94
    91
       AND IsActive='Y'
adrian@94
    92
       ORDER BY SeqNo) LOOP
adrian@94
    93
  IF (Cur_Elements.ElementType = 'OO') THEN
adrian@94
    94
   OPEN Cur_OO;
adrian@94
    95
   FETCH Cur_OO INTO v_TempValue, v_TempName;
adrian@94
    96
   IF (Cur_OO%NOTFOUND) THEN -- mandatory
adrian@94
    97
    v_IsFullyQualified := 'N';
adrian@94
    98
   END IF;
adrian@94
    99
   CLOSE Cur_OO;
adrian@94
   100
  ELSIF (Cur_Elements.ElementType = 'AC') THEN
adrian@94
   101
   OPEN Cur_AC;
adrian@94
   102
   FETCH Cur_AC INTO v_TempValue, v_TempName;
adrian@94
   103
   IF (Cur_AC%NOTFOUND) THEN -- mandatory
adrian@94
   104
    v_IsFullyQualified := 'N';
adrian@94
   105
   ELSE
adrian@94
   106
    :new.Alias := v_TempValue; --Alias is the account no
adrian@94
   107
   END IF;
adrian@94
   108
   CLOSE Cur_AC;
adrian@94
   109
  ELSIF (Cur_Elements.ElementType = 'PR') THEN
adrian@94
   110
   OPEN Cur_PR;
adrian@94
   111
   FETCH Cur_PR INTO v_TempValue, v_TempName;
adrian@94
   112
   IF (Cur_PR%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   113
    v_IsFullyQualified := 'N';
adrian@94
   114
   END IF;
adrian@94
   115
   CLOSE Cur_PR;
adrian@94
   116
  ELSIF (Cur_Elements.ElementType = 'BP') THEN
adrian@94
   117
   OPEN Cur_BP;
adrian@94
   118
   FETCH Cur_BP INTO v_TempValue, v_TempName;
adrian@94
   119
   IF (Cur_BP%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   120
    v_IsFullyQualified := 'N';
adrian@94
   121
   END IF;
adrian@94
   122
   CLOSE Cur_BP;
adrian@94
   123
  ELSIF (Cur_Elements.ElementType = 'TO') THEN
adrian@94
   124
   OPEN Cur_TO;
adrian@94
   125
   FETCH Cur_TO INTO v_TempValue, v_TempName;
adrian@94
   126
   IF (Cur_TO%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   127
    v_IsFullyQualified := 'N';
adrian@94
   128
   END IF;
adrian@94
   129
   CLOSE Cur_TO;
adrian@94
   130
  ELSIF (Cur_Elements.ElementType = 'LF') THEN
adrian@94
   131
   OPEN Cur_LF;
adrian@94
   132
   FETCH Cur_LF INTO v_TempValue, v_TempName;
adrian@94
   133
   IF (Cur_LF%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   134
    v_IsFullyQualified := 'N';
adrian@94
   135
   END IF;
adrian@94
   136
   CLOSE Cur_LF;
adrian@94
   137
  ELSIF (Cur_Elements.ElementType = 'LT') THEN
adrian@94
   138
   OPEN Cur_LT;
adrian@94
   139
   FETCH Cur_LT INTO v_TempValue, v_TempName;
adrian@94
   140
   IF (Cur_LT%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   141
    v_IsFullyQualified := 'N';
adrian@94
   142
   END IF;
adrian@94
   143
   CLOSE Cur_LT;
adrian@94
   144
  ELSIF (Cur_Elements.ElementType = 'SR') THEN
adrian@94
   145
   OPEN Cur_SR;
adrian@94
   146
   FETCH Cur_SR INTO v_TempValue, v_TempName;
adrian@94
   147
   IF (Cur_SR%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   148
    v_IsFullyQualified := 'N';
adrian@94
   149
   END IF;
adrian@94
   150
   CLOSE Cur_SR;
adrian@94
   151
  ELSIF (Cur_Elements.ElementType = 'PJ') THEN
adrian@94
   152
   OPEN Cur_PJ;
adrian@94
   153
   FETCH Cur_PJ INTO v_TempValue, v_TempName;
adrian@94
   154
   IF (Cur_PJ%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   155
    v_IsFullyQualified := 'N';
adrian@94
   156
   END IF;
adrian@94
   157
   CLOSE Cur_PJ;
adrian@94
   158
  ELSIF (Cur_Elements.ElementType = 'MC') THEN
adrian@94
   159
   OPEN Cur_MC;
adrian@94
   160
   FETCH Cur_MC INTO v_TempValue, v_TempName;
adrian@94
   161
   IF (Cur_MC%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   162
    v_IsFullyQualified := 'N';
adrian@94
   163
   END IF;
adrian@94
   164
   CLOSE Cur_MC;
adrian@94
   165
  ELSIF (Cur_Elements.ElementType = 'AY') THEN
adrian@94
   166
   OPEN Cur_AY;
adrian@94
   167
   FETCH Cur_AY INTO v_TempValue, v_TempName;
adrian@94
   168
   IF (Cur_AY%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   169
    v_IsFullyQualified := 'N';
adrian@94
   170
   END IF;
adrian@94
   171
   CLOSE Cur_AY;
adrian@94
   172
  ELSIF (Cur_Elements.ElementType = 'U1') THEN
adrian@94
   173
   OPEN Cur_U1;
adrian@94
   174
   FETCH Cur_U1 INTO v_TempValue, v_TempName;
adrian@94
   175
   IF (Cur_U1%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   176
    v_IsFullyQualified := 'N';
adrian@94
   177
   END IF;
adrian@94
   178
   CLOSE Cur_U1;
adrian@94
   179
  ELSIF (Cur_Elements.ElementType = 'U2') THEN
adrian@94
   180
   OPEN Cur_U2;
adrian@94
   181
   FETCH Cur_U2 INTO v_TempValue, v_TempName;
adrian@94
   182
   IF (Cur_U2%NOTFOUND AND Cur_Elements.IsMandatory='Y') THEN
adrian@94
   183
    v_IsFullyQualified := 'N';
adrian@94
   184
   END IF;
adrian@94
   185
   CLOSE Cur_U2;
adrian@94
   186
  END IF;
adrian@94
   187
adrian@94
   188
  -- Construct Info Fields
adrian@94
   189
  v_Combination := COALESCE(v_Combination,'') || COALESCE(v_TempValue,'') || COALESCE(v_Separator,'');
adrian@94
   190
  v_Description := COALESCE(v_Description,'') || COALESCE(v_TempName,'') || COALESCE(v_Separator,'');
adrian@94
   191
  v_TempValue := '_';
adrian@94
   192
  v_TempName := '_';
adrian@94
   193
 END LOOP;
adrian@94
   194
adrian@94
   195
 --  Fully Qualified?
adrian@94
   196
 IF NOT DELETING THEN
adrian@94
   197
  IF (:new.IsFullyQualified='Y' AND v_IsFullyQualified='N') THEN
adrian@94
   198
   RAISE Not_Fully_Qualified;
adrian@94
   199
  END IF;
adrian@94
   200
 END IF;
adrian@94
   201
adrian@94
   202
 --  Finish
adrian@94
   203
 :new.Combination := SUBSTR(v_Combination,1,LENGTH(v_Combination)-1);
adrian@94
   204
 :new.Description := SUBSTR(v_Description,1,LENGTH(v_Description)-1);
adrian@94
   205
 :new.IsFullyQualified := v_IsFullyQualified;
adrian@94
   206
 IF NOT INSERTING THEN
adrian@94
   207
  IF NOT (:new.Combination=:old.Combination AND :new.Description=:old.Description) THEN
adrian@94
   208
    DBMS_OUTPUT.PUT_LINE(:new.Combination || ' - ' || :new.Description);
adrian@94
   209
  END IF;
adrian@94
   210
 END IF;
adrian@94
   211
adrian@94
   212
EXCEPTION
adrian@94
   213
 WHEN Not_Fully_Qualified THEN
adrian@94
   214
  RAISE_APPLICATION_ERROR (-20101, 'Not fully Qualified: ' || v_Combination || '(' || :new.C_ValidCombination_ID || ')');
adrian@94
   215
adrian@94
   216
END C_ValidCombination_Trg 
adrian@94
   217
</body>
adrian@94
   218
    </trigger>
adrian@94
   219
  </database>