]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/tests/datasets/sql/env_create.sql
d27d69857ed4598bf88f04062ffb5e13d7a4154e
[working/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)
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, 1
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 /*
25  * create a copy for every callnumber in the database whose label and owninb_lib 
26  * matches, appending the callnumber ID to the copy barcode to differentate.
27  */
28 CREATE FUNCTION evergreen.populate_copy 
29     (circlib INTEGER, ownlib INTEGER, barcode TEXT, label TEXT)
30 RETURNS void AS $$
31     INSERT INTO asset.copy (call_number, circ_lib, creator, editor, loan_duration, fine_level, barcode)
32         SELECT id, $1, 1, 1, 1, 1, $3 || id::text
33         FROM asset.call_number
34         WHERE record > 0 AND label LIKE $4 || '%' AND owning_lib = $2;
35 $$ LANGUAGE SQL;
36
37 /** Returns the next (by ID) non-deleted asset.copy */
38 CREATE FUNCTION evergreen.next_copy (copy_id BIGINT) RETURNS asset.copy AS $$
39     SELECT * FROM asset.copy 
40     WHERE id > $1 AND NOT deleted
41     ORDER BY id LIMIT 1;
42 $$ LANGUAGE SQL;
43
44 /** Returns the next (by ID) non-deleted biblio.record_entry */
45 CREATE FUNCTION evergreen.next_bib (bib_id BIGINT) RETURNS biblio.record_entry AS $$
46     SELECT * FROM biblio.record_entry
47     WHERE id > $1 AND NOT deleted
48     ORDER BY id LIMIT 1;
49 $$ LANGUAGE SQL;
50
51
52 /** Create one circulation */
53 CREATE FUNCTION evergreen.populate_circ (
54     patron_id INTEGER,
55     staff_id INTEGER,
56     copy_id BIGINT,
57     circ_lib INTEGER,
58     duration_rule TEXT,
59     recurring_fine_rule TEXT,
60     max_fine_rule TEXT,
61     overdue BOOLEAN
62 )
63
64 RETURNS void AS $$
65     DECLARE duration config.rule_circ_duration%ROWTYPE;
66     DECLARE recurring config.rule_recurring_fine%ROWTYPE;
67     DECLARE max_fine config.rule_max_fine%ROWTYPE;
68     DECLARE due_date TIMESTAMP;
69     DECLARE xact_start TIMESTAMP;
70 BEGIN
71
72     SELECT INTO duration * FROM config.rule_circ_duration WHERE name = duration_rule;
73     SELECT INTO recurring * FROM config.rule_recurring_fine WHERE name = recurring_fine_rule;
74     SELECT INTO max_fine * FROM config.rule_max_fine WHERE name = max_fine_rule;
75
76     IF overdue THEN
77         -- if duration is '7 days', the overdue item was due 7 days ago
78         due_date := NOW() - duration.normal;
79         -- make overdue circs appear as if they were created two durations ago
80         xact_start := NOW() - duration.normal - duration.normal;
81     ELSE
82         due_date := NOW() + duration.normal;
83         xact_start := NOW();
84     END IF;
85
86     IF duration.normal >= '1 day'::INTERVAL THEN
87         due_date := (DATE(due_date) || ' 23:59:59')::TIMESTAMP;
88     END IF;
89
90     INSERT INTO action.circulation (
91         xact_start, usr, target_copy, circ_lib, circ_staff, renewal_remaining,
92         grace_period, duration, recurring_fine, max_fine, duration_rule,
93         recurring_fine_rule, max_fine_rule, due_date )
94     VALUES (
95         xact_start,
96         patron_id,
97         copy_id,
98         circ_lib, 
99         staff_id,
100         duration.max_renewals,
101         recurring.grace_period,
102         duration.normal,
103         recurring.normal,
104         max_fine.amount,
105         duration.name,
106         recurring.name,
107         max_fine.name,
108         due_date
109     );
110 END;
111 $$ LANGUAGE PLPGSQL;
112
113
114
115 /** Create one hold */
116 CREATE FUNCTION evergreen.populate_hold (
117     hold_type TEXT,
118     target BIGINT,
119     patron_id INTEGER,
120     requestor INTEGER,
121     pickup_lib INTEGER,
122     frozen BOOLEAN,
123     thawdate TIMESTAMP WITH TIME ZONE
124 ) RETURNS void AS $$
125 BEGIN
126     INSERT INTO action.hold_request (
127         requestor, hold_type, target, usr, pickup_lib, 
128             request_lib, selection_ou, frozen, thaw_date)
129     VALUES (
130         requestor,
131         hold_type,
132         target,
133         patron_id,
134         pickup_lib,
135         pickup_lib,
136         pickup_lib,
137         frozen,
138         thawdate
139     );
140 END;
141 $$ LANGUAGE PLPGSQL;
142
143
144
145