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;
46 ** Here we define the tables that make up the circ matrix. Conceptually, this implements
47 ** the "sparse matrix" that everyone talks about, instead of using traditional rules logic.
48 ** Physically, we cut the matrix up into separate tables (almost 3rd normal form!) that handle
49 ** different portions of the matrix. This wil simplify creation of the UI (I hope), and help the
50 ** developers focus on specific parts of the matrix.
53 CREATE TABLE config.circ_matrix_matchpoint (
54 id SERIAL PRIMARY KEY,
55 active BOOL NOT NULL DEFAULT TRUE,
57 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"
58 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
59 circ_modifier TEXT REFERENCES config.circ_modifier (code) DEFERRABLE INITIALLY DEFERRED,
64 copy_circ_lib INT REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
65 copy_owning_lib INT REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
66 user_home_ou INT REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
70 usr_age_lower_bound INTERVAL,
71 usr_age_upper_bound INTERVAL,
73 circulate BOOL, -- Hard "can't circ" flag requiring an override
74 duration_rule INT REFERENCES config.rule_circ_duration (id) DEFERRABLE INITIALLY DEFERRED,
75 recurring_fine_rule INT REFERENCES config.rule_recurring_fine (id) DEFERRABLE INITIALLY DEFERRED,
76 max_fine_rule INT REFERENCES config.rule_max_fine (id) DEFERRABLE INITIALLY DEFERRED,
77 hard_due_date INT REFERENCES config.hard_due_date (id) DEFERRABLE INITIALLY DEFERRED,
78 renewals INT, -- Renewal count override
79 grace_period INTERVAL, -- Grace period override
80 script_test TEXT, -- javascript source
81 total_copy_hold_ratio FLOAT,
82 available_copy_hold_ratio FLOAT
85 -- Nulls don't count for a constraint match, so we have to coalesce them into something that does.
86 CREATE UNIQUE INDEX ccmm_once_per_paramset ON config.circ_matrix_matchpoint (org_unit, grp, COALESCE(circ_modifier, ''), COALESCE(marc_type, ''), COALESCE(marc_form, ''), COALESCE(marc_bib_level,''), COALESCE(marc_vr_format, ''), COALESCE(copy_circ_lib::TEXT, ''), COALESCE(copy_owning_lib::TEXT, ''), COALESCE(user_home_ou::TEXT, ''), COALESCE(ref_flag::TEXT, ''), COALESCE(juvenile_flag::TEXT, ''), COALESCE(is_renewal::TEXT, ''), COALESCE(usr_age_lower_bound::TEXT, ''), COALESCE(usr_age_upper_bound::TEXT, '')) WHERE active;
88 -- Tests for max items out by circ_modifier
89 CREATE TABLE config.circ_matrix_circ_mod_test (
90 id SERIAL PRIMARY KEY,
91 matchpoint INT NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
92 items_out INT NOT NULL -- Total current active circulations must be less than this, NULL means skip (always pass)
95 CREATE TABLE config.circ_matrix_circ_mod_test_map (
96 id SERIAL PRIMARY KEY,
97 circ_mod_test INT NOT NULL REFERENCES config.circ_matrix_circ_mod_test (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
98 circ_mod TEXT NOT NULL REFERENCES config.circ_modifier (code) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
99 CONSTRAINT cm_once_per_test UNIQUE (circ_mod_test, circ_mod)
102 CREATE TYPE action.found_circ_matrix_matchpoint AS ( success BOOL, matchpoint config.circ_matrix_matchpoint, buildrows INT[] );
104 CREATE OR REPLACE FUNCTION action.find_circ_matrix_matchpoint( context_ou INT, item_object asset.copy, user_object actor.usr, renewal BOOL ) RETURNS action.found_circ_matrix_matchpoint AS $func$
106 cn_object asset.call_number%ROWTYPE;
107 rec_descriptor metabib.rec_descriptor%ROWTYPE;
108 cur_matchpoint config.circ_matrix_matchpoint%ROWTYPE;
109 matchpoint config.circ_matrix_matchpoint%ROWTYPE;
110 weights config.circ_matrix_weights%ROWTYPE;
112 denominator NUMERIC(6,2);
114 result action.found_circ_matrix_matchpoint;
117 result.success = false;
120 SELECT INTO cn_object * FROM asset.call_number WHERE id = item_object.call_number;
121 SELECT INTO rec_descriptor * FROM metabib.rec_descriptor WHERE record = cn_object.record;
123 -- Pre-generate this so we only calc it once
124 IF user_object.dob IS NOT NULL THEN
125 SELECT INTO user_age age(user_object.dob);
128 -- Grab the closest set circ weight setting.
129 SELECT INTO weights cw.*
130 FROM config.weight_assoc wa
131 JOIN config.circ_matrix_weights cw ON (cw.id = wa.circ_weights)
132 JOIN actor.org_unit_ancestors_distance( context_ou ) d ON (wa.org_unit = d.id)
137 -- No weights? Bad admin! Defaults to handle that anyway.
138 IF weights.id IS NULL THEN
140 weights.org_unit := 10.0;
141 weights.circ_modifier := 5.0;
142 weights.marc_type := 4.0;
143 weights.marc_form := 3.0;
144 weights.marc_bib_level := 2.0;
145 weights.marc_vr_format := 2.0;
146 weights.copy_circ_lib := 8.0;
147 weights.copy_owning_lib := 8.0;
148 weights.user_home_ou := 8.0;
149 weights.ref_flag := 1.0;
150 weights.juvenile_flag := 6.0;
151 weights.is_renewal := 7.0;
152 weights.usr_age_lower_bound := 0.0;
153 weights.usr_age_upper_bound := 0.0;
156 -- Determine the max (expected) depth (+1) of the org tree and max depth of the permisson tree
157 -- If you break your org tree with funky parenting this may be wrong
158 -- Note: This CTE is duplicated in the find_hold_matrix_matchpoint function, and it may be a good idea to split it off to a function
159 -- We use one denominator for all tree-based checks for when permission groups and org units have the same weighting
160 WITH all_distance(distance) AS (
161 SELECT depth AS distance FROM actor.org_unit_type
163 SELECT distance AS distance FROM permission.grp_ancestors_distance((SELECT id FROM permission.grp_tree WHERE parent IS NULL))
165 SELECT INTO denominator MAX(distance) + 1 FROM all_distance;
167 -- Loop over all the potential matchpoints
168 FOR cur_matchpoint IN
170 FROM config.circ_matrix_matchpoint m
171 /*LEFT*/ JOIN permission.grp_ancestors_distance( user_object.profile ) upgad ON m.grp = upgad.id
172 /*LEFT*/ JOIN actor.org_unit_ancestors_distance( context_ou ) ctoua ON m.org_unit = ctoua.id
173 LEFT JOIN actor.org_unit_ancestors_distance( cn_object.owning_lib ) cnoua ON m.copy_owning_lib = cnoua.id
174 LEFT JOIN actor.org_unit_ancestors_distance( item_object.circ_lib ) iooua ON m.copy_circ_lib = iooua.id
175 LEFT JOIN actor.org_unit_ancestors_distance( user_object.home_ou ) uhoua ON m.user_home_ou = uhoua.id
178 -- AND (m.grp IS NULL OR upgad.id IS NOT NULL) -- Optional Permission Group?
180 -- AND (m.org_unit IS NULL OR ctoua.id IS NOT NULL) -- Optional Org Unit?
181 AND (m.copy_owning_lib IS NULL OR cnoua.id IS NOT NULL)
182 AND (m.copy_circ_lib IS NULL OR iooua.id IS NOT NULL)
183 AND (m.user_home_ou IS NULL OR uhoua.id IS NOT NULL)
185 AND (m.is_renewal IS NULL OR m.is_renewal = renewal)
186 -- Static User Checks
187 AND (m.juvenile_flag IS NULL OR m.juvenile_flag = user_object.juvenile)
188 AND (m.usr_age_lower_bound IS NULL OR (user_age IS NOT NULL AND m.usr_age_lower_bound < user_age))
189 AND (m.usr_age_upper_bound IS NULL OR (user_age IS NOT NULL AND m.usr_age_upper_bound > user_age))
190 -- Static Item Checks
191 AND (m.circ_modifier IS NULL OR m.circ_modifier = item_object.circ_modifier)
192 AND (m.marc_type IS NULL OR m.marc_type = COALESCE(item_object.circ_as_type, rec_descriptor.item_type))
193 AND (m.marc_form IS NULL OR m.marc_form = rec_descriptor.item_form)
194 AND (m.marc_bib_level IS NULL OR m.marc_bib_level = rec_descriptor.bib_level)
195 AND (m.marc_vr_format IS NULL OR m.marc_vr_format = rec_descriptor.vr_format)
196 AND (m.ref_flag IS NULL OR m.ref_flag = item_object.ref)
199 CASE WHEN upgad.distance IS NOT NULL THEN 2^(2*weights.grp - (upgad.distance/denominator)) ELSE 0.0 END +
201 CASE WHEN ctoua.distance IS NOT NULL THEN 2^(2*weights.org_unit - (ctoua.distance/denominator)) ELSE 0.0 END +
202 CASE WHEN cnoua.distance IS NOT NULL THEN 2^(2*weights.copy_owning_lib - (cnoua.distance/denominator)) ELSE 0.0 END +
203 CASE WHEN iooua.distance IS NOT NULL THEN 2^(2*weights.copy_circ_lib - (iooua.distance/denominator)) ELSE 0.0 END +
204 CASE WHEN uhoua.distance IS NOT NULL THEN 2^(2*weights.user_home_ou - (uhoua.distance/denominator)) ELSE 0.0 END +
205 -- Circ Type -- Note: 4^x is equiv to 2^(2*x)
206 CASE WHEN m.is_renewal IS NOT NULL THEN 4^weights.is_renewal ELSE 0.0 END +
207 -- Static User Checks
208 CASE WHEN m.juvenile_flag IS NOT NULL THEN 4^weights.juvenile_flag ELSE 0.0 END +
209 CASE WHEN m.usr_age_lower_bound IS NOT NULL THEN 4^weights.usr_age_lower_bound ELSE 0.0 END +
210 CASE WHEN m.usr_age_upper_bound IS NOT NULL THEN 4^weights.usr_age_upper_bound ELSE 0.0 END +
211 -- Static Item Checks
212 CASE WHEN m.circ_modifier IS NOT NULL THEN 4^weights.circ_modifier ELSE 0.0 END +
213 CASE WHEN m.marc_type IS NOT NULL THEN 4^weights.marc_type ELSE 0.0 END +
214 CASE WHEN m.marc_form IS NOT NULL THEN 4^weights.marc_form ELSE 0.0 END +
215 CASE WHEN m.marc_vr_format IS NOT NULL THEN 4^weights.marc_vr_format ELSE 0.0 END +
216 CASE WHEN m.ref_flag IS NOT NULL THEN 4^weights.ref_flag ELSE 0.0 END DESC,
217 -- Final sort on id, so that if two rules have the same sorting in the previous sort they have a defined order
218 -- This prevents "we changed the table order by updating a rule, and we started getting different results"
221 -- Record the full matching row list
222 row_list := row_list || cur_matchpoint.id;
224 -- No matchpoint yet?
225 IF matchpoint.id IS NULL THEN
226 -- Take the entire matchpoint as a starting point
227 matchpoint := cur_matchpoint;
228 CONTINUE; -- No need to look at this row any more.
231 -- Incomplete matchpoint?
232 IF matchpoint.circulate IS NULL THEN
233 matchpoint.circulate := cur_matchpoint.circulate;
235 IF matchpoint.duration_rule IS NULL THEN
236 matchpoint.duration_rule := cur_matchpoint.duration_rule;
238 IF matchpoint.recurring_fine_rule IS NULL THEN
239 matchpoint.recurring_fine_rule := cur_matchpoint.recurring_fine_rule;
241 IF matchpoint.max_fine_rule IS NULL THEN
242 matchpoint.max_fine_rule := cur_matchpoint.max_fine_rule;
244 IF matchpoint.hard_due_date IS NULL THEN
245 matchpoint.hard_due_date := cur_matchpoint.hard_due_date;
247 IF matchpoint.total_copy_hold_ratio IS NULL THEN
248 matchpoint.total_copy_hold_ratio := cur_matchpoint.total_copy_hold_ratio;
250 IF matchpoint.available_copy_hold_ratio IS NULL THEN
251 matchpoint.available_copy_hold_ratio := cur_matchpoint.available_copy_hold_ratio;
253 IF matchpoint.renewals IS NULL THEN
254 matchpoint.renewals := cur_matchpoint.renewals;
256 IF matchpoint.grace_period IS NULL THEN
257 matchpoint.grace_period := cur_matchpoint.grace_period;
261 -- Check required fields
262 IF matchpoint.circulate IS NOT NULL AND
263 matchpoint.duration_rule IS NOT NULL AND
264 matchpoint.recurring_fine_rule IS NOT NULL AND
265 matchpoint.max_fine_rule IS NOT NULL THEN
266 -- All there? We have a completed match.
267 result.success := true;
270 -- Include the assembled matchpoint, even if it isn't complete
271 result.matchpoint := matchpoint;
273 -- Include (for debugging) the full list of matching rows
274 result.buildrows := row_list;
276 -- Hand the result back to caller
279 $func$ LANGUAGE plpgsql;
281 -- Helper function - For manual calling, it can be easier to pass in IDs instead of objects
282 CREATE OR REPLACE FUNCTION action.find_circ_matrix_matchpoint( context_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS SETOF action.found_circ_matrix_matchpoint AS $func$
284 item_object asset.copy%ROWTYPE;
285 user_object actor.usr%ROWTYPE;
287 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
288 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
290 RETURN QUERY SELECT * FROM action.find_circ_matrix_matchpoint( context_ou, item_object, user_object, renewal );
292 $func$ LANGUAGE plpgsql;
294 CREATE TYPE action.hold_stats AS (
298 total_copy_ratio FLOAT,
299 available_copy_ratio FLOAT
302 CREATE OR REPLACE FUNCTION action.copy_related_hold_stats (copy_id INT) RETURNS action.hold_stats AS $func$
304 output action.hold_stats%ROWTYPE;
307 available_count INT := 0;
308 hold_map_data RECORD;
311 output.hold_count := 0;
312 output.copy_count := 0;
313 output.available_count := 0;
315 SELECT COUNT( DISTINCT m.hold ) INTO hold_count
316 FROM action.hold_copy_map m
317 JOIN action.hold_request h ON (m.hold = h.id)
318 WHERE m.target_copy = copy_id
321 output.hold_count := hold_count;
323 IF output.hold_count > 0 THEN
325 SELECT DISTINCT m.target_copy,
327 FROM action.hold_copy_map m
328 JOIN asset.copy acp ON (m.target_copy = acp.id)
329 JOIN action.hold_request h ON (m.hold = h.id)
330 WHERE m.hold IN ( SELECT DISTINCT hold FROM action.hold_copy_map WHERE target_copy = copy_id ) AND NOT h.frozen
332 output.copy_count := output.copy_count + 1;
333 IF hold_map_data.status IN (0,7,12) THEN
334 output.available_count := output.available_count + 1;
337 output.total_copy_ratio = output.copy_count::FLOAT / output.hold_count::FLOAT;
338 output.available_copy_ratio = output.available_count::FLOAT / output.hold_count::FLOAT;
345 $func$ LANGUAGE PLPGSQL;
347 CREATE TYPE action.circ_matrix_test_result AS ( success BOOL, fail_part TEXT, buildrows INT[], matchpoint INT, circulate BOOL, duration_rule INT, recurring_fine_rule INT, max_fine_rule INT, hard_due_date INT, renewals INT, grace_period INTERVAL );
348 CREATE OR REPLACE FUNCTION action.item_user_circ_test( circ_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS SETOF action.circ_matrix_test_result AS $func$
350 user_object actor.usr%ROWTYPE;
351 standing_penalty config.standing_penalty%ROWTYPE;
352 item_object asset.copy%ROWTYPE;
353 item_status_object config.copy_status%ROWTYPE;
354 item_location_object asset.copy_location%ROWTYPE;
355 result action.circ_matrix_test_result;
356 circ_test action.found_circ_matrix_matchpoint;
357 circ_matchpoint config.circ_matrix_matchpoint%ROWTYPE;
358 out_by_circ_mod config.circ_matrix_circ_mod_test%ROWTYPE;
359 circ_mod_map config.circ_matrix_circ_mod_test_map%ROWTYPE;
360 hold_ratio action.hold_stats%ROWTYPE;
363 context_org_list INT[];
366 -- Assume success unless we hit a failure condition
367 result.success := TRUE;
369 -- Fail if the user is BARRED
370 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
372 -- Fail if we couldn't find the user
373 IF user_object.id IS NULL THEN
374 result.fail_part := 'no_user';
375 result.success := FALSE;
381 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
383 -- Fail if we couldn't find the item
384 IF item_object.id IS NULL THEN
385 result.fail_part := 'no_item';
386 result.success := FALSE;
392 IF user_object.barred IS TRUE THEN
393 result.fail_part := 'actor.usr.barred';
394 result.success := FALSE;
399 -- Fail if the item can't circulate
400 IF item_object.circulate IS FALSE THEN
401 result.fail_part := 'asset.copy.circulate';
402 result.success := FALSE;
407 -- Fail if the item isn't in a circulateable status on a non-renewal
408 IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN
409 result.fail_part := 'asset.copy.status';
410 result.success := FALSE;
413 ELSIF renewal AND item_object.status <> 1 THEN
414 result.fail_part := 'asset.copy.status';
415 result.success := FALSE;
420 -- Fail if the item can't circulate because of the shelving location
421 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
422 IF item_location_object.circulate IS FALSE THEN
423 result.fail_part := 'asset.copy_location.circulate';
424 result.success := FALSE;
429 SELECT INTO circ_test * FROM action.find_circ_matrix_matchpoint(circ_ou, item_object, user_object, renewal);
431 circ_matchpoint := circ_test.matchpoint;
432 result.matchpoint := circ_matchpoint.id;
433 result.circulate := circ_matchpoint.circulate;
434 result.duration_rule := circ_matchpoint.duration_rule;
435 result.recurring_fine_rule := circ_matchpoint.recurring_fine_rule;
436 result.max_fine_rule := circ_matchpoint.max_fine_rule;
437 result.hard_due_date := circ_matchpoint.hard_due_date;
438 result.renewals := circ_matchpoint.renewals;
439 result.grace_period := circ_matchpoint.grace_period;
440 result.buildrows := circ_test.buildrows;
442 -- Fail if we couldn't find a matchpoint
443 IF circ_test.success = false THEN
444 result.fail_part := 'no_matchpoint';
445 result.success := FALSE;
448 RETURN; -- All tests after this point require a matchpoint. No sense in running on an incomplete or missing one.
451 -- Apparently....use the circ matchpoint org unit to determine what org units are valid.
452 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( circ_matchpoint.org_unit );
455 penalty_type = '%RENEW%';
457 penalty_type = '%CIRC%';
460 FOR standing_penalty IN
461 SELECT DISTINCT csp.*
462 FROM actor.usr_standing_penalty usp
463 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
464 WHERE usr = match_user
465 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
466 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
467 AND csp.block_list LIKE penalty_type LOOP
469 result.fail_part := standing_penalty.name;
470 result.success := FALSE;
475 -- Fail if the test is set to hard non-circulating
476 IF circ_matchpoint.circulate IS FALSE THEN
477 result.fail_part := 'config.circ_matrix_test.circulate';
478 result.success := FALSE;
483 -- Fail if the total copy-hold ratio is too low
484 IF circ_matchpoint.total_copy_hold_ratio IS NOT NULL THEN
485 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
486 IF hold_ratio.total_copy_ratio IS NOT NULL AND hold_ratio.total_copy_ratio < circ_matchpoint.total_copy_hold_ratio THEN
487 result.fail_part := 'config.circ_matrix_test.total_copy_hold_ratio';
488 result.success := FALSE;
494 -- Fail if the available copy-hold ratio is too low
495 IF circ_matchpoint.available_copy_hold_ratio IS NOT NULL THEN
496 IF hold_ratio.hold_count IS NULL THEN
497 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
499 IF hold_ratio.available_copy_ratio IS NOT NULL AND hold_ratio.available_copy_ratio < circ_matchpoint.available_copy_hold_ratio THEN
500 result.fail_part := 'config.circ_matrix_test.available_copy_hold_ratio';
501 result.success := FALSE;
507 -- Fail if the user has too many items with specific circ_modifiers checked out
508 FOR out_by_circ_mod IN SELECT * FROM config.circ_matrix_circ_mod_test WHERE matchpoint = circ_matchpoint.id LOOP
509 SELECT INTO items_out COUNT(*)
510 FROM action.circulation circ
511 JOIN asset.copy cp ON (cp.id = circ.target_copy)
512 WHERE circ.usr = match_user
513 AND circ.circ_lib IN ( SELECT * FROM unnest(context_org_list) )
514 AND circ.checkin_time IS NULL
515 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL)
516 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);
517 IF items_out >= out_by_circ_mod.items_out THEN
518 result.fail_part := 'config.circ_matrix_circ_mod_test';
519 result.success := FALSE;
525 -- If we passed everything, return the successful matchpoint id
532 $func$ LANGUAGE plpgsql;
534 CREATE OR REPLACE FUNCTION action.item_user_circ_test( INT, BIGINT, INT ) RETURNS SETOF action.circ_matrix_test_result AS $func$
535 SELECT * FROM action.item_user_circ_test( $1, $2, $3, FALSE );
538 CREATE OR REPLACE FUNCTION action.item_user_renew_test( INT, BIGINT, INT ) RETURNS SETOF action.circ_matrix_test_result AS $func$
539 SELECT * FROM action.item_user_circ_test( $1, $2, $3, TRUE );
543 CREATE OR REPLACE FUNCTION actor.calculate_system_penalties( match_user INT, context_org INT ) RETURNS SETOF actor.usr_standing_penalty AS $func$
545 user_object actor.usr%ROWTYPE;
546 new_sp_row actor.usr_standing_penalty%ROWTYPE;
547 existing_sp_row actor.usr_standing_penalty%ROWTYPE;
548 collections_fines permission.grp_penalty_threshold%ROWTYPE;
549 max_fines permission.grp_penalty_threshold%ROWTYPE;
550 max_overdue permission.grp_penalty_threshold%ROWTYPE;
551 max_items_out permission.grp_penalty_threshold%ROWTYPE;
555 context_org_list INT[];
556 current_fines NUMERIC(8,2) := 0.0;
557 tmp_fines NUMERIC(8,2);
560 tmp_org actor.org_unit%ROWTYPE;
561 tmp_penalty config.standing_penalty%ROWTYPE;
564 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
567 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
569 -- Fail if the user has a high fine balance
571 tmp_grp := user_object.profile;
573 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 1 AND org_unit = tmp_org.id;
575 IF max_fines.threshold IS NULL THEN
576 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
581 IF tmp_grp IS NULL THEN
586 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
590 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
594 IF max_fines.threshold IS NOT NULL THEN
598 FROM actor.usr_standing_penalty
599 WHERE usr = match_user
600 AND org_unit = max_fines.org_unit
601 AND (stop_date IS NULL or stop_date > NOW())
602 AND standing_penalty = 1;
604 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( max_fines.org_unit );
606 SELECT SUM(f.balance_owed) INTO current_fines
607 FROM money.materialized_billable_xact_summary f
610 FROM booking.reservation r
611 WHERE r.usr = match_user
612 AND r.pickup_lib IN (SELECT * FROM unnest(context_org_list))
613 AND xact_finish IS NULL
617 WHERE g.usr = match_user
618 AND g.billing_location IN (SELECT * FROM unnest(context_org_list))
619 AND xact_finish IS NULL
622 FROM action.circulation circ
623 WHERE circ.usr = match_user
624 AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
625 AND xact_finish IS NULL ) l USING (id);
627 IF current_fines >= max_fines.threshold THEN
628 new_sp_row.usr := match_user;
629 new_sp_row.org_unit := max_fines.org_unit;
630 new_sp_row.standing_penalty := 1;
631 RETURN NEXT new_sp_row;
635 -- Start over for max overdue
636 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
638 -- Fail if the user has too many overdue items
640 tmp_grp := user_object.profile;
643 SELECT * INTO max_overdue FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 2 AND org_unit = tmp_org.id;
645 IF max_overdue.threshold IS NULL THEN
646 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
651 IF tmp_grp IS NULL THEN
656 IF max_overdue.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
660 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
664 IF max_overdue.threshold IS NOT NULL THEN
668 FROM actor.usr_standing_penalty
669 WHERE usr = match_user
670 AND org_unit = max_overdue.org_unit
671 AND (stop_date IS NULL or stop_date > NOW())
672 AND standing_penalty = 2;
674 SELECT INTO items_overdue COUNT(*)
675 FROM action.circulation circ
676 JOIN actor.org_unit_full_path( max_overdue.org_unit ) fp ON (circ.circ_lib = fp.id)
677 WHERE circ.usr = match_user
678 AND circ.checkin_time IS NULL
679 AND circ.due_date < NOW()
680 AND (circ.stop_fines = 'MAXFINES' OR circ.stop_fines IS NULL);
682 IF items_overdue >= max_overdue.threshold::INT THEN
683 new_sp_row.usr := match_user;
684 new_sp_row.org_unit := max_overdue.org_unit;
685 new_sp_row.standing_penalty := 2;
686 RETURN NEXT new_sp_row;
690 -- Start over for max out
691 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
693 -- Fail if the user has too many checked out items
695 tmp_grp := user_object.profile;
697 SELECT * INTO max_items_out FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 3 AND org_unit = tmp_org.id;
699 IF max_items_out.threshold IS NULL THEN
700 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
705 IF tmp_grp IS NULL THEN
710 IF max_items_out.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
714 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
719 -- Fail if the user has too many items checked out
720 IF max_items_out.threshold IS NOT NULL THEN
724 FROM actor.usr_standing_penalty
725 WHERE usr = match_user
726 AND org_unit = max_items_out.org_unit
727 AND (stop_date IS NULL or stop_date > NOW())
728 AND standing_penalty = 3;
730 SELECT INTO items_out COUNT(*)
731 FROM action.circulation circ
732 JOIN actor.org_unit_full_path( max_items_out.org_unit ) fp ON (circ.circ_lib = fp.id)
733 WHERE circ.usr = match_user
734 AND circ.checkin_time IS NULL
735 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL);
737 IF items_out >= max_items_out.threshold::INT THEN
738 new_sp_row.usr := match_user;
739 new_sp_row.org_unit := max_items_out.org_unit;
740 new_sp_row.standing_penalty := 3;
741 RETURN NEXT new_sp_row;
745 -- Start over for collections warning
746 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
748 -- Fail if the user has a collections-level fine balance
750 tmp_grp := user_object.profile;
752 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 4 AND org_unit = tmp_org.id;
754 IF max_fines.threshold IS NULL THEN
755 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
760 IF tmp_grp IS NULL THEN
765 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
769 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
773 IF max_fines.threshold IS NOT NULL THEN
777 FROM actor.usr_standing_penalty
778 WHERE usr = match_user
779 AND org_unit = max_fines.org_unit
780 AND (stop_date IS NULL or stop_date > NOW())
781 AND standing_penalty = 4;
783 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( max_fines.org_unit );
785 SELECT SUM(f.balance_owed) INTO current_fines
786 FROM money.materialized_billable_xact_summary f
789 FROM booking.reservation r
790 WHERE r.usr = match_user
791 AND r.pickup_lib IN (SELECT * FROM unnest(context_org_list))
792 AND r.xact_finish IS NULL
796 WHERE g.usr = match_user
797 AND g.billing_location IN (SELECT * FROM unnest(context_org_list))
798 AND g.xact_finish IS NULL
801 FROM action.circulation circ
802 WHERE circ.usr = match_user
803 AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
804 AND circ.xact_finish IS NULL ) l USING (id);
806 IF current_fines >= max_fines.threshold THEN
807 new_sp_row.usr := match_user;
808 new_sp_row.org_unit := max_fines.org_unit;
809 new_sp_row.standing_penalty := 4;
810 RETURN NEXT new_sp_row;
814 -- Start over for in collections
815 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
817 -- Remove the in-collections penalty if the user has paid down enough
818 -- This penalty is different, because this code is not responsible for creating
819 -- new in-collections penalties, only for removing them
821 tmp_grp := user_object.profile;
823 SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 30 AND org_unit = tmp_org.id;
825 IF max_fines.threshold IS NULL THEN
826 SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
831 IF tmp_grp IS NULL THEN
836 IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
840 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
844 IF max_fines.threshold IS NOT NULL THEN
846 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( max_fines.org_unit );
848 -- first, see if the user had paid down to the threshold
849 SELECT SUM(f.balance_owed) INTO current_fines
850 FROM money.materialized_billable_xact_summary f
853 FROM booking.reservation r
854 WHERE r.usr = match_user
855 AND r.pickup_lib IN (SELECT * FROM unnest(context_org_list))
856 AND r.xact_finish IS NULL
860 WHERE g.usr = match_user
861 AND g.billing_location IN (SELECT * FROM unnest(context_org_list))
862 AND g.xact_finish IS NULL
865 FROM action.circulation circ
866 WHERE circ.usr = match_user
867 AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
868 AND circ.xact_finish IS NULL ) l USING (id);
870 IF current_fines IS NULL OR current_fines <= max_fines.threshold THEN
871 -- patron has paid down enough
873 SELECT INTO tmp_penalty * FROM config.standing_penalty WHERE id = 30;
875 IF tmp_penalty.org_depth IS NOT NULL THEN
877 -- since this code is not responsible for applying the penalty, it can't
878 -- guarantee the current context org will match the org at which the penalty
879 --- was applied. search up the org tree until we hit the configured penalty depth
880 SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
881 SELECT INTO tmp_depth depth FROM actor.org_unit_type WHERE id = tmp_org.ou_type;
883 WHILE tmp_depth >= tmp_penalty.org_depth LOOP
887 FROM actor.usr_standing_penalty
888 WHERE usr = match_user
889 AND org_unit = tmp_org.id
890 AND (stop_date IS NULL or stop_date > NOW())
891 AND standing_penalty = 30;
893 IF tmp_org.parent_ou IS NULL THEN
897 SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
898 SELECT INTO tmp_depth depth FROM actor.org_unit_type WHERE id = tmp_org.ou_type;
903 -- no penalty depth is defined, look for exact matches
907 FROM actor.usr_standing_penalty
908 WHERE usr = match_user
909 AND org_unit = max_fines.org_unit
910 AND (stop_date IS NULL or stop_date > NOW())
911 AND standing_penalty = 30;
920 $func$ LANGUAGE plpgsql;