src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
author Unai Martirena <unai.martirena@openbravo.com>
Wed, 11 Mar 2015 13:52:26 +0100
changeset 26214 35a4e54c3ffc
parent 25602 fbc674d56f04
child 26277 4cfbd5d699d2
permissions -rw-r--r--
Fixes bug 29073: InvoiceLine tax delete only is done when is needed

C_INVOICELINE_TRG2 has been changed to better manage when Invoice Line taxes are deleted and created again. Instead of doing it on every time an invoiceline is inserted or updated, it will be done when certain values in Invoice Line are changed and this affects in the related Invoice Line Tax record. The result will be the same, because with the previous code when there was no change in the Invoice Line the Invoice Line taxes were recreated with the same values as before, and now will be skipped.

Apart of this previous change, all not used variables have been removed, and in the same way a complete select sentence that was obtaining values that were not used later.
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
asier@18314
    17
* All portions are Copyright (C) 2001-2012 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
eduardo@6017
    25
  v_Processed C_ORDER.PROCESSED%TYPE;
eduardo@6017
    26
  v_ID VARCHAR2(32);
eduardo@6017
    27
  v_oldLine NUMBER;
eduardo@6017
    28
  v_newLineNetAmt NUMBER;
eduardo@6017
    29
  v_newLineAlternate NUMBER;
eduardo@6017
    30
  v_taxAmt NUMBER;
eduardo@6017
    31
  v_Prec C_CURRENCY.STDPRECISION%TYPE;
hari@17336
    32
  v_istaxincluded CHAR(1) ;
hari@17336
    33
  v_oldGrossAmt NUMBER:=0;
hari@17336
    34
  v_newGrossAmt NUMBER:=0;
hari@17336
    35
  v_totallines NUMBER:=0;
hari@17336
    36
  v_grandtotal NUMBER:=0;
unai@26214
    37
  v_oldLineAlternate NUMBER;
unai@26214
    38
  v_create CHAR(1):='Y';
hari@17336
    39
        
adrian@94
    40
BEGIN
asier@2084
    41
    
asier@2084
    42
    IF AD_isTriggerEnabled()='N' THEN RETURN;
asier@2078
    43
    END IF;
asier@2078
    44
asier@2078
    45
adrian@94
    46
-- This trigger is used for calculate the applied offers f0r the invoice
adrian@94
    47
eduardo@6017
    48
IF (DELETING) THEN
eduardo@6017
    49
  v_ID:=:OLD.C_INVOICE_ID;
eduardo@6017
    50
ELSE
eduardo@6017
    51
  v_ID:=:NEW.C_INVOICE_ID;
eduardo@6017
    52
END IF;
adrian@94
    53
eduardo@6017
    54
  /**************************************************************************
eduardo@6017
    55
   * Calculate Tax, etc.
eduardo@6017
    56
   */
mikel@17454
    57
 SELECT processed, stdPrecision, pl.istaxincluded
mikel@17454
    58
   INTO v_Processed, v_Prec, v_istaxincluded
mikel@17454
    59
 FROM C_Invoice, C_Currency, m_pricelist pl
eduardo@6017
    60
 WHERE C_Invoice.C_Currency_ID = C_Currency.C_Currency_ID
mikel@17454
    61
 AND C_Invoice.m_pricelist_id = pl.m_pricelist_id
eduardo@6017
    62
 AND C_Invoice_ID=v_ID;
eduardo@6017
    63
 v_oldLine:=0;
eduardo@6017
    64
 v_newLineNetAmt:=0;
eduardo@6017
    65
 IF(v_Processed='N') THEN
eduardo@6017
    66
  -- Calculate taxes
unai@26214
    67
  IF (v_istaxincluded = 'Y') THEN
unai@26214
    68
     IF (UPDATING) THEN
unai@26214
    69
        v_oldgrossamt := :old.line_gross_amount;
unai@26214
    70
        v_newgrossamt := :new.line_gross_amount;
unai@26214
    71
     ELSE
unai@26214
    72
        v_newgrossamt := :new.line_gross_amount;
unai@26214
    73
     END IF;
unai@26214
    74
   END IF;
harikrishnan@7382
    75
  IF(UPDATING OR DELETING) THEN
eduardo@6017
    76
  v_oldLine:= COALESCE(:old.LineNetAmt,0);
eduardo@6017
    77
  END IF;  
eduardo@6017
    78
  IF (INSERTING OR UPDATING) THEN
