7da7497d3a788d55aaecc710f6a7894247850e54
[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::DateUtils qw( dt_from_string output_pref );
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             }
112         }
113     );
114 }
115 elsif ($op eq 'delete-branch-cat') {
116     my $categorycode  = $input->param('categorycode');
117     if ($branch eq "*") {
118         if ($categorycode eq "*") {
119             Koha::CirculationRules->set_rules(
120                 {
121                     branchcode   => undef,
122                     categorycode => undef,
123                     rules        => {
124                         max_holds                      => undef,
125                         patron_maxissueqty             => undef,
126                         patron_maxonsiteissueqty       => undef,
127                     }
128                 }
129             );
130             Koha::CirculationRules->set_rules(
131                 {
132                     branchcode   => undef,
133                     itemtype     => undef,
134                     rules        => {
135                         holdallowed             => undef,
136                         hold_fulfillment_policy => undef,
137                         returnbranch            => undef,
138                     }
139                 }
140             );
141         } else {
142             Koha::CirculationRules->set_rules(
143                 {
144                     categorycode => $categorycode,
145                     branchcode   => undef,
146                     rules        => {
147                         max_holds                => undef,
148                         patron_maxissueqty       => undef,
149                         patron_maxonsiteissueqty => undef,
150                     }
151                 }
152             );
153         }
154     } elsif ($categorycode eq "*") {
155         Koha::CirculationRules->set_rules(
156             {
157                 branchcode   => $branch,
158                 categorycode => undef,
159                 rules        => {
160                     max_holds                => undef,
161                     patron_maxissueqty       => undef,
162                     patron_maxonsiteissueqty => undef,
163                 }
164             }
165         );
166         Koha::CirculationRules->set_rules(
167             {
168                 branchcode   => $branch,
169                 itemtype     => undef,
170                 rules        => {
171                     holdallowed             => undef,
172                     hold_fulfillment_policy => undef,
173                     returnbranch            => undef,
174                 }
175             }
176         );
177     } else {
178         Koha::CirculationRules->set_rules(
179             {
180                 categorycode => $categorycode,
181                 branchcode   => $branch,
182                 rules        => {
183                     max_holds         => undef,
184                     patron_maxissueqty       => undef,
185                     patron_maxonsiteissueqty => undef,
186                 }
187             }
188         );
189     }
190 }
191 elsif ($op eq 'delete-branch-item') {
192     my $itemtype  = $input->param('itemtype');
193     if ($branch eq "*") {
194         if ($itemtype eq "*") {
195             Koha::CirculationRules->set_rules(
196                 {
197                     branchcode   => undef,
198                     itemtype     => undef,
199                     rules        => {
200                         holdallowed             => undef,
201                         hold_fulfillment_policy => undef,
202                         returnbranch            => undef,
203                     }
204                 }
205             );
206         } else {
207             Koha::CirculationRules->set_rules(
208                 {
209                     branchcode   => undef,
210                     itemtype     => $itemtype,
211                     rules        => {
212                         holdallowed             => undef,
213                         hold_fulfillment_policy => undef,
214                         returnbranch            => undef,
215                     }
216                 }
217             );
218         }
219     } elsif ($itemtype eq "*") {
220         Koha::CirculationRules->set_rules(
221             {
222                 branchcode   => $branch,
223                 itemtype     => undef,
224                 rules        => {
225                     holdallowed             => undef,
226                     hold_fulfillment_policy => undef,
227                     returnbranch            => undef,
228                 }
229             }
230         );
231     } else {
232         Koha::CirculationRules->set_rules(
233             {
234                 branchcode   => $branch,
235                 itemtype     => $itemtype,
236                 rules        => {
237                     holdallowed             => undef,
238                     hold_fulfillment_policy => undef,
239                     returnbranch            => undef,
240                 }
241             }
242         );
243     }
244 }
245 # save the values entered
246 elsif ($op eq 'add') {
247     my $br = $branch; # branch
248     my $bor  = $input->param('categorycode'); # borrower category
249     my $itemtype  = $input->param('itemtype');     # item type
250     my $fine = $input->param('fine');
251     my $finedays     = $input->param('finedays');
252     my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
253     my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
254     my $firstremind  = $input->param('firstremind');
255     my $chargeperiod = $input->param('chargeperiod');
256     my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
257     my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
258     my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
259     my $renewalsallowed  = $input->param('renewalsallowed');
260     my $unseen_renewals_allowed  = $input->param('unseen_renewals_allowed');
261     my $renewalperiod    = $input->param('renewalperiod');
262     my $norenewalbefore  = $input->param('norenewalbefore');
263     $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
264     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
265     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
266     $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
267     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
268     $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
269     $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 );
270     my $reservesallowed  = strip_non_numeric( scalar $input->param('reservesallowed') );
271     my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
272     my $holds_per_day    = strip_non_numeric( scalar $input->param('holds_per_day') );
273     my $onshelfholds     = $input->param('onshelfholds') || 0;
274     my $issuelength  = $input->param('issuelength');
275     $issuelength = $issuelength eq q{} ? undef : $issuelength;
276     my $daysmode = $input->param('daysmode');
277     my $lengthunit  = $input->param('lengthunit');
278     my $hardduedate = $input->param('hardduedate') || undef;
279     $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
280     $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
281     my $hardduedatecompare = $input->param('hardduedatecompare');
282     my $rentaldiscount = $input->param('rentaldiscount');
283     my $opacitemholds = $input->param('opacitemholds') || 0;
284     my $article_requests = $input->param('article_requests') || 'no';
285     my $overduefinescap = $input->param('overduefinescap') || '';
286     my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
287     my $note = $input->param('note');
288     my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
289
290     my $rules = {
291         maxissueqty                   => $maxissueqty,
292         maxonsiteissueqty             => $maxonsiteissueqty,
293         rentaldiscount                => $rentaldiscount,
294         fine                          => $fine,
295         finedays                      => $finedays,
296         maxsuspensiondays             => $maxsuspensiondays,
297         suspension_chargeperiod       => $suspension_chargeperiod,
298         firstremind                   => $firstremind,
299         chargeperiod                  => $chargeperiod,
300         chargeperiod_charge_at        => $chargeperiod_charge_at,
301         issuelength                   => $issuelength,
302         daysmode                      => $daysmode,
303         lengthunit                    => $lengthunit,
304         hardduedate                   => $hardduedate,
305         hardduedatecompare            => $hardduedatecompare,
306         renewalsallowed               => $renewalsallowed,
307         unseen_renewals_allowed       => $unseen_renewals_allowed,
308         renewalperiod                 => $renewalperiod,
309         norenewalbefore               => $norenewalbefore,
310         auto_renew                    => $auto_renew,
311         no_auto_renewal_after         => $no_auto_renewal_after,
312         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
313         reservesallowed               => $reservesallowed,
314         holds_per_record              => $holds_per_record,
315         holds_per_day                 => $holds_per_day,
316         onshelfholds                  => $onshelfholds,
317         opacitemholds                 => $opacitemholds,
318         overduefinescap               => $overduefinescap,
319         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
320         article_requests              => $article_requests,
321         note                          => $note,
322         decreaseloanholds             => $decreaseloanholds,
323     };
324
325     Koha::CirculationRules->set_rules(
326         {
327             categorycode => $bor eq '*' ? undef : $bor,
328             itemtype     => $itemtype eq '*' ? undef : $itemtype,
329             branchcode   => $br eq '*' ? undef : $br,
330             rules        => $rules,
331         }
332     );
333
334 }
335 elsif ($op eq "set-branch-defaults") {
336     my $categorycode  = $input->param('categorycode');
337     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
338     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
339     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
340     my $holdallowed   = $input->param('holdallowed');
341     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
342     my $returnbranch  = $input->param('returnbranch');
343     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
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     if ($branch eq "*") {
467         if ($itemtype eq "*") {
468             Koha::CirculationRules->set_rules(
469                 {
470                     itemtype     => undef,
471                     branchcode   => undef,
472                     rules        => {
473                         holdallowed             => $holdallowed,
474                         hold_fulfillment_policy => $hold_fulfillment_policy,
475                         returnbranch            => $returnbranch,
476                     }
477                 }
478             );
479         } else {
480             Koha::CirculationRules->set_rules(
481                 {
482                     itemtype     => $itemtype,
483                     branchcode   => undef,
484                     rules        => {
485                         holdallowed             => $holdallowed,
486                         hold_fulfillment_policy => $hold_fulfillment_policy,
487                         returnbranch            => $returnbranch,
488                     }
489                 }
490             );
491         }
492     } elsif ($itemtype eq "*") {
493             Koha::CirculationRules->set_rules(
494                 {
495                     itemtype     => undef,
496                     branchcode   => $branch,
497                     rules        => {
498                         holdallowed             => $holdallowed,
499                         hold_fulfillment_policy => $hold_fulfillment_policy,
500                         returnbranch            => $returnbranch,
501                     }
502                 }
503             );
504     } else {
505         Koha::CirculationRules->set_rules(
506             {
507                 itemtype     => $itemtype,
508                 branchcode   => $branch,
509                 rules        => {
510                     holdallowed             => $holdallowed,
511                     hold_fulfillment_policy => $hold_fulfillment_policy,
512                     returnbranch            => $returnbranch,
513                 }
514             }
515         );
516     }
517 }
518 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
519
520     my $lostreturn = $input->param('lostreturn');
521
522     if ( $lostreturn eq '*' ) {
523         if ( $branch ne '*' ) {
524             # only do something for $lostreturn eq '*' if branch-specific
525             Koha::CirculationRules->set_rules(
526                 {
527                     branchcode   => $branch,
528                     rules        => {
529                         lostreturn => undef
530                     }
531                 }
532             );
533         }
534     } else {
535         Koha::CirculationRules->set_rules(
536             {
537                 branchcode   => $branch,
538                 rules        => {
539                     lostreturn => $lostreturn
540                 }
541             }
542         );
543     }
544 }
545
546 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
547 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
548 $template->param(
549     refundLostItemFeeRule => $refundLostItemFeeRule,
550     defaultRefundRule     => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
551 );
552
553 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
554
555 my $itemtypes = Koha::ItemTypes->search_with_localization;
556
557 my $humanbranch = ( $branch ne '*' ? $branch : undef );
558
559 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
560 my $definedbranch = $all_rules->count ? 1 : 0;
561
562 my $rules = {};
563 while ( my $r = $all_rules->next ) {
564     $r = $r->unblessed;
565     $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
566 }
567
568 $template->param(show_branch_cat_rule_form => 1);
569
570 $template->param(
571     patron_categories => $patron_categories,
572     itemtypeloop      => $itemtypes,
573     humanbranch       => $humanbranch,
574     current_branch    => $branch,
575     definedbranch     => $definedbranch,
576     all_rules         => $rules,
577 );
578 output_html_with_http_headers $input, $cookie, $template->output;
579
580 exit 0;
581
582 # sort by patron category, then item type, putting
583 # default entries at the bottom
584 sub by_category_and_itemtype {
585     unless (by_category($a, $b)) {
586         return by_itemtype($a, $b);
587     }
588 }
589
590 sub by_category {
591     my ($a, $b) = @_;
592     if ($a->{'default_humancategorycode'}) {
593         return ($b->{'default_humancategorycode'} ? 0 : 1);
594     } elsif ($b->{'default_humancategorycode'}) {
595         return -1;
596     } else {
597         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
598     }
599 }
600
601 sub by_itemtype {
602     my ($a, $b) = @_;
603     if ($a->{default_translated_description}) {
604         return ($b->{'default_translated_description'} ? 0 : 1);
605     } elsif ($b->{'default_translated_description'}) {
606         return -1;
607     } else {
608         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
609     }
610 }
611
612 sub strip_non_numeric {
613     my $string = shift;
614     $string =~ s/\s//g;
615     $string = '' if $string !~ /^\d+/;
616     return $string;
617 }