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