src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
author Mikel Irurita <mikel.irurita@openbravo.com>
Thu, 12 Jul 2012 17:11:01 +0200
changeset 17454 745fd4a939d1
parent 17348 5671a6587aa6
child 18314 6113c22ee84c
permissions -rw-r--r--
Fixes issue 21053: C_INVOICELINE_TRG2: PIT logic not working
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
priya@9072
     7
* Version  1.1  (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.
ggi@6701
    16
* The Initial Developer of the Original Code is Openbravo SLU
ggi@6701
    17
* All portions are Copyright (C) 2001-2008 Openbravo SLU
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
juanpablo@1605
    29
  v_InvoiceLine_Offer_ID VARCHAR2(32);
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;
juanpablo@1605
    34
  v_BPartner_ID VARCHAR2(32);
juanpablo@1605
    35
  v_Order_ID VARCHAR2(32);
adrian@94
    36
  v_Insert BOOLEAN :=FALSE;
adrian@94
    37
  v_DateInvoiced DATE;
adrian@94
    38
  v_Precision NUMBER;
juanpablo@1605
    39
  v_PriceList_ID VARCHAR2(32);
eduardo@6017
    40
  v_Processed C_ORDER.PROCESSED%TYPE;
eduardo@6017
    41
  v_ID VARCHAR2(32);
harikrishnan@7567
    42
  v_Issotrx CHAR(1);
harikrishnan@7567
    43
  v_pricecancelAD CHAR(1);
eduardo@6017
    44
  v_oldLine NUMBER;
eduardo@6017
    45
  v_newLineNetAmt NUMBER;
eduardo@6017
    46
  v_newLineAlternate NUMBER;
eduardo@6017
    47
  v_taxAmt NUMBER;
eduardo@6017
    48
  v_Prec C_CURRENCY.STDPRECISION%TYPE;
harikrishnan@7567
    49
  v_BaseAmount C_TAX.BASEAMOUNT%TYPE;  
hari@17336
    50
  v_istaxincluded CHAR(1) ;
hari@17336
    51
  v_oldGrossAmt NUMBER:=0;
hari@17336
    52
  v_newGrossAmt NUMBER:=0;
hari@17336
    53
  v_totallines NUMBER:=0;
hari@17336
    54
  v_grandtotal NUMBER:=0;
hari@17336
    55
        
adrian@94
    56
BEGIN
asier@2084
    57
    
asier@2084
    58
    IF AD_isTriggerEnabled()='N' THEN RETURN;
asier@2078
    59
    END IF;
asier@2078
    60
asier@2078
    61
adrian@94
    62
-- This trigger is used for calculate the applied offers f0r the invoice
adrian@94
    63
eduardo@6017
    64
IF (DELETING) THEN
eduardo@6017
    65
  v_ID:=:OLD.C_INVOICE_ID;
eduardo@6017
    66
ELSE
eduardo@6017
    67
  v_ID:=:NEW.C_INVOICE_ID;
eduardo@6017
    68
END IF;
adrian@94
    69
adrian@94
    70
 IF UPDATING THEN
juanpablo@1605
    71
  IF (    (COALESCE(:NEW.M_Product_ID,'0') != COALESCE(:OLD.M_Product_ID,'0'))
adrian@94
    72
    OR (COALESCE(:NEW.PriceActual,0) != COALESCE(:OLD.PriceActual,0))
adrian@94
    73
    OR (COALESCE(:NEW.QtyInvoiced,0) != COALESCE(:OLD.QtyInvoiced,0))) THEN
adrian@94
    74
  DELETE FROM C_INVOICELINE_OFFER
adrian@94
    75
   WHERE C_InvoiceLine_ID = :NEW.C_InvoiceLine_ID;
adrian@94
    76
   v_Insert := TRUE;
adrian@94
    77
   END IF;
adrian@94
    78
 END IF;
mikel@17454
    79
 
adrian@94
    80
 IF INSERTING OR v_Insert THEN
hari@17336
    81
   SELECT C_BPartner_ID, dateInvoiced, priceprecision, i.M_PriceList_ID,p.istaxincluded
hari@17336
    82
    INTO v_BPartner_ID, v_DateInvoiced, v_Precision, v_PriceList_ID, v_istaxincluded
hari@17336
    83
     FROM C_INVOICE i
hari@17336
    84
             JOIN m_pricelist p ON i.m_pricelist_id = p.m_pricelist_id,
hari@17336
    85
adrian@94
    86
          C_CURRENCY c
hari@17336
    87
          
adrian@94
    88
   WHERE C_Invoice_ID = :NEW.C_Invoice_ID
adrian@94
    89
     AND i.C_Currency_ID = c.C_Currency_ID;
adrian@94
    90
hari@17336
    91
   IF (v_istaxincluded = 'Y') THEN
mikel@17454
    92
     IF (UPDATING) THEN
hari@17336
    93
        v_oldgrossamt := :old.line_gross_amount;
hari@17336
    94
        v_newgrossamt := :new.line_gross_amount;
hari@17336
    95
     ELSE
hari@17336
    96
        v_newgrossamt := :new.line_gross_amount;
hari@17336
    97
     END IF;
hari@17336
    98
   END IF;
adrian@94
    99
hari@17336
   100
 
adrian@94
   101
   --Get the new "std" price from the Actual price (if actual price is the std price they will be equal)
adrian@94
   102
   v_Price := :NEW.PriceActual;
adrian@94
   103
    FOR Cur_Offer IN (SELECT M_OFFER_ID
adrian@94
   104
         FROM M_OFFER
gorkaion@280
   105
         WHERE v_DateInvoiced BETWEEN DATEFROM AND COALESCE(DATETO,TO_DATE('31-12-9999','DD-MM-YYYY'))
adrian@94
   106
   AND IsActive = 'Y'
gorkaion@239
   107
   AND (:NEW.QtyInvoiced >= COALESCE(Qty_From,0) OR Qty_From IS NULL)
gorkaion@239
   108
   AND (:NEW.QtyInvoiced <= COALESCE(Qty_To,0) OR Qty_To IS NULL)
adrian@94
   109
   AND ((PRICELIST_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
   110
           WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
   111
           AND M_PRICELIST_ID = v_PriceList_ID))
adrian@94
   112
     OR (PRICELIST_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
   113
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   114
            AND M_PRICELIST_ID = v_PriceList_ID)))
