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