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,
119 holds_pickup_period => undef,
124 elsif ($op eq 'delete-branch-cat') {
125 my $categorycode = $input->param('categorycode');
126 if ($branch eq "*") {
127 if ($categorycode eq "*") {
128 Koha::CirculationRules->set_rules(
131 categorycode => undef,
134 patron_maxissueqty => undef,
135 patron_maxonsiteissueqty => undef,
139 Koha::CirculationRules->set_rules(
144 holdallowed => undef,
145 hold_fulfillment_policy => undef,
146 returnbranch => undef,
151 Koha::CirculationRules->set_rules(
153 categorycode => $categorycode,
157 patron_maxissueqty => undef,
158 patron_maxonsiteissueqty => undef,
163 } elsif ($categorycode eq "*") {
164 Koha::CirculationRules->set_rules(
166 branchcode => $branch,
167 categorycode => undef,
170 patron_maxissueqty => undef,
171 patron_maxonsiteissueqty => undef,
175 Koha::CirculationRules->set_rules(
177 branchcode => $branch,
180 holdallowed => undef,
181 hold_fulfillment_policy => undef,
182 returnbranch => undef,
187 Koha::CirculationRules->set_rules(
189 categorycode => $categorycode,
190 branchcode => $branch,
193 patron_maxissueqty => undef,
194 patron_maxonsiteissueqty => undef,
200 elsif ($op eq 'delete-branch-item') {
201 my $itemtype = $input->param('itemtype');
202 if ($branch eq "*") {
203 if ($itemtype eq "*") {
204 Koha::CirculationRules->set_rules(
209 holdallowed => undef,
210 hold_fulfillment_policy => undef,
211 returnbranch => undef,
216 Koha::CirculationRules->set_rules(
219 itemtype => $itemtype,
221 holdallowed => undef,
222 hold_fulfillment_policy => undef,
223 returnbranch => undef,
228 } elsif ($itemtype eq "*") {
229 Koha::CirculationRules->set_rules(
231 branchcode => $branch,
234 holdallowed => undef,
235 hold_fulfillment_policy => undef,
236 returnbranch => undef,
241 Koha::CirculationRules->set_rules(
243 branchcode => $branch,
244 itemtype => $itemtype,
246 holdallowed => undef,
247 hold_fulfillment_policy => undef,
248 returnbranch => undef,
254 # save the values entered
255 elsif ( $op eq 'add' ) {
256 my $br = $branch; # branch
257 my $bor = $input->param('categorycode'); # borrower category
258 my $itemtype = $input->param('itemtype'); # item type
259 my $fine = $input->param('fine');
260 my $finedays = $input->param('finedays');
261 my $maxsuspensiondays = $input->param('maxsuspensiondays') || q{};
262 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
263 my $firstremind = $input->param('firstremind');
264 my $chargeperiod = $input->param('chargeperiod');
265 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
266 my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
267 my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
268 my $renewalsallowed = $input->param('renewalsallowed');
269 my $unseen_renewals_allowed =
270 defined $input->param('unseen_renewals_allowed')
271 ? strip_non_numeric( scalar $input->param('unseen_renewals_allowed') )
273 my $renewalperiod = $input->param('renewalperiod');
274 my $norenewalbefore = $input->param('norenewalbefore');
275 $norenewalbefore = q{} if $norenewalbefore =~ /^\s*$/;
276 my $noautorenewalbefore = $input->param('noautorenewalbefore');
277 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
278 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
279 $no_auto_renewal_after = q{} if $no_auto_renewal_after =~ /^\s*$/;
280 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || q{};
281 my $reservesallowed = strip_non_numeric( scalar $input->param('reservesallowed') );
282 my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
283 my $holds_per_day = strip_non_numeric( scalar $input->param('holds_per_day') );
284 my $onshelfholds = $input->param('onshelfholds') || 0;
285 my $issuelength = $input->param('issuelength') || 0;
286 my $daysmode = $input->param('daysmode');
287 my $lengthunit = $input->param('lengthunit');
288 my $hardduedate = $input->param('hardduedate') || q{};
289 my $hardduedatecompare = $input->param('hardduedatecompare');
290 my $rentaldiscount = $input->param('rentaldiscount') || 0;
291 my $opacitemholds = $input->param('opacitemholds') || 0;
292 my $article_requests = $input->param('article_requests') || 'no';
293 my $overduefinescap = $input->param('overduefinescap')
294 && ( $input->param('overduefinescap') + 0 ) > 0 ? sprintf( "%.02f", $input->param('overduefinescap') ) : q{};
295 my $cap_fine_to_replacement_price = ( $input->param('cap_fine_to_replacement_price') || q{} ) eq 'on';
296 my $note = $input->param('note');
297 my $decreaseloanholds = $input->param('decreaseloanholds') || q{};
298 my $recalls_allowed = $input->param('recalls_allowed');
299 my $recalls_per_record = $input->param('recalls_per_record');
300 my $on_shelf_recalls = $input->param('on_shelf_recalls');
301 my $recall_due_date_interval = $input->param('recall_due_date_interval');
302 my $recall_overdue_fine = $input->param('recall_overdue_fine');
303 my $recall_shelf_time = $input->param('recall_shelf_time');
304 my $holds_pickup_period = strip_non_numeric( scalar $input->param('holds_pickup_period') );
307 maxissueqty => $maxissueqty,
308 maxonsiteissueqty => $maxonsiteissueqty,
309 rentaldiscount => $rentaldiscount,
311 finedays => $finedays,
312 maxsuspensiondays => $maxsuspensiondays,
313 suspension_chargeperiod => $suspension_chargeperiod,
314 firstremind => $firstremind,
315 chargeperiod => $chargeperiod,
316 chargeperiod_charge_at => $chargeperiod_charge_at,
317 issuelength => $issuelength,
318 daysmode => $daysmode,
319 lengthunit => $lengthunit,
320 hardduedate => $hardduedate,
321 hardduedatecompare => $hardduedatecompare,
322 renewalsallowed => $renewalsallowed,
323 unseen_renewals_allowed => $unseen_renewals_allowed,
324 renewalperiod => $renewalperiod,
325 norenewalbefore => $norenewalbefore,
326 noautorenewalbefore => $noautorenewalbefore,
327 auto_renew => $auto_renew,
328 no_auto_renewal_after => $no_auto_renewal_after,
329 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
330 onshelfholds => $onshelfholds,
331 opacitemholds => $opacitemholds,
332 overduefinescap => $overduefinescap,
333 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
334 article_requests => $article_requests,
336 decreaseloanholds => $decreaseloanholds,
337 recalls_allowed => $recalls_allowed,
338 recalls_per_record => $recalls_per_record,
339 on_shelf_recalls => $on_shelf_recalls,
340 recall_due_date_interval => $recall_due_date_interval,
341 recall_overdue_fine => $recall_overdue_fine,
342 recall_shelf_time => $recall_shelf_time,
343 holds_pickup_period => $holds_pickup_period,
346 Koha::CirculationRules->set_rules(
348 categorycode => $bor eq '*' ? undef : $bor,
349 itemtype => $itemtype eq '*' ? undef : $itemtype,
350 branchcode => $br eq '*' ? undef : $br,
356 elsif ($op eq "set-branch-defaults") {
357 my $categorycode = $input->param('categorycode');
358 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
359 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
360 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
361 my $holdallowed = $input->param('holdallowed');
362 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
363 my $returnbranch = $input->param('returnbranch');
364 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
366 if ($branch eq "*") {
367 Koha::CirculationRules->set_rules(
372 holdallowed => $holdallowed,
373 hold_fulfillment_policy => $hold_fulfillment_policy,
374 returnbranch => $returnbranch,
378 Koha::CirculationRules->set_rules(
380 categorycode => undef,
383 patron_maxissueqty => $patron_maxissueqty,
384 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
389 Koha::CirculationRules->set_rules(
392 branchcode => $branch,
394 holdallowed => $holdallowed,
395 hold_fulfillment_policy => $hold_fulfillment_policy,
396 returnbranch => $returnbranch,
400 Koha::CirculationRules->set_rules(
402 categorycode => undef,
403 branchcode => $branch,
405 patron_maxissueqty => $patron_maxissueqty,
406 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
411 Koha::CirculationRules->set_rule(
413 branchcode => $branch,
414 categorycode => undef,
415 rule_name => 'max_holds',
416 rule_value => $max_holds,
420 elsif ($op eq "add-branch-cat") {
421 my $categorycode = $input->param('categorycode');
422 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
423 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
424 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
425 my $max_holds = $input->param('max_holds');
426 $max_holds = strip_non_numeric($max_holds);
428 if ($branch eq "*") {
429 if ($categorycode eq "*") {
430 Koha::CirculationRules->set_rules(
432 categorycode => undef,
435 max_holds => $max_holds,
436 patron_maxissueqty => $patron_maxissueqty,
437 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
442 Koha::CirculationRules->set_rules(
444 categorycode => $categorycode,
447 max_holds => $max_holds,
448 patron_maxissueqty => $patron_maxissueqty,
449 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
454 } elsif ($categorycode eq "*") {
455 Koha::CirculationRules->set_rules(
457 categorycode => undef,
458 branchcode => $branch,
460 max_holds => $max_holds,
461 patron_maxissueqty => $patron_maxissueqty,
462 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
467 Koha::CirculationRules->set_rules(
469 categorycode => $categorycode,
470 branchcode => $branch,
472 max_holds => $max_holds,
473 patron_maxissueqty => $patron_maxissueqty,
474 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
480 elsif ( $op eq "add-open-article-requests-limit" ) {
481 my $categorycode = $input->param('categorycode');
482 my $open_article_requests_limit = strip_non_numeric( scalar $input->param('open_article_requests_limit') );
484 Koha::Exception->throw("No value passed for article request limit")
485 if not defined $open_article_requests_limit # There is a JS check for that
486 || $open_article_requests_limit eq q{};
488 if ( $branch eq "*" ) {
489 if ( $categorycode eq "*" ) {
490 Koha::CirculationRules->set_rules(
491 { categorycode => undef,
493 rules => { open_article_requests_limit => $open_article_requests_limit, }
497 Koha::CirculationRules->set_rules(
498 { categorycode => $categorycode,
500 rules => { open_article_requests_limit => $open_article_requests_limit, }
504 } elsif ( $categorycode eq "*" ) {
505 Koha::CirculationRules->set_rules(
506 { categorycode => undef,
507 branchcode => $branch,
508 rules => { open_article_requests_limit => $open_article_requests_limit, }
512 Koha::CirculationRules->set_rules(
513 { categorycode => $categorycode,
514 branchcode => $branch,
515 rules => { open_article_requests_limit => $open_article_requests_limit, }
519 } elsif ( $op eq 'del-open-article-requests-limit' ) {
520 my $categorycode = $input->param('categorycode');
521 if ( $branch eq "*" ) {
522 if ( $categorycode eq "*" ) {
523 Koha::CirculationRules->set_rules(
524 { branchcode => undef,
525 categorycode => undef,
526 rules => { open_article_requests_limit => undef, }
530 Koha::CirculationRules->set_rules(
531 { categorycode => $categorycode,
533 rules => { open_article_requests_limit => undef, }
537 } elsif ( $categorycode eq "*" ) {
538 Koha::CirculationRules->set_rules(
539 { branchcode => $branch,
540 categorycode => undef,
541 rules => { open_article_requests_limit => undef, }
545 Koha::CirculationRules->set_rules(
546 { categorycode => $categorycode,
547 branchcode => $branch,
548 rules => { open_article_requests_limit => undef, }
553 elsif ( $op eq "set-article-request-fee" ) {
555 my $category = $input->param('article_request_fee_category');
556 my $fee = strip_non_numeric( scalar $input->param('article_request_fee') );
558 Koha::Exception->throw("No value passed for article request fee")
559 if not defined $fee # There is a JS check for that
562 Koha::CirculationRules->set_rules(
563 { categorycode => ( $category eq '*' ) ? undef : $category,
564 branchcode => ( $branch eq '*' ) ? undef : $branch,
565 rules => { article_request_fee => $fee },
569 } elsif ( $op eq 'del-article-request-fee' ) {
571 my $category = $input->param('article_request_fee_category');
573 Koha::CirculationRules->set_rules(
574 { categorycode => ( $category eq '*' ) ? undef : $category,
575 branchcode => ( $branch eq '*' ) ? undef : $branch,
576 rules => { article_request_fee => undef },
580 elsif ($op eq "add-branch-item") {
581 my $itemtype = $input->param('itemtype');
582 my $holdallowed = $input->param('holdallowed');
583 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
584 my $returnbranch = $input->param('returnbranch');
586 if ($branch eq "*") {
587 if ($itemtype eq "*") {
588 Koha::CirculationRules->set_rules(
593 holdallowed => $holdallowed,
594 hold_fulfillment_policy => $hold_fulfillment_policy,
595 returnbranch => $returnbranch,
600 Koha::CirculationRules->set_rules(
602 itemtype => $itemtype,
605 holdallowed => $holdallowed,
606 hold_fulfillment_policy => $hold_fulfillment_policy,
607 returnbranch => $returnbranch,
612 } elsif ($itemtype eq "*") {
613 Koha::CirculationRules->set_rules(
616 branchcode => $branch,
618 holdallowed => $holdallowed,
619 hold_fulfillment_policy => $hold_fulfillment_policy,
620 returnbranch => $returnbranch,
625 Koha::CirculationRules->set_rules(
627 itemtype => $itemtype,
628 branchcode => $branch,
630 holdallowed => $holdallowed,
631 hold_fulfillment_policy => $hold_fulfillment_policy,
632 returnbranch => $returnbranch,
638 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
640 my $lostreturn = $input->param('lostreturn');
642 if ( $lostreturn eq '*' ) {
643 if ( $branch ne '*' ) {
644 # only do something for $lostreturn eq '*' if branch-specific
645 Koha::CirculationRules->set_rules(
647 branchcode => $branch,
655 Koha::CirculationRules->set_rules(
657 branchcode => $branch,
659 lostreturn => $lostreturn
665 my $processingreturn = $input->param('processingreturn');
667 if ( $processingreturn eq '*' ) {
668 if ( $branch ne '*' ) {
669 # only do something for $processingreturn eq '*' if branch-specific
670 Koha::CirculationRules->set_rules(
672 branchcode => $branch,
674 processingreturn => undef
680 Koha::CirculationRules->set_rules(
682 branchcode => $branch,
684 processingreturn => $processingreturn
689 } elsif ( $op eq "set-waiting-hold-cancellation" ) {
691 my $category = $input->param('waiting_hold_cancellation_category');
692 my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
693 my $policy = strip_non_numeric( scalar $input->param('waiting_hold_cancellation_policy') )
697 Koha::Exception->throw("No value passed for waiting holds cancellation policy")
698 if not defined $policy # There is a JS check for that
701 Koha::CirculationRules->set_rules(
702 { categorycode => ( $category eq '*' ) ? undef : $category,
703 itemtype => ( $itemtype eq '*' ) ? undef : $itemtype,
704 branchcode => ( $branch eq '*' ) ? undef : $branch,
705 rules => { waiting_hold_cancellation => $policy },
709 } elsif ( $op eq 'del-waiting-hold-cancellation' ) {
711 my $category = $input->param('waiting_hold_cancellation_category');
712 my $itemtype = $input->param('waiting_hold_cancellation_itemtype');
714 Koha::CirculationRules->set_rules(
715 { categorycode => ( $category eq '*' ) ? undef : $category,
716 itemtype => ( $itemtype eq '*' ) ? undef : $itemtype,
717 branchcode => ( $branch eq '*' ) ? undef : $branch,
718 rules => { waiting_hold_cancellation => undef },
724 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
725 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
726 my $refundProcessingFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'processingreturn' });
727 my $defaultProcessingFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'processingreturn' });
729 refundLostItemFeeRule => $refundLostItemFeeRule,
730 defaultRefundRule => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund',
731 refundProcessingFeeRule => $refundProcessingFeeRule,
732 defaultProcessingRefundRule => $defaultProcessingFeeRule ? $defaultProcessingFeeRule->rule_value : 'refund',
735 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
737 my $itemtypes = Koha::ItemTypes->search_with_localization;
739 my $humanbranch = ( $branch ne '*' ? $branch : undef );
741 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
742 my $definedbranch = $all_rules->count ? 1 : 0;
745 while ( my $r = $all_rules->next ) {
747 $rules->{ $r->{categorycode} // q{} }->{ $r->{itemtype} // q{} }->{ $r->{rule_name} } = $r->{rule_value};
750 $template->param(show_branch_cat_rule_form => 1);
753 patron_categories => $patron_categories,
754 itemtypeloop => $itemtypes,
755 humanbranch => $humanbranch,
756 current_branch => $branch,
757 definedbranch => $definedbranch,
760 output_html_with_http_headers $input, $cookie, $template->output;
764 # sort by patron category, then item type, putting
765 # default entries at the bottom
766 sub by_category_and_itemtype {
767 unless (by_category($a, $b)) {
768 return by_itemtype($a, $b);
774 if ($a->{'default_humancategorycode'}) {
775 return ($b->{'default_humancategorycode'} ? 0 : 1);
776 } elsif ($b->{'default_humancategorycode'}) {
779 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
785 if ($a->{default_translated_description}) {
786 return ($b->{'default_translated_description'} ? 0 : 1);
787 } elsif ($b->{'default_translated_description'}) {
790 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
794 sub strip_non_numeric {
797 $string = q{} if $string !~ /^\d+/;