Bug 25393: Add new noautorenewalbefore circulation rule
[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::Database;
27 use Koha::Logger;
28 use Koha::Libraries;
29 use Koha::CirculationRules;
30 use Koha::Patron::Categories;
31 use Koha::Caches;
32 use Koha::Patrons;
33
34 my $input = CGI->new;
35 my $dbh = C4::Context->dbh;
36
37 # my $flagsrequired;
38 # $flagsrequired->{circulation}=1;
39 my ($template, $loggedinuser, $cookie)
40     = get_template_and_user({template_name => "admin/smart-rules.tt",
41                             query => $input,
42                             type => "intranet",
43                             flagsrequired => {parameters => 'manage_circ_rules'},
44                             });
45
46 my $type=$input->param('type');
47
48 my $branch = $input->param('branch');
49 unless ( $branch ) {
50     if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
51         $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
52     }
53     else {
54         $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
55     }
56 }
57
58 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
59
60 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
61 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
62 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
63
64 my $op = $input->param('op') || q{};
65 my $language = C4::Languages::getlanguage();
66
67 my $cache = Koha::Caches->get_instance;
68 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
69
70 if ($op eq 'delete') {
71     my $itemtype     = $input->param('itemtype');
72     my $categorycode = $input->param('categorycode');
73
74     Koha::CirculationRules->set_rules(
75         {
76             categorycode => $categorycode eq '*' ? undef : $categorycode,
77             branchcode   => $branch eq '*' ? undef : $branch,
78             itemtype     => $itemtype eq '*' ? undef : $itemtype,
79             rules        => {
80                 maxissueqty                      => undef,
81                 maxonsiteissueqty                => undef,
82                 rentaldiscount                   => undef,
83                 fine                             => undef,
84                 finedays                         => undef,
85                 maxsuspensiondays                => undef,
86                 suspension_chargeperiod          => undef,
87                 firstremind                      => undef,
88                 chargeperiod                     => undef,
89                 chargeperiod_charge_at           => undef,
90                 issuelength                      => undef,
91                 daysmode                         => undef,
92                 lengthunit                       => undef,
93                 hardduedate                      => undef,
94                 hardduedatecompare               => undef,
95                 renewalsallowed                  => undef,
96                 unseen_renewals_allowed          => undef,
97                 renewalperiod                    => undef,
98                 norenewalbefore                  => undef,
99                 noautorenewalbefore              => 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') || q{};
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  = defined $input->param('unseen_renewals_allowed') ? strip_non_numeric( scalar $input->param('unseen_renewals_allowed') ) : q{};
269     my $renewalperiod    = $input->param('renewalperiod');
270     my $norenewalbefore  = $input->param('norenewalbefore');
271     $norenewalbefore = q{} if $norenewalbefore =~ /^\s*$/;
272     my $noautorenewalbefore = $input->param('noautorenewalbefore');
273     $noautorenewalbefore = q{} if $noautorenewalbefore =~ /^\s*$/;
274     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
275     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
276     $no_auto_renewal_after = q{} if $no_auto_renewal_after =~ /^\s*$/;
277     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || q{};
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') || 0;
283     my $daysmode = $input->param('daysmode');
284     my $lengthunit  = $input->param('lengthunit');
285     my $hardduedate = $input->param('hardduedate') || q{};
286     my $hardduedatecompare = $input->param('hardduedatecompare');
287     my $rentaldiscount = $input->param('rentaldiscount') || 0;
288     my $opacitemholds = $input->param('opacitemholds') || 0;
289     my $article_requests = $input->param('article_requests') || 'no';
290     my $overduefinescap = $input->param('overduefinescap')
291         && ( $input->param('overduefinescap') + 0 ) > 0 ? sprintf( "%.02f", $input->param('overduefinescap') ) : q{};
292     my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || q{}) eq 'on';
293     my $note = $input->param('note');
294     my $decreaseloanholds = $input->param('decreaseloanholds') || q{};
295     my $recalls_allowed = $input->param('recalls_allowed');
296     my $recalls_per_record = $input->param('recalls_per_record');
297     my $on_shelf_recalls = $input->param('on_shelf_recalls');
298     my $recall_due_date_interval = $input->param('recall_due_date_interval');
299     my $recall_overdue_fine = $input->param('recall_overdue_fine');
300     my $recall_shelf_time = $input->param('recall_shelf_time');
301
302     my $rules = {
303         maxissueqty                   => $maxissueqty,
304         maxonsiteissueqty             => $maxonsiteissueqty,
305         rentaldiscount                => $rentaldiscount,
306         fine                          => $fine,
307         finedays                      => $finedays,
308         maxsuspensiondays             => $maxsuspensiondays,
309         suspension_chargeperiod       => $suspension_chargeperiod,
310         firstremind                   => $firstremind,
311         chargeperiod                  => $chargeperiod,
312         chargeperiod_charge_at        => $chargeperiod_charge_at,
313         issuelength                   => $issuelength,
314         daysmode                      => $daysmode,
315         lengthunit                    => $lengthunit,
316         hardduedate                   => $hardduedate,
317         hardduedatecompare            => $hardduedatecompare,
318         renewalsallowed               => $renewalsallowed,
319         unseen_renewals_allowed       => $unseen_renewals_allowed,
320         renewalperiod                 => $renewalperiod,
321         norenewalbefore               => $norenewalbefore,
322         noautorenewalbefore           => $noautorenewalbefore,
323         auto_renew                    => $auto_renew,
324         no_auto_renewal_after         => $no_auto_renewal_after,
325         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
326         reservesallowed               => $reservesallowed,
327         holds_per_record              => $holds_per_record,
328         holds_per_day                 => $holds_per_day,
329         onshelfholds                  => $onshelfholds,
330         opacitemholds                 => $opacitemholds,
331         overduefinescap               => $overduefinescap,
332         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
333         article_requests              => $article_requests,
334         note                          => $note,
335         decreaseloanholds             => $decreaseloanholds,
336         recalls_allowed               => $recalls_allowed,
337         recalls_per_record            => $recalls_per_record,
338         on_shelf_recalls              => $on_shelf_recalls,
339         recall_due_date_interval      => $recall_due_date_interval,
340         recall_overdue_fine           => $recall_overdue_fine,
341         recall_shelf_time             => $recall_shelf_time,
342     };
343
344     Koha::CirculationRules->set_rules(
345         {
346             categorycode => $bor eq '*' ? undef : $bor,
347             itemtype     => $itemtype eq '*' ? undef : $itemtype,
348             branchcode   => $br eq '*' ? undef : $br,
349             rules        => $rules,
350         }
351     );
352
353 }
354 elsif ($op eq "set-branch-defaults") {
355     my $categorycode  = $input->param('categorycode');
356     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
357     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
358     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
359     my $holdallowed   = $input->param('holdallowed');
360     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
361     my $returnbranch  = $input->param('returnbranch');
362     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
363
364     if ($branch eq "*") {
365         Koha::CirculationRules->set_rules(
366             {
367                 itemtype     => undef,
368                 branchcode   => undef,
369                 rules        => {
370                     holdallowed             => $holdallowed,
371                     hold_fulfillment_policy => $hold_fulfillment_policy,
372                     returnbranch            => $returnbranch,
373                 }
374             }
375         );
376         Koha::CirculationRules->set_rules(
377             {
378                 categorycode => undef,
379                 branchcode   => undef,
380                 rules        => {
381                     patron_maxissueqty             => $patron_maxissueqty,
382                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
383                 }
384             }
385         );
386     } else {
387         Koha::CirculationRules->set_rules(
388             {
389                 itemtype     => undef,
390                 branchcode   => $branch,
391                 rules        => {
392                     holdallowed             => $holdallowed,
393                     hold_fulfillment_policy => $hold_fulfillment_policy,
394                     returnbranch            => $returnbranch,
395                 }
396             }
397         );
398         Koha::CirculationRules->set_rules(
399             {
400                 categorycode => undef,
401                 branchcode   => $branch,
402                 rules        => {
403                     patron_maxissueqty             => $patron_maxissueqty,
404                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
405                 }
406             }
407         );
408     }
409     Koha::CirculationRules->set_rule(
410         {
411             branchcode   => $branch,
412             categorycode => undef,
413             rule_name    => 'max_holds',
414             rule_value   => $max_holds,
415         }
416     );
417 }
418 elsif ($op eq "add-branch-cat") {
419     my $categorycode  = $input->param('categorycode');
420     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
421     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
422     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
423     my $max_holds = $input->param('max_holds');
424     $max_holds = strip_non_numeric($max_holds);
425
426     if ($branch eq "*") {
427         if ($categorycode eq "*") {
428             Koha::CirculationRules->set_rules(
429                 {
430                     categorycode => undef,
431                     branchcode   => undef,
432                     rules        => {
433                         max_holds         => $max_holds,
434                         patron_maxissueqty       => $patron_maxissueqty,
435                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
436                     }
437                 }
438             );
439         } else {
440             Koha::CirculationRules->set_rules(
441                 {
442                     categorycode => $categorycode,
443                     branchcode   => undef,
444                     rules        => {
445                         max_holds         => $max_holds,
446                         patron_maxissueqty       => $patron_maxissueqty,
447                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
448                     }
449                 }
450             );
451         }
452     } elsif ($categorycode eq "*") {
453         Koha::CirculationRules->set_rules(
454             {
455                 categorycode => undef,
456                 branchcode   => $branch,
457                 rules        => {
458                     max_holds         => $max_holds,
459                     patron_maxissueqty       => $patron_maxissueqty,
460                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
461                 }
462             }
463         );
464     } else {
465         Koha::CirculationRules->set_rules(
466             {
467                 categorycode => $categorycode,
468                 branchcode   => $branch,
469                 rules        => {
470                     max_holds         => $max_holds,
471                     patron_maxissueqty       => $patron_maxissueqty,
472                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
473                 }
474             }
475         );
476     }
477 }
478 elsif ( $op eq "add-open-article-requests-limit" ) {
479     my $categorycode                = $input->param('categorycode');
480     my $open_article_requests_limit = strip_non_numeric( scalar $input->param('open_article_requests_limit') );
481
482     Koha::Exception->throw("No value passed for article request limit")
483       if not defined $open_article_requests_limit # There is a JS check for that
484       || $open_article_requests_limit eq q{};
485
486     if ( $branch eq "*" ) {
487         if ( $categorycode eq "*" ) {
488             Koha::CirculationRules->set_rules(
489                 {   categorycode => undef,
490                     branchcode   => undef,
491                     rules        => { open_article_requests_limit => $open_article_requests_limit, }
492                 }
493             );
494         } else {
495             Koha::CirculationRules->set_rules(
496                 {   categorycode => $categorycode,
497                     branchcode   => undef,
498                     rules        => { open_article_requests_limit => $open_article_requests_limit, }
499                 }
500             );
501         }
502     } elsif ( $categorycode eq "*" ) {
503         Koha::CirculationRules->set_rules(
504             {   categorycode => undef,
505                 branchcode   => $branch,
506                 rules        => { open_article_requests_limit => $open_article_requests_limit, }
507             }
508         );
509     } else {
510         Koha::CirculationRules->set_rules(
511             {   categorycode => $categorycode,
512                 branchcode   => $branch,
513                 rules        => { open_article_requests_limit => $open_article_requests_limit, }
514             }
515         );
516     }
517 } elsif ( $op eq 'del-open-article-requests-limit' ) {
518     my $categorycode = $input->param('categorycode');
519     if ( $branch eq "*" ) {
520         if ( $categorycode eq "*" ) {
521             Koha::CirculationRules->set_rules(
522                 {   branchcode   => undef,
523                     categorycode => undef,
524                     rules        => { open_article_requests_limit => undef, }
525                 }
526             );
527         } else {
528             Koha::CirculationRules->set_rules(
529                 {   categorycode => $categorycode,
530                     branchcode   => undef,
531                     rules        => { open_article_requests_limit => undef, }
532                 }
533             );
534         }
535     } elsif ( $categorycode eq "*" ) {
536         Koha::CirculationRules->set_rules(
537             {   branchcode   => $branch,
538                 categorycode => undef,
539                 rules        => { open_article_requests_limit => undef, }
540             }
541         );
542     } else {
543         Koha::CirculationRules->set_rules(
544             {   categorycode => $categorycode,
545                 branchcode   => $branch,
546                 rules        => { open_article_requests_limit => undef, }
547             }
548         );
549     }
550 }
551 elsif ( $op eq "set-article-request-fee" ) {
552
553     my $category = $input->param('article_request_fee_category');
554     my $fee      = strip_non_numeric( scalar $input->param('article_request_fee') );
555
556     Koha::Exception->throw("No value passed for article request fee")
557       if not defined $fee # There is a JS check for that
558       || $fee eq q{};
559
560     Koha::CirculationRules->set_rules(
561         {   categorycode => ( $category  eq '*' ) ? undef : $category,
562             branchcode   => ( $branch    eq '*' ) ? undef : $branch,
563             rules        => { article_request_fee => $fee },
564         }
565     );
566
567 } elsif ( $op eq 'del-article-request-fee' ) {
568
569     my $category  = $input->param('article_request_fee_category');
570
571     Koha::CirculationRules->set_rules(
572         {   categorycode => ( $category eq  '*' ) ? undef : $category,
573             branchcode   => ( $branch eq    '*' ) ? undef : $branch,
574             rules        => { article_request_fee => undef },
575         }
576     );
577 }
578 elsif ($op eq "add-branch-item") {
579     my $itemtype                = $input->param('itemtype');
580     my $holdallowed             = $input->param('holdallowed');
581     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
582     my $returnbranch            = $input->param('returnbranch');
583
584     if ($branch eq "*") {
585         if ($itemtype eq "*") {
586             Koha::CirculationRules->set_rules(
587                 {
588                     itemtype     => undef,
589                     branchcode   => undef,
590                     rules        => {
591                         holdallowed             => $holdallowed,
592                         hold_fulfillment_policy => $hold_fulfillment_policy,
593                         returnbranch            => $returnbranch,
594                     }
595                 }
596             );
597         } else {
598             Koha::CirculationRules->set_rules(
599                 {
600                     itemtype     => $itemtype,
601                     branchcode   => undef,
602                     rules        => {
603                         holdallowed             => $holdallowed,
604                         hold_fulfillment_policy => $hold_fulfillment_policy,
605                         returnbranch            => $returnbranch,
606                     }
607                 }
608             );
609         }
610     } elsif ($itemtype eq "*") {
611             Koha::CirculationRules->set_rules(
612                 {
613                     itemtype     => undef,
614                     branchcode   => $branch,
615                     rules        => {
616                         holdallowed             => $holdallowed,
617                         hold_fulfillment_policy => $hold_fulfillment_policy,
618                         returnbranch            => $returnbranch,
619                     }
620                 }
621             );
622     } else {
623         Koha::CirculationRules->set_rules(
624             {
625                 itemtype     => $itemtype,
626                 branchcode   => $branch,
627                 rules        => {
628                     holdallowed             => $holdallowed,
629                     hold_fulfillment_policy => $hold_fulfillment_policy,
630                     returnbranch            => $returnbranch,
631                 }
632             }
633         );
634     }
635 }
636 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
637
638     my $lostreturn = $input->param('lostreturn');
639
640     if ( $lostreturn eq '*' ) {
641         if ( $branch ne '*' ) {
642             # only do something for $lostreturn eq '*' if branch-specific
643             Koha::CirculationRules->set_rules(
644                 {
645                     branchcode   => $branch,
646                     rules        => {
647                         lostreturn => undef
648                     }
649                 }
650             );
651         }
652     } else {
653         Koha::CirculationRules->set_rules(
654             {
655                 branchcode   => $branch,
656                 rules        => {
657                     lostreturn => $lostreturn
658                 }
659             }
660         );
661     }
662
663     my $processingreturn = $input->param('processingreturn');
664
665     if ( $processingreturn eq '*' ) {
666         if ( $branch ne '*' ) {
667             # only do something for $processingreturn eq '*' if branch-specific
668             Koha::CirculationRules->set_rules(
669                 {
670                     branchcode   => $branch,
671                     rules        => {
672                         processingreturn => undef
673                     }
674                 }
675             );
676         }
677     } else {
678         Koha::CirculationRules->set_rules(
679             {
680                 branchcode   => $branch,
681                 rules        => {
682                     processingreturn => $processingreturn
683                 }
684             }
685         );
686     }
687 } elsif ( $op eq "set-waiting-hold-cancellation" ) {
688
689     my $category = $input->param('waiting_hold_cancellation_category');
690     my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
691     my $policy   = strip_non_numeric( scalar $input->param('waiting_hold_cancellation_policy') )
692                     ? 1
693                     : 0;
694
695     Koha::Exception->throw("No value passed for waiting holds cancellation policy")
696       if not defined $policy # There is a JS check for that
697       || $policy eq '';
698
699     Koha::CirculationRules->set_rules(
700         {   categorycode => ( $category eq '*' ) ? undef : $category,
701             itemtype     => ( $itemtype eq '*' ) ? undef : $itemtype,
702             branchcode   => ( $branch   eq '*' ) ? undef : $branch,
703             rules        => { waiting_hold_cancellation => $policy },
704         }
705     );
706
707 } elsif ( $op eq 'del-waiting-hold-cancellation' ) {
708
709     my $category = $input->param('waiting_hold_cancellation_category');
710     my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
711
712     Koha::CirculationRules->set_rules(
713         {   categorycode => ( $category eq '*' ) ? undef : $category,
714             itemtype     => ( $itemtype eq '*' ) ? undef : $itemtype,
715             branchcode   => ( $branch   eq '*' ) ? undef : $branch,
716             rules        => { waiting_hold_cancellation => undef },
717         }
718     );
719 }
720
721
722 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
723 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
724 my $refundProcessingFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'processingreturn' });
725 my $defaultProcessingFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'processingreturn' });
726 $template->param(
727     refundLostItemFeeRule => $refundLostItemFeeRule,
728     defaultRefundRule     => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund',
729     refundProcessingFeeRule => $refundProcessingFeeRule,
730     defaultProcessingRefundRule => $defaultProcessingFeeRule ? $defaultProcessingFeeRule->rule_value : 'refund',
731 );
732
733 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
734
735 my $itemtypes = Koha::ItemTypes->search_with_localization;
736
737 my $humanbranch = ( $branch ne '*' ? $branch : undef );
738
739 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
740 my $definedbranch = $all_rules->count ? 1 : 0;
741
742 my $rules = {};
743 while ( my $r = $all_rules->next ) {
744     $r = $r->unblessed;
745     $rules->{ $r->{categorycode} // q{} }->{ $r->{itemtype} // q{} }->{ $r->{rule_name} } = $r->{rule_value};
746 }
747
748 $template->param(show_branch_cat_rule_form => 1);
749
750 $template->param(
751     patron_categories => $patron_categories,
752     itemtypeloop      => $itemtypes,
753     humanbranch       => $humanbranch,
754     current_branch    => $branch,
755     definedbranch     => $definedbranch,
756     all_rules         => $rules,
757 );
758 output_html_with_http_headers $input, $cookie, $template->output;
759
760 exit 0;
761
762 # sort by patron category, then item type, putting
763 # default entries at the bottom
764 sub by_category_and_itemtype {
765     unless (by_category($a, $b)) {
766         return by_itemtype($a, $b);
767     }
768 }
769
770 sub by_category {
771     my ($a, $b) = @_;
772     if ($a->{'default_humancategorycode'}) {
773         return ($b->{'default_humancategorycode'} ? 0 : 1);
774     } elsif ($b->{'default_humancategorycode'}) {
775         return -1;
776     } else {
777         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
778     }
779 }
780
781 sub by_itemtype {
782     my ($a, $b) = @_;
783     if ($a->{default_translated_description}) {
784         return ($b->{'default_translated_description'} ? 0 : 1);
785     } elsif ($b->{'default_translated_description'}) {
786         return -1;
787     } else {
788         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
789     }
790 }
791
792 sub strip_non_numeric {
793     my $string = shift;
794     $string =~ s/\s//g;
795     $string = q{} if $string !~ /^\d+/;
796     return $string;
797 }