]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/tests/datasets/sql/env_create.sql
LP1756564: Add sample data for the booking module
[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 an address for a given actor.org_unit
9  *
10  * The "address_type" parameter accepts a TEXT value that contains the
11  * strings 'mailing', 'interlibrary', 'billing', and 'holds' to enable
12  * you to provide granular control over which address is associated for
13  * each function; if given NULL, then all functions are associated with
14  * the incoming address.
15  *
16  * This will happily create duplicate addresses if given duplicate info.
17  */
18 CREATE FUNCTION evergreen.create_aou_address
19     (owning_lib INTEGER, street1 TEXT, street2 TEXT, city TEXT, state TEXT, country TEXT,
20      post_code TEXT, address_type TEXT)
21 RETURNS void AS $$
22 BEGIN
23     INSERT INTO actor.org_address (org_unit, street1, street2, city, state, country, post_code)
24         VALUES ($1, $2, $3, $4, $5, $6, $7);
25     
26     IF $8 IS NULL THEN
27         UPDATE actor.org_unit SET holds_address = currval('actor.org_address_id_seq'), ill_address = currval('actor.org_address_id_seq'), billing_address = currval('actor.org_address_id_seq'), mailing_address = currval('actor.org_address_id_seq') WHERE id = $1;
28     END IF;
29     IF $8 ~ 'holds' THEN
30         UPDATE actor.org_unit SET holds_address = currval('actor.org_address_id_seq') WHERE id = $1;
31     END IF;
32     IF $8 ~ 'interlibrary' THEN
33         UPDATE actor.org_unit SET ill_address = currval('actor.org_address_id_seq') WHERE id = $1;
34     END IF;
35     IF $8 ~ 'billing' THEN
36         UPDATE actor.org_unit SET billing_address = currval('actor.org_address_id_seq') WHERE id = $1;
37     END IF;
38     IF $8 ~ 'mailing' THEN
39         UPDATE actor.org_unit SET mailing_address = currval('actor.org_address_id_seq') WHERE id = $1;
40     END IF;
41 END
42 $$ LANGUAGE PLPGSQL;
43
44 /**
45  * create a callnumber for every bib record in the database,
46  * appending the bib ID to the callnumber label to differentiate.
47  * If set, 'bib_tag' will limit the inserted callnumbers to bibs
48  * whose last_xact_id matches bib_tag
49  */
50 CREATE FUNCTION evergreen.populate_call_number 
51     (ownlib INTEGER, label TEXT, bib_tag TEXT, class INTEGER DEFAULT 1)
52 RETURNS void AS $$
53     INSERT INTO asset.call_number (record, creator, editor, owning_lib, label, label_class)
54         SELECT id, 1, 1, $1, $2 || id::text, $4
55         FROM biblio.record_entry
56         WHERE id > 0 AND 
57             CASE WHEN $3 IS NULL THEN TRUE 
58                 ELSE last_xact_id = $3 END;
59 $$ LANGUAGE SQL;
60
61 CREATE FUNCTION evergreen.populate_call_number 
62     (ownlib INTEGER, label TEXT, bib_tag TEXT)
63 RETURNS void AS $$
64     SELECT evergreen.populate_call_number($1, $2, $3, NULL);
65 $$ LANGUAGE SQL;
66
67 /*
68  * Each copy needs a price to be able to test lost/longoverdue and other
69  * real-life situations. Randomly generate a price between 1.99 and 25.99.
70  */
71 CREATE OR REPLACE FUNCTION evergreen.generate_price() RETURNS FLOAT AS $$
72 BEGIN
73         RETURN trunc(random() * 25 + 1) + .99;
74 END;
75 $$ LANGUAGE 'plpgsql';
76
77 /*
78  * create a copy for every callnumber in the database whose label and owning_lib 
79  * matches, appending the callnumber ID to the copy barcode to differentate.
80  */
81 CREATE FUNCTION evergreen.populate_copy 
82     (circlib INTEGER, ownlib INTEGER, barcode TEXT, label TEXT)
83 RETURNS void AS $$
84     INSERT INTO asset.copy (call_number, circ_lib, creator, editor, loan_duration, fine_level, price, barcode)
85         SELECT id, $1, 1, 1, 1, 1, (SELECT evergreen.generate_price()), $3 || id::text
86         FROM asset.call_number
87         WHERE record > 0 AND label LIKE $4 || '%' AND owning_lib = $2;
88 $$ LANGUAGE SQL;
89
90 /** Returns the next (by ID) non-deleted asset.copy */
91 CREATE FUNCTION evergreen.next_copy (copy_id BIGINT) RETURNS asset.copy AS $$
92     SELECT * FROM asset.copy 
93     WHERE id > $1 AND NOT deleted
94     ORDER BY id LIMIT 1;
95 $$ LANGUAGE SQL;
96
97 /** Returns the next (by ID) non-deleted biblio.record_entry */
98 CREATE FUNCTION evergreen.next_bib (bib_id BIGINT) RETURNS biblio.record_entry AS $$
99     SELECT * FROM biblio.record_entry
100     WHERE id > $1 AND NOT deleted
101     ORDER BY id LIMIT 1;
102 $$ LANGUAGE SQL;
103
104
105 /** Create one circulation */
106 CREATE FUNCTION evergreen.populate_circ (
107     patron_id INTEGER,
108     staff_id INTEGER,
109     copy_id BIGINT,
110     circ_lib INTEGER,
111     duration_rule TEXT,
112     recurring_fine_rule TEXT,
113     max_fine_rule TEXT,
114     overdue BOOLEAN
115 )
116
117 RETURNS void AS $$
118     DECLARE duration config.rule_circ_duration%ROWTYPE;
119     DECLARE recurring config.rule_recurring_fine%ROWTYPE;
120     DECLARE max_fine config.rule_max_fine%ROWTYPE;
121     DECLARE patron actor.usr%ROWTYPE;
122     DECLARE xact_base_date TIMESTAMP;
123     DECLARE due_date TIMESTAMP;
124     DECLARE xact_start TIMESTAMP;
125 BEGIN
126
127     SELECT INTO duration * FROM config.rule_circ_duration WHERE name = duration_rule;
128     SELECT INTO recurring * FROM config.rule_recurring_fine WHERE name = recurring_fine_rule;
129     SELECT INTO max_fine * FROM config.rule_max_fine WHERE name = max_fine_rule;
130     SELECT INTO patron * FROM actor.usr WHERE id = patron_id;
131
132     IF patron.expire_date < NOW() THEN
133         xact_base_date = patron.expire_date;
134     ELSE
135         xact_base_date = NOW();
136     END IF;
137
138     IF overdue THEN
139         -- if duration is '7 days', the overdue item was due 7 days ago
140         due_date := xact_base_date - duration.normal;
141         -- make overdue circs appear as if they were created two durations ago
142         xact_start := xact_base_date - duration.normal - duration.normal;
143     ELSE
144         due_date := xact_base_date + duration.normal;
145         xact_start := xact_base_date;
146     END IF;
147
148     IF duration.normal >= '1 day'::INTERVAL THEN
149         due_date := (DATE(due_date) || ' 23:59:59')::TIMESTAMP;
150     END IF;
151
152     INSERT INTO action.circulation (
153         xact_start, usr, target_copy, circ_lib, circ_staff, renewal_remaining,
154         grace_period, duration, recurring_fine, max_fine, duration_rule,
155         recurring_fine_rule, max_fine_rule, due_date )
156     VALUES (
157         xact_start,
158         patron_id,
159         copy_id,
160         circ_lib, 
161         staff_id,
162         duration.max_renewals,
163         recurring.grace_period,
164         duration.normal,
165         recurring.normal,
166         max_fine.amount,
167         duration.name,
168         recurring.name,
169         max_fine.name,
170         due_date
171     );
172
173     -- mark copy as checked out
174     UPDATE asset.copy SET status = 1 WHERE id = copy_id;
175
176 END;
177 $$ LANGUAGE PLPGSQL;
178
179
180
181 /** Create one hold */
182 CREATE FUNCTION evergreen.populate_hold (
183     hold_type TEXT,
184     target BIGINT,
185     patron_id INTEGER,
186     requestor INTEGER,
187     pickup_lib INTEGER,
188     frozen BOOLEAN,
189     thawdate TIMESTAMP WITH TIME ZONE,
190     holdable_formats TEXT DEFAULT NULL
191 ) RETURNS void AS $$
192 BEGIN
193     INSERT INTO action.hold_request (
194         requestor, hold_type, target, usr, pickup_lib, 
195             request_lib, selection_ou, frozen, thaw_date, holdable_formats)
196     VALUES (
197         requestor,
198         hold_type,
199         target,
200         patron_id,
201         pickup_lib,
202         pickup_lib,
203         pickup_lib,
204         frozen,
205         thawdate,
206         holdable_formats
207     );
208
209     -- Create hold notes for staff-placed holds: 1 public, 1 private
210     IF requestor != patron_id THEN
211         INSERT INTO action.hold_request_note (hold, title, body, pub, staff)
212             VALUES (
213                currval('action.hold_request_id_seq'),
214                'Public: Title of hold# ' || currval('action.hold_request_id_seq'),
215                'Public: Hold note body for ' || currval('action.hold_request_id_seq'),
216                TRUE, TRUE
217             ), (
218                currval('action.hold_request_id_seq'),
219                'Private: title of hold# ' || currval('action.hold_request_id_seq'),
220                'Private: Hold note body for ' || currval('action.hold_request_id_seq'),
221                FALSE, TRUE 
222             );
223     END IF;
224 END;
225 $$ LANGUAGE PLPGSQL;
226
227
228
229 /**
230  * Create a booking resource type for all 
231  * bib records with a specific last_xact_id
232 */
233 CREATE FUNCTION evergreen.populate_booking_resource_type        
234     (ownlib INTEGER, bib_tag TEXT)
235 RETURNS void AS $$
236     INSERT INTO booking.resource_type(name, owner, catalog_item, transferable, record)
237         SELECT TRIM (' ./' FROM (XPATH(
238                 '//marc:datafield[@tag="245"]/marc:subfield[@code="a"]/text()',
239                 marc::XML,
240                 ARRAY[ARRAY['marc', 'http://www.loc.gov/MARC21/slim']]
241         ))[1]::TEXT),
242         $1, True, True, id
243         FROM biblio.record_entry
244         WHERE id > 0 AND last_xact_id = $2;
245 $$ LANGUAGE SQL;
246
247 /**
248  * Make all items with barcodes that start 
249  * with a certain substring bookable
250 */
251
252 CREATE FUNCTION evergreen.populate_booking_resource
253     (barcode_start TEXT)
254 RETURNS void AS $$
255     INSERT INTO booking.resource(owner, type, barcode)
256         SELECT circ_lib, resource_type.id, barcode FROM asset.copy
257         INNER JOIN asset.call_number on copy.call_number=call_number.id
258         INNER JOIN booking.resource_type on call_number.record=resource_type.record
259         WHERE barcode LIKE $1 || '%';
260 $$ LANGUAGE SQL;
261