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 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
71 my $op = $input->param('op') || q{};
72 my $language = C4::Languages::getlanguage();
74 my $cache = Koha::Caches->get_instance;
75 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
77 if ($op eq 'delete') {
78 my $itemtype = $input->param('itemtype');
79 my $categorycode = $input->param('categorycode');
80 $debug and warn "deleting $1 $2 $branch";
82 Koha::CirculationRules->set_rules(
84 categorycode => $categorycode,
85 branchcode => $branch,
86 itemtype => $itemtype,
88 restrictedtype => undef,
89 rentaldiscount => undef,
92 maxsuspensiondays => 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 overduefinescap => undef,
110 cap_fine_to_replacement_price => undef,
111 onshelfholds => undef,
112 opacitemholds => undef,
113 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(
124 categorycode => undef,
128 patron_maxissueqty => undef,
129 patron_maxonsiteissueqty => undef,
130 holdallowed => undef,
131 hold_fulfillment_policy => undef,
132 returnbranch => undef,
138 Koha::CirculationRules->set_rules(
140 categorycode => $categorycode,
145 patron_maxissueqty => undef,
146 patron_maxonsiteissueqty => undef,
151 } elsif ($categorycode eq "*") {
152 Koha::CirculationRules->set_rules(
154 categorycode => undef,
155 branchcode => $branch,
158 patron_maxissueqty => undef,
159 patron_maxonsiteissueqty => undef,
160 holdallowed => undef,
161 hold_fulfillment_policy => undef,
162 returnbranch => undef,
168 Koha::CirculationRules->set_rules(
170 categorycode => $categorycode,
171 branchcode => $branch,
175 patron_maxissueqty => undef,
176 patron_maxonsiteissueqty => undef,
182 elsif ($op eq 'delete-branch-item') {
183 my $itemtype = $input->param('itemtype');
184 if ($branch eq "*") {
185 if ($itemtype eq "*") {
186 Koha::CirculationRules->set_rules(
188 categorycode => undef,
192 patron_maxissueqty => undef,
193 patron_maxonsiteissueqty => undef,
194 holdallowed => undef,
195 hold_fulfillment_policy => undef,
196 returnbranch => undef,
201 Koha::CirculationRules->set_rules(
203 categorycode => undef,
205 itemtype => $itemtype,
207 holdallowed => undef,
208 hold_fulfillment_policy => undef,
209 returnbranch => undef,
214 } elsif ($itemtype eq "*") {
215 Koha::CirculationRules->set_rules(
217 categorycode => undef,
218 branchcode => $branch,
221 maxissueqty => undef,
222 maxonsiteissueqty => undef,
223 holdallowed => undef,
224 hold_fulfillment_policy => undef,
225 returnbranch => undef,
230 Koha::CirculationRules->set_rules(
232 categorycode => undef,
233 branchcode => $branch,
234 itemtype => $itemtype,
236 holdallowed => undef,
237 hold_fulfillment_policy => undef,
238 returnbranch => undef,
244 # save the values entered
245 elsif ($op eq 'add') {
246 my $br = $branch; # branch
247 my $bor = $input->param('categorycode'); # borrower category
248 my $itemtype = $input->param('itemtype'); # item type
249 my $fine = $input->param('fine');
250 my $finedays = $input->param('finedays');
251 my $maxsuspensiondays = $input->param('maxsuspensiondays');
252 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
253 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
254 my $firstremind = $input->param('firstremind');
255 my $chargeperiod = $input->param('chargeperiod');
256 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
257 my $maxissueqty = $input->param('maxissueqty');
258 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
259 my $renewalsallowed = $input->param('renewalsallowed');
260 my $renewalperiod = $input->param('renewalperiod');
261 my $norenewalbefore = $input->param('norenewalbefore');
262 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
263 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
264 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
265 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
266 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
267 $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 );
268 $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 );
269 my $reservesallowed = $input->param('reservesallowed');
270 my $holds_per_record = $input->param('holds_per_record');
271 my $holds_per_day = $input->param('holds_per_day');
272 $holds_per_day =~ s/\s//g;
273 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
274 my $onshelfholds = $input->param('onshelfholds') || 0;
275 $maxissueqty =~ s/\s//g;
276 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
277 $maxonsiteissueqty =~ s/\s//g;
278 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
279 my $issuelength = $input->param('issuelength');
280 $issuelength = $issuelength eq q{} ? undef : $issuelength;
281 my $lengthunit = $input->param('lengthunit');
282 my $hardduedate = $input->param('hardduedate') || undef;
283 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
284 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
285 my $hardduedatecompare = $input->param('hardduedatecompare');
286 my $rentaldiscount = $input->param('rentaldiscount');
287 my $opacitemholds = $input->param('opacitemholds') || 0;
288 my $article_requests = $input->param('article_requests') || 'no';
289 my $overduefinescap = $input->param('overduefinescap') || undef;
290 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
291 my $note = $input->param('note');
292 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
295 maxissueqty => $maxissueqty,
296 maxonsiteissueqty => $maxonsiteissueqty,
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 renewalsallowed => $renewalsallowed,
305 renewalperiod => $renewalperiod,
306 norenewalbefore => $norenewalbefore,
307 auto_renew => $auto_renew,
308 no_auto_renewal_after => $no_auto_renewal_after,
309 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
310 reservesallowed => $reservesallowed,
311 holds_per_record => $holds_per_record,
312 holds_per_day => $holds_per_day,
313 issuelength => $issuelength,
314 lengthunit => $lengthunit,
315 hardduedate => $hardduedate,
316 hardduedatecompare => $hardduedatecompare,
317 rentaldiscount => $rentaldiscount,
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,
329 itemtype => $itemtype,
336 elsif ($op eq "set-branch-defaults") {
337 my $categorycode = $input->param('categorycode');
338 my $patron_maxissueqty = $input->param('patron_maxissueqty');
339 my $patron_maxonsiteissueqty = $input->param('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 = $input->param('max_holds');
344 $patron_maxissueqty =~ s/\s//g;
345 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
346 $patron_maxonsiteissueqty =~ s/\s//g;
347 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
348 $holdallowed =~ s/\s//g;
349 $holdallowed = undef if $holdallowed !~ /^\d+/;
350 $max_holds =~ s/\s//g;
351 $max_holds = '' if $max_holds !~ /^\d+/;
353 if ($branch eq "*") {
354 Koha::CirculationRules->set_rules(
356 categorycode => undef,
360 patron_maxissueqty => $patron_maxissueqty,
361 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
362 holdallowed => $holdallowed,
363 hold_fulfillment_policy => $hold_fulfillment_policy,
364 returnbranch => $returnbranch,
369 Koha::CirculationRules->set_rules(
371 categorycode => undef,
373 branchcode => $branch,
375 patron_maxissueqty => $patron_maxissueqty,
376 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
377 holdallowed => $holdallowed,
378 hold_fulfillment_policy => $hold_fulfillment_policy,
379 returnbranch => $returnbranch,
384 Koha::CirculationRules->set_rule(
386 branchcode => $branch,
387 categorycode => undef,
389 rule_name => 'max_holds',
390 rule_value => $max_holds,
394 elsif ($op eq "add-branch-cat") {
395 my $categorycode = $input->param('categorycode');
396 my $patron_maxissueqty = $input->param('patron_maxissueqty');
397 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
398 my $max_holds = $input->param('max_holds');
399 $patron_maxissueqty =~ s/\s//g;
400 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
401 $patron_maxonsiteissueqty =~ s/\s//g;
402 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
403 $max_holds =~ s/\s//g;
404 $max_holds = undef if $max_holds !~ /^\d+/;
406 if ($branch eq "*") {
407 if ($categorycode eq "*") {
408 Koha::CirculationRules->set_rules(
410 categorycode => undef,
414 max_holds => $max_holds,
415 patron_maxissueqty => $patron_maxissueqty,
416 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
421 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,
439 branchcode => $branch,
441 max_holds => $max_holds,
442 patron_maxissueqty => $patron_maxissueqty,
443 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
448 Koha::CirculationRules->set_rules(
450 categorycode => $categorycode,
452 branchcode => $branch,
454 max_holds => $max_holds,
455 patron_maxissueqty => $patron_maxissueqty,
456 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
462 elsif ($op eq "add-branch-item") {
463 my $itemtype = $input->param('itemtype');
464 my $holdallowed = $input->param('holdallowed');
465 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
466 my $returnbranch = $input->param('returnbranch');
468 $holdallowed =~ s/\s//g;
469 $holdallowed = undef if $holdallowed !~ /^\d+/;
471 if ($branch eq "*") {
472 if ($itemtype eq "*") {
473 Koha::CirculationRules->set_rules(
475 categorycode => undef,
479 holdallowed => $holdallowed,
480 hold_fulfillment_policy => $hold_fulfillment_policy,
481 returnbranch => $returnbranch,
486 Koha::CirculationRules->set_rules(
488 categorycode => undef,
489 itemtype => $itemtype,
492 holdallowed => $holdallowed,
493 hold_fulfillment_policy => $hold_fulfillment_policy,
494 returnbranch => $returnbranch,
499 } elsif ($itemtype eq "*") {
500 Koha::CirculationRules->set_rules(
502 categorycode => undef,
504 branchcode => $branch,
506 holdallowed => $holdallowed,
507 hold_fulfillment_policy => $hold_fulfillment_policy,
508 returnbranch => $returnbranch,
513 Koha::CirculationRules->set_rules(
515 categorycode => undef,
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 categorycode => undef,
538 branchcode => $branch,
546 Koha::CirculationRules->set_rules(
548 categorycode => undef,
550 branchcode => $branch,
559 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef:$branch });
562 refundLostItemFeeRule => $refundLostItemFeeRule,
563 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
566 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
568 my $itemtypes = Koha::ItemTypes->search_with_localization;
570 $template->param(show_branch_cat_rule_form => 1);
573 patron_categories => $patron_categories,
574 itemtypeloop => $itemtypes,
575 humanbranch => ( $branch ne '*' ? $branch : '' ),
576 current_branch => $branch,
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'};