Bug 24083: Add circ rules management
[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::Libraries;
31 use Koha::CirculationRules;
32 use Koha::Patron::Categories;
33 use Koha::Caches;
34 use Koha::Patrons;
35
36 my $input = CGI->new;
37 my $dbh = C4::Context->dbh;
38
39 # my $flagsrequired;
40 # $flagsrequired->{circulation}=1;
41 my ($template, $loggedinuser, $cookie)
42     = get_template_and_user({template_name => "admin/smart-rules.tt",
43                             query => $input,
44                             type => "intranet",
45                             flagsrequired => {parameters => 'manage_circ_rules'},
46                             debug => 1,
47                             });
48
49 my $type=$input->param('type');
50
51 my $branch = $input->param('branch');
52 unless ( $branch ) {
53     if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
54         $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
55     }
56     else {
57         $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
58     }
59 }
60
61 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
62
63 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
64 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
65 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
66
67 my $op = $input->param('op') || q{};
68 my $language = C4::Languages::getlanguage();
69
70 my $cache = Koha::Caches->get_instance;
71 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
72
73 if ($op eq 'delete') {
74     my $itemtype     = $input->param('itemtype');
75     my $categorycode = $input->param('categorycode');
76     $debug and warn "deleting $1 $2 $branch";
77
78     Koha::CirculationRules->set_rules(
79         {
80             categorycode => $categorycode eq '*' ? undef : $categorycode,
81             branchcode   => $branch eq '*' ? undef : $branch,
82             itemtype     => $itemtype eq '*' ? undef : $itemtype,
83             rules        => {
84                 maxissueqty                      => undef,
85                 maxonsiteissueqty                => undef,
86                 rentaldiscount                   => undef,
87                 fine                             => undef,
88                 finedays                         => undef,
89                 maxsuspensiondays                => undef,
90                 suspension_chargeperiod          => undef,
91                 firstremind                      => undef,
92                 chargeperiod                     => undef,
93                 chargeperiod_charge_at           => undef,
94                 issuelength                      => undef,
95                 daysmode                         => 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 $unseen_renewals_allowed  = $input->param('unseen_renewals_allowed');
264     my $renewalperiod    = $input->param('renewalperiod');
265     my $norenewalbefore  = $input->param('norenewalbefore');
266     $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
267     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
268     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
269     $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
270     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
271     $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
272     $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 );
273     my $reservesallowed  = strip_non_numeric( scalar $input->param('reservesallowed') );
274     my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
275     my $holds_per_day    = strip_non_numeric( scalar $input->param('holds_per_day') );
276     my $onshelfholds     = $input->param('onshelfholds') || 0;
277     my $issuelength  = $input->param('issuelength');
278     $issuelength = $issuelength eq q{} ? undef : $issuelength;
279     my $daysmode = $input->param('daysmode');
280     my $lengthunit  = $input->param('lengthunit');
281     my $hardduedate = $input->param('hardduedate') || undef;
282     $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
283     $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
284     my $hardduedatecompare = $input->param('hardduedatecompare');
285     my $rentaldiscount = $input->param('rentaldiscount');
286     my $opacitemholds = $input->param('opacitemholds') || 0;
287     my $article_requests = $input->param('article_requests') || 'no';
288     my $overduefinescap = $input->param('overduefinescap') || '';
289     my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
290     my $note = $input->param('note');
291     my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
292     $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
293
294     my $rules = {
295         maxissueqty                   => $maxissueqty,
296         maxonsiteissueqty             => $maxonsiteissueqty,
297         rentaldiscount                => $rentaldiscount,
298         fine                          => $fine,
299         finedays                      => $finedays,
300         maxsuspensiondays             => $maxsuspensiondays,
301         suspension_chargeperiod       => $suspension_chargeperiod,
302         firstremind                   => $firstremind,
303         chargeperiod                  => $chargeperiod,
304         chargeperiod_charge_at        => $chargeperiod_charge_at,
305         issuelength                   => $issuelength,
306         daysmode                      => $daysmode,
307         lengthunit                    => $lengthunit,
308         hardduedate                   => $hardduedate,
309         hardduedatecompare            => $hardduedatecompare,
310         renewalsallowed               => $renewalsallowed,
311         unseen_renewals_allowed       => $unseen_renewals_allowed,
312         renewalperiod                 => $renewalperiod,
313         norenewalbefore               => $norenewalbefore,
314         auto_renew                    => $auto_renew,
315         no_auto_renewal_after         => $no_auto_renewal_after,
316         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
317         reservesallowed               => $reservesallowed,
318         holds_per_record              => $holds_per_record,
319         holds_per_day                 => $holds_per_day,
320         onshelfholds                  => $onshelfholds,
321         opacitemholds                 => $opacitemholds,
322         overduefinescap               => $overduefinescap,
323         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
324         article_requests              => $article_requests,
325         note                          => $note,
326         decreaseloanholds             => $decreaseloanholds,
327     };
328
329     Koha::CirculationRules->set_rules(
330         {
331             categorycode => $bor eq '*' ? undef : $bor,
332             itemtype     => $itemtype eq '*' ? undef : $itemtype,
333             branchcode   => $br eq '*' ? undef : $br,
334             rules        => $rules,
335         }
336     );
337
338 }
339 elsif ($op eq "set-branch-defaults") {
340     my $categorycode  = $input->param('categorycode');
341     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
342     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
343     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
344     my $holdallowed   = $input->param('holdallowed');
345     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
346     my $returnbranch  = $input->param('returnbranch');
347     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
348
349     if ($branch eq "*") {
350         Koha::CirculationRules->set_rules(
351             {
352                 itemtype     => undef,
353                 branchcode   => undef,
354                 rules        => {
355                     holdallowed             => $holdallowed,
356                     hold_fulfillment_policy => $hold_fulfillment_policy,
357                     returnbranch            => $returnbranch,
358                 }
359             }
360         );
361         Koha::CirculationRules->set_rules(
362             {
363                 categorycode => undef,
364                 branchcode   => undef,
365                 rules        => {
366                     patron_maxissueqty             => $patron_maxissueqty,
367                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
368                 }
369             }
370         );
371     } else {
372         Koha::CirculationRules->set_rules(
373             {
374                 itemtype     => undef,
375                 branchcode   => $branch,
376                 rules        => {
377                     holdallowed             => $holdallowed,
378                     hold_fulfillment_policy => $hold_fulfillment_policy,
379                     returnbranch            => $returnbranch,
380                 }
381             }
382         );
383         Koha::CirculationRules->set_rules(
384             {
385                 categorycode => undef,
386                 branchcode   => $branch,
387                 rules        => {
388                     patron_maxissueqty             => $patron_maxissueqty,
389                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
390                 }
391             }
392         );
393     }
394     Koha::CirculationRules->set_rule(
395         {
396             branchcode   => $branch,
397             categorycode => undef,
398             rule_name    => 'max_holds',
399             rule_value   => $max_holds,
400         }
401     );
402 }
403 elsif ($op eq "add-branch-cat") {
404     my $categorycode  = $input->param('categorycode');
405     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
406     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
407     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
408     my $max_holds = $input->param('max_holds');
409     $max_holds =~ s/\s//g;
410     $max_holds = undef if $max_holds !~ /^\d+/;
411
412     if ($branch eq "*") {
413         if ($categorycode eq "*") {
414             Koha::CirculationRules->set_rules(
415                 {
416                     categorycode => undef,
417                     branchcode   => undef,
418                     rules        => {
419                         max_holds         => $max_holds,
420                         patron_maxissueqty       => $patron_maxissueqty,
421                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
422                     }
423                 }
424             );
425         } else {
426             Koha::CirculationRules->set_rules(
427                 {
428                     categorycode => $categorycode,
429                     branchcode   => undef,
430                     rules        => {
431                         max_holds         => $max_holds,
432                         patron_maxissueqty       => $patron_maxissueqty,
433                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
434                     }
435                 }
436             );
437         }
438     } elsif ($categorycode eq "*") {
439         Koha::CirculationRules->set_rules(
440             {
441                 categorycode => undef,
442                 branchcode   => $branch,
443                 rules        => {
444                     max_holds         => $max_holds,
445                     patron_maxissueqty       => $patron_maxissueqty,
446                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
447                 }
448             }
449         );
450     } else {
451         Koha::CirculationRules->set_rules(
452             {
453                 categorycode => $categorycode,
454                 branchcode   => $branch,
455                 rules        => {
456                     max_holds         => $max_holds,
457                     patron_maxissueqty       => $patron_maxissueqty,
458                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
459                 }
460             }
461         );
462     }
463 }
464 elsif ($op eq "add-branch-item") {
465     my $itemtype                = $input->param('itemtype');
466     my $holdallowed             = $input->param('holdallowed');
467     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
468     my $returnbranch            = $input->param('returnbranch');
469
470     if ($branch eq "*") {
471         if ($itemtype eq "*") {
472             Koha::CirculationRules->set_rules(
473                 {
474                     itemtype     => undef,
475                     branchcode   => undef,
476                     rules        => {
477                         holdallowed             => $holdallowed,
478                         hold_fulfillment_policy => $hold_fulfillment_policy,
479                         returnbranch            => $returnbranch,
480                     }
481                 }
482             );
483         } else {
484             Koha::CirculationRules->set_rules(
485                 {
486                     itemtype     => $itemtype,
487                     branchcode   => undef,
488                     rules        => {
489                         holdallowed             => $holdallowed,
490                         hold_fulfillment_policy => $hold_fulfillment_policy,
491                         returnbranch            => $returnbranch,
492                     }
493                 }
494             );
495         }
496     } elsif ($itemtype eq "*") {
497             Koha::CirculationRules->set_rules(
498                 {
499                     itemtype     => undef,
500                     branchcode   => $branch,
501                     rules        => {
502                         holdallowed             => $holdallowed,
503                         hold_fulfillment_policy => $hold_fulfillment_policy,
504                         returnbranch            => $returnbranch,
505                     }
506                 }
507             );
508     } else {
509         Koha::CirculationRules->set_rules(
510             {
511                 itemtype     => $itemtype,
512                 branchcode   => $branch,
513                 rules        => {
514                     holdallowed             => $holdallowed,
515                     hold_fulfillment_policy => $hold_fulfillment_policy,
516                     returnbranch            => $returnbranch,
517                 }
518             }
519         );
520     }
521 }
522 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
523
524     my $lostreturn = $input->param('lostreturn');
525
526     if ( $lostreturn eq '*' ) {
527         if ( $branch ne '*' ) {
528             # only do something for $lostreturn eq '*' if branch-specific
529             Koha::CirculationRules->set_rules(
530                 {
531                     branchcode   => $branch,
532                     rules        => {
533                         lostreturn => undef
534                     }
535                 }
536             );
537         }
538     } else {
539         Koha::CirculationRules->set_rules(
540             {
541                 branchcode   => $branch,
542                 rules        => {
543                     lostreturn => $lostreturn
544                 }
545             }
546         );
547     }
548 }
549
550 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
551 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
552 $template->param(
553     refundLostItemFeeRule => $refundLostItemFeeRule,
554     defaultRefundRule     => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
555 );
556
557 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
558
559 my $itemtypes = Koha::ItemTypes->search_with_localization;
560
561 my $humanbranch = ( $branch ne '*' ? $branch : undef );
562
563 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
564 my $definedbranch = $all_rules->count ? 1 : 0;
565
566 my $rules = {};
567 while ( my $r = $all_rules->next ) {
568     $r = $r->unblessed;
569     $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
570 }
571
572 $template->param(show_branch_cat_rule_form => 1);
573
574 $template->param(
575     patron_categories => $patron_categories,
576     itemtypeloop      => $itemtypes,
577     humanbranch       => $humanbranch,
578     current_branch    => $branch,
579     definedbranch     => $definedbranch,
580     all_rules         => $rules,
581 );
582 output_html_with_http_headers $input, $cookie, $template->output;
583
584 exit 0;
585
586 # sort by patron category, then item type, putting
587 # default entries at the bottom
588 sub by_category_and_itemtype {
589     unless (by_category($a, $b)) {
590         return by_itemtype($a, $b);
591     }
592 }
593
594 sub by_category {
595     my ($a, $b) = @_;
596     if ($a->{'default_humancategorycode'}) {
597         return ($b->{'default_humancategorycode'} ? 0 : 1);
598     } elsif ($b->{'default_humancategorycode'}) {
599         return -1;
600     } else {
601         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
602     }
603 }
604
605 sub by_itemtype {
606     my ($a, $b) = @_;
607     if ($a->{default_translated_description}) {
608         return ($b->{'default_translated_description'} ? 0 : 1);
609     } elsif ($b->{'default_translated_description'}) {
610         return -1;
611     } else {
612         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
613     }
614 }
615
616 sub strip_non_numeric {
617     my $string = shift;
618     $string =~ s/\s//g;
619     $string = '' if $string !~ /^\d+/;
620     return $string;
621 }