]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/100.circ_matrix.sql
pushing in-db circ and hold to use directly calculated standing penalties where possible.
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 100.circ_matrix.sql
1
2 BEGIN;
3
4 CREATE OR REPLACE FUNCTION explode_array(anyarray) RETURNS SETOF anyelement AS $BODY$
5     SELECT ($1)[s] FROM generate_series(1, array_upper($1, 1)) AS s;
6 $BODY$
7 LANGUAGE 'sql' IMMUTABLE;
8
9 -- NOTE: current config.item_type should get sip2_media_type and magnetic_media columns
10
11 -- New table needed to handle circ modifiers inside the DB.  Will still require
12 -- central admin.  The circ_modifier column on asset.copy will become an fkey to this table.
13 CREATE TABLE config.circ_modifier (
14         code                    TEXT    PRIMARY KEY,
15         name            TEXT    UNIQUE NOT NULL,
16         description         TEXT        NOT NULL,
17         sip2_media_type TEXT    NOT NULL,
18         magnetic_media  BOOL    NOT NULL DEFAULT TRUE
19 );
20
21 /*
22 -- for instance ...
23 INSERT INTO config.circ_modifier VALUES ( 'DVD', 'DVD', 'um ... DVDs', '001', FALSE );
24 INSERT INTO config.circ_modifier VALUES ( 'VIDEO', 'VIDEO', 'Tapes', '001', TRUE );
25 INSERT INTO config.circ_modifier VALUES ( 'BOOK', 'BOOK', 'Dead tree', '001', FALSE );
26 INSERT INTO config.circ_modifier VALUES ( 'CRAZY_ARL-ATH_SETTING', 'R2R_TAPE', 'reel2reel tape', '007', TRUE );
27 */
28
29 -- But, just to get us started, use this
30 /*
31
32 UPDATE asset.copy SET circ_modifier = UPPER(circ_modifier) WHERE circ_modifier IS NOT NULL AND circ_modifier <> '';
33 UPDATE asset.copy SET circ_modifier = NULL WHERE circ_modifier = '';
34
35 INSERT INTO config.circ_modifier (code, name, description, sip2_media_type )
36     SELECT DISTINCT
37             UPPER(circ_modifier),
38             UPPER(circ_modifier),
39             LOWER(circ_modifier),
40             '001'
41       FROM  asset.copy
42       WHERE circ_modifier IS NOT NULL;
43
44 */
45
46 -- add an fkey pointing to the new circ mod table
47 ALTER TABLE asset.copy ADD CONSTRAINT circ_mod_fkey FOREIGN KEY (circ_modifier) REFERENCES config.circ_modifier (code) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED;
48
49 -- config table to hold the vr_format names
50 CREATE TABLE config.videorecording_format_map (
51         code    TEXT    PRIMARY KEY,
52         value   TEXT    NOT NULL
53 );
54
55 INSERT INTO config.videorecording_format_map VALUES ('a','Beta');
56 INSERT INTO config.videorecording_format_map VALUES ('b','VHS');
57 INSERT INTO config.videorecording_format_map VALUES ('c','U-matic');
58 INSERT INTO config.videorecording_format_map VALUES ('d','EIAJ');
59 INSERT INTO config.videorecording_format_map VALUES ('e','Type C');
60 INSERT INTO config.videorecording_format_map VALUES ('f','Quadruplex');
61 INSERT INTO config.videorecording_format_map VALUES ('g','Laserdisc');
62 INSERT INTO config.videorecording_format_map VALUES ('h','CED');
63 INSERT INTO config.videorecording_format_map VALUES ('i','Betacam');
64 INSERT INTO config.videorecording_format_map VALUES ('j','Betacam SP');
65 INSERT INTO config.videorecording_format_map VALUES ('k','Super-VHS');
66 INSERT INTO config.videorecording_format_map VALUES ('m','M-II');
67 INSERT INTO config.videorecording_format_map VALUES ('o','D-2');
68 INSERT INTO config.videorecording_format_map VALUES ('p','8 mm.');
69 INSERT INTO config.videorecording_format_map VALUES ('q','Hi-8 mm.');
70 INSERT INTO config.videorecording_format_map VALUES ('u','Unknown');
71 INSERT INTO config.videorecording_format_map VALUES ('v','DVD');
72 INSERT INTO config.videorecording_format_map VALUES ('z','Other');
73
74
75
76 /**
77  **  Here we define the tables that make up the circ matrix.  Conceptually, this implements
78  **  the "sparse matrix" that everyone talks about, instead of using traditional rules logic.
79  **  Physically, we cut the matrix up into separate tables (almost 3rd normal form!) that handle
80  **  different portions of the matrix.  This wil simplify creation of the UI (I hope), and help the
81  **  developers focus on specific parts of the matrix.
82  **/
83
84
85 --
86 --                 ****** Which ruleset and tests to use *******
87 --
88 -- * Most specific range for org_unit and grp wins.
89 --
90 -- * circ_modifier match takes precidence over marc_type match, if circ_modifier is set here
91 --
92 -- * marc_type is first checked against the circ_as_type from the copy, then the item type from the marc record
93 --
94 -- * If neither circ_modifier nor marc_type is set (both are NULLABLE) then the entry defines the default
95 --   ruleset and tests for the OU + group (like BOOK in PINES)
96 --
97
98 CREATE TABLE config.circ_matrix_matchpoint (
99         id                      SERIAL  PRIMARY KEY,
100         active                  BOOL    NOT NULL DEFAULT TRUE,
101         org_unit                INT     NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,  -- Set to the top OU for the matchpoint applicability range; we can use org_unit_prox to choose the "best"
102         grp                     INT     NOT NULL REFERENCES permission.grp_tree (id) DEFERRABLE INITIALLY DEFERRED,     -- Set to the top applicable group from the group tree; will need descendents and prox functions for filtering
103         circ_modifier   TEXT    REFERENCES config.circ_modifier (code) DEFERRABLE INITIALLY DEFERRED,
104         marc_type               TEXT    REFERENCES config.item_type_map (code) DEFERRABLE INITIALLY DEFERRED,
105         marc_form               TEXT    REFERENCES config.item_form_map (code) DEFERRABLE INITIALLY DEFERRED,
106         marc_vr_format  TEXT    REFERENCES config.videorecording_format_map (code) DEFERRABLE INITIALLY DEFERRED,
107         ref_flag                BOOL,
108         is_renewal      BOOL,
109         usr_age_lower_bound     INTERVAL,
110         usr_age_upper_bound     INTERVAL,
111         CONSTRAINT ep_once_per_grp_loc_mod_marc UNIQUE (grp, org_unit, circ_modifier, marc_type, marc_form, marc_vr_format, ref_flag, usr_age_lower_bound, usr_age_upper_bound, is_renewal)
112 );
113
114
115 -- Tests to determine if circ can occur for this item at this location for this patron
116 CREATE TABLE config.circ_matrix_test (
117         matchpoint      INT     PRIMARY KEY NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
118         circulate       BOOL    NOT NULL DEFAULT TRUE,  -- Hard "can't circ" flag requiring an override
119         max_items_out   INT,                            -- Total current active circulations must be less than this, NULL means skip (always pass)
120         org_depth       INT,                            -- Set to the top OU for the max-out applicability range
121         script_test     TEXT                            -- filename or javascript source ??
122 );
123
124 -- Tests for max items out by circ_modifier
125 CREATE TABLE config.circ_matrix_circ_mod_test (
126         id          SERIAL     PRIMARY KEY,
127         matchpoint  INT     NOT NULL REFERENCES config.circ_matrix_matchpoint (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
128         items_out   INT     NOT NULL,                           -- Total current active circulations must be less than this, NULL means skip (always pass)
129         circ_mod    TEXT    NOT NULL REFERENCES config.circ_modifier (code) ON DELETE CASCADE ON UPDATE CASCADE  DEFERRABLE INITIALLY DEFERRED-- circ_modifier type that the max out applies to
130 );
131
132
133 -- How to circ, assuming tests pass
134 CREATE TABLE config.circ_matrix_ruleset (
135         matchpoint              INT     PRIMARY KEY REFERENCES config.circ_matrix_matchpoint (id) DEFERRABLE INITIALLY DEFERRED,
136         duration_rule           INT     NOT NULL REFERENCES config.rule_circ_duration (id) DEFERRABLE INITIALLY DEFERRED,
137         recurring_fine_rule     INT     NOT NULL REFERENCES config.rule_recuring_fine (id) DEFERRABLE INITIALLY DEFERRED,
138         max_fine_rule           INT     NOT NULL REFERENCES config.rule_max_fine (id) DEFERRABLE INITIALLY DEFERRED
139 );
140
141 CREATE OR REPLACE FUNCTION action.find_circ_matrix_matchpoint( context_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS INT AS $func$
142 DECLARE
143         current_group   permission.grp_tree%ROWTYPE;
144         user_object     actor.usr%ROWTYPE;
145         item_object     asset.copy%ROWTYPE;
146         rec_descriptor  metabib.rec_descriptor%ROWTYPE;
147         current_mp      config.circ_matrix_matchpoint%ROWTYPE;
148         matchpoint      config.circ_matrix_matchpoint%ROWTYPE;
149 BEGIN
150         SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
151         SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
152         SELECT INTO rec_descriptor r.* FROM metabib.rec_descriptor r JOIN asset.call_number c USING (record) WHERE c.id = item_object.call_number;
153         SELECT INTO current_group * FROM permission.grp_tree WHERE id = user_object.profile;
154
155         LOOP 
156                 -- for each potential matchpoint for this ou and group ...
157                 FOR current_mp IN
158                         SELECT  m.*
159                           FROM  config.circ_matrix_matchpoint m
160                                 JOIN actor.org_unit_ancestors( context_ou ) d ON (m.org_unit = d.id)
161                                 LEFT JOIN actor.org_unit_proximity p ON (p.from_org = context_ou AND p.to_org = d.id)
162                           WHERE m.grp = current_group.id AND m.active
163                           ORDER BY      CASE WHEN p.prox                IS NULL THEN 999 ELSE p.prox END,
164                                         CASE WHEN m.is_renewal = renewal        THEN 64 ELSE 0 END +
165                                         CASE WHEN m.circ_modifier       IS NOT NULL THEN 32 ELSE 0 END +
166                                         CASE WHEN m.marc_type           IS NOT NULL THEN 16 ELSE 0 END +
167                                         CASE WHEN m.marc_form           IS NOT NULL THEN 8 ELSE 0 END +
168                                         CASE WHEN m.marc_vr_format      IS NOT NULL THEN 4 ELSE 0 END +
169                                         CASE WHEN m.ref_flag            IS NOT NULL THEN 2 ELSE 0 END +
170                                         CASE WHEN m.usr_age_lower_bound IS NOT NULL THEN 0.5 ELSE 0 END +
171                                         CASE WHEN m.usr_age_upper_bound IS NOT NULL THEN 0.5 ELSE 0 END DESC LOOP
172
173                         IF current_mp.circ_modifier IS NOT NULL THEN
174                                 CONTINUE WHEN current_mp.circ_modifier <> item_object.circ_modifier;
175                         END IF;
176
177                         IF current_mp.marc_type IS NOT NULL THEN
178                                 IF item_object.circ_as_type IS NOT NULL THEN
179                                         CONTINUE WHEN current_mp.marc_type <> item_object.circ_as_type;
180                                 ELSE
181                                         CONTINUE WHEN current_mp.marc_type <> rec_descriptor.item_type;
182                                 END IF;
183                         END IF;
184
185                         IF current_mp.marc_form IS NOT NULL THEN
186                                 CONTINUE WHEN current_mp.marc_form <> rec_descriptor.item_form;
187                         END IF;
188
189                         IF current_mp.marc_vr_format IS NOT NULL THEN
190                                 CONTINUE WHEN current_mp.marc_vr_format <> rec_descriptor.vr_format;
191                         END IF;
192
193                         IF current_mp.ref_flag IS NOT NULL THEN
194                                 CONTINUE WHEN current_mp.ref_flag <> item_object.ref;
195                         END IF;
196
197                         IF current_mp.usr_age_lower_bound IS NOT NULL THEN
198                                 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_lower_bound < age(user_object.dob);
199                         END IF;
200
201                         IF current_mp.usr_age_upper_bound IS NOT NULL THEN
202                                 CONTINUE WHEN user_object.dob IS NULL OR current_mp.usr_age_upper_bound > age(user_object.dob);
203                         END IF;
204
205
206                         -- everything was undefined or matched
207                         matchpoint = current_mp;
208
209                         EXIT WHEN matchpoint.id IS NOT NULL;
210                 END LOOP;
211
212                 EXIT WHEN current_group.parent IS NULL OR matchpoint.id IS NOT NULL;
213
214                 SELECT INTO current_group * FROM permission.grp_tree WHERE id = current_group.parent;
215         END LOOP;
216
217         RETURN matchpoint.id;
218 END;
219 $func$ LANGUAGE plpgsql;
220
221
222 CREATE TYPE action.matrix_test_result AS ( success BOOL, matchpoint INT, fail_part TEXT );
223 CREATE OR REPLACE FUNCTION action.item_user_circ_test( circ_ou INT, match_item BIGINT, match_user INT, renewal BOOL ) RETURNS SETOF action.matrix_test_result AS $func$
224 DECLARE
225         matchpoint_id           INT;
226         user_object             actor.usr%ROWTYPE;
227         item_object             asset.copy%ROWTYPE;
228         item_status_object      config.copy_status%ROWTYPE;
229         item_location_object    asset.copy_location%ROWTYPE;
230         result                  action.matrix_test_result;
231         circ_test               config.circ_matrix_test%ROWTYPE;
232         out_by_circ_mod         config.circ_matrix_circ_mod_test%ROWTYPE;
233         patron_penalties                INT;
234         tmp_grp                 INT;
235         items_out               INT;
236         max_overdue             INT;
237         items_overdue           INT;
238         overdue_orgs            INT[];
239         max_fines                   NUMERIC(8,2) := 0.0;
240         current_fines           NUMERIC(8,2) := 0.0;
241         tmp_fines               NUMERIC(8,2);
242         tmp_groc                RECORD;
243         tmp_circ                RECORD;
244         done                    BOOL := FALSE;
245 BEGIN
246         result.success := TRUE;
247
248         -- Fail if the user is BARRED
249         SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
250
251         -- Fail if we couldn't find a set of tests
252         IF user_object.id IS NULL THEN
253                 result.fail_part := 'no_user';
254                 result.success := FALSE;
255                 done := TRUE;
256                 RETURN NEXT result;
257                 RETURN;
258         END IF;
259
260         IF user_object.barred IS TRUE THEN
261                 result.fail_part := 'actor.usr.barred';
262                 result.success := FALSE;
263                 done := TRUE;
264                 RETURN NEXT result;
265         END IF;
266
267         -- Fail if the item can't circulate
268         SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
269         IF item_object.circulate IS FALSE THEN
270                 result.fail_part := 'asset.copy.circulate';
271                 result.success := FALSE;
272                 done := TRUE;
273                 RETURN NEXT result;
274         END IF;
275
276         -- Fail if the item isn't in a circulateable status on a non-renewal
277         IF NOT renewal AND item_object.status NOT IN ( 0, 7, 8 ) THEN 
278                 result.fail_part := 'asset.copy.status';
279                 result.success := FALSE;
280                 done := TRUE;
281                 RETURN NEXT result;
282         ELSIF renewal AND item_object.status <> 1 THEN
283                 result.fail_part := 'asset.copy.status';
284                 result.success := FALSE;
285                 done := TRUE;
286                 RETURN NEXT result;
287         END IF;
288
289         -- Fail if the item can't circulate because of the shelving location
290         SELECT INTO item_location_object * FROM asset.copy_location WHERE id = item_object.location;
291         IF item_location_object.circulate IS FALSE THEN
292                 result.fail_part := 'asset.copy_location.circulate';
293                 result.success := FALSE;
294                 done := TRUE;
295                 RETURN NEXT result;
296         END IF;
297
298         SELECT INTO matchpoint_id action.find_circ_matrix_matchpoint(circ_ou, match_item, match_user, renewal);
299         result.matchpoint := matchpoint_id;
300
301         SELECT INTO circ_test * from config.circ_matrix_test WHERE matchpoint = result.matchpoint;
302
303         IF circ_test.org_depth IS NOT NULL THEN
304                 SELECT INTO overdue_orgs ARRAY_ACCUM(id) FROM actor.org_unit_descendants( circ_ou, circ_test.org_depth );
305         END IF; 
306
307         -- Fail if we couldn't find a set of tests
308         IF result.matchpoint IS NULL THEN
309                 result.fail_part := 'no_matchpoint';
310                 result.success := FALSE;
311                 done := TRUE;
312                 RETURN NEXT result;
313         END IF;
314
315         -- Fail if the test is set to hard non-circulating
316         IF circ_test.circulate IS FALSE THEN
317                 result.fail_part := 'config.circ_matrix_test.circulate';
318                 result.success := FALSE;
319                 done := TRUE;
320                 RETURN NEXT result;
321         END IF;
322
323     SELECT  INTO patron_penalties COUNT(*)
324       FROM  actor.usr_standing_penalty usp
325             JOIN config.standing_penalty csp ON (csp.id = usp.penalty)
326       WHERE usr = match_user
327             AND csp.block_list LIKE '%RENEW%';
328
329     IF patron_penalties > 0 THEN
330         result.fail_part := 'config.circ_matrix_test.stop_blocked_user.circ';
331         result.success := FALSE;
332         done := TRUE;
333         RETURN NEXT result;
334     END IF;
335
336     patron_penalties := 0;
337
338     SELECT  INTO patron_penalties COUNT(*)
339       FROM  actor.usr_standing_penalty usp
340             JOIN config.standing_penalty csp ON (csp.id = usp.penalty)
341       WHERE usr = match_user
342             AND csp.block_list LIKE '%CIRC%';
343
344     IF patron_penalties > 0 THEN
345         result.fail_part := 'config.circ_matrix_test.stop_blocked_user.renew';
346         result.success := FALSE;
347         done := TRUE;
348         RETURN NEXT result;
349     END IF;
350
351         -- Fail if the user has too many items checked out
352         IF circ_test.max_items_out IS NOT NULL THEN
353         SELECT  INTO items_out COUNT(*)
354           FROM  action.circulation
355           WHERE usr = match_user
356                 AND (circ_test.org_depth IS NULL OR (circ_test.org_depth IS NOT NULL AND circ_lib IN ( SELECT * FROM explode_array(overdue_orgs) )))
357                 AND checkin_time IS NULL
358                 AND (stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE') OR stop_fines IS NULL);
359                 IF items_out >= circ_test.max_items_out THEN
360                 result.fail_part := 'config.circ_matrix_test.max_items_out';
361                         result.success := FALSE;
362                         done := TRUE;
363                         RETURN NEXT result;
364                 END IF;
365         END IF;
366
367         -- Fail if the user has too many items with specific circ_modifiers checked out
368         FOR out_by_circ_mod IN SELECT * FROM config.circ_matrix_circ_mod_test WHERE matchpoint = matchpoint_id LOOP
369                 SELECT  INTO items_out COUNT(*)
370                   FROM  action.circulation circ
371                         JOIN asset.copy cp ON (cp.id = circ.target_copy)
372                   WHERE circ.usr = match_user
373                         AND (circ_test.org_depth IS NULL OR (circ_test.org_depth IS NOT NULL AND circ_lib IN ( SELECT * FROM explode_array(overdue_orgs) )))
374                         AND circ.checkin_time IS NULL
375                         AND (circ.stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE') OR circ.stop_fines IS NULL)
376                         AND cp.circ_modifier = out_by_circ_mod.circ_mod;
377                 IF items_out >= out_by_circ_mod.items_out THEN
378                         result.fail_part := 'config.circ_matrix_circ_mod_test';
379                         result.success := FALSE;
380                         done := TRUE;
381                         RETURN NEXT result;
382                 END IF;
383         END LOOP;
384
385         -- Fail if the user has too many overdue items
386     tmp_grp := user_object.profile;
387     LOOP
388         SELECT pgpt.threshold::INT INTO max_overdue FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 2;
389         IF max_overdue IS NULL THEN
390             SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
391         ELSE
392             EXIT;
393         END IF;
394
395         IF tmp_grp IS NULL THEN
396             EXIT;
397         END IF;
398     END LOOP;
399
400         IF max_overdue IS NOT NULL THEN
401                 SELECT  INTO items_overdue COUNT(*)
402                   FROM  action.circulation
403                   WHERE usr = match_user
404                         AND (circ_test.org_depth IS NULL OR (circ_test.org_depth IS NOT NULL AND circ_lib IN ( SELECT * FROM explode_array(overdue_orgs) )))
405                         AND checkin_time IS NULL
406                         AND due_date < NOW()
407                         AND (stop_fines NOT IN ('LOST','CLAIMSRETURNED','LONGOVERDUE') OR stop_fines IS NULL);
408                 IF items_overdue >= max_overdue THEN
409             DELETE FROM actor.usr_standing_penalty WHERE usr = match_usr AND standing_penalty = 2;
410             INSERT INTO actor.usr_standing_penalty (usr, standing_penalty) VALUES (match_usr, 2);
411                         result.fail_part := 'config.circ_matrix_test.max_overdue';
412                         result.success := FALSE;
413                         done := TRUE;
414                         RETURN NEXT result;
415                 END IF;
416         END IF;
417
418         -- Fail if the user has a high fine balance
419     tmp_grp := user_object.profile;
420     LOOP
421         SELECT pgpt.threshold INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 1;
422         IF max_overdue IS NULL THEN
423             SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
424         ELSE
425             EXIT;
426         END IF;
427
428         IF tmp_grp IS NULL THEN
429             EXIT;
430         END IF;
431     END LOOP;
432
433         IF max_fines IS NOT NULL THEN
434                 FOR tmp_groc IN SELECT * FROM money.grocery WHERE usr = match_usr AND xact_finish IS NULL AND (circ_test.org_depth IS NULL OR (circ_test.org_depth IS NOT NULL AND billing_location IN ( SELECT * FROM explode_array(overdue_orgs) ))) LOOP
435                         SELECT INTO tmp_fines SUM( amount ) FROM money.billing WHERE xact = tmp_groc.id AND NOT voided;
436                         current_fines = current_fines + COALESCE(tmp_fines, 0.0);
437                         SELECT INTO tmp_fines SUM( amount ) FROM money.payment WHERE xact = tmp_groc.id AND NOT voided;
438                         current_fines = current_fines - COALESCE(tmp_fines, 0.0);
439                 END LOOP;
440
441                 FOR tmp_circ IN SELECT * FROM action.circulation WHERE usr = match_usr AND xact_finish IS NULL AND (circ_test.org_depth IS NULL OR (circ_test.org_depth IS NOT NULL AND circ_lib IN ( SELECT * FROM explode_array(overdue_orgs) ))) LOOP
442                         SELECT INTO tmp_fines SUM( amount ) FROM money.billing WHERE xact = tmp_circ.id AND NOT voided;
443                         current_fines = current_fines + COALESCE(tmp_fines, 0.0);
444                         SELECT INTO tmp_fines SUM( amount ) FROM money.payment WHERE xact = tmp_circ.id AND NOT voided;
445                         current_fines = current_fines - COALESCE(tmp_fines, 0.0);
446                 END LOOP;
447
448                 IF current_fines >= max_fines THEN
449             DELETE FROM actor.usr_standing_penalty WHERE usr = match_usr AND standing_penalty = 1;
450             INSERT INTO actor.usr_standing_penalty (usr, standing_penalty) VALUES (match_usr, 1);
451                         result.fail_part := 'config.circ_matrix_test.max_fines';
452                         result.success := FALSE;
453                         RETURN NEXT result;
454                         done := TRUE;
455                 END IF;
456         END IF;
457
458         -- If we passed everything, return the successful matchpoint id
459         IF NOT done THEN
460                 RETURN NEXT result;
461         END IF;
462
463         RETURN;
464 END;
465 $func$ LANGUAGE plpgsql;
466
467 CREATE OR REPLACE FUNCTION action.item_user_circ_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
468         SELECT * FROM action.item_user_circ_test( $1, $2, $3, FALSE );
469 $func$ LANGUAGE SQL;
470
471 CREATE OR REPLACE FUNCTION action.item_user_renew_test( INT, BIGINT, INT ) RETURNS SETOF action.matrix_test_result AS $func$
472         SELECT * FROM action.item_user_circ_test( $1, $2, $3, TRUE );
473 $func$ LANGUAGE SQL;
474
475 CREATE OR REPLACE FUNCTION actor.refresh_auto_penalties( user INT ) RETURNS INT AS $func$
476 $func$ LANGUAGE plpgsql;
477
478 COMMIT;
479