src-db/database/model/triggers/C_INVOICELINE_TRG3.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Mon, 28 Apr 2008 09:28:08 +0000
changeset 785 8dba91261590
parent 760 f1e6a64dde3f
child 1605 8a0fe0193bef
permissions -rw-r--r--
Fix last update of copyright years in database files
juanpablo@728
     1
<?xml version="1.0"?>
juanpablo@728
     2
  <database name="TRIGGER C_INVOICELINE_TRG3">
juanpablo@728
     3
    <trigger name="C_INVOICELINE_TRG3" table="C_INVOICELINE" fires="after" insert="true" update="true" delete="true" foreach="row">
carlos@730
     4
      <body><![CDATA[/*************************************************************************
carlos@730
     5
* The contents of this file are subject to the Openbravo  Public  License
carlos@730
     6
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@730
     7
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@730
     8
* file except in compliance with the License. You  may  obtain  a copy of
carlos@730
     9
* the License at http://www.openbravo.com/legal/license.html
carlos@730
    10
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@730
    11
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@730
    12
* License for the specific  language  governing  rights  and  limitations
carlos@730
    13
* under the License.
carlos@730
    14
* The Original Code is Openbravo ERP.
carlos@730
    15
* The Initial Developer of the Original Code is Openbravo SL
carlos@730
    16
* All portions are Copyright (C) 2001-2008 Openbravo SL
carlos@730
    17
* All Rights Reserved.
carlos@730
    18
* Contributor(s):  ______________________________________.
carlos@730
    19
************************************************************************/
eduardo@723
    20
--Invoice
eduardo@723
    21
vwithholdingtotal NUMBER;
eduardo@723
    22
vBusPartId NUMBER;
eduardo@723
    23
vIsSalesInvoice CHAR(1);
eduardo@723
    24
vProcessed  CHAR(1);
antonio@737
    25
--InvoiceLine
eduardo@723
    26
v_invoiceid NUMBER;
eduardo@723
    27
vexcludeforwh CHAR(1);
eduardo@723
    28
vlinebaseamountnew NUMBER;
eduardo@723
    29
vlinebaseamountold NUMBER;
eduardo@723
    30
-- BP_Withholding
eduardo@723
    31
isbaseamountpercent CHAR(1);
eduardo@723
    32
baseamountpercent NUMBER;
eduardo@723
    33
includetaxinps CHAR(1);
eduardo@723
    34
taxinps_id NUMBER;
antonio@737
    35
-- Tax
eduardo@723
    36
vTaxAmount  NUMBER;
eduardo@723
    37
-- Withholding
eduardo@723
    38
withpercent NUMBER;
antonio@737
    39
--Calculated Variable
eduardo@723
    40
vwithholdingnew NUMBER;
eduardo@723
    41
vwithholdingold NUMBER;
eduardo@723
    42
taxamountwith  NUMBER;
eduardo@723
    43
vwithholdingtax NUMBER;
eduardo@723
    44
vwithid NUMBER;
carlos@730
    45
finish_process BOOLEAN := FALSE;
eduardo@723
    46
BEGIN
eduardo@723
    47
  IF(INSERTING) THEN
eduardo@723
    48
    v_invoiceid := :NEW.c_invoice_id;
eduardo@723
    49
    vexcludeforwh := :NEW.excludeforwithholding;
eduardo@723
    50
    vlinebaseamountnew := :NEW.linenetamt;
eduardo@723
    51
    vlinebaseamountold := 0;
antonio@737
    52
eduardo@723
    53
    ELSIF(UPDATING) THEN
eduardo@723
    54
      v_invoiceid := :NEW.c_invoice_id;
eduardo@723
    55
      vexcludeforwh := :NEW.excludeforwithholding;
eduardo@723
    56
      vlinebaseamountnew := :NEW.linenetamt;
eduardo@723
    57
      vlinebaseamountold := :OLD.linenetamt;
antonio@737
    58
eduardo@723
    59
      ELSIF(DELETING) THEN
eduardo@723
    60
        v_invoiceid := :OLD.c_invoice_id;
eduardo@723
    61
        vexcludeforwh := :OLD.excludeforwithholding;
eduardo@723
    62
        vlinebaseamountnew := 0;
eduardo@723
    63
        vlinebaseamountold := :OLD.linenetamt;
antonio@737
    64
eduardo@723
    65
      END IF;
eduardo@723
    66
antonio@737
    67
      -- If The lines is excluded for calculate the witholding --> exit
eduardo@723
    68
      IF vexcludeforwh = 'Y' THEN
carlos@730
    69
        finish_process:= true;
eduardo@723
    70
      END IF;
carlos@730
    71
  IF (NOT finish_process) THEN
eduardo@723
    72
      SELECT COALESCE(withholdingamount,   0), c_bpartner_id, IsSOTrx , coalesce(processed,'N') ,c_withholding_id
eduardo@723
    73
      INTO vwithholdingtotal,vBusPartId, vIsSalesInvoice, vProcessed  ,vwithid
eduardo@723
    74
      FROM c_invoice
eduardo@723
    75
      WHERE c_invoice_id = v_invoiceid;
carlos@730
    76
  END IF;
carlos@730
    77
  IF (NOT finish_process) THEN
eduardo@723
    78
      -- Se the invoice isn't purchase exit from trigger
eduardo@723
    79
      if (vIsSalesInvoice <> 'N') then
carlos@730
    80
        finish_process:= true;
antonio@737
    81
      end if;
carlos@730
    82
  END IF;
carlos@730
    83
  IF (NOT finish_process) THEN
eduardo@723
    84
    BEGIN
eduardo@723
    85
      select bpwh.is_percent_wh, bpwh.wh_percent, bpwh.include_tax, bpwh.c_tax_id,wh.rate
carlos@730
    86
      into isbaseamountpercent, baseamountpercent,  includetaxinps, taxinps_id,withpercent
eduardo@723
    87
        from c_bp_withholding bpwh, c_withholding wh
antonio@737
    88
       where bpwh.c_bpartner_id = vBusPartId
antonio@737
    89
       and bpwh.c_withholding_id = wh.c_withholding_id
eduardo@723
    90
       and bpwh.c_withholding_id = vwithid;
eduardo@723
    91
antonio@737
    92
    EXCEPTION
eduardo@723
    93
    When NO_DATA_FOUND THEN
carlos@730
    94
      finish_process:= true;
eduardo@723
    95
    end;
carlos@730
    96
  END IF;
carlos@730
    97
  IF (NOT finish_process) THEN
antonio@737
    98
   -- ReadOnly Check
carlos@730
    99
    IF (vProcessed = 'N') THEN
antonio@737
   100
eduardo@723
   101
      IF(isbaseamountpercent <> 'Y') THEN
eduardo@723
   102
        baseamountpercent := 100;
eduardo@723
   103
      END IF;
eduardo@723
   104
      -- New WithHolding For LineAmount
eduardo@723
   105
      vlinebaseamountnew := vlinebaseamountnew *baseamountpercent / 100;
eduardo@723
   106
      vwithholdingnew :=(withpercent * vlinebaseamountnew) / 100;
eduardo@723
   107
      -- Old WithHolding For LineAmount
eduardo@723
   108
      vlinebaseamountold := vlinebaseamountold *baseamountpercent / 100;
eduardo@723
   109
      vwithholdingold :=(withpercent *vlinebaseamountold) / 100;
eduardo@723
   110
eduardo@723
   111
antonio@737
   112
      -- WithHolding Tax
eduardo@723
   113
      vTaxAmount := 0;
eduardo@723
   114
      IF (includetaxinps = 'Y') THEN
eduardo@723
   115
       select coalesce(sum(taxamt),0) into vTaxAmount
antonio@737
   116
        from c_invoicetax invtax , c_tax tax
eduardo@723
   117
       where tax.c_tax_id = invtax.c_tax_id
eduardo@760
   118
        and tax.iswithholdingtax='Y'
eduardo@723
   119
        and invtax.c_invoice_id = v_invoiceid
eduardo@723
   120
        and tax.c_tax_id=taxinps_id;
eduardo@723
   121
      END IF;
antonio@737
   122
eduardo@723
   123
      taxamountwith := baseamountpercent * (vTaxAmount)/100;
eduardo@723
   124
      vwithholdingtax :=  withpercent*taxamountwith/100 ;
eduardo@723
   125
eduardo@723
   126
      -- Update WITHHOLDINGAMOUNT della c_invoice = totale+(new valore wh of line) -(old valore wh of line)
eduardo@723
   127
      vwithholdingtotal := vwithholdingtotal + vwithholdingnew -vwithholdingold+ vwithholdingtax;
eduardo@723
   128
eduardo@723
   129
      UPDATE c_invoice
eduardo@760
   130
      SET withholdingamount = vwithholdingtotal
eduardo@723
   131
      WHERE c_invoice_id = v_invoiceid;
carlos@730
   132
    END IF;
eduardo@723
   133
  END IF;
antonio@737
   134
eduardo@723
   135
END C_INVOICELINE_TRG3
juanpablo@728
   136
]]></body>
juanpablo@728
   137
    </trigger>
juanpablo@728
   138
  </database>