adrian@94
   115
         AND ((BPARTNER_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
   116
           WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
   117
           AND C_BPARTNER_ID = v_BPartner_ID))
adrian@94
   118
         OR (BPARTNER_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
   119
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   120
            AND C_BPARTNER_ID = v_BPartner_ID)))
adrian@94
   121
         AND ((BP_GROUP_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
   122
           WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   123
           AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
   124
           AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID))
adrian@94
   125
         OR (BP_GROUP_SELECTION='N' AND EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
   126
           WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   127
           AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
   128
           AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID)))
adrian@94
   129
         AND ((PRODUCT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   130
           WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   131
           AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID))
adrian@94
   132
         OR (PRODUCT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   133
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   134
            AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID)))
adrian@94
   135
         AND ((PROD_CAT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   136
           WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   137
           AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   138
           AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID))
adrian@94
   139
         OR (PROD_CAT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   140
           WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   141
           AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   142
           AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID)))
adrian@94
   143
         ORDER BY PRIORITY DESC, M_OFFER_ID) LOOP
adrian@94
   144
    v_Price := M_Get_Offer_Std_Price(Cur_Offer.M_Offer_ID, v_Price);
adrian@94
   145
   END LOOP;
adrian@94
   146
adrian@94
   147
   FOR Cur_Offer IN (SELECT M_OFFER_ID
adrian@94
   148
    FROM M_OFFER
gorkaion@280
   149
    WHERE v_DateInvoiced BETWEEN DATEFROM AND COALESCE(DATETO,TO_DATE('31-12-9999','DD-MM-YYYY'))
adrian@94
   150
 AND IsActive = 'Y'
gorkaion@239
   151
 AND (:NEW.QtyInvoiced >= COALESCE(Qty_From,0) OR Qty_From IS NULL)
gorkaion@239
   152
   AND (:NEW.QtyInvoiced <= COALESCE(Qty_To,0) OR Qty_To IS NULL)
adrian@94
   153
   AND ((PRICELIST_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
   154
           WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
   155
           AND M_PRICELIST_ID = v_PriceList_ID))
adrian@94
   156
     OR (PRICELIST_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_PRICELIST
adrian@94
   157
            WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   158
            AND M_PRICELIST_ID = v_PriceList_ID)))
