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