1 -- Evergreen DB patch 0579.schema.fix_checkout_overrides.sql
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0579', :eg_version);
10 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$
12 user_object actor.usr%ROWTYPE;
13 standing_penalty config.standing_penalty%ROWTYPE;
14 item_object asset.copy%ROWTYPE;
15 item_status_object config.copy_status%ROWTYPE;
16 item_location_object asset.copy_location%ROWTYPE;
17 result action.circ_matrix_test_result;
18 circ_test action.found_circ_matrix_matchpoint;
19 circ_matchpoint config.circ_matrix_matchpoint%ROWTYPE;
20 out_by_circ_mod config.circ_matrix_circ_mod_test%ROWTYPE;
21 circ_mod_map config.circ_matrix_circ_mod_test_map%ROWTYPE;
22 hold_ratio action.hold_stats%ROWTYPE;
25 context_org_list INT[];
28 -- Assume success unless we hit a failure condition
29 result.success := TRUE;
31 -- Need user info to look up matchpoints
32 SELECT INTO user_object * FROM actor.usr WHERE id = match_user AND NOT deleted;
34 -- (Insta)Fail if we couldn't find the user
35 IF user_object.id IS NULL THEN
36 result.fail_part := 'no_user';
37 result.success := FALSE;
43 -- Need item info to look up matchpoints
44 SELECT INTO item_object * FROM asset.copy WHERE id = match_item AND NOT deleted;
46 -- (Insta)Fail if we couldn't find the item
47 IF item_object.id IS NULL THEN
48 result.fail_part := 'no_item';
49 result.success := FALSE;
55 SELECT INTO circ_test * FROM action.find_circ_matrix_matchpoint(circ_ou, item_object, user_object, renewal);
57 circ_matchpoint := circ_test.matchpoint;
58 result.matchpoint := circ_matchpoint.id;
59 result.circulate := circ_matchpoint.circulate;
60 result.duration_rule := circ_matchpoint.duration_rule;
61 result.recurring_fine_rule := circ_matchpoint.recurring_fine_rule;
62 result.max_fine_rule := circ_matchpoint.max_fine_rule;
63 result.hard_due_date := circ_matchpoint.hard_due_date;
64 result.renewals := circ_matchpoint.renewals;
65 result.grace_period := circ_matchpoint.grace_period;
66 result.buildrows := circ_test.buildrows;
68 -- (Insta)Fail if we couldn't find a matchpoint
69 IF circ_test.success = false THEN
70 result.fail_part := 'no_matchpoint';
71 result.success := FALSE;
77 -- All failures before this point are non-recoverable
78 -- Below this point are possibly overridable failures
80 -- Fail if the user is barred
81 IF user_object.barred IS TRUE THEN
82 result.fail_part := 'actor.usr.barred';
83 result.success := FALSE;
88 -- Fail if the item can't circulate
89 IF item_object.circulate IS FALSE THEN
90 result.fail_part := 'asset.copy.circulate';
91 result.success := FALSE;
96 -- Fail if the item isn't in a circulateable status on a non-renewal
97 IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN
98 result.fail_part := 'asset.copy.status';
99 result.success := FALSE;
102 -- Alternately, fail if the item isn't checked out on a renewal
103 ELSIF renewal AND item_object.status <> 1 THEN
104 result.fail_part := 'asset.copy.status';
105 result.success := FALSE;
110 -- Fail if the item can't circulate because of the shelving location
111 SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
112 IF item_location_object.circulate IS FALSE THEN
113 result.fail_part := 'asset.copy_location.circulate';
114 result.success := FALSE;
119 -- Apparently....use the circ matchpoint org unit to determine what org units are valid.
120 SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( circ_matchpoint.org_unit );
123 penalty_type = '%RENEW%';
125 penalty_type = '%CIRC%';
128 FOR standing_penalty IN
129 SELECT DISTINCT csp.*
130 FROM actor.usr_standing_penalty usp
131 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
132 WHERE usr = match_user
133 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
134 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
135 AND csp.block_list LIKE penalty_type LOOP
137 result.fail_part := standing_penalty.name;
138 result.success := FALSE;
143 -- Fail if the test is set to hard non-circulating
144 IF circ_matchpoint.circulate IS FALSE THEN
145 result.fail_part := 'config.circ_matrix_test.circulate';
146 result.success := FALSE;
151 -- Fail if the total copy-hold ratio is too low
152 IF circ_matchpoint.total_copy_hold_ratio IS NOT NULL THEN
153 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
154 IF hold_ratio.total_copy_ratio IS NOT NULL AND hold_ratio.total_copy_ratio < circ_matchpoint.total_copy_hold_ratio THEN
155 result.fail_part := 'config.circ_matrix_test.total_copy_hold_ratio';
156 result.success := FALSE;
162 -- Fail if the available copy-hold ratio is too low
163 IF circ_matchpoint.available_copy_hold_ratio IS NOT NULL THEN
164 IF hold_ratio.hold_count IS NULL THEN
165 SELECT INTO hold_ratio * FROM action.copy_related_hold_stats(match_item);
167 IF hold_ratio.available_copy_ratio IS NOT NULL AND hold_ratio.available_copy_ratio < circ_matchpoint.available_copy_hold_ratio THEN
168 result.fail_part := 'config.circ_matrix_test.available_copy_hold_ratio';
169 result.success := FALSE;
175 -- Fail if the user has too many items with specific circ_modifiers checked out
176 FOR out_by_circ_mod IN SELECT * FROM config.circ_matrix_circ_mod_test WHERE matchpoint = circ_matchpoint.id LOOP
177 SELECT INTO items_out COUNT(*)
178 FROM action.circulation circ
179 JOIN asset.copy cp ON (cp.id = circ.target_copy)
180 WHERE circ.usr = match_user
181 AND circ.circ_lib IN ( SELECT * FROM unnest(context_org_list) )
182 AND circ.checkin_time IS NULL
183 AND (circ.stop_fines IN ('MAXFINES','LONGOVERDUE') OR circ.stop_fines IS NULL)
184 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);
185 IF items_out >= out_by_circ_mod.items_out THEN
186 result.fail_part := 'config.circ_matrix_circ_mod_test';
187 result.success := FALSE;
193 -- If we passed everything, return the successful matchpoint
200 $func$ LANGUAGE plpgsql;