Bug 13590: Add ability to charge fines at start of charge period
[koha.git] / admin / smart-rules.pl
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # copyright 2010 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Debug;
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use Koha::Database;
31
32 my $input = CGI->new;
33 my $dbh = C4::Context->dbh;
34
35 # my $flagsrequired;
36 # $flagsrequired->{circulation}=1;
37 my ($template, $loggedinuser, $cookie)
38     = get_template_and_user({template_name => "admin/smart-rules.tt",
39                             query => $input,
40                             type => "intranet",
41                             authnotrequired => 0,
42                             flagsrequired => {parameters => 'manage_circ_rules'},
43                             debug => 1,
44                             });
45
46 my $type=$input->param('type');
47 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
48 my $op = $input->param('op') || q{};
49
50 if ($op eq 'delete') {
51     my $itemtype     = $input->param('itemtype');
52     my $categorycode = $input->param('categorycode');
53     $debug and warn "deleting $1 $2 $branch";
54
55     my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
56     $sth_Idelete->execute($branch, $categorycode, $itemtype);
57 }
58 elsif ($op eq 'delete-branch-cat') {
59     my $categorycode  = $input->param('categorycode');
60     if ($branch eq "*") {
61         if ($categorycode eq "*") {
62             my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
63             $sth_delete->execute();
64         } else {
65             my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
66                                             WHERE categorycode = ?");
67             $sth_delete->execute($categorycode);
68         }
69     } elsif ($categorycode eq "*") {
70         my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
71                                         WHERE branchcode = ?");
72         $sth_delete->execute($branch);
73     } else {
74         my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
75                                         WHERE branchcode = ?
76                                         AND categorycode = ?");
77         $sth_delete->execute($branch, $categorycode);
78     }
79 }
80 elsif ($op eq 'delete-branch-item') {
81     my $itemtype  = $input->param('itemtype');
82     if ($branch eq "*") {
83         if ($itemtype eq "*") {
84             my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
85             $sth_delete->execute();
86         } else {
87             my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
88                                             WHERE itemtype = ?");
89             $sth_delete->execute($itemtype);
90         }
91     } elsif ($itemtype eq "*") {
92         my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
93                                         WHERE branchcode = ?");
94         $sth_delete->execute($branch);
95     } else {
96         my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
97                                         WHERE branchcode = ?
98                                         AND itemtype = ?");
99         $sth_delete->execute($branch, $itemtype);
100     }
101 }
102 # save the values entered
103 elsif ($op eq 'add') {
104     my $br = $branch; # branch
105     my $bor  = $input->param('categorycode'); # borrower category
106     my $itemtype  = $input->param('itemtype');     # item type
107     my $fine = $input->param('fine');
108     my $finedays     = $input->param('finedays');
109     my $maxsuspensiondays = $input->param('maxsuspensiondays');
110     $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
111     my $firstremind  = $input->param('firstremind');
112     my $chargeperiod = $input->param('chargeperiod');
113     my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
114     my $maxissueqty  = $input->param('maxissueqty');
115     my $maxonsiteissueqty  = $input->param('maxonsiteissueqty');
116     my $renewalsallowed  = $input->param('renewalsallowed');
117     my $renewalperiod    = $input->param('renewalperiod');
118     my $norenewalbefore  = $input->param('norenewalbefore');
119     $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
120     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
121     my $reservesallowed  = $input->param('reservesallowed');
122     my $onshelfholds     = $input->param('onshelfholds') || 0;
123     $maxissueqty =~ s/\s//g;
124     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
125     $maxonsiteissueqty =~ s/\s//g;
126     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
127     my $issuelength  = $input->param('issuelength');
128     my $lengthunit  = $input->param('lengthunit');
129     my $hardduedate = $input->param('hardduedate');
130     $hardduedate = format_date_in_iso($hardduedate);
131     my $hardduedatecompare = $input->param('hardduedatecompare');
132     my $rentaldiscount = $input->param('rentaldiscount');
133     my $opacitemholds = $input->param('opacitemholds') || 0;
134     my $overduefinescap = $input->param('overduefinescap') || undef;
135     $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
136
137     my $schema = Koha::Database->new()->schema();
138     my $rs = $schema->resultset('Issuingrule');
139
140     my $params = {
141         branchcode             => $br,
142         categorycode           => $bor,
143         itemtype               => $itemtype,
144         fine                   => $fine,
145         finedays               => $finedays,
146         maxsuspensiondays      => $maxsuspensiondays,
147         firstremind            => $firstremind,
148         chargeperiod           => $chargeperiod,
149         chargeperiod_charge_at => $chargeperiod_charge_at,
150         maxissueqty            => $maxissueqty,
151         maxonsiteissueqty      => $maxonsiteissueqty,
152         renewalsallowed        => $renewalsallowed,
153         renewalperiod          => $renewalperiod,
154         norenewalbefore        => $norenewalbefore,
155         auto_renew             => $auto_renew,
156         reservesallowed        => $reservesallowed,
157         issuelength            => $issuelength,
158         lengthunit             => $lengthunit,
159         hardduedate            => $hardduedate,
160         hardduedatecompare     => $hardduedatecompare,
161         rentaldiscount         => $rentaldiscount,
162         onshelfholds           => $onshelfholds,
163         opacitemholds          => $opacitemholds,
164         overduefinescap        => $overduefinescap,
165     };
166
167     $rs->update_or_create($params);
168
169 }
170 elsif ($op eq "set-branch-defaults") {
171     my $categorycode  = $input->param('categorycode');
172     my $maxissueqty   = $input->param('maxissueqty');
173     my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
174     my $holdallowed   = $input->param('holdallowed');
175     my $returnbranch  = $input->param('returnbranch');
176     $maxissueqty =~ s/\s//g;
177     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
178     $maxonsiteissueqty =~ s/\s//g;
179     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
180     $holdallowed =~ s/\s//g;
181     $holdallowed = undef if $holdallowed !~ /^\d+/;
182
183     if ($branch eq "*") {
184         my $sth_search = $dbh->prepare("SELECT count(*) AS total
185                                         FROM default_circ_rules");
186         my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
187                                         (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
188                                         VALUES (?, ?, ?, ?)");
189         my $sth_update = $dbh->prepare("UPDATE default_circ_rules
190                                         SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
191
192         $sth_search->execute();
193         my $res = $sth_search->fetchrow_hashref();
194         if ($res->{total}) {
195             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
196         } else {
197             $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
198         }
199     } else {
200         my $sth_search = $dbh->prepare("SELECT count(*) AS total
201                                         FROM default_branch_circ_rules
202                                         WHERE branchcode = ?");
203         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
204                                         (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
205                                         VALUES (?, ?, ?, ?, ?)");
206         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
207                                         SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
208                                         WHERE branchcode = ?");
209         $sth_search->execute($branch);
210         my $res = $sth_search->fetchrow_hashref();
211         if ($res->{total}) {
212             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
213         } else {
214             $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
215         }
216     }
217 }
218 elsif ($op eq "add-branch-cat") {
219     my $categorycode  = $input->param('categorycode');
220     my $maxissueqty   = $input->param('maxissueqty');
221     my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
222     $maxissueqty =~ s/\s//g;
223     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
224     $maxonsiteissueqty =~ s/\s//g;
225     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
226
227     if ($branch eq "*") {
228         if ($categorycode eq "*") {
229             my $sth_search = $dbh->prepare("SELECT count(*) AS total
230                                             FROM default_circ_rules");
231             my $sth_insert = $dbh->prepare(q|
232                 INSERT INTO default_circ_rules
233                     (maxissueqty, maxonsiteissueqty)
234                     VALUES (?, ?)
235             |);
236             my $sth_update = $dbh->prepare(q|
237                 UPDATE default_circ_rules
238                 SET maxissueqty = ?,
239                     maxonsiteissueqty = ?
240             |);
241
242             $sth_search->execute();
243             my $res = $sth_search->fetchrow_hashref();
244             if ($res->{total}) {
245                 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
246             } else {
247                 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
248             }
249         } else {
250             my $sth_search = $dbh->prepare("SELECT count(*) AS total
251                                             FROM default_borrower_circ_rules
252                                             WHERE categorycode = ?");
253             my $sth_insert = $dbh->prepare(q|
254                 INSERT INTO default_borrower_circ_rules
255                     (categorycode, maxissueqty, maxonsiteissueqty)
256                     VALUES (?, ?, ?)
257             |);
258             my $sth_update = $dbh->prepare(q|
259                 UPDATE default_borrower_circ_rules
260                 SET maxissueqty = ?,
261                     maxonsiteissueqty = ?
262                 WHERE categorycode = ?
263             |);
264             $sth_search->execute($branch);
265             my $res = $sth_search->fetchrow_hashref();
266             if ($res->{total}) {
267                 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
268             } else {
269                 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
270             }
271         }
272     } elsif ($categorycode eq "*") {
273         my $sth_search = $dbh->prepare("SELECT count(*) AS total
274                                         FROM default_branch_circ_rules
275                                         WHERE branchcode = ?");
276         my $sth_insert = $dbh->prepare(q|
277             INSERT INTO default_branch_circ_rules
278             (branchcode, maxissueqty, maxonsiteissueqty)
279             VALUES (?, ?, ?)
280         |);
281         my $sth_update = $dbh->prepare(q|
282             UPDATE default_branch_circ_rules
283             SET maxissueqty = ?,
284                 maxonsiteissueqty = ?
285             WHERE branchcode = ?
286         |);
287         $sth_search->execute($branch);
288         my $res = $sth_search->fetchrow_hashref();
289         if ($res->{total}) {
290             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
291         } else {
292             $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
293         }
294     } else {
295         my $sth_search = $dbh->prepare("SELECT count(*) AS total
296                                         FROM branch_borrower_circ_rules
297                                         WHERE branchcode = ?
298                                         AND   categorycode = ?");
299         my $sth_insert = $dbh->prepare(q|
300             INSERT INTO branch_borrower_circ_rules
301             (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
302             VALUES (?, ?, ?, ?)
303         |);
304         my $sth_update = $dbh->prepare(q|
305             UPDATE branch_borrower_circ_rules
306             SET maxissueqty = ?,
307                 maxonsiteissueqty = ?
308             WHERE branchcode = ?
309             AND categorycode = ?
310         |);
311
312         $sth_search->execute($branch, $categorycode);
313         my $res = $sth_search->fetchrow_hashref();
314         if ($res->{total}) {
315             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
316         } else {
317             $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
318         }
319     }
320 }
321 elsif ($op eq "add-branch-item") {
322     my $itemtype  = $input->param('itemtype');
323     my $holdallowed   = $input->param('holdallowed');
324     my $returnbranch  = $input->param('returnbranch');
325     $holdallowed =~ s/\s//g;
326     $holdallowed = undef if $holdallowed !~ /^\d+/;
327
328     if ($branch eq "*") {
329         if ($itemtype eq "*") {
330             my $sth_search = $dbh->prepare("SELECT count(*) AS total
331                                             FROM default_circ_rules");
332             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
333                                             (holdallowed, returnbranch)
334                                             VALUES (?, ?)");
335             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
336                                             SET holdallowed = ?, returnbranch = ?");
337
338             $sth_search->execute();
339             my $res = $sth_search->fetchrow_hashref();
340             if ($res->{total}) {
341                 $sth_update->execute($holdallowed, $returnbranch);
342             } else {
343                 $sth_insert->execute($holdallowed, $returnbranch);
344             }
345         } else {
346             my $sth_search = $dbh->prepare("SELECT count(*) AS total
347                                             FROM default_branch_item_rules
348                                             WHERE itemtype = ?");
349             my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
350                                             (itemtype, holdallowed, returnbranch)
351                                             VALUES (?, ?, ?)");
352             my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
353                                             SET holdallowed = ?, returnbranch = ?
354                                             WHERE itemtype = ?");
355             $sth_search->execute($itemtype);
356             my $res = $sth_search->fetchrow_hashref();
357             if ($res->{total}) {
358                 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
359             } else {
360                 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
361             }
362         }
363     } elsif ($itemtype eq "*") {
364         my $sth_search = $dbh->prepare("SELECT count(*) AS total
365                                         FROM default_branch_circ_rules
366                                         WHERE branchcode = ?");
367         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
368                                         (branchcode, holdallowed, returnbranch)
369                                         VALUES (?, ?, ?)");
370         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
371                                         SET holdallowed = ?, returnbranch = ?
372                                         WHERE branchcode = ?");
373         $sth_search->execute($branch);
374         my $res = $sth_search->fetchrow_hashref();
375         if ($res->{total}) {
376             $sth_update->execute($holdallowed, $returnbranch, $branch);
377         } else {
378             $sth_insert->execute($branch, $holdallowed, $returnbranch);
379         }
380     } else {
381         my $sth_search = $dbh->prepare("SELECT count(*) AS total
382                                         FROM branch_item_rules
383                                         WHERE branchcode = ?
384                                         AND   itemtype = ?");
385         my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
386                                         (branchcode, itemtype, holdallowed, returnbranch)
387                                         VALUES (?, ?, ?, ?)");
388         my $sth_update = $dbh->prepare("UPDATE branch_item_rules
389                                         SET holdallowed = ?, returnbranch = ?
390                                         WHERE branchcode = ?
391                                         AND itemtype = ?");
392
393         $sth_search->execute($branch, $itemtype);
394         my $res = $sth_search->fetchrow_hashref();
395         if ($res->{total}) {
396             $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
397         } else {
398             $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
399         }
400     }
401 }
402
403 my $branches = GetBranches();
404 my @branchloop;
405 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
406     push @branchloop, {
407         value      => $thisbranch,
408         selected   => $thisbranch eq $branch,
409         branchname => $branches->{$thisbranch}->{'branchname'},
410     };
411 }
412
413 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
414 $sth->execute;
415 my @category_loop;
416 while (my $data=$sth->fetchrow_hashref){
417     push @category_loop,$data;
418 }
419
420 $sth->finish;
421 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
422 $sth->execute;
423 # $i=0;
424 my @row_loop;
425 my @itemtypes;
426 while (my $row=$sth->fetchrow_hashref){
427     push @itemtypes,$row;
428 }
429
430 my $sth2 = $dbh->prepare("
431     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
432     FROM issuingrules
433     LEFT JOIN itemtypes
434         ON (itemtypes.itemtype = issuingrules.itemtype)
435     LEFT JOIN categories
436         ON (categories.categorycode = issuingrules.categorycode)
437     WHERE issuingrules.branchcode = ?
438 ");
439 $sth2->execute($branch);
440
441 while (my $row = $sth2->fetchrow_hashref) {
442     $row->{'current_branch'} ||= $row->{'branchcode'};
443     $row->{'humanitemtype'} ||= $row->{'itemtype'};
444     $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
445     $row->{'humancategorycode'} ||= $row->{'categorycode'};
446     $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
447     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
448     if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
449        $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
450        $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
451        $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} ==  0);
452        $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} ==  1);
453     } else {
454        $row->{'hardduedate'} = 0;
455     }
456     push @row_loop, $row;
457 }
458 $sth->finish;
459
460 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
461
462 my $sth_branch_cat;
463 if ($branch eq "*") {
464     $sth_branch_cat = $dbh->prepare("
465         SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
466         FROM default_borrower_circ_rules
467         JOIN categories USING (categorycode)
468
469     ");
470     $sth_branch_cat->execute();
471 } else {
472     $sth_branch_cat = $dbh->prepare("
473         SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
474         FROM branch_borrower_circ_rules
475         JOIN categories USING (categorycode)
476         WHERE branch_borrower_circ_rules.branchcode = ?
477     ");
478     $sth_branch_cat->execute($branch);
479 }
480
481 my @branch_cat_rules = ();
482 while (my $row = $sth_branch_cat->fetchrow_hashref) {
483     push @branch_cat_rules, $row;
484 }
485 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
486
487 # note undef maxissueqty so that template can deal with them
488 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
489     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
490     $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
491 }
492
493 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
494
495 my $sth_branch_item;
496 if ($branch eq "*") {
497     $sth_branch_item = $dbh->prepare("
498         SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
499         FROM default_branch_item_rules
500         JOIN itemtypes USING (itemtype)
501     ");
502     $sth_branch_item->execute();
503 } else {
504     $sth_branch_item = $dbh->prepare("
505         SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
506         FROM branch_item_rules
507         JOIN itemtypes USING (itemtype)
508         WHERE branch_item_rules.branchcode = ?
509     ");
510     $sth_branch_item->execute($branch);
511 }
512
513 my @branch_item_rules = ();
514 while (my $row = $sth_branch_item->fetchrow_hashref) {
515     push @branch_item_rules, $row;
516 }
517 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
518
519 # note undef holdallowed so that template can deal with them
520 foreach my $entry (@sorted_branch_item_rules) {
521     $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
522     $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
523 }
524
525 $template->param(show_branch_cat_rule_form => 1);
526 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
527 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
528
529 my $sth_defaults;
530 if ($branch eq "*") {
531     $sth_defaults = $dbh->prepare("
532         SELECT *
533         FROM default_circ_rules
534     ");
535     $sth_defaults->execute();
536 } else {
537     $sth_defaults = $dbh->prepare("
538         SELECT *
539         FROM default_branch_circ_rules
540         WHERE branchcode = ?
541     ");
542     $sth_defaults->execute($branch);
543 }
544
545 my $defaults = $sth_defaults->fetchrow_hashref;
546
547 if ($defaults) {
548     $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
549     $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
550     $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
551     $template->param(default_maxissueqty => $defaults->{maxissueqty});
552     $template->param(default_maxonsiteissueqty => $defaults->{maxonsiteissueqty});
553     $template->param(default_returnbranch => $defaults->{returnbranch});
554 }
555
556 $template->param(default_rules => ($defaults ? 1 : 0));
557
558 $template->param(categoryloop => \@category_loop,
559                         itemtypeloop => \@itemtypes,
560                         rules => \@sorted_row_loop,
561                         branchloop => \@branchloop,
562                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
563                         current_branch => $branch,
564                         definedbranch => scalar(@sorted_row_loop)>0
565                         );
566 output_html_with_http_headers $input, $cookie, $template->output;
567
568 exit 0;
569
570 # sort by patron category, then item type, putting
571 # default entries at the bottom
572 sub by_category_and_itemtype {
573     unless (by_category($a, $b)) {
574         return by_itemtype($a, $b);
575     }
576 }
577
578 sub by_category {
579     my ($a, $b) = @_;
580     if ($a->{'default_humancategorycode'}) {
581         return ($b->{'default_humancategorycode'} ? 0 : 1);
582     } elsif ($b->{'default_humancategorycode'}) {
583         return -1;
584     } else {
585         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
586     }
587 }
588
589 sub by_itemtype {
590     my ($a, $b) = @_;
591     if ($a->{'default_humanitemtype'}) {
592         return ($b->{'default_humanitemtype'} ? 0 : 1);
593     } elsif ($b->{'default_humanitemtype'}) {
594         return -1;
595     } else {
596         return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};
597     }
598 }