src-db/database/model/triggers/C_DEBT_PAYMENT_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Mon, 28 Apr 2008 09:28:08 +0000
changeset 785 8dba91261590
parent 756 ae11e4610537
child 1605 8a0fe0193bef
permissions -rw-r--r--
Fix last update of copyright years in database files
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER C_DEBT_PAYMENT_TRG">
adrian@94
     3
    <trigger name="C_DEBT_PAYMENT_TRG" table="C_DEBT_PAYMENT" fires="before" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
gorkaion@239
     5
adrian@170
     6
adrian@94
     7
carlos@59
     8
carlos@0
     9
    /*************************************************************************
carlos@0
    10
    * The contents of this file are subject to the Openbravo  Public  License
carlos@0
    11
    * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    12
    * Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    13
    * file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    14
    * the License at http://www.openbravo.com/legal/license.html
carlos@0
    15
    * Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    16
    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    17
    * License for the specific  language  governing  rights  and  limitations
carlos@0
    18
    * under the License.
carlos@0
    19
    * The Original Code is Openbravo ERP.
carlos@0
    20
    * The Initial Developer of the Original Code is Openbravo SL
carlos@0
    21
    * All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    22
    * All Rights Reserved.
carlos@0
    23
    * Contributor(s):  ______________________________________.
carlos@0
    24
    ************************************************************************/
carlos@0
    25
carlos@0
    26
 v_IsProcessed CHAR(1);
antonio@735
    27
 v_Canceled NUMBER:=0;
antonio@735
    28
 v_Generated NUMBER:=0;
antonio@735
    29
 v_Applied NUMBER:=0;
carlos@0
    30
 v_Settlement_ID NUMBER(10);
carlos@0
    31
 v_Currency_ID NUMBER(10);
carlos@0
    32
 v_S_Currency_ID NUMBER(10);
carlos@0
    33
 v_S_Date  DATE;
carlos@0
    34
 v_Client_ID NUMBER(10);
carlos@0
    35
 v_Org_ID NUMBER(10);
antonio@735
    36
 v_multiplier NUMBER:=1;
antonio@735
    37
 v_Oldmultiplier NUMBER:=1;
carlos@0
    38
 v_Processed CHAR(1);
carlos@0
    39
 v_Aux NUMBER;
carlos@0
    40
BEGIN
carlos@0
    41
  /* Checking constraints */
carlos@0
    42
  IF (DELETING) THEN
carlos@0
    43
    IF (:old.isValid = 'Y') THEN
carlos@0
    44
      SELECT COALESCE(MAX(Processed),'Y') INTO v_Processed
carlos@0
    45
      FROM C_Invoice
carlos@0
    46
      WHERE C_Invoice_ID = :old.C_Invoice_ID;
carlos@0
    47
      --dbms_output.put_line('inv:'||coalesce(:old.C_Invoice_ID,'null') ||' ord:'|| coalesce(:old.C_Order_ID,'null'));
carlos@0
    48
      IF (v_Processed = 'Y' AND NOT (:old.C_Invoice_ID IS NULL AND :old.C_Order_ID IS NOT NULL)) THEN
carlos@0
    49
        RAISE_APPLICATION_ERROR(-20513, 'A valid Debt/Payment should not be deleted');
carlos@0
    50
      END IF;
carlos@0
    51
    END IF;
carlos@0
    52
  ELSIF (INSERTING) THEN
carlos@0
    53
    IF (:new.C_Settlement_Generate_ID IS NOT NULL) THEN
carlos@0
    54
      SELECT processed INTO v_IsProcessed
carlos@0
    55
       FROM C_Settlement
carlos@0
    56
       WHERE C_Settlement_ID =:new.C_Settlement_Generate_ID;
carlos@0
    57
      IF v_IsProcessed='Y' THEN
carlos@0
    58
        RAISE_APPLICATION_ERROR(-20510, 'The settlement is processed');
carlos@0
    59
      END IF;
carlos@0
    60
    END IF;
antonio@737
    61
carlos@0
    62
  IF (COALESCE(:NEW.IsAutomaticGenerated,'Y')='N' AND :NEW.C_Order_ID IS NOT NULL) THEN
