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