src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
author Sandra Huguet <sandra.huguet@openbravo.com>
Tue, 23 Dec 2014 18:53:13 +0100
changeset 25602 fbc674d56f04
parent 25568 132f3d7f9f75
child 26214 35a4e54c3ffc
permissions -rw-r--r--
Fixed issue 27580 fixed issue to cash vat invoices

Fixed wrong journal entries when you void a cash vat sales invoice
and fixed total prepayment amount
<?xml version="1.0"?>
  <database name="TRIGGER C_INVOICELINE_TRG2">
    <trigger name="C_INVOICELINE_TRG2" table="C_INVOICELINE" fires="after" insert="true" update="true" delete="true" foreach="row">
      <body><![CDATA[
/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/license.html
* Software distributed under the License  is  distributed  on  an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific  language  governing  rights  and  limitations
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2001-2012 Openbravo SLU
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/





   TYPE RECORD IS REF CURSOR;
   Cur_Offer RECORD;

  v_InvoiceLine_Offer_ID VARCHAR2(32);
  v_lineno NUMBER:=0;
  v_Price NUMBER;
  v_PriceNew NUMBER;
  v_Amt NUMBER;
  v_BPartner_ID VARCHAR2(32);
  v_Order_ID VARCHAR2(32);
  v_Insert BOOLEAN :=FALSE;
  v_DateInvoiced DATE;
  v_Precision NUMBER;
  v_PriceList_ID VARCHAR2(32);
  v_Processed C_ORDER.PROCESSED%TYPE;
  v_ID VARCHAR2(32);
  v_Issotrx CHAR(1);
  v_pricecancelAD CHAR(1);
  v_oldLine NUMBER;
  v_newLineNetAmt NUMBER;
  v_newLineAlternate NUMBER;
  v_taxAmt NUMBER;
  v_Prec C_CURRENCY.STDPRECISION%TYPE;
  v_BaseAmount C_TAX.BASEAMOUNT%TYPE;  
  v_istaxincluded CHAR(1) ;
  v_oldGrossAmt NUMBER:=0;
  v_newGrossAmt NUMBER:=0;
  v_totallines NUMBER:=0;
  v_grandtotal NUMBER:=0;
        
BEGIN
    
    IF AD_isTriggerEnabled()='N' THEN RETURN;
    END IF;


-- This trigger is used for calculate the applied offers f0r the invoice

IF (DELETING) THEN
  v_ID:=:OLD.C_INVOICE_ID;
ELSE
  v_ID:=:NEW.C_INVOICE_ID;
END IF;
 
 IF INSERTING OR UPDATING THEN
   SELECT C_BPartner_ID, dateInvoiced, priceprecision, i.M_PriceList_ID,p.istaxincluded
    INTO v_BPartner_ID, v_DateInvoiced, v_Precision, v_PriceList_ID, v_istaxincluded
     FROM C_INVOICE i
             JOIN m_pricelist p ON i.m_pricelist_id = p.m_pricelist_id,

          C_CURRENCY c
          
   WHERE C_Invoice_ID = :NEW.C_Invoice_ID
     AND i.C_Currency_ID = c.C_Currency_ID;

   IF (v_istaxincluded = 'Y') THEN
     IF (UPDATING) THEN
        v_oldgrossamt := :old.line_gross_amount;
        v_newgrossamt := :new.line_gross_amount;
     ELSE
        v_newgrossamt := :new.line_gross_amount;
     END IF;
   END IF;
 END IF;

  /**************************************************************************
   * Calculate Tax, etc.
   */
 SELECT processed, stdPrecision, pl.istaxincluded
   INTO v_Processed, v_Prec, v_istaxincluded
 FROM C_Invoice, C_Currency, m_pricelist pl
 WHERE C_Invoice.C_Currency_ID = C_Currency.C_Currency_ID
 AND C_Invoice.m_pricelist_id = pl.m_pricelist_id
 AND C_Invoice_ID=v_ID;
 v_oldLine:=0;
 v_newLineNetAmt:=0;
 IF(v_Processed='N') THEN
  -- Calculate taxes
  IF(UPDATING OR DELETING) THEN
  v_oldLine:= COALESCE(:old.LineNetAmt,0);
  END IF;  
  IF (INSERTING OR UPDATING) THEN
   DELETE FROM C_INVOICELINETAX WHERE C_INVOICELINE_ID = :new.C_InvoiceLine_ID;
   v_newLineNetAmt := COALESCE(:new.LineNetAmt,0);
   v_newLineAlternate := COALESCE(:new.TaxBaseAmt,0);
   IF(:new.C_Tax_ID IS NOT NULL) THEN
     SELECT BaseAmount INTO v_BaseAmount
     FROM C_TAX
     WHERE C_TAX_ID = :new.C_Tax_ID;
     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);
     IF (v_istaxincluded = 'Y') THEN
        C_INVOICELINETAX_ROUNDING(:new.C_InvoiceLine_ID, :new.line_gross_amount, v_newLineNetAmt);
     END IF;
    
   END IF;
  -- Get Total Tax Amt
   SELECT SUM(TaxAmt)
     INTO v_taxAmt
   FROM C_InvoiceTax
   WHERE C_Invoice_ID=:new.C_Invoice_ID;
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
   -- Update Header
       -- Get Total Tax Amt
   UPDATE C_Invoice
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
     GrandTotal = CASE v_istaxincluded
                     WHEN 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt
                     ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
                  END
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
   WHERE C_Invoice_ID = :new.C_Invoice_ID;
  ELSE -- DELETING
   IF (v_istaxincluded = 'Y') THEN
     v_oldgrossamt := :old.line_gross_amount;
   END IF;
   SELECT SUM(TaxAmt)
     INTO v_taxAmt
   FROM C_InvoiceTax
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
  -- DBMS_OUTPUT.PUT_LINE('TaxAmt = ' || v_taxAmt);
   -- Update Header
   UPDATE C_Invoice
     SET TotalLines = TotalLines - v_oldLine + v_newLineNetAmt,
     GrandTotal = CASE v_istaxincluded
                       WHEN 'Y' THEN grandtotal - v_oldgrossamt
                       ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
                  END
   --  Updated = SysDate -- Don't update as otherwise it does not save changes
   WHERE C_Invoice_ID=:old.C_Invoice_ID;
  END IF;
  IF (v_istaxincluded = 'Y') THEN
    SELECT totallines, grandtotal INTO v_totallines, v_grandtotal
    FROM C_invoice
    WHERE c_invoice_id = v_id;
    C_INVOICETAX_ROUNDING(v_id, v_grandtotal, v_totallines);
  END IF;
  
 END IF;


END C_INVOICELINE_TRG2
]]></body>
    </trigger>
  </database>