carlos@0
    63
      -- Check if it is totally invoiced
antonio@737
    64
      SELECT COALESCE(SUM(NOTINVOICED),0)
carlos@0
    65
        INTO v_Aux
carlos@0
    66
        FROM (SELECT (O.QTYORDERED- COALESCE(LL.QTYINVOICED,0)) AS NOTINVOICED
carlos@0
    67
                FROM C_ORDERLINE O LEFT JOIN (SELECT IL.C_INVOICELINE_ID, IL.QTYINVOICED, IL.C_ORDERLINE_ID
carlos@0
    68
                                                FROM C_INVOICELINE IL,
carlos@0
    69
                                                     C_INVOICE I
carlos@0
    70
                                                WHERE IL.C_INVOICE_ID = I.C_INVOICE_ID
carlos@0
    71
                                                  AND I.PROCESSED='Y') LL
carlos@0
    72
                                                  ON LL.C_ORDERLINE_ID = O.C_ORDERLINE_ID
carlos@0
    73
              WHERE O.C_ORDER_ID = :NEW.C_Order_ID) AAA;
carlos@0
    74
        IF v_Aux = 0 THEN
carlos@0
    75
          select count(*)
carlos@0
    76
            into v_Aux
carlos@0
    77
            from c_orderline
carlos@0
    78
           where c_order_id = :NEW.C_Order_ID;
gorkaion@239
    79
           if v_Aux>0 then
antonio@735
    80
             RAISE_APPLICATION_ERROR(-20000, '@OrderCompletelyInvoiced@');
carlos@0
    81
           end if;
carlos@0
    82
        END IF;
antonio@737
    83
carlos@0
    84
      -- If Order is processed, DP should be valid
carlos@0
    85
      SELECT PROCESSED
carlos@0
    86
       INTO v_IsProcessed
carlos@0
    87
        FROM C_ORDER
carlos@0
    88
      WHERE C_ORDER_ID = :NEW.C_Order_ID;
carlos@0
    89
      IF v_IsProcessed = 'Y' THEN
carlos@0
    90
         :NEW.IsValid:='Y';
carlos@0
    91
      END IF;
carlos@0
    92
    END IF;
antonio@737
    93
carlos@0
    94
  IF (COALESCE(:new.IsAutomaticGenerated,'Y')='N' AND :new.C_Invoice_ID IS NOT NULL) THEN
carlos@0
    95
      SELECT Processed
carlos@0
    96
     INTO v_IsProcessed
carlos@0
    97
     FROM C_Invoice
carlos@0
    98
    WHERE C_Invoice_ID = :new.C_Invoice_ID;
carlos@0
    99
    IF v_IsProcessed = 'Y' THEN
carlos@0
   100
      RAISE_APPLICATION_ERROR(-20508, 'The invoice is processed');
carlos@0
   101
    END IF;
carlos@0
   102
    END IF;
carlos@0
   103
    :new.Status := :new.Status_Initial;
carlos@0
   104
  ELSIF (UPDATING) THEN
gorkaion@239
   105
    IF (:old.IsValid = 'Y' AND ((:old.IsActive <> :new.IsActive)
gorkaion@239
   106
                   OR (:old.IsReceipt<>:new.IsReceipt)
gorkaion@239
   107
               OR (COALESCE(:old.C_Settlement_Generate_ID,0)<>COALESCE(:new.C_Settlement_Generate_ID,0))
gorkaion@239
   108
               -- OR (COALESCE(:old.C_Invoice_ID,0)<>COALESCE(:new.C_Invoice_ID,0))
gorkaion@239
   109
                OR (COALESCE(:old.C_BPartner_ID,0)<>COALESCE(:new.C_BPartner_ID,0))
gorkaion@239
   110
               OR (:old.C_Currency_ID<>:new.C_Currency_ID)
gorkaion@239
   111
               OR (:old.Amount<>:new.Amount)
gorkaion@239
   112
               OR (:old.IsManual<>:new.IsManual))) THEN
