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