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