database/model/triggers/C_VALIDCOMBINATION_TRG.xml
author Adrián Romero <adrian.romero@openbravo.com>
Wed, 05 Dec 2007 20:43:45 +0000
changeset 170 4b5493776a7f
parent 132 33c5ca45fcff
child 239 a30acae847e1
permissions -rw-r--r--
Bug fixed: [ 1845110 ] Database model not in sync with database created
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@170
     4
      <body>
adrian@170
     5
adrian@94
     6
adrian@94
     7
adrian@94
     8
adrian@94
     9
    /*************************************************************************
adrian@94
    10
    * The contents of this file are subject to the Openbravo  Public  License
adrian@94
    11
    * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
adrian@94
    12
    * Version 1.1  with a permitted attribution clause; you may not  use this
adrian@94
    13
    * file except in compliance with the License. You  may  obtain  a copy of
adrian@94
    14
    * the License at http://www.openbravo.com/legal/license.html
adrian@94
    15
    * Software distributed under the License  is  distributed  on  an "AS IS"
adrian@94
    16
    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
adrian@94
    17
    * License for the specific  language  governing  rights  and  limitations
adrian@94
    18
    * under the License.
adrian@94
    19
    * The Original Code is Openbravo ERP.
adrian@94
    20
    * The Initial Developer of the Original Code is Openbravo SL
adrian@94
    21
    * All portions are Copyright (C) 2001-2006 Openbravo SL
adrian@94
    22
    * All Rights Reserved.
adrian@94
    23
    * Contributor(s):  ______________________________________.
adrian@94
    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
adrian@94
   215
  RAISE_APPLICATION_ERROR (-20101, 'Not fully Qualified: ' || v_Combination || '(' || :new.C_ValidCombination_ID || ')');
adrian@94
   216
adrian@94
   217
END C_ValidCombination_Trg 
adrian@170
   218
adrian@170
   219
</body>
adrian@94
   220
    </trigger>
adrian@94
   221
  </database>