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