adrian@94
   159
    AND ((BPARTNER_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
   160
      WHERE M_OFFER_ID=M_OFFER.M_OFFER_ID
adrian@94
   161
      AND C_BPARTNER_ID = v_BPartner_ID))
adrian@94
   162
    OR (BPARTNER_SELECTION='N' AND EXISTS(SELECT 1 FROM M_OFFER_BPARTNER
adrian@94
   163
       WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   164
       AND C_BPARTNER_ID = v_BPartner_ID)))
adrian@94
   165
    AND ((BP_GROUP_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
   166
      WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   167
      AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
   168
      AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID))
adrian@94
   169
    OR (BP_GROUP_SELECTION='N' AND EXISTS (SELECT 1 FROM C_BPARTNER B, M_OFFER_BP_GROUP OB
adrian@94
   170
      WHERE OB.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   171
      AND B.C_BPARTNER_ID = v_BPartner_ID
adrian@94
   172
      AND OB.C_BP_GROUP_ID = B.C_BP_GROUP_ID)))
adrian@94
   173
    AND ((PRODUCT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   174
      WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   175
      AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID))
adrian@94
   176
    OR (PRODUCT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_OFFER_PRODUCT
adrian@94
   177
       WHERE M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   178
       AND M_PRODUCT_ID = :NEW.M_PRODUCT_ID)))
adrian@94
   179
    AND ((PROD_CAT_SELECTION='Y' AND NOT EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   180
      WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   181
      AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   182
      AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID))
adrian@94
   183
    OR (PROD_CAT_SELECTION='N' AND EXISTS (SELECT 1 FROM M_PRODUCT P, M_OFFER_PROD_CAT OP
adrian@94
   184
      WHERE OP.M_OFFER_ID = M_OFFER.M_OFFER_ID
adrian@94
   185
      AND P.M_PRODUCT_ID = :NEW.M_PRODUCT_ID
adrian@94
   186
      AND OP.M_PRODUCT_CATEGORY_ID = P.M_PRODUCT_CATEGORY_ID)))
adrian@94
   187
    ORDER BY PRIORITY, M_OFFER_ID DESC) LOOP
adrian@94
   188
   Ad_Sequence_Next('C_InvoiceLine_Offer', :NEW.AD_Client_ID, v_InvoiceLine_Offer_ID);
adrian@94
   189
   v_lineno := v_lineno + 10;
adrian@94
   190
   v_PriceNew := M_Get_Offer_Price(Cur_Offer.M_Offer_ID, v_Price);
adrian@94
   191
   v_Amt := v_Price - v_PriceNew;
harikrishnan@7567
   192
   v_pricecancelAD := 'N';
harikrishnan@7567
   193
	IF(:NEW.C_ORDERLINE_ID IS NOT NULL) THEN
harikrishnan@7579
   194
	  	SELECT o.issotrx,ol.CancelPriceAD into v_Issotrx,v_pricecancelAD FROM C_ORDER o,C_ORDERLINE ol WHERE ol.C_ORDERLINE_ID = :NEW.C_ORDERLINE_ID AND o.C_ORDER_ID = ol.C_ORDER_ID;
