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 );
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 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,
122 elsif ($op eq 'delete-branch-cat') {
123 my $categorycode = $input->param('categorycode');
124 if ($branch eq "*") {
125 if ($categorycode eq "*") {
126 Koha::CirculationRules->set_rules(
129 categorycode => undef,
132 patron_maxissueqty => undef,
133 patron_maxonsiteissueqty => undef,
137 Koha::CirculationRules->set_rules(
142 holdallowed => undef,
143 hold_fulfillment_policy => undef,
144 returnbranch => undef,
149 Koha::CirculationRules->set_rules(
151 categorycode => $categorycode,
155 patron_maxissueqty => undef,
156 patron_maxonsiteissueqty => undef,
161 } elsif ($categorycode eq "*") {
162 Koha::CirculationRules->set_rules(
164 branchcode => $branch,
165 categorycode => undef,
168 patron_maxissueqty => undef,
169 patron_maxonsiteissueqty => undef,
173 Koha::CirculationRules->set_rules(
175 branchcode => $branch,
178 holdallowed => undef,
179 hold_fulfillment_policy => undef,
180 returnbranch => undef,
185 Koha::CirculationRules->set_rules(
187 categorycode => $categorycode,
188 branchcode => $branch,
191 patron_maxissueqty => undef,
192 patron_maxonsiteissueqty => undef,
198 elsif ($op eq 'delete-branch-item') {
199 my $itemtype = $input->param('itemtype');
200 if ($branch eq "*") {
201 if ($itemtype eq "*") {
202 Koha::CirculationRules->set_rules(
207 holdallowed => undef,
208 hold_fulfillment_policy => undef,
209 returnbranch => undef,
214 Koha::CirculationRules->set_rules(
217 itemtype => $itemtype,
219 holdallowed => undef,
220 hold_fulfillment_policy => undef,
221 returnbranch => undef,
226 } elsif ($itemtype eq "*") {
227 Koha::CirculationRules->set_rules(
229 branchcode => $branch,
232 holdallowed => undef,
233 hold_fulfillment_policy => undef,
234 returnbranch => undef,
239 Koha::CirculationRules->set_rules(
241 branchcode => $branch,
242 itemtype => $itemtype,
244 holdallowed => undef,
245 hold_fulfillment_policy => undef,
246 returnbranch => undef,
252 # save the values entered
253 elsif ($op eq 'add') {
254 my $br = $branch; # branch
255 my $bor = $input->param('categorycode'); # borrower category
256 my $itemtype = $input->param('itemtype'); # item type
257 my $fine = $input->param('fine');
258 my $finedays = $input->param('finedays');
259 my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
260 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
261 my $firstremind = $input->param('firstremind');
262 my $chargeperiod = $input->param('chargeperiod');
263 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
264 my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
265 my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
266 my $renewalsallowed = $input->param('renewalsallowed');
267 my $unseen_renewals_allowed = strip_non_numeric( scalar $input->param('unseen_renewals_allowed') ) // '';
268 my $renewalperiod = $input->param('renewalperiod');
269 my $norenewalbefore = $input->param('norenewalbefore');
270 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
271 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
272 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
273 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
274 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
275 $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
276 $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 );
277 my $reservesallowed = strip_non_numeric( scalar $input->param('reservesallowed') );
278 my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
279 my $holds_per_day = strip_non_numeric( scalar $input->param('holds_per_day') );
280 my $onshelfholds = $input->param('onshelfholds') || 0;
281 my $issuelength = $input->param('issuelength') || 0;
282 my $daysmode = $input->param('daysmode');
283 my $lengthunit = $input->param('lengthunit');
284 my $hardduedate = $input->param('hardduedate') || '';
285 $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
286 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
287 my $hardduedatecompare = $input->param('hardduedatecompare');
288 my $rentaldiscount = $input->param('rentaldiscount') || 0;
289 my $opacitemholds = $input->param('opacitemholds') || 0;
290 my $article_requests = $input->param('article_requests') || 'no';
291 my $overduefinescap = $input->param('overduefinescap') || '';
292 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
293 my $note = $input->param('note');
294 my $decreaseloanholds = $input->param('decreaseloanholds') || '';
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 auto_renew => $auto_renew,
323 no_auto_renewal_after => $no_auto_renewal_after,
324 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
325 reservesallowed => $reservesallowed,
326 holds_per_record => $holds_per_record,
327 holds_per_day => $holds_per_day,
328 onshelfholds => $onshelfholds,
329 opacitemholds => $opacitemholds,
330 overduefinescap => $overduefinescap,
331 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
332 article_requests => $article_requests,
334 decreaseloanholds => $decreaseloanholds,
335 recalls_allowed => $recalls_allowed,
336 recalls_per_record => $recalls_per_record,
337 on_shelf_recalls => $on_shelf_recalls,
338 recall_due_date_interval => $recall_due_date_interval,
339 recall_overdue_fine => $recall_overdue_fine,
340 recall_shelf_time => $recall_shelf_time,
343 Koha::CirculationRules->set_rules(
345 categorycode => $bor eq '*' ? undef : $bor,
346 itemtype => $itemtype eq '*' ? undef : $itemtype,
347 branchcode => $br eq '*' ? undef : $br,
353 elsif ($op eq "set-branch-defaults") {
354 my $categorycode = $input->param('categorycode');
355 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
356 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
357 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
358 my $holdallowed = $input->param('holdallowed');
359 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
360 my $returnbranch = $input->param('returnbranch');
361 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
363 if ($branch eq "*") {
364 Koha::CirculationRules->set_rules(
369 holdallowed => $holdallowed,
370 hold_fulfillment_policy => $hold_fulfillment_policy,
371 returnbranch => $returnbranch,
375 Koha::CirculationRules->set_rules(
377 categorycode => undef,
380 patron_maxissueqty => $patron_maxissueqty,
381 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
386 Koha::CirculationRules->set_rules(
389 branchcode => $branch,
391 holdallowed => $holdallowed,
392 hold_fulfillment_policy => $hold_fulfillment_policy,
393 returnbranch => $returnbranch,
397 Koha::CirculationRules->set_rules(
399 categorycode => undef,
400 branchcode => $branch,
402 patron_maxissueqty => $patron_maxissueqty,
403 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
408 Koha::CirculationRules->set_rule(
410 branchcode => $branch,
411 categorycode => undef,
412 rule_name => 'max_holds',
413 rule_value => $max_holds,
417 elsif ($op eq "add-branch-cat") {
418 my $categorycode = $input->param('categorycode');
419 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
420 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
421 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
422 my $max_holds = $input->param('max_holds');
423 $max_holds =~ s/\s//g;
424 $max_holds = undef if $max_holds !~ /^\d+/;
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 '';
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
664 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
665 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
667 refundLostItemFeeRule => $refundLostItemFeeRule,
668 defaultRefundRule => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
671 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
673 my $itemtypes = Koha::ItemTypes->search_with_localization;
675 my $humanbranch = ( $branch ne '*' ? $branch : undef );
677 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
678 my $definedbranch = $all_rules->count ? 1 : 0;
681 while ( my $r = $all_rules->next ) {
683 $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
686 $template->param(show_branch_cat_rule_form => 1);
689 patron_categories => $patron_categories,
690 itemtypeloop => $itemtypes,
691 humanbranch => $humanbranch,
692 current_branch => $branch,
693 definedbranch => $definedbranch,
696 output_html_with_http_headers $input, $cookie, $template->output;
700 # sort by patron category, then item type, putting
701 # default entries at the bottom
702 sub by_category_and_itemtype {
703 unless (by_category($a, $b)) {
704 return by_itemtype($a, $b);
710 if ($a->{'default_humancategorycode'}) {
711 return ($b->{'default_humancategorycode'} ? 0 : 1);
712 } elsif ($b->{'default_humancategorycode'}) {
715 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
721 if ($a->{default_translated_description}) {
722 return ($b->{'default_translated_description'} ? 0 : 1);
723 } elsif ($b->{'default_translated_description'}) {
726 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
730 sub strip_non_numeric {
733 $string = '' if $string !~ /^\d+/;