carlos@0
   113
      RAISE_APPLICATION_ERROR(-20501, 'A valid Debt/Payment should not be modified');
carlos@0
   114
    END IF;
carlos@0
   115
    IF (C_DEBT_PAYMENT_STATUS(:old.C_Settlement_Cancel_ID,:old.Cancel_Processed,:old.Generate_Processed,
carlos@0
   116
        :old.IsPaid,:old.IsValid,:old.C_CashLine_ID,:old.C_BankStatementLine_ID) NOT IN ('P','I')
gorkaion@239
   117
     AND ((COALESCE(:old.C_BankAccount_ID,0)<>COALESCE(:new.C_BankAccount_ID,0)) OR
gorkaion@239
   118
       (COALESCE(:old.C_CashBook_ID,0)<>COALESCE(:new.C_CashBook_ID,0)) OR
gorkaion@239
   119
        (:old.PaymentRule<>:new.PaymentRule) OR (:old.DatePlanned<>:new.DatePlanned))) THEN
carlos@0
   120
      RAISE_APPLICATION_ERROR(-20511, 'This Debt/Payment can not be modified');
carlos@0
   121
    END IF;
carlos@0
   122
    IF (:new.C_Settlement_Cancel_ID IS NOT NULL) THEN
gorkaion@239
   123
     IF (:old.C_Settlement_Cancel_ID<>:new.C_Settlement_Cancel_ID) THEN
carlos@0
   124
        RAISE_APPLICATION_ERROR(-20512, 'The Debt/Payment is already in a Settlement Cancel');
carlos@0
   125
     END IF;
gorkaion@239
   126
     IF ((COALESCE(:old.C_Settlement_Cancel_ID,0)<>COALESCE(:new.C_Settlement_Cancel_ID,0))
gorkaion@239
   127
      OR (:old.IsPaid<>:new.IsPaid)
gorkaion@239
   128
      OR (:old.WriteOffAmt<>:new.WriteOffAmt)) THEN
carlos@0
   129
          SELECT processed INTO v_IsProcessed FROM C_Settlement
carlos@0
   130
          WHERE C_Settlement_ID =:new.C_Settlement_Cancel_ID;
carlos@0
   131
carlos@0
   132
          IF v_IsProcessed='Y' THEN
carlos@0
   133
           RAISE_APPLICATION_ERROR(-20510, 'The settlement is processed');
carlos@0
   134
          END IF;
carlos@0
   135
     END IF;
carlos@0
   136
    END IF;
carlos@0
   137
    IF (:new.C_Settlement_Generate_ID IS NOT NULL) THEN
gorkaion@239
   138
      IF ((:old.C_GLItem_ID<>:new.C_GLItem_ID)
gorkaion@239
   139
        OR (:old.IsDirectPosting<>:new.IsDirectPosting)) THEN
carlos@0
   140
        SELECT processed INTO v_IsProcessed FROM C_Settlement
carlos@0
   141
          WHERE C_Settlement_ID =:new.C_Settlement_Generate_ID;
carlos@0
   142
          IF v_IsProcessed='Y' THEN
carlos@0
   143
           RAISE_APPLICATION_ERROR(-20510, 'The settlement is processed');
carlos@0
   144
          END IF;
carlos@0
   145
      END IF;
carlos@0
   146
    END IF;
carlos@0
   147
  END IF;
carlos@0
   148
carlos@0
   149
carlos@0
   150
  /**
carlos@0
   151
  * Calculate amounts for Settlements
carlos@0
   152
  */
carlos@0
   153
  IF (DELETING) THEN
carlos@0
   154
    IF (:old.C_Settlement_Generate_ID IS NOT NULL AND :old.Generate_Processed = 'N') THEN
carlos@0
   155
   IF (:old.IsReceipt = 'N') THEN
carlos@0
   156
     v_multiplier := -1;
carlos@0
   157
  END IF;
carlos@0
   158
      v_Generated := (- COALESCE(:old.Amount, 0))*v_multiplier;
carlos@0
   159
  IF (:old.IsPaid = 'Y') THEN
