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,
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') || '';
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 = $input->param('unseen_renewals_allowed');
269 my $renewalperiod = $input->param('renewalperiod');
270 my $norenewalbefore = $input->param('norenewalbefore');
271 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
272 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
273 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
274 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
275 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
276 $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
277 $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 );
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');
283 $issuelength = $issuelength eq q{} ? undef : $issuelength;
284 my $daysmode = $input->param('daysmode');
285 my $lengthunit = $input->param('lengthunit');
286 my $hardduedate = $input->param('hardduedate') || undef;
287 $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
288 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
289 my $hardduedatecompare = $input->param('hardduedatecompare');
290 my $rentaldiscount = $input->param('rentaldiscount');
291 my $opacitemholds = $input->param('opacitemholds') || 0;
292 my $article_requests = $input->param('article_requests') || 'no';
293 my $overduefinescap = $input->param('overduefinescap') || '';
294 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
295 my $note = $input->param('note');
296 my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
297 my $recalls_allowed = $input->param('recalls_allowed');
298 my $recalls_per_record = $input->param('recalls_per_record');
299 my $on_shelf_recalls = $input->param('on_shelf_recalls');
300 my $recall_due_date_interval = $input->param('recall_due_date_interval');
301 my $recall_overdue_fine = $input->param('recall_overdue_fine');
302 my $recall_shelf_time = $input->param('recall_shelf_time');
305 maxissueqty => $maxissueqty,
306 maxonsiteissueqty => $maxonsiteissueqty,
307 rentaldiscount => $rentaldiscount,
309 finedays => $finedays,
310 maxsuspensiondays => $maxsuspensiondays,
311 suspension_chargeperiod => $suspension_chargeperiod,
312 firstremind => $firstremind,
313 chargeperiod => $chargeperiod,
314 chargeperiod_charge_at => $chargeperiod_charge_at,
315 issuelength => $issuelength,
316 daysmode => $daysmode,
317 lengthunit => $lengthunit,
318 hardduedate => $hardduedate,
319 hardduedatecompare => $hardduedatecompare,
320 renewalsallowed => $renewalsallowed,
321 unseen_renewals_allowed => $unseen_renewals_allowed,
322 renewalperiod => $renewalperiod,
323 norenewalbefore => $norenewalbefore,
324 auto_renew => $auto_renew,
325 no_auto_renewal_after => $no_auto_renewal_after,
326 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
327 reservesallowed => $reservesallowed,
328 holds_per_record => $holds_per_record,
329 holds_per_day => $holds_per_day,
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,
345 Koha::CirculationRules->set_rules(
347 categorycode => $bor eq '*' ? undef : $bor,
348 itemtype => $itemtype eq '*' ? undef : $itemtype,
349 branchcode => $br eq '*' ? undef : $br,
355 elsif ($op eq "set-branch-defaults") {
356 my $categorycode = $input->param('categorycode');
357 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
358 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
359 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
360 my $holdallowed = $input->param('holdallowed');
361 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
362 my $returnbranch = $input->param('returnbranch');
363 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
365 if ($branch eq "*") {
366 Koha::CirculationRules->set_rules(
371 holdallowed => $holdallowed,
372 hold_fulfillment_policy => $hold_fulfillment_policy,
373 returnbranch => $returnbranch,
377 Koha::CirculationRules->set_rules(
379 categorycode => undef,
382 patron_maxissueqty => $patron_maxissueqty,
383 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
388 Koha::CirculationRules->set_rules(
391 branchcode => $branch,
393 holdallowed => $holdallowed,
394 hold_fulfillment_policy => $hold_fulfillment_policy,
395 returnbranch => $returnbranch,
399 Koha::CirculationRules->set_rules(
401 categorycode => undef,
402 branchcode => $branch,
404 patron_maxissueqty => $patron_maxissueqty,
405 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
410 Koha::CirculationRules->set_rule(
412 branchcode => $branch,
413 categorycode => undef,
414 rule_name => 'max_holds',
415 rule_value => $max_holds,
419 elsif ($op eq "add-branch-cat") {
420 my $categorycode = $input->param('categorycode');
421 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
422 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
423 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
424 my $max_holds = $input->param('max_holds');
425 $max_holds =~ s/\s//g;
426 $max_holds = undef if $max_holds !~ /^\d+/;
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 '';
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
666 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
667 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
669 refundLostItemFeeRule => $refundLostItemFeeRule,
670 defaultRefundRule => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
673 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
675 my $itemtypes = Koha::ItemTypes->search_with_localization;
677 my $humanbranch = ( $branch ne '*' ? $branch : undef );
679 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
680 my $definedbranch = $all_rules->count ? 1 : 0;
683 while ( my $r = $all_rules->next ) {
685 $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
688 $template->param(show_branch_cat_rule_form => 1);
691 patron_categories => $patron_categories,
692 itemtypeloop => $itemtypes,
693 humanbranch => $humanbranch,
694 current_branch => $branch,
695 definedbranch => $definedbranch,
698 output_html_with_http_headers $input, $cookie, $template->output;
702 # sort by patron category, then item type, putting
703 # default entries at the bottom
704 sub by_category_and_itemtype {
705 unless (by_category($a, $b)) {
706 return by_itemtype($a, $b);
712 if ($a->{'default_humancategorycode'}) {
713 return ($b->{'default_humancategorycode'} ? 0 : 1);
714 } elsif ($b->{'default_humancategorycode'}) {
717 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
723 if ($a->{default_translated_description}) {
724 return ($b->{'default_translated_description'} ? 0 : 1);
725 } elsif ($b->{'default_translated_description'}) {
728 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
732 sub strip_non_numeric {
735 $string = '' if $string !~ /^\d+/;