Bug 30007: Regression tests
[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 Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output qw( output_html_with_http_headers );
24 use C4::Auth qw( get_template_and_user );
25 use Koha::Exception;
26 use Koha::DateUtils qw( dt_from_string output_pref );
27 use Koha::Database;
28 use Koha::Logger;
29 use Koha::Libraries;
30 use Koha::CirculationRules;
31 use Koha::Patron::Categories;
32 use Koha::Caches;
33 use Koha::Patrons;
34
35 my $input = CGI->new;
36 my $dbh = C4::Context->dbh;
37
38 # my $flagsrequired;
39 # $flagsrequired->{circulation}=1;
40 my ($template, $loggedinuser, $cookie)
41     = get_template_and_user({template_name => "admin/smart-rules.tt",
42                             query => $input,
43                             type => "intranet",
44                             flagsrequired => {parameters => 'manage_circ_rules'},
45                             });
46
47 my $type=$input->param('type');
48
49 my $branch = $input->param('branch');
50 unless ( $branch ) {
51     if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
52         $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
53     }
54     else {
55         $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
56     }
57 }
58
59 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
60
61 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
62 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
63 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
64
65 my $op = $input->param('op') || q{};
66 my $language = C4::Languages::getlanguage();
67
68 my $cache = Koha::Caches->get_instance;
69 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
70
71 if ($op eq 'delete') {
72     my $itemtype     = $input->param('itemtype');
73     my $categorycode = $input->param('categorycode');
74
75     Koha::CirculationRules->set_rules(
76         {
77             categorycode => $categorycode eq '*' ? undef : $categorycode,
78             branchcode   => $branch eq '*' ? undef : $branch,
79             itemtype     => $itemtype eq '*' ? undef : $itemtype,
80             rules        => {
81                 maxissueqty                      => undef,
82                 maxonsiteissueqty                => undef,
83                 rentaldiscount                   => undef,
84                 fine                             => undef,
85                 finedays                         => undef,
86                 maxsuspensiondays                => undef,
87                 suspension_chargeperiod          => undef,
88                 firstremind                      => undef,
89                 chargeperiod                     => undef,
90                 chargeperiod_charge_at           => undef,
91                 issuelength                      => undef,
92                 daysmode                         => undef,
93                 lengthunit                       => undef,
94                 hardduedate                      => undef,
95                 hardduedatecompare               => undef,
96                 renewalsallowed                  => undef,
97                 unseen_renewals_allowed          => undef,
98                 renewalperiod                    => undef,
99                 norenewalbefore                  => undef,
100                 auto_renew                       => undef,
101                 no_auto_renewal_after            => undef,
102                 no_auto_renewal_after_hard_limit => undef,
103                 reservesallowed                  => undef,
104                 holds_per_record                 => undef,
105                 holds_per_day                    => undef,
106                 onshelfholds                     => undef,
107                 opacitemholds                    => undef,
108                 overduefinescap                  => undef,
109                 cap_fine_to_replacement_price    => undef,
110                 article_requests                 => undef,
111                 note                             => undef,
112             }
113         }
114     );
115 }
116 elsif ($op eq 'delete-branch-cat') {
117     my $categorycode  = $input->param('categorycode');
118     if ($branch eq "*") {
119         if ($categorycode eq "*") {
120             Koha::CirculationRules->set_rules(
121                 {
122                     branchcode   => undef,
123                     categorycode => undef,
124                     rules        => {
125                         max_holds                      => undef,
126                         patron_maxissueqty             => undef,
127                         patron_maxonsiteissueqty       => undef,
128                     }
129                 }
130             );
131             Koha::CirculationRules->set_rules(
132                 {
133                     branchcode   => undef,
134                     itemtype     => undef,
135                     rules        => {
136                         holdallowed             => undef,
137                         hold_fulfillment_policy => undef,
138                         returnbranch            => undef,
139                     }
140                 }
141             );
142         } else {
143             Koha::CirculationRules->set_rules(
144                 {
145                     categorycode => $categorycode,
146                     branchcode   => undef,
147                     rules        => {
148                         max_holds                => undef,
149                         patron_maxissueqty       => undef,
150                         patron_maxonsiteissueqty => undef,
151                     }
152                 }
153             );
154         }
155     } elsif ($categorycode eq "*") {
156         Koha::CirculationRules->set_rules(
157             {
158                 branchcode   => $branch,
159                 categorycode => undef,
160                 rules        => {
161                     max_holds                => undef,
162                     patron_maxissueqty       => undef,
163                     patron_maxonsiteissueqty => undef,
164                 }
165             }
166         );
167         Koha::CirculationRules->set_rules(
168             {
169                 branchcode   => $branch,
170                 itemtype     => undef,
171                 rules        => {
172                     holdallowed             => undef,
173                     hold_fulfillment_policy => undef,
174                     returnbranch            => undef,
175                 }
176             }
177         );
178     } else {
179         Koha::CirculationRules->set_rules(
180             {
181                 categorycode => $categorycode,
182                 branchcode   => $branch,
183                 rules        => {
184                     max_holds         => undef,
185                     patron_maxissueqty       => undef,
186                     patron_maxonsiteissueqty => undef,
187                 }
188             }
189         );
190     }
191 }
192 elsif ($op eq 'delete-branch-item') {
193     my $itemtype  = $input->param('itemtype');
194     if ($branch eq "*") {
195         if ($itemtype eq "*") {
196             Koha::CirculationRules->set_rules(
197                 {
198                     branchcode   => undef,
199                     itemtype     => undef,
200                     rules        => {
201                         holdallowed             => undef,
202                         hold_fulfillment_policy => undef,
203                         returnbranch            => undef,
204                     }
205                 }
206             );
207         } else {
208             Koha::CirculationRules->set_rules(
209                 {
210                     branchcode   => undef,
211                     itemtype     => $itemtype,
212                     rules        => {
213                         holdallowed             => undef,
214                         hold_fulfillment_policy => undef,
215                         returnbranch            => undef,
216                     }
217                 }
218             );
219         }
220     } elsif ($itemtype eq "*") {
221         Koha::CirculationRules->set_rules(
222             {
223                 branchcode   => $branch,
224                 itemtype     => undef,
225                 rules        => {
226                     holdallowed             => undef,
227                     hold_fulfillment_policy => undef,
228                     returnbranch            => undef,
229                 }
230             }
231         );
232     } else {
233         Koha::CirculationRules->set_rules(
234             {
235                 branchcode   => $branch,
236                 itemtype     => $itemtype,
237                 rules        => {
238                     holdallowed             => undef,
239                     hold_fulfillment_policy => undef,
240                     returnbranch            => undef,
241                 }
242             }
243         );
244     }
245 }
246 # save the values entered
247 elsif ($op eq 'add') {
248     my $br = $branch; # branch
249     my $bor  = $input->param('categorycode'); # borrower category
250     my $itemtype  = $input->param('itemtype');     # item type
251     my $fine = $input->param('fine');
252     my $finedays     = $input->param('finedays');
253     my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
254     my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
255     my $firstremind  = $input->param('firstremind');
256     my $chargeperiod = $input->param('chargeperiod');
257     my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
258     my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
259     my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
260     my $renewalsallowed  = $input->param('renewalsallowed');
261     my $unseen_renewals_allowed  = $input->param('unseen_renewals_allowed');
262     my $renewalperiod    = $input->param('renewalperiod');
263     my $norenewalbefore  = $input->param('norenewalbefore');
264     $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
265     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
266     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
267     $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
268     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
269     $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
270     $no_auto_renewal_after_hard_limit = output_pref( { dt => $no_auto_renewal_after_hard_limit, dateonly => 1, dateformat => 'iso' } ) if ( $no_auto_renewal_after_hard_limit );
271     my $reservesallowed  = strip_non_numeric( scalar $input->param('reservesallowed') );
272     my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
273     my $holds_per_day    = strip_non_numeric( scalar $input->param('holds_per_day') );
274     my $onshelfholds     = $input->param('onshelfholds') || 0;
275     my $issuelength  = $input->param('issuelength');
276     $issuelength = $issuelength eq q{} ? undef : $issuelength;
277     my $daysmode = $input->param('daysmode');
278     my $lengthunit  = $input->param('lengthunit');
279     my $hardduedate = $input->param('hardduedate') || undef;
280     $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
281     $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
282     my $hardduedatecompare = $input->param('hardduedatecompare');
283     my $rentaldiscount = $input->param('rentaldiscount');
284     my $opacitemholds = $input->param('opacitemholds') || 0;
285     my $article_requests = $input->param('article_requests') || 'no';
286     my $overduefinescap = $input->param('overduefinescap') || '';
287     my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
288     my $note = $input->param('note');
289     my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
290
291     my $rules = {
292         maxissueqty                   => $maxissueqty,
293         maxonsiteissueqty             => $maxonsiteissueqty,
294         rentaldiscount                => $rentaldiscount,
295         fine                          => $fine,
296         finedays                      => $finedays,
297         maxsuspensiondays             => $maxsuspensiondays,
298         suspension_chargeperiod       => $suspension_chargeperiod,
299         firstremind                   => $firstremind,
300         chargeperiod                  => $chargeperiod,
301         chargeperiod_charge_at        => $chargeperiod_charge_at,
302         issuelength                   => $issuelength,
303         daysmode                      => $daysmode,
304         lengthunit                    => $lengthunit,
305         hardduedate                   => $hardduedate,
306         hardduedatecompare            => $hardduedatecompare,
307         renewalsallowed               => $renewalsallowed,
308         unseen_renewals_allowed       => $unseen_renewals_allowed,
309         renewalperiod                 => $renewalperiod,
310         norenewalbefore               => $norenewalbefore,
311         auto_renew                    => $auto_renew,
312         no_auto_renewal_after         => $no_auto_renewal_after,
313         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
314         reservesallowed               => $reservesallowed,
315         holds_per_record              => $holds_per_record,
316         holds_per_day                 => $holds_per_day,
317         onshelfholds                  => $onshelfholds,
318         opacitemholds                 => $opacitemholds,
319         overduefinescap               => $overduefinescap,
320         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
321         article_requests              => $article_requests,
322         note                          => $note,
323         decreaseloanholds             => $decreaseloanholds,
324     };
325
326     Koha::CirculationRules->set_rules(
327         {
328             categorycode => $bor eq '*' ? undef : $bor,
329             itemtype     => $itemtype eq '*' ? undef : $itemtype,
330             branchcode   => $br eq '*' ? undef : $br,
331             rules        => $rules,
332         }
333     );
334
335 }
336 elsif ($op eq "set-branch-defaults") {
337     my $categorycode  = $input->param('categorycode');
338     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
339     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
340     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
341     my $holdallowed   = $input->param('holdallowed');
342     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
343     my $returnbranch  = $input->param('returnbranch');
344     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
345
346     if ($branch eq "*") {
347         Koha::CirculationRules->set_rules(
348             {
349                 itemtype     => undef,
350                 branchcode   => undef,
351                 rules        => {
352                     holdallowed             => $holdallowed,
353                     hold_fulfillment_policy => $hold_fulfillment_policy,
354                     returnbranch            => $returnbranch,
355                 }
356             }
357         );
358         Koha::CirculationRules->set_rules(
359             {
360                 categorycode => undef,
361                 branchcode   => undef,
362                 rules        => {
363                     patron_maxissueqty             => $patron_maxissueqty,
364                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
365                 }
366             }
367         );
368     } else {
369         Koha::CirculationRules->set_rules(
370             {
371                 itemtype     => undef,
372                 branchcode   => $branch,
373                 rules        => {
374                     holdallowed             => $holdallowed,
375                     hold_fulfillment_policy => $hold_fulfillment_policy,
376                     returnbranch            => $returnbranch,
377                 }
378             }
379         );
380         Koha::CirculationRules->set_rules(
381             {
382                 categorycode => undef,
383                 branchcode   => $branch,
384                 rules        => {
385                     patron_maxissueqty             => $patron_maxissueqty,
386                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
387                 }
388             }
389         );
390     }
391     Koha::CirculationRules->set_rule(
392         {
393             branchcode   => $branch,
394             categorycode => undef,
395             rule_name    => 'max_holds',
396             rule_value   => $max_holds,
397         }
398     );
399 }
400 elsif ($op eq "add-branch-cat") {
401     my $categorycode  = $input->param('categorycode');
402     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
403     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
404     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
405     my $max_holds = $input->param('max_holds');
406     $max_holds =~ s/\s//g;
407     $max_holds = undef if $max_holds !~ /^\d+/;
408
409     if ($branch eq "*") {
410         if ($categorycode eq "*") {
411             Koha::CirculationRules->set_rules(
412                 {
413                     categorycode => undef,
414                     branchcode   => undef,
415                     rules        => {
416                         max_holds         => $max_holds,
417                         patron_maxissueqty       => $patron_maxissueqty,
418                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
419                     }
420                 }
421             );
422         } else {
423             Koha::CirculationRules->set_rules(
424                 {
425                     categorycode => $categorycode,
426                     branchcode   => undef,
427                     rules        => {
428                         max_holds         => $max_holds,
429                         patron_maxissueqty       => $patron_maxissueqty,
430                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
431                     }
432                 }
433             );
434         }
435     } elsif ($categorycode eq "*") {
436         Koha::CirculationRules->set_rules(
437             {
438                 categorycode => undef,
439                 branchcode   => $branch,
440                 rules        => {
441                     max_holds         => $max_holds,
442                     patron_maxissueqty       => $patron_maxissueqty,
443                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
444                 }
445             }
446         );
447     } else {
448         Koha::CirculationRules->set_rules(
449             {
450                 categorycode => $categorycode,
451                 branchcode   => $branch,
452                 rules        => {
453                     max_holds         => $max_holds,
454                     patron_maxissueqty       => $patron_maxissueqty,
455                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
456                 }
457             }
458         );
459     }
460 }
461 elsif ( $op eq "add-open-article-requests-limit" ) {
462     my $categorycode                = $input->param('categorycode');
463     my $open_article_requests_limit = strip_non_numeric( scalar $input->param('open_article_requests_limit') );
464
465     Koha::Exception->throw("No value passed for article request limit")
466       if not defined $open_article_requests_limit # There is a JS check for that
467       || $open_article_requests_limit eq '';
468
469     if ( $branch eq "*" ) {
470         if ( $categorycode eq "*" ) {
471             Koha::CirculationRules->set_rules(
472                 {   categorycode => undef,
473                     branchcode   => undef,
474                     rules        => { open_article_requests_limit => $open_article_requests_limit, }
475                 }
476             );
477         } else {
478             Koha::CirculationRules->set_rules(
479                 {   categorycode => $categorycode,
480                     branchcode   => undef,
481                     rules        => { open_article_requests_limit => $open_article_requests_limit, }
482                 }
483             );
484         }
485     } elsif ( $categorycode eq "*" ) {
486         Koha::CirculationRules->set_rules(
487             {   categorycode => undef,
488                 branchcode   => $branch,
489                 rules        => { open_article_requests_limit => $open_article_requests_limit, }
490             }
491         );
492     } else {
493         Koha::CirculationRules->set_rules(
494             {   categorycode => $categorycode,
495                 branchcode   => $branch,
496                 rules        => { open_article_requests_limit => $open_article_requests_limit, }
497             }
498         );
499     }
500 } elsif ( $op eq 'del-open-article-requests-limit' ) {
501     my $categorycode = $input->param('categorycode');
502     if ( $branch eq "*" ) {
503         if ( $categorycode eq "*" ) {
504             Koha::CirculationRules->set_rules(
505                 {   branchcode   => undef,
506                     categorycode => undef,
507                     rules        => { open_article_requests_limit => undef, }
508                 }
509             );
510         } else {
511             Koha::CirculationRules->set_rules(
512                 {   categorycode => $categorycode,
513                     branchcode   => undef,
514                     rules        => { open_article_requests_limit => undef, }
515                 }
516             );
517         }
518     } elsif ( $categorycode eq "*" ) {
519         Koha::CirculationRules->set_rules(
520             {   branchcode   => $branch,
521                 categorycode => undef,
522                 rules        => { open_article_requests_limit => undef, }
523             }
524         );
525     } else {
526         Koha::CirculationRules->set_rules(
527             {   categorycode => $categorycode,
528                 branchcode   => $branch,
529                 rules        => { open_article_requests_limit => undef, }
530             }
531         );
532     }
533 }
534 elsif ( $op eq "set-article-request-fee" ) {
535
536     my $category = $input->param('article_request_fee_category');
537     my $fee      = strip_non_numeric( scalar $input->param('article_request_fee') );
538
539     Koha::Exception->throw("No value passed for article request fee")
540       if not defined $fee # There is a JS check for that
541       || $fee eq '';
542
543     Koha::CirculationRules->set_rules(
544         {   categorycode => ( $category  eq '*' ) ? undef : $category,
545             branchcode   => ( $branch    eq '*' ) ? undef : $branch,
546             rules        => { article_request_fee => $fee },
547         }
548     );
549
550 } elsif ( $op eq 'del-article-request-fee' ) {
551
552     my $category  = $input->param('article_request_fee_category');
553
554     Koha::CirculationRules->set_rules(
555         {   categorycode => ( $category eq  '*' ) ? undef : $category,
556             branchcode   => ( $branch eq    '*' ) ? undef : $branch,
557             rules        => { article_request_fee => undef },
558         }
559     );
560 }
561 elsif ($op eq "add-branch-item") {
562     my $itemtype                = $input->param('itemtype');
563     my $holdallowed             = $input->param('holdallowed');
564     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
565     my $returnbranch            = $input->param('returnbranch');
566
567     if ($branch eq "*") {
568         if ($itemtype eq "*") {
569             Koha::CirculationRules->set_rules(
570                 {
571                     itemtype     => undef,
572                     branchcode   => undef,
573                     rules        => {
574                         holdallowed             => $holdallowed,
575                         hold_fulfillment_policy => $hold_fulfillment_policy,
576                         returnbranch            => $returnbranch,
577                     }
578                 }
579             );
580         } else {
581             Koha::CirculationRules->set_rules(
582                 {
583                     itemtype     => $itemtype,
584                     branchcode   => undef,
585                     rules        => {
586                         holdallowed             => $holdallowed,
587                         hold_fulfillment_policy => $hold_fulfillment_policy,
588                         returnbranch            => $returnbranch,
589                     }
590                 }
591             );
592         }
593     } elsif ($itemtype eq "*") {
594             Koha::CirculationRules->set_rules(
595                 {
596                     itemtype     => undef,
597                     branchcode   => $branch,
598                     rules        => {
599                         holdallowed             => $holdallowed,
600                         hold_fulfillment_policy => $hold_fulfillment_policy,
601                         returnbranch            => $returnbranch,
602                     }
603                 }
604             );
605     } else {
606         Koha::CirculationRules->set_rules(
607             {
608                 itemtype     => $itemtype,
609                 branchcode   => $branch,
610                 rules        => {
611                     holdallowed             => $holdallowed,
612                     hold_fulfillment_policy => $hold_fulfillment_policy,
613                     returnbranch            => $returnbranch,
614                 }
615             }
616         );
617     }
618 }
619 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
620
621     my $lostreturn = $input->param('lostreturn');
622
623     if ( $lostreturn eq '*' ) {
624         if ( $branch ne '*' ) {
625             # only do something for $lostreturn eq '*' if branch-specific
626             Koha::CirculationRules->set_rules(
627                 {
628                     branchcode   => $branch,
629                     rules        => {
630                         lostreturn => undef
631                     }
632                 }
633             );
634         }
635     } else {
636         Koha::CirculationRules->set_rules(
637             {
638                 branchcode   => $branch,
639                 rules        => {
640                     lostreturn => $lostreturn
641                 }
642             }
643         );
644     }
645 }
646
647 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
648 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
649 $template->param(
650     refundLostItemFeeRule => $refundLostItemFeeRule,
651     defaultRefundRule     => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
652 );
653
654 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
655
656 my $itemtypes = Koha::ItemTypes->search_with_localization;
657
658 my $humanbranch = ( $branch ne '*' ? $branch : undef );
659
660 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
661 my $definedbranch = $all_rules->count ? 1 : 0;
662
663 my $rules = {};
664 while ( my $r = $all_rules->next ) {
665     $r = $r->unblessed;
666     $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
667 }
668
669 $template->param(show_branch_cat_rule_form => 1);
670
671 $template->param(
672     patron_categories => $patron_categories,
673     itemtypeloop      => $itemtypes,
674     humanbranch       => $humanbranch,
675     current_branch    => $branch,
676     definedbranch     => $definedbranch,
677     all_rules         => $rules,
678 );
679 output_html_with_http_headers $input, $cookie, $template->output;
680
681 exit 0;
682
683 # sort by patron category, then item type, putting
684 # default entries at the bottom
685 sub by_category_and_itemtype {
686     unless (by_category($a, $b)) {
687         return by_itemtype($a, $b);
688     }
689 }
690
691 sub by_category {
692     my ($a, $b) = @_;
693     if ($a->{'default_humancategorycode'}) {
694         return ($b->{'default_humancategorycode'} ? 0 : 1);
695     } elsif ($b->{'default_humancategorycode'}) {
696         return -1;
697     } else {
698         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
699     }
700 }
701
702 sub by_itemtype {
703     my ($a, $b) = @_;
704     if ($a->{default_translated_description}) {
705         return ($b->{'default_translated_description'} ? 0 : 1);
706     } elsif ($b->{'default_translated_description'}) {
707         return -1;
708     } else {
709         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
710     }
711 }
712
713 sub strip_non_numeric {
714     my $string = shift;
715     $string =~ s/\s//g;
716     $string = '' if $string !~ /^\d+/;
717     return $string;
718 }