3 -- create an index on location so it's fast..
7 -- returns the exact quantity of stock at the invdetail_id time..
13 CREATE OR REPLACE FUNCTION invdetail_bydate(i_id int)
14 RETURNS numeric(18,6) AS
17 v_return numeric(18,6) ;
20 SELECT invdetail_at_id(i_id) into v_return;
26 LANGUAGE plpgsql STABLE
28 ALTER FUNCTION invdetail_bydate(int)
32 CREATE OR REPLACE FUNCTION invdetail_at_id(i_id int)
33 RETURNS numeric(18,6) AS
36 v_itemsite_id INTEGER;
37 v_location_id INTEGER;
38 v_transdate timestamp with time zone;
40 v_return numeric(18,6) ;
49 invdetail_location_id,
57 FROM invdetail LEFT JOIN invhist ON
58 invdetail_invhist_id = invhist_id
64 -- # when transactions are the same day, we only want to include the ones with lower ids..
68 COALESCE(SUM( invdetail_qty), 0) + v_qty INTO v_return
70 FROM invdetail LEFT JOIN invhist ON
71 invdetail_invhist_id = invhist_id
73 invdetail_location_id = v_location_id
75 invhist_itemsite_id = v_itemsite_id
77 invhist_transdate < v_transdate
79 (invhist_transdate = v_transdate AND invdetail_id < i_id)
82 IF (v_return IS NULL) THEN
87 -- UPDATE invdetail SET invdetail_bydate_qty_after = v_return WHERE invdetail_id = i_id;
93 LANGUAGE plpgsql STABLE
95 ALTER FUNCTION invdetail_byid(int)
102 -- DATE, location_id, itemsite_id
103 CREATE OR REPLACE FUNCTION invdetail_atdate(i_transdate timestamp with time zone, i_location_id int, i_itemsite_id int)
104 RETURNS numeric(18,6) AS
108 v_return numeric(18,6) ;
113 SELECT COALESCE(SUM( invdetail_qty), 0)
120 invhist_itemsite_id = i_itemsite_id
122 invhist_transdate < i_transdate
127 invdetail_location_id = i_location_id;
130 RETURN COALESCE(v_return ,0);
135 LANGUAGE plpgsql STABLE
137 ALTER FUNCTION invdetail_atdate(timestamp with time zone, int, int)
142 -- DATE, location_id, itemsite_id
143 CREATE OR REPLACE FUNCTION invdetail_cost_atdate(timestamp with time zone, int, int)
144 RETURNS numeric(18,6) AS
147 i_transdate ALIAS FOR $1;
148 i_location_id ALIAS FOR $2;
149 i_itemsite_id ALIAS FOR $3;
150 v_return numeric(18,6) ;
156 COALESCE(SUM( invfifo_landedunitcost * invdetail_qty) , 0)
162 invhist_itemsite_id = i_itemsite_id
164 invhist_transdate < i_transdate
166 invdetail_location_id = i_location_id
170 RETURN COALESCE(v_return ,0);
175 LANGUAGE plpgsql STABLE
177 ALTER FUNCTION invdetail_cost_atdate(timestamp with time zone, int, int)
191 -- testing vlogic. on 2012-08-31 (124)
193 -- select invdetail_location_atdate('2012-08-31', 124,1);
194 --itemsite 1231 has 93?
198 -- invhist_itemsite_id,
199 -- sum(invdetail_qty) as invdetail_total
203 -- invhist_transdate < '2012-08-31'
205 -- invdetail_location_id = 124
207 -- invhist_itemsite_id
209 -- SUM(invdetail_qty) > 0
214 CREATE OR REPLACE FUNCTION invdetail_location_atdate(timestamp with time zone, int, int)
215 RETURNS numeric(18,6) AS
218 i_transdate ALIAS FOR $1;
219 i_location_id ALIAS FOR $2;
220 v_posneg ALIAS FOR $3;
221 v_return numeric(18,6) ;
229 SELECT SUM(invdetail_total)
234 sum(invdetail_qty) as invdetail_total
238 invhist_transdate < i_transdate
240 invdetail_location_id = i_location_id
246 SUM(invdetail_qty) > 0
251 RETURN COALESCE(v_return ,0);
255 SELECT SUM(invdetail_total)
260 sum(invdetail_qty) as invdetail_total
264 invhist_transdate < i_transdate
266 invdetail_location_id = i_location_id
272 SUM(invdetail_qty) < 0
277 RETURN COALESCE(v_return ,0);
281 LANGUAGE plpgsql STABLE
283 ALTER FUNCTION invdetail_location_atdate(timestamp with time zone, int, int )
288 CREATE OR REPLACE FUNCTION invcost_location_atdate(timestamp with time zone, int)
289 RETURNS numeric(18,6) AS
292 i_transdate ALIAS FOR $1;
293 i_location_id ALIAS FOR $2;
294 v_return numeric(18,6) ;
300 COALESCE(SUM( invfifo_landedunitcost * invdetail_qty ), 0)
306 invhist_transdate < i_transdate
308 invdetail_location_id = i_location_id
312 RETURN COALESCE(v_return ,0);
317 LANGUAGE plpgsql STABLE
319 ALTER FUNCTION invcost_location_atdate(timestamp with time zone, int )
324 CREATE OR REPLACE FUNCTION invdetail_cost_location_atdate(timestamp with time zone, int)
325 RETURNS numeric(18,6) AS
328 i_transdate ALIAS FOR $1;
329 i_location_id ALIAS FOR $2;
330 v_return numeric(18,6) ;
336 COALESCE(SUM( invfifo_totalcost * (invdetail_qty / ABS(invdetail_qty)) ), 0)
343 invhist_transdate < i_transdate
345 invdetail_location_id = i_location_id
352 RETURN COALESCE(v_return ,0);
357 LANGUAGE plpgsql STABLE
359 ALTER FUNCTION invdetail_cost_location_atdate(timestamp with time zone, int )
365 CREATE OR REPLACE FUNCTION invdetail_qty_at_id(int)
366 RETURNS numeric(18,6) AS
370 v_itemsite_id INTEGER;
371 v_location_id INTEGER;
372 v_transdate timestamp with time zone;
373 v_return numeric(18,6) ;
379 invdetail_location_id,
386 LEFT JOIN invhist ON invdetail_invhist_id = invhist_id
391 SELECT COALESCE(SUM( invdetail_qty), 0) INTO v_return
393 LEFT JOIN invhist ON invdetail_invhist_id = invhist_id
394 LEFT JOIN invfifo ON invdetail_id = invfifo_invdetail_id
396 invdetail_location_id = v_location_id
398 invhist_itemsite_id = v_itemsite_id
400 ((invhist_transdate < v_transdate) OR (invhist_transdate = v_transdate AND invdetail_id <= i_id))
402 (invfifo_void IS NULL OR invfifo_void = 0);
404 IF (v_return IS NULL) THEN
412 LANGUAGE plpgsql STABLE
414 ALTER FUNCTION invdetail_qty_at_id(int)
420 -- DATE, location_id, itemsite_id
421 CREATE OR REPLACE FUNCTION invdetail_sold_atdate(timestamp with time zone, int, int)
422 RETURNS numeric(18,6) AS
425 i_transdate ALIAS FOR $1;
426 i_location_id ALIAS FOR $2;
427 i_itemsite_id ALIAS FOR $3;
428 v_return numeric(18,6) ;
433 SELECT COALESCE(SUM( invdetail_qty), 0)
440 invhist_itemsite_id = i_itemsite_id
442 invhist_transdate::date = i_transdate::date
447 invdetail_location_id = i_location_id;
450 RETURN COALESCE(v_return ,0);
455 LANGUAGE plpgsql STABLE
457 ALTER FUNCTION invdetail_sold_atdate(timestamp with time zone, int, int)
462 -- location_id, itemsite_id
463 CREATE OR REPLACE FUNCTION invdetail_sold_after(timestamp with time zone, int, int)
464 RETURNS numeric(18,6) AS
467 i_transdate ALIAS FOR $1;
468 i_location_id ALIAS FOR $2;
469 i_itemsite_id ALIAS FOR $3;
470 v_return numeric(18,6) ;
475 SELECT COALESCE(SUM( invdetail_qty), 0)
481 invhist_itemsite_id = i_itemsite_id
483 invhist_transdate::date > i_transdate::date
490 invdetail_location_id = i_location_id;
493 RETURN COALESCE(v_return ,0);
498 LANGUAGE plpgsql STABLE
500 ALTER FUNCTION invdetail_sold_after(timestamp with time zone, int, int)
507 CREATE OR REPLACE FUNCTION invdetail_balance_byitem( i_itemsite_id INTEGER, i_location_id INTEGER)
508 RETURNS NUMERIC(18,6) AS
511 v_return NUMERIC(18,6) ;
516 COALESCE(SUM(invdetail_qty),0)
524 invdetail_invhist_id = invhist_id
526 invhist_itemsite_id = i_itemsite_id
528 invdetail_location_id = i_location_id;
535 LANGUAGE plpgsql STABLE
537 ALTER FUNCTION invdetail_balance_byitem(INTEGER, INTEGER)