]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0553.unnest_action_hold_request_permit_test.sql
LP1889113 Staff catalog record holds sticky org select
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0553.unnest_action_hold_request_permit_test.sql
1 -- Evergreen DB patch 0553.unnest_action_hold_request_permit_test.sql
2 --
3 -- Replace usage of custom explode_array() function with native unnest()
4 --
5 BEGIN;
6
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0553', :eg_version);
9
10 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$
11 DECLARE
12     matchpoint_id        INT;
13     user_object        actor.usr%ROWTYPE;
14     age_protect_object    config.rule_age_hold_protect%ROWTYPE;
15     standing_penalty    config.standing_penalty%ROWTYPE;
16     transit_range_ou_type    actor.org_unit_type%ROWTYPE;
17     transit_source        actor.org_unit%ROWTYPE;
18     item_object        asset.copy%ROWTYPE;
19     item_cn_object     asset.call_number%ROWTYPE;
20     ou_skip              actor.org_unit_setting%ROWTYPE;
21     result            action.matrix_test_result;
22     hold_test        config.hold_matrix_matchpoint%ROWTYPE;
23     hold_count        INT;
24     hold_transit_prox    INT;
25     frozen_hold_count    INT;
26     context_org_list    INT[];
27     done            BOOL := FALSE;
28 BEGIN
29     SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
30     SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( pickup_ou );
31
32     result.success := TRUE;
33
34     -- Fail if we couldn't find a user
35     IF user_object.id IS NULL THEN
36         result.fail_part := 'no_user';
37         result.success := FALSE;
38         done := TRUE;
39         RETURN NEXT result;
40         RETURN;
41     END IF;
42
43     SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
44
45     -- Fail if we couldn't find a copy
46     IF item_object.id IS NULL THEN
47         result.fail_part := 'no_item';
48         result.success := FALSE;
49         done := TRUE;
50         RETURN NEXT result;
51         RETURN;
52     END IF;
53
54     SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(pickup_ou, request_ou, match_item, match_user, match_requestor);
55     result.matchpoint := matchpoint_id;
56
57     SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
58
59     -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
60     IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
61         result.fail_part := 'circ.holds.target_skip_me';
62         result.success := FALSE;
63         done := TRUE;
64         RETURN NEXT result;
65         RETURN;
66     END IF;
67
68     -- Fail if user is barred
69     IF user_object.barred IS TRUE THEN
70         result.fail_part := 'actor.usr.barred';
71         result.success := FALSE;
72         done := TRUE;
73         RETURN NEXT result;
74         RETURN;
75     END IF;
76
77     -- Fail if we couldn't find any matchpoint (requires a default)
78     IF matchpoint_id IS NULL THEN
79         result.fail_part := 'no_matchpoint';
80         result.success := FALSE;
81         done := TRUE;
82         RETURN NEXT result;
83         RETURN;
84     END IF;
85
86     SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
87
88     IF hold_test.holdable IS FALSE THEN
89         result.fail_part := 'config.hold_matrix_test.holdable';
90         result.success := FALSE;
91         done := TRUE;
92         RETURN NEXT result;
93     END IF;
94
95     IF hold_test.transit_range IS NOT NULL THEN
96         SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
97         IF hold_test.distance_is_from_owner THEN
98             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;
99         ELSE
100             SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
101         END IF;
102
103         PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
104
105         IF NOT FOUND THEN
106             result.fail_part := 'transit_range';
107             result.success := FALSE;
108             done := TRUE;
109             RETURN NEXT result;
110         END IF;
111     END IF;
112  
113     FOR standing_penalty IN
114         SELECT  DISTINCT csp.*
115           FROM  actor.usr_standing_penalty usp
116                 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
117           WHERE usr = match_user
118                 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
119                 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
120                 AND csp.block_list LIKE '%HOLD%' LOOP
121
122         result.fail_part := standing_penalty.name;
123         result.success := FALSE;
124         done := TRUE;
125         RETURN NEXT result;
126     END LOOP;
127
128     IF hold_test.stop_blocked_user IS TRUE THEN
129         FOR standing_penalty IN
130             SELECT  DISTINCT csp.*
131               FROM  actor.usr_standing_penalty usp
132                     JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
133               WHERE usr = match_user
134                     AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
135                     AND (usp.stop_date IS NULL or usp.stop_date > NOW())
136                     AND csp.block_list LIKE '%CIRC%' LOOP
137     
138             result.fail_part := standing_penalty.name;
139             result.success := FALSE;
140             done := TRUE;
141             RETURN NEXT result;
142         END LOOP;
143     END IF;
144
145     IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
146         SELECT    INTO hold_count COUNT(*)
147           FROM    action.hold_request
148           WHERE    usr = match_user
149             AND fulfillment_time IS NULL
150             AND cancel_time IS NULL
151             AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
152
153         IF hold_count >= hold_test.max_holds THEN
154             result.fail_part := 'config.hold_matrix_test.max_holds';
155             result.success := FALSE;
156             done := TRUE;
157             RETURN NEXT result;
158         END IF;
159     END IF;
160
161     IF item_object.age_protect IS NOT NULL THEN
162         SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
163
164         IF item_object.create_date + age_protect_object.age > NOW() THEN
165             IF hold_test.distance_is_from_owner THEN
166                 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
167                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = pickup_ou;
168             ELSE
169                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
170             END IF;
171
172             IF hold_transit_prox > age_protect_object.prox THEN
173                 result.fail_part := 'config.rule_age_hold_protect.prox';
174                 result.success := FALSE;
175                 done := TRUE;
176                 RETURN NEXT result;
177             END IF;
178         END IF;
179     END IF;
180
181     IF NOT done THEN
182         RETURN NEXT result;
183     END IF;
184
185     RETURN;
186 END;
187 $func$ LANGUAGE plpgsql;
188
189 COMMIT;