carlos@0
   160
     v_Applied := (COALESCE(:old.Amount, 0) - COALESCE(:old.WriteOffAmt, 0))*v_multiplier;
carlos@0
   161
  END IF;
carlos@0
   162
  v_Settlement_ID := :old.C_Settlement_Generate_ID;
carlos@0
   163
  v_Currency_ID := :old.C_Currency_ID;
carlos@0
   164
    END IF;
carlos@0
   165
  END IF;
carlos@0
   166
  IF (UPDATING) THEN
carlos@0
   167
    IF (:new.Ad_Org_Id!=:old.ad_Org_ID) and (:NEW.C_Invoice_ID is not null) THEN
carlos@0
   168
     SELECT Processed
carlos@0
   169
         INTO v_IsProcessed
carlos@0
   170
         FROM C_Invoice
carlos@0
   171
        WHERE C_Invoice_ID = :new.C_Invoice_ID;
carlos@0
   172
        IF v_IsProcessed = 'Y' THEN
carlos@0
   173
          RAISE_APPLICATION_ERROR(-20508, 'The invoice is processed');
carlos@0
   174
        END IF;
carlos@0
   175
    END IF;
carlos@0
   176
carlos@0
   177
     IF (:old.C_Settlement_Generate_ID IS NOT NULL AND :old.Generate_Processed = 'N'
carlos@0
   178
    AND :new.Generate_Processed = 'N')
carlos@0
   179
   THEN
carlos@0
   180
   IF (:old.IsReceipt = 'N') THEN
carlos@0
   181
    v_Oldmultiplier := -1;
carlos@0
   182
   END IF;
carlos@0
   183
  IF (:new.IsReceipt = 'N') THEN
carlos@0
   184
      v_multiplier := -1;
carlos@0
   185
  END IF;
carlos@0
   186
    v_Generated := v_Generated - (COALESCE(:old.Amount, 0)*v_Oldmultiplier);
carlos@0
   187
  IF (:old.IsPaid = 'Y') THEN
carlos@0
   188
      v_Applied := COALESCE(v_Applied, 0) - (COALESCE(:old.Amount, 0) - COALESCE(:old.WriteOffAmt, 0))*v_Oldmultiplier;
carlos@0
   189
  END IF;
carlos@0
   190
     v_Generated := v_Generated + (COALESCE(:new.Amount, 0)*v_multiplier);
carlos@0
   191
  IF (:new.IsPaid = 'Y') THEN
carlos@0
   192
      v_Applied := COALESCE(v_Applied,0) + (COALESCE(:new.Amount,0) - COALESCE(:new.WriteOffAmt,0))*v_multiplier;
carlos@0
   193
  END IF;
carlos@0
   194
  v_Settlement_ID := :new.C_Settlement_Generate_ID;
carlos@0
   195
  v_Currency_ID := :new.C_Currency_ID;
carlos@0
   196
    END IF;
carlos@0
   197
    IF (:old.C_Settlement_Cancel_ID IS NOT NULL AND :old.Cancel_Processed = 'N' AND :new.Cancel_Processed = 'N') THEN
carlos@0
   198
   IF (:old.IsReceipt = 'N') THEN
carlos@0
   199
    v_multiplier := -1;
carlos@0
   200
   END IF;
carlos@0
   201
   IF (:old.IsPaid = 'Y') THEN
carlos@0
   202
    v_Applied := COALESCE(v_Applied,0) - (COALESCE(:old.Amount,0) - COALESCE(:old.WriteOffAmt,0))*v_multiplier;
carlos@0
   203
   ELSE
carlos@0
   204
    v_Canceled := COALESCE(v_Canceled,0) - (COALESCE(:old.Amount,0) - COALESCE(:old.WriteOffAmt,0))*v_multiplier;
carlos@0
   205
   END IF;
carlos@0
   206
   v_Settlement_ID := :old.C_Settlement_Cancel_ID;
carlos@0
   207
   v_Currency_ID := :old.C_Currency_ID;
carlos@0
   208
  END IF;
carlos@0
   209
  IF (:new.C_Settlement_Cancel_ID IS NOT NULL AND :old.Cancel_Processed = 'N' AND :new.Cancel_Processed = 'N') THEN
