Bug 14045: Introduce the maxonsiteissueqty field as a copy of maxissueqty
[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 $maxissueqty  = $input->param('maxissueqty');
114     my $maxonsiteissueqty  = $input->param('maxonsiteissueqty');
115     my $renewalsallowed  = $input->param('renewalsallowed');
116     my $renewalperiod    = $input->param('renewalperiod');
117     my $norenewalbefore  = $input->param('norenewalbefore');
118     $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
119     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
120     my $reservesallowed  = $input->param('reservesallowed');
121     my $onshelfholds     = $input->param('onshelfholds') || 0;
122     $maxissueqty =~ s/\s//g;
123     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
124     $maxonsiteissueqty =~ s/\s//g;
125     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
126     my $issuelength  = $input->param('issuelength');
127     my $lengthunit  = $input->param('lengthunit');
128     my $hardduedate = $input->param('hardduedate');
129     $hardduedate = format_date_in_iso($hardduedate);
130     my $hardduedatecompare = $input->param('hardduedatecompare');
131     my $rentaldiscount = $input->param('rentaldiscount');
132     my $opacitemholds = $input->param('opacitemholds') || 0;
133     my $overduefinescap = $input->param('overduefinescap') || undef;
134     $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
135
136     my $schema = Koha::Database->new()->schema();
137     my $rs = $schema->resultset('Issuingrule');
138
139     my $params = {
140         branchcode         => $br,
141         categorycode       => $bor,
142         itemtype           => $itemtype,
143         fine               => $fine,
144         finedays           => $finedays,
145         maxsuspensiondays  => $maxsuspensiondays,
146         firstremind        => $firstremind,
147         chargeperiod       => $chargeperiod,
148         maxissueqty        => $maxissueqty,
149         maxonsiteissueqty  => $maxonsiteissueqty,
150         renewalsallowed    => $renewalsallowed,
151         renewalperiod      => $renewalperiod,
152         norenewalbefore    => $norenewalbefore,
153         auto_renew         => $auto_renew,
154         reservesallowed    => $reservesallowed,
155         issuelength        => $issuelength,
156         lengthunit         => $lengthunit,
157         hardduedate        => $hardduedate,
158         hardduedatecompare => $hardduedatecompare,
159         rentaldiscount     => $rentaldiscount,
160         onshelfholds       => $onshelfholds,
161         opacitemholds      => $opacitemholds,
162         overduefinescap    => $overduefinescap,
163     };
164
165     $rs->update_or_create($params);
166
167 }
168 elsif ($op eq "set-branch-defaults") {
169     my $categorycode  = $input->param('categorycode');
170     my $maxissueqty   = $input->param('maxissueqty');
171     my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
172     my $holdallowed   = $input->param('holdallowed');
173     my $returnbranch  = $input->param('returnbranch');
174     $maxissueqty =~ s/\s//g;
175     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
176     $maxonsiteissueqty =~ s/\s//g;
177     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
178     $holdallowed =~ s/\s//g;
179     $holdallowed = undef if $holdallowed !~ /^\d+/;
180
181     if ($branch eq "*") {
182         my $sth_search = $dbh->prepare("SELECT count(*) AS total
183                                         FROM default_circ_rules");
184         my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
185                                         (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
186                                         VALUES (?, ?, ?, ?)");
187         my $sth_update = $dbh->prepare("UPDATE default_circ_rules
188                                         SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
189
190         $sth_search->execute();
191         my $res = $sth_search->fetchrow_hashref();
192         if ($res->{total}) {
193             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
194         } else {
195             $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
196         }
197     } else {
198         my $sth_search = $dbh->prepare("SELECT count(*) AS total
199                                         FROM default_branch_circ_rules
200                                         WHERE branchcode = ?");
201         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
202                                         (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
203                                         VALUES (?, ?, ?, ?, ?)");
204         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
205                                         SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
206                                         WHERE branchcode = ?");
207         $sth_search->execute($branch);
208         my $res = $sth_search->fetchrow_hashref();
209         if ($res->{total}) {
210             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
211         } else {
212             $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
213         }
214     }
215 }
216 elsif ($op eq "add-branch-cat") {
217     my $categorycode  = $input->param('categorycode');
218     my $maxissueqty   = $input->param('maxissueqty');
219     my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
220     $maxissueqty =~ s/\s//g;
221     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
222     $maxonsiteissueqty =~ s/\s//g;
223     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
224
225     if ($branch eq "*") {
226         if ($categorycode eq "*") {
227             my $sth_search = $dbh->prepare("SELECT count(*) AS total
228                                             FROM default_circ_rules");
229             my $sth_insert = $dbh->prepare(q|
230                 INSERT INTO default_circ_rules
231                     (maxissueqty, maxonsiteissueqty)
232                     VALUES (?, ?)
233             |);
234             my $sth_update = $dbh->prepare(q|
235                 UPDATE default_circ_rules
236                 SET maxissueqty = ?,
237                     maxonsiteissueqty = ?
238             |);
239
240             $sth_search->execute();
241             my $res = $sth_search->fetchrow_hashref();
242             if ($res->{total}) {
243                 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
244             } else {
245                 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
246             }
247         } else {
248             my $sth_search = $dbh->prepare("SELECT count(*) AS total
249                                             FROM default_borrower_circ_rules
250                                             WHERE categorycode = ?");
251             my $sth_insert = $dbh->prepare(q|
252                 INSERT INTO default_borrower_circ_rules
253                     (categorycode, maxissueqty, maxonsiteissueqty)
254                     VALUES (?, ?, ?)
255             |);
256             my $sth_update = $dbh->prepare(q|
257                 UPDATE default_borrower_circ_rules
258                 SET maxissueqty = ?,
259                     maxonsiteissueqty = ?
260                 WHERE categorycode = ?
261             |);
262             $sth_search->execute($branch);
263             my $res = $sth_search->fetchrow_hashref();
264             if ($res->{total}) {
265                 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
266             } else {
267                 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
268             }
269         }
270     } elsif ($categorycode eq "*") {
271         my $sth_search = $dbh->prepare("SELECT count(*) AS total
272                                         FROM default_branch_circ_rules
273                                         WHERE branchcode = ?");
274         my $sth_insert = $dbh->prepare(q|
275             INSERT INTO default_branch_circ_rules
276             (branchcode, maxissueqty, maxonsiteissueqty)
277             VALUES (?, ?, ?)
278         |);
279         my $sth_update = $dbh->prepare(q|
280             UPDATE default_branch_circ_rules
281             SET maxissueqty = ?,
282                 maxonsiteissueqty = ?
283             WHERE branchcode = ?
284         |);
285         $sth_search->execute($branch);
286         my $res = $sth_search->fetchrow_hashref();
287         if ($res->{total}) {
288             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
289         } else {
290             $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
291         }
292     } else {
293         my $sth_search = $dbh->prepare("SELECT count(*) AS total
294                                         FROM branch_borrower_circ_rules
295                                         WHERE branchcode = ?
296                                         AND   categorycode = ?");
297         my $sth_insert = $dbh->prepare(q|
298             INSERT INTO branch_borrower_circ_rules
299             (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
300             VALUES (?, ?, ?, ?)
301         |);
302         my $sth_update = $dbh->prepare(q|
303             UPDATE branch_borrower_circ_rules
304             SET maxissueqty = ?,
305                 maxonsiteissueqty = ?
306             WHERE branchcode = ?
307             AND categorycode = ?
308         |);
309
310         $sth_search->execute($branch, $categorycode);
311         my $res = $sth_search->fetchrow_hashref();
312         if ($res->{total}) {
313             $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
314         } else {
315             $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
316         }
317     }
318 }
319 elsif ($op eq "add-branch-item") {
320     my $itemtype  = $input->param('itemtype');
321     my $holdallowed   = $input->param('holdallowed');
322     my $returnbranch  = $input->param('returnbranch');
323     $holdallowed =~ s/\s//g;
324     $holdallowed = undef if $holdallowed !~ /^\d+/;
325
326     if ($branch eq "*") {
327         if ($itemtype eq "*") {
328             my $sth_search = $dbh->prepare("SELECT count(*) AS total
329                                             FROM default_circ_rules");
330             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
331                                             (holdallowed, returnbranch)
332                                             VALUES (?, ?)");
333             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
334                                             SET holdallowed = ?, returnbranch = ?");
335
336             $sth_search->execute();
337             my $res = $sth_search->fetchrow_hashref();
338             if ($res->{total}) {
339                 $sth_update->execute($holdallowed, $returnbranch);
340             } else {
341                 $sth_insert->execute($holdallowed, $returnbranch);
342             }
343         } else {
344             my $sth_search = $dbh->prepare("SELECT count(*) AS total
345                                             FROM default_branch_item_rules
346                                             WHERE itemtype = ?");
347             my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
348                                             (itemtype, holdallowed, returnbranch)
349                                             VALUES (?, ?, ?)");
350             my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
351                                             SET holdallowed = ?, returnbranch = ?
352                                             WHERE itemtype = ?");
353             $sth_search->execute($itemtype);
354             my $res = $sth_search->fetchrow_hashref();
355             if ($res->{total}) {
356                 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
357             } else {
358                 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
359             }
360         }
361     } elsif ($itemtype eq "*") {
362         my $sth_search = $dbh->prepare("SELECT count(*) AS total
363                                         FROM default_branch_circ_rules
364                                         WHERE branchcode = ?");
365         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
366                                         (branchcode, holdallowed, returnbranch)
367                                         VALUES (?, ?, ?)");
368         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
369                                         SET holdallowed = ?, returnbranch = ?
370                                         WHERE branchcode = ?");
371         $sth_search->execute($branch);
372         my $res = $sth_search->fetchrow_hashref();
373         if ($res->{total}) {
374             $sth_update->execute($holdallowed, $returnbranch, $branch);
375         } else {
376             $sth_insert->execute($branch, $holdallowed, $returnbranch);
377         }
378     } else {
379         my $sth_search = $dbh->prepare("SELECT count(*) AS total
380                                         FROM branch_item_rules
381                                         WHERE branchcode = ?
382                                         AND   itemtype = ?");
383         my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
384                                         (branchcode, itemtype, holdallowed, returnbranch)
385                                         VALUES (?, ?, ?, ?)");
386         my $sth_update = $dbh->prepare("UPDATE branch_item_rules
387                                         SET holdallowed = ?, returnbranch = ?
388                                         WHERE branchcode = ?
389                                         AND itemtype = ?");
390
391         $sth_search->execute($branch, $itemtype);
392         my $res = $sth_search->fetchrow_hashref();
393         if ($res->{total}) {
394             $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
395         } else {
396             $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
397         }
398     }
399 }
400
401 my $branches = GetBranches();
402 my @branchloop;
403 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
404     push @branchloop, {
405         value      => $thisbranch,
406         selected   => $thisbranch eq $branch,
407         branchname => $branches->{$thisbranch}->{'branchname'},
408     };
409 }
410
411 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
412 $sth->execute;
413 my @category_loop;
414 while (my $data=$sth->fetchrow_hashref){
415     push @category_loop,$data;
416 }
417
418 $sth->finish;
419 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
420 $sth->execute;
421 # $i=0;
422 my @row_loop;
423 my @itemtypes;
424 while (my $row=$sth->fetchrow_hashref){
425     push @itemtypes,$row;
426 }
427
428 my $sth2 = $dbh->prepare("
429     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
430     FROM issuingrules
431     LEFT JOIN itemtypes
432         ON (itemtypes.itemtype = issuingrules.itemtype)
433     LEFT JOIN categories
434         ON (categories.categorycode = issuingrules.categorycode)
435     WHERE issuingrules.branchcode = ?
436 ");
437 $sth2->execute($branch);
438
439 while (my $row = $sth2->fetchrow_hashref) {
440     $row->{'current_branch'} ||= $row->{'branchcode'};
441     $row->{'humanitemtype'} ||= $row->{'itemtype'};
442     $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
443     $row->{'humancategorycode'} ||= $row->{'categorycode'};
444     $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
445     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
446     if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
447        $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
448        $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
449        $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} ==  0);
450        $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} ==  1);
451     } else {
452        $row->{'hardduedate'} = 0;
453     }
454     push @row_loop, $row;
455 }
456 $sth->finish;
457
458 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
459
460 my $sth_branch_cat;
461 if ($branch eq "*") {
462     $sth_branch_cat = $dbh->prepare("
463         SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
464         FROM default_borrower_circ_rules
465         JOIN categories USING (categorycode)
466
467     ");
468     $sth_branch_cat->execute();
469 } else {
470     $sth_branch_cat = $dbh->prepare("
471         SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
472         FROM branch_borrower_circ_rules
473         JOIN categories USING (categorycode)
474         WHERE branch_borrower_circ_rules.branchcode = ?
475     ");
476     $sth_branch_cat->execute($branch);
477 }
478
479 my @branch_cat_rules = ();
480 while (my $row = $sth_branch_cat->fetchrow_hashref) {
481     push @branch_cat_rules, $row;
482 }
483 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
484
485 # note undef maxissueqty so that template can deal with them
486 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
487     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
488     $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
489 }
490
491 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
492
493 my $sth_branch_item;
494 if ($branch eq "*") {
495     $sth_branch_item = $dbh->prepare("
496         SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
497         FROM default_branch_item_rules
498         JOIN itemtypes USING (itemtype)
499     ");
500     $sth_branch_item->execute();
501 } else {
502     $sth_branch_item = $dbh->prepare("
503         SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
504         FROM branch_item_rules
505         JOIN itemtypes USING (itemtype)
506         WHERE branch_item_rules.branchcode = ?
507     ");
508     $sth_branch_item->execute($branch);
509 }
510
511 my @branch_item_rules = ();
512 while (my $row = $sth_branch_item->fetchrow_hashref) {
513     push @branch_item_rules, $row;
514 }
515 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
516
517 # note undef holdallowed so that template can deal with them
518 foreach my $entry (@sorted_branch_item_rules) {
519     $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
520     $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
521 }
522
523 $template->param(show_branch_cat_rule_form => 1);
524 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
525 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
526
527 my $sth_defaults;
528 if ($branch eq "*") {
529     $sth_defaults = $dbh->prepare("
530         SELECT *
531         FROM default_circ_rules
532     ");
533     $sth_defaults->execute();
534 } else {
535     $sth_defaults = $dbh->prepare("
536         SELECT *
537         FROM default_branch_circ_rules
538         WHERE branchcode = ?
539     ");
540     $sth_defaults->execute($branch);
541 }
542
543 my $defaults = $sth_defaults->fetchrow_hashref;
544
545 if ($defaults) {
546     $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
547     $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
548     $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
549     $template->param(default_maxissueqty => $defaults->{maxissueqty});
550     $template->param(default_maxonsiteissueqty => $defaults->{maxonsiteissueqty});
551     $template->param(default_returnbranch => $defaults->{returnbranch});
552 }
553
554 $template->param(default_rules => ($defaults ? 1 : 0));
555
556 $template->param(categoryloop => \@category_loop,
557                         itemtypeloop => \@itemtypes,
558                         rules => \@sorted_row_loop,
559                         branchloop => \@branchloop,
560                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
561                         current_branch => $branch,
562                         definedbranch => scalar(@sorted_row_loop)>0
563                         );
564 output_html_with_http_headers $input, $cookie, $template->output;
565
566 exit 0;
567
568 # sort by patron category, then item type, putting
569 # default entries at the bottom
570 sub by_category_and_itemtype {
571     unless (by_category($a, $b)) {
572         return by_itemtype($a, $b);
573     }
574 }
575
576 sub by_category {
577     my ($a, $b) = @_;
578     if ($a->{'default_humancategorycode'}) {
579         return ($b->{'default_humancategorycode'} ? 0 : 1);
580     } elsif ($b->{'default_humancategorycode'}) {
581         return -1;
582     } else {
583         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
584     }
585 }
586
587 sub by_itemtype {
588     my ($a, $b) = @_;
589     if ($a->{'default_humanitemtype'}) {
590         return ($b->{'default_humanitemtype'} ? 0 : 1);
591     } elsif ($b->{'default_humanitemtype'}) {
592         return -1;
593     } else {
594         return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};
595     }
596 }