]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0873.function.hold_test_params.sql
LP1893463: Follow-up to address de-duplication and adding release notes.
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0873.function.hold_test_params.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('0873', :eg_version);
4
5 CREATE OR REPLACE FUNCTION action.find_hold_matrix_matchpoint(pickup_ou integer, request_ou integer, match_item bigint, match_user integer, match_requestor integer)
6   RETURNS integer AS
7 $func$
8 DECLARE
9     requestor_object    actor.usr%ROWTYPE;
10     user_object         actor.usr%ROWTYPE;
11     item_object         asset.copy%ROWTYPE;
12     item_cn_object      asset.call_number%ROWTYPE;
13     my_item_age         INTERVAL;
14     rec_descriptor      metabib.rec_descriptor%ROWTYPE;
15     matchpoint          config.hold_matrix_matchpoint%ROWTYPE;
16     weights             config.hold_matrix_weights%ROWTYPE;
17     denominator         NUMERIC(6,2);
18     v_pickup_ou         ALIAS FOR pickup_ou;
19     v_request_ou         ALIAS FOR request_ou;
20 BEGIN
21     SELECT INTO user_object         * FROM actor.usr                WHERE id = match_user;
22     SELECT INTO requestor_object    * FROM actor.usr                WHERE id = match_requestor;
23     SELECT INTO item_object         * FROM asset.copy               WHERE id = match_item;
24     SELECT INTO item_cn_object      * FROM asset.call_number        WHERE id = item_object.call_number;
25     SELECT INTO rec_descriptor      * FROM metabib.rec_descriptor   WHERE record = item_cn_object.record;
26
27     SELECT INTO my_item_age age(coalesce(item_object.active_date, now()));
28
29     -- The item's owner should probably be the one determining if the item is holdable
30     -- How to decide that is debatable. Decided to default to the circ library (where the item lives)
31     -- This flag will allow for setting it to the owning library (where the call number "lives")
32     PERFORM * FROM config.internal_flag WHERE name = 'circ.holds.weight_owner_not_circ' AND enabled;
33
34     -- Grab the closest set circ weight setting.
35     IF NOT FOUND THEN
36         -- Default to circ library
37         SELECT INTO weights hw.*
38           FROM config.weight_assoc wa
39                JOIN config.hold_matrix_weights hw ON (hw.id = wa.hold_weights)
40                JOIN actor.org_unit_ancestors_distance( item_object.circ_lib ) d ON (wa.org_unit = d.id)
41           WHERE active
42           ORDER BY d.distance
43           LIMIT 1;
44     ELSE
45         -- Flag is set, use owning library
46         SELECT INTO weights hw.*
47           FROM config.weight_assoc wa
48                JOIN config.hold_matrix_weights hw ON (hw.id = wa.hold_weights)
49                JOIN actor.org_unit_ancestors_distance( item_cn_object.owning_lib ) d ON (wa.org_unit = d.id)
50           WHERE active
51           ORDER BY d.distance
52           LIMIT 1;
53     END IF;
54
55     -- No weights? Bad admin! Defaults to handle that anyway.
56     IF weights.id IS NULL THEN
57         weights.user_home_ou    := 5.0;
58         weights.request_ou      := 5.0;
59         weights.pickup_ou       := 5.0;
60         weights.item_owning_ou  := 5.0;
61         weights.item_circ_ou    := 5.0;
62         weights.usr_grp         := 7.0;
63         weights.requestor_grp   := 8.0;
64         weights.circ_modifier   := 4.0;
65         weights.marc_type       := 3.0;
66         weights.marc_form       := 2.0;
67         weights.marc_bib_level  := 1.0;
68         weights.marc_vr_format  := 1.0;
69         weights.juvenile_flag   := 4.0;
70         weights.ref_flag        := 0.0;
71         weights.item_age        := 0.0;
72     END IF;
73
74     -- Determine the max (expected) depth (+1) of the org tree and max depth of the permisson tree
75     -- If you break your org tree with funky parenting this may be wrong
76     -- Note: This CTE is duplicated in the find_circ_matrix_matchpoint function, and it may be a good idea to split it off to a function
77     -- We use one denominator for all tree-based checks for when permission groups and org units have the same weighting
78     WITH all_distance(distance) AS (
79             SELECT depth AS distance FROM actor.org_unit_type
80         UNION
81             SELECT distance AS distance FROM permission.grp_ancestors_distance((SELECT id FROM permission.grp_tree WHERE parent IS NULL))
82         )
83     SELECT INTO denominator MAX(distance) + 1 FROM all_distance;
84
85     -- To ATTEMPT to make this work like it used to, make it reverse the user/requestor profile ids.
86     -- This may be better implemented as part of the upgrade script?
87     -- Set usr_grp = requestor_grp, requestor_grp = 1 or something when this flag is already set
88     -- Then remove this flag, of course.
89     PERFORM * FROM config.internal_flag WHERE name = 'circ.holds.usr_not_requestor' AND enabled;
90
91     IF FOUND THEN
92         -- Note: This, to me, is REALLY hacky. I put it in anyway.
93         -- If you can't tell, this is a single call swap on two variables.
94         SELECT INTO user_object.profile, requestor_object.profile
95                     requestor_object.profile, user_object.profile;
96     END IF;
97
98     -- Select the winning matchpoint into the matchpoint variable for returning
99     SELECT INTO matchpoint m.*
100       FROM  config.hold_matrix_matchpoint m
101             /*LEFT*/ JOIN permission.grp_ancestors_distance( requestor_object.profile ) rpgad ON m.requestor_grp = rpgad.id
102             LEFT JOIN permission.grp_ancestors_distance( user_object.profile ) upgad ON m.usr_grp = upgad.id
103             LEFT JOIN actor.org_unit_ancestors_distance( v_pickup_ou ) puoua ON m.pickup_ou = puoua.id
104             LEFT JOIN actor.org_unit_ancestors_distance( v_request_ou ) rqoua ON m.request_ou = rqoua.id
105             LEFT JOIN actor.org_unit_ancestors_distance( item_cn_object.owning_lib ) cnoua ON m.item_owning_ou = cnoua.id
106             LEFT JOIN actor.org_unit_ancestors_distance( item_object.circ_lib ) iooua ON m.item_circ_ou = iooua.id
107             LEFT JOIN actor.org_unit_ancestors_distance( user_object.home_ou  ) uhoua ON m.user_home_ou = uhoua.id
108       WHERE m.active
109             -- Permission Groups
110          -- AND (m.requestor_grp        IS NULL OR upgad.id IS NOT NULL) -- Optional Requestor Group?
111             AND (m.usr_grp              IS NULL OR upgad.id IS NOT NULL)
112             -- Org Units
113             AND (m.pickup_ou            IS NULL OR (puoua.id IS NOT NULL AND (puoua.distance = 0 OR NOT m.strict_ou_match)))
114             AND (m.request_ou           IS NULL OR (rqoua.id IS NOT NULL AND (rqoua.distance = 0 OR NOT m.strict_ou_match)))
115             AND (m.item_owning_ou       IS NULL OR (cnoua.id IS NOT NULL AND (cnoua.distance = 0 OR NOT m.strict_ou_match)))
116             AND (m.item_circ_ou         IS NULL OR (iooua.id IS NOT NULL AND (iooua.distance = 0 OR NOT m.strict_ou_match)))
117             AND (m.user_home_ou         IS NULL OR (uhoua.id IS NOT NULL AND (uhoua.distance = 0 OR NOT m.strict_ou_match)))
118             -- Static User Checks
119             AND (m.juvenile_flag        IS NULL OR m.juvenile_flag = user_object.juvenile)
120             -- Static Item Checks
121             AND (m.circ_modifier        IS NULL OR m.circ_modifier = item_object.circ_modifier)
122             AND (m.marc_type            IS NULL OR m.marc_type = COALESCE(item_object.circ_as_type, rec_descriptor.item_type))
123             AND (m.marc_form            IS NULL OR m.marc_form = rec_descriptor.item_form)
124             AND (m.marc_bib_level       IS NULL OR m.marc_bib_level = rec_descriptor.bib_level)
125             AND (m.marc_vr_format       IS NULL OR m.marc_vr_format = rec_descriptor.vr_format)
126             AND (m.ref_flag             IS NULL OR m.ref_flag = item_object.ref)
127             AND (m.item_age             IS NULL OR (my_item_age IS NOT NULL AND m.item_age > my_item_age))
128       ORDER BY
129             -- Permission Groups
130             CASE WHEN rpgad.distance    IS NOT NULL THEN 2^(2*weights.requestor_grp - (rpgad.distance/denominator)) ELSE 0.0 END +
131             CASE WHEN upgad.distance    IS NOT NULL THEN 2^(2*weights.usr_grp - (upgad.distance/denominator)) ELSE 0.0 END +
132             -- Org Units
133             CASE WHEN puoua.distance    IS NOT NULL THEN 2^(2*weights.pickup_ou - (puoua.distance/denominator)) ELSE 0.0 END +
134             CASE WHEN rqoua.distance    IS NOT NULL THEN 2^(2*weights.request_ou - (rqoua.distance/denominator)) ELSE 0.0 END +
135             CASE WHEN cnoua.distance    IS NOT NULL THEN 2^(2*weights.item_owning_ou - (cnoua.distance/denominator)) ELSE 0.0 END +
136             CASE WHEN iooua.distance    IS NOT NULL THEN 2^(2*weights.item_circ_ou - (iooua.distance/denominator)) ELSE 0.0 END +
137             CASE WHEN uhoua.distance    IS NOT NULL THEN 2^(2*weights.user_home_ou - (uhoua.distance/denominator)) ELSE 0.0 END +
138             -- Static User Checks       -- Note: 4^x is equiv to 2^(2*x)
139             CASE WHEN m.juvenile_flag   IS NOT NULL THEN 4^weights.juvenile_flag ELSE 0.0 END +
140             -- Static Item Checks
141             CASE WHEN m.circ_modifier   IS NOT NULL THEN 4^weights.circ_modifier ELSE 0.0 END +
142             CASE WHEN m.marc_type       IS NOT NULL THEN 4^weights.marc_type ELSE 0.0 END +
143             CASE WHEN m.marc_form       IS NOT NULL THEN 4^weights.marc_form ELSE 0.0 END +
144             CASE WHEN m.marc_vr_format  IS NOT NULL THEN 4^weights.marc_vr_format ELSE 0.0 END +
145             CASE WHEN m.ref_flag        IS NOT NULL THEN 4^weights.ref_flag ELSE 0.0 END +
146             -- Item age has a slight adjustment to weight based on value.
147             -- This should ensure that a shorter age limit comes first when all else is equal.
148             -- NOTE: This assumes that intervals will normally be in days.
149             CASE WHEN m.item_age            IS NOT NULL THEN 4^weights.item_age - 86400/EXTRACT(EPOCH FROM m.item_age) ELSE 0.0 END DESC,
150             -- Final sort on id, so that if two rules have the same sorting in the previous sort they have a defined order
151             -- This prevents "we changed the table order by updating a rule, and we started getting different results"
152             m.id;
153
154     -- Return just the ID for now
155     RETURN matchpoint.id;
156 END;
157 $func$ LANGUAGE 'plpgsql';
158
159 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$
160 DECLARE
161     matchpoint_id        INT;
162     user_object        actor.usr%ROWTYPE;
163     age_protect_object    config.rule_age_hold_protect%ROWTYPE;
164     standing_penalty    config.standing_penalty%ROWTYPE;
165     transit_range_ou_type    actor.org_unit_type%ROWTYPE;
166     transit_source        actor.org_unit%ROWTYPE;
167     item_object        asset.copy%ROWTYPE;
168     item_cn_object     asset.call_number%ROWTYPE;
169     item_status_object  config.copy_status%ROWTYPE;
170     item_location_object    asset.copy_location%ROWTYPE;
171     ou_skip              actor.org_unit_setting%ROWTYPE;
172     result            action.matrix_test_result;
173     hold_test        config.hold_matrix_matchpoint%ROWTYPE;
174     use_active_date   TEXT;
175     age_protect_date  TIMESTAMP WITH TIME ZONE;
176     hold_count        INT;
177     hold_transit_prox    INT;
178     frozen_hold_count    INT;
179     context_org_list    INT[];
180     done            BOOL := FALSE;
181     hold_penalty TEXT;
182     v_pickup_ou ALIAS FOR pickup_ou;
183     v_request_ou ALIAS FOR request_ou;
184 BEGIN
185     SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
186     SELECT INTO context_org_list ARRAY_AGG(id) FROM actor.org_unit_full_path( v_pickup_ou );
187
188     result.success := TRUE;
189
190     -- The HOLD penalty block only applies to new holds.
191     -- The CAPTURE penalty block applies to existing holds.
192     hold_penalty := 'HOLD';
193     IF retargetting THEN
194         hold_penalty := 'CAPTURE';
195     END IF;
196
197     -- Fail if we couldn't find a user
198     IF user_object.id IS NULL THEN
199         result.fail_part := 'no_user';
200         result.success := FALSE;
201         done := TRUE;
202         RETURN NEXT result;
203         RETURN;
204     END IF;
205
206     SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
207
208     -- Fail if we couldn't find a copy
209     IF item_object.id IS NULL THEN
210         result.fail_part := 'no_item';
211         result.success := FALSE;
212         done := TRUE;
213         RETURN NEXT result;
214         RETURN;
215     END IF;
216
217     SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(v_pickup_ou, v_request_ou, match_item, match_user, match_requestor);
218     result.matchpoint := matchpoint_id;
219
220     SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
221
222     -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
223     IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
224         result.fail_part := 'circ.holds.target_skip_me';
225         result.success := FALSE;
226         done := TRUE;
227         RETURN NEXT result;
228         RETURN;
229     END IF;
230
231     -- Fail if user is barred
232     IF user_object.barred IS TRUE THEN
233         result.fail_part := 'actor.usr.barred';
234         result.success := FALSE;
235         done := TRUE;
236         RETURN NEXT result;
237         RETURN;
238     END IF;
239
240     SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
241     SELECT INTO item_status_object * FROM config.copy_status WHERE id = item_object.status;
242     SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
243
244     -- Fail if we couldn't find any matchpoint (requires a default)
245     IF matchpoint_id IS NULL THEN
246         result.fail_part := 'no_matchpoint';
247         result.success := FALSE;
248         done := TRUE;
249         RETURN NEXT result;
250         RETURN;
251     END IF;
252
253     SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
254
255     IF hold_test.holdable IS FALSE THEN
256         result.fail_part := 'config.hold_matrix_test.holdable';
257         result.success := FALSE;
258         done := TRUE;
259         RETURN NEXT result;
260     END IF;
261
262     IF item_object.holdable IS FALSE THEN
263         result.fail_part := 'item.holdable';
264         result.success := FALSE;
265         done := TRUE;
266         RETURN NEXT result;
267     END IF;
268
269     IF item_status_object.holdable IS FALSE THEN
270         result.fail_part := 'status.holdable';
271         result.success := FALSE;
272         done := TRUE;
273         RETURN NEXT result;
274     END IF;
275
276     IF item_location_object.holdable IS FALSE THEN
277         result.fail_part := 'location.holdable';
278         result.success := FALSE;
279         done := TRUE;
280         RETURN NEXT result;
281     END IF;
282
283     IF hold_test.transit_range IS NOT NULL THEN
284         SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
285         IF hold_test.distance_is_from_owner THEN
286             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;
287         ELSE
288             SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
289         END IF;
290
291         PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = v_pickup_ou;
292
293         IF NOT FOUND THEN
294             result.fail_part := 'transit_range';
295             result.success := FALSE;
296             done := TRUE;
297             RETURN NEXT result;
298         END IF;
299     END IF;
300  
301     FOR standing_penalty IN
302         SELECT  DISTINCT csp.*
303           FROM  actor.usr_standing_penalty usp
304                 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
305           WHERE usr = match_user
306                 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
307                 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
308                 AND csp.block_list LIKE '%' || hold_penalty || '%' LOOP
309
310         result.fail_part := standing_penalty.name;
311         result.success := FALSE;
312         done := TRUE;
313         RETURN NEXT result;
314     END LOOP;
315
316     IF hold_test.stop_blocked_user IS TRUE THEN
317         FOR standing_penalty IN
318             SELECT  DISTINCT csp.*
319               FROM  actor.usr_standing_penalty usp
320                     JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
321               WHERE usr = match_user
322                     AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
323                     AND (usp.stop_date IS NULL or usp.stop_date > NOW())
324                     AND csp.block_list LIKE '%CIRC%' LOOP
325     
326             result.fail_part := standing_penalty.name;
327             result.success := FALSE;
328             done := TRUE;
329             RETURN NEXT result;
330         END LOOP;
331     END IF;
332
333     IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
334         SELECT    INTO hold_count COUNT(*)
335           FROM    action.hold_request
336           WHERE    usr = match_user
337             AND fulfillment_time IS NULL
338             AND cancel_time IS NULL
339             AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
340
341         IF hold_count >= hold_test.max_holds THEN
342             result.fail_part := 'config.hold_matrix_test.max_holds';
343             result.success := FALSE;
344             done := TRUE;
345             RETURN NEXT result;
346         END IF;
347     END IF;
348
349     IF item_object.age_protect IS NOT NULL THEN
350         SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
351         IF hold_test.distance_is_from_owner THEN
352             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_cn_object.owning_lib);
353         ELSE
354             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
355         END IF;
356         IF use_active_date = 'true' THEN
357             age_protect_date := COALESCE(item_object.active_date, NOW());
358         ELSE
359             age_protect_date := item_object.create_date;
360         END IF;
361         IF age_protect_date + age_protect_object.age > NOW() THEN
362             IF hold_test.distance_is_from_owner THEN
363                 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
364                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = v_pickup_ou;
365             ELSE
366                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = v_pickup_ou;
367             END IF;
368
369             IF hold_transit_prox > age_protect_object.prox THEN
370                 result.fail_part := 'config.rule_age_hold_protect.prox';
371                 result.success := FALSE;
372                 done := TRUE;
373                 RETURN NEXT result;
374             END IF;
375         END IF;
376     END IF;
377
378     IF NOT done THEN
379         RETURN NEXT result;
380     END IF;
381
382     RETURN;
383 END;
384 $func$ LANGUAGE plpgsql;
385
386 COMMIT;
387