4 SELECT evergreen.upgrade_deps_block_check('0721', :eg_version);
6 UPDATE config.standing_penalty
7 SET block_list = REPLACE(block_list, 'HOLD', 'HOLD|CAPTURE')
9 -- STAFF_ penalties have names that match their block list
10 name NOT LIKE 'STAFF_%'
11 -- belt & suspenders, also good for testing
12 AND block_list NOT LIKE '%CAPTURE%';
14 -- CIRC|FULFILL is now the same as CIRC previously was by itself
15 UPDATE config.standing_penalty
16 SET block_list = REPLACE(block_list, 'CIRC', 'CIRC|FULFILL')
18 -- STAFF_ penalties have names that match their block list
19 name NOT LIKE 'STAFF_%'
20 -- belt & suspenders, also good for testing
21 AND block_list NOT LIKE '%FULFILL%';
24 -- apply the HOLD vs CAPTURE block logic
25 CREATE OR REPLACE FUNCTION action.hold_request_permit_test( pickup_ou INT, request_ou INT, match_item BIGINT, match_user INT, match_requestor INT, retargetting BOOL ) RETURNS SETOF action.matrix_test_result AS $func$
28 user_object actor.usr%ROWTYPE;
29 age_protect_object config.rule_age_hold_protect%ROWTYPE;
30 standing_penalty config.standing_penalty%ROWTYPE;
31 transit_range_ou_type actor.org_unit_type%ROWTYPE;
32 transit_source actor.org_unit%ROWTYPE;
33 item_object asset.copy%ROWTYPE;
34 item_cn_object asset.call_number%ROWTYPE;
35 item_status_object config.copy_status%ROWTYPE;
36 item_location_object asset.copy_location%ROWTYPE;
37 ou_skip actor.org_unit_setting%ROWTYPE;
38 result action.matrix_test_result;
39 hold_test config.hold_matrix_matchpoint%ROWTYPE;
41 age_protect_date TIMESTAMP WITH TIME ZONE;
43 hold_transit_prox INT;
44 frozen_hold_count INT;
45 context_org_list INT[];
49 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
50 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( pickup_ou );
52 result.success := TRUE;
54 -- The HOLD penalty block only applies to new holds.
55 -- The CAPTURE penalty block applies to existing holds.
56 hold_penalty := 'HOLD';
58 hold_penalty := 'CAPTURE';
61 -- Fail if we couldn't find a user
62 IF user_object.id IS NULL THEN
63 result.fail_part := 'no_user';
64 result.success := FALSE;
70 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
72 -- Fail if we couldn't find a copy
73 IF item_object.id IS NULL THEN
74 result.fail_part := 'no_item';
75 result.success := FALSE;
81 SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(pickup_ou, request_ou, match_item, match_user, match_requestor);
82 result.matchpoint := matchpoint_id;
84 SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
86 -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
87 IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
88 result.fail_part := 'circ.holds.target_skip_me';
89 result.success := FALSE;
95 -- Fail if user is barred
96 IF user_object.barred IS TRUE THEN
97 result.fail_part := 'actor.usr.barred';
98 result.success := FALSE;
104 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
105 SELECT INTO item_status_object * FROM config.copy_status WHERE id = item_object.status;
106 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
108 -- Fail if we couldn't find any matchpoint (requires a default)
109 IF matchpoint_id IS NULL THEN
110 result.fail_part := 'no_matchpoint';
111 result.success := FALSE;
117 SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
119 IF hold_test.holdable IS FALSE THEN
120 result.fail_part := 'config.hold_matrix_test.holdable';
121 result.success := FALSE;
126 IF item_object.holdable IS FALSE THEN
127 result.fail_part := 'item.holdable';
128 result.success := FALSE;
133 IF item_status_object.holdable IS FALSE THEN
134 result.fail_part := 'status.holdable';
135 result.success := FALSE;
140 IF item_location_object.holdable IS FALSE THEN
141 result.fail_part := 'location.holdable';
142 result.success := FALSE;
147 IF hold_test.transit_range IS NOT NULL THEN
148 SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
149 IF hold_test.distance_is_from_owner THEN
150 SELECT INTO transit_source ou.* FROM actor.org_unit ou JOIN asset.call_number cn ON (cn.owning_lib = ou.id) WHERE cn.id = item_object.call_number;
152 SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
155 PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
158 result.fail_part := 'transit_range';
159 result.success := FALSE;
165 FOR standing_penalty IN
166 SELECT DISTINCT csp.*
167 FROM actor.usr_standing_penalty usp
168 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
169 WHERE usr = match_user
170 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
171 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
172 AND csp.block_list LIKE '%' || hold_penalty || '%' LOOP
174 result.fail_part := standing_penalty.name;
175 result.success := FALSE;
180 IF hold_test.stop_blocked_user IS TRUE THEN
181 FOR standing_penalty IN
182 SELECT DISTINCT csp.*
183 FROM actor.usr_standing_penalty usp
184 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
185 WHERE usr = match_user
186 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
187 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
188 AND csp.block_list LIKE '%CIRC%' LOOP
190 result.fail_part := standing_penalty.name;
191 result.success := FALSE;
197 IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
198 SELECT INTO hold_count COUNT(*)
199 FROM action.hold_request
200 WHERE usr = match_user
201 AND fulfillment_time IS NULL
202 AND cancel_time IS NULL
203 AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
205 IF hold_count >= hold_test.max_holds THEN
206 result.fail_part := 'config.hold_matrix_test.max_holds';
207 result.success := FALSE;
213 IF item_object.age_protect IS NOT NULL THEN
214 SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
215 IF hold_test.distance_is_from_owner THEN
216 SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_cn_object.owning_lib);
218 SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
220 IF use_active_date = 'true' THEN
221 age_protect_date := COALESCE(item_object.active_date, NOW());
223 age_protect_date := item_object.create_date;
225 IF age_protect_date + age_protect_object.age > NOW() THEN
226 IF hold_test.distance_is_from_owner THEN
227 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
228 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = pickup_ou;
230 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
233 IF hold_transit_prox > age_protect_object.prox THEN
234 result.fail_part := 'config.rule_age_hold_protect.prox';
235 result.success := FALSE;
248 $func$ LANGUAGE plpgsql;