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>.
30 use Koha
::RefundLostItemFeeRules
;
32 use Koha
::CirculationRules
;
33 use Koha
::Patron
::Categories
;
38 my $dbh = C4
::Context
->dbh;
41 # $flagsrequired->{circulation}=1;
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user
({template_name
=> "admin/smart-rules.tt",
47 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
51 my $type=$input->param('type');
53 my $branch = $input->param('branch');
55 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
56 $branch = Koha
::Libraries
->search->count() == 1 ?
undef : C4
::Context
::mybranch
();
59 $branch = C4
::Context
::only_my_library
() ?
( C4
::Context
::mybranch
() || '*' ) : '*';
63 my $logged_in_patron = Koha
::Patrons
->find( $loggedinuser );
65 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters
=> 'manage_circ_rules_from_any_libraries' } );
66 $template->param( restricted_to_own_library
=> not $can_edit_from_any_library );
67 $branch = C4
::Context
::mybranch
() unless $can_edit_from_any_library;
69 my $op = $input->param('op') || q{};
70 my $language = C4
::Languages
::getlanguage
();
72 my $cache = Koha
::Caches
->get_instance;
73 $cache->clear_from_cache( Koha
::CirculationRules
::GUESSED_ITEMTYPES_KEY
);
75 if ($op eq 'delete') {
76 my $itemtype = $input->param('itemtype');
77 my $categorycode = $input->param('categorycode');
78 $debug and warn "deleting $1 $2 $branch";
80 Koha
::CirculationRules
->set_rules(
82 categorycode
=> $categorycode eq '*' ?
undef : $categorycode,
83 branchcode
=> $branch eq '*' ?
undef : $branch,
84 itemtype
=> $itemtype eq '*' ?
undef : $itemtype,
87 maxonsiteissueqty
=> undef,
88 rentaldiscount
=> undef,
91 maxsuspensiondays
=> undef,
92 suspension_chargeperiod
=> undef,
94 chargeperiod
=> undef,
95 chargeperiod_charge_at
=> undef,
100 hardduedatecompare
=> undef,
101 renewalsallowed
=> undef,
102 renewalperiod
=> undef,
103 norenewalbefore
=> undef,
105 no_auto_renewal_after
=> undef,
106 no_auto_renewal_after_hard_limit
=> undef,
107 reservesallowed
=> undef,
108 holds_per_record
=> undef,
109 holds_per_day
=> undef,
110 onshelfholds
=> undef,
111 opacitemholds
=> undef,
112 overduefinescap
=> undef,
113 cap_fine_to_replacement_price
=> undef,
114 article_requests
=> undef,
120 elsif ($op eq 'delete-branch-cat') {
121 my $categorycode = $input->param('categorycode');
122 if ($branch eq "*") {
123 if ($categorycode eq "*") {
124 Koha
::CirculationRules
->set_rules(
127 categorycode
=> undef,
130 patron_maxissueqty
=> undef,
131 patron_maxonsiteissueqty
=> undef,
135 Koha
::CirculationRules
->set_rules(
140 holdallowed
=> undef,
141 hold_fulfillment_policy
=> undef,
142 returnbranch
=> undef,
147 Koha
::CirculationRules
->set_rules(
149 categorycode
=> $categorycode,
153 patron_maxissueqty
=> undef,
154 patron_maxonsiteissueqty
=> undef,
159 } elsif ($categorycode eq "*") {
160 Koha
::CirculationRules
->set_rules(
162 branchcode
=> $branch,
163 categorycode
=> undef,
166 patron_maxissueqty
=> undef,
167 patron_maxonsiteissueqty
=> undef,
171 Koha
::CirculationRules
->set_rules(
173 branchcode
=> $branch,
176 holdallowed
=> undef,
177 hold_fulfillment_policy
=> undef,
178 returnbranch
=> undef,
183 Koha
::CirculationRules
->set_rules(
185 categorycode
=> $categorycode,
186 branchcode
=> $branch,
189 patron_maxissueqty
=> undef,
190 patron_maxonsiteissueqty
=> undef,
196 elsif ($op eq 'delete-branch-item') {
197 my $itemtype = $input->param('itemtype');
198 if ($branch eq "*") {
199 if ($itemtype eq "*") {
200 Koha
::CirculationRules
->set_rules(
205 holdallowed
=> undef,
206 hold_fulfillment_policy
=> undef,
207 returnbranch
=> undef,
212 Koha
::CirculationRules
->set_rules(
215 itemtype
=> $itemtype,
217 holdallowed
=> undef,
218 hold_fulfillment_policy
=> undef,
219 returnbranch
=> undef,
224 } elsif ($itemtype eq "*") {
225 Koha
::CirculationRules
->set_rules(
227 branchcode
=> $branch,
230 holdallowed
=> undef,
231 hold_fulfillment_policy
=> undef,
232 returnbranch
=> undef,
237 Koha
::CirculationRules
->set_rules(
239 branchcode
=> $branch,
240 itemtype
=> $itemtype,
242 holdallowed
=> undef,
243 hold_fulfillment_policy
=> undef,
244 returnbranch
=> undef,
250 # save the values entered
251 elsif ($op eq 'add') {
252 my $br = $branch; # branch
253 my $bor = $input->param('categorycode'); # borrower category
254 my $itemtype = $input->param('itemtype'); # item type
255 my $fine = $input->param('fine');
256 my $finedays = $input->param('finedays');
257 my $maxsuspensiondays = $input->param('maxsuspensiondays');
258 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
259 $maxsuspensiondays = '' if $maxsuspensiondays eq q
||;
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
($input->param('maxissueqty'));
265 my $maxonsiteissueqty = strip_non_numeric
($input->param('maxonsiteissueqty'));
266 my $renewalsallowed = $input->param('renewalsallowed');
267 my $renewalperiod = $input->param('renewalperiod');
268 my $norenewalbefore = $input->param('norenewalbefore');
269 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
270 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
271 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
272 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
273 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
274 $no_auto_renewal_after_hard_limit = eval { dt_from_string
( $input->param('no_auto_renewal_after_hard_limit') ) } if ( $no_auto_renewal_after_hard_limit );
275 $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 );
276 my $reservesallowed = strip_non_numeric
($input->param('reservesallowed'));
277 my $holds_per_record = strip_non_numeric
($input->param('holds_per_record'));
278 my $holds_per_day = strip_non_numeric
($input->param('holds_per_day'));
279 my $onshelfholds = $input->param('onshelfholds') || 0;
280 my $issuelength = $input->param('issuelength');
281 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
282 my $daysmode = $input->param('daysmode');
283 my $lengthunit = $input->param('lengthunit');
284 my $hardduedate = $input->param('hardduedate') || undef;
285 $hardduedate = eval { dt_from_string
( $input->param('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');
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 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
297 maxissueqty
=> $maxissueqty,
298 maxonsiteissueqty
=> $maxonsiteissueqty,
299 rentaldiscount
=> $rentaldiscount,
301 finedays
=> $finedays,
302 maxsuspensiondays
=> $maxsuspensiondays,
303 suspension_chargeperiod
=> $suspension_chargeperiod,
304 firstremind
=> $firstremind,
305 chargeperiod
=> $chargeperiod,
306 chargeperiod_charge_at
=> $chargeperiod_charge_at,
307 issuelength
=> $issuelength,
308 daysmode
=> $daysmode,
309 lengthunit
=> $lengthunit,
310 hardduedate
=> $hardduedate,
311 hardduedatecompare
=> $hardduedatecompare,
312 renewalsallowed
=> $renewalsallowed,
313 renewalperiod
=> $renewalperiod,
314 norenewalbefore
=> $norenewalbefore,
315 auto_renew
=> $auto_renew,
316 no_auto_renewal_after
=> $no_auto_renewal_after,
317 no_auto_renewal_after_hard_limit
=> $no_auto_renewal_after_hard_limit,
318 reservesallowed
=> $reservesallowed,
319 holds_per_record
=> $holds_per_record,
320 holds_per_day
=> $holds_per_day,
321 onshelfholds
=> $onshelfholds,
322 opacitemholds
=> $opacitemholds,
323 overduefinescap
=> $overduefinescap,
324 cap_fine_to_replacement_price
=> $cap_fine_to_replacement_price,
325 article_requests
=> $article_requests,
329 Koha
::CirculationRules
->set_rules(
331 categorycode
=> $bor eq '*' ?
undef : $bor,
332 itemtype
=> $itemtype eq '*' ?
undef : $itemtype,
333 branchcode
=> $br eq '*' ?
undef : $br,
339 elsif ($op eq "set-branch-defaults") {
340 my $categorycode = $input->param('categorycode');
341 my $patron_maxissueqty = strip_non_numeric
($input->param('patron_maxissueqty'));
342 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
343 $patron_maxonsiteissueqty = strip_non_numeric
($patron_maxonsiteissueqty);
344 my $holdallowed = $input->param('holdallowed');
345 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
346 my $returnbranch = $input->param('returnbranch');
347 my $max_holds = strip_non_numeric
($input->param('max_holds'));
348 $holdallowed =~ s/\s//g;
349 $holdallowed = undef if $holdallowed !~ /^\d+/;
351 if ($branch eq "*") {
352 Koha
::CirculationRules
->set_rules(
357 holdallowed
=> $holdallowed,
358 hold_fulfillment_policy
=> $hold_fulfillment_policy,
359 returnbranch
=> $returnbranch,
363 Koha
::CirculationRules
->set_rules(
365 categorycode
=> undef,
368 patron_maxissueqty
=> $patron_maxissueqty,
369 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
374 Koha
::CirculationRules
->set_rules(
377 branchcode
=> $branch,
379 holdallowed
=> $holdallowed,
380 hold_fulfillment_policy
=> $hold_fulfillment_policy,
381 returnbranch
=> $returnbranch,
385 Koha
::CirculationRules
->set_rules(
387 categorycode
=> undef,
388 branchcode
=> $branch,
390 patron_maxissueqty
=> $patron_maxissueqty,
391 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
396 Koha
::CirculationRules
->set_rule(
398 branchcode
=> $branch,
399 categorycode
=> undef,
400 rule_name
=> 'max_holds',
401 rule_value
=> $max_holds,
405 elsif ($op eq "add-branch-cat") {
406 my $categorycode = $input->param('categorycode');
407 my $patron_maxissueqty = strip_non_numeric
($input->param('patron_maxissueqty'));
408 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
409 $patron_maxonsiteissueqty = strip_non_numeric
($patron_maxonsiteissueqty);
410 my $max_holds = $input->param('max_holds');
411 $max_holds =~ s/\s//g;
412 $max_holds = undef if $max_holds !~ /^\d+/;
414 if ($branch eq "*") {
415 if ($categorycode eq "*") {
416 Koha
::CirculationRules
->set_rules(
418 categorycode
=> undef,
421 max_holds
=> $max_holds,
422 patron_maxissueqty
=> $patron_maxissueqty,
423 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
428 Koha
::CirculationRules
->set_rules(
430 categorycode
=> $categorycode,
433 max_holds
=> $max_holds,
434 patron_maxissueqty
=> $patron_maxissueqty,
435 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
440 } elsif ($categorycode eq "*") {
441 Koha
::CirculationRules
->set_rules(
443 categorycode
=> undef,
444 branchcode
=> $branch,
446 max_holds
=> $max_holds,
447 patron_maxissueqty
=> $patron_maxissueqty,
448 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
453 Koha
::CirculationRules
->set_rules(
455 categorycode
=> $categorycode,
456 branchcode
=> $branch,
458 max_holds
=> $max_holds,
459 patron_maxissueqty
=> $patron_maxissueqty,
460 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
466 elsif ($op eq "add-branch-item") {
467 my $itemtype = $input->param('itemtype');
468 my $holdallowed = $input->param('holdallowed');
469 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
470 my $returnbranch = $input->param('returnbranch');
472 $holdallowed =~ s/\s//g;
473 $holdallowed = undef if $holdallowed !~ /^\d+/;
475 if ($branch eq "*") {
476 if ($itemtype eq "*") {
477 Koha
::CirculationRules
->set_rules(
482 holdallowed
=> $holdallowed,
483 hold_fulfillment_policy
=> $hold_fulfillment_policy,
484 returnbranch
=> $returnbranch,
489 Koha
::CirculationRules
->set_rules(
491 itemtype
=> $itemtype,
494 holdallowed
=> $holdallowed,
495 hold_fulfillment_policy
=> $hold_fulfillment_policy,
496 returnbranch
=> $returnbranch,
501 } elsif ($itemtype eq "*") {
502 Koha
::CirculationRules
->set_rules(
505 branchcode
=> $branch,
507 holdallowed
=> $holdallowed,
508 hold_fulfillment_policy
=> $hold_fulfillment_policy,
509 returnbranch
=> $returnbranch,
514 Koha
::CirculationRules
->set_rules(
516 itemtype
=> $itemtype,
517 branchcode
=> $branch,
519 holdallowed
=> $holdallowed,
520 hold_fulfillment_policy
=> $hold_fulfillment_policy,
521 returnbranch
=> $returnbranch,
527 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
529 my $refund = $input->param('refund');
531 if ( $refund eq '*' ) {
532 if ( $branch ne '*' ) {
533 # only do something for $refund eq '*' if branch-specific
534 Koha
::CirculationRules
->set_rules(
536 branchcode
=> $branch,
544 Koha
::CirculationRules
->set_rules(
546 branchcode
=> $branch,
555 my $refundLostItemFeeRule = Koha
::RefundLostItemFeeRules
->find({ branchcode
=> ($branch eq '*') ?
undef : $branch });
557 refundLostItemFeeRule
=> $refundLostItemFeeRule,
558 defaultRefundRule
=> Koha
::RefundLostItemFeeRules
->_default_rule
561 my $patron_categories = Koha
::Patron
::Categories
->search({}, { order_by
=> ['description'] });
563 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
565 my $humanbranch = ( $branch ne '*' ?
$branch : undef );
567 my $all_rules = Koha
::CirculationRules
->search({ branchcode
=> $humanbranch });
568 my $definedbranch = $all_rules->count ?
1 : 0;
571 while ( my $r = $all_rules->next ) {
573 $rules->{ $r->{categorycode
} }->{ $r->{itemtype
} }->{ $r->{rule_name
} } = $r->{rule_value
};
576 $template->param(show_branch_cat_rule_form
=> 1);
579 patron_categories
=> $patron_categories,
580 itemtypeloop
=> $itemtypes,
581 humanbranch
=> $humanbranch,
582 current_branch
=> $branch,
583 definedbranch
=> $definedbranch,
586 output_html_with_http_headers
$input, $cookie, $template->output;
590 # sort by patron category, then item type, putting
591 # default entries at the bottom
592 sub by_category_and_itemtype
{
593 unless (by_category
($a, $b)) {
594 return by_itemtype
($a, $b);
600 if ($a->{'default_humancategorycode'}) {
601 return ($b->{'default_humancategorycode'} ?
0 : 1);
602 } elsif ($b->{'default_humancategorycode'}) {
605 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
611 if ($a->{default_translated_description
}) {
612 return ($b->{'default_translated_description'} ?
0 : 1);
613 } elsif ($b->{'default_translated_description'}) {
616 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
620 sub strip_non_numeric
{
623 $string = '' if $string !~ /^\d+/;