3 SELECT evergreen.upgrade_deps_block_check('0720', :eg_version);
5 ALTER TABLE config.circ_matrix_weights
6 ADD COLUMN copy_location NUMERIC(6,2) NOT NULL DEFAULT 5.0;
7 UPDATE config.circ_matrix_weights
8 SET copy_location = 0.0 WHERE name = 'All_Equal';
9 ALTER TABLE config.circ_matrix_weights
10 ALTER COLUMN copy_location DROP DEFAULT; -- for consistency w/ baseline schema
12 ALTER TABLE config.circ_matrix_matchpoint
13 ADD COLUMN copy_location INTEGER REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED;
15 DROP INDEX config.ccmm_once_per_paramset;
17 CREATE UNIQUE INDEX ccmm_once_per_paramset ON config.circ_matrix_matchpoint (org_unit, grp, COALESCE(circ_modifier, ''), COALESCE(copy_location::TEXT, ''), 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, ''), COALESCE(item_age::TEXT, '')) WHERE active;
19 -- Linkage between limit sets and circ mods
20 CREATE TABLE config.circ_limit_set_copy_loc_map (
21 id SERIAL PRIMARY KEY,
22 limit_set INT NOT NULL REFERENCES config.circ_limit_set (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
23 copy_loc INT NOT NULL REFERENCES asset.copy_location (id) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
24 CONSTRAINT cl_once_per_set UNIQUE (limit_set, copy_loc)
27 -- Add support for checking config.circ_limit_set_copy_loc_map's
28 CREATE OR REPLACE FUNCTION action.item_user_circ_test( circ_ou INT, match_item BIGINT, match_user INT, renewal BOOL )
29 RETURNS SETOF action.circ_matrix_test_result AS $func$
31 user_object actor.usr%ROWTYPE;
32 standing_penalty config.standing_penalty%ROWTYPE;
33 item_object asset.copy%ROWTYPE;
34 item_status_object config.copy_status%ROWTYPE;
35 item_location_object asset.copy_location%ROWTYPE;
36 result action.circ_matrix_test_result;
37 circ_test action.found_circ_matrix_matchpoint;
38 circ_matchpoint config.circ_matrix_matchpoint%ROWTYPE;
39 circ_limit_set config.circ_limit_set%ROWTYPE;
40 hold_ratio action.hold_stats%ROWTYPE;
43 context_org_list INT[];
46 -- Assume success unless we hit a failure condition
47 result.success := TRUE;
49 -- Need user info to look up matchpoints
50 SELECT INTO user_object * FROM actor.usr WHERE id = match_user AND NOT deleted;
52 -- (Insta)Fail if we couldn't find the user
53 IF user_object.id IS NULL THEN
54 result.fail_part := 'no_user';
55 result.success := FALSE;
61 -- Need item info to look up matchpoints
62 SELECT INTO item_object * FROM asset.copy WHERE id = match_item AND NOT deleted;
64 -- (Insta)Fail if we couldn't find the item
65 IF item_object.id IS NULL THEN
66 result.fail_part := 'no_item';
67 result.success := FALSE;
73 SELECT INTO circ_test * FROM action.find_circ_matrix_matchpoint(circ_ou, item_object, user_object, renewal);
75 circ_matchpoint := circ_test.matchpoint;
76 result.matchpoint := circ_matchpoint.id;
77 result.circulate := circ_matchpoint.circulate;
78 result.duration_rule := circ_matchpoint.duration_rule;
79 result.recurring_fine_rule := circ_matchpoint.recurring_fine_rule;
80 result.max_fine_rule := circ_matchpoint.max_fine_rule;
81 result.hard_due_date := circ_matchpoint.hard_due_date;
82 result.renewals := circ_matchpoint.renewals;
83 result.grace_period := circ_matchpoint.grace_period;
84 result.buildrows := circ_test.buildrows;
86 -- (Insta)Fail if we couldn't find a matchpoint
87 IF circ_test.success = false THEN
88 result.fail_part := 'no_matchpoint';
89 result.success := FALSE;
95 -- All failures before this point are non-recoverable
96 -- Below this point are possibly overridable failures
98 -- Fail if the user is barred
99 IF user_object.barred IS TRUE THEN
100 result.fail_part := 'actor.usr.barred';
101 result.success := FALSE;
106 -- Fail if the item can't circulate
107 IF item_object.circulate IS FALSE THEN
108 result.fail_part := 'asset.copy.circulate';
109 result.success := FALSE;
114 -- Fail if the item isn't in a circulateable status on a non-renewal
115 IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN
116 result.fail_part := 'asset.copy.status';
117 result.success := FALSE;
120 -- Alternately, fail if the item isn't checked out on a renewal
121 ELSIF renewal AND item_object.status <> 1 THEN
122 result.fail_part := 'asset.copy.status';
123 result.success := FALSE;
128 -- Fail if the item can't circulate because of the shelving location
129 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
130 IF item_location_object.circulate IS FALSE THEN
131 result.fail_part := 'asset.copy_location.circulate';
132 result.success := FALSE;
137 -- Use Circ OU for penalties and such
138 SELECT INTO context_org_list ARRAY_AGG(id) FROM actor.org_unit_full_path( circ_ou );
141 penalty_type = '%RENEW%';
143 penalty_type = '%CIRC%';
146 FOR standing_penalty IN
147 SELECT DISTINCT csp.*
148 FROM actor.usr_standing_penalty usp
149 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
150 WHERE usr = match_user
151 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
152 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
153 AND csp.block_list LIKE penalty_type LOOP
155 result.fail_part := standing_penalty.name;
156 result.success := FALSE;
161 -- Fail if the test is set to hard non-circulating
162 IF circ_matchpoint.circulate IS FALSE THEN
163 result.fail_part := 'config.circ_matrix_test.circulate';
164 result.success := FALSE;
169 -- Fail if the total copy-hold ratio is too low
170 IF circ_matchpoint.total_copy_hold_ratio IS NOT NULL THEN
171 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
172 IF hold_ratio.total_copy_ratio IS NOT NULL AND hold_ratio.total_copy_ratio < circ_matchpoint.total_copy_hold_ratio THEN
173 result.fail_part := 'config.circ_matrix_test.total_copy_hold_ratio';
174 result.success := FALSE;
180 -- Fail if the available copy-hold ratio is too low
181 IF circ_matchpoint.available_copy_hold_ratio IS NOT NULL THEN
182 IF hold_ratio.hold_count IS NULL THEN
183 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
185 IF hold_ratio.available_copy_ratio IS NOT NULL AND hold_ratio.available_copy_ratio < circ_matchpoint.available_copy_hold_ratio THEN
186 result.fail_part := 'config.circ_matrix_test.available_copy_hold_ratio';
187 result.success := FALSE;
193 -- Fail if the user has too many items out by defined limit sets
194 FOR circ_limit_set IN SELECT ccls.* FROM config.circ_limit_set ccls
195 JOIN config.circ_matrix_limit_set_map ccmlsm ON ccmlsm.limit_set = ccls.id
196 WHERE ccmlsm.active AND ( ccmlsm.matchpoint = circ_matchpoint.id OR
197 ( ccmlsm.matchpoint IN (SELECT * FROM unnest(result.buildrows)) AND ccmlsm.fallthrough )
199 IF circ_limit_set.items_out > 0 AND NOT renewal THEN
200 SELECT INTO context_org_list ARRAY_AGG(aou.id)
201 FROM actor.org_unit_full_path( circ_ou ) aou
202 JOIN actor.org_unit_type aout ON aou.ou_type = aout.id
203 WHERE aout.depth >= circ_limit_set.depth;
204 IF circ_limit_set.global THEN
205 WITH RECURSIVE descendant_depth AS (
208 FROM actor.org_unit ou
209 WHERE ou.id IN (SELECT * FROM unnest(context_org_list))
213 FROM actor.org_unit ou
214 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
215 ) SELECT INTO context_org_list ARRAY_AGG(ou.id) FROM actor.org_unit ou JOIN descendant_depth USING (id);
217 SELECT INTO items_out COUNT(DISTINCT circ.id)
218 FROM action.circulation circ
219 JOIN asset.copy copy ON (copy.id = circ.target_copy)
220 LEFT JOIN action.circulation_limit_group_map aclgm ON (circ.id = aclgm.circ)
221 WHERE circ.usr = match_user
222 AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
223 AND circ.checkin_time IS NULL
224 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL)
225 AND (copy.circ_modifier IN (SELECT circ_mod FROM config.circ_limit_set_circ_mod_map WHERE limit_set = circ_limit_set.id)
226 OR copy.location IN (SELECT copy_loc FROM config.circ_limit_set_copy_loc_map WHERE limit_set = circ_limit_set.id)
227 OR aclgm.limit_group IN (SELECT limit_group FROM config.circ_limit_set_group_map WHERE limit_set = circ_limit_set.id)
229 IF items_out >= circ_limit_set.items_out THEN
230 result.fail_part := 'config.circ_matrix_circ_mod_test';
231 result.success := FALSE;
236 SELECT INTO result.limit_groups result.limit_groups || ARRAY_AGG(limit_group) FROM config.circ_limit_set_group_map WHERE limit_set = circ_limit_set.id AND NOT check_only;
239 -- If we passed everything, return the successful matchpoint
246 $func$ LANGUAGE plpgsql;
249 -- adding copy_loc to circ_matrix_matchpoint
250 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$
252 cn_object asset.call_number%ROWTYPE;
253 rec_descriptor metabib.rec_descriptor%ROWTYPE;
254 cur_matchpoint config.circ_matrix_matchpoint%ROWTYPE;
255 matchpoint config.circ_matrix_matchpoint%ROWTYPE;
256 weights config.circ_matrix_weights%ROWTYPE;
258 my_item_age INTERVAL;
259 denominator NUMERIC(6,2);
261 result action.found_circ_matrix_matchpoint;
264 result.success = false;
267 SELECT INTO cn_object * FROM asset.call_number WHERE id = item_object.call_number;
268 SELECT INTO rec_descriptor * FROM metabib.rec_descriptor WHERE record = cn_object.record;
270 -- Pre-generate this so we only calc it once
271 IF user_object.dob IS NOT NULL THEN
272 SELECT INTO user_age age(user_object.dob);
276 SELECT INTO my_item_age age(coalesce(item_object.active_date, now()));
278 -- Grab the closest set circ weight setting.
279 SELECT INTO weights cw.*
280 FROM config.weight_assoc wa
281 JOIN config.circ_matrix_weights cw ON (cw.id = wa.circ_weights)
282 JOIN actor.org_unit_ancestors_distance( context_ou ) d ON (wa.org_unit = d.id)
287 -- No weights? Bad admin! Defaults to handle that anyway.
288 IF weights.id IS NULL THEN
290 weights.org_unit := 10.0;
291 weights.circ_modifier := 5.0;
292 weights.copy_location := 5.0;
293 weights.marc_type := 4.0;
294 weights.marc_form := 3.0;
295 weights.marc_bib_level := 2.0;
296 weights.marc_vr_format := 2.0;
297 weights.copy_circ_lib := 8.0;
298 weights.copy_owning_lib := 8.0;
299 weights.user_home_ou := 8.0;
300 weights.ref_flag := 1.0;
301 weights.juvenile_flag := 6.0;
302 weights.is_renewal := 7.0;
303 weights.usr_age_lower_bound := 0.0;
304 weights.usr_age_upper_bound := 0.0;
305 weights.item_age := 0.0;
308 -- Determine the max (expected) depth (+1) of the org tree and max depth of the permisson tree
309 -- If you break your org tree with funky parenting this may be wrong
310 -- 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
311 -- We use one denominator for all tree-based checks for when permission groups and org units have the same weighting
312 WITH all_distance(distance) AS (
313 SELECT depth AS distance FROM actor.org_unit_type
315 SELECT distance AS distance FROM permission.grp_ancestors_distance((SELECT id FROM permission.grp_tree WHERE parent IS NULL))
317 SELECT INTO denominator MAX(distance) + 1 FROM all_distance;
319 -- Loop over all the potential matchpoints
320 FOR cur_matchpoint IN
322 FROM config.circ_matrix_matchpoint m
323 /*LEFT*/ JOIN permission.grp_ancestors_distance( user_object.profile ) upgad ON m.grp = upgad.id
324 /*LEFT*/ JOIN actor.org_unit_ancestors_distance( context_ou ) ctoua ON m.org_unit = ctoua.id
325 LEFT JOIN actor.org_unit_ancestors_distance( cn_object.owning_lib ) cnoua ON m.copy_owning_lib = cnoua.id
326 LEFT JOIN actor.org_unit_ancestors_distance( item_object.circ_lib ) iooua ON m.copy_circ_lib = iooua.id
327 LEFT JOIN actor.org_unit_ancestors_distance( user_object.home_ou ) uhoua ON m.user_home_ou = uhoua.id
330 -- AND (m.grp IS NULL OR upgad.id IS NOT NULL) -- Optional Permission Group?
332 -- AND (m.org_unit IS NULL OR ctoua.id IS NOT NULL) -- Optional Org Unit?
333 AND (m.copy_owning_lib IS NULL OR cnoua.id IS NOT NULL)
334 AND (m.copy_circ_lib IS NULL OR iooua.id IS NOT NULL)
335 AND (m.user_home_ou IS NULL OR uhoua.id IS NOT NULL)
337 AND (m.is_renewal IS NULL OR m.is_renewal = renewal)
338 -- Static User Checks
339 AND (m.juvenile_flag IS NULL OR m.juvenile_flag = user_object.juvenile)
340 AND (m.usr_age_lower_bound IS NULL OR (user_age IS NOT NULL AND m.usr_age_lower_bound < user_age))
341 AND (m.usr_age_upper_bound IS NULL OR (user_age IS NOT NULL AND m.usr_age_upper_bound > user_age))
342 -- Static Item Checks
343 AND (m.circ_modifier IS NULL OR m.circ_modifier = item_object.circ_modifier)
344 AND (m.copy_location IS NULL OR m.copy_location = item_object.location)
345 AND (m.marc_type IS NULL OR m.marc_type = COALESCE(item_object.circ_as_type, rec_descriptor.item_type))
346 AND (m.marc_form IS NULL OR m.marc_form = rec_descriptor.item_form)
347 AND (m.marc_bib_level IS NULL OR m.marc_bib_level = rec_descriptor.bib_level)
348 AND (m.marc_vr_format IS NULL OR m.marc_vr_format = rec_descriptor.vr_format)
349 AND (m.ref_flag IS NULL OR m.ref_flag = item_object.ref)
350 AND (m.item_age IS NULL OR (my_item_age IS NOT NULL AND m.item_age > my_item_age))
353 CASE WHEN upgad.distance IS NOT NULL THEN 2^(2*weights.grp - (upgad.distance/denominator)) ELSE 0.0 END +
355 CASE WHEN ctoua.distance IS NOT NULL THEN 2^(2*weights.org_unit - (ctoua.distance/denominator)) ELSE 0.0 END +
356 CASE WHEN cnoua.distance IS NOT NULL THEN 2^(2*weights.copy_owning_lib - (cnoua.distance/denominator)) ELSE 0.0 END +
357 CASE WHEN iooua.distance IS NOT NULL THEN 2^(2*weights.copy_circ_lib - (iooua.distance/denominator)) ELSE 0.0 END +
358 CASE WHEN uhoua.distance IS NOT NULL THEN 2^(2*weights.user_home_ou - (uhoua.distance/denominator)) ELSE 0.0 END +
359 -- Circ Type -- Note: 4^x is equiv to 2^(2*x)
360 CASE WHEN m.is_renewal IS NOT NULL THEN 4^weights.is_renewal ELSE 0.0 END +
361 -- Static User Checks
362 CASE WHEN m.juvenile_flag IS NOT NULL THEN 4^weights.juvenile_flag ELSE 0.0 END +
363 CASE WHEN m.usr_age_lower_bound IS NOT NULL THEN 4^weights.usr_age_lower_bound ELSE 0.0 END +
364 CASE WHEN m.usr_age_upper_bound IS NOT NULL THEN 4^weights.usr_age_upper_bound ELSE 0.0 END +
365 -- Static Item Checks
366 CASE WHEN m.circ_modifier IS NOT NULL THEN 4^weights.circ_modifier ELSE 0.0 END +
367 CASE WHEN m.copy_location IS NOT NULL THEN 4^weights.copy_location ELSE 0.0 END +
368 CASE WHEN m.marc_type IS NOT NULL THEN 4^weights.marc_type ELSE 0.0 END +
369 CASE WHEN m.marc_form IS NOT NULL THEN 4^weights.marc_form ELSE 0.0 END +
370 CASE WHEN m.marc_vr_format IS NOT NULL THEN 4^weights.marc_vr_format ELSE 0.0 END +
371 CASE WHEN m.ref_flag IS NOT NULL THEN 4^weights.ref_flag ELSE 0.0 END +
372 -- Item age has a slight adjustment to weight based on value.
373 -- This should ensure that a shorter age limit comes first when all else is equal.
374 -- NOTE: This assumes that intervals will normally be in days.
375 CASE WHEN m.item_age IS NOT NULL THEN 4^weights.item_age - 1 + 86400/EXTRACT(EPOCH FROM m.item_age) ELSE 0.0 END DESC,
376 -- Final sort on id, so that if two rules have the same sorting in the previous sort they have a defined order
377 -- This prevents "we changed the table order by updating a rule, and we started getting different results"
380 -- Record the full matching row list
381 row_list := row_list || cur_matchpoint.id;
383 -- No matchpoint yet?
384 IF matchpoint.id IS NULL THEN
385 -- Take the entire matchpoint as a starting point
386 matchpoint := cur_matchpoint;
387 CONTINUE; -- No need to look at this row any more.
390 -- Incomplete matchpoint?
391 IF matchpoint.circulate IS NULL THEN
392 matchpoint.circulate := cur_matchpoint.circulate;
394 IF matchpoint.duration_rule IS NULL THEN
395 matchpoint.duration_rule := cur_matchpoint.duration_rule;
397 IF matchpoint.recurring_fine_rule IS NULL THEN
398 matchpoint.recurring_fine_rule := cur_matchpoint.recurring_fine_rule;
400 IF matchpoint.max_fine_rule IS NULL THEN
401 matchpoint.max_fine_rule := cur_matchpoint.max_fine_rule;
403 IF matchpoint.hard_due_date IS NULL THEN
404 matchpoint.hard_due_date := cur_matchpoint.hard_due_date;
406 IF matchpoint.total_copy_hold_ratio IS NULL THEN
407 matchpoint.total_copy_hold_ratio := cur_matchpoint.total_copy_hold_ratio;
409 IF matchpoint.available_copy_hold_ratio IS NULL THEN
410 matchpoint.available_copy_hold_ratio := cur_matchpoint.available_copy_hold_ratio;
412 IF matchpoint.renewals IS NULL THEN
413 matchpoint.renewals := cur_matchpoint.renewals;
415 IF matchpoint.grace_period IS NULL THEN
416 matchpoint.grace_period := cur_matchpoint.grace_period;
420 -- Check required fields
421 IF matchpoint.circulate IS NOT NULL AND
422 matchpoint.duration_rule IS NOT NULL AND
423 matchpoint.recurring_fine_rule IS NOT NULL AND
424 matchpoint.max_fine_rule IS NOT NULL THEN
425 -- All there? We have a completed match.
426 result.success := true;
429 -- Include the assembled matchpoint, even if it isn't complete
430 result.matchpoint := matchpoint;
432 -- Include (for debugging) the full list of matching rows
433 result.buildrows := row_list;
435 -- Hand the result back to caller
438 $func$ LANGUAGE plpgsql;