2 * 'id' gives us insert sort order
3 * 'tag' is mapped to the last_xact_id at import
5 CREATE TABLE marcxml_import (id SERIAL PRIMARY KEY, marc TEXT, tag TEXT);
8 * Create an address for a given actor.org_unit
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.
16 * This will happily create duplicate addresses if given duplicate info.
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)
23 INSERT INTO actor.org_address (org_unit, street1, street2, city, state, country, post_code)
24 VALUES ($1, $2, $3, $4, $5, $6, $7);
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;
30 UPDATE actor.org_unit SET holds_address = currval('actor.org_address_id_seq') WHERE id = $1;
32 IF $8 ~ 'interlibrary' THEN
33 UPDATE actor.org_unit SET ill_address = currval('actor.org_address_id_seq') WHERE id = $1;
35 IF $8 ~ 'billing' THEN
36 UPDATE actor.org_unit SET billing_address = currval('actor.org_address_id_seq') WHERE id = $1;
38 IF $8 ~ 'mailing' THEN
39 UPDATE actor.org_unit SET mailing_address = currval('actor.org_address_id_seq') WHERE id = $1;
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
50 CREATE FUNCTION evergreen.populate_call_number
51 (ownlib INTEGER, label TEXT, bib_tag TEXT, class INTEGER DEFAULT 1)
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
57 CASE WHEN $3 IS NULL THEN TRUE
58 ELSE last_xact_id = $3 END;
61 CREATE FUNCTION evergreen.populate_call_number
62 (ownlib INTEGER, label TEXT, bib_tag TEXT)
64 SELECT evergreen.populate_call_number($1, $2, $3, NULL);
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.
71 CREATE OR REPLACE FUNCTION evergreen.generate_price() RETURNS FLOAT AS $$
73 RETURN trunc(random() * 25 + 1) + .99;
75 $$ LANGUAGE 'plpgsql';
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.
81 CREATE FUNCTION evergreen.populate_copy
82 (circlib INTEGER, ownlib INTEGER, barcode TEXT, label TEXT)
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;
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
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
105 /** Create one circulation */
106 CREATE FUNCTION evergreen.populate_circ (
112 recurring_fine_rule TEXT,
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;
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;
132 IF patron.expire_date < NOW() THEN
133 xact_base_date = patron.expire_date;
135 xact_base_date = NOW();
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;
144 due_date := xact_base_date + duration.normal;
145 xact_start := xact_base_date;
148 IF duration.normal >= '1 day'::INTERVAL THEN
149 due_date := (DATE(due_date) || ' 23:59:59')::TIMESTAMP;
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 )
162 duration.max_renewals,
163 recurring.grace_period,
173 -- mark copy as checked out
174 UPDATE asset.copy SET status = 1 WHERE id = copy_id;
181 /** Create one hold */
182 CREATE FUNCTION evergreen.populate_hold (
189 thawdate TIMESTAMP WITH TIME ZONE,
190 holdable_formats TEXT DEFAULT NULL
193 INSERT INTO action.hold_request (
194 requestor, hold_type, target, usr, pickup_lib,
195 request_lib, selection_ou, frozen, thaw_date, holdable_formats)
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)
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'),
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'),
230 * Create a booking resource type for all
231 * bib records with a specific last_xact_id
233 CREATE FUNCTION evergreen.populate_booking_resource_type
234 (ownlib INTEGER, bib_tag TEXT)
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()',
240 ARRAY[ARRAY['marc', 'http://www.loc.gov/MARC21/slim']]
243 FROM biblio.record_entry
244 WHERE id > 0 AND last_xact_id = $2;
248 * Make all items with barcodes that start
249 * with a certain substring bookable
252 CREATE FUNCTION evergreen.populate_booking_resource
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 || '%';