harikrishnan@7579
   195
		IF((v_Issotrx = 'Y' AND v_pricecancelAD <> 'Y') OR v_Issotrx = 'N') THEN
harikrishnan@7579
   196
		   INSERT INTO C_INVOICELINE_OFFER
harikrishnan@7579
   197
		       (C_INVOICELINE_OFFER_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
harikrishnan@7579
   198
		      CREATED, CREATEDBY, UPDATED, UPDATEDBY,
harikrishnan@7579
   199
		      C_INVOICELINE_ID, LINE, M_OFFER_ID, PRICEOFFER, AMTOFFER)
harikrishnan@7579
   200
		   VALUES
harikrishnan@7579
   201
		       (v_InvoiceLine_Offer_ID, :NEW.AD_Client_ID, :NEW.AD_Org_ID, :NEW.IsActive,
harikrishnan@7579
   202
		      now(),:NEW.CreatedBy, now(),:NEW.UpdatedBy,
harikrishnan@7579
   203
		     :NEW.C_InvoiceLine_ID, v_lineno, Cur_Offer.M_Offer_Id, ROUND(v_Price,v_Precision), ROUND(v_Amt, v_Precision));
harikrishnan@7579
   204
	     END IF;
harikrishnan@7579
   205
    ELSE
harikrishnan@7579
   206
    	INSERT INTO C_INVOICELINE_OFFER
adrian@94
   207
       (C_INVOICELINE_OFFER_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
adrian@94
   208
      CREATED, CREATEDBY, UPDATED, UPDATEDBY,
adrian@94
   209
      C_INVOICELINE_ID, LINE, M_OFFER_ID, PRICEOFFER, AMTOFFER)
harikrishnan@7579
   210
   	VALUES
adrian@94
   211
       (v_InvoiceLine_Offer_ID, :NEW.AD_Client_ID, :NEW.AD_Org_ID, :NEW.IsActive,
antonio@735
   212
      now(),:NEW.CreatedBy, now(),:NEW.UpdatedBy,
adrian@94
   213
     :NEW.C_InvoiceLine_ID, v_lineno, Cur_Offer.M_Offer_Id, ROUND(v_Price,v_Precision), ROUND(v_Amt, v_Precision));
harikrishnan@7579
   214
    END IF;
adrian@94
   215
    v_Price := v_PriceNew;
adrian@94
   216
  END LOOP;
adrian@94
   217
 END IF;
adrian@94
   218
eduardo@6017
   219
  /**************************************************************************
eduardo@6017
   220
   * Calculate Tax, etc.
eduardo@6017
   221
   */
mikel@17454
   222
 SELECT processed, stdPrecision, pl.istaxincluded
mikel@17454
   223
   INTO v_Processed, v_Prec, v_istaxincluded
mikel@17454
   224
 FROM C_Invoice, C_Currency, m_pricelist pl
eduardo@6017
   225
 WHERE C_Invoice.C_Currency_ID = C_Currency.C_Currency_ID
mikel@17454
   226
 AND C_Invoice.m_pricelist_id = pl.m_pricelist_id
eduardo@6017
   227
 AND C_Invoice_ID=v_ID;
eduardo@6017
   228
 v_oldLine:=0;
eduardo@6017
   229
 v_newLineNetAmt:=0;
eduardo@6017
   230
 IF(v_Processed='N') THEN
eduardo@6017
   231
  -- Calculate taxes
harikrishnan@7382
   232
  IF(UPDATING OR DELETING) THEN
eduardo@6017
   233
  v_oldLine:= COALESCE(:old.LineNetAmt,0);
eduardo@6017
   234
  END IF;  
eduardo@6017
   235
  IF (INSERTING OR UPDATING) THEN
eduardo@6017
   236
   DELETE FROM C_INVOICELINETAX WHERE C_INVOICELINE_ID = :new.C_InvoiceLine_ID;
