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",
45 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
49 my $type=$input->param('type');
51 my $branch = $input->param('branch');
53 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
54 $branch = Koha
::Libraries
->search->count() == 1 ?
undef : C4
::Context
::mybranch
();
57 $branch = C4
::Context
::only_my_library
() ?
( C4
::Context
::mybranch
() || '*' ) : '*';
61 my $logged_in_patron = Koha
::Patrons
->find( $loggedinuser );
63 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters
=> 'manage_circ_rules_from_any_libraries' } );
64 $template->param( restricted_to_own_library
=> not $can_edit_from_any_library );
65 $branch = C4
::Context
::mybranch
() unless $can_edit_from_any_library;
67 my $op = $input->param('op') || q{};
68 my $language = C4
::Languages
::getlanguage
();
70 my $cache = Koha
::Caches
->get_instance;
71 $cache->clear_from_cache( Koha
::CirculationRules
::GUESSED_ITEMTYPES_KEY
);
73 if ($op eq 'delete') {
74 my $itemtype = $input->param('itemtype');
75 my $categorycode = $input->param('categorycode');
76 $debug and warn "deleting $1 $2 $branch";
78 Koha
::CirculationRules
->set_rules(
80 categorycode
=> $categorycode eq '*' ?
undef : $categorycode,
81 branchcode
=> $branch eq '*' ?
undef : $branch,
82 itemtype
=> $itemtype eq '*' ?
undef : $itemtype,
85 maxonsiteissueqty
=> undef,
86 rentaldiscount
=> undef,
89 maxsuspensiondays
=> undef,
90 suspension_chargeperiod
=> undef,
92 chargeperiod
=> undef,
93 chargeperiod_charge_at
=> undef,
98 hardduedatecompare
=> undef,
99 renewalsallowed
=> undef,
100 renewalperiod
=> undef,
101 norenewalbefore
=> undef,
103 no_auto_renewal_after
=> undef,
104 no_auto_renewal_after_hard_limit
=> undef,
105 reservesallowed
=> undef,
106 holds_per_record
=> undef,
107 holds_per_day
=> undef,
108 onshelfholds
=> undef,
109 opacitemholds
=> undef,
110 overduefinescap
=> undef,
111 cap_fine_to_replacement_price
=> undef,
112 article_requests
=> undef,
118 elsif ($op eq 'delete-branch-cat') {
119 my $categorycode = $input->param('categorycode');
120 if ($branch eq "*") {
121 if ($categorycode eq "*") {
122 Koha
::CirculationRules
->set_rules(
125 categorycode
=> undef,
128 patron_maxissueqty
=> undef,
129 patron_maxonsiteissueqty
=> undef,
133 Koha
::CirculationRules
->set_rules(
138 holdallowed
=> undef,
139 hold_fulfillment_policy
=> undef,
140 returnbranch
=> undef,
145 Koha
::CirculationRules
->set_rules(
147 categorycode
=> $categorycode,
151 patron_maxissueqty
=> undef,
152 patron_maxonsiteissueqty
=> undef,
157 } elsif ($categorycode eq "*") {
158 Koha
::CirculationRules
->set_rules(
160 branchcode
=> $branch,
161 categorycode
=> undef,
164 patron_maxissueqty
=> undef,
165 patron_maxonsiteissueqty
=> undef,
169 Koha
::CirculationRules
->set_rules(
171 branchcode
=> $branch,
174 holdallowed
=> undef,
175 hold_fulfillment_policy
=> undef,
176 returnbranch
=> undef,
181 Koha
::CirculationRules
->set_rules(
183 categorycode
=> $categorycode,
184 branchcode
=> $branch,
187 patron_maxissueqty
=> undef,
188 patron_maxonsiteissueqty
=> undef,
194 elsif ($op eq 'delete-branch-item') {
195 my $itemtype = $input->param('itemtype');
196 if ($branch eq "*") {
197 if ($itemtype eq "*") {
198 Koha
::CirculationRules
->set_rules(
203 holdallowed
=> undef,
204 hold_fulfillment_policy
=> undef,
205 returnbranch
=> undef,
210 Koha
::CirculationRules
->set_rules(
213 itemtype
=> $itemtype,
215 holdallowed
=> undef,
216 hold_fulfillment_policy
=> undef,
217 returnbranch
=> undef,
222 } elsif ($itemtype eq "*") {
223 Koha
::CirculationRules
->set_rules(
225 branchcode
=> $branch,
228 holdallowed
=> undef,
229 hold_fulfillment_policy
=> undef,
230 returnbranch
=> undef,
235 Koha
::CirculationRules
->set_rules(
237 branchcode
=> $branch,
238 itemtype
=> $itemtype,
240 holdallowed
=> undef,
241 hold_fulfillment_policy
=> undef,
242 returnbranch
=> undef,
248 # save the values entered
249 elsif ($op eq 'add') {
250 my $br = $branch; # branch
251 my $bor = $input->param('categorycode'); # borrower category
252 my $itemtype = $input->param('itemtype'); # item type
253 my $fine = $input->param('fine');
254 my $finedays = $input->param('finedays');
255 my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
256 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
257 my $firstremind = $input->param('firstremind');
258 my $chargeperiod = $input->param('chargeperiod');
259 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
260 my $maxissueqty = strip_non_numeric
( scalar $input->param('maxissueqty') );
261 my $maxonsiteissueqty = strip_non_numeric
( scalar $input->param('maxonsiteissueqty') );
262 my $renewalsallowed = $input->param('renewalsallowed');
263 my $renewalperiod = $input->param('renewalperiod');
264 my $norenewalbefore = $input->param('norenewalbefore');
265 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
266 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
267 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
268 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
269 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
270 $no_auto_renewal_after_hard_limit = eval { dt_from_string
( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
271 $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 );
272 my $reservesallowed = strip_non_numeric
( scalar $input->param('reservesallowed') );
273 my $holds_per_record = strip_non_numeric
( scalar $input->param('holds_per_record') );
274 my $holds_per_day = strip_non_numeric
( scalar $input->param('holds_per_day') );
275 my $onshelfholds = $input->param('onshelfholds') || 0;
276 my $issuelength = $input->param('issuelength');
277 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
278 my $daysmode = $input->param('daysmode');
279 my $lengthunit = $input->param('lengthunit');
280 my $hardduedate = $input->param('hardduedate') || undef;
281 $hardduedate = eval { dt_from_string
( scalar $hardduedate ) } if ( $hardduedate );
282 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
283 my $hardduedatecompare = $input->param('hardduedatecompare');
284 my $rentaldiscount = $input->param('rentaldiscount');
285 my $opacitemholds = $input->param('opacitemholds') || 0;
286 my $article_requests = $input->param('article_requests') || 'no';
287 my $overduefinescap = $input->param('overduefinescap') || '';
288 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
289 my $note = $input->param('note');
290 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
293 maxissueqty
=> $maxissueqty,
294 maxonsiteissueqty
=> $maxonsiteissueqty,
295 rentaldiscount
=> $rentaldiscount,
297 finedays
=> $finedays,
298 maxsuspensiondays
=> $maxsuspensiondays,
299 suspension_chargeperiod
=> $suspension_chargeperiod,
300 firstremind
=> $firstremind,
301 chargeperiod
=> $chargeperiod,
302 chargeperiod_charge_at
=> $chargeperiod_charge_at,
303 issuelength
=> $issuelength,
304 daysmode
=> $daysmode,
305 lengthunit
=> $lengthunit,
306 hardduedate
=> $hardduedate,
307 hardduedatecompare
=> $hardduedatecompare,
308 renewalsallowed
=> $renewalsallowed,
309 renewalperiod
=> $renewalperiod,
310 norenewalbefore
=> $norenewalbefore,
311 auto_renew
=> $auto_renew,
312 no_auto_renewal_after
=> $no_auto_renewal_after,
313 no_auto_renewal_after_hard_limit
=> $no_auto_renewal_after_hard_limit,
314 reservesallowed
=> $reservesallowed,
315 holds_per_record
=> $holds_per_record,
316 holds_per_day
=> $holds_per_day,
317 onshelfholds
=> $onshelfholds,
318 opacitemholds
=> $opacitemholds,
319 overduefinescap
=> $overduefinescap,
320 cap_fine_to_replacement_price
=> $cap_fine_to_replacement_price,
321 article_requests
=> $article_requests,
325 Koha
::CirculationRules
->set_rules(
327 categorycode
=> $bor eq '*' ?
undef : $bor,
328 itemtype
=> $itemtype eq '*' ?
undef : $itemtype,
329 branchcode
=> $br eq '*' ?
undef : $br,
335 elsif ($op eq "set-branch-defaults") {
336 my $categorycode = $input->param('categorycode');
337 my $patron_maxissueqty = strip_non_numeric
( scalar $input->param('patron_maxissueqty') );
338 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
339 $patron_maxonsiteissueqty = strip_non_numeric
($patron_maxonsiteissueqty);
340 my $holdallowed = $input->param('holdallowed');
341 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
342 my $returnbranch = $input->param('returnbranch');
343 my $max_holds = strip_non_numeric
( scalar $input->param('max_holds') );
345 if ($branch eq "*") {
346 Koha
::CirculationRules
->set_rules(
351 holdallowed
=> $holdallowed,
352 hold_fulfillment_policy
=> $hold_fulfillment_policy,
353 returnbranch
=> $returnbranch,
357 Koha
::CirculationRules
->set_rules(
359 categorycode
=> undef,
362 patron_maxissueqty
=> $patron_maxissueqty,
363 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
368 Koha
::CirculationRules
->set_rules(
371 branchcode
=> $branch,
373 holdallowed
=> $holdallowed,
374 hold_fulfillment_policy
=> $hold_fulfillment_policy,
375 returnbranch
=> $returnbranch,
379 Koha
::CirculationRules
->set_rules(
381 categorycode
=> undef,
382 branchcode
=> $branch,
384 patron_maxissueqty
=> $patron_maxissueqty,
385 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
390 Koha
::CirculationRules
->set_rule(
392 branchcode
=> $branch,
393 categorycode
=> undef,
394 rule_name
=> 'max_holds',
395 rule_value
=> $max_holds,
399 elsif ($op eq "add-branch-cat") {
400 my $categorycode = $input->param('categorycode');
401 my $patron_maxissueqty = strip_non_numeric
( scalar $input->param('patron_maxissueqty') );
402 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
403 $patron_maxonsiteissueqty = strip_non_numeric
($patron_maxonsiteissueqty);
404 my $max_holds = $input->param('max_holds');
405 $max_holds =~ s/\s//g;
406 $max_holds = undef if $max_holds !~ /^\d+/;
408 if ($branch eq "*") {
409 if ($categorycode eq "*") {
410 Koha
::CirculationRules
->set_rules(
412 categorycode
=> undef,
415 max_holds
=> $max_holds,
416 patron_maxissueqty
=> $patron_maxissueqty,
417 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
422 Koha
::CirculationRules
->set_rules(
424 categorycode
=> $categorycode,
427 max_holds
=> $max_holds,
428 patron_maxissueqty
=> $patron_maxissueqty,
429 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
434 } elsif ($categorycode eq "*") {
435 Koha
::CirculationRules
->set_rules(
437 categorycode
=> undef,
438 branchcode
=> $branch,
440 max_holds
=> $max_holds,
441 patron_maxissueqty
=> $patron_maxissueqty,
442 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
447 Koha
::CirculationRules
->set_rules(
449 categorycode
=> $categorycode,
450 branchcode
=> $branch,
452 max_holds
=> $max_holds,
453 patron_maxissueqty
=> $patron_maxissueqty,
454 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
460 elsif ($op eq "add-branch-item") {
461 my $itemtype = $input->param('itemtype');
462 my $holdallowed = $input->param('holdallowed');
463 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
464 my $returnbranch = $input->param('returnbranch');
466 if ($branch eq "*") {
467 if ($itemtype eq "*") {
468 Koha
::CirculationRules
->set_rules(
473 holdallowed
=> $holdallowed,
474 hold_fulfillment_policy
=> $hold_fulfillment_policy,
475 returnbranch
=> $returnbranch,
480 Koha
::CirculationRules
->set_rules(
482 itemtype
=> $itemtype,
485 holdallowed
=> $holdallowed,
486 hold_fulfillment_policy
=> $hold_fulfillment_policy,
487 returnbranch
=> $returnbranch,
492 } elsif ($itemtype eq "*") {
493 Koha
::CirculationRules
->set_rules(
496 branchcode
=> $branch,
498 holdallowed
=> $holdallowed,
499 hold_fulfillment_policy
=> $hold_fulfillment_policy,
500 returnbranch
=> $returnbranch,
505 Koha
::CirculationRules
->set_rules(
507 itemtype
=> $itemtype,
508 branchcode
=> $branch,
510 holdallowed
=> $holdallowed,
511 hold_fulfillment_policy
=> $hold_fulfillment_policy,
512 returnbranch
=> $returnbranch,
518 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
520 my $refund = $input->param('refund');
522 if ( $refund eq '*' ) {
523 if ( $branch ne '*' ) {
524 # only do something for $refund eq '*' if branch-specific
525 Koha
::CirculationRules
->set_rules(
527 branchcode
=> $branch,
535 Koha
::CirculationRules
->set_rules(
537 branchcode
=> $branch,
546 my $refundLostItemFeeRule = Koha
::CirculationRules
->find({ branchcode
=> ($branch eq '*') ?
undef : $branch, rule_name
=> 'refund' });
547 my $defaultLostItemFeeRule = Koha
::CirculationRules
->find({ branchcode
=> undef, rule_name
=> 'refund' });
549 refundLostItemFeeRule
=> $refundLostItemFeeRule,
550 defaultRefundRule
=> $defaultLostItemFeeRule ?
$defaultLostItemFeeRule->rule_value : 1
553 my $patron_categories = Koha
::Patron
::Categories
->search({}, { order_by
=> ['description'] });
555 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
557 my $humanbranch = ( $branch ne '*' ?
$branch : undef );
559 my $all_rules = Koha
::CirculationRules
->search({ branchcode
=> $humanbranch });
560 my $definedbranch = $all_rules->count ?
1 : 0;
563 while ( my $r = $all_rules->next ) {
565 $rules->{ $r->{categorycode
} // '' }->{ $r->{itemtype
} // '' }->{ $r->{rule_name
} } = $r->{rule_value
};
568 $template->param(show_branch_cat_rule_form
=> 1);
571 patron_categories
=> $patron_categories,
572 itemtypeloop
=> $itemtypes,
573 humanbranch
=> $humanbranch,
574 current_branch
=> $branch,
575 definedbranch
=> $definedbranch,
578 output_html_with_http_headers
$input, $cookie, $template->output;
582 # sort by patron category, then item type, putting
583 # default entries at the bottom
584 sub by_category_and_itemtype
{
585 unless (by_category
($a, $b)) {
586 return by_itemtype
($a, $b);
592 if ($a->{'default_humancategorycode'}) {
593 return ($b->{'default_humancategorycode'} ?
0 : 1);
594 } elsif ($b->{'default_humancategorycode'}) {
597 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
603 if ($a->{default_translated_description
}) {
604 return ($b->{'default_translated_description'} ?
0 : 1);
605 } elsif ($b->{'default_translated_description'}) {
608 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
612 sub strip_non_numeric
{
615 $string = '' if $string !~ /^\d+/;