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