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