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 );
29 use Koha::CirculationRules;
30 use Koha::Patron::Categories;
35 my $dbh = C4::Context->dbh;
38 # $flagsrequired->{circulation}=1;
39 my ($template, $loggedinuser, $cookie)
40 = get_template_and_user({template_name => "admin/smart-rules.tt",
43 flagsrequired => {parameters => 'manage_circ_rules'},
46 my $type=$input->param('type');
48 my $branch = $input->param('branch');
50 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
51 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
54 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
58 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
60 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
61 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
62 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
64 my $op = $input->param('op') || q{};
65 my $language = C4::Languages::getlanguage();
67 my $cache = Koha::Caches->get_instance;
68 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
70 if ($op eq 'delete') {
71 my $itemtype = $input->param('itemtype');
72 my $categorycode = $input->param('categorycode');
74 Koha::CirculationRules->set_rules(
76 categorycode => $categorycode eq '*' ? undef : $categorycode,
77 branchcode => $branch eq '*' ? undef : $branch,
78 itemtype => $itemtype eq '*' ? undef : $itemtype,
81 maxonsiteissueqty => undef,
82 rentaldiscount => undef,
85 maxsuspensiondays => undef,
86 suspension_chargeperiod => undef,
88 chargeperiod => undef,
89 chargeperiod_charge_at => undef,
94 hardduedatecompare => undef,
95 renewalsallowed => undef,
96 unseen_renewals_allowed => undef,
97 renewalperiod => undef,
98 norenewalbefore => undef,
99 noautorenewalbefore => 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 recalls_allowed => undef,
113 recalls_per_record => undef,
114 on_shelf_recalls => undef,
115 recall_due_date_interval => undef,
116 recall_overdue_fine => undef,
117 recall_shelf_time => undef,
118 decreaseloanholds => undef,
123 elsif ($op eq 'delete-branch-cat') {
124 my $categorycode = $input->param('categorycode');
125 if ($branch eq "*") {
126 if ($categorycode eq "*") {
127 Koha::CirculationRules->set_rules(
130 categorycode => undef,
133 patron_maxissueqty => undef,
134 patron_maxonsiteissueqty => undef,
138 Koha::CirculationRules->set_rules(
143 holdallowed => undef,
144 hold_fulfillment_policy => undef,
145 returnbranch => undef,
150 Koha::CirculationRules->set_rules(
152 categorycode => $categorycode,
156 patron_maxissueqty => undef,
157 patron_maxonsiteissueqty => undef,
162 } elsif ($categorycode eq "*") {
163 Koha::CirculationRules->set_rules(
165 branchcode => $branch,
166 categorycode => undef,
169 patron_maxissueqty => undef,
170 patron_maxonsiteissueqty => undef,
174 Koha::CirculationRules->set_rules(
176 branchcode => $branch,
179 holdallowed => undef,
180 hold_fulfillment_policy => undef,
181 returnbranch => undef,
186 Koha::CirculationRules->set_rules(
188 categorycode => $categorycode,
189 branchcode => $branch,
192 patron_maxissueqty => undef,
193 patron_maxonsiteissueqty => undef,
199 elsif ($op eq 'delete-branch-item') {
200 my $itemtype = $input->param('itemtype');
201 if ($branch eq "*") {
202 if ($itemtype eq "*") {
203 Koha::CirculationRules->set_rules(
208 holdallowed => undef,
209 hold_fulfillment_policy => undef,
210 returnbranch => undef,
215 Koha::CirculationRules->set_rules(
218 itemtype => $itemtype,
220 holdallowed => undef,
221 hold_fulfillment_policy => undef,
222 returnbranch => undef,
227 } elsif ($itemtype eq "*") {
228 Koha::CirculationRules->set_rules(
230 branchcode => $branch,
233 holdallowed => undef,
234 hold_fulfillment_policy => undef,
235 returnbranch => undef,
240 Koha::CirculationRules->set_rules(
242 branchcode => $branch,
243 itemtype => $itemtype,
245 holdallowed => undef,
246 hold_fulfillment_policy => undef,
247 returnbranch => undef,
253 # save the values entered
254 elsif ($op eq 'add') {
255 my $br = $branch; # branch
256 my $bor = $input->param('categorycode'); # borrower category
257 my $itemtype = $input->param('itemtype'); # item type
258 my $fine = $input->param('fine');
259 my $finedays = $input->param('finedays');
260 my $maxsuspensiondays = $input->param('maxsuspensiondays') || q{};
261 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
262 my $firstremind = $input->param('firstremind');
263 my $chargeperiod = $input->param('chargeperiod');
264 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
265 my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
266 my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
267 my $renewalsallowed = $input->param('renewalsallowed');
268 my $unseen_renewals_allowed = defined $input->param('unseen_renewals_allowed') ? strip_non_numeric( scalar $input->param('unseen_renewals_allowed') ) : q{};
269 my $renewalperiod = $input->param('renewalperiod');
270 my $norenewalbefore = $input->param('norenewalbefore');
271 $norenewalbefore = q{} if $norenewalbefore =~ /^\s*$/;
272 my $noautorenewalbefore = $input->param('noautorenewalbefore');
273 $noautorenewalbefore = q{} if $noautorenewalbefore =~ /^\s*$/;
274 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
275 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
276 $no_auto_renewal_after = q{} if $no_auto_renewal_after =~ /^\s*$/;
277 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || q{};
278 my $reservesallowed = strip_non_numeric( scalar $input->param('reservesallowed') );
279 my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
280 my $holds_per_day = strip_non_numeric( scalar $input->param('holds_per_day') );
281 my $onshelfholds = $input->param('onshelfholds') || 0;
282 my $issuelength = $input->param('issuelength') || 0;
283 my $daysmode = $input->param('daysmode');
284 my $lengthunit = $input->param('lengthunit');
285 my $hardduedate = $input->param('hardduedate') || q{};
286 my $hardduedatecompare = $input->param('hardduedatecompare');
287 my $rentaldiscount = $input->param('rentaldiscount') || 0;
288 my $opacitemholds = $input->param('opacitemholds') || 0;
289 my $article_requests = $input->param('article_requests') || 'no';
290 my $overduefinescap = $input->param('overduefinescap')
291 && ( $input->param('overduefinescap') + 0 ) > 0 ? sprintf( "%.02f", $input->param('overduefinescap') ) : q{};
292 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || q{}) eq 'on';
293 my $note = $input->param('note');
294 my $decreaseloanholds = $input->param('decreaseloanholds') || q{};
295 my $recalls_allowed = $input->param('recalls_allowed');
296 my $recalls_per_record = $input->param('recalls_per_record');
297 my $on_shelf_recalls = $input->param('on_shelf_recalls');
298 my $recall_due_date_interval = $input->param('recall_due_date_interval');
299 my $recall_overdue_fine = $input->param('recall_overdue_fine');
300 my $recall_shelf_time = $input->param('recall_shelf_time');
303 maxissueqty => $maxissueqty,
304 maxonsiteissueqty => $maxonsiteissueqty,
305 rentaldiscount => $rentaldiscount,
307 finedays => $finedays,
308 maxsuspensiondays => $maxsuspensiondays,
309 suspension_chargeperiod => $suspension_chargeperiod,
310 firstremind => $firstremind,
311 chargeperiod => $chargeperiod,
312 chargeperiod_charge_at => $chargeperiod_charge_at,
313 issuelength => $issuelength,
314 daysmode => $daysmode,
315 lengthunit => $lengthunit,
316 hardduedate => $hardduedate,
317 hardduedatecompare => $hardduedatecompare,
318 renewalsallowed => $renewalsallowed,
319 unseen_renewals_allowed => $unseen_renewals_allowed,
320 renewalperiod => $renewalperiod,
321 norenewalbefore => $norenewalbefore,
322 noautorenewalbefore => $noautorenewalbefore,
323 auto_renew => $auto_renew,
324 no_auto_renewal_after => $no_auto_renewal_after,
325 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
326 reservesallowed => $reservesallowed,
327 holds_per_record => $holds_per_record,
328 holds_per_day => $holds_per_day,
329 onshelfholds => $onshelfholds,
330 opacitemholds => $opacitemholds,
331 overduefinescap => $overduefinescap,
332 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
333 article_requests => $article_requests,
335 decreaseloanholds => $decreaseloanholds,
336 recalls_allowed => $recalls_allowed,
337 recalls_per_record => $recalls_per_record,
338 on_shelf_recalls => $on_shelf_recalls,
339 recall_due_date_interval => $recall_due_date_interval,
340 recall_overdue_fine => $recall_overdue_fine,
341 recall_shelf_time => $recall_shelf_time,
344 Koha::CirculationRules->set_rules(
346 categorycode => $bor eq '*' ? undef : $bor,
347 itemtype => $itemtype eq '*' ? undef : $itemtype,
348 branchcode => $br eq '*' ? undef : $br,
354 elsif ($op eq "set-branch-defaults") {
355 my $categorycode = $input->param('categorycode');
356 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
357 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
358 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
359 my $holdallowed = $input->param('holdallowed');
360 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
361 my $returnbranch = $input->param('returnbranch');
362 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
364 if ($branch eq "*") {
365 Koha::CirculationRules->set_rules(
370 holdallowed => $holdallowed,
371 hold_fulfillment_policy => $hold_fulfillment_policy,
372 returnbranch => $returnbranch,
376 Koha::CirculationRules->set_rules(
378 categorycode => undef,
381 patron_maxissueqty => $patron_maxissueqty,
382 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
387 Koha::CirculationRules->set_rules(
390 branchcode => $branch,
392 holdallowed => $holdallowed,
393 hold_fulfillment_policy => $hold_fulfillment_policy,
394 returnbranch => $returnbranch,
398 Koha::CirculationRules->set_rules(
400 categorycode => undef,
401 branchcode => $branch,
403 patron_maxissueqty => $patron_maxissueqty,
404 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
409 Koha::CirculationRules->set_rule(
411 branchcode => $branch,
412 categorycode => undef,
413 rule_name => 'max_holds',
414 rule_value => $max_holds,
418 elsif ($op eq "add-branch-cat") {
419 my $categorycode = $input->param('categorycode');
420 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
421 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
422 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
423 my $max_holds = $input->param('max_holds');
424 $max_holds = strip_non_numeric($max_holds);
426 if ($branch eq "*") {
427 if ($categorycode eq "*") {
428 Koha::CirculationRules->set_rules(
430 categorycode => undef,
433 max_holds => $max_holds,
434 patron_maxissueqty => $patron_maxissueqty,
435 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
440 Koha::CirculationRules->set_rules(
442 categorycode => $categorycode,
445 max_holds => $max_holds,
446 patron_maxissueqty => $patron_maxissueqty,
447 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
452 } elsif ($categorycode eq "*") {
453 Koha::CirculationRules->set_rules(
455 categorycode => undef,
456 branchcode => $branch,
458 max_holds => $max_holds,
459 patron_maxissueqty => $patron_maxissueqty,
460 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
465 Koha::CirculationRules->set_rules(
467 categorycode => $categorycode,
468 branchcode => $branch,
470 max_holds => $max_holds,
471 patron_maxissueqty => $patron_maxissueqty,
472 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
478 elsif ( $op eq "add-open-article-requests-limit" ) {
479 my $categorycode = $input->param('categorycode');
480 my $open_article_requests_limit = strip_non_numeric( scalar $input->param('open_article_requests_limit') );
482 Koha::Exception->throw("No value passed for article request limit")
483 if not defined $open_article_requests_limit # There is a JS check for that
484 || $open_article_requests_limit eq q{};
486 if ( $branch eq "*" ) {
487 if ( $categorycode eq "*" ) {
488 Koha::CirculationRules->set_rules(
489 { categorycode => undef,
491 rules => { open_article_requests_limit => $open_article_requests_limit, }
495 Koha::CirculationRules->set_rules(
496 { categorycode => $categorycode,
498 rules => { open_article_requests_limit => $open_article_requests_limit, }
502 } elsif ( $categorycode eq "*" ) {
503 Koha::CirculationRules->set_rules(
504 { categorycode => undef,
505 branchcode => $branch,
506 rules => { open_article_requests_limit => $open_article_requests_limit, }
510 Koha::CirculationRules->set_rules(
511 { categorycode => $categorycode,
512 branchcode => $branch,
513 rules => { open_article_requests_limit => $open_article_requests_limit, }
517 } elsif ( $op eq 'del-open-article-requests-limit' ) {
518 my $categorycode = $input->param('categorycode');
519 if ( $branch eq "*" ) {
520 if ( $categorycode eq "*" ) {
521 Koha::CirculationRules->set_rules(
522 { branchcode => undef,
523 categorycode => undef,
524 rules => { open_article_requests_limit => undef, }
528 Koha::CirculationRules->set_rules(
529 { categorycode => $categorycode,
531 rules => { open_article_requests_limit => undef, }
535 } elsif ( $categorycode eq "*" ) {
536 Koha::CirculationRules->set_rules(
537 { branchcode => $branch,
538 categorycode => undef,
539 rules => { open_article_requests_limit => undef, }
543 Koha::CirculationRules->set_rules(
544 { categorycode => $categorycode,
545 branchcode => $branch,
546 rules => { open_article_requests_limit => undef, }
551 elsif ( $op eq "set-article-request-fee" ) {
553 my $category = $input->param('article_request_fee_category');
554 my $fee = strip_non_numeric( scalar $input->param('article_request_fee') );
556 Koha::Exception->throw("No value passed for article request fee")
557 if not defined $fee # There is a JS check for that
560 Koha::CirculationRules->set_rules(
561 { categorycode => ( $category eq '*' ) ? undef : $category,
562 branchcode => ( $branch eq '*' ) ? undef : $branch,
563 rules => { article_request_fee => $fee },
567 } elsif ( $op eq 'del-article-request-fee' ) {
569 my $category = $input->param('article_request_fee_category');
571 Koha::CirculationRules->set_rules(
572 { categorycode => ( $category eq '*' ) ? undef : $category,
573 branchcode => ( $branch eq '*' ) ? undef : $branch,
574 rules => { article_request_fee => undef },
578 elsif ($op eq "add-branch-item") {
579 my $itemtype = $input->param('itemtype');
580 my $holdallowed = $input->param('holdallowed');
581 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
582 my $returnbranch = $input->param('returnbranch');
584 if ($branch eq "*") {
585 if ($itemtype eq "*") {
586 Koha::CirculationRules->set_rules(
591 holdallowed => $holdallowed,
592 hold_fulfillment_policy => $hold_fulfillment_policy,
593 returnbranch => $returnbranch,
598 Koha::CirculationRules->set_rules(
600 itemtype => $itemtype,
603 holdallowed => $holdallowed,
604 hold_fulfillment_policy => $hold_fulfillment_policy,
605 returnbranch => $returnbranch,
610 } elsif ($itemtype eq "*") {
611 Koha::CirculationRules->set_rules(
614 branchcode => $branch,
616 holdallowed => $holdallowed,
617 hold_fulfillment_policy => $hold_fulfillment_policy,
618 returnbranch => $returnbranch,
623 Koha::CirculationRules->set_rules(
625 itemtype => $itemtype,
626 branchcode => $branch,
628 holdallowed => $holdallowed,
629 hold_fulfillment_policy => $hold_fulfillment_policy,
630 returnbranch => $returnbranch,
636 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
638 my $lostreturn = $input->param('lostreturn');
640 if ( $lostreturn eq '*' ) {
641 if ( $branch ne '*' ) {
642 # only do something for $lostreturn eq '*' if branch-specific
643 Koha::CirculationRules->set_rules(
645 branchcode => $branch,
653 Koha::CirculationRules->set_rules(
655 branchcode => $branch,
657 lostreturn => $lostreturn
663 my $processingreturn = $input->param('processingreturn');
665 if ( $processingreturn eq '*' ) {
666 if ( $branch ne '*' ) {
667 # only do something for $processingreturn eq '*' if branch-specific
668 Koha::CirculationRules->set_rules(
670 branchcode => $branch,
672 processingreturn => undef
678 Koha::CirculationRules->set_rules(
680 branchcode => $branch,
682 processingreturn => $processingreturn
687 } elsif ( $op eq "set-waiting-hold-cancellation" ) {
689 my $category = $input->param('waiting_hold_cancellation_category');
690 my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
691 my $policy = strip_non_numeric( scalar $input->param('waiting_hold_cancellation_policy') )
695 Koha::Exception->throw("No value passed for waiting holds cancellation policy")
696 if not defined $policy # There is a JS check for that
699 Koha::CirculationRules->set_rules(
700 { categorycode => ( $category eq '*' ) ? undef : $category,
701 itemtype => ( $itemtype eq '*' ) ? undef : $itemtype,
702 branchcode => ( $branch eq '*' ) ? undef : $branch,
703 rules => { waiting_hold_cancellation => $policy },
707 } elsif ( $op eq 'del-waiting-hold-cancellation' ) {
709 my $category = $input->param('waiting_hold_cancellation_category');
710 my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
712 Koha::CirculationRules->set_rules(
713 { categorycode => ( $category eq '*' ) ? undef : $category,
714 itemtype => ( $itemtype eq '*' ) ? undef : $itemtype,
715 branchcode => ( $branch eq '*' ) ? undef : $branch,
716 rules => { waiting_hold_cancellation => undef },
722 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
723 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
724 my $refundProcessingFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'processingreturn' });
725 my $defaultProcessingFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'processingreturn' });
727 refundLostItemFeeRule => $refundLostItemFeeRule,
728 defaultRefundRule => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund',
729 refundProcessingFeeRule => $refundProcessingFeeRule,
730 defaultProcessingRefundRule => $defaultProcessingFeeRule ? $defaultProcessingFeeRule->rule_value : 'refund',
733 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
735 my $itemtypes = Koha::ItemTypes->search_with_localization;
737 my $humanbranch = ( $branch ne '*' ? $branch : undef );
739 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
740 my $definedbranch = $all_rules->count ? 1 : 0;
743 while ( my $r = $all_rules->next ) {
745 $rules->{ $r->{categorycode} // q{} }->{ $r->{itemtype} // q{} }->{ $r->{rule_name} } = $r->{rule_value};
748 $template->param(show_branch_cat_rule_form => 1);
751 patron_categories => $patron_categories,
752 itemtypeloop => $itemtypes,
753 humanbranch => $humanbranch,
754 current_branch => $branch,
755 definedbranch => $definedbranch,
758 output_html_with_http_headers $input, $cookie, $template->output;
762 # sort by patron category, then item type, putting
763 # default entries at the bottom
764 sub by_category_and_itemtype {
765 unless (by_category($a, $b)) {
766 return by_itemtype($a, $b);
772 if ($a->{'default_humancategorycode'}) {
773 return ($b->{'default_humancategorycode'} ? 0 : 1);
774 } elsif ($b->{'default_humancategorycode'}) {
777 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
783 if ($a->{default_translated_description}) {
784 return ($b->{'default_translated_description'} ? 0 : 1);
785 } elsif ($b->{'default_translated_description'}) {
788 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
792 sub strip_non_numeric {
795 $string = q{} if $string !~ /^\d+/;