Bug 35306: Fix Expired holds when modding request
[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                 holds_pickup_period              => undef,
120             }
121         }
122     );
123 }
124 elsif ($op eq 'delete-branch-cat') {
125     my $categorycode  = $input->param('categorycode');
126     if ($branch eq "*") {
127         if ($categorycode eq "*") {
128             Koha::CirculationRules->set_rules(
129                 {
130                     branchcode   => undef,
131                     categorycode => undef,
132                     rules        => {
133                         max_holds                      => undef,
134                         patron_maxissueqty             => undef,
135                         patron_maxonsiteissueqty       => undef,
136                     }
137                 }
138             );
139             Koha::CirculationRules->set_rules(
140                 {
141                     branchcode   => undef,
142                     itemtype     => undef,
143                     rules        => {
144                         holdallowed             => undef,
145                         hold_fulfillment_policy => undef,
146                         returnbranch            => undef,
147                     }
148                 }
149             );
150         } else {
151             Koha::CirculationRules->set_rules(
152                 {
153                     categorycode => $categorycode,
154                     branchcode   => undef,
155                     rules        => {
156                         max_holds                => undef,
157                         patron_maxissueqty       => undef,
158                         patron_maxonsiteissueqty => undef,
159                     }
160                 }
161             );
162         }
163     } elsif ($categorycode eq "*") {
164         Koha::CirculationRules->set_rules(
165             {
166                 branchcode   => $branch,
167                 categorycode => undef,
168                 rules        => {
169                     max_holds                => undef,
170                     patron_maxissueqty       => undef,
171                     patron_maxonsiteissueqty => undef,
172                 }
173             }
174         );
175         Koha::CirculationRules->set_rules(
176             {
177                 branchcode   => $branch,
178                 itemtype     => undef,
179                 rules        => {
180                     holdallowed             => undef,
181                     hold_fulfillment_policy => undef,
182                     returnbranch            => undef,
183                 }
184             }
185         );
186     } else {
187         Koha::CirculationRules->set_rules(
188             {
189                 categorycode => $categorycode,
190                 branchcode   => $branch,
191                 rules        => {
192                     max_holds         => undef,
193                     patron_maxissueqty       => undef,
194                     patron_maxonsiteissueqty => undef,
195                 }
196             }
197         );
198     }
199 }
200 elsif ($op eq 'delete-branch-item') {
201     my $itemtype  = $input->param('itemtype');
202     if ($branch eq "*") {
203         if ($itemtype eq "*") {
204             Koha::CirculationRules->set_rules(
205                 {
206                     branchcode   => undef,
207                     itemtype     => undef,
208                     rules        => {
209                         holdallowed             => undef,
210                         hold_fulfillment_policy => undef,
211                         returnbranch            => undef,
212                     }
213                 }
214             );
215         } else {
216             Koha::CirculationRules->set_rules(
217                 {
218                     branchcode   => undef,
219                     itemtype     => $itemtype,
220                     rules        => {
221                         holdallowed             => undef,
222                         hold_fulfillment_policy => undef,
223                         returnbranch            => undef,
224                     }
225                 }
226             );
227         }
228     } elsif ($itemtype eq "*") {
229         Koha::CirculationRules->set_rules(
230             {
231                 branchcode   => $branch,
232                 itemtype     => undef,
233                 rules        => {
234                     holdallowed             => undef,
235                     hold_fulfillment_policy => undef,
236                     returnbranch            => undef,
237                 }
238             }
239         );
240     } else {
241         Koha::CirculationRules->set_rules(
242             {
243                 branchcode   => $branch,
244                 itemtype     => $itemtype,
245                 rules        => {
246                     holdallowed             => undef,
247                     hold_fulfillment_policy => undef,
248                     returnbranch            => undef,
249                 }
250             }
251         );
252     }
253 }
254 # save the values entered
255 elsif ( $op eq 'add' ) {
256     my $br                      = $branch;                          # branch
257     my $bor                     = $input->param('categorycode');    # borrower category
258     my $itemtype                = $input->param('itemtype');        # item type
259     my $fine                    = $input->param('fine');
260     my $finedays                = $input->param('finedays');
261     my $maxsuspensiondays       = $input->param('maxsuspensiondays')       || q{};
262     my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
263     my $firstremind             = $input->param('firstremind');
264     my $chargeperiod            = $input->param('chargeperiod');
265     my $chargeperiod_charge_at  = $input->param('chargeperiod_charge_at');
266     my $maxissueqty             = strip_non_numeric( scalar $input->param('maxissueqty') );
267     my $maxonsiteissueqty       = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
268     my $renewalsallowed         = $input->param('renewalsallowed');
269     my $unseen_renewals_allowed =
270         defined $input->param('unseen_renewals_allowed')
271         ? strip_non_numeric( scalar $input->param('unseen_renewals_allowed') )
272         : q{};
273     my $renewalperiod   = $input->param('renewalperiod');
274     my $norenewalbefore = $input->param('norenewalbefore');
275     $norenewalbefore = q{} if $norenewalbefore =~ /^\s*$/;
276     my $noautorenewalbefore = $input->param('noautorenewalbefore');
277     my $auto_renew            = $input->param('auto_renew') eq 'yes' ? 1 : 0;
278     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
279     $no_auto_renewal_after = q{} if $no_auto_renewal_after =~ /^\s*$/;
280     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || q{};
281     my $reservesallowed                  = strip_non_numeric( scalar $input->param('reservesallowed') );
282     my $holds_per_record                 = strip_non_numeric( scalar $input->param('holds_per_record') );
283     my $holds_per_day                    = strip_non_numeric( scalar $input->param('holds_per_day') );
284     my $onshelfholds                     = $input->param('onshelfholds') || 0;
285     my $issuelength                      = $input->param('issuelength')  || 0;
286     my $daysmode                         = $input->param('daysmode');
287     my $lengthunit                       = $input->param('lengthunit');
288     my $hardduedate                      = $input->param('hardduedate') || q{};
289     my $hardduedatecompare               = $input->param('hardduedatecompare');
290     my $rentaldiscount                   = $input->param('rentaldiscount')   || 0;
291     my $opacitemholds                    = $input->param('opacitemholds')    || 0;
292     my $article_requests                 = $input->param('article_requests') || 'no';
293     my $overduefinescap                  = $input->param('overduefinescap')
294         && ( $input->param('overduefinescap') + 0 ) > 0 ? sprintf( "%.02f", $input->param('overduefinescap') ) : q{};
295     my $cap_fine_to_replacement_price = ( $input->param('cap_fine_to_replacement_price') || q{} ) eq 'on';
296     my $note                          = $input->param('note');
297     my $decreaseloanholds             = $input->param('decreaseloanholds') || q{};
298     my $recalls_allowed               = $input->param('recalls_allowed');
299     my $recalls_per_record            = $input->param('recalls_per_record');
300     my $on_shelf_recalls              = $input->param('on_shelf_recalls');
301     my $recall_due_date_interval      = $input->param('recall_due_date_interval');
302     my $recall_overdue_fine           = $input->param('recall_overdue_fine');
303     my $recall_shelf_time             = $input->param('recall_shelf_time');
304     my $holds_pickup_period           = strip_non_numeric( scalar $input->param('holds_pickup_period') );
305
306     my $rules = {
307         maxissueqty                      => $maxissueqty,
308         maxonsiteissueqty                => $maxonsiteissueqty,
309         rentaldiscount                   => $rentaldiscount,
310         fine                             => $fine,
311         finedays                         => $finedays,
312         maxsuspensiondays                => $maxsuspensiondays,
313         suspension_chargeperiod          => $suspension_chargeperiod,
314         firstremind                      => $firstremind,
315         chargeperiod                     => $chargeperiod,
316         chargeperiod_charge_at           => $chargeperiod_charge_at,
317         issuelength                      => $issuelength,
318         daysmode                         => $daysmode,
319         lengthunit                       => $lengthunit,
320         hardduedate                      => $hardduedate,
321         hardduedatecompare               => $hardduedatecompare,
322         renewalsallowed                  => $renewalsallowed,
323         unseen_renewals_allowed          => $unseen_renewals_allowed,
324         renewalperiod                    => $renewalperiod,
325         norenewalbefore                  => $norenewalbefore,
326         noautorenewalbefore              => $noautorenewalbefore,
327         auto_renew                       => $auto_renew,
328         no_auto_renewal_after            => $no_auto_renewal_after,
329         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
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         holds_pickup_period              => $holds_pickup_period,
344     };
345
346     Koha::CirculationRules->set_rules(
347         {
348             categorycode => $bor eq '*'      ? undef : $bor,
349             itemtype     => $itemtype eq '*' ? undef : $itemtype,
350             branchcode   => $br eq '*'       ? undef : $br,
351             rules        => $rules,
352         }
353     );
354
355 }
356 elsif ($op eq "set-branch-defaults") {
357     my $categorycode  = $input->param('categorycode');
358     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
359     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
360     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
361     my $holdallowed   = $input->param('holdallowed');
362     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
363     my $returnbranch  = $input->param('returnbranch');
364     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
365
366     if ($branch eq "*") {
367         Koha::CirculationRules->set_rules(
368             {
369                 itemtype     => undef,
370                 branchcode   => undef,
371                 rules        => {
372                     holdallowed             => $holdallowed,
373                     hold_fulfillment_policy => $hold_fulfillment_policy,
374                     returnbranch            => $returnbranch,
375                 }
376             }
377         );
378         Koha::CirculationRules->set_rules(
379             {
380                 categorycode => undef,
381                 branchcode   => undef,
382                 rules        => {
383                     patron_maxissueqty             => $patron_maxissueqty,
384                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
385                 }
386             }
387         );
388     } else {
389         Koha::CirculationRules->set_rules(
390             {
391                 itemtype     => undef,
392                 branchcode   => $branch,
393                 rules        => {
394                     holdallowed             => $holdallowed,
395                     hold_fulfillment_policy => $hold_fulfillment_policy,
396                     returnbranch            => $returnbranch,
397                 }
398             }
399         );
400         Koha::CirculationRules->set_rules(
401             {
402                 categorycode => undef,
403                 branchcode   => $branch,
404                 rules        => {
405                     patron_maxissueqty             => $patron_maxissueqty,
406                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
407                 }
408             }
409         );
410     }
411     Koha::CirculationRules->set_rule(
412         {
413             branchcode   => $branch,
414             categorycode => undef,
415             rule_name    => 'max_holds',
416             rule_value   => $max_holds,
417         }
418     );
419 }
420 elsif ($op eq "add-branch-cat") {
421     my $categorycode  = $input->param('categorycode');
422     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
423     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
424     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
425     my $max_holds = $input->param('max_holds');
426     $max_holds = strip_non_numeric($max_holds);
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 q{};
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 q{};
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     my $processingreturn = $input->param('processingreturn');
666
667     if ( $processingreturn eq '*' ) {
668         if ( $branch ne '*' ) {
669             # only do something for $processingreturn eq '*' if branch-specific
670             Koha::CirculationRules->set_rules(
671                 {
672                     branchcode   => $branch,
673                     rules        => {
674                         processingreturn => undef
675                     }
676                 }
677             );
678         }
679     } else {
680         Koha::CirculationRules->set_rules(
681             {
682                 branchcode   => $branch,
683                 rules        => {
684                     processingreturn => $processingreturn
685                 }
686             }
687         );
688     }
689 } elsif ( $op eq "set-waiting-hold-cancellation" ) {
690
691     my $category = $input->param('waiting_hold_cancellation_category');
692     my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
693     my $policy   = strip_non_numeric( scalar $input->param('waiting_hold_cancellation_policy') )
694                     ? 1
695                     : 0;
696
697     Koha::Exception->throw("No value passed for waiting holds cancellation policy")
698       if not defined $policy # There is a JS check for that
699       || $policy eq '';
700
701     Koha::CirculationRules->set_rules(
702         {   categorycode => ( $category eq '*' ) ? undef : $category,
703             itemtype     => ( $itemtype eq '*' ) ? undef : $itemtype,
704             branchcode   => ( $branch   eq '*' ) ? undef : $branch,
705             rules        => { waiting_hold_cancellation => $policy },
706         }
707     );
708
709 } elsif ( $op eq 'del-waiting-hold-cancellation' ) {
710
711     my $category = $input->param('waiting_hold_cancellation_category');
712     my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
713
714     Koha::CirculationRules->set_rules(
715         {   categorycode => ( $category eq '*' ) ? undef : $category,
716             itemtype     => ( $itemtype eq '*' ) ? undef : $itemtype,
717             branchcode   => ( $branch   eq '*' ) ? undef : $branch,
718             rules        => { waiting_hold_cancellation => undef },
719         }
720     );
721 }
722
723
724 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
725 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
726 my $refundProcessingFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'processingreturn' });
727 my $defaultProcessingFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'processingreturn' });
728 $template->param(
729     refundLostItemFeeRule => $refundLostItemFeeRule,
730     defaultRefundRule     => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund',
731     refundProcessingFeeRule => $refundProcessingFeeRule,
732     defaultProcessingRefundRule => $defaultProcessingFeeRule ? $defaultProcessingFeeRule->rule_value : 'refund',
733 );
734
735 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
736
737 my $itemtypes = Koha::ItemTypes->search_with_localization;
738
739 my $humanbranch = ( $branch ne '*' ? $branch : undef );
740
741 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
742 my $definedbranch = $all_rules->count ? 1 : 0;
743
744 my $rules = {};
745 while ( my $r = $all_rules->next ) {
746     $r = $r->unblessed;
747     $rules->{ $r->{categorycode} // q{} }->{ $r->{itemtype} // q{} }->{ $r->{rule_name} } = $r->{rule_value};
748 }
749
750 $template->param(show_branch_cat_rule_form => 1);
751
752 $template->param(
753     patron_categories => $patron_categories,
754     itemtypeloop      => $itemtypes,
755     humanbranch       => $humanbranch,
756     current_branch    => $branch,
757     definedbranch     => $definedbranch,
758     all_rules         => $rules,
759 );
760 output_html_with_http_headers $input, $cookie, $template->output;
761
762 exit 0;
763
764 # sort by patron category, then item type, putting
765 # default entries at the bottom
766 sub by_category_and_itemtype {
767     unless (by_category($a, $b)) {
768         return by_itemtype($a, $b);
769     }
770 }
771
772 sub by_category {
773     my ($a, $b) = @_;
774     if ($a->{'default_humancategorycode'}) {
775         return ($b->{'default_humancategorycode'} ? 0 : 1);
776     } elsif ($b->{'default_humancategorycode'}) {
777         return -1;
778     } else {
779         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
780     }
781 }
782
783 sub by_itemtype {
784     my ($a, $b) = @_;
785     if ($a->{default_translated_description}) {
786         return ($b->{'default_translated_description'} ? 0 : 1);
787     } elsif ($b->{'default_translated_description'}) {
788         return -1;
789     } else {
790         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
791     }
792 }
793
794 sub strip_non_numeric {
795     my $string = shift;
796     $string =~ s/\s//g;
797     $string = q{} if $string !~ /^\d+/;
798     return $string;
799 }