]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1221.function.calc-prox-and-hold-permit.sql
LP1915464 follow-up: use spaces, not tabs; remove extra comma
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1221.function.calc-prox-and-hold-permit.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1221', :eg_version);
4
5 CREATE OR REPLACE FUNCTION action.copy_calculated_proximity(
6     pickup  INT,
7     request INT,
8     vacp_cl  INT,
9     vacp_cm  TEXT,
10     vacn_ol  INT,
11     vacl_ol  INT
12 ) RETURNS NUMERIC AS $f$
13 DECLARE
14     baseline_prox   NUMERIC;
15     aoupa           actor.org_unit_proximity_adjustment%ROWTYPE;
16 BEGIN
17
18     -- First, gather the baseline proximity of "here" to pickup lib
19     SELECT prox INTO baseline_prox FROM actor.org_unit_proximity WHERE from_org = vacp_cl AND to_org = pickup;
20
21     -- Find any absolute adjustments, and set the baseline prox to that
22     SELECT  adj.* INTO aoupa
23       FROM  actor.org_unit_proximity_adjustment adj
24             LEFT JOIN actor.org_unit_ancestors_distance(vacp_cl) acp_cl ON (acp_cl.id = adj.item_circ_lib)
25             LEFT JOIN actor.org_unit_ancestors_distance(vacn_ol) acn_ol ON (acn_ol.id = adj.item_owning_lib)
26             LEFT JOIN actor.org_unit_ancestors_distance(vacl_ol) acl_ol ON (acl_ol.id = adj.copy_location)
27             LEFT JOIN actor.org_unit_ancestors_distance(pickup) ahr_pl ON (ahr_pl.id = adj.hold_pickup_lib)
28             LEFT JOIN actor.org_unit_ancestors_distance(request) ahr_rl ON (ahr_rl.id = adj.hold_request_lib)
29       WHERE (adj.circ_mod IS NULL OR adj.circ_mod = vacp_cm) AND
30             (adj.item_circ_lib IS NULL OR adj.item_circ_lib = acp_cl.id) AND
31             (adj.item_owning_lib IS NULL OR adj.item_owning_lib = acn_ol.id) AND
32             (adj.copy_location IS NULL OR adj.copy_location = acl_ol.id) AND
33             (adj.hold_pickup_lib IS NULL OR adj.hold_pickup_lib = ahr_pl.id) AND
34             (adj.hold_request_lib IS NULL OR adj.hold_request_lib = ahr_rl.id) AND
35             absolute_adjustment AND
36             COALESCE(acp_cl.id, acn_ol.id, acl_ol.id, ahr_pl.id, ahr_rl.id) IS NOT NULL
37       ORDER BY
38             COALESCE(acp_cl.distance,999)
39                 + COALESCE(acn_ol.distance,999)
40                 + COALESCE(acl_ol.distance,999)
41                 + COALESCE(ahr_pl.distance,999)
42                 + COALESCE(ahr_rl.distance,999),
43             adj.pos
44       LIMIT 1;
45
46     IF FOUND THEN
47         baseline_prox := aoupa.prox_adjustment;
48     END IF;
49
50     -- Now find any relative adjustments, and change the baseline prox based on them
51     FOR aoupa IN
52         SELECT  adj.*
53           FROM  actor.org_unit_proximity_adjustment adj
54                 LEFT JOIN actor.org_unit_ancestors_distance(vacp_cl) acp_cl ON (acp_cl.id = adj.item_circ_lib)
55                 LEFT JOIN actor.org_unit_ancestors_distance(vacn_ol) acn_ol ON (acn_ol.id = adj.item_owning_lib)
56                 LEFT JOIN actor.org_unit_ancestors_distance(vacl_ol) acl_ol ON (acn_ol.id = adj.copy_location)
57                 LEFT JOIN actor.org_unit_ancestors_distance(pickup) ahr_pl ON (ahr_pl.id = adj.hold_pickup_lib)
58                 LEFT JOIN actor.org_unit_ancestors_distance(request) ahr_rl ON (ahr_rl.id = adj.hold_request_lib)
59           WHERE (adj.circ_mod IS NULL OR adj.circ_mod = vacp_cm) AND
60                 (adj.item_circ_lib IS NULL OR adj.item_circ_lib = acp_cl.id) AND
61                 (adj.item_owning_lib IS NULL OR adj.item_owning_lib = acn_ol.id) AND
62                 (adj.copy_location IS NULL OR adj.copy_location = acl_ol.id) AND
63                 (adj.hold_pickup_lib IS NULL OR adj.hold_pickup_lib = ahr_pl.id) AND
64                 (adj.hold_request_lib IS NULL OR adj.hold_request_lib = ahr_rl.id) AND
65                 NOT absolute_adjustment AND
66                 COALESCE(acp_cl.id, acn_ol.id, acl_ol.id, ahr_pl.id, ahr_rl.id) IS NOT NULL
67     LOOP
68         baseline_prox := baseline_prox + aoupa.prox_adjustment;
69     END LOOP;
70
71     RETURN baseline_prox;
72 END;
73 $f$ LANGUAGE PLPGSQL;
74
75 CREATE OR REPLACE FUNCTION action.hold_copy_calculated_proximity(
76     ahr_id INT,
77     acp_id BIGINT,
78     copy_context_ou INT DEFAULT NULL
79     -- TODO maybe? hold_context_ou INT DEFAULT NULL.  This would optionally
80     -- support an "ahprox" measurement: adjust prox between copy circ lib and
81     -- hold request lib, but I'm unsure whether to use this theoretical
82     -- argument only in the baseline calculation or later in the other
83     -- queries in this function.
84 ) RETURNS NUMERIC AS $f$
85 DECLARE
86     ahr  action.hold_request%ROWTYPE;
87     acp  asset.copy%ROWTYPE;
88     acn  asset.call_number%ROWTYPE;
89     acl  asset.copy_location%ROWTYPE;
90
91     prox NUMERIC;
92 BEGIN
93
94     SELECT * INTO ahr FROM action.hold_request WHERE id = ahr_id;
95     SELECT * INTO acp FROM asset.copy WHERE id = acp_id;
96     SELECT * INTO acn FROM asset.call_number WHERE id = acp.call_number;
97     SELECT * INTO acl FROM asset.copy_location WHERE id = acp.location;
98
99     IF copy_context_ou IS NULL THEN
100         copy_context_ou := acp.circ_lib;
101     END IF;
102
103     SELECT action.copy_calculated_proximity(
104         ahr.pickup_lib,
105         ahr.request_lib,
106         copy_context_ou,
107         acp.circ_modifier,
108         acn.owning_lib,
109         acl.owning_lib
110     ) INTO prox;
111
112     RETURN prox;
113 END;
114 $f$ LANGUAGE PLPGSQL;
115
116 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$
117 DECLARE
118     matchpoint_id        INT;
119     user_object        actor.usr%ROWTYPE;
120     age_protect_object    config.rule_age_hold_protect%ROWTYPE;
121     standing_penalty    config.standing_penalty%ROWTYPE;
122     transit_range_ou_type    actor.org_unit_type%ROWTYPE;
123     transit_source        actor.org_unit%ROWTYPE;
124     item_object        asset.copy%ROWTYPE;
125     item_cn_object     asset.call_number%ROWTYPE;
126     item_status_object  config.copy_status%ROWTYPE;
127     item_location_object    asset.copy_location%ROWTYPE;
128     ou_skip              actor.org_unit_setting%ROWTYPE;
129     calc_age_prox        actor.org_unit_setting%ROWTYPE;
130     result            action.matrix_test_result;
131     hold_test        config.hold_matrix_matchpoint%ROWTYPE;
132     use_active_date   TEXT;
133     prox_ou           INT;
134     age_protect_date  TIMESTAMP WITH TIME ZONE;
135     hold_count        INT;
136     hold_transit_prox    NUMERIC;
137     frozen_hold_count    INT;
138     context_org_list    INT[];
139     done            BOOL := FALSE;
140     hold_penalty TEXT;
141     v_pickup_ou ALIAS FOR pickup_ou;
142     v_request_ou ALIAS FOR request_ou;
143     item_prox INT;
144     pickup_prox INT;
145 BEGIN
146     SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
147     SELECT INTO context_org_list ARRAY_AGG(id) FROM actor.org_unit_full_path( v_pickup_ou );
148
149     result.success := TRUE;
150
151     -- The HOLD penalty block only applies to new holds.
152     -- The CAPTURE penalty block applies to existing holds.
153     hold_penalty := 'HOLD';
154     IF retargetting THEN
155         hold_penalty := 'CAPTURE';
156     END IF;
157
158     -- Fail if we couldn't find a user
159     IF user_object.id IS NULL THEN
160         result.fail_part := 'no_user';
161         result.success := FALSE;
162         done := TRUE;
163         RETURN NEXT result;
164         RETURN;
165     END IF;
166
167     SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
168
169     -- Fail if we couldn't find a copy
170     IF item_object.id IS NULL THEN
171         result.fail_part := 'no_item';
172         result.success := FALSE;
173         done := TRUE;
174         RETURN NEXT result;
175         RETURN;
176     END IF;
177
178     SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(v_pickup_ou, v_request_ou, match_item, match_user, match_requestor);
179     result.matchpoint := matchpoint_id;
180
181     SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
182
183     -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
184     IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
185         result.fail_part := 'circ.holds.target_skip_me';
186         result.success := FALSE;
187         done := TRUE;
188         RETURN NEXT result;
189         RETURN;
190     END IF;
191
192     -- Fail if user is barred
193     IF user_object.barred IS TRUE THEN
194         result.fail_part := 'actor.usr.barred';
195         result.success := FALSE;
196         done := TRUE;
197         RETURN NEXT result;
198         RETURN;
199     END IF;
200
201     SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
202     SELECT INTO item_status_object * FROM config.copy_status WHERE id = item_object.status;
203     SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
204
205     -- Fail if we couldn't find any matchpoint (requires a default)
206     IF matchpoint_id IS NULL THEN
207         result.fail_part := 'no_matchpoint';
208         result.success := FALSE;
209         done := TRUE;
210         RETURN NEXT result;
211         RETURN;
212     END IF;
213
214     SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
215
216     IF hold_test.holdable IS FALSE THEN
217         result.fail_part := 'config.hold_matrix_test.holdable';
218         result.success := FALSE;
219         done := TRUE;
220         RETURN NEXT result;
221     END IF;
222
223     IF item_object.holdable IS FALSE THEN
224         result.fail_part := 'item.holdable';
225         result.success := FALSE;
226         done := TRUE;
227         RETURN NEXT result;
228     END IF;
229
230     IF item_status_object.holdable IS FALSE THEN
231         result.fail_part := 'status.holdable';
232         result.success := FALSE;
233         done := TRUE;
234         RETURN NEXT result;
235     END IF;
236
237     IF item_location_object.holdable IS FALSE THEN
238         result.fail_part := 'location.holdable';
239         result.success := FALSE;
240         done := TRUE;
241         RETURN NEXT result;
242     END IF;
243
244     IF hold_test.transit_range IS NOT NULL THEN
245         SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
246         IF hold_test.distance_is_from_owner THEN
247             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;
248         ELSE
249             SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
250         END IF;
251
252         PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = v_pickup_ou;
253
254         IF NOT FOUND THEN
255             result.fail_part := 'transit_range';
256             result.success := FALSE;
257             done := TRUE;
258             RETURN NEXT result;
259         END IF;
260     END IF;
261
262     -- Proximity of user's home_ou to the pickup_lib to see if penalty should be ignored.
263     SELECT INTO pickup_prox prox FROM actor.org_unit_proximity WHERE from_org = user_object.home_ou AND to_org = v_pickup_ou;
264     -- Proximity of user's home_ou to the items' lib to see if penalty should be ignored.
265     IF hold_test.distance_is_from_owner THEN
266         SELECT INTO item_prox prox FROM actor.org_unit_proximity WHERE from_org = user_object.home_ou AND to_org = item_cn_object.owning_lib;
267     ELSE
268         SELECT INTO item_prox prox FROM actor.org_unit_proximity WHERE from_org = user_object.home_ou AND to_org = item_object.circ_lib;
269     END IF;
270
271     FOR standing_penalty IN
272         SELECT  DISTINCT csp.*
273           FROM  actor.usr_standing_penalty usp
274                 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
275           WHERE usr = match_user
276                 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
277                 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
278                 AND (csp.ignore_proximity IS NULL OR csp.ignore_proximity < item_prox
279                      OR csp.ignore_proximity < pickup_prox)
280                 AND csp.block_list LIKE '%' || hold_penalty || '%' LOOP
281
282         result.fail_part := standing_penalty.name;
283         result.success := FALSE;
284         done := TRUE;
285         RETURN NEXT result;
286     END LOOP;
287
288     IF hold_test.stop_blocked_user IS TRUE THEN
289         FOR standing_penalty IN
290             SELECT  DISTINCT csp.*
291               FROM  actor.usr_standing_penalty usp
292                     JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
293               WHERE usr = match_user
294                     AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
295                     AND (usp.stop_date IS NULL or usp.stop_date > NOW())
296                     AND csp.block_list LIKE '%CIRC%' LOOP
297
298             result.fail_part := standing_penalty.name;
299             result.success := FALSE;
300             done := TRUE;
301             RETURN NEXT result;
302         END LOOP;
303     END IF;
304
305     IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
306         SELECT    INTO hold_count COUNT(*)
307           FROM    action.hold_request
308           WHERE    usr = match_user
309             AND fulfillment_time IS NULL
310             AND cancel_time IS NULL
311             AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
312
313         IF hold_count >= hold_test.max_holds THEN
314             result.fail_part := 'config.hold_matrix_test.max_holds';
315             result.success := FALSE;
316             done := TRUE;
317             RETURN NEXT result;
318         END IF;
319     END IF;
320
321     IF item_object.age_protect IS NOT NULL THEN
322         SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
323         IF hold_test.distance_is_from_owner THEN
324             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_cn_object.owning_lib);
325         ELSE
326             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
327         END IF;
328         IF use_active_date = 'true' THEN
329             age_protect_date := COALESCE(item_object.active_date, NOW());
330         ELSE
331             age_protect_date := item_object.create_date;
332         END IF;
333         IF age_protect_date + age_protect_object.age > NOW() THEN
334             SELECT INTO calc_age_prox * FROM actor.org_unit_setting WHERE name = 'circ.holds.calculated_age_proximity' AND org_unit = item_object.circ_lib;
335             IF hold_test.distance_is_from_owner THEN
336                 prox_ou := item_cn_object.owning_lib;
337             ELSE
338                 prox_ou := item_object.circ_lib;
339             END IF;
340             IF calc_age_prox.id IS NOT NULL AND calc_age_prox.value = 'true' THEN
341                 SELECT INTO hold_transit_prox action.copy_calculated_proximity(
342                     v_pickup_ou,
343                     v_request_ou,
344                     prox_ou,
345                     item_object.circ_modifier,
346                     item_cn_object.owning_lib,
347                     item_location_object.owning_lib
348                 );
349             ELSE
350                 SELECT INTO hold_transit_prox prox::NUMERIC FROM actor.org_unit_proximity WHERE from_org = prox_ou AND to_org = v_pickup_ou;
351             END IF;
352
353             IF hold_transit_prox > age_protect_object.prox::NUMERIC THEN
354                 result.fail_part := 'config.rule_age_hold_protect.prox';
355                 result.success := FALSE;
356                 done := TRUE;
357                 RETURN NEXT result;
358             END IF;
359         END IF;
360     END IF;
361
362     IF NOT done THEN
363         RETURN NEXT result;
364     END IF;
365
366     RETURN;
367 END;
368 $func$ LANGUAGE plpgsql;
369
370 COMMIT;
371