eduardo@6017
   237
   v_newLineNetAmt := COALESCE(:new.LineNetAmt,0);
eduardo@6017
   238
   v_newLineAlternate := COALESCE(:new.TaxBaseAmt,0);
eduardo@6054
   239
   IF(:new.C_Tax_ID IS NOT NULL) THEN
eduardo@6054
   240
     SELECT BaseAmount INTO v_BaseAmount
eduardo@6054
   241
     FROM C_TAX
eduardo@6054
   242
     WHERE C_TAX_ID = :new.C_Tax_ID;
eduardo@6054
   243
     C_INVOICELINETAX_INSERT(:new.AD_Org_ID, :new.C_Invoice_ID, :new.C_InvoiceLine_ID, :new.UpdatedBy, :new.C_Tax_ID, :new.C_Tax_ID, v_newLineNetAmt, v_newLineAlternate, v_Prec);
hari@17336
   244
     IF (v_istaxincluded = 'Y') THEN
hari@17336
   245
        C_INVOICELINETAX_ROUNDING(:new.C_InvoiceLine_ID, :new.line_gross_amount, v_newLineNetAmt);
hari@17336
   246
     END IF;
hari@17336
   247
    
eduardo@6054
   248
   END IF;
eduardo@6017
   249
  -- Get Total Tax Amt
eduardo@6017
   250
   SELECT SUM(TaxAmt)
eduardo@6017
   251
     INTO v_taxAmt
eduardo@6017
   252
   FROM C_InvoiceTax
eduardo@6017
   253
   WHERE C_Invoice_ID=:new.C_Invoice_ID;
eduardo@6017
   254
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
eduardo@6017
   255
   -- Update Header
hari@17336
   256
       -- Get Total Tax Amt
eduardo@6017
   257
   UPDATE C_Invoice
eduardo@6017
   258
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
hari@17336
   259
     GrandTotal = CASE v_istaxincluded
hari@17336
   260
                     WHEN 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt
hari@17336
   261
                     ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
hari@17336
   262
                  END
eduardo@6017
   263
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
eduardo@6017
   264
   WHERE C_Invoice_ID = :new.C_Invoice_ID;
eduardo@6017
   265
  ELSE -- DELETING
mikel@17454
   266
   IF (v_istaxincluded = 'Y') THEN
mikel@17454
   267
     v_oldgrossamt := :old.line_gross_amount;
mikel@17454
   268
   END IF;
eduardo@6017
   269
   SELECT SUM(TaxAmt)
eduardo@6017
   270
     INTO v_taxAmt
eduardo@6017
   271
   FROM C_InvoiceTax
eduardo@6017
   272
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
eduardo@6017
   273
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
eduardo@6017
   274
   -- Update Header
eduardo@6017
   275
   UPDATE C_Invoice
eduardo@6017
   276
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
hari@17336
   277
     GrandTotal = CASE v_istaxincluded
hari@17336
   278
                       WHEN 'Y' THEN grandtotal - v_oldgrossamt
hari@17336
   279
                       ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
hari@17336
   280
                  END
eduardo@6017
   281
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
eduardo@6017
   282
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
eduardo@6017
   283
  END IF;
hari@17336
   284
  IF (v_istaxincluded = 'Y') THEN
hari@17336
   285
    SELECT totallines, grandtotal INTO v_totallines, v_grandtotal
gorkaion@17348
   286
    FROM C_invoice
hari@17336
   287
    WHERE c_invoice_id = v_id;
gorkaion@17348
   288
    C_INVOICETAX_ROUNDING(v_id, v_grandtotal, v_totallines);
hari@17336
   289
  END IF;
hari@17336
   290
  
eduardo@6017
   291
 END IF;
eduardo@6017
   292
eduardo@6017
   293
antonio@735
   294
END C_INVOICELINE_TRG2
gorkaion@239
   295
]]></body>
adrian@94
   296
    </trigger>
adrian@94
   297
  </database>