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