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