3 -- NOTE: current config.item_type should get sip2_media_type and magnetic_media columns
5 -- New table needed to handle circ modifiers inside the DB. Will still require
6 -- central admin. The circ_modifier column on asset.copy will become an fkey to this table.
7 CREATE TABLE config.circ_modifier (
9 name TEXT UNIQUE NOT NULL,
10 description TEXT NOT NULL,
11 sip2_media_type TEXT NOT NULL,
12 magnetic_media BOOL NOT NULL DEFAULT TRUE,
13 avg_wait_time INTERVAL
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,
102 copy_circ_lib INT REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
103 copy_owning_lib INT REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
107 usr_age_lower_bound INTERVAL,
108 usr_age_upper_bound INTERVAL,
109 circulate BOOL NOT NULL DEFAULT TRUE, -- Hard "can't circ" flag requiring an override
110 duration_rule INT NOT NULL REFERENCES config.rule_circ_duration (id) DEFERRABLE INITIALLY DEFERRED,
111 recurring_fine_rule INT NOT NULL REFERENCES config.rule_recurring_fine (id) DEFERRABLE INITIALLY DEFERRED,
112 max_fine_rule INT NOT NULL REFERENCES config.rule_max_fine (id) DEFERRABLE INITIALLY DEFERRED,
113 hard_due_date INT REFERENCES config.hard_due_date (id) DEFERRABLE INITIALLY DEFERRED,
114 script_test TEXT, -- javascript source
115 total_copy_hold_ratio FLOAT,
116 available_copy_hold_ratio FLOAT,
117 CONSTRAINT ep_once_per_grp_loc_mod_marc UNIQUE (
118 grp, org_unit, circ_modifier, marc_type, marc_form, marc_vr_format, ref_flag,
119 juvenile_flag, usr_age_lower_bound, usr_age_upper_bound, is_renewal, copy_circ_lib,
125 -- Tests for max items out by circ_modifier
126 CREATE TABLE config.circ_matrix_circ_mod_test (
127 id SERIAL PRIMARY KEY,
128 matchpoint INT NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
129 items_out INT NOT NULL -- Total current active circulations must be less than this, NULL means skip (always pass)
132 CREATE TABLE config.circ_matrix_circ_mod_test_map (
133 id SERIAL PRIMARY KEY,
134 circ_mod_test INT NOT NULL REFERENCES config.circ_matrix_circ_mod_test (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
135 circ_mod TEXT NOT NULL REFERENCES config.circ_modifier (code) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
136 CONSTRAINT cm_once_per_test UNIQUE (circ_mod_test, circ_mod)
139 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$
141 current_group permission.grp_tree%ROWTYPE;
142 user_object actor.usr%ROWTYPE;
143 item_object asset.copy%ROWTYPE;
144 cn_object asset.call_number%ROWTYPE;
145 rec_descriptor metabib.rec_descriptor%ROWTYPE;
146 current_mp config.circ_matrix_matchpoint%ROWTYPE;
147 matchpoint config.circ_matrix_matchpoint%ROWTYPE;
149 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
150 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
151 SELECT INTO cn_object * FROM asset.call_number WHERE id = item_object.call_number;
152 SELECT INTO rec_descriptor r.* FROM metabib.rec_descriptor r JOIN asset.call_number c USING (record) WHERE c.id = item_object.call_number;
153 SELECT INTO current_group * FROM permission.grp_tree WHERE id = user_object.profile;
156 -- for each potential matchpoint for this ou and group ...
159 FROM config.circ_matrix_matchpoint m
160 JOIN actor.org_unit_ancestors( context_ou ) d ON (m.org_unit = d.id)
161 LEFT JOIN actor.org_unit_proximity p ON (p.from_org = context_ou AND p.to_org = d.id)
162 WHERE m.grp = current_group.id
164 AND (m.copy_owning_lib IS NULL OR cn_object.owning_lib IN ( SELECT id FROM actor.org_unit_descendants(m.copy_owning_lib) ))
165 AND (m.copy_circ_lib IS NULL OR item_object.circ_lib IN ( SELECT id FROM actor.org_unit_descendants(m.copy_circ_lib) ))
166 ORDER BY CASE WHEN p.prox IS NULL THEN 999 ELSE p.prox END,
167 CASE WHEN m.copy_owning_lib IS NOT NULL
168 THEN 256 / ( SELECT COALESCE(prox, 255) + 1 FROM actor.org_unit_proximity WHERE to_org = cn_object.owning_lib AND from_org = m.copy_owning_lib LIMIT 1 )
171 CASE WHEN m.copy_circ_lib IS NOT NULL
172 THEN 256 / ( SELECT COALESCE(prox, 255) + 1 FROM actor.org_unit_proximity WHERE to_org = item_object.circ_lib AND from_org = m.copy_circ_lib LIMIT 1 )
175 CASE WHEN m.is_renewal = renewal THEN 128 ELSE 0 END +
176 CASE WHEN m.juvenile_flag IS NOT NULL THEN 64 ELSE 0 END +
177 CASE WHEN m.circ_modifier IS NOT NULL THEN 32 ELSE 0 END +
178 CASE WHEN m.marc_type IS NOT NULL THEN 16 ELSE 0 END +
179 CASE WHEN m.marc_form IS NOT NULL THEN 8 ELSE 0 END +
180 CASE WHEN m.marc_vr_format IS NOT NULL THEN 4 ELSE 0 END +
181 CASE WHEN m.ref_flag IS NOT NULL THEN 2 ELSE 0 END +
182 CASE WHEN m.usr_age_lower_bound IS NOT NULL THEN 0.5 ELSE 0 END +
183 CASE WHEN m.usr_age_upper_bound IS NOT NULL THEN 0.5 ELSE 0 END DESC LOOP
185 IF current_mp.is_renewal IS NOT NULL THEN
186 CONTINUE WHEN current_mp.is_renewal <> renewal;
189 IF current_mp.circ_modifier IS NOT NULL THEN
190 CONTINUE WHEN current_mp.circ_modifier <> item_object.circ_modifier OR item_object.circ_modifier IS NULL;
193 IF current_mp.marc_type IS NOT NULL THEN
194 IF item_object.circ_as_type IS NOT NULL THEN
195 CONTINUE WHEN current_mp.marc_type <> item_object.circ_as_type;
197 CONTINUE WHEN current_mp.marc_type <> rec_descriptor.item_type;
201 IF current_mp.marc_form IS NOT NULL THEN
202 CONTINUE WHEN current_mp.marc_form <> rec_descriptor.item_form;
205 IF current_mp.marc_vr_format IS NOT NULL THEN
206 CONTINUE WHEN current_mp.marc_vr_format <> rec_descriptor.vr_format;
209 IF current_mp.ref_flag IS NOT NULL THEN
210 CONTINUE WHEN current_mp.ref_flag <> item_object.ref;
213 IF current_mp.juvenile_flag IS NOT NULL THEN
214 CONTINUE WHEN current_mp.juvenile_flag <> user_object.juvenile;
217 IF current_mp.usr_age_lower_bound IS NOT NULL THEN
218 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_lower_bound < age(user_object.dob);
221 IF current_mp.usr_age_upper_bound IS NOT NULL THEN
222 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_upper_bound > age(user_object.dob);
226 -- everything was undefined or matched
227 matchpoint = current_mp;
229 EXIT WHEN matchpoint.id IS NOT NULL;
232 EXIT WHEN current_group.parent IS NULL OR matchpoint.id IS NOT NULL;
234 SELECT INTO current_group * FROM permission.grp_tree WHERE id = current_group.parent;
239 $func$ LANGUAGE plpgsql;
241 CREATE TYPE action.hold_stats AS (
245 total_copy_ratio FLOAT,
246 available_copy_ratio FLOAT
249 CREATE OR REPLACE FUNCTION action.copy_related_hold_stats (copy_id INT) RETURNS action.hold_stats AS $func$
251 output action.hold_stats%ROWTYPE;
254 available_count INT := 0;
255 hold_map_data RECORD;
258 output.hold_count := 0;
259 output.copy_count := 0;
260 output.available_count := 0;
262 SELECT COUNT( DISTINCT m.hold ) INTO hold_count
263 FROM action.hold_copy_map m
264 JOIN action.hold_request h ON (m.hold = h.id)
265 WHERE m.target_copy = copy_id
268 output.hold_count := hold_count;
270 IF output.hold_count > 0 THEN
272 SELECT DISTINCT m.target_copy,
274 FROM action.hold_copy_map m
275 JOIN asset.copy acp ON (m.target_copy = acp.id)
276 JOIN action.hold_request h ON (m.hold = h.id)
277 WHERE m.hold IN ( SELECT DISTINCT hold FROM action.hold_copy_map WHERE target_copy = copy_id ) AND NOT h.frozen
279 output.copy_count := output.copy_count + 1;
280 IF hold_map_data.status IN (0,7,12) THEN
281 output.available_count := output.available_count + 1;
284 output.total_copy_ratio = output.copy_count::FLOAT / output.hold_count::FLOAT;
285 output.available_copy_ratio = output.available_count::FLOAT / output.hold_count::FLOAT;
292 $func$ LANGUAGE PLPGSQL;
294 CREATE TYPE action.matrix_test_result AS ( success BOOL, matchpoint INT, fail_part TEXT );
295 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$
297 user_object actor.usr%ROWTYPE;
298 standing_penalty config.standing_penalty%ROWTYPE;
299 item_object asset.copy%ROWTYPE;
300 item_status_object config.copy_status%ROWTYPE;
301 item_location_object asset.copy_location%ROWTYPE;
302 result action.matrix_test_result;
303 circ_test config.circ_matrix_matchpoint%ROWTYPE;
304 out_by_circ_mod config.circ_matrix_circ_mod_test%ROWTYPE;
305 circ_mod_map config.circ_matrix_circ_mod_test_map%ROWTYPE;
306 hold_ratio action.hold_stats%ROWTYPE;
310 context_org_list INT[];
313 result.success := TRUE;
315 -- Fail if the user is BARRED
316 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
318 -- Fail if we couldn't find the user
319 IF user_object.id IS NULL THEN
320 result.fail_part := 'no_user';
321 result.success := FALSE;
327 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
329 -- Fail if we couldn't find the item
330 IF item_object.id IS NULL THEN
331 result.fail_part := 'no_item';
332 result.success := FALSE;
338 SELECT INTO circ_test * FROM action.find_circ_matrix_matchpoint(circ_ou, match_item, match_user, renewal);
339 result.matchpoint := circ_test.id;
341 -- Fail if we couldn't find a matchpoint
342 IF result.matchpoint IS NULL THEN
343 result.fail_part := 'no_matchpoint';
344 result.success := FALSE;
349 IF user_object.barred IS TRUE THEN
350 result.fail_part := 'actor.usr.barred';
351 result.success := FALSE;
356 -- Fail if the item can't circulate
357 IF item_object.circulate IS FALSE THEN
358 result.fail_part := 'asset.copy.circulate';
359 result.success := FALSE;
364 -- Fail if the item isn't in a circulateable status on a non-renewal
365 IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN
366 result.fail_part := 'asset.copy.status';
367 result.success := FALSE;
370 ELSIF renewal AND item_object.status <> 1 THEN
371 result.fail_part := 'asset.copy.status';
372 result.success := FALSE;
377 -- Fail if the item can't circulate because of the shelving location
378 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
379 IF item_location_object.circulate IS FALSE THEN
380 result.fail_part := 'asset.copy_location.circulate';
381 result.success := FALSE;
386 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( circ_test.org_unit );
388 -- Fail if the test is set to hard non-circulating
389 IF circ_test.circulate IS FALSE THEN
390 result.fail_part := 'config.circ_matrix_test.circulate';
391 result.success := FALSE;
396 -- Fail if the total copy-hold ratio is too low
397 IF circ_test.total_copy_hold_ratio IS NOT NULL THEN
398 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
399 IF hold_ratio.total_copy_ratio IS NOT NULL AND hold_ratio.total_copy_ratio < circ_test.total_copy_hold_ratio THEN
400 result.fail_part := 'config.circ_matrix_test.total_copy_hold_ratio';
401 result.success := FALSE;
407 -- Fail if the available copy-hold ratio is too low
408 IF circ_test.available_copy_hold_ratio IS NOT NULL THEN
409 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
410 IF hold_ratio.available_copy_ratio IS NOT NULL AND hold_ratio.available_copy_ratio < circ_test.available_copy_hold_ratio THEN
411 result.fail_part := 'config.circ_matrix_test.available_copy_hold_ratio';
412 result.success := FALSE;
419 penalty_type = '%RENEW%';
421 penalty_type = '%CIRC%';
424 FOR standing_penalty IN
425 SELECT DISTINCT csp.*
426 FROM actor.usr_standing_penalty usp
427 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
428 WHERE usr = match_user
429 AND usp.org_unit IN ( SELECT * FROM explode_array(context_org_list) )
430 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
431 AND csp.block_list LIKE penalty_type LOOP
433 result.fail_part := standing_penalty.name;
434 result.success := FALSE;
439 -- Fail if the user has too many items with specific circ_modifiers checked out
440 FOR out_by_circ_mod IN SELECT * FROM config.circ_matrix_circ_mod_test WHERE matchpoint = circ_test.id LOOP
441 SELECT INTO items_out COUNT(*)
442 FROM action.circulation circ
443 JOIN asset.copy cp ON (cp.id = circ.target_copy)
444 WHERE circ.usr = match_user
445 AND circ.circ_lib IN ( SELECT * FROM explode_array(context_org_list) )
446 AND circ.checkin_time IS NULL
447 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL)
448 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);
449 IF items_out >= out_by_circ_mod.items_out THEN
450 result.fail_part := 'config.circ_matrix_circ_mod_test';
451 result.success := FALSE;
457 -- If we passed everything, return the successful matchpoint id
464 $func$ LANGUAGE plpgsql;
466 CREATE OR REPLACE FUNCTION action.item_user_circ_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
467 SELECT * FROM action.item_user_circ_test( $1, $2, $3, FALSE );
470 CREATE OR REPLACE FUNCTION action.item_user_renew_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
471 SELECT * FROM action.item_user_circ_test( $1, $2, $3, TRUE );
475 CREATE OR REPLACE FUNCTION actor.calculate_system_penalties( match_user INT, context_org INT ) RETURNS SETOF actor.usr_standing_penalty AS $func$
477 user_object actor.usr%ROWTYPE;
478 new_sp_row actor.usr_standing_penalty%ROWTYPE;
479 existing_sp_row actor.usr_standing_penalty%ROWTYPE;
480 collections_fines permission.grp_penalty_threshold%ROWTYPE;
481 max_fines permission.grp_penalty_threshold%ROWTYPE;
482 max_overdue permission.grp_penalty_threshold%ROWTYPE;
483 max_items_out permission.grp_penalty_threshold%ROWTYPE;
487 context_org_list INT[];
488 current_fines NUMERIC(8,2) := 0.0;
489 tmp_fines NUMERIC(8,2);
492 tmp_org actor.org_unit%ROWTYPE;
493 tmp_penalty config.standing_penalty%ROWTYPE;
496 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
499 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
501 -- Fail if the user has a high fine balance
503 tmp_grp := user_object.profile;
505 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 1 AND org_unit = tmp_org.id;
507 IF max_fines.threshold IS NULL THEN
508 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
513 IF tmp_grp IS NULL THEN
518 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
522 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
526 IF max_fines.threshold IS NOT NULL THEN
528 FOR existing_sp_row IN
530 FROM actor.usr_standing_penalty
531 WHERE usr = match_user
532 AND org_unit = max_fines.org_unit
533 AND (stop_date IS NULL or stop_date > NOW())
534 AND standing_penalty = 1
536 RETURN NEXT existing_sp_row;
539 SELECT SUM(f.balance_owed) INTO current_fines
540 FROM money.materialized_billable_xact_summary f
543 FROM booking.reservation r
544 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (r.pickup_lib = fp.id)
545 WHERE usr = match_user
546 AND xact_finish IS NULL
550 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (g.billing_location = fp.id)
551 WHERE usr = match_user
552 AND xact_finish IS NULL
555 FROM action.circulation circ
556 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (circ.circ_lib = fp.id)
557 WHERE usr = match_user
558 AND xact_finish IS NULL ) l USING (id);
560 IF current_fines >= max_fines.threshold THEN
561 new_sp_row.usr := match_user;
562 new_sp_row.org_unit := max_fines.org_unit;
563 new_sp_row.standing_penalty := 1;
564 RETURN NEXT new_sp_row;
568 -- Start over for max overdue
569 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
571 -- Fail if the user has too many overdue items
573 tmp_grp := user_object.profile;
576 SELECT * INTO max_overdue FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 2 AND org_unit = tmp_org.id;
578 IF max_overdue.threshold IS NULL THEN
579 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
584 IF tmp_grp IS NULL THEN
589 IF max_overdue.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
593 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
597 IF max_overdue.threshold IS NOT NULL THEN
599 FOR existing_sp_row IN
601 FROM actor.usr_standing_penalty
602 WHERE usr = match_user
603 AND org_unit = max_overdue.org_unit
604 AND (stop_date IS NULL or stop_date > NOW())
605 AND standing_penalty = 2
607 RETURN NEXT existing_sp_row;
610 SELECT INTO items_overdue COUNT(*)
611 FROM action.circulation circ
612 JOIN actor.org_unit_full_path( max_overdue.org_unit ) fp ON (circ.circ_lib = fp.id)
613 WHERE circ.usr = match_user
614 AND circ.checkin_time IS NULL
615 AND circ.due_date < NOW()
616 AND (circ.stop_fines = 'MAXFINES' OR circ.stop_fines IS NULL);
618 IF items_overdue >= max_overdue.threshold::INT THEN
619 new_sp_row.usr := match_user;
620 new_sp_row.org_unit := max_overdue.org_unit;
621 new_sp_row.standing_penalty := 2;
622 RETURN NEXT new_sp_row;
626 -- Start over for max out
627 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
629 -- Fail if the user has too many checked out items
631 tmp_grp := user_object.profile;
633 SELECT * INTO max_items_out FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 3 AND org_unit = tmp_org.id;
635 IF max_items_out.threshold IS NULL THEN
636 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
641 IF tmp_grp IS NULL THEN
646 IF max_items_out.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
650 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
655 -- Fail if the user has too many items checked out
656 IF max_items_out.threshold IS NOT NULL THEN
658 FOR existing_sp_row IN
660 FROM actor.usr_standing_penalty
661 WHERE usr = match_user
662 AND org_unit = max_items_out.org_unit
663 AND (stop_date IS NULL or stop_date > NOW())
664 AND standing_penalty = 3
666 RETURN NEXT existing_sp_row;
669 SELECT INTO items_out COUNT(*)
670 FROM action.circulation circ
671 JOIN actor.org_unit_full_path( max_items_out.org_unit ) fp ON (circ.circ_lib = fp.id)
672 WHERE circ.usr = match_user
673 AND circ.checkin_time IS NULL
674 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL);
676 IF items_out >= max_items_out.threshold::INT THEN
677 new_sp_row.usr := match_user;
678 new_sp_row.org_unit := max_items_out.org_unit;
679 new_sp_row.standing_penalty := 3;
680 RETURN NEXT new_sp_row;
684 -- Start over for collections warning
685 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
687 -- Fail if the user has a collections-level fine balance
689 tmp_grp := user_object.profile;
691 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 4 AND org_unit = tmp_org.id;
693 IF max_fines.threshold IS NULL THEN
694 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
699 IF tmp_grp IS NULL THEN
704 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
708 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
712 IF max_fines.threshold IS NOT NULL THEN
714 FOR existing_sp_row IN
716 FROM actor.usr_standing_penalty
717 WHERE usr = match_user
718 AND org_unit = max_fines.org_unit
719 AND (stop_date IS NULL or stop_date > NOW())
720 AND standing_penalty = 4
722 RETURN NEXT existing_sp_row;
725 SELECT SUM(f.balance_owed) INTO current_fines
726 FROM money.materialized_billable_xact_summary f
729 FROM booking.reservation r
730 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (r.pickup_lib = fp.id)
731 WHERE usr = match_user
732 AND xact_finish IS NULL
736 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (g.billing_location = fp.id)
737 WHERE usr = match_user
738 AND xact_finish IS NULL
741 FROM action.circulation circ
742 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (circ.circ_lib = fp.id)
743 WHERE usr = match_user
744 AND xact_finish IS NULL ) l USING (id);
746 IF current_fines >= max_fines.threshold THEN
747 new_sp_row.usr := match_user;
748 new_sp_row.org_unit := max_fines.org_unit;
749 new_sp_row.standing_penalty := 4;
750 RETURN NEXT new_sp_row;
754 -- Start over for in collections
755 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
757 -- Remove the in-collections penalty if the user has paid down enough
758 -- This penalty is different, because this code is not responsible for creating
759 -- new in-collections penalties, only for removing them
761 tmp_grp := user_object.profile;
763 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 30 AND org_unit = tmp_org.id;
765 IF max_fines.threshold IS NULL THEN
766 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
771 IF tmp_grp IS NULL THEN
776 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
780 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
784 IF max_fines.threshold IS NOT NULL THEN
786 -- first, see if the user had paid down to the threshold
787 SELECT SUM(f.balance_owed) INTO current_fines
788 FROM money.materialized_billable_xact_summary f
791 FROM booking.reservation r
792 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (r.pickup_lib = fp.id)
793 WHERE usr = match_user
794 AND xact_finish IS NULL
798 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (g.billing_location = fp.id)
799 WHERE usr = match_user
800 AND xact_finish IS NULL
803 FROM action.circulation circ
804 JOIN actor.org_unit_full_path( max_fines.org_unit ) fp ON (circ.circ_lib = fp.id)
805 WHERE usr = match_user
806 AND xact_finish IS NULL ) l USING (id);
808 IF current_fines IS NULL OR current_fines <= max_fines.threshold THEN
809 -- patron has paid down enough
811 SELECT INTO tmp_penalty * FROM config.standing_penalty WHERE id = 30;
813 IF tmp_penalty.org_depth IS NOT NULL THEN
815 -- since this code is not responsible for applying the penalty, it can't
816 -- guarantee the current context org will match the org at which the penalty
817 --- was applied. search up the org tree until we hit the configured penalty depth
818 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
819 SELECT INTO tmp_depth depth FROM actor.org_unit_type WHERE id = tmp_org.ou_type;
821 WHILE tmp_depth >= tmp_penalty.org_depth LOOP
823 FOR existing_sp_row IN
825 FROM actor.usr_standing_penalty
826 WHERE usr = match_user
827 AND org_unit = tmp_org.id
828 AND (stop_date IS NULL or stop_date > NOW())
829 AND standing_penalty = 30
832 -- Penalty exists, return it for removal
833 RETURN NEXT existing_sp_row;
836 IF tmp_org.parent_ou IS NULL THEN
840 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
841 SELECT INTO tmp_depth depth FROM actor.org_unit_type WHERE id = tmp_org.ou_type;
846 -- no penalty depth is defined, look for exact matches
848 FOR existing_sp_row IN
850 FROM actor.usr_standing_penalty
851 WHERE usr = match_user
852 AND org_unit = max_fines.org_unit
853 AND (stop_date IS NULL or stop_date > NOW())
854 AND standing_penalty = 30
856 -- Penalty exists, return it for removal
857 RETURN NEXT existing_sp_row;
867 $func$ LANGUAGE plpgsql;