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