src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
author Sandra Huguet <sandra.huguet@openbravo.com>
Wed, 17 Dec 2014 17:30:22 +0100
changeset 25568 132f3d7f9f75
parent 18403 84881ccb60f5
child 25602 fbc674d56f04
permissions -rw-r--r--
Related to issue 27580
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
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;
mikel@17454
    69
 
david@18403
    70
 IF INSERTING OR UPDATING THEN
hari@17336
    71
   SELECT C_BPartner_ID, dateInvoiced, priceprecision, i.M_PriceList_ID,p.istaxincluded
hari@17336
    72
    INTO v_BPartner_ID, v_DateInvoiced, v_Precision, v_PriceList_ID, v_istaxincluded
hari@17336
    73
     FROM C_INVOICE i
hari@17336
    74
             JOIN m_pricelist p ON i.m_pricelist_id = p.m_pricelist_id,
hari@17336
    75
adrian@94
    76
          C_CURRENCY c
hari@17336
    77
          
adrian@94
    78
   WHERE C_Invoice_ID = :NEW.C_Invoice_ID
adrian@94
    79
     AND i.C_Currency_ID = c.C_Currency_ID;
adrian@94
    80
hari@17336
    81
   IF (v_istaxincluded = 'Y') THEN
mikel@17454
    82
     IF (UPDATING) THEN
hari@17336
    83
        v_oldgrossamt := :old.line_gross_amount;
hari@17336
    84
        v_newgrossamt := :new.line_gross_amount;
hari@17336
    85
     ELSE
hari@17336
    86
        v_newgrossamt := :new.line_gross_amount;
hari@17336
    87
     END IF;
hari@17336
    88
   END IF;
adrian@94
    89
 END IF;
adrian@94
    90
eduardo@6017
    91
  /**************************************************************************
eduardo@6017
    92
   * Calculate Tax, etc.
eduardo@6017
    93
   */
mikel@17454
    94
 SELECT processed, stdPrecision, pl.istaxincluded
mikel@17454
    95
   INTO v_Processed, v_Prec, v_istaxincluded
mikel@17454
    96
 FROM C_Invoice, C_Currency, m_pricelist pl
eduardo@6017
    97
 WHERE C_Invoice.C_Currency_ID = C_Currency.C_Currency_ID
mikel@17454
    98
 AND C_Invoice.m_pricelist_id = pl.m_pricelist_id
eduardo@6017
    99
 AND C_Invoice_ID=v_ID;
eduardo@6017
   100
 v_oldLine:=0;
eduardo@6017
   101
 v_newLineNetAmt:=0;
eduardo@6017
   102
 IF(v_Processed='N') THEN
eduardo@6017
   103
  -- Calculate taxes
harikrishnan@7382
   104
  IF(UPDATING OR DELETING) THEN
eduardo@6017
   105
  v_oldLine:= COALESCE(:old.LineNetAmt,0);
eduardo@6017
   106
  END IF;  
eduardo@6017
   107
  IF (INSERTING OR UPDATING) THEN
eduardo@6017
   108
   DELETE FROM C_INVOICELINETAX WHERE C_INVOICELINE_ID = :new.C_InvoiceLine_ID;
eduardo@6017
   109
   v_newLineNetAmt := COALESCE(:new.LineNetAmt,0);
eduardo@6017
   110
   v_newLineAlternate := COALESCE(:new.TaxBaseAmt,0);
eduardo@6054
   111
   IF(:new.C_Tax_ID IS NOT NULL) THEN
eduardo@6054
   112
     SELECT BaseAmount INTO v_BaseAmount
eduardo@6054
   113
     FROM C_TAX
eduardo@6054
   114
     WHERE C_TAX_ID = :new.C_Tax_ID;
eduardo@6054
   115
     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
   116
     IF (v_istaxincluded = 'Y') THEN
hari@17336
   117
        C_INVOICELINETAX_ROUNDING(:new.C_InvoiceLine_ID, :new.line_gross_amount, v_newLineNetAmt);
hari@17336
   118
     END IF;
hari@17336
   119
    
eduardo@6054
   120
   END IF;
eduardo@6017
   121
  -- Get Total Tax Amt
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=:new.C_Invoice_ID;
eduardo@6017
   126
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
eduardo@6017
   127
   -- Update Header
hari@17336
   128
       -- Get Total Tax Amt
eduardo@6017
   129
   UPDATE C_Invoice
eduardo@6017
   130
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
hari@17336
   131
     GrandTotal = CASE v_istaxincluded
hari@17336
   132
                     WHEN 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt
hari@17336
   133
                     ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
hari@17336
   134
                  END
eduardo@6017
   135
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
eduardo@6017
   136
   WHERE C_Invoice_ID = :new.C_Invoice_ID;
eduardo@6017
   137
  ELSE -- DELETING
mikel@17454
   138
   IF (v_istaxincluded = 'Y') THEN
mikel@17454
   139
     v_oldgrossamt := :old.line_gross_amount;
mikel@17454
   140
   END IF;
eduardo@6017
   141
   SELECT SUM(TaxAmt)
eduardo@6017
   142
     INTO v_taxAmt
eduardo@6017
   143
   FROM C_InvoiceTax
eduardo@6017
   144
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
eduardo@6017
   145
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
eduardo@6017
   146
   -- Update Header
eduardo@6017
   147
   UPDATE C_Invoice
eduardo@6017
   148
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
hari@17336
   149
     GrandTotal = CASE v_istaxincluded
hari@17336
   150
                       WHEN 'Y' THEN grandtotal - v_oldgrossamt
hari@17336
   151
                       ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
sandra@25568
   152
                  END,  prepaymentamt=prepaymentamt - v_oldLine + v_newLineNetAmt
eduardo@6017
   153
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
eduardo@6017
   154
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
eduardo@6017
   155
  END IF;
hari@17336
   156
  IF (v_istaxincluded = 'Y') THEN
hari@17336
   157
    SELECT totallines, grandtotal INTO v_totallines, v_grandtotal
gorkaion@17348
   158
    FROM C_invoice
hari@17336
   159
    WHERE c_invoice_id = v_id;
gorkaion@17348
   160
    C_INVOICETAX_ROUNDING(v_id, v_grandtotal, v_totallines);
hari@17336
   161
  END IF;
hari@17336
   162
  
eduardo@6017
   163
 END IF;
eduardo@6017
   164
eduardo@6017
   165
antonio@735
   166
END C_INVOICELINE_TRG2
gorkaion@239
   167
]]></body>
adrian@94
   168
    </trigger>
adrian@94
   169
  </database>