4 -- NOTE: current config.item_type should get sip2_media_type and magnetic_media columns
6 -- New table needed to handle circ modifiers inside the DB. Will still require
7 -- central admin. The circ_modifier column on asset.copy will become an fkey to this table.
8 CREATE TABLE config.circ_modifier (
10 name TEXT UNIQUE NOT NULL,
11 description TEXT NOT NULL,
12 sip2_media_type TEXT NOT NULL,
13 magnetic_media BOOL NOT NULL DEFAULT TRUE
18 INSERT INTO config.circ_modifier VALUES ( 'DVD', 'DVD', 'um ... DVDs', '001', FALSE );
19 INSERT INTO config.circ_modifier VALUES ( 'VIDEO', 'VIDEO', 'Tapes', '001', TRUE );
20 INSERT INTO config.circ_modifier VALUES ( 'BOOK', 'BOOK', 'Dead tree', '001', FALSE );
21 INSERT INTO config.circ_modifier VALUES ( 'CRAZY_ARL-ATH_SETTING', 'R2R_TAPE', 'reel2reel tape', '007', TRUE );
24 -- But, just to get us started, use this
27 UPDATE asset.copy SET circ_modifier = UPPER(circ_modifier) WHERE circ_modifier IS NOT NULL AND circ_modifier <> '';
28 UPDATE asset.copy SET circ_modifier = NULL WHERE circ_modifier = '';
30 INSERT INTO config.circ_modifier (code, name, description, sip2_media_type )
37 WHERE circ_modifier IS NOT NULL;
41 -- add an fkey pointing to the new circ mod table
42 ALTER TABLE asset.copy ADD CONSTRAINT circ_mod_fkey FOREIGN KEY (circ_modifier) REFERENCES config.circ_modifier (code) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED;
44 -- config table to hold the vr_format names
45 CREATE TABLE config.videorecording_format_map (
46 code TEXT PRIMARY KEY,
50 INSERT INTO config.videorecording_format_map VALUES ('a','Beta');
51 INSERT INTO config.videorecording_format_map VALUES ('b','VHS');
52 INSERT INTO config.videorecording_format_map VALUES ('c','U-matic');
53 INSERT INTO config.videorecording_format_map VALUES ('d','EIAJ');
54 INSERT INTO config.videorecording_format_map VALUES ('e','Type C');
55 INSERT INTO config.videorecording_format_map VALUES ('f','Quadruplex');
56 INSERT INTO config.videorecording_format_map VALUES ('g','Laserdisc');
57 INSERT INTO config.videorecording_format_map VALUES ('h','CED');
58 INSERT INTO config.videorecording_format_map VALUES ('i','Betacam');
59 INSERT INTO config.videorecording_format_map VALUES ('j','Betacam SP');
60 INSERT INTO config.videorecording_format_map VALUES ('k','Super-VHS');
61 INSERT INTO config.videorecording_format_map VALUES ('m','M-II');
62 INSERT INTO config.videorecording_format_map VALUES ('o','D-2');
63 INSERT INTO config.videorecording_format_map VALUES ('p','8 mm.');
64 INSERT INTO config.videorecording_format_map VALUES ('q','Hi-8 mm.');
65 INSERT INTO config.videorecording_format_map VALUES ('u','Unknown');
66 INSERT INTO config.videorecording_format_map VALUES ('v','DVD');
67 INSERT INTO config.videorecording_format_map VALUES ('z','Other');
72 ** Here we define the tables that make up the circ matrix. Conceptually, this implements
73 ** the "sparse matrix" that everyone talks about, instead of using traditional rules logic.
74 ** Physically, we cut the matrix up into separate tables (almost 3rd normal form!) that handle
75 ** different portions of the matrix. This wil simplify creation of the UI (I hope), and help the
76 ** developers focus on specific parts of the matrix.
81 -- ****** Which ruleset and tests to use *******
83 -- * Most specific range for org_unit and grp wins.
85 -- * circ_modifier match takes precidence over marc_type match, if circ_modifier is set here
87 -- * marc_type is first checked against the circ_as_type from the copy, then the item type from the marc record
89 -- * If neither circ_modifier nor marc_type is set (both are NULLABLE) then the entry defines the default
90 -- ruleset and tests for the OU + group (like BOOK in PINES)
93 CREATE TABLE config.circ_matrix_matchpoint (
94 id SERIAL PRIMARY KEY,
95 active BOOL NOT NULL DEFAULT TRUE,
96 org_unit INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED, -- Set to the top OU for the matchpoint applicability range; we can use org_unit_prox to choose the "best"
97 grp INT NOT NULL REFERENCES permission.grp_tree (id) DEFERRABLE INITIALLY DEFERRED, -- Set to the top applicable group from the group tree; will need descendents and prox functions for filtering
98 circ_modifier TEXT REFERENCES config.circ_modifier (code) DEFERRABLE INITIALLY DEFERRED,
99 marc_type TEXT REFERENCES config.item_type_map (code) DEFERRABLE INITIALLY DEFERRED,
100 marc_form TEXT REFERENCES config.item_form_map (code) DEFERRABLE INITIALLY DEFERRED,
101 marc_vr_format TEXT REFERENCES config.videorecording_format_map (code) DEFERRABLE INITIALLY DEFERRED,
104 usr_age_lower_bound INTERVAL,
105 usr_age_upper_bound INTERVAL,
106 CONSTRAINT ep_once_per_grp_loc_mod_marc UNIQUE (grp, org_unit, circ_modifier, marc_type, marc_form, marc_vr_format, ref_flag, usr_age_lower_bound, usr_age_upper_bound, is_renewal)
110 -- Tests to determine if circ can occur for this item at this location for this patron
111 CREATE TABLE config.circ_matrix_test (
112 matchpoint INT PRIMARY KEY NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
113 circulate BOOL NOT NULL DEFAULT TRUE, -- Hard "can't circ" flag requiring an override
114 max_items_out INT, -- Total current active circulations must be less than this, NULL means skip (always pass)
115 max_overdue INT, -- Total overdue active circulations must be less than this, NULL means skip (always pass)
116 max_fines NUMERIC(8,2), -- Total fines owed must be less than this, NULL means skip (always pass)
117 script_test TEXT -- filename or javascript source ??
120 -- Tests for max items out by circ_modifier
121 CREATE TABLE config.circ_matrix_circ_mod_test (
122 id SERIAL PRIMARY KEY,
123 matchpoint INT NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
124 items_out INT NOT NULL, -- Total current active circulations must be less than this, NULL means skip (always pass)
125 circ_mod TEXT NOT NULL REFERENCES config.circ_modifier (code) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED-- circ_modifier type that the max out applies to
129 -- How to circ, assuming tests pass
130 CREATE TABLE config.circ_matrix_ruleset (
131 matchpoint INT PRIMARY KEY REFERENCES config.circ_matrix_matchpoint (id) DEFERRABLE INITIALLY DEFERRED,
132 duration_rule INT NOT NULL REFERENCES config.rule_circ_duration (id) DEFERRABLE INITIALLY DEFERRED,
133 recurring_fine_rule INT NOT NULL REFERENCES config.rule_recuring_fine (id) DEFERRABLE INITIALLY DEFERRED,
134 max_fine_rule INT NOT NULL REFERENCES config.rule_max_fine (id) DEFERRABLE INITIALLY DEFERRED
137 CREATE OR REPLACE FUNCTION action.find_circ_matrix_matchpoint( context_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS INT AS $func$
139 current_group permission.grp_tree%ROWTYPE;
140 user_object actor.usr%ROWTYPE;
141 item_object asset.copy%ROWTYPE;
142 rec_descriptor metabib.rec_descriptor%ROWTYPE;
143 current_mp config.circ_matrix_matchpoint%ROWTYPE;
144 matchpoint config.circ_matrix_matchpoint%ROWTYPE;
146 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
147 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
148 SELECT INTO rec_descriptor r.* FROM metabib.rec_descriptor r JOIN asset.call_number c USING (record) WHERE c.id = item_object.call_number;
149 SELECT INTO current_group * FROM permission.grp_tree WHERE id = user_object.profile;
152 -- for each potential matchpoint for this ou and group ...
155 FROM config.circ_matrix_matchpoint m
156 JOIN actor.org_unit_ancestors( context_ou ) d ON (m.org_unit = d.id)
157 LEFT JOIN actor.org_unit_proximity p ON (p.from_org = context_ou AND p.to_org = d.id)
158 WHERE m.grp = current_group.id AND m.active
159 ORDER BY CASE WHEN p.prox IS NULL THEN 999 ELSE p.prox END,
160 CASE WHEN m.is_renewal = renewal THEN 64 ELSE 0 END +
161 CASE WHEN m.circ_modifier IS NOT NULL THEN 32 ELSE 0 END +
162 CASE WHEN m.marc_type IS NOT NULL THEN 16 ELSE 0 END +
163 CASE WHEN m.marc_form IS NOT NULL THEN 8 ELSE 0 END +
164 CASE WHEN m.marc_vr_format IS NOT NULL THEN 4 ELSE 0 END +
165 CASE WHEN m.ref_flag IS NOT NULL THEN 2 ELSE 0 END +
166 CASE WHEN m.usr_age_lower_bound IS NOT NULL THEN 0.5 ELSE 0 END +
167 CASE WHEN m.usr_age_upper_bound IS NOT NULL THEN 0.5 ELSE 0 END DESC LOOP
169 IF current_mp.circ_modifier IS NOT NULL THEN
170 CONTINUE WHEN current_mp.circ_modifier <> item_object.circ_modifier;
173 IF current_mp.marc_type IS NOT NULL THEN
174 IF item_object.circ_as_type IS NOT NULL THEN
175 CONTINUE WHEN current_mp.marc_type <> item_object.circ_as_type;
177 CONTINUE WHEN current_mp.marc_type <> rec_descriptor.item_type;
181 IF current_mp.marc_form IS NOT NULL THEN
182 CONTINUE WHEN current_mp.marc_form <> rec_descriptor.item_form;
185 IF current_mp.marc_vr_format IS NOT NULL THEN
186 CONTINUE WHEN current_mp.marc_vr_format <> rec_descriptor.vr_format;
189 IF current_mp.ref_flag IS NOT NULL THEN
190 CONTINUE WHEN current_mp.ref_flag <> item_object.ref;
193 IF current_mp.usr_age_lower_bound IS NOT NULL THEN
194 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_lower_bound < age(user_object.dob);
197 IF current_mp.usr_age_upper_bound IS NOT NULL THEN
198 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_upper_bound > age(user_object.dob);
202 -- everything was undefined or matched
203 matchpoint = current_mp;
205 EXIT WHEN matchpoint.id IS NOT NULL;
208 EXIT WHEN current_group.parent IS NULL OR matchpoint.id IS NOT NULL;
210 SELECT INTO current_group * FROM permission.grp_tree WHERE id = current_group.parent;
213 RETURN matchpoint.id;
215 $func$ LANGUAGE plpgsql;
218 CREATE TYPE action.matrix_test_result AS ( success BOOL, matchpoint INT, fail_part TEXT );
219 CREATE OR REPLACE FUNCTION action.item_user_circ_test( circ_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS SETOF action.matrix_test_result AS $func$
222 user_object actor.usr%ROWTYPE;
223 item_object asset.copy%ROWTYPE;
224 item_status_object config.copy_status%ROWTYPE;
225 item_location_object asset.copy_location%ROWTYPE;
226 result action.matrix_test_result;
227 circ_test config.circ_matrix_test%ROWTYPE;
228 out_by_circ_mod config.circ_matrix_circ_mod_test%ROWTYPE;
231 current_fines NUMERIC(8,2) := 0.0;
232 tmp_fines NUMERIC(8,2);
236 result.success := TRUE;
238 -- Fail if the user is BARRED
239 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
241 -- Fail if we couldn't find a set of tests
242 IF user_object.id IS NULL THEN
243 result.fail_part := 'no_user';
244 result.success := FALSE;
250 IF user_object.barred IS TRUE THEN
251 result.fail_part := 'actor.usr.barred';
252 result.success := FALSE;
257 -- Fail if the item can't circulate
258 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
259 IF item_object.circulate IS FALSE THEN
260 result.fail_part := 'asset.copy.circulate';
261 result.success := FALSE;
266 -- Fail if the item isn't in a circulateable status on a non-renewal
267 IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN
268 result.fail_part := 'asset.copy.status';
269 result.success := FALSE;
272 ELSIF renewal AND item_object.status <> 1 THEN
273 result.fail_part := 'asset.copy.status';
274 result.success := FALSE;
279 -- Fail if the item can't circulate because of the shelving location
280 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
281 IF item_location_object.circulate IS FALSE THEN
282 result.fail_part := 'asset.copy_location.circulate';
283 result.success := FALSE;
288 SELECT INTO matchpoint_id action.find_circ_matrix_matchpoint(circ_ou, match_item, match_user, renewal);
289 result.matchpoint := matchpoint_id;
291 -- Fail if we couldn't find a set of tests
292 IF result.matchpoint IS NULL THEN
293 result.fail_part := 'no_matchpoint';
294 result.success := FALSE;
299 -- Fail if the test is set to hard non-circulating
300 IF circ_test.circulate IS FALSE THEN
301 result.fail_part := 'config.circ_matrix_test.circulate';
302 result.success := FALSE;
307 -- Fail if the user has too many items checked out
308 IF circ_test.max_items_out IS NOT NULL THEN
309 SELECT INTO items_out COUNT(*)
310 FROM action.circulation
311 WHERE usr = match_user
312 AND checkin_time IS NULL
313 AND (stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE') OR stop_fines IS NULL);
314 IF items_out >= circ_test.max_items_out THEN
315 result.fail_part := 'config.circ_matrix_test.max_items_out';
316 result.success := FALSE;
322 -- Fail if the user has too many items with specific circ_modifiers checked out
323 FOR out_by_circ_mod IN SELECT * FROM config.circ_matrix_circ_mod_test WHERE matchpoint = matchpoint_id LOOP
324 SELECT INTO items_out COUNT(*)
325 FROM action.circulation circ
326 JOIN asset.copy cp ON (cp.id = circ.target_copy)
327 WHERE circ.usr = match_user
328 AND circ.checkin_time IS NULL
329 AND (circ.stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE') OR circ.stop_fines IS NULL)
330 AND cp.circ_modifier = out_by_circ_mod.circ_mod;
331 IF items_out >= out_by_circ_mod.items_out THEN
332 result.fail_part := 'config.circ_matrix_circ_mod_test';
333 result.success := FALSE;
339 -- Fail if the user has too many overdue items
340 IF circ_test.max_overdue IS NOT NULL THEN
341 SELECT INTO items_overdue COUNT(*)
342 FROM action.circulation
343 WHERE usr = match_user
344 AND checkin_time IS NULL
346 AND (stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE') OR stop_fines IS NULL);
347 IF items_overdue >= circ_test.max_overdue THEN
348 result.fail_part := 'config.circ_matrix_test.max_overdue';
349 result.success := FALSE;
355 -- Fail if the user has a high fine balance
356 IF circ_test.max_fines IS NOT NULL THEN
357 FOR tmp_xact IN SELECT * FROM money.billable_xact WHERE usr = match_usr AND xact_finish IS NULL LOOP
358 SELECT INTO tmp_fines SUM( amount ) FROM money.billing WHERE xact = tmp_xact.id AND NOT voided;
359 current_fines = current_fines + COALESCE(tmp_fines, 0.0);
360 SELECT INTO tmp_fines SUM( amount ) FROM money.payment WHERE xact = tmp_xact.id AND NOT voided;
361 current_fines = current_fines - COALESCE(tmp_fines, 0.0);
364 IF current_fines >= circ_test.max_fines THEN
365 result.fail_part := 'config.circ_matrix_test.max_fines';
366 result.success := FALSE;
372 -- If we passed everything, return the successful matchpoint id
379 $func$ LANGUAGE plpgsql;
381 CREATE OR REPLACE FUNCTION action.item_user_circ_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
382 SELECT * FROM action.item_user_circ_test( $1, $2, $3, FALSE );
385 CREATE OR REPLACE FUNCTION action.item_user_renew_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
386 SELECT * FROM action.item_user_circ_test( $1, $2, $3, TRUE );