pgsql/investigations/ar-cash.sql
[Pman.Xtuple] / pgsql / x-fifo-invfifo-unitcost-at-qty.sql
1
2
3
4 -- find the qty available.. 
5 CREATE OR REPLACE FUNCTION invfifo_unitcost_at_qty(integer, integer, numeric(18, 6))
6     RETURNS  numeric(18, 6) 
7     
8 AS $BODY$
9 DECLARE
10     i_itemsite_id  ALIAS FOR $1;
11     i_location_id  ALIAS FOR $2;
12     i_qty           ALIAS FOR $3;
13     v_ret               numeric(18, 6);
14     
15       
16 BEGIN
17        
18    -- price at a specific qty.
19     SELECT
20             invfifo_landedunitcost
21         
22         INTO
23             v_ret
24         FROM
25             invdetailview
26         WHERE
27             invfifo_qty_after <= i_qty
28             AND    
29             invdetail_location_id = i_location_id
30             AND
31             invhist_itemsite_id = i_itemsite_id
32             AND
33             invdetail_qty > 0
34             AND
35             invhist_transtype = 'RP' -- purchases only!
36             AND
37             invfifo_void = 0
38         LIMIT
39             1;
40     
41     IF  FOUND THEN
42          return v_ret;
43          
44     END IF;    
45     
46     return 0;
47     
48 END;
49 $BODY$
50   LANGUAGE plpgsql VOLATILE
51   COST 100;
52   
53 ALTER FUNCTION  invfifo_unitcost_at_qty(integer, integer, numeric(18, 6))
54   OWNER TO admin;
55           
56           -- find the qty available.. 
57 CREATE OR REPLACE FUNCTION invfifo_unitcost_at_date(integer,  date)
58     RETURNS  numeric(18, 6) 
59     
60 AS $BODY$
61 DECLARE
62     i_itemsite_id  ALIAS FOR $1;
63    
64     i_date           ALIAS FOR $2;
65     v_ret               numeric(18, 6);
66     
67       
68 BEGIN
69        
70    -- price at a specific qty.
71     SELECT
72         invfifo_landedunitcost
73         
74         INTO
75             v_ret
76         FROM
77             invdetailview
78         WHERE
79             invhist_transdate < i_date
80             AND
81             invhist_itemsite_id = i_itemsite_id
82             AND
83             invhist_transtype = 'RP' -- purchases only!
84             AND
85             invdetail_qty > 0
86             AND
87             invfifo_void = 0
88         ORDER BY
89             invhist_transdate  DESC
90         LIMIT
91             1;
92     
93     IF  FOUND THEN
94          return v_ret;
95          
96     END IF;
97     
98     
99     
100     -- not found, then try next purchase price..
101     
102     SELECT
103         invfifo_landedunitcost
104         
105         INTO
106             v_ret
107         FROM
108             invdetailview
109         WHERE
110             invhist_transdate >= i_date
111             AND
112             invhist_itemsite_id = i_itemsite_id
113             AND
114             invhist_transtype = 'RP' -- purchases only!
115             AND
116             invdetail_qty > 0
117             AND
118             invfifo_void = 0
119         ORDER BY
120             invhist_transdate  ASC
121         LIMIT
122             1;
123     
124     IF  FOUND THEN
125          return v_ret;
126          
127     END IF;    
128     
129     -- if we really can not find any purchases, then we should use the stdcost?
130
131     SELECT stdcost(itemsite_item_id) INTO v_ret FROM itemsite where itemsite_id = i_itemsite_id;
132
133     return v_ret;
134
135
136     --return 0;
137     
138 END;
139 $BODY$
140   LANGUAGE plpgsql VOLATILE
141   COST 100;
142   
143 ALTER FUNCTION  invfifo_unitcost_at_date(integer, date)
144   OWNER TO admin;
145           
146