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