Bug 24663: (follow-up) Remove authnotrequired if set to 0
[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;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Debug;
27 use Koha::DateUtils;
28 use Koha::Database;
29 use Koha::Logger;
30 use Koha::RefundLostItemFeeRules;
31 use Koha::Libraries;
32 use Koha::CirculationRules;
33 use Koha::Patron::Categories;
34 use Koha::Caches;
35 use Koha::Patrons;
36
37 my $input = CGI->new;
38 my $dbh = C4::Context->dbh;
39
40 # my $flagsrequired;
41 # $flagsrequired->{circulation}=1;
42 my ($template, $loggedinuser, $cookie)
43     = get_template_and_user({template_name => "admin/smart-rules.tt",
44                             query => $input,
45                             type => "intranet",
46                             flagsrequired => {parameters => 'manage_circ_rules'},
47                             debug => 1,
48                             });
49
50 my $type=$input->param('type');
51
52 my $branch = $input->param('branch');
53 unless ( $branch ) {
54     if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
55         $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
56     }
57     else {
58         $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
59     }
60 }
61
62 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
63
64 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
65 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
66 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
67
68 my $op = $input->param('op') || q{};
69 my $language = C4::Languages::getlanguage();
70
71 my $cache = Koha::Caches->get_instance;
72 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
73
74 if ($op eq 'delete') {
75     my $itemtype     = $input->param('itemtype');
76     my $categorycode = $input->param('categorycode');
77     $debug and warn "deleting $1 $2 $branch";
78
79     Koha::CirculationRules->set_rules(
80         {
81             categorycode => $categorycode eq '*' ? undef : $categorycode,
82             branchcode   => $branch eq '*' ? undef : $branch,
83             itemtype     => $itemtype eq '*' ? undef : $itemtype,
84             rules        => {
85                 maxissueqty                      => undef,
86                 maxonsiteissueqty                => undef,
87                 rentaldiscount                   => undef,
88                 fine                             => undef,
89                 finedays                         => undef,
90                 maxsuspensiondays                => undef,
91                 suspension_chargeperiod          => undef,
92                 firstremind                      => undef,
93                 chargeperiod                     => undef,
94                 chargeperiod_charge_at           => undef,
95                 issuelength                      => undef,
96                 lengthunit                       => undef,
97                 hardduedate                      => undef,
98                 hardduedatecompare               => undef,
99                 renewalsallowed                  => undef,
100                 renewalperiod                    => undef,
101                 norenewalbefore                  => undef,
102                 auto_renew                       => undef,
103                 no_auto_renewal_after            => undef,
104                 no_auto_renewal_after_hard_limit => undef,
105                 reservesallowed                  => undef,
106                 holds_per_record                 => undef,
107                 holds_per_day                    => undef,
108                 onshelfholds                     => undef,
109                 opacitemholds                    => undef,
110                 overduefinescap                  => undef,
111                 cap_fine_to_replacement_price    => undef,
112                 article_requests                 => undef,
113                 note                             => undef,
114             }
115         }
116     );
117 }
118 elsif ($op eq 'delete-branch-cat') {
119     my $categorycode  = $input->param('categorycode');
120     if ($branch eq "*") {
121         if ($categorycode eq "*") {
122             Koha::CirculationRules->set_rules(
123                 {
124                     branchcode   => undef,
125                     categorycode => undef,
126                     rules        => {
127                         max_holds                      => undef,
128                         patron_maxissueqty             => undef,
129                         patron_maxonsiteissueqty       => undef,
130                     }
131                 }
132             );
133             Koha::CirculationRules->set_rules(
134                 {
135                     branchcode   => undef,
136                     itemtype     => undef,
137                     rules        => {
138                         holdallowed             => undef,
139                         hold_fulfillment_policy => undef,
140                         returnbranch            => undef,
141                     }
142                 }
143             );
144         } else {
145             Koha::CirculationRules->set_rules(
146                 {
147                     categorycode => $categorycode,
148                     branchcode   => undef,
149                     rules        => {
150                         max_holds                => undef,
151                         patron_maxissueqty       => undef,
152                         patron_maxonsiteissueqty => undef,
153                     }
154                 }
155             );
156         }
157     } elsif ($categorycode eq "*") {
158         Koha::CirculationRules->set_rules(
159             {
160                 branchcode   => $branch,
161                 categorycode => undef,
162                 rules        => {
163                     max_holds                => undef,
164                     patron_maxissueqty       => undef,
165                     patron_maxonsiteissueqty => undef,
166                 }
167             }
168         );
169         Koha::CirculationRules->set_rules(
170             {
171                 branchcode   => $branch,
172                 itemtype     => undef,
173                 rules        => {
174                     holdallowed             => undef,
175                     hold_fulfillment_policy => undef,
176                     returnbranch            => undef,
177                 }
178             }
179         );
180     } else {
181         Koha::CirculationRules->set_rules(
182             {
183                 categorycode => $categorycode,
184                 branchcode   => $branch,
185                 rules        => {
186                     max_holds         => undef,
187                     patron_maxissueqty       => undef,
188                     patron_maxonsiteissueqty => undef,
189                 }
190             }
191         );
192     }
193 }
194 elsif ($op eq 'delete-branch-item') {
195     my $itemtype  = $input->param('itemtype');
196     if ($branch eq "*") {
197         if ($itemtype eq "*") {
198             Koha::CirculationRules->set_rules(
199                 {
200                     branchcode   => undef,
201                     itemtype     => undef,
202                     rules        => {
203                         holdallowed             => undef,
204                         hold_fulfillment_policy => undef,
205                         returnbranch            => undef,
206                     }
207                 }
208             );
209         } else {
210             Koha::CirculationRules->set_rules(
211                 {
212                     branchcode   => undef,
213                     itemtype     => $itemtype,
214                     rules        => {
215                         holdallowed             => undef,
216                         hold_fulfillment_policy => undef,
217                         returnbranch            => undef,
218                     }
219                 }
220             );
221         }
222     } elsif ($itemtype eq "*") {
223         Koha::CirculationRules->set_rules(
224             {
225                 branchcode   => $branch,
226                 itemtype     => undef,
227                 rules        => {
228                     holdallowed             => undef,
229                     hold_fulfillment_policy => undef,
230                     returnbranch            => undef,
231                 }
232             }
233         );
234     } else {
235         Koha::CirculationRules->set_rules(
236             {
237                 branchcode   => $branch,
238                 itemtype     => $itemtype,
239                 rules        => {
240                     holdallowed             => undef,
241                     hold_fulfillment_policy => undef,
242                     returnbranch            => undef,
243                 }
244             }
245         );
246     }
247 }
248 # save the values entered
249 elsif ($op eq 'add') {
250     my $br = $branch; # branch
251     my $bor  = $input->param('categorycode'); # borrower category
252     my $itemtype  = $input->param('itemtype');     # item type
253     my $fine = $input->param('fine');
254     my $finedays     = $input->param('finedays');
255     my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
256     my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
257     my $firstremind  = $input->param('firstremind');
258     my $chargeperiod = $input->param('chargeperiod');
259     my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
260     my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
261     my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
262     my $renewalsallowed  = $input->param('renewalsallowed');
263     my $renewalperiod    = $input->param('renewalperiod');
264     my $norenewalbefore  = $input->param('norenewalbefore');
265     $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
266     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
267     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
268     $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
269     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
270     $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
271     $no_auto_renewal_after_hard_limit = output_pref( { dt => $no_auto_renewal_after_hard_limit, dateonly => 1, dateformat => 'iso' } ) if ( $no_auto_renewal_after_hard_limit );
272     my $reservesallowed  = strip_non_numeric( scalar $input->param('reservesallowed') );
273     my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
274     my $holds_per_day    = strip_non_numeric( scalar $input->param('holds_per_day') );
275     my $onshelfholds     = $input->param('onshelfholds') || 0;
276     my $issuelength  = $input->param('issuelength');
277     $issuelength = $issuelength eq q{} ? undef : $issuelength;
278     my $lengthunit  = $input->param('lengthunit');
279     my $hardduedate = $input->param('hardduedate') || undef;
280     $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
281     $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
282     my $hardduedatecompare = $input->param('hardduedatecompare');
283     my $rentaldiscount = $input->param('rentaldiscount');
284     my $opacitemholds = $input->param('opacitemholds') || 0;
285     my $article_requests = $input->param('article_requests') || 'no';
286     my $overduefinescap = $input->param('overduefinescap') || '';
287     my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
288     my $note = $input->param('note');
289     $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
290
291     my $rules = {
292         maxissueqty                   => $maxissueqty,
293         maxonsiteissueqty             => $maxonsiteissueqty,
294         rentaldiscount                => $rentaldiscount,
295         fine                          => $fine,
296         finedays                      => $finedays,
297         maxsuspensiondays             => $maxsuspensiondays,
298         suspension_chargeperiod       => $suspension_chargeperiod,
299         firstremind                   => $firstremind,
300         chargeperiod                  => $chargeperiod,
301         chargeperiod_charge_at        => $chargeperiod_charge_at,
302         issuelength                   => $issuelength,
303         lengthunit                    => $lengthunit,
304         hardduedate                   => $hardduedate,
305         hardduedatecompare            => $hardduedatecompare,
306         renewalsallowed               => $renewalsallowed,
307         renewalperiod                 => $renewalperiod,
308         norenewalbefore               => $norenewalbefore,
309         auto_renew                    => $auto_renew,
310         no_auto_renewal_after         => $no_auto_renewal_after,
311         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
312         reservesallowed               => $reservesallowed,
313         holds_per_record              => $holds_per_record,
314         holds_per_day                 => $holds_per_day,
315         onshelfholds                  => $onshelfholds,
316         opacitemholds                 => $opacitemholds,
317         overduefinescap               => $overduefinescap,
318         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
319         article_requests              => $article_requests,
320         note                          => $note,
321     };
322
323     Koha::CirculationRules->set_rules(
324         {
325             categorycode => $bor eq '*' ? undef : $bor,
326             itemtype     => $itemtype eq '*' ? undef : $itemtype,
327             branchcode   => $br eq '*' ? undef : $br,
328             rules        => $rules,
329         }
330     );
331
332 }
333 elsif ($op eq "set-branch-defaults") {
334     my $categorycode  = $input->param('categorycode');
335     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
336     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
337     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
338     my $holdallowed   = $input->param('holdallowed');
339     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
340     my $returnbranch  = $input->param('returnbranch');
341     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
342     $holdallowed =~ s/\s//g;
343     $holdallowed = undef if $holdallowed !~ /^\d+/;
344
345     if ($branch eq "*") {
346         Koha::CirculationRules->set_rules(
347             {
348                 itemtype     => undef,
349                 branchcode   => undef,
350                 rules        => {
351                     holdallowed             => $holdallowed,
352                     hold_fulfillment_policy => $hold_fulfillment_policy,
353                     returnbranch            => $returnbranch,
354                 }
355             }
356         );
357         Koha::CirculationRules->set_rules(
358             {
359                 categorycode => undef,
360                 branchcode   => undef,
361                 rules        => {
362                     patron_maxissueqty             => $patron_maxissueqty,
363                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
364                 }
365             }
366         );
367     } else {
368         Koha::CirculationRules->set_rules(
369             {
370                 itemtype     => undef,
371                 branchcode   => $branch,
372                 rules        => {
373                     holdallowed             => $holdallowed,
374                     hold_fulfillment_policy => $hold_fulfillment_policy,
375                     returnbranch            => $returnbranch,
376                 }
377             }
378         );
379         Koha::CirculationRules->set_rules(
380             {
381                 categorycode => undef,
382                 branchcode   => $branch,
383                 rules        => {
384                     patron_maxissueqty             => $patron_maxissueqty,
385                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
386                 }
387             }
388         );
389     }
390     Koha::CirculationRules->set_rule(
391         {
392             branchcode   => $branch,
393             categorycode => undef,
394             rule_name    => 'max_holds',
395             rule_value   => $max_holds,
396         }
397     );
398 }
399 elsif ($op eq "add-branch-cat") {
400     my $categorycode  = $input->param('categorycode');
401     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
402     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
403     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
404     my $max_holds = $input->param('max_holds');
405     $max_holds =~ s/\s//g;
406     $max_holds = undef if $max_holds !~ /^\d+/;
407
408     if ($branch eq "*") {
409         if ($categorycode eq "*") {
410             Koha::CirculationRules->set_rules(
411                 {
412                     categorycode => undef,
413                     branchcode   => undef,
414                     rules        => {
415                         max_holds         => $max_holds,
416                         patron_maxissueqty       => $patron_maxissueqty,
417                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
418                     }
419                 }
420             );
421         } else {
422             Koha::CirculationRules->set_rules(
423                 {
424                     categorycode => $categorycode,
425                     branchcode   => undef,
426                     rules        => {
427                         max_holds         => $max_holds,
428                         patron_maxissueqty       => $patron_maxissueqty,
429                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
430                     }
431                 }
432             );
433         }
434     } elsif ($categorycode eq "*") {
435         Koha::CirculationRules->set_rules(
436             {
437                 categorycode => undef,
438                 branchcode   => $branch,
439                 rules        => {
440                     max_holds         => $max_holds,
441                     patron_maxissueqty       => $patron_maxissueqty,
442                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
443                 }
444             }
445         );
446     } else {
447         Koha::CirculationRules->set_rules(
448             {
449                 categorycode => $categorycode,
450                 branchcode   => $branch,
451                 rules        => {
452                     max_holds         => $max_holds,
453                     patron_maxissueqty       => $patron_maxissueqty,
454                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
455                 }
456             }
457         );
458     }
459 }
460 elsif ($op eq "add-branch-item") {
461     my $itemtype                = $input->param('itemtype');
462     my $holdallowed             = $input->param('holdallowed');
463     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
464     my $returnbranch            = $input->param('returnbranch');
465
466     $holdallowed =~ s/\s//g;
467     $holdallowed = undef if $holdallowed !~ /^\d+/;
468
469     if ($branch eq "*") {
470         if ($itemtype eq "*") {
471             Koha::CirculationRules->set_rules(
472                 {
473                     itemtype     => undef,
474                     branchcode   => undef,
475                     rules        => {
476                         holdallowed             => $holdallowed,
477                         hold_fulfillment_policy => $hold_fulfillment_policy,
478                         returnbranch            => $returnbranch,
479                     }
480                 }
481             );
482         } else {
483             Koha::CirculationRules->set_rules(
484                 {
485                     itemtype     => $itemtype,
486                     branchcode   => undef,
487                     rules        => {
488                         holdallowed             => $holdallowed,
489                         hold_fulfillment_policy => $hold_fulfillment_policy,
490                         returnbranch            => $returnbranch,
491                     }
492                 }
493             );
494         }
495     } elsif ($itemtype eq "*") {
496             Koha::CirculationRules->set_rules(
497                 {
498                     itemtype     => undef,
499                     branchcode   => $branch,
500                     rules        => {
501                         holdallowed             => $holdallowed,
502                         hold_fulfillment_policy => $hold_fulfillment_policy,
503                         returnbranch            => $returnbranch,
504                     }
505                 }
506             );
507     } else {
508         Koha::CirculationRules->set_rules(
509             {
510                 itemtype     => $itemtype,
511                 branchcode   => $branch,
512                 rules        => {
513                     holdallowed             => $holdallowed,
514                     hold_fulfillment_policy => $hold_fulfillment_policy,
515                     returnbranch            => $returnbranch,
516                 }
517             }
518         );
519     }
520 }
521 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
522
523     my $refund = $input->param('refund');
524
525     if ( $refund eq '*' ) {
526         if ( $branch ne '*' ) {
527             # only do something for $refund eq '*' if branch-specific
528             Koha::CirculationRules->set_rules(
529                 {
530                     branchcode   => $branch,
531                     rules        => {
532                         refund => undef
533                     }
534                 }
535             );
536         }
537     } else {
538         Koha::CirculationRules->set_rules(
539             {
540                 branchcode   => $branch,
541                 rules        => {
542                     refund => $refund
543                 }
544             }
545         );
546     }
547 }
548
549 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef : $branch });
550 $template->param(
551     refundLostItemFeeRule => $refundLostItemFeeRule,
552     defaultRefundRule     => Koha::RefundLostItemFeeRules->_default_rule
553 );
554
555 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
556
557 my $itemtypes = Koha::ItemTypes->search_with_localization;
558
559 my $humanbranch = ( $branch ne '*' ? $branch : undef );
560
561 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
562 my $definedbranch = $all_rules->count ? 1 : 0;
563
564 my $rules = {};
565 while ( my $r = $all_rules->next ) {
566     $r = $r->unblessed;
567     $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
568 }
569
570 $template->param(show_branch_cat_rule_form => 1);
571
572 $template->param(
573     patron_categories => $patron_categories,
574     itemtypeloop      => $itemtypes,
575     humanbranch       => $humanbranch,
576     current_branch    => $branch,
577     definedbranch     => $definedbranch,
578     all_rules         => $rules,
579 );
580 output_html_with_http_headers $input, $cookie, $template->output;
581
582 exit 0;
583
584 # sort by patron category, then item type, putting
585 # default entries at the bottom
586 sub by_category_and_itemtype {
587     unless (by_category($a, $b)) {
588         return by_itemtype($a, $b);
589     }
590 }
591
592 sub by_category {
593     my ($a, $b) = @_;
594     if ($a->{'default_humancategorycode'}) {
595         return ($b->{'default_humancategorycode'} ? 0 : 1);
596     } elsif ($b->{'default_humancategorycode'}) {
597         return -1;
598     } else {
599         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
600     }
601 }
602
603 sub by_itemtype {
604     my ($a, $b) = @_;
605     if ($a->{default_translated_description}) {
606         return ($b->{'default_translated_description'} ? 0 : 1);
607     } elsif ($b->{'default_translated_description'}) {
608         return -1;
609     } else {
610         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
611     }
612 }
613
614 sub strip_non_numeric {
615     my $string = shift;
616     $string =~ s/\s//g;
617     $string = '' if $string !~ /^\d+/;
618     return $string;
619 }