2 -- loc bal is disabled at present - it's slow, and does not help much..
5 CREATE SEQUENCE locbal_id_seq
11 GRANT ALL ON SEQUENCE locbal_id_seq TO xtrole;
15 locbal_id INTEGER NOT NULL DEFAULT nextval(('locbal_id_seq'::text)::regclass),
16 locbal_period_id integer,
17 locbal_location_id integer,
19 locbal_itemsite_id integer,
21 locbal_beginning numeric(20,2),
22 locbal_ending numeric(20,2),
24 locbal_credits numeric(20,2),
25 locbal_debits numeric(20,2),
27 CONSTRAINT locbal_itemsite_id_fkey FOREIGN KEY (locbal_itemsite_id)
28 REFERENCES itemsite (itemsite_id) MATCH SIMPLE,
31 CONSTRAINT locbal_period_id_fkey FOREIGN KEY (locbal_period_id)
32 REFERENCES period (period_id) MATCH SIMPLE,
34 CONSTRAINT locbal_location_id_fkey FOREIGN KEY (locbal_location_id)
35 REFERENCES location (location_id),
36 CONSTRAINT locbal_id_pkey PRIMARY KEY (locbal_id)
39 CREATE UNIQUE INDEX locbal_locsite_idx ON locbal USING btree (locbal_period_id, locbal_location_id, locbal_itemsite_id );
43 ALTER TABLE public.locbal OWNER TO admin;
44 GRANT ALL ON TABLE locbal TO xtrole;
45 COMMENT ON TABLE locbal IS 'Location Balance information';
47 CREATE SEQUENCE loccurbal_id_seq
54 GRANT ALL ON TABLE loccurbal_id_seq TO xtrole;
56 CREATE TABLE loccurbal (
57 loccurbal_id INTEGER NOT NULL DEFAULT nextval(('loccurbal_id_seq'::text)::regclass),
58 loccurbal_location_id integer,
59 loccurbal_itemsite_id integer,
60 loccurbal_ending numeric(20,2),
62 CONSTRAINT loccurbal_itemsite_id_fkey FOREIGN KEY (loccurbal_itemsite_id)
63 REFERENCES itemsite (itemsite_id) MATCH SIMPLE,
64 CONSTRAINT loccurbal_location_id_fkey FOREIGN KEY (loccurbal_location_id)
65 REFERENCES location (location_id),
66 CONSTRAINT loccurbal_id_pkey PRIMARY KEY (loccurbal_id)
69 ALTER TABLE public.loccurbal OWNER TO admin;
71 GRANT ALL ON TABLE loccurbal TO xtrole;
73 COMMENT ON TABLE loccurbal IS 'Location Balance current';
76 CREATE UNIQUE INDEX loccurbal_locsite_idx ON loccurbal USING btree (loccurbal_location_id, loccurbal_itemsite_id );
85 -- SELECT distinct(invhist_itemsite_id), invdetaillocation_id, NOW() FROM invdetailview GROUP BY invhist_itemsite_id LIMIT 100;
89 DROP FUNCTION locbal_update_location_itemsite(integer, integer, date );
90 CREATE OR REPLACE FUNCTION locbal_update_location_itemsite(integer, integer, date )
93 -- Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple.
94 -- See www.xtuple.com/CPAL for the full text of the software license.
96 i_location_id ALIAS FOR $1;
97 i_itemsite_id ALIAS FOR $2;
106 --RAISE NOTICE 'update locbal(a) % , %, %',i_location_id, i_itemsite_id, i_date;
114 period_start <= i_date
116 period_end >= i_date;
119 RAISE NOTICE 'no period found';
123 -- make sure there are some transactions for that item/location
131 invhist_itemsite_id = i_itemsite_id
133 invdetail_location_id = i_location_id
139 RAISE NOTICE 'no transactions found';
144 --RAISE NOTICE 'locbal_update_location_itemsite_period( % , %, %) ',i_location_id, i_itemsite_id, v_id;
146 -- this handle current and before..
148 locbal_update_location_itemsite_period(i_location_id, i_itemsite_id, v_id)
152 -- we should update after...
155 -- finally update the total..
156 --RAISE NOTICE 'locbal_update_begin_end % ',i_date ;
159 locbal_update_begin_end(locbal_id)
165 locbal_period_id = period_id
167 period_start > i_date
169 locbal_itemsite_id = i_itemsite_id
171 locbal_location_id = i_location_id
176 --RAISE NOTICE 'loccurbal_update( % , % ) ',i_location_id, i_itemsite_id ;
177 ---- updates loccurbal find final locbal_id
178 PERFORM loccurbal_update(i_location_id, i_itemsite_id);
184 LANGUAGE plpgsql VOLATILE
186 ALTER FUNCTION locbal_update_location_itemsite(integer, integer, date )
193 CREATE OR REPLACE FUNCTION locbal_update_begin_end(integer )
196 -- Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple.
197 -- See www.xtuple.com/CPAL for the full text of the software license.
200 v_location_id INTEGER;
201 v_itemsite_id INTEGER;
222 locbal_period_id = period_id
238 locbal_period_id = period_id
242 period_start < v_period_start
244 locbal_location_id = v_location_id
246 locbal_itemsite_id = v_itemsite_id
252 -- RAISE NOTICE 'no more previous periods';
259 locbal_beginning = v_balance,
260 locbal_ending = v_balance + locbal_credits - locbal_debits
267 LANGUAGE plpgsql VOLATILE
269 ALTER FUNCTION locbal_update_begin_end(integer )
277 CREATE OR REPLACE FUNCTION loccurbal_update(integer, integer)
280 -- Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple.
281 -- See www.xtuple.com/CPAL for the full text of the software license.
284 i_location_id ALIAS FOR $1;
285 i_itemsite_id ALIAS FOR $2;
297 COALESCE(locbal_ending, 0)
305 locbal_period_id = period_id
308 locbal_location_id = i_location_id
310 locbal_itemsite_id = i_itemsite_id
326 loccurbal_location_id = i_location_id
328 loccurbal_itemsite_id = i_itemsite_id
332 INSERT INTO loccurbal (
333 loccurbal_location_id,
334 loccurbal_itemsite_id,
348 loccurbal_ending = v_balance
355 LANGUAGE plpgsql VOLATILE
357 ALTER FUNCTION loccurbal_update(integer, integer)
365 CREATE OR REPLACE FUNCTION locbal_update_location_itemsite_period(integer,integer, integer)
368 -- Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple.
369 -- See www.xtuple.com/CPAL for the full text of the software license.
371 i_location_id ALIAS FOR $1;
372 i_itemsite_id ALIAS FOR $2;
373 i_period_id ALIAS FOR $3;
377 v_prev_period_id INTEGER;
388 --RAISE NOTICE 'SELECT locbal_update_location_itemsite_period (% , %, %)',i_location_id, i_itemsite_id, i_period_id;
398 period_id = i_period_id;
400 -- check to see if we have a value for the previous period.
410 period_start < v_start
412 period_id != i_period_id
418 -- RAISE NOTICE 'no more previous periods';
421 -- find out if we have a balance for the previous period..
430 locbal_location_id = i_location_id
432 locbal_itemsite_id = i_itemsite_id
434 locbal_period_id = v_prev_period_id;
442 locbal_update_location_itemsite_period(i_location_id,i_itemsite_id, v_prev_period_id)
455 COALESCE(SUM( CASE WHEN invdetail_qty > 0 THEN invdetail_qty ELSE 0 END) , 0) ,
456 COALESCE(SUM( CASE WHEN invdetail_qty < 0 THEN invdetail_qty * -1 ELSE 0 END) , 0)
464 invhist_transdate >= v_start
466 invhist_transdate < v_end + INTERVAL '1 DAY'
468 invhist_itemsite_id = i_itemsite_id
470 invdetail_location_id = i_location_id
480 -- do we have a current record...
489 locbal_location_id = i_location_id
491 locbal_itemsite_id = i_itemsite_id
493 locbal_period_id = i_period_id;
496 --RAISE NOTICE 'adding locbal';
516 v_balance + v_credit - v_debit,
523 --RAISE NOTICE 'updating locbal';
526 locbal_beginning = v_balance,
527 locbal_ending = v_balance + v_credit - v_debit ,
529 locbal_credits = v_credit,
530 locbal_debits = v_debit
536 RETURN v_balance + v_credit - v_debit ;
540 LANGUAGE plpgsql VOLATILE
542 ALTER FUNCTION locbal_update_location_itemsite_period(integer,integer, integer)
548 --SELECT locbal_update_location_itemsite(a,b,c)
550 -- SELECT distinct(invhist_itemsite_id) b, invdetail_location_id a, MAX(invhist_transdate)::date c
551 -- FROM invdetailview
553 -- GROUP BY invhist_itemsite_id, invdetail_location_id ) d;
560 CREATE OR REPLACE FUNCTION _invhist_locbaltrigger()
567 IF (TG_OP = 'INSERT') THEN
568 SELECT locbal_update_location_itemsite(NEW.invdetail_location_id, invhist_itemsite_id, invhist_transdate::date)
570 FROM invhist WHERE invhist_id = NEW.invdetail_invhist_id;
574 IF (TG_OP = 'UPDATE') THEN
576 SELECT locbal_update_location_itemsite(NEW.invdetail_location_id, invhist_itemsite_id, invhist_transdate::date)
578 FROM invhist WHERE invhist_id = NEW.invdetail_invhist_id;
580 SELECT locbal_update_location_itemsite(OLD.invdetail_location_id, invhist_itemsite_id, invhist_transdate::date)
582 FROM invhist WHERE invhist_id = OLD.invdetail_invhist_id;
587 IF (TG_OP = 'DELETE') THEN
588 SELECT locbal_update_location_itemsite(OLD.invdetail_location_id, invhist_itemsite_id, invhist_transdate::date)
590 FROM invhist WHERE invhist_id = OLD.invdetail_invhist_id;
599 LANGUAGE plpgsql VOLATILE
601 ALTER FUNCTION _invhist_locbaltrigger()
604 --CREATE TRIGGER invhist_locbaltrigger
605 -- AFTER INSERT OR UPDATE OR DELETE
608 -- EXECUTE PROCEDURE _invhist_locbaltrigger();
610 DROP TRIGGER invhist_locbaltrigger ON invdetail;