2 # Copyright 2000-2002 Katipo Communications
3 # copyright 2010 BibLibre
5 # This file is part of Koha.
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.
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.
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>.
23 use C4::Output qw( output_html_with_http_headers );
24 use C4::Auth qw( get_template_and_user );
25 use Koha::Exceptions::Exception;
26 use Koha::DateUtils qw( dt_from_string output_pref );
30 use Koha::CirculationRules;
31 use Koha::Patron::Categories;
36 my $dbh = C4::Context->dbh;
39 # $flagsrequired->{circulation}=1;
40 my ($template, $loggedinuser, $cookie)
41 = get_template_and_user({template_name => "admin/smart-rules.tt",
44 flagsrequired => {parameters => 'manage_circ_rules'},
47 my $type=$input->param('type');
49 my $branch = $input->param('branch');
51 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
52 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
55 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
59 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
61 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
62 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
63 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
65 my $op = $input->param('op') || q{};
66 my $language = C4::Languages::getlanguage();
68 my $cache = Koha::Caches->get_instance;
69 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
71 if ($op eq 'delete') {
72 my $itemtype = $input->param('itemtype');
73 my $categorycode = $input->param('categorycode');
75 Koha::CirculationRules->set_rules(
77 categorycode => $categorycode eq '*' ? undef : $categorycode,
78 branchcode => $branch eq '*' ? undef : $branch,
79 itemtype => $itemtype eq '*' ? undef : $itemtype,
82 maxonsiteissueqty => undef,
83 rentaldiscount => undef,
86 maxsuspensiondays => undef,
87 suspension_chargeperiod => undef,
89 chargeperiod => undef,
90 chargeperiod_charge_at => undef,
95 hardduedatecompare => undef,
96 renewalsallowed => undef,
97 unseen_renewals_allowed => undef,
98 renewalperiod => undef,
99 norenewalbefore => undef,
101 no_auto_renewal_after => undef,
102 no_auto_renewal_after_hard_limit => undef,
103 reservesallowed => undef,
104 holds_per_record => undef,
105 holds_per_day => undef,
106 onshelfholds => undef,
107 opacitemholds => undef,
108 overduefinescap => undef,
109 cap_fine_to_replacement_price => undef,
110 article_requests => undef,
112 decreaseloanholds => undef,
117 elsif ($op eq 'delete-branch-cat') {
118 my $categorycode = $input->param('categorycode');
119 if ($branch eq "*") {
120 if ($categorycode eq "*") {
121 Koha::CirculationRules->set_rules(
124 categorycode => undef,
127 patron_maxissueqty => undef,
128 patron_maxonsiteissueqty => undef,
132 Koha::CirculationRules->set_rules(
137 holdallowed => undef,
138 hold_fulfillment_policy => undef,
139 returnbranch => undef,
144 Koha::CirculationRules->set_rules(
146 categorycode => $categorycode,
150 patron_maxissueqty => undef,
151 patron_maxonsiteissueqty => undef,
156 } elsif ($categorycode eq "*") {
157 Koha::CirculationRules->set_rules(
159 branchcode => $branch,
160 categorycode => undef,
163 patron_maxissueqty => undef,
164 patron_maxonsiteissueqty => undef,
168 Koha::CirculationRules->set_rules(
170 branchcode => $branch,
173 holdallowed => undef,
174 hold_fulfillment_policy => undef,
175 returnbranch => undef,
180 Koha::CirculationRules->set_rules(
182 categorycode => $categorycode,
183 branchcode => $branch,
186 patron_maxissueqty => undef,
187 patron_maxonsiteissueqty => undef,
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(
202 holdallowed => undef,
203 hold_fulfillment_policy => undef,
204 returnbranch => undef,
209 Koha::CirculationRules->set_rules(
212 itemtype => $itemtype,
214 holdallowed => undef,
215 hold_fulfillment_policy => undef,
216 returnbranch => undef,
221 } elsif ($itemtype eq "*") {
222 Koha::CirculationRules->set_rules(
224 branchcode => $branch,
227 holdallowed => undef,
228 hold_fulfillment_policy => undef,
229 returnbranch => undef,
234 Koha::CirculationRules->set_rules(
236 branchcode => $branch,
237 itemtype => $itemtype,
239 holdallowed => undef,
240 hold_fulfillment_policy => undef,
241 returnbranch => undef,
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 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
256 my $firstremind = $input->param('firstremind');
257 my $chargeperiod = $input->param('chargeperiod');
258 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
259 my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
260 my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
261 my $renewalsallowed = $input->param('renewalsallowed');
262 my $unseen_renewals_allowed = $input->param('unseen_renewals_allowed');
263 my $renewalperiod = $input->param('renewalperiod');
264 my $norenewalbefore = $input->param('norenewalbefore');
265 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
266 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
267 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
268 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
269 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
270 $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
271 $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 );
272 my $reservesallowed = strip_non_numeric( scalar $input->param('reservesallowed') );
273 my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
274 my $holds_per_day = strip_non_numeric( scalar $input->param('holds_per_day') );
275 my $onshelfholds = $input->param('onshelfholds') || 0;
276 my $issuelength = $input->param('issuelength');
277 $issuelength = $issuelength eq q{} ? undef : $issuelength;
278 my $daysmode = $input->param('daysmode');
279 my $lengthunit = $input->param('lengthunit');
280 my $hardduedate = $input->param('hardduedate') || undef;
281 $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
282 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
283 my $hardduedatecompare = $input->param('hardduedatecompare');
284 my $rentaldiscount = $input->param('rentaldiscount');
285 my $opacitemholds = $input->param('opacitemholds') || 0;
286 my $article_requests = $input->param('article_requests') || 'no';
287 my $overduefinescap = $input->param('overduefinescap') || '';
288 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
289 my $note = $input->param('note');
290 my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
293 maxissueqty => $maxissueqty,
294 maxonsiteissueqty => $maxonsiteissueqty,
295 rentaldiscount => $rentaldiscount,
297 finedays => $finedays,
298 maxsuspensiondays => $maxsuspensiondays,
299 suspension_chargeperiod => $suspension_chargeperiod,
300 firstremind => $firstremind,
301 chargeperiod => $chargeperiod,
302 chargeperiod_charge_at => $chargeperiod_charge_at,
303 issuelength => $issuelength,
304 daysmode => $daysmode,
305 lengthunit => $lengthunit,
306 hardduedate => $hardduedate,
307 hardduedatecompare => $hardduedatecompare,
308 renewalsallowed => $renewalsallowed,
309 unseen_renewals_allowed => $unseen_renewals_allowed,
310 renewalperiod => $renewalperiod,
311 norenewalbefore => $norenewalbefore,
312 auto_renew => $auto_renew,
313 no_auto_renewal_after => $no_auto_renewal_after,
314 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
315 reservesallowed => $reservesallowed,
316 holds_per_record => $holds_per_record,
317 holds_per_day => $holds_per_day,
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,
324 decreaseloanholds => $decreaseloanholds,
327 Koha::CirculationRules->set_rules(
329 categorycode => $bor eq '*' ? undef : $bor,
330 itemtype => $itemtype eq '*' ? undef : $itemtype,
331 branchcode => $br eq '*' ? undef : $br,
337 elsif ($op eq "set-branch-defaults") {
338 my $categorycode = $input->param('categorycode');
339 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
340 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
341 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
342 my $holdallowed = $input->param('holdallowed');
343 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
344 my $returnbranch = $input->param('returnbranch');
345 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
347 if ($branch eq "*") {
348 Koha::CirculationRules->set_rules(
353 holdallowed => $holdallowed,
354 hold_fulfillment_policy => $hold_fulfillment_policy,
355 returnbranch => $returnbranch,
359 Koha::CirculationRules->set_rules(
361 categorycode => undef,
364 patron_maxissueqty => $patron_maxissueqty,
365 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
370 Koha::CirculationRules->set_rules(
373 branchcode => $branch,
375 holdallowed => $holdallowed,
376 hold_fulfillment_policy => $hold_fulfillment_policy,
377 returnbranch => $returnbranch,
381 Koha::CirculationRules->set_rules(
383 categorycode => undef,
384 branchcode => $branch,
386 patron_maxissueqty => $patron_maxissueqty,
387 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
392 Koha::CirculationRules->set_rule(
394 branchcode => $branch,
395 categorycode => undef,
396 rule_name => 'max_holds',
397 rule_value => $max_holds,
401 elsif ($op eq "add-branch-cat") {
402 my $categorycode = $input->param('categorycode');
403 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
404 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
405 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
406 my $max_holds = $input->param('max_holds');
407 $max_holds =~ s/\s//g;
408 $max_holds = undef if $max_holds !~ /^\d+/;
410 if ($branch eq "*") {
411 if ($categorycode eq "*") {
412 Koha::CirculationRules->set_rules(
414 categorycode => undef,
417 max_holds => $max_holds,
418 patron_maxissueqty => $patron_maxissueqty,
419 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
424 Koha::CirculationRules->set_rules(
426 categorycode => $categorycode,
429 max_holds => $max_holds,
430 patron_maxissueqty => $patron_maxissueqty,
431 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
436 } elsif ($categorycode eq "*") {
437 Koha::CirculationRules->set_rules(
439 categorycode => undef,
440 branchcode => $branch,
442 max_holds => $max_holds,
443 patron_maxissueqty => $patron_maxissueqty,
444 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
449 Koha::CirculationRules->set_rules(
451 categorycode => $categorycode,
452 branchcode => $branch,
454 max_holds => $max_holds,
455 patron_maxissueqty => $patron_maxissueqty,
456 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
462 elsif ( $op eq "add-open-article-requests-limit" ) {
463 my $categorycode = $input->param('categorycode');
464 my $open_article_requests_limit = strip_non_numeric( scalar $input->param('open_article_requests_limit') );
466 Koha::Exceptions::Exception->throw("No value passed for article request limit")
467 if not defined $open_article_requests_limit # There is a JS check for that
468 || $open_article_requests_limit eq '';
470 if ( $branch eq "*" ) {
471 if ( $categorycode eq "*" ) {
472 Koha::CirculationRules->set_rules(
473 { categorycode => undef,
475 rules => { open_article_requests_limit => $open_article_requests_limit, }
479 Koha::CirculationRules->set_rules(
480 { categorycode => $categorycode,
482 rules => { open_article_requests_limit => $open_article_requests_limit, }
486 } elsif ( $categorycode eq "*" ) {
487 Koha::CirculationRules->set_rules(
488 { categorycode => undef,
489 branchcode => $branch,
490 rules => { open_article_requests_limit => $open_article_requests_limit, }
494 Koha::CirculationRules->set_rules(
495 { categorycode => $categorycode,
496 branchcode => $branch,
497 rules => { open_article_requests_limit => $open_article_requests_limit, }
501 } elsif ( $op eq 'del-open-article-requests-limit' ) {
502 my $categorycode = $input->param('categorycode');
503 if ( $branch eq "*" ) {
504 if ( $categorycode eq "*" ) {
505 Koha::CirculationRules->set_rules(
506 { branchcode => undef,
507 categorycode => undef,
508 rules => { open_article_requests_limit => undef, }
512 Koha::CirculationRules->set_rules(
513 { categorycode => $categorycode,
515 rules => { open_article_requests_limit => undef, }
519 } elsif ( $categorycode eq "*" ) {
520 Koha::CirculationRules->set_rules(
521 { branchcode => $branch,
522 categorycode => undef,
523 rules => { open_article_requests_limit => undef, }
527 Koha::CirculationRules->set_rules(
528 { categorycode => $categorycode,
529 branchcode => $branch,
530 rules => { open_article_requests_limit => undef, }
535 elsif ($op eq "add-branch-item") {
536 my $itemtype = $input->param('itemtype');
537 my $holdallowed = $input->param('holdallowed');
538 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
539 my $returnbranch = $input->param('returnbranch');
541 if ($branch eq "*") {
542 if ($itemtype eq "*") {
543 Koha::CirculationRules->set_rules(
548 holdallowed => $holdallowed,
549 hold_fulfillment_policy => $hold_fulfillment_policy,
550 returnbranch => $returnbranch,
555 Koha::CirculationRules->set_rules(
557 itemtype => $itemtype,
560 holdallowed => $holdallowed,
561 hold_fulfillment_policy => $hold_fulfillment_policy,
562 returnbranch => $returnbranch,
567 } elsif ($itemtype eq "*") {
568 Koha::CirculationRules->set_rules(
571 branchcode => $branch,
573 holdallowed => $holdallowed,
574 hold_fulfillment_policy => $hold_fulfillment_policy,
575 returnbranch => $returnbranch,
580 Koha::CirculationRules->set_rules(
582 itemtype => $itemtype,
583 branchcode => $branch,
585 holdallowed => $holdallowed,
586 hold_fulfillment_policy => $hold_fulfillment_policy,
587 returnbranch => $returnbranch,
593 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
595 my $lostreturn = $input->param('lostreturn');
597 if ( $lostreturn eq '*' ) {
598 if ( $branch ne '*' ) {
599 # only do something for $lostreturn eq '*' if branch-specific
600 Koha::CirculationRules->set_rules(
602 branchcode => $branch,
610 Koha::CirculationRules->set_rules(
612 branchcode => $branch,
614 lostreturn => $lostreturn
621 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
622 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
624 refundLostItemFeeRule => $refundLostItemFeeRule,
625 defaultRefundRule => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
628 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
630 my $itemtypes = Koha::ItemTypes->search_with_localization;
632 my $humanbranch = ( $branch ne '*' ? $branch : undef );
634 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
635 my $definedbranch = $all_rules->count ? 1 : 0;
638 while ( my $r = $all_rules->next ) {
640 $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
643 $template->param(show_branch_cat_rule_form => 1);
646 patron_categories => $patron_categories,
647 itemtypeloop => $itemtypes,
648 humanbranch => $humanbranch,
649 current_branch => $branch,
650 definedbranch => $definedbranch,
653 output_html_with_http_headers $input, $cookie, $template->output;
657 # sort by patron category, then item type, putting
658 # default entries at the bottom
659 sub by_category_and_itemtype {
660 unless (by_category($a, $b)) {
661 return by_itemtype($a, $b);
667 if ($a->{'default_humancategorycode'}) {
668 return ($b->{'default_humancategorycode'} ? 0 : 1);
669 } elsif ($b->{'default_humancategorycode'}) {
672 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
678 if ($a->{default_translated_description}) {
679 return ($b->{'default_translated_description'} ? 0 : 1);
680 } elsif ($b->{'default_translated_description'}) {
683 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
687 sub strip_non_numeric {
690 $string = '' if $string !~ /^\d+/;