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