]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0721.data.hold_cap_fill_penalty_blocks.sql
LP#1643709: Stamping upgrade scripts
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0721.data.hold_cap_fill_penalty_blocks.sql
1
2 BEGIN;
3
4 SELECT evergreen.upgrade_deps_block_check('0721', :eg_version);
5
6 UPDATE config.standing_penalty 
7     SET block_list = REPLACE(block_list, 'HOLD', 'HOLD|CAPTURE') 
8     WHERE   
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%'; 
13
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') 
17     WHERE   
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%'; 
22
23
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$
26 DECLARE
27     matchpoint_id        INT;
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;
40     use_active_date   TEXT;
41     age_protect_date  TIMESTAMP WITH TIME ZONE;
42     hold_count        INT;
43     hold_transit_prox    INT;
44     frozen_hold_count    INT;
45     context_org_list    INT[];
46     done            BOOL := FALSE;
47     hold_penalty TEXT;
48 BEGIN
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 );
51
52     result.success := TRUE;
53
54     -- The HOLD penalty block only applies to new holds.
55     -- The CAPTURE penalty block applies to existing holds.
56     hold_penalty := 'HOLD';
57     IF retargetting THEN
58         hold_penalty := 'CAPTURE';
59     END IF;
60
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;
65         done := TRUE;
66         RETURN NEXT result;
67         RETURN;
68     END IF;
69
70     SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
71
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;
76         done := TRUE;
77         RETURN NEXT result;
78         RETURN;
79     END IF;
80
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;
83
84     SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
85
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;
90         done := TRUE;
91         RETURN NEXT result;
92         RETURN;
93     END IF;
94
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;
99         done := TRUE;
100         RETURN NEXT result;
101         RETURN;
102     END IF;
103
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;
107
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;
112         done := TRUE;
113         RETURN NEXT result;
114         RETURN;
115     END IF;
116
117     SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
118
119     IF hold_test.holdable IS FALSE THEN
120         result.fail_part := 'config.hold_matrix_test.holdable';
121         result.success := FALSE;
122         done := TRUE;
123         RETURN NEXT result;
124     END IF;
125
126     IF item_object.holdable IS FALSE THEN
127         result.fail_part := 'item.holdable';
128         result.success := FALSE;
129         done := TRUE;
130         RETURN NEXT result;
131     END IF;
132
133     IF item_status_object.holdable IS FALSE THEN
134         result.fail_part := 'status.holdable';
135         result.success := FALSE;
136         done := TRUE;
137         RETURN NEXT result;
138     END IF;
139
140     IF item_location_object.holdable IS FALSE THEN
141         result.fail_part := 'location.holdable';
142         result.success := FALSE;
143         done := TRUE;
144         RETURN NEXT result;
145     END IF;
146
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;
151         ELSE
152             SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
153         END IF;
154
155         PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
156
157         IF NOT FOUND THEN
158             result.fail_part := 'transit_range';
159             result.success := FALSE;
160             done := TRUE;
161             RETURN NEXT result;
162         END IF;
163     END IF;
164  
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
173
174         result.fail_part := standing_penalty.name;
175         result.success := FALSE;
176         done := TRUE;
177         RETURN NEXT result;
178     END LOOP;
179
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
189     
190             result.fail_part := standing_penalty.name;
191             result.success := FALSE;
192             done := TRUE;
193             RETURN NEXT result;
194         END LOOP;
195     END IF;
196
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;
204
205         IF hold_count >= hold_test.max_holds THEN
206             result.fail_part := 'config.hold_matrix_test.max_holds';
207             result.success := FALSE;
208             done := TRUE;
209             RETURN NEXT result;
210         END IF;
211     END IF;
212
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);
217         ELSE
218             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
219         END IF;
220         IF use_active_date = 'true' THEN
221             age_protect_date := COALESCE(item_object.active_date, NOW());
222         ELSE
223             age_protect_date := item_object.create_date;
224         END IF;
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;
229             ELSE
230                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
231             END IF;
232
233             IF hold_transit_prox > age_protect_object.prox THEN
234                 result.fail_part := 'config.rule_age_hold_protect.prox';
235                 result.success := FALSE;
236                 done := TRUE;
237                 RETURN NEXT result;
238             END IF;
239         END IF;
240     END IF;
241
242     IF NOT done THEN
243         RETURN NEXT result;
244     END IF;
245
246     RETURN;
247 END;
248 $func$ LANGUAGE plpgsql;
249
250
251 COMMIT;