1 -- Evergreen DB patch 0668.schema.fix_indb_hold_permit.sql
3 -- FIXME: insert description of change, if needed
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0668', :eg_version);
11 -- FIXME: add/check SQL statements to perform the upgrade
12 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$
15 user_object actor.usr%ROWTYPE;
16 age_protect_object config.rule_age_hold_protect%ROWTYPE;
17 standing_penalty config.standing_penalty%ROWTYPE;
18 transit_range_ou_type actor.org_unit_type%ROWTYPE;
19 transit_source actor.org_unit%ROWTYPE;
20 item_object asset.copy%ROWTYPE;
21 item_cn_object asset.call_number%ROWTYPE;
22 item_status_object config.copy_status%ROWTYPE;
23 item_location_object asset.copy_location%ROWTYPE;
24 ou_skip actor.org_unit_setting%ROWTYPE;
25 result action.matrix_test_result;
26 hold_test config.hold_matrix_matchpoint%ROWTYPE;
28 age_protect_date TIMESTAMP WITH TIME ZONE;
30 hold_transit_prox INT;
31 frozen_hold_count INT;
32 context_org_list INT[];
35 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
36 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( pickup_ou );
38 result.success := TRUE;
40 -- Fail if we couldn't find a user
41 IF user_object.id IS NULL THEN
42 result.fail_part := 'no_user';
43 result.success := FALSE;
49 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
51 -- Fail if we couldn't find a copy
52 IF item_object.id IS NULL THEN
53 result.fail_part := 'no_item';
54 result.success := FALSE;
60 SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(pickup_ou, request_ou, match_item, match_user, match_requestor);
61 result.matchpoint := matchpoint_id;
63 SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
65 -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
66 IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
67 result.fail_part := 'circ.holds.target_skip_me';
68 result.success := FALSE;
74 -- Fail if user is barred
75 IF user_object.barred IS TRUE THEN
76 result.fail_part := 'actor.usr.barred';
77 result.success := FALSE;
83 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
84 SELECT INTO item_status_object * FROM config.copy_status WHERE id = item_object.status;
85 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
87 -- Fail if we couldn't find any matchpoint (requires a default)
88 IF matchpoint_id IS NULL THEN
89 result.fail_part := 'no_matchpoint';
90 result.success := FALSE;
96 SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
98 IF hold_test.holdable IS FALSE THEN
99 result.fail_part := 'config.hold_matrix_test.holdable';
100 result.success := FALSE;
105 IF item_object.holdable IS FALSE THEN
106 result.fail_part := 'item.holdable';
107 result.success := FALSE;
112 IF item_status_object.holdable IS FALSE THEN
113 result.fail_part := 'status.holdable';
114 result.success := FALSE;
119 IF item_location_object.holdable IS FALSE THEN
120 result.fail_part := 'location.holdable';
121 result.success := FALSE;
126 IF hold_test.transit_range IS NOT NULL THEN
127 SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
128 IF hold_test.distance_is_from_owner THEN
129 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;
131 SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
134 PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
137 result.fail_part := 'transit_range';
138 result.success := FALSE;
144 FOR standing_penalty IN
145 SELECT DISTINCT csp.*
146 FROM actor.usr_standing_penalty usp
147 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
148 WHERE usr = match_user
149 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
150 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
151 AND csp.block_list LIKE '%HOLD%' LOOP
153 result.fail_part := standing_penalty.name;
154 result.success := FALSE;
159 IF hold_test.stop_blocked_user IS TRUE THEN
160 FOR standing_penalty IN
161 SELECT DISTINCT csp.*
162 FROM actor.usr_standing_penalty usp
163 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
164 WHERE usr = match_user
165 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
166 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
167 AND csp.block_list LIKE '%CIRC%' LOOP
169 result.fail_part := standing_penalty.name;
170 result.success := FALSE;
176 IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
177 SELECT INTO hold_count COUNT(*)
178 FROM action.hold_request
179 WHERE usr = match_user
180 AND fulfillment_time IS NULL
181 AND cancel_time IS NULL
182 AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
184 IF hold_count >= hold_test.max_holds THEN
185 result.fail_part := 'config.hold_matrix_test.max_holds';
186 result.success := FALSE;
192 IF item_object.age_protect IS NOT NULL THEN
193 SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
194 IF hold_test.distance_is_from_owner THEN
195 SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_cn_object.owning_lib);
197 SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
199 IF use_active_date = 'true' THEN
200 age_protect_date := COALESCE(item_object.active_date, NOW());
202 age_protect_date := item_object.create_date;
204 IF age_protect_date + age_protect_object.age > NOW() THEN
205 IF hold_test.distance_is_from_owner THEN
206 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
207 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = pickup_ou;
209 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
212 IF hold_transit_prox > age_protect_object.prox THEN
213 result.fail_part := 'config.rule_age_hold_protect.prox';
214 result.success := FALSE;
227 $func$ LANGUAGE plpgsql;