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