pgsql/investigations/ar-creditmemo.sql
[Pman.Xtuple] / pgsql / x-dragon-itemprice_wrp.sql
1  
2  
3
4 CREATE OR REPLACE FUNCTION itemprice_wrp(integer, integer)
5     RETURNS  numeric(18, 6)
6
7 AS $BODY$
8 DECLARE        
9            
10     i_itemsite_id  ALIAS FOR $1;
11     i_curr_id  ALIAS FOR $2;
12     v_result numeric(18, 6);
13     
14 BEGIN
15      
16     
17
18     SELECT
19             ROUND(MAX(ipsitem_price),3)
20         INTO
21             v_result
22         FROM
23             ipsiteminfo
24         LEFT JOIN
25             ipshead
26         ON
27             ipshead_id = ipsitem_ipshead_id 
28         LEFT JOIN
29             itemsite
30         ON
31             ipsitem_item_id  = itemsite_item_id
32         WHERE
33             ipshead_name like '%WRP%'
34         AND
35             ipshead_curr_id = i_curr_id
36         AND
37             itemsite_id = i_itemsite_id;
38         
39         -- not found?
40     IF FOUND AND v_result IS NOT NULL THEN 
41         RETURN v_result;
42     END IF;
43         
44     
45               
46             
47     -- default...  could not work it out above..      
48     
49         -- use the list price.
50         
51     SELECT   currtocurr(
52                     basecurrid(),
53                     i_curr_id,
54                     item_listprice,
55                     NOW()::date
56                 )
57         INTO
58             v_result
59         FROM
60             itemsite
61         LEFT JOIN
62             item
63         ON
64             item_id = itemsite_item_id
65         WHERE
66             itemsite_id = i_itemsite_id
67         LIMIT 1;
68         
69         -- not found?
70         
71     RETURN v_result;
72         
73          END;
74 $BODY$
75   LANGUAGE plpgsql VOLATILE
76   COST 100;
77   
78 ALTER FUNCTION  itemprice_wrp(integer, integer)
79   OWNER TO admin;
80     
81     
82  -- select itemprice_wrp(itemsite_id, getcurrid('USD')) FROM itemsite;