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>.
31 use Koha
::CirculationRules
;
32 use Koha
::Patron
::Categories
;
37 my $dbh = C4
::Context
->dbh;
40 # $flagsrequired->{circulation}=1;
41 my ($template, $loggedinuser, $cookie)
42 = get_template_and_user
({template_name
=> "admin/smart-rules.tt",
46 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
50 my $type=$input->param('type');
52 my $branch = $input->param('branch');
54 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
55 $branch = Koha
::Libraries
->search->count() == 1 ?
undef : C4
::Context
::mybranch
();
58 $branch = C4
::Context
::only_my_library
() ?
( C4
::Context
::mybranch
() || '*' ) : '*';
62 my $logged_in_patron = Koha
::Patrons
->find( $loggedinuser );
64 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters
=> 'manage_circ_rules_from_any_libraries' } );
65 $template->param( restricted_to_own_library
=> not $can_edit_from_any_library );
66 $branch = C4
::Context
::mybranch
() unless $can_edit_from_any_library;
68 my $op = $input->param('op') || q{};
69 my $language = C4
::Languages
::getlanguage
();
71 my $cache = Koha
::Caches
->get_instance;
72 $cache->clear_from_cache( Koha
::CirculationRules
::GUESSED_ITEMTYPES_KEY
);
74 if ($op eq 'delete') {
75 my $itemtype = $input->param('itemtype');
76 my $categorycode = $input->param('categorycode');
77 $debug and warn "deleting $1 $2 $branch";
79 Koha
::CirculationRules
->set_rules(
81 categorycode
=> $categorycode eq '*' ?
undef : $categorycode,
82 branchcode
=> $branch eq '*' ?
undef : $branch,
83 itemtype
=> $itemtype eq '*' ?
undef : $itemtype,
86 maxonsiteissueqty
=> undef,
87 rentaldiscount
=> undef,
90 maxsuspensiondays
=> undef,
91 suspension_chargeperiod
=> undef,
93 chargeperiod
=> undef,
94 chargeperiod_charge_at
=> undef,
99 hardduedatecompare
=> undef,
100 renewalsallowed
=> undef,
101 renewalperiod
=> undef,
102 norenewalbefore
=> undef,
104 no_auto_renewal_after
=> undef,
105 no_auto_renewal_after_hard_limit
=> undef,
106 reservesallowed
=> undef,
107 holds_per_record
=> undef,
108 holds_per_day
=> undef,
109 onshelfholds
=> undef,
110 opacitemholds
=> undef,
111 overduefinescap
=> undef,
112 cap_fine_to_replacement_price
=> undef,
113 article_requests
=> undef,
119 elsif ($op eq 'delete-branch-cat') {
120 my $categorycode = $input->param('categorycode');
121 if ($branch eq "*") {
122 if ($categorycode eq "*") {
123 Koha
::CirculationRules
->set_rules(
126 categorycode
=> undef,
129 patron_maxissueqty
=> undef,
130 patron_maxonsiteissueqty
=> undef,
134 Koha
::CirculationRules
->set_rules(
139 holdallowed
=> undef,
140 hold_fulfillment_policy
=> undef,
141 returnbranch
=> undef,
146 Koha
::CirculationRules
->set_rules(
148 categorycode
=> $categorycode,
152 patron_maxissueqty
=> undef,
153 patron_maxonsiteissueqty
=> undef,
158 } elsif ($categorycode eq "*") {
159 Koha
::CirculationRules
->set_rules(
161 branchcode
=> $branch,
162 categorycode
=> undef,
165 patron_maxissueqty
=> undef,
166 patron_maxonsiteissueqty
=> undef,
170 Koha
::CirculationRules
->set_rules(
172 branchcode
=> $branch,
175 holdallowed
=> undef,
176 hold_fulfillment_policy
=> undef,
177 returnbranch
=> undef,
182 Koha
::CirculationRules
->set_rules(
184 categorycode
=> $categorycode,
185 branchcode
=> $branch,
188 patron_maxissueqty
=> undef,
189 patron_maxonsiteissueqty
=> undef,
195 elsif ($op eq 'delete-branch-item') {
196 my $itemtype = $input->param('itemtype');
197 if ($branch eq "*") {
198 if ($itemtype eq "*") {
199 Koha
::CirculationRules
->set_rules(
204 holdallowed
=> undef,
205 hold_fulfillment_policy
=> undef,
206 returnbranch
=> undef,
211 Koha
::CirculationRules
->set_rules(
214 itemtype
=> $itemtype,
216 holdallowed
=> undef,
217 hold_fulfillment_policy
=> undef,
218 returnbranch
=> undef,
223 } elsif ($itemtype eq "*") {
224 Koha
::CirculationRules
->set_rules(
226 branchcode
=> $branch,
229 holdallowed
=> undef,
230 hold_fulfillment_policy
=> undef,
231 returnbranch
=> undef,
236 Koha
::CirculationRules
->set_rules(
238 branchcode
=> $branch,
239 itemtype
=> $itemtype,
241 holdallowed
=> undef,
242 hold_fulfillment_policy
=> undef,
243 returnbranch
=> undef,
249 # save the values entered
250 elsif ($op eq 'add') {
251 my $br = $branch; # branch
252 my $bor = $input->param('categorycode'); # borrower category
253 my $itemtype = $input->param('itemtype'); # item type
254 my $fine = $input->param('fine');
255 my $finedays = $input->param('finedays');
256 my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
257 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
258 my $firstremind = $input->param('firstremind');
259 my $chargeperiod = $input->param('chargeperiod');
260 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
261 my $maxissueqty = strip_non_numeric
( scalar $input->param('maxissueqty') );
262 my $maxonsiteissueqty = strip_non_numeric
( scalar $input->param('maxonsiteissueqty') );
263 my $renewalsallowed = $input->param('renewalsallowed');
264 my $renewalperiod = $input->param('renewalperiod');
265 my $norenewalbefore = $input->param('norenewalbefore');
266 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
267 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
268 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
269 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
270 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
271 $no_auto_renewal_after_hard_limit = eval { dt_from_string
( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
272 $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 );
273 my $reservesallowed = strip_non_numeric
( scalar $input->param('reservesallowed') );
274 my $holds_per_record = strip_non_numeric
( scalar $input->param('holds_per_record') );
275 my $holds_per_day = strip_non_numeric
( scalar $input->param('holds_per_day') );
276 my $onshelfholds = $input->param('onshelfholds') || 0;
277 my $issuelength = $input->param('issuelength');
278 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
279 my $daysmode = $input->param('daysmode');
280 my $lengthunit = $input->param('lengthunit');
281 my $hardduedate = $input->param('hardduedate') || undef;
282 $hardduedate = eval { dt_from_string
( scalar $hardduedate ) } if ( $hardduedate );
283 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
284 my $hardduedatecompare = $input->param('hardduedatecompare');
285 my $rentaldiscount = $input->param('rentaldiscount');
286 my $opacitemholds = $input->param('opacitemholds') || 0;
287 my $article_requests = $input->param('article_requests') || 'no';
288 my $overduefinescap = $input->param('overduefinescap') || '';
289 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
290 my $note = $input->param('note');
291 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
294 maxissueqty
=> $maxissueqty,
295 maxonsiteissueqty
=> $maxonsiteissueqty,
296 rentaldiscount
=> $rentaldiscount,
298 finedays
=> $finedays,
299 maxsuspensiondays
=> $maxsuspensiondays,
300 suspension_chargeperiod
=> $suspension_chargeperiod,
301 firstremind
=> $firstremind,
302 chargeperiod
=> $chargeperiod,
303 chargeperiod_charge_at
=> $chargeperiod_charge_at,
304 issuelength
=> $issuelength,
305 daysmode
=> $daysmode,
306 lengthunit
=> $lengthunit,
307 hardduedate
=> $hardduedate,
308 hardduedatecompare
=> $hardduedatecompare,
309 renewalsallowed
=> $renewalsallowed,
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,
326 Koha
::CirculationRules
->set_rules(
328 categorycode
=> $bor eq '*' ?
undef : $bor,
329 itemtype
=> $itemtype eq '*' ?
undef : $itemtype,
330 branchcode
=> $br eq '*' ?
undef : $br,
336 elsif ($op eq "set-branch-defaults") {
337 my $categorycode = $input->param('categorycode');
338 my $patron_maxissueqty = strip_non_numeric
( scalar $input->param('patron_maxissueqty') );
339 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
340 $patron_maxonsiteissueqty = strip_non_numeric
($patron_maxonsiteissueqty);
341 my $holdallowed = $input->param('holdallowed');
342 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
343 my $returnbranch = $input->param('returnbranch');
344 my $max_holds = strip_non_numeric
( scalar $input->param('max_holds') );
345 $holdallowed =~ s/\s//g;
346 $holdallowed = undef if $holdallowed !~ /^\d+/;
348 if ($branch eq "*") {
349 Koha
::CirculationRules
->set_rules(
354 holdallowed
=> $holdallowed,
355 hold_fulfillment_policy
=> $hold_fulfillment_policy,
356 returnbranch
=> $returnbranch,
360 Koha
::CirculationRules
->set_rules(
362 categorycode
=> undef,
365 patron_maxissueqty
=> $patron_maxissueqty,
366 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
371 Koha
::CirculationRules
->set_rules(
374 branchcode
=> $branch,
376 holdallowed
=> $holdallowed,
377 hold_fulfillment_policy
=> $hold_fulfillment_policy,
378 returnbranch
=> $returnbranch,
382 Koha
::CirculationRules
->set_rules(
384 categorycode
=> undef,
385 branchcode
=> $branch,
387 patron_maxissueqty
=> $patron_maxissueqty,
388 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
393 Koha
::CirculationRules
->set_rule(
395 branchcode
=> $branch,
396 categorycode
=> undef,
397 rule_name
=> 'max_holds',
398 rule_value
=> $max_holds,
402 elsif ($op eq "add-branch-cat") {
403 my $categorycode = $input->param('categorycode');
404 my $patron_maxissueqty = strip_non_numeric
( scalar $input->param('patron_maxissueqty') );
405 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
406 $patron_maxonsiteissueqty = strip_non_numeric
($patron_maxonsiteissueqty);
407 my $max_holds = $input->param('max_holds');
408 $max_holds =~ s/\s//g;
409 $max_holds = undef if $max_holds !~ /^\d+/;
411 if ($branch eq "*") {
412 if ($categorycode eq "*") {
413 Koha
::CirculationRules
->set_rules(
415 categorycode
=> undef,
418 max_holds
=> $max_holds,
419 patron_maxissueqty
=> $patron_maxissueqty,
420 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
425 Koha
::CirculationRules
->set_rules(
427 categorycode
=> $categorycode,
430 max_holds
=> $max_holds,
431 patron_maxissueqty
=> $patron_maxissueqty,
432 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
437 } elsif ($categorycode eq "*") {
438 Koha
::CirculationRules
->set_rules(
440 categorycode
=> undef,
441 branchcode
=> $branch,
443 max_holds
=> $max_holds,
444 patron_maxissueqty
=> $patron_maxissueqty,
445 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
450 Koha
::CirculationRules
->set_rules(
452 categorycode
=> $categorycode,
453 branchcode
=> $branch,
455 max_holds
=> $max_holds,
456 patron_maxissueqty
=> $patron_maxissueqty,
457 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
463 elsif ($op eq "add-branch-item") {
464 my $itemtype = $input->param('itemtype');
465 my $holdallowed = $input->param('holdallowed');
466 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
467 my $returnbranch = $input->param('returnbranch');
469 $holdallowed =~ s/\s//g;
470 $holdallowed = undef if $holdallowed !~ /^\d+/;
472 if ($branch eq "*") {
473 if ($itemtype eq "*") {
474 Koha
::CirculationRules
->set_rules(
479 holdallowed
=> $holdallowed,
480 hold_fulfillment_policy
=> $hold_fulfillment_policy,
481 returnbranch
=> $returnbranch,
486 Koha
::CirculationRules
->set_rules(
488 itemtype
=> $itemtype,
491 holdallowed
=> $holdallowed,
492 hold_fulfillment_policy
=> $hold_fulfillment_policy,
493 returnbranch
=> $returnbranch,
498 } elsif ($itemtype eq "*") {
499 Koha
::CirculationRules
->set_rules(
502 branchcode
=> $branch,
504 holdallowed
=> $holdallowed,
505 hold_fulfillment_policy
=> $hold_fulfillment_policy,
506 returnbranch
=> $returnbranch,
511 Koha
::CirculationRules
->set_rules(
513 itemtype
=> $itemtype,
514 branchcode
=> $branch,
516 holdallowed
=> $holdallowed,
517 hold_fulfillment_policy
=> $hold_fulfillment_policy,
518 returnbranch
=> $returnbranch,
524 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
526 my $refund = $input->param('refund');
528 if ( $refund eq '*' ) {
529 if ( $branch ne '*' ) {
530 # only do something for $refund eq '*' if branch-specific
531 Koha
::CirculationRules
->set_rules(
533 branchcode
=> $branch,
541 Koha
::CirculationRules
->set_rules(
543 branchcode
=> $branch,
552 my $refundLostItemFeeRule = Koha
::CirculationRules
->find({ branchcode
=> ($branch eq '*') ?
undef : $branch, rule_name
=> 'refund' });
553 my $defaultLostItemFeeRule = Koha
::CirculationRules
->find({ branchcode
=> undef, rule_name
=> 'refund' });
555 refundLostItemFeeRule
=> $refundLostItemFeeRule,
556 defaultRefundRule
=> $defaultLostItemFeeRule ?
$defaultLostItemFeeRule->rule_value : 1
559 my $patron_categories = Koha
::Patron
::Categories
->search({}, { order_by
=> ['description'] });
561 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
563 my $humanbranch = ( $branch ne '*' ?
$branch : undef );
565 my $all_rules = Koha
::CirculationRules
->search({ branchcode
=> $humanbranch });
566 my $definedbranch = $all_rules->count ?
1 : 0;
569 while ( my $r = $all_rules->next ) {
571 $rules->{ $r->{categorycode
} // '' }->{ $r->{itemtype
} // '' }->{ $r->{rule_name
} } = $r->{rule_value
};
574 $template->param(show_branch_cat_rule_form
=> 1);
577 patron_categories
=> $patron_categories,
578 itemtypeloop
=> $itemtypes,
579 humanbranch
=> $humanbranch,
580 current_branch
=> $branch,
581 definedbranch
=> $definedbranch,
584 output_html_with_http_headers
$input, $cookie, $template->output;
588 # sort by patron category, then item type, putting
589 # default entries at the bottom
590 sub by_category_and_itemtype
{
591 unless (by_category
($a, $b)) {
592 return by_itemtype
($a, $b);
598 if ($a->{'default_humancategorycode'}) {
599 return ($b->{'default_humancategorycode'} ?
0 : 1);
600 } elsif ($b->{'default_humancategorycode'}) {
603 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
609 if ($a->{default_translated_description
}) {
610 return ($b->{'default_translated_description'} ?
0 : 1);
611 } elsif ($b->{'default_translated_description'}) {
614 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
618 sub strip_non_numeric
{
621 $string = '' if $string !~ /^\d+/;