src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 756 ae11e4610537
child 1605 8a0fe0193bef
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER C_INVOICELINE_TRG2">
adrian@94
     3
    <trigger name="C_INVOICELINE_TRG2" table="C_INVOICELINE" fires="after" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
asier@799
     5
/*************************************************************************
asier@799
     6
* The contents of this file are subject to the Openbravo  Public  License
asier@799
     7
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
asier@799
     8
* Version 1.1  with a permitted attribution clause; you may not  use this
asier@799
     9
* file except in compliance with the License. You  may  obtain  a copy of
asier@799
    10
* the License at http://www.openbravo.com/legal/license.html
asier@799
    11
* Software distributed under the License  is  distributed  on  an "AS IS"
asier@799
    12
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
asier@799
    13
* License for the specific  language  governing  rights  and  limitations
asier@799
    14
* under the License.
asier@799
    15
* The Original Code is Openbravo ERP.
asier@799
    16
* The Initial Developer of the Original Code is Openbravo SL
asier@799
    17
* All portions are Copyright (C) 2001-2008 Openbravo SL
asier@799
    18
* All Rights Reserved.
asier@799
    19
* Contributor(s):  ______________________________________.
asier@799
    20
************************************************************************/
gorkaion@239
    21
adrian@170
    22
adrian@94
    23
adrian@94
    24
adrian@94
    25
adrian@94
    26
   TYPE RECORD IS REF CURSOR;
adrian@94
    27
   Cur_Offer RECORD;
adrian@94
    28
adrian@94
    29
  v_InvoiceLine_Offer_Id NUMBER;
antonio@735
    30
  v_lineno NUMBER:=0;
adrian@94
    31
  v_Price NUMBER;
adrian@94
    32
  v_PriceNew NUMBER;
adrian@94
    33
  v_Amt NUMBER;
adrian@94
    34
  v_BPartner_ID NUMBER;
adrian@94
    35
  v_Order_ID NUMBER;
adrian@94
    36
  v_Insert BOOLEAN :=FALSE;
adrian@94
    37
  v_DateInvoiced DATE;
adrian@94
    38
  v_Precision NUMBER;
adrian@94
    39
  v_PriceList_ID NUMBER;
adrian@94
    40
BEGIN
adrian@94
    41
-- This trigger is used for calculate the applied offers f0r the invoice
adrian@94
    42
adrian@94
    43
adrian@94
    44
adrian@94
    45
 IF DELETING THEN
adrian@94
    46
   DELETE FROM C_INVOICELINE_OFFER
adrian@94
    47
   WHERE C_InvoiceLine_ID = :OLD.C_InvoiceLine_ID;
adrian@94
    48
 END IF;
adrian@94
    49
adrian@94
    50
 IF UPDATING THEN
adrian@94
    51
  IF (    (COALESCE(:NEW.M_Product_ID,0) != COALESCE(:OLD.M_Product_ID,0))
adrian@94
    52
    OR (COALESCE(:NEW.PriceActual,0) != COALESCE(:OLD.PriceActual,0))
adrian@94
    53
    OR (COALESCE(:NEW.QtyInvoiced,0) != COALESCE(:OLD.QtyInvoiced,0))) THEN
adrian@94
    54
  DELETE FROM C_INVOICELINE_OFFER
adrian@94
    55
   WHERE C_InvoiceLine_ID = :NEW.C_InvoiceLine_ID;
adrian@94
    56
   v_Insert := TRUE;
adrian@94
    57
   END IF;
adrian@94
    58
 END IF;
adrian@94
    59
adrian@94
    60
 IF INSERTING OR v_Insert THEN
adrian@94
    61
   SELECT C_BPartner_ID, dateInvoiced, priceprecision, M_PriceList_ID
adrian@94
    62
    INTO v_BPartner_ID, v_DateInvoiced, v_Precision, v_PriceList_ID
adrian@94
    63
     FROM C_INVOICE i,
adrian@94
    64
          C_CURRENCY c
adrian@94
    65
   WHERE C_Invoice_ID = :NEW.C_Invoice_ID
adrian@94
    66
     AND i.C_Currency_ID = c.C_Currency_ID;
adrian@94
    67
adrian@94
    68
adrian@94
    69
   --Get the new "std" price from the Actual price (if actual price is the std price they will be equal)
adrian@94
    70
   v_Price := :NEW.PriceActual;
adrian@94
    71
    FOR Cur_Offer IN (SELECT M_OFFER_ID
adrian@94
    72
         FROM M_OFFER
gorkaion@280
    73
         WHERE v_DateInvoiced BETWEEN DATEFROM AND COALESCE(DATETO,TO_DATE('31-12-9999','DD-MM-YYYY'))
adrian@94
    74
   AND IsActive = 'Y'
gorkaion@239
    75
   AND (:NEW.QtyInvoiced >= COALESCE(Qty_From,0) OR Qty_From IS NULL)
gorkaion@239
    76
   AND (:NEW.QtyInvoiced <= COALESCE(Qty_To,0) OR Qty_To IS NULL)
adrian@94
    77
   AND ((PRICELIST_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
    78
           WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
    79
           AND M_PRICELIST_ID = v_PriceList_ID))
adrian@94
    80
     OR (PRICELIST_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
    81
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
    82
            AND M_PRICELIST_ID = v_PriceList_ID)))
