4 CREATE OR REPLACE FUNCTION explode_array(anyarray) RETURNS SETOF anyelement AS $BODY$
5 SELECT ($1)[s] FROM generate_series(1, array_upper($1, 1)) AS s;
7 LANGUAGE 'sql' IMMUTABLE;
9 -- NOTE: current config.item_type should get sip2_media_type and magnetic_media columns
11 -- New table needed to handle circ modifiers inside the DB. Will still require
12 -- central admin. The circ_modifier column on asset.copy will become an fkey to this table.
13 CREATE TABLE config.circ_modifier (
14 code TEXT PRIMARY KEY,
15 name TEXT UNIQUE NOT NULL,
16 description TEXT NOT NULL,
17 sip2_media_type TEXT NOT NULL,
18 magnetic_media BOOL NOT NULL DEFAULT TRUE
23 INSERT INTO config.circ_modifier VALUES ( 'DVD', 'DVD', 'um ... DVDs', '001', FALSE );
24 INSERT INTO config.circ_modifier VALUES ( 'VIDEO', 'VIDEO', 'Tapes', '001', TRUE );
25 INSERT INTO config.circ_modifier VALUES ( 'BOOK', 'BOOK', 'Dead tree', '001', FALSE );
26 INSERT INTO config.circ_modifier VALUES ( 'CRAZY_ARL-ATH_SETTING', 'R2R_TAPE', 'reel2reel tape', '007', TRUE );
29 -- But, just to get us started, use this
32 UPDATE asset.copy SET circ_modifier = UPPER(circ_modifier) WHERE circ_modifier IS NOT NULL AND circ_modifier <> '';
33 UPDATE asset.copy SET circ_modifier = NULL WHERE circ_modifier = '';
35 INSERT INTO config.circ_modifier (code, name, description, sip2_media_type )
42 WHERE circ_modifier IS NOT NULL;
46 -- add an fkey pointing to the new circ mod table
47 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;
49 -- config table to hold the vr_format names
50 CREATE TABLE config.videorecording_format_map (
51 code TEXT PRIMARY KEY,
55 INSERT INTO config.videorecording_format_map VALUES ('a','Beta');
56 INSERT INTO config.videorecording_format_map VALUES ('b','VHS');
57 INSERT INTO config.videorecording_format_map VALUES ('c','U-matic');
58 INSERT INTO config.videorecording_format_map VALUES ('d','EIAJ');
59 INSERT INTO config.videorecording_format_map VALUES ('e','Type C');
60 INSERT INTO config.videorecording_format_map VALUES ('f','Quadruplex');
61 INSERT INTO config.videorecording_format_map VALUES ('g','Laserdisc');
62 INSERT INTO config.videorecording_format_map VALUES ('h','CED');
63 INSERT INTO config.videorecording_format_map VALUES ('i','Betacam');
64 INSERT INTO config.videorecording_format_map VALUES ('j','Betacam SP');
65 INSERT INTO config.videorecording_format_map VALUES ('k','Super-VHS');
66 INSERT INTO config.videorecording_format_map VALUES ('m','M-II');
67 INSERT INTO config.videorecording_format_map VALUES ('o','D-2');
68 INSERT INTO config.videorecording_format_map VALUES ('p','8 mm.');
69 INSERT INTO config.videorecording_format_map VALUES ('q','Hi-8 mm.');
70 INSERT INTO config.videorecording_format_map VALUES ('u','Unknown');
71 INSERT INTO config.videorecording_format_map VALUES ('v','DVD');
72 INSERT INTO config.videorecording_format_map VALUES ('z','Other');
77 ** Here we define the tables that make up the circ matrix. Conceptually, this implements
78 ** the "sparse matrix" that everyone talks about, instead of using traditional rules logic.
79 ** Physically, we cut the matrix up into separate tables (almost 3rd normal form!) that handle
80 ** different portions of the matrix. This wil simplify creation of the UI (I hope), and help the
81 ** developers focus on specific parts of the matrix.
86 -- ****** Which ruleset and tests to use *******
88 -- * Most specific range for org_unit and grp wins.
90 -- * circ_modifier match takes precidence over marc_type match, if circ_modifier is set here
92 -- * marc_type is first checked against the circ_as_type from the copy, then the item type from the marc record
94 -- * If neither circ_modifier nor marc_type is set (both are NULLABLE) then the entry defines the default
95 -- ruleset and tests for the OU + group (like BOOK in PINES)
98 CREATE TABLE config.circ_matrix_matchpoint (
99 id SERIAL PRIMARY KEY,
100 active BOOL NOT NULL DEFAULT TRUE,
101 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"
102 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
103 circ_modifier TEXT REFERENCES config.circ_modifier (code) DEFERRABLE INITIALLY DEFERRED,
104 marc_type TEXT REFERENCES config.item_type_map (code) DEFERRABLE INITIALLY DEFERRED,
105 marc_form TEXT REFERENCES config.item_form_map (code) DEFERRABLE INITIALLY DEFERRED,
106 marc_vr_format TEXT REFERENCES config.videorecording_format_map (code) DEFERRABLE INITIALLY DEFERRED,
110 usr_age_lower_bound INTERVAL,
111 usr_age_upper_bound INTERVAL,
112 circulate BOOL NOT NULL DEFAULT TRUE, -- Hard "can't circ" flag requiring an override
113 duration_rule INT NOT NULL REFERENCES config.rule_circ_duration (id) DEFERRABLE INITIALLY DEFERRED,
114 recurring_fine_rule INT NOT NULL REFERENCES config.rule_recurring_fine (id) DEFERRABLE INITIALLY DEFERRED,
115 max_fine_rule INT NOT NULL REFERENCES config.rule_max_fine (id) DEFERRABLE INITIALLY DEFERRED,
116 script_test TEXT, -- javascript source
117 total_copy_hold_ratio FLOAT,
118 available_copy_hold_ratio FLOAT,
119 CONSTRAINT ep_once_per_grp_loc_mod_marc UNIQUE (grp, org_unit, circ_modifier, marc_type, marc_form, marc_vr_format, ref_flag, juvenile_flag, usr_age_lower_bound, usr_age_upper_bound, is_renewal)
123 -- Tests for max items out by circ_modifier
124 CREATE TABLE config.circ_matrix_circ_mod_test (
125 id SERIAL PRIMARY KEY,
126 matchpoint INT NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
127 items_out INT NOT NULL -- Total current active circulations must be less than this, NULL means skip (always pass)
130 CREATE TABLE config.circ_matrix_circ_mod_test_map (
131 id SERIAL PRIMARY KEY,
132 circ_mod_test INT NOT NULL REFERENCES config.circ_matrix_circ_mod_test (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
133 circ_mod TEXT NOT NULL REFERENCES config.circ_modifier (code) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
134 CONSTRAINT cm_once_per_test UNIQUE (circ_mod_test, circ_mod)
137 CREATE OR REPLACE FUNCTION action.find_circ_matrix_matchpoint( context_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS config.circ_matrix_matchpoint 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 128 ELSE 0 END +
161 CASE WHEN m.juvenile_flag IS NOT NULL THEN 64 ELSE 0 END +
162 CASE WHEN m.circ_modifier IS NOT NULL THEN 32 ELSE 0 END +
163 CASE WHEN m.marc_type IS NOT NULL THEN 16 ELSE 0 END +
164 CASE WHEN m.marc_form IS NOT NULL THEN 8 ELSE 0 END +
165 CASE WHEN m.marc_vr_format IS NOT NULL THEN 4 ELSE 0 END +
166 CASE WHEN m.ref_flag IS NOT NULL THEN 2 ELSE 0 END +
167 CASE WHEN m.usr_age_lower_bound IS NOT NULL THEN 0.5 ELSE 0 END +
168 CASE WHEN m.usr_age_upper_bound IS NOT NULL THEN 0.5 ELSE 0 END DESC LOOP
170 IF current_mp.circ_modifier IS NOT NULL THEN
171 CONTINUE WHEN current_mp.circ_modifier <> item_object.circ_modifier OR item_object.circ_modifier IS NULL;
174 IF current_mp.marc_type IS NOT NULL THEN
175 IF item_object.circ_as_type IS NOT NULL THEN
176 CONTINUE WHEN current_mp.marc_type <> item_object.circ_as_type;
178 CONTINUE WHEN current_mp.marc_type <> rec_descriptor.item_type;
182 IF current_mp.marc_form IS NOT NULL THEN
183 CONTINUE WHEN current_mp.marc_form <> rec_descriptor.item_form;
186 IF current_mp.marc_vr_format IS NOT NULL THEN
187 CONTINUE WHEN current_mp.marc_vr_format <> rec_descriptor.vr_format;
190 IF current_mp.ref_flag IS NOT NULL THEN
191 CONTINUE WHEN current_mp.ref_flag <> item_object.ref;
194 IF current_mp.juvenile_flag IS NOT NULL THEN
195 CONTINUE WHEN current_mp.juvenile_flag <> user_object.juvenile;
198 IF current_mp.usr_age_lower_bound IS NOT NULL THEN
199 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_lower_bound < age(user_object.dob);
202 IF current_mp.usr_age_upper_bound IS NOT NULL THEN
203 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_upper_bound > age(user_object.dob);
207 -- everything was undefined or matched
208 matchpoint = current_mp;
210 EXIT WHEN matchpoint.id IS NOT NULL;
213 EXIT WHEN current_group.parent IS NULL OR matchpoint.id IS NOT NULL;
215 SELECT INTO current_group * FROM permission.grp_tree WHERE id = current_group.parent;
220 $func$ LANGUAGE plpgsql;
222 CREATE TYPE action.hold_stats AS (
226 total_copy_ratio FLOAT,
227 available_copy_ratio FLOAT
230 CREATE OR REPLACE FUNCTION action.copy_related_hold_stats (copy_id INT) RETURNS action.hold_stats AS $func$
232 output action.hold_stats%ROWTYPE;
235 available_count INT := 0;
236 hold_map_data RECORD;
239 output.hold_count := 0;
240 output.copy_count := 0;
241 output.available_count := 0;
243 SELECT COUNT( DISTINCT m.hold ) INTO hold_count
244 FROM action.hold_copy_map m
245 JOIN action.hold_request h ON (m.hold = h.id)
246 WHERE m.target_copy = copy_id
249 output.hold_count := hold_count;
251 IF output.hold_count > 0 THEN
253 SELECT DISTINCT m.target_copy,
255 FROM action.hold_copy_map m
256 JOIN asset.copy acp ON (m.target_copy = acp.id)
257 JOIN action.hold_request h ON (m.hold = h.id)
258 WHERE m.hold IN ( SELECT DISTINCT hold FROM action.hold_copy_map WHERE target_copy = copy_id ) AND NOT h.frozen
260 output.copy_count := output.copy_count + 1;
261 IF hold_map_data.status IN (0,7,12) THEN
262 output.available_count := output.available_count + 1;
265 output.total_copy_ratio = output.copy_count::FLOAT / output.hold_count::FLOAT;
266 output.available_copy_ratio = output.available_count::FLOAT / output.hold_count::FLOAT;
273 $func$ LANGUAGE PLPGSQL;
275 CREATE TYPE action.matrix_test_result AS ( success BOOL, matchpoint INT, fail_part TEXT );
276 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$
278 user_object actor.usr%ROWTYPE;
279 standing_penalty config.standing_penalty%ROWTYPE;
280 item_object asset.copy%ROWTYPE;
281 item_status_object config.copy_status%ROWTYPE;
282 item_location_object asset.copy_location%ROWTYPE;
283 result action.matrix_test_result;
284 circ_test config.circ_matrix_matchpoint%ROWTYPE;
285 out_by_circ_mod config.circ_matrix_circ_mod_test%ROWTYPE;
286 circ_mod_map config.circ_matrix_circ_mod_test_map%ROWTYPE;
287 hold_ratio action.hold_stats%ROWTYPE;
291 context_org_list INT[];
294 result.success := TRUE;
296 -- Fail if the user is BARRED
297 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
299 -- Fail if we couldn't find a set of tests
300 IF user_object.id IS NULL THEN
301 result.fail_part := 'no_user';
302 result.success := FALSE;
308 IF user_object.barred IS TRUE THEN
309 result.fail_part := 'actor.usr.barred';
310 result.success := FALSE;
315 -- Fail if the item can't circulate
316 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
317 IF item_object.circulate IS FALSE THEN
318 result.fail_part := 'asset.copy.circulate';
319 result.success := FALSE;
324 -- Fail if the item isn't in a circulateable status on a non-renewal
325 IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN
326 result.fail_part := 'asset.copy.status';
327 result.success := FALSE;
330 ELSIF renewal AND item_object.status <> 1 THEN
331 result.fail_part := 'asset.copy.status';
332 result.success := FALSE;
337 -- Fail if the item can't circulate because of the shelving location
338 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
339 IF item_location_object.circulate IS FALSE THEN
340 result.fail_part := 'asset.copy_location.circulate';
341 result.success := FALSE;
346 SELECT INTO circ_test * FROM action.find_circ_matrix_matchpoint(circ_ou, match_item, match_user, renewal);
347 result.matchpoint := circ_test.id;
349 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( circ_test.org_unit );
351 -- Fail if we couldn't find a set of tests
352 IF result.matchpoint IS NULL THEN
353 result.fail_part := 'no_matchpoint';
354 result.success := FALSE;
359 -- Fail if the test is set to hard non-circulating
360 IF circ_test.circulate IS FALSE THEN
361 result.fail_part := 'config.circ_matrix_test.circulate';
362 result.success := FALSE;
367 -- Fail if the total copy-hold ratio is too low
368 IF circ_test.total_copy_hold_ratio IS NOT NULL THEN
369 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
370 IF hold_ratio.total_copy_ratio IS NOT NULL AND hold_ratio.total_copy_ratio < circ_test.total_copy_hold_ratio THEN
371 result.fail_part := 'config.circ_matrix_test.total_copy_hold_ratio';
372 result.success := FALSE;
378 -- Fail if the available copy-hold ratio is too low
379 IF circ_test.available_copy_hold_ratio IS NOT NULL THEN
380 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
381 IF hold_ratio.available_copy_ratio IS NOT NULL AND hold_ratio.available_copy_ratio < circ_test.available_copy_hold_ratio THEN
382 result.fail_part := 'config.circ_matrix_test.available_copy_hold_ratio';
383 result.success := FALSE;
390 penalty_type = '%RENEW%';
392 penalty_type = '%CIRC%';
395 FOR standing_penalty IN
396 SELECT DISTINCT csp.*
397 FROM actor.usr_standing_penalty usp
398 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
399 WHERE usr = match_user
400 AND usp.org_unit IN ( SELECT * FROM explode_array(context_org_list) )
401 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
402 AND csp.block_list LIKE penalty_type LOOP
404 result.fail_part := standing_penalty.name;
405 result.success := FALSE;
410 -- Fail if the user has too many items with specific circ_modifiers checked out
411 FOR out_by_circ_mod IN SELECT * FROM config.circ_matrix_circ_mod_test WHERE matchpoint = circ_test.id LOOP
412 SELECT INTO items_out COUNT(*)
413 FROM action.circulation circ
414 JOIN asset.copy cp ON (cp.id = circ.target_copy)
415 WHERE circ.usr = match_user
416 AND circ.circ_lib IN ( SELECT * FROM explode_array(context_org_list) )
417 AND circ.checkin_time IS NULL
418 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL)
419 AND cp.circ_modifier IN (SELECT circ_mod FROM config.circ_matrix_circ_mod_test_map WHERE circ_mod_test = out_by_circ_mod.id);
420 IF items_out >= out_by_circ_mod.items_out THEN
421 result.fail_part := 'config.circ_matrix_circ_mod_test';
422 result.success := FALSE;
428 -- If we passed everything, return the successful matchpoint id
435 $func$ LANGUAGE plpgsql;
437 CREATE OR REPLACE FUNCTION action.item_user_circ_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
438 SELECT * FROM action.item_user_circ_test( $1, $2, $3, FALSE );
441 CREATE OR REPLACE FUNCTION action.item_user_renew_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
442 SELECT * FROM action.item_user_circ_test( $1, $2, $3, TRUE );
446 CREATE OR REPLACE FUNCTION actor.calculate_system_penalties( match_user INT, context_org INT ) RETURNS SETOF actor.usr_standing_penalty AS $func$
448 user_object actor.usr%ROWTYPE;
449 new_sp_row actor.usr_standing_penalty%ROWTYPE;
450 existing_sp_row actor.usr_standing_penalty%ROWTYPE;
451 collections_fines permission.grp_penalty_threshold%ROWTYPE;
452 max_fines permission.grp_penalty_threshold%ROWTYPE;
453 max_overdue permission.grp_penalty_threshold%ROWTYPE;
454 max_items_out permission.grp_penalty_threshold%ROWTYPE;
458 context_org_list INT[];
459 current_fines NUMERIC(8,2) := 0.0;
460 tmp_fines NUMERIC(8,2);
463 tmp_org actor.org_unit%ROWTYPE;
465 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
468 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
470 -- Fail if the user has a high fine balance
472 tmp_grp := user_object.profile;
474 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 1 AND org_unit = tmp_org.id;
476 IF max_fines.threshold IS NULL THEN
477 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
482 IF tmp_grp IS NULL THEN
487 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
491 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
495 IF max_fines.threshold IS NOT NULL THEN
497 FOR existing_sp_row IN
499 FROM actor.usr_standing_penalty
500 WHERE usr = match_user
501 AND org_unit = max_fines.org_unit
502 AND (stop_date IS NULL or stop_date > NOW())
503 AND standing_penalty = 1
505 RETURN NEXT existing_sp_row;
508 SELECT SUM(f.balance_owed) INTO current_fines
509 FROM money.materialized_billable_xact_summary f
512 FROM booking.reservation r
513 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (r.pickup_lib = fp.id)
514 WHERE usr = match_user
515 AND xact_finish IS NULL
519 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (g.billing_location = fp.id)
520 WHERE usr = match_user
521 AND xact_finish IS NULL
524 FROM action.circulation circ
525 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (circ.circ_lib = fp.id)
526 WHERE usr = match_user
527 AND xact_finish IS NULL ) l USING (id);
529 IF current_fines >= max_fines.threshold THEN
530 new_sp_row.usr := match_user;
531 new_sp_row.org_unit := max_fines.org_unit;
532 new_sp_row.standing_penalty := 1;
533 RETURN NEXT new_sp_row;
537 -- Start over for max overdue
538 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
540 -- Fail if the user has too many overdue items
542 tmp_grp := user_object.profile;
545 SELECT * INTO max_overdue FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 2 AND org_unit = tmp_org.id;
547 IF max_overdue.threshold IS NULL THEN
548 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
553 IF tmp_grp IS NULL THEN
558 IF max_overdue.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
562 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
566 IF max_overdue.threshold IS NOT NULL THEN
568 FOR existing_sp_row IN
570 FROM actor.usr_standing_penalty
571 WHERE usr = match_user
572 AND org_unit = max_overdue.org_unit
573 AND (stop_date IS NULL or stop_date > NOW())
574 AND standing_penalty = 2
576 RETURN NEXT existing_sp_row;
579 SELECT INTO items_overdue COUNT(*)
580 FROM action.circulation circ
581 JOIN actor.org_unit_full_path( max_overdue.org_unit ) fp ON (circ.circ_lib = fp.id)
582 WHERE circ.usr = match_user
583 AND circ.checkin_time IS NULL
584 AND circ.due_date < NOW()
585 AND (circ.stop_fines = 'MAXFINES' OR circ.stop_fines IS NULL);
587 IF items_overdue >= max_overdue.threshold::INT THEN
588 new_sp_row.usr := match_user;
589 new_sp_row.org_unit := max_overdue.org_unit;
590 new_sp_row.standing_penalty := 2;
591 RETURN NEXT new_sp_row;
595 -- Start over for max out
596 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
598 -- Fail if the user has too many checked out items
600 tmp_grp := user_object.profile;
602 SELECT * INTO max_items_out FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 3 AND org_unit = tmp_org.id;
604 IF max_items_out.threshold IS NULL THEN
605 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
610 IF tmp_grp IS NULL THEN
615 IF max_items_out.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
619 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
624 -- Fail if the user has too many items checked out
625 IF max_items_out.threshold IS NOT NULL THEN
627 FOR existing_sp_row IN
629 FROM actor.usr_standing_penalty
630 WHERE usr = match_user
631 AND org_unit = max_items_out.org_unit
632 AND (stop_date IS NULL or stop_date > NOW())
633 AND standing_penalty = 3
635 RETURN NEXT existing_sp_row;
638 SELECT INTO items_out COUNT(*)
639 FROM action.circulation circ
640 JOIN actor.org_unit_full_path( max_items_out.org_unit ) fp ON (circ.circ_lib = fp.id)
641 WHERE circ.usr = match_user
642 AND circ.checkin_time IS NULL
643 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL);
645 IF items_out >= max_items_out.threshold::INT THEN
646 new_sp_row.usr := match_user;
647 new_sp_row.org_unit := max_items_out.org_unit;
648 new_sp_row.standing_penalty := 3;
649 RETURN NEXT new_sp_row;
653 -- Start over for collections warning
654 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
656 -- Fail if the user has a collections-level fine balance
658 tmp_grp := user_object.profile;
660 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 4 AND org_unit = tmp_org.id;
662 IF max_fines.threshold IS NULL THEN
663 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
668 IF tmp_grp IS NULL THEN
673 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
677 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
681 IF max_fines.threshold IS NOT NULL THEN
683 FOR existing_sp_row IN
685 FROM actor.usr_standing_penalty
686 WHERE usr = match_user
687 AND org_unit = max_fines.org_unit
688 AND (stop_date IS NULL or stop_date > NOW())
689 AND standing_penalty = 4
691 RETURN NEXT existing_sp_row;
694 SELECT SUM(f.balance_owed) INTO current_fines
695 FROM money.materialized_billable_xact_summary f
698 FROM booking.reservation r
699 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (r.pickup_lib = fp.id)
700 WHERE usr = match_user
701 AND xact_finish IS NULL
705 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (g.billing_location = fp.id)
706 WHERE usr = match_user
707 AND xact_finish IS NULL
710 FROM action.circulation circ
711 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (circ.circ_lib = fp.id)
712 WHERE usr = match_user
713 AND xact_finish IS NULL ) l USING (id);
715 IF current_fines >= max_fines.threshold THEN
716 new_sp_row.usr := match_user;
717 new_sp_row.org_unit := max_fines.org_unit;
718 new_sp_row.standing_penalty := 4;
719 RETURN NEXT new_sp_row;
726 $func$ LANGUAGE plpgsql;