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