Bug 18936: Convert issuingrules fields to circulation_rules
[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::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,
85             branchcode   => $branch,
86             itemtype     => $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                      categorycode => undef,
125                      branchcode   => undef,
126                      itemtype     => undef,
127                      rules        => {
128                          patron_maxissueqty             => undef,
129                          patron_maxonsiteissueqty       => undef,
130                          holdallowed             => undef,
131                          hold_fulfillment_policy => undef,
132                          returnbranch            => undef,
133                          max_holds               => undef,
134                      }
135                  }
136              );
137          } else {
138              Koha::CirculationRules->set_rules(
139                  {
140                      categorycode => $categorycode,
141                      branchcode   => undef,
142                      itemtype     => undef,
143                      rules        => {
144                          max_holds         => undef,
145                          patron_maxissueqty       => undef,
146                          patron_maxonsiteissueqty => undef,
147                      }
148                  }
149              );
150         }
151     } elsif ($categorycode eq "*") {
152         Koha::CirculationRules->set_rules(
153             {
154                 categorycode => undef,
155                 branchcode   => $branch,
156                 itemtype     => undef,
157                 rules        => {
158                     patron_maxissueqty             => undef,
159                     patron_maxonsiteissueqty       => undef,
160                     holdallowed             => undef,
161                     hold_fulfillment_policy => undef,
162                     returnbranch            => undef,
163                     max_holds               => undef,
164                 }
165             }
166         );
167     } else {
168         Koha::CirculationRules->set_rules(
169             {
170                 categorycode => $categorycode,
171                 branchcode   => $branch,
172                 itemtype     => undef,
173                 rules        => {
174                     max_holds         => undef,
175                     patron_maxissueqty       => undef,
176                     patron_maxonsiteissueqty => undef,
177                 }
178             }
179         );
180     }
181 }
182 elsif ($op eq 'delete-branch-item') {
183     my $itemtype  = $input->param('itemtype');
184     if ($branch eq "*") {
185         if ($itemtype eq "*") {
186             Koha::CirculationRules->set_rules(
187                 {
188                     categorycode => undef,
189                     branchcode   => undef,
190                     itemtype     => undef,
191                     rules        => {
192                         patron_maxissueqty             => undef,
193                         patron_maxonsiteissueqty       => undef,
194                         holdallowed             => undef,
195                         hold_fulfillment_policy => undef,
196                         returnbranch            => undef,
197                     }
198                 }
199             );
200         } else {
201             Koha::CirculationRules->set_rules(
202                 {
203                     categorycode => undef,
204                     branchcode   => undef,
205                     itemtype     => $itemtype,
206                     rules        => {
207                         holdallowed             => undef,
208                         hold_fulfillment_policy => undef,
209                         returnbranch            => undef,
210                     }
211                 }
212             );
213         }
214     } elsif ($itemtype eq "*") {
215         Koha::CirculationRules->set_rules(
216             {
217                 categorycode => undef,
218                 branchcode   => $branch,
219                 itemtype     => undef,
220                 rules        => {
221                     maxissueqty             => undef,
222                     maxonsiteissueqty       => undef,
223                     holdallowed             => undef,
224                     hold_fulfillment_policy => undef,
225                     returnbranch            => undef,
226                 }
227             }
228         );
229     } else {
230         Koha::CirculationRules->set_rules(
231             {
232                 categorycode => undef,
233                 branchcode   => $branch,
234                 itemtype     => $itemtype,
235                 rules        => {
236                     holdallowed             => undef,
237                     hold_fulfillment_policy => undef,
238                     returnbranch            => undef,
239                 }
240             }
241         );
242     }
243 }
244 # save the values entered
245 elsif ($op eq 'add') {
246     my $br = $branch; # branch
247     my $bor  = $input->param('categorycode'); # borrower category
248     my $itemtype  = $input->param('itemtype');     # item type
249     my $fine = $input->param('fine');
250     my $finedays     = $input->param('finedays');
251     my $maxsuspensiondays = $input->param('maxsuspensiondays');
252     $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
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  = $input->param('maxissueqty');
258     my $maxonsiteissueqty  = $input->param('maxonsiteissueqty');
259     my $renewalsallowed  = $input->param('renewalsallowed');
260     my $renewalperiod    = $input->param('renewalperiod');
261     my $norenewalbefore  = $input->param('norenewalbefore');
262     $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
263     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
264     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
265     $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
266     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
267     $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 );
268     $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 );
269     my $reservesallowed  = $input->param('reservesallowed');
270     my $holds_per_record = $input->param('holds_per_record');
271     my $holds_per_day    = $input->param('holds_per_day');
272     $holds_per_day =~ s/\s//g;
273     $holds_per_day = undef if $holds_per_day !~ /^\d+/;
274     my $onshelfholds     = $input->param('onshelfholds') || 0;
275     $maxissueqty =~ s/\s//g;
276     $maxissueqty = '' if $maxissueqty !~ /^\d+/;
277     $maxonsiteissueqty =~ s/\s//g;
278     $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
279     my $issuelength  = $input->param('issuelength');
280     $issuelength = $issuelength eq q{} ? undef : $issuelength;
281     my $lengthunit  = $input->param('lengthunit');
282     my $hardduedate = $input->param('hardduedate') || undef;
283     $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
284     $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
285     my $hardduedatecompare = $input->param('hardduedatecompare');
286     my $rentaldiscount = $input->param('rentaldiscount');
287     my $opacitemholds = $input->param('opacitemholds') || 0;
288     my $article_requests = $input->param('article_requests') || 'no';
289     my $overduefinescap = $input->param('overduefinescap') || undef;
290     my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
291     my $note = $input->param('note');
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         fine                          => $fine,
298         finedays                      => $finedays,
299         maxsuspensiondays             => $maxsuspensiondays,
300         suspension_chargeperiod       => $suspension_chargeperiod,
301         firstremind                   => $firstremind,
302         chargeperiod                  => $chargeperiod,
303         chargeperiod_charge_at        => $chargeperiod_charge_at,
304         renewalsallowed               => $renewalsallowed,
305         renewalperiod                 => $renewalperiod,
306         norenewalbefore               => $norenewalbefore,
307         auto_renew                    => $auto_renew,
308         no_auto_renewal_after         => $no_auto_renewal_after,
309         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
310         reservesallowed               => $reservesallowed,
311         holds_per_record              => $holds_per_record,
312         holds_per_day                 => $holds_per_day,
313         issuelength                   => $issuelength,
314         lengthunit                    => $lengthunit,
315         hardduedate                   => $hardduedate,
316         hardduedatecompare            => $hardduedatecompare,
317         rentaldiscount                => $rentaldiscount,
318         onshelfholds                  => $onshelfholds,
319         opacitemholds                 => $opacitemholds,
320         overduefinescap               => $overduefinescap,
321         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
322         article_requests              => $article_requests,
323         note                          => $note,
324     };
325
326     Koha::CirculationRules->set_rules(
327         {
328             categorycode => $bor,
329             itemtype     => $itemtype,
330             branchcode   => $br,
331             rules        => $rules,
332         }
333     );
334
335 }
336 elsif ($op eq "set-branch-defaults") {
337     my $categorycode  = $input->param('categorycode');
338     my $patron_maxissueqty   = $input->param('patron_maxissueqty');
339     my $patron_maxonsiteissueqty = $input->param('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 = $input->param('max_holds');
344     $patron_maxissueqty =~ s/\s//g;
345     $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
346     $patron_maxonsiteissueqty =~ s/\s//g;
347     $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
348     $holdallowed =~ s/\s//g;
349     $holdallowed = undef if $holdallowed !~ /^\d+/;
350     $max_holds =~ s/\s//g;
351     $max_holds = '' if $max_holds !~ /^\d+/;
352
353     if ($branch eq "*") {
354         Koha::CirculationRules->set_rules(
355             {
356                 categorycode => undef,
357                 itemtype     => undef,
358                 branchcode   => undef,
359                 rules        => {
360                     patron_maxissueqty       => $patron_maxissueqty,
361                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
362                     holdallowed              => $holdallowed,
363                     hold_fulfillment_policy  => $hold_fulfillment_policy,
364                     returnbranch             => $returnbranch,
365                 }
366             }
367         );
368     } else {
369         Koha::CirculationRules->set_rules(
370             {
371                 categorycode => undef,
372                 itemtype     => undef,
373                 branchcode   => $branch,
374                 rules        => {
375                     patron_maxissueqty       => $patron_maxissueqty,
376                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
377                     holdallowed              => $holdallowed,
378                     hold_fulfillment_policy  => $hold_fulfillment_policy,
379                     returnbranch             => $returnbranch,
380                 }
381             }
382         );
383     }
384     Koha::CirculationRules->set_rule(
385         {
386             branchcode   => $branch,
387             categorycode => undef,
388             itemtype     => undef,
389             rule_name    => 'max_holds',
390             rule_value   => $max_holds,
391         }
392     );
393 }
394 elsif ($op eq "add-branch-cat") {
395     my $categorycode  = $input->param('categorycode');
396     my $patron_maxissueqty   = $input->param('patron_maxissueqty');
397     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
398     my $max_holds = $input->param('max_holds');
399     $patron_maxissueqty =~ s/\s//g;
400     $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
401     $patron_maxonsiteissueqty =~ s/\s//g;
402     $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
403     $max_holds =~ s/\s//g;
404     $max_holds = undef if $max_holds !~ /^\d+/;
405
406     if ($branch eq "*") {
407         if ($categorycode eq "*") {
408             Koha::CirculationRules->set_rules(
409                 {
410                     categorycode => undef,
411                     itemtype     => undef,
412                     branchcode   => undef,
413                     rules        => {
414                         max_holds         => $max_holds,
415                         patron_maxissueqty       => $patron_maxissueqty,
416                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
417                     }
418                 }
419             );
420         } else {
421             Koha::CirculationRules->set_rules(
422                 {
423                     branchcode   => undef,
424                     categorycode => $categorycode,
425                     itemtype     => 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                 itemtype     => undef,
439                 branchcode   => $branch,
440                 rules        => {
441                     max_holds         => $max_holds,
442                     patron_maxissueqty       => $patron_maxissueqty,
443                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
444                 }
445             }
446         );
447     } else {
448         Koha::CirculationRules->set_rules(
449             {
450                 categorycode => $categorycode,
451                 itemtype     => undef,
452                 branchcode   => $branch,
453                 rules        => {
454                     max_holds         => $max_holds,
455                     patron_maxissueqty       => $patron_maxissueqty,
456                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
457                 }
458             }
459         );
460     }
461 }
462 elsif ($op eq "add-branch-item") {
463     my $itemtype                = $input->param('itemtype');
464     my $holdallowed             = $input->param('holdallowed');
465     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
466     my $returnbranch            = $input->param('returnbranch');
467
468     $holdallowed =~ s/\s//g;
469     $holdallowed = undef if $holdallowed !~ /^\d+/;
470
471     if ($branch eq "*") {
472         if ($itemtype eq "*") {
473             Koha::CirculationRules->set_rules(
474                 {
475                     categorycode => undef,
476                     itemtype     => undef,
477                     branchcode   => undef,
478                     rules        => {
479                         holdallowed             => $holdallowed,
480                         hold_fulfillment_policy => $hold_fulfillment_policy,
481                         returnbranch            => $returnbranch,
482                     }
483                 }
484             );
485         } else {
486             Koha::CirculationRules->set_rules(
487                 {
488                     categorycode => undef,
489                     itemtype     => $itemtype,
490                     branchcode   => undef,
491                     rules        => {
492                         holdallowed             => $holdallowed,
493                         hold_fulfillment_policy => $hold_fulfillment_policy,
494                         returnbranch            => $returnbranch,
495                     }
496                 }
497             );
498         }
499     } elsif ($itemtype eq "*") {
500             Koha::CirculationRules->set_rules(
501                 {
502                     categorycode => undef,
503                     itemtype     => undef,
504                     branchcode   => $branch,
505                     rules        => {
506                         holdallowed             => $holdallowed,
507                         hold_fulfillment_policy => $hold_fulfillment_policy,
508                         returnbranch            => $returnbranch,
509                     }
510                 }
511             );
512     } else {
513         Koha::CirculationRules->set_rules(
514             {
515                 categorycode => undef,
516                 itemtype     => $itemtype,
517                 branchcode   => $branch,
518                 rules        => {
519                     holdallowed             => $holdallowed,
520                     hold_fulfillment_policy => $hold_fulfillment_policy,
521                     returnbranch            => $returnbranch,
522                 }
523             }
524         );
525     }
526 }
527 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
528
529     my $refund = $input->param('refund');
530
531     if ( $refund eq '*' ) {
532         if ( $branch ne '*' ) {
533             # only do something for $refund eq '*' if branch-specific
534             Koha::CirculationRules->set_rules(
535                 {
536                     categorycode => undef,
537                     itemtype     => undef,
538                     branchcode   => $branch,
539                     rules        => {
540                         refund => undef
541                     }
542                 }
543             );
544         }
545     } else {
546         Koha::CirculationRules->set_rules(
547             {
548                 categorycode => undef,
549                 itemtype     => undef,
550                 branchcode   => $branch,
551                 rules        => {
552                     refund => $refund
553                 }
554             }
555         );
556     }
557 }
558
559 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef:$branch });
560
561 $template->param(
562     refundLostItemFeeRule => $refundLostItemFeeRule,
563     defaultRefundRule     => Koha::RefundLostItemFeeRules->_default_rule
564 );
565
566 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
567
568 my $itemtypes = Koha::ItemTypes->search_with_localization;
569
570 $template->param(show_branch_cat_rule_form => 1);
571
572 $template->param(
573     patron_categories => $patron_categories,
574     itemtypeloop      => $itemtypes,
575     humanbranch       => ( $branch ne '*' ? $branch : '' ),
576     current_branch    => $branch,
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 }