]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/tests/datasets/sql/env_create.sql
4a2970d6dfdc7355425c33ab0bfe7b85cc1a5bf3
[Evergreen.git] / Open-ILS / tests / datasets / sql / env_create.sql
1 /*
2  * 'id' gives us insert sort order
3  * 'tag' is mapped to the last_xact_id at import
4  */
5 CREATE TABLE marcxml_import (id SERIAL PRIMARY KEY, marc TEXT, tag TEXT);
6
7 /**
8  * create a callnumber for every bib record in the database,
9  * appending the bib ID to the callnumber label to differentiate.
10  * If set, 'bib_tag' will limit the inserted callnumbers to bibs
11  * whose last_xact_id matches bib_tag
12  */
13 CREATE FUNCTION evergreen.populate_call_number 
14     (ownlib INTEGER, label TEXT, bib_tag TEXT, class INTEGER DEFAULT 1)
15 RETURNS void AS $$
16     INSERT INTO asset.call_number (record, creator, editor, owning_lib, label, label_class)
17         SELECT id, 1, 1, $1, $2 || id::text, $4
18         FROM biblio.record_entry
19         WHERE id > 0 AND 
20             CASE WHEN $3 IS NULL THEN TRUE 
21                 ELSE last_xact_id = $3 END;
22 $$ LANGUAGE SQL;
23
24 CREATE FUNCTION evergreen.populate_call_number 
25     (ownlib INTEGER, label TEXT, bib_tag TEXT)
26 RETURNS void AS $$
27     SELECT evergreen.populate_call_number($1, $2, $3, NULL);
28 $$ LANGUAGE SQL;
29
30
31 /*
32  * create a copy for every callnumber in the database whose label and owning_lib 
33  * matches, appending the callnumber ID to the copy barcode to differentate.
34  */
35 CREATE FUNCTION evergreen.populate_copy 
36     (circlib INTEGER, ownlib INTEGER, barcode TEXT, label TEXT)
37 RETURNS void AS $$
38     INSERT INTO asset.copy (call_number, circ_lib, creator, editor, loan_duration, fine_level, barcode)
39         SELECT id, $1, 1, 1, 1, 1, $3 || id::text
40         FROM asset.call_number
41         WHERE record > 0 AND label LIKE $4 || '%' AND owning_lib = $2;
42 $$ LANGUAGE SQL;
43
44 /** Returns the next (by ID) non-deleted asset.copy */
45 CREATE FUNCTION evergreen.next_copy (copy_id BIGINT) RETURNS asset.copy AS $$
46     SELECT * FROM asset.copy 
47     WHERE id > $1 AND NOT deleted
48     ORDER BY id LIMIT 1;
49 $$ LANGUAGE SQL;
50
51 /** Returns the next (by ID) non-deleted biblio.record_entry */
52 CREATE FUNCTION evergreen.next_bib (bib_id BIGINT) RETURNS biblio.record_entry AS $$
53     SELECT * FROM biblio.record_entry
54     WHERE id > $1 AND NOT deleted
55     ORDER BY id LIMIT 1;
56 $$ LANGUAGE SQL;
57
58
59 /** Create one circulation */
60 CREATE FUNCTION evergreen.populate_circ (
61     patron_id INTEGER,
62     staff_id INTEGER,
63     copy_id BIGINT,
64     circ_lib INTEGER,
65     duration_rule TEXT,
66     recurring_fine_rule TEXT,
67     max_fine_rule TEXT,
68     overdue BOOLEAN
69 )
70
71 RETURNS void AS $$
72     DECLARE duration config.rule_circ_duration%ROWTYPE;
73     DECLARE recurring config.rule_recurring_fine%ROWTYPE;
74     DECLARE max_fine config.rule_max_fine%ROWTYPE;
75     DECLARE patron actor.usr%ROWTYPE;
76     DECLARE xact_base_date TIMESTAMP;
77     DECLARE due_date TIMESTAMP;
78     DECLARE xact_start TIMESTAMP;
79 BEGIN
80
81     SELECT INTO duration * FROM config.rule_circ_duration WHERE name = duration_rule;
82     SELECT INTO recurring * FROM config.rule_recurring_fine WHERE name = recurring_fine_rule;
83     SELECT INTO max_fine * FROM config.rule_max_fine WHERE name = max_fine_rule;
84     SELECT INTO patron * FROM actor.usr WHERE id = patron_id;
85
86     IF patron.expire_date < NOW() THEN
87         xact_base_date = patron.expire_date;
88     ELSE
89         xact_base_date = NOW();
90     END IF;
91
92     IF overdue THEN
93         -- if duration is '7 days', the overdue item was due 7 days ago
94         due_date := xact_base_date - duration.normal;
95         -- make overdue circs appear as if they were created two durations ago
96         xact_start := xact_base_date - duration.normal - duration.normal;
97     ELSE
98         due_date := xact_base_date + duration.normal;
99         xact_start := xact_base_date;
100     END IF;
101
102     IF duration.normal >= '1 day'::INTERVAL THEN
103         due_date := (DATE(due_date) || ' 23:59:59')::TIMESTAMP;
104     END IF;
105
106     INSERT INTO action.circulation (
107         xact_start, usr, target_copy, circ_lib, circ_staff, renewal_remaining,
108         grace_period, duration, recurring_fine, max_fine, duration_rule,
109         recurring_fine_rule, max_fine_rule, due_date )
110     VALUES (
111         xact_start,
112         patron_id,
113         copy_id,
114         circ_lib, 
115         staff_id,
116         duration.max_renewals,
117         recurring.grace_period,
118         duration.normal,
119         recurring.normal,
120         max_fine.amount,
121         duration.name,
122         recurring.name,
123         max_fine.name,
124         due_date
125     );
126 END;
127 $$ LANGUAGE PLPGSQL;
128
129
130
131 /** Create one hold */
132 CREATE FUNCTION evergreen.populate_hold (
133     hold_type TEXT,
134     target BIGINT,
135     patron_id INTEGER,
136     requestor INTEGER,
137     pickup_lib INTEGER,
138     frozen BOOLEAN,
139     thawdate TIMESTAMP WITH TIME ZONE
140 ) RETURNS void AS $$
141 BEGIN
142     INSERT INTO action.hold_request (
143         requestor, hold_type, target, usr, pickup_lib, 
144             request_lib, selection_ou, frozen, thaw_date)
145     VALUES (
146         requestor,
147         hold_type,
148         target,
149         patron_id,
150         pickup_lib,
151         pickup_lib,
152         pickup_lib,
153         frozen,
154         thawdate
155     );
156 END;
157 $$ LANGUAGE PLPGSQL;
158
159
160
161