eduardo@6017
    79
   v_newLineNetAmt := COALESCE(:new.LineNetAmt,0);
eduardo@6017
    80
   v_newLineAlternate := COALESCE(:new.TaxBaseAmt,0);
unai@26214
    81
   
unai@26214
    82
   IF (UPDATING) THEN
unai@26214
    83
     v_oldLineAlternate := COALESCE(:old.TaxBaseAmt,0);
unai@26214
    84
     IF (:new.AD_Org_ID <> :old.AD_Org_ID OR :new.C_Tax_ID <> :old.C_Tax_ID OR
unai@26214
    85
         v_newLineNetAmt <> v_oldLine OR v_newLineAlternate <> v_oldLineAlternate OR
unai@26214
    86
         :new.line_gross_amount <> :old.line_gross_amount) THEN
unai@26214
    87
       DELETE FROM C_INVOICELINETAX WHERE C_INVOICELINE_ID = :new.C_InvoiceLine_ID;
unai@26214
    88
       v_create := 'Y';
unai@26214
    89
     ELSE
unai@26214
    90
       v_create := 'N';
hari@17336
    91
     END IF;
unai@26214
    92
   END IF;   
unai@26214
    93
unai@26214
    94
   IF (v_create = 'Y') THEN  
unai@26214
    95
     IF(:new.C_Tax_ID IS NOT NULL) THEN
unai@26214
    96
       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);
unai@26214
    97
       IF (v_istaxincluded = 'Y') THEN
unai@26214
    98
         C_INVOICELINETAX_ROUNDING(:new.C_InvoiceLine_ID, :new.line_gross_amount, v_newLineNetAmt);
unai@26214
    99
       END IF;
unai@26214
   100
     END IF;
unai@26214
   101
    -- Get Total Tax Amt
unai@26214
   102
     SELECT SUM(TaxAmt)
unai@26214
   103
       INTO v_taxAmt
unai@26214
   104
     FROM C_InvoiceTax
unai@26214
   105
     WHERE C_Invoice_ID=:new.C_Invoice_ID;
unai@26214
   106
    -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
unai@26214
   107
     -- Update Header
unai@26214
   108
         -- Get Total Tax Amt
unai@26214
   109
     UPDATE C_Invoice
unai@26214
   110
       SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
unai@26214
   111
       GrandTotal = CASE v_istaxincluded
unai@26214
   112
                       WHEN 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt
unai@26214
   113
                       ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
unai@26214
   114
                    END
unai@26214
   115
     --  Updated = SysDate -- Don't update as otherwise it does not save changes
unai@26214
   116
     WHERE C_Invoice_ID = :new.C_Invoice_ID;
eduardo@6054
   117
   END IF;
eduardo@6017
   118
  ELSE -- DELETING
mikel@17454
   119
   IF (v_istaxincluded = 'Y') THEN
mikel@17454
   120
     v_oldgrossamt := :old.line_gross_amount;
mikel@17454
   121
   END IF;
eduardo@6017
   122
   SELECT SUM(TaxAmt)
eduardo@6017
   123
     INTO v_taxAmt
eduardo@6017
   124
   FROM C_InvoiceTax
eduardo@6017
   125
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
eduardo@6017
   126
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
eduardo@6017
   127
   -- Update Header
eduardo@6017
   128
   UPDATE C_Invoice
eduardo@6017
   129
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
hari@17336
   130
     GrandTotal = CASE v_istaxincluded
hari@17336
   131
                       WHEN 'Y' THEN grandtotal - v_oldgrossamt
hari@17336
   132
                       ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
sandra@25602
   133
                  END
eduardo@6017
   134
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
eduardo@6017
   135
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
eduardo@6017
   136
  END IF;
hari@17336
   137
  IF (v_istaxincluded = 'Y') THEN
hari@17336
   138
    SELECT totallines, grandtotal INTO v_totallines, v_grandtotal
gorkaion@17348
   139
    FROM C_invoice
hari@17336
   140
    WHERE c_invoice_id = v_id;
gorkaion@17348
   141
    C_INVOICETAX_ROUNDING(v_id, v_grandtotal, v_totallines);
hari@17336
   142
  END IF;
hari@17336
   143
  
eduardo@6017
   144
 END IF;
eduardo@6017
   145
eduardo@6017
   146
antonio@735
   147
END C_INVOICELINE_TRG2
gorkaion@239
   148
]]></body>
adrian@94
   149
    </trigger>
adrian@94
   150
  </database>