carlos@0
   210
   IF (:new.IsReceipt = 'N') THEN
carlos@0
   211
    v_multiplier := -1;
carlos@0
   212
   END IF;
carlos@0
   213
   IF (:new.IsPaid = 'Y') THEN
carlos@0
   214
    v_Applied := COALESCE(v_Applied,0) + (COALESCE(:new.Amount,0) - COALESCE(:new.WriteOffAmt,0))*v_multiplier;
carlos@0
   215
   ELSE
carlos@0
   216
    v_Canceled := COALESCE(v_Canceled,0) + (COALESCE(:new.Amount,0) - COALESCE(:new.WriteOffAmt,0))*v_multiplier;
carlos@0
   217
   END IF;
carlos@0
   218
   v_Settlement_ID := :new.C_Settlement_Cancel_ID;
carlos@0
   219
   v_Currency_ID := :new.C_Currency_ID;
carlos@0
   220
  END IF;
carlos@0
   221
 END IF;
carlos@0
   222
  IF (INSERTING) THEN
carlos@0
   223
    IF (:new.C_Settlement_Generate_ID IS NOT NULL AND :new.Generate_Processed = 'N') THEN
carlos@0
   224
   IF (:new.IsReceipt = 'N') THEN
carlos@0
   225
    v_multiplier := -1;
carlos@0
   226
   END IF;
carlos@0
   227
   v_Generated := COALESCE(:new.Amount,0)*v_multiplier;
carlos@0
   228
   IF (:new.IsPaid = 'Y') THEN
carlos@0
   229
    v_Applied := COALESCE(v_Applied,0) + (COALESCE(:new.Amount,0) - COALESCE(:new.WriteOffAmt,0))*v_multiplier;
carlos@0
   230
   END IF;
carlos@0
   231
   v_Settlement_ID := :new.C_Settlement_Generate_ID;
carlos@0
   232
   v_Currency_ID := :new.C_Currency_ID;
carlos@0
   233
    END IF;
carlos@0
   234
  END IF;
carlos@0
   235
  IF (v_Settlement_ID IS NOT NULL) THEN
carlos@0
   236
    SELECT C_Currency_ID, DateTrx, AD_Client_ID, AD_Org_ID
carlos@0
   237
  INTO v_S_Currency_ID, v_S_Date, v_Client_ID, v_Org_ID
carlos@0
   238
  FROM C_Settlement WHERE C_Settlement_ID = v_Settlement_ID;
carlos@0
   239
gorkaion@239
   240
  IF (v_Currency_ID <> v_S_Currency_ID) THEN
carlos@0
   241
   v_Canceled :=C_Currency_Convert(v_Canceled, v_Currency_ID, v_S_Currency_ID, v_S_Date, null, v_Client_ID, v_Org_ID);
carlos@0
   242
   v_Generated := C_Currency_Convert(v_Generated, v_Currency_ID, v_S_Currency_ID, v_S_Date, null, v_Client_ID, v_Org_ID);
carlos@0
   243
   v_Applied :=C_Currency_Convert(v_Applied, v_Currency_ID, v_S_Currency_ID, v_S_Date, null, v_Client_ID, v_Org_ID);
carlos@0
   244
  END IF;
carlos@0
   245
  -- Update header
carlos@0
   246
    UPDATE C_Settlement
carlos@0
   247
    SET CanceledNotChargeAmt = COALESCE(CanceledNotChargeAmt, 0) + COALESCE(v_Canceled, 0),
carlos@0
   248
  GeneratedAmt = COALESCE(GeneratedAmt, 0) + COALESCE(v_Generated, 0),
carlos@0
   249
  ChargedAmt = COALESCE(ChargedAmt, 0) + COALESCE(v_Applied, 0)
carlos@0
   250
    WHERE C_Settlement_ID = v_Settlement_ID;
carlos@0
   251
 END IF;
antonio@735
   252
END C_DEBT_PAYMENT_TRG
gorkaion@239
   253
]]></body>
adrian@94
   254
    </trigger>
adrian@94
   255
  </database>