]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0668.schema.fix_indb_hold_permit.sql
LP#980296: Void Lost Fines if copy claims returned.
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0668.schema.fix_indb_hold_permit.sql
1 -- Evergreen DB patch 0668.schema.fix_indb_hold_permit.sql
2 --
3 -- FIXME: insert description of change, if needed
4 --
5 BEGIN;
6
7
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0668', :eg_version);
10
11 -- FIXME: add/check SQL statements to perform the upgrade
12 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$
13 DECLARE
14     matchpoint_id        INT;
15     user_object        actor.usr%ROWTYPE;
16     age_protect_object    config.rule_age_hold_protect%ROWTYPE;
17     standing_penalty    config.standing_penalty%ROWTYPE;
18     transit_range_ou_type    actor.org_unit_type%ROWTYPE;
19     transit_source        actor.org_unit%ROWTYPE;
20     item_object        asset.copy%ROWTYPE;
21     item_cn_object     asset.call_number%ROWTYPE;
22     item_status_object  config.copy_status%ROWTYPE;
23     item_location_object    asset.copy_location%ROWTYPE;
24     ou_skip              actor.org_unit_setting%ROWTYPE;
25     result            action.matrix_test_result;
26     hold_test        config.hold_matrix_matchpoint%ROWTYPE;
27     use_active_date   TEXT;
28     age_protect_date  TIMESTAMP WITH TIME ZONE;
29     hold_count        INT;
30     hold_transit_prox    INT;
31     frozen_hold_count    INT;
32     context_org_list    INT[];
33     done            BOOL := FALSE;
34 BEGIN
35     SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
36     SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( pickup_ou );
37
38     result.success := TRUE;
39
40     -- Fail if we couldn't find a user
41     IF user_object.id IS NULL THEN
42         result.fail_part := 'no_user';
43         result.success := FALSE;
44         done := TRUE;
45         RETURN NEXT result;
46         RETURN;
47     END IF;
48
49     SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
50
51     -- Fail if we couldn't find a copy
52     IF item_object.id IS NULL THEN
53         result.fail_part := 'no_item';
54         result.success := FALSE;
55         done := TRUE;
56         RETURN NEXT result;
57         RETURN;
58     END IF;
59
60     SELECT INTO matchpoint_id action.find_hold_matrix_matchpoint(pickup_ou, request_ou, match_item, match_user, match_requestor);
61     result.matchpoint := matchpoint_id;
62
63     SELECT INTO ou_skip * FROM actor.org_unit_setting WHERE name = 'circ.holds.target_skip_me' AND org_unit = item_object.circ_lib;
64
65     -- Fail if the circ_lib for the item has circ.holds.target_skip_me set to true
66     IF ou_skip.id IS NOT NULL AND ou_skip.value = 'true' THEN
67         result.fail_part := 'circ.holds.target_skip_me';
68         result.success := FALSE;
69         done := TRUE;
70         RETURN NEXT result;
71         RETURN;
72     END IF;
73
74     -- Fail if user is barred
75     IF user_object.barred IS TRUE THEN
76         result.fail_part := 'actor.usr.barred';
77         result.success := FALSE;
78         done := TRUE;
79         RETURN NEXT result;
80         RETURN;
81     END IF;
82
83     SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
84     SELECT INTO item_status_object * FROM config.copy_status WHERE id = item_object.status;
85     SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
86
87     -- Fail if we couldn't find any matchpoint (requires a default)
88     IF matchpoint_id IS NULL THEN
89         result.fail_part := 'no_matchpoint';
90         result.success := FALSE;
91         done := TRUE;
92         RETURN NEXT result;
93         RETURN;
94     END IF;
95
96     SELECT INTO hold_test * FROM config.hold_matrix_matchpoint WHERE id = matchpoint_id;
97
98     IF hold_test.holdable IS FALSE THEN
99         result.fail_part := 'config.hold_matrix_test.holdable';
100         result.success := FALSE;
101         done := TRUE;
102         RETURN NEXT result;
103     END IF;
104
105     IF item_object.holdable IS FALSE THEN
106         result.fail_part := 'item.holdable';
107         result.success := FALSE;
108         done := TRUE;
109         RETURN NEXT result;
110     END IF;
111
112     IF item_status_object.holdable IS FALSE THEN
113         result.fail_part := 'status.holdable';
114         result.success := FALSE;
115         done := TRUE;
116         RETURN NEXT result;
117     END IF;
118
119     IF item_location_object.holdable IS FALSE THEN
120         result.fail_part := 'location.holdable';
121         result.success := FALSE;
122         done := TRUE;
123         RETURN NEXT result;
124     END IF;
125
126     IF hold_test.transit_range IS NOT NULL THEN
127         SELECT INTO transit_range_ou_type * FROM actor.org_unit_type WHERE id = hold_test.transit_range;
128         IF hold_test.distance_is_from_owner THEN
129             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;
130         ELSE
131             SELECT INTO transit_source * FROM actor.org_unit WHERE id = item_object.circ_lib;
132         END IF;
133
134         PERFORM * FROM actor.org_unit_descendants( transit_source.id, transit_range_ou_type.depth ) WHERE id = pickup_ou;
135
136         IF NOT FOUND THEN
137             result.fail_part := 'transit_range';
138             result.success := FALSE;
139             done := TRUE;
140             RETURN NEXT result;
141         END IF;
142     END IF;
143  
144     FOR standing_penalty IN
145         SELECT  DISTINCT csp.*
146           FROM  actor.usr_standing_penalty usp
147                 JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
148           WHERE usr = match_user
149                 AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
150                 AND (usp.stop_date IS NULL or usp.stop_date > NOW())
151                 AND csp.block_list LIKE '%HOLD%' LOOP
152
153         result.fail_part := standing_penalty.name;
154         result.success := FALSE;
155         done := TRUE;
156         RETURN NEXT result;
157     END LOOP;
158
159     IF hold_test.stop_blocked_user IS TRUE THEN
160         FOR standing_penalty IN
161             SELECT  DISTINCT csp.*
162               FROM  actor.usr_standing_penalty usp
163                     JOIN config.standing_penalty csp ON (csp.id = usp.standing_penalty)
164               WHERE usr = match_user
165                     AND usp.org_unit IN ( SELECT * FROM unnest(context_org_list) )
166                     AND (usp.stop_date IS NULL or usp.stop_date > NOW())
167                     AND csp.block_list LIKE '%CIRC%' LOOP
168     
169             result.fail_part := standing_penalty.name;
170             result.success := FALSE;
171             done := TRUE;
172             RETURN NEXT result;
173         END LOOP;
174     END IF;
175
176     IF hold_test.max_holds IS NOT NULL AND NOT retargetting THEN
177         SELECT    INTO hold_count COUNT(*)
178           FROM    action.hold_request
179           WHERE    usr = match_user
180             AND fulfillment_time IS NULL
181             AND cancel_time IS NULL
182             AND CASE WHEN hold_test.include_frozen_holds THEN TRUE ELSE frozen IS FALSE END;
183
184         IF hold_count >= hold_test.max_holds THEN
185             result.fail_part := 'config.hold_matrix_test.max_holds';
186             result.success := FALSE;
187             done := TRUE;
188             RETURN NEXT result;
189         END IF;
190     END IF;
191
192     IF item_object.age_protect IS NOT NULL THEN
193         SELECT INTO age_protect_object * FROM config.rule_age_hold_protect WHERE id = item_object.age_protect;
194         IF hold_test.distance_is_from_owner THEN
195             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_cn_object.owning_lib);
196         ELSE
197             SELECT INTO use_active_date value FROM actor.org_unit_ancestor_setting('circ.holds.age_protect.active_date', item_object.circ_lib);
198         END IF;
199         IF use_active_date = 'true' THEN
200             age_protect_date := COALESCE(item_object.active_date, NOW());
201         ELSE
202             age_protect_date := item_object.create_date;
203         END IF;
204         IF age_protect_date + age_protect_object.age > NOW() THEN
205             IF hold_test.distance_is_from_owner THEN
206                 SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
207                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_cn_object.owning_lib AND to_org = pickup_ou;
208             ELSE
209                 SELECT INTO hold_transit_prox prox FROM actor.org_unit_proximity WHERE from_org = item_object.circ_lib AND to_org = pickup_ou;
210             END IF;
211
212             IF hold_transit_prox > age_protect_object.prox THEN
213                 result.fail_part := 'config.rule_age_hold_protect.prox';
214                 result.success := FALSE;
215                 done := TRUE;
216                 RETURN NEXT result;
217             END IF;
218         END IF;
219     END IF;
220
221     IF NOT done THEN
222         RETURN NEXT result;
223     END IF;
224
225     RETURN;
226 END;
227 $func$ LANGUAGE plpgsql;
228
229
230 COMMIT;