df47bb400194f30d0155a73b7d4513034d734224
[xtuple] / foundation-database / public / functions / calculatetaxdetailline.sql
1
2 CREATE OR REPLACE FUNCTION calculatetaxdetailline(text, integer) 
3   RETURNS SETOF taxdetail AS
4 $BODY$
5 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
6 -- See www.xtuple.com/CPAL for the full text of the software license.
7 DECLARE
8   pOrderType ALIAS FOR $1;
9   pOrderId ALIAS FOR $2;
10   _row taxdetail%ROWTYPE;
11   _qry text;
12   _totaltax numeric;
13   _y RECORD;
14   _table text;
15   
16 BEGIN
17    _totaltax=0.0;
18
19    IF pOrderType = 'II' THEN
20      _table := 'invcitemtax';
21    ELSIF pOrderType = 'BI' THEN
22      _table := 'cobilltax';
23    ELSIF pOrderType = 'CI' THEN
24      _table := 'cmitemtax';
25    ELSIF pOrderType = 'VI' THEN
26      _table := 'voitemtax';
27    ELSIF pOrderType = 'TI' THEN
28      _table := 'toitemtax';
29    ELSIF pOrderType = 'AR' THEN
30      _table := 'aropentax';
31    ELSIF pOrderType = 'AP' THEN
32      _table := 'apopentax';
33    END IF;
34      
35    _qry := 'SELECT taxhist_tax_id as tax_id, tax_code, tax_descrip, taxhist_tax, COALESCE(taxhist_sequence,0) AS taxhist_sequence
36             FROM taxhist 
37              JOIN tax ON (taxhist_tax_id=tax_id) 
38              JOIN pg_class ON (pg_class.oid=taxhist.tableoid) 
39             WHERE ( (taxhist_parent_id = ' || pOrderId || ')
40              AND (relname=''' || _table || ''') );';
41     
42    FOR _y IN  EXECUTE _qry
43    LOOP
44      _row.taxdetail_tax_id=_y.tax_id;
45      _row.taxdetail_tax_code = _y.tax_code;
46      _row.taxdetail_tax_descrip = _y.tax_descrip;
47      _row.taxdetail_tax = _y.taxhist_tax;
48      _row.taxdetail_level= 0 ;
49      _row.taxdetail_taxclass_sequence= _y.taxhist_sequence;
50      _totaltax = _totaltax + _y.taxhist_tax;
51      RETURN NEXT _row;
52    END LOOP;
53  END;
54 $BODY$
55   LANGUAGE 'plpgsql' VOLATILE;