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