1 -- Evergreen DB patch XXXX.schema.unnest-hold-permit-upgrade-script-repair.sql
3 -- This patch makes no changes to the baseline schema and is
4 -- only meant to repair a previous upgrade script.
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0651', :eg_version);
11 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$
14 user_object actor.usr%ROWTYPE;
15 age_protect_object config.rule_age_hold_protect%ROWTYPE;
16 standing_penalty config.standing_penalty%ROWTYPE;
17 transit_range_ou_type actor.org_unit_type%ROWTYPE;
18 transit_source actor.org_unit%ROWTYPE;
19 item_object asset.copy%ROWTYPE;
20 item_cn_object asset.call_number%ROWTYPE;
21 ou_skip actor.org_unit_setting%ROWTYPE;
22 result action.matrix_test_result;
23 hold_test config.hold_matrix_matchpoint%ROWTYPE;
25 age_protect_date TIMESTAMP WITH TIME ZONE;
27 hold_transit_prox INT;
28 frozen_hold_count INT;
29 context_org_list INT[];
32 SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
33 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( pickup_ou );
35 result.success := TRUE;
37 -- Fail if we couldn't find a user
38 IF user_object.id IS NULL THEN
39 result.fail_part := 'no_user';
40 result.success := FALSE;
46 SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
48 -- Fail if we couldn't find a copy
49 IF item_object.id IS NULL THEN
50 result.fail_part := 'no_item';
51 result.success := FALSE;
57 SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(pickup_ou, request_ou, match_item, match_user, match_requestor);
58 result.matchpoint := matchpoint_id;
60 SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
62 -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
63 IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
64 result.fail_part := 'circ.holds.target_skip_me';
65 result.success := FALSE;
71 -- Fail if user is barred
72 IF user_object.barred IS TRUE THEN
73 result.fail_part := 'actor.usr.barred';
74 result.success := FALSE;
80 -- Fail if we couldn't find any matchpoint (requires a default)
81 IF matchpoint_id IS NULL THEN
82 result.fail_part := 'no_matchpoint';
83 result.success := FALSE;
89 SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
91 IF hold_test.holdable IS FALSE THEN
92 result.fail_part := 'config.hold_matrix_test.holdable';
93 result.success := FALSE;
98 IF hold_test.transit_range IS NOT NULL THEN
99 SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
100 IF hold_test.distance_is_from_owner THEN
101 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;
103 SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
106 PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
109 result.fail_part := 'transit_range';
110 result.success := FALSE;
116 FOR standing_penalty IN
117 SELECT DISTINCT csp.*
118 FROM actor.usr_standing_penalty usp
119 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
120 WHERE usr = match_user
121 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
122 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
123 AND csp.block_list LIKE '%HOLD%' LOOP
125 result.fail_part := standing_penalty.name;
126 result.success := FALSE;
131 IF hold_test.stop_blocked_user IS TRUE THEN
132 FOR standing_penalty IN
133 SELECT DISTINCT csp.*
134 FROM actor.usr_standing_penalty usp
135 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
136 WHERE usr = match_user
137 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
138 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
139 AND csp.block_list LIKE '%CIRC%' LOOP
141 result.fail_part := standing_penalty.name;
142 result.success := FALSE;
148 IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
149 SELECT INTO hold_count COUNT(*)
150 FROM action.hold_request
151 WHERE usr = match_user
152 AND fulfillment_time IS NULL
153 AND cancel_time IS NULL
154 AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
156 IF hold_count >= hold_test.max_holds THEN
157 result.fail_part := 'config.hold_matrix_test.max_holds';
158 result.success := FALSE;
164 IF item_object.age_protect IS NOT NULL THEN
165 SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
166 IF hold_test.distance_is_from_owner THEN
167 SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_cn_object.owning_lib);
169 SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
171 IF use_active_date = 'true' THEN
172 age_protect_date := COALESCE(item_object.active_date, NOW());
174 age_protect_date := item_object.create_date;
176 IF age_protect_date + age_protect_object.age > NOW() THEN
177 IF hold_test.distance_is_from_owner THEN
178 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
179 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = pickup_ou;
181 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
184 IF hold_transit_prox > age_protect_object.prox THEN
185 result.fail_part := 'config.rule_age_hold_protect.prox';
186 result.success := FALSE;
199 $func$ LANGUAGE plpgsql;