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 eq '*' ? undef : $categorycode,
85 branchcode => $branch eq '*' ? undef : $branch,
86 itemtype => $itemtype eq '*' ? undef : $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(
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,
163 patron_maxissueqty => undef,
164 patron_maxonsiteissueqty => undef,
168 Koha::CirculationRules->set_rules(
170 branchcode => $branch,
172 holdallowed => undef,
173 hold_fulfillment_policy => undef,
174 returnbranch => undef,
180 Koha::CirculationRules->set_rules(
182 categorycode => $categorycode,
183 branchcode => $branch,
186 patron_maxissueqty => undef,
187 patron_maxonsiteissueqty => undef,
193 elsif ($op eq 'delete-branch-item') {
194 my $itemtype = $input->param('itemtype');
195 if ($branch eq "*") {
196 if ($itemtype eq "*") {
197 Koha::CirculationRules->set_rules(
202 holdallowed => undef,
203 hold_fulfillment_policy => undef,
204 returnbranch => undef,
209 Koha::CirculationRules->set_rules(
212 itemtype => $itemtype,
214 holdallowed => undef,
215 hold_fulfillment_policy => undef,
216 returnbranch => undef,
221 } elsif ($itemtype eq "*") {
222 Koha::CirculationRules->set_rules(
224 branchcode => $branch,
227 holdallowed => undef,
228 hold_fulfillment_policy => undef,
229 returnbranch => undef,
234 Koha::CirculationRules->set_rules(
236 branchcode => $branch,
237 itemtype => $itemtype,
239 holdallowed => undef,
240 hold_fulfillment_policy => undef,
241 returnbranch => undef,
247 # save the values entered
248 elsif ($op eq 'add') {
249 my $br = $branch; # branch
250 my $bor = $input->param('categorycode'); # borrower category
251 my $itemtype = $input->param('itemtype'); # item type
252 my $fine = $input->param('fine');
253 my $finedays = $input->param('finedays');
254 my $maxsuspensiondays = $input->param('maxsuspensiondays');
255 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
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 = $input->param('maxissueqty');
261 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
262 my $renewalsallowed = $input->param('renewalsallowed');
263 my $renewalperiod = $input->param('renewalperiod');
264 my $norenewalbefore = $input->param('norenewalbefore');
265 $norenewalbefore = undef 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 = undef if $no_auto_renewal_after =~ /^\s*$/;
269 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
270 $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 );
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 = $input->param('reservesallowed');
273 my $holds_per_record = $input->param('holds_per_record');
274 my $holds_per_day = $input->param('holds_per_day');
275 $holds_per_day =~ s/\s//g;
276 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
277 my $onshelfholds = $input->param('onshelfholds') || 0;
278 $maxissueqty =~ s/\s//g;
279 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
280 $maxonsiteissueqty =~ s/\s//g;
281 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
282 my $issuelength = $input->param('issuelength');
283 $issuelength = $issuelength eq q{} ? undef : $issuelength;
284 my $lengthunit = $input->param('lengthunit');
285 my $hardduedate = $input->param('hardduedate') || undef;
286 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
287 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
288 my $hardduedatecompare = $input->param('hardduedatecompare');
289 my $rentaldiscount = $input->param('rentaldiscount');
290 my $opacitemholds = $input->param('opacitemholds') || 0;
291 my $article_requests = $input->param('article_requests') || 'no';
292 my $overduefinescap = $input->param('overduefinescap') || undef;
293 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
294 my $note = $input->param('note');
295 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
298 maxissueqty => $maxissueqty,
299 maxonsiteissueqty => $maxonsiteissueqty,
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 renewalsallowed => $renewalsallowed,
308 renewalperiod => $renewalperiod,
309 norenewalbefore => $norenewalbefore,
310 auto_renew => $auto_renew,
311 no_auto_renewal_after => $no_auto_renewal_after,
312 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
313 reservesallowed => $reservesallowed,
314 holds_per_record => $holds_per_record,
315 holds_per_day => $holds_per_day,
316 issuelength => $issuelength,
317 lengthunit => $lengthunit,
318 hardduedate => $hardduedate,
319 hardduedatecompare => $hardduedatecompare,
320 rentaldiscount => $rentaldiscount,
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 = $input->param('patron_maxissueqty');
342 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
343 my $holdallowed = $input->param('holdallowed');
344 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
345 my $returnbranch = $input->param('returnbranch');
346 my $max_holds = $input->param('max_holds');
347 $patron_maxissueqty =~ s/\s//g;
348 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
349 $patron_maxonsiteissueqty =~ s/\s//g;
350 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
351 $holdallowed =~ s/\s//g;
352 $holdallowed = undef if $holdallowed !~ /^\d+/;
353 $max_holds =~ s/\s//g;
354 $max_holds = '' if $max_holds !~ /^\d+/;
356 if ($branch eq "*") {
357 Koha::CirculationRules->set_rules(
362 holdallowed => $holdallowed,
363 hold_fulfillment_policy => $hold_fulfillment_policy,
364 returnbranch => $returnbranch,
368 Koha::CirculationRules->set_rules(
370 categorycode => undef,
373 patron_maxissueqty => $patron_maxissueqty,
374 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
379 Koha::CirculationRules->set_rules(
382 branchcode => $branch,
384 holdallowed => $holdallowed,
385 hold_fulfillment_policy => $hold_fulfillment_policy,
386 returnbranch => $returnbranch,
390 Koha::CirculationRules->set_rules(
392 categorycode => undef,
393 branchcode => $branch,
395 patron_maxissueqty => $patron_maxissueqty,
396 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
401 Koha::CirculationRules->set_rule(
403 branchcode => $branch,
404 categorycode => undef,
406 rule_name => 'max_holds',
407 rule_value => $max_holds,
411 elsif ($op eq "add-branch-cat") {
412 my $categorycode = $input->param('categorycode');
413 my $patron_maxissueqty = $input->param('patron_maxissueqty');
414 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
415 my $max_holds = $input->param('max_holds');
416 $patron_maxissueqty =~ s/\s//g;
417 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
418 $patron_maxonsiteissueqty =~ s/\s//g;
419 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
420 $max_holds =~ s/\s//g;
421 $max_holds = undef if $max_holds !~ /^\d+/;
423 if ($branch eq "*") {
424 if ($categorycode eq "*") {
425 Koha::CirculationRules->set_rules(
427 categorycode => undef,
430 max_holds => $max_holds,
431 patron_maxissueqty => $patron_maxissueqty,
432 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
437 Koha::CirculationRules->set_rules(
439 categorycode => $categorycode,
442 max_holds => $max_holds,
443 patron_maxissueqty => $patron_maxissueqty,
444 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
449 } elsif ($categorycode eq "*") {
450 Koha::CirculationRules->set_rules(
452 categorycode => undef,
453 branchcode => $branch,
455 max_holds => $max_holds,
456 patron_maxissueqty => $patron_maxissueqty,
457 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
462 Koha::CirculationRules->set_rules(
464 categorycode => $categorycode,
465 branchcode => $branch,
467 max_holds => $max_holds,
468 patron_maxissueqty => $patron_maxissueqty,
469 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
475 elsif ($op eq "add-branch-item") {
476 my $itemtype = $input->param('itemtype');
477 my $holdallowed = $input->param('holdallowed');
478 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
479 my $returnbranch = $input->param('returnbranch');
481 $holdallowed =~ s/\s//g;
482 $holdallowed = undef if $holdallowed !~ /^\d+/;
484 if ($branch eq "*") {
485 if ($itemtype eq "*") {
486 Koha::CirculationRules->set_rules(
491 holdallowed => $holdallowed,
492 hold_fulfillment_policy => $hold_fulfillment_policy,
493 returnbranch => $returnbranch,
498 Koha::CirculationRules->set_rules(
500 itemtype => $itemtype,
503 holdallowed => $holdallowed,
504 hold_fulfillment_policy => $hold_fulfillment_policy,
505 returnbranch => $returnbranch,
510 } elsif ($itemtype eq "*") {
511 Koha::CirculationRules->set_rules(
514 branchcode => $branch,
516 holdallowed => $holdallowed,
517 hold_fulfillment_policy => $hold_fulfillment_policy,
518 returnbranch => $returnbranch,
523 Koha::CirculationRules->set_rules(
525 itemtype => $itemtype,
526 branchcode => $branch,
528 holdallowed => $holdallowed,
529 hold_fulfillment_policy => $hold_fulfillment_policy,
530 returnbranch => $returnbranch,
536 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
538 my $refund = $input->param('refund');
540 if ( $refund eq '*' ) {
541 if ( $branch ne '*' ) {
542 # only do something for $refund eq '*' if branch-specific
543 Koha::CirculationRules->set_rules(
545 branchcode => $branch,
553 Koha::CirculationRules->set_rules(
564 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef : $branch });
566 refundLostItemFeeRule => $refundLostItemFeeRule,
567 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
570 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
572 my $itemtypes = Koha::ItemTypes->search_with_localization;
574 $template->param(show_branch_cat_rule_form => 1);
577 patron_categories => $patron_categories,
578 itemtypeloop => $itemtypes,
579 humanbranch => ( $branch ne '*' ? $branch : undef ),
580 current_branch => $branch,
582 output_html_with_http_headers $input, $cookie, $template->output;
586 # sort by patron category, then item type, putting
587 # default entries at the bottom
588 sub by_category_and_itemtype {
589 unless (by_category($a, $b)) {
590 return by_itemtype($a, $b);
596 if ($a->{'default_humancategorycode'}) {
597 return ($b->{'default_humancategorycode'} ? 0 : 1);
598 } elsif ($b->{'default_humancategorycode'}) {
601 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
607 if ($a->{default_translated_description}) {
608 return ($b->{'default_translated_description'} ? 0 : 1);
609 } elsif ($b->{'default_translated_description'}) {
612 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};