]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0101.schema.circ-chain.sql
LP1079041 - making state not required (continued)
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0101.schema.circ-chain.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0101');
4
5 -- represents a circ chain summary
6 CREATE TYPE action.circ_chain_summary AS (
7     num_circs INTEGER,
8     start_time TIMESTAMP WITH TIME ZONE,
9     checkout_workstation TEXT,
10     last_renewal_time TIMESTAMP WITH TIME ZONE, -- NULL if no renewals
11     last_stop_fines TEXT,
12     last_stop_fines_time TIMESTAMP WITH TIME ZONE,
13     last_renewal_workstation TEXT, -- NULL if no renewals
14     last_checkin_workstation TEXT,
15     last_checkin_time TIMESTAMP WITH TIME ZONE,
16     last_checkin_scan_time TIMESTAMP WITH TIME ZONE
17 );
18
19
20 CREATE OR REPLACE FUNCTION action.circ_chain ( ctx_circ_id INTEGER ) RETURNS SETOF action.circulation AS $$
21 DECLARE
22     tmp_circ action.circulation%ROWTYPE;
23     circ_0 action.circulation%ROWTYPE;
24 BEGIN
25
26     SELECT INTO tmp_circ * FROM action.circulation WHERE id = ctx_circ_id;
27
28     IF tmp_circ IS NULL THEN
29         RETURN NEXT tmp_circ;
30     END IF;
31     circ_0 := tmp_circ;
32
33     -- find the front of the chain
34     WHILE TRUE LOOP
35         SELECT INTO tmp_circ * FROM action.circulation WHERE id = tmp_circ.parent_circ;
36         IF tmp_circ IS NULL THEN
37             EXIT;
38         END IF;
39         circ_0 := tmp_circ;
40     END LOOP;
41
42     -- now send the circs to the caller, oldest to newest
43     tmp_circ := circ_0;
44     WHILE TRUE LOOP
45         IF tmp_circ IS NULL THEN
46             EXIT;
47         END IF;
48         RETURN NEXT tmp_circ;
49         SELECT INTO tmp_circ * FROM action.circulation WHERE parent_circ = tmp_circ.id;
50     END LOOP;
51
52 END;
53 $$ LANGUAGE 'plpgsql';
54
55 CREATE OR REPLACE FUNCTION action.summarize_circ_chain ( ctx_circ_id INTEGER ) RETURNS action.circ_chain_summary AS $$
56
57 DECLARE
58
59     -- first circ in the chain
60     circ_0 action.circulation%ROWTYPE;
61
62     -- last circ in the chain
63     circ_n action.circulation%ROWTYPE;
64
65     -- circ chain under construction
66     chain action.circ_chain_summary;
67     tmp_circ action.circulation%ROWTYPE;
68
69 BEGIN
70     
71     chain.num_circs := 0;
72     FOR tmp_circ IN SELECT * FROM action.circ_chain(ctx_circ_id) LOOP
73
74         IF chain.num_circs = 0 THEN
75             circ_0 := tmp_circ;
76         END IF;
77
78         chain.num_circs := chain.num_circs + 1;
79         circ_n := tmp_circ;
80     END LOOP;
81
82     chain.start_time := circ_0.xact_start;
83     chain.last_stop_fines := circ_n.stop_fines;
84     chain.last_stop_fines_time := circ_n.stop_fines_time;
85     chain.last_checkin_time := circ_n.checkin_time;
86     chain.last_checkin_scan_time := circ_n.checkin_scan_time;
87     SELECT INTO chain.checkout_workstation name FROM actor.workstation WHERE id = circ_0.workstation;
88     SELECT INTO chain.last_checkin_workstation name FROM actor.workstation WHERE id = circ_n.checkin_workstation;
89
90     IF chain.num_circs > 1 THEN
91         chain.last_renewal_time := circ_n.xact_start;
92         SELECT INTO chain.last_renewal_workstation name FROM actor.workstation WHERE id = circ_n.workstation;
93     END IF;
94
95     RETURN chain;
96
97 END;
98 $$ LANGUAGE 'plpgsql';
99
100
101 COMMIT;
102
103