adrian@94
    83
         AND ((BPARTNER_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
    84
           WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
    85
           AND C_BPARTNER_ID = v_BPartner_ID))
adrian@94
    86
         OR (BPARTNER_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
    87
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
    88
            AND C_BPARTNER_ID = v_BPartner_ID)))
adrian@94
    89
         AND ((BP_GROUP_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
    90
           WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
    91
           AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
    92
           AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID))
adrian@94
    93
         OR (BP_GROUP_SELECTION='N' AND EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
    94
           WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
    95
           AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
    96
           AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID)))
adrian@94
    97
         AND ((PRODUCT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
    98
           WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
    99
           AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID))
adrian@94
   100
         OR (PRODUCT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   101
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   102
            AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID)))
adrian@94
   103
         AND ((PROD_CAT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   104
           WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   105
           AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   106
           AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID))
adrian@94
   107
         OR (PROD_CAT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   108
           WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   109
           AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   110
           AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID)))
adrian@94
   111
         ORDER BY PRIORITY DESC, M_OFFER_ID) LOOP
adrian@94
   112
    v_Price := M_Get_Offer_Std_Price(Cur_Offer.M_Offer_ID, v_Price);
adrian@94
   113
   END LOOP;
adrian@94
   114
adrian@94
   115
   FOR Cur_Offer IN (SELECT M_OFFER_ID
adrian@94
   116
    FROM M_OFFER
gorkaion@280
   117
    WHERE v_DateInvoiced BETWEEN DATEFROM AND COALESCE(DATETO,TO_DATE('31-12-9999','DD-MM-YYYY'))
adrian@94
   118
 AND IsActive = 'Y'
gorkaion@239
   119
 AND (:NEW.QtyInvoiced >= COALESCE(Qty_From,0) OR Qty_From IS NULL)
gorkaion@239
   120
   AND (:NEW.QtyInvoiced <= COALESCE(Qty_To,0) OR Qty_To IS NULL)
adrian@94
   121
   AND ((PRICELIST_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
   122
           WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
   123
           AND M_PRICELIST_ID = v_PriceList_ID))
adrian@94
   124
     OR (PRICELIST_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
   125
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   126
            AND M_PRICELIST_ID = v_PriceList_ID)))
adrian@94
   127
    AND ((BPARTNER_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
   128
      WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
   129
      AND C_BPARTNER_ID = v_BPartner_ID))
adrian@94
   130
    OR (BPARTNER_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
   131
       WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   132
       AND C_BPARTNER_ID = v_BPartner_ID)))
adrian@94
   133
    AND ((BP_GROUP_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
   134
      WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   135
      AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
   136
      AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID))
adrian@94
   137
    OR (BP_GROUP_SELECTION='N' AND EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
   138
      WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   139
      AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
   140
      AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID)))
adrian@94
   141
    AND ((PRODUCT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   142
      WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   143
      AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID))
adrian@94
   144
    OR (PRODUCT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   145
       WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   146
       AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID)))
adrian@94
   147
    AND ((PROD_CAT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   148
      WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   149
      AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   150
      AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID))
adrian@94
   151
    OR (PROD_CAT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   152
      WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   153
      AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   154
      AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID)))
adrian@94
   155
    ORDER BY PRIORITY, M_OFFER_ID DESC) LOOP
adrian@94
   156
   Ad_Sequence_Next('C_InvoiceLine_Offer', :NEW.AD_Client_ID, v_InvoiceLine_Offer_ID);
adrian@94
   157
   v_lineno := v_lineno + 10;
adrian@94
   158
   v_PriceNew := M_Get_Offer_Price(Cur_Offer.M_Offer_ID, v_Price);
adrian@94
   159
   v_Amt := v_Price - v_PriceNew;
adrian@94
   160
adrian@94
   161
   INSERT INTO C_INVOICELINE_OFFER
adrian@94
   162
       (C_INVOICELINE_OFFER_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
adrian@94
   163
      CREATED, CREATEDBY, UPDATED, UPDATEDBY,
adrian@94
   164
      C_INVOICELINE_ID, LINE, M_OFFER_ID, PRICEOFFER, AMTOFFER)
adrian@94
   165
   VALUES
adrian@94
   166
       (v_InvoiceLine_Offer_ID, :NEW.AD_Client_ID, :NEW.AD_Org_ID, :NEW.IsActive,
antonio@735
   167
      now(),:NEW.CreatedBy, now(),:NEW.UpdatedBy,
adrian@94
   168
     :NEW.C_InvoiceLine_ID, v_lineno, Cur_Offer.M_Offer_Id, ROUND(v_Price,v_Precision), ROUND(v_Amt, v_Precision));
adrian@94
   169
    v_Price := v_PriceNew;
adrian@94
   170
  END LOOP;
adrian@94
   171
 END IF;
adrian@94
   172
antonio@735
   173
END C_INVOICELINE_TRG2
gorkaion@239
   174
]]></body>
adrian@94
   175
    </trigger>
adrian@94
   176
  </database>