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 $maxsuspensiondays = '' if $maxsuspensiondays eq q||;
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 = $input->param('maxissueqty');
262 my $maxonsiteissueqty = $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( $input->param('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 = $input->param('reservesallowed');
274 my $holds_per_record = $input->param('holds_per_record');
275 my $holds_per_day = $input->param('holds_per_day');
276 $holds_per_day =~ s/\s//g;
277 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
278 my $onshelfholds = $input->param('onshelfholds') || 0;
279 $maxissueqty =~ s/\s//g;
280 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
281 $maxonsiteissueqty =~ s/\s//g;
282 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
283 my $issuelength = $input->param('issuelength');
284 $issuelength = $issuelength eq q{} ? undef : $issuelength;
285 my $lengthunit = $input->param('lengthunit');
286 my $hardduedate = $input->param('hardduedate') || undef;
287 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
288 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
289 my $hardduedatecompare = $input->param('hardduedatecompare');
290 my $rentaldiscount = $input->param('rentaldiscount');
291 my $opacitemholds = $input->param('opacitemholds') || 0;
292 my $article_requests = $input->param('article_requests') || 'no';
293 my $overduefinescap = $input->param('overduefinescap') || '';
294 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
295 my $note = $input->param('note');
296 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
299 maxissueqty => $maxissueqty,
300 maxonsiteissueqty => $maxonsiteissueqty,
302 finedays => $finedays,
303 maxsuspensiondays => $maxsuspensiondays,
304 suspension_chargeperiod => $suspension_chargeperiod,
305 firstremind => $firstremind,
306 chargeperiod => $chargeperiod,
307 chargeperiod_charge_at => $chargeperiod_charge_at,
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 issuelength => $issuelength,
318 lengthunit => $lengthunit,
319 hardduedate => $hardduedate,
320 hardduedatecompare => $hardduedatecompare,
321 rentaldiscount => $rentaldiscount,
322 onshelfholds => $onshelfholds,
323 opacitemholds => $opacitemholds,
324 overduefinescap => $overduefinescap,
325 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
326 article_requests => $article_requests,
330 Koha::CirculationRules->set_rules(
332 categorycode => $bor eq '*' ? undef : $bor,
333 itemtype => $itemtype eq '*' ? undef : $itemtype,
334 branchcode => $br eq '*' ? undef : $br,
340 elsif ($op eq "set-branch-defaults") {
341 my $categorycode = $input->param('categorycode');
342 my $patron_maxissueqty = $input->param('patron_maxissueqty');
343 my $patron_maxonsiteissueqty = $input->param('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 = $input->param('max_holds');
348 $patron_maxissueqty =~ s/\s//g;
349 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
350 $patron_maxonsiteissueqty =~ s/\s//g;
351 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
352 $holdallowed =~ s/\s//g;
353 $holdallowed = undef if $holdallowed !~ /^\d+/;
354 $max_holds =~ s/\s//g;
355 $max_holds = '' if $max_holds !~ /^\d+/;
357 if ($branch eq "*") {
358 Koha::CirculationRules->set_rules(
363 holdallowed => $holdallowed,
364 hold_fulfillment_policy => $hold_fulfillment_policy,
365 returnbranch => $returnbranch,
369 Koha::CirculationRules->set_rules(
371 categorycode => undef,
374 patron_maxissueqty => $patron_maxissueqty,
375 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
380 Koha::CirculationRules->set_rules(
383 branchcode => $branch,
385 holdallowed => $holdallowed,
386 hold_fulfillment_policy => $hold_fulfillment_policy,
387 returnbranch => $returnbranch,
391 Koha::CirculationRules->set_rules(
393 categorycode => undef,
394 branchcode => $branch,
396 patron_maxissueqty => $patron_maxissueqty,
397 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
402 Koha::CirculationRules->set_rule(
404 branchcode => $branch,
405 categorycode => undef,
407 rule_name => 'max_holds',
408 rule_value => $max_holds,
412 elsif ($op eq "add-branch-cat") {
413 my $categorycode = $input->param('categorycode');
414 my $patron_maxissueqty = $input->param('patron_maxissueqty');
415 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
416 my $max_holds = $input->param('max_holds');
417 $patron_maxissueqty =~ s/\s//g;
418 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
419 $patron_maxonsiteissueqty =~ s/\s//g;
420 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
421 $max_holds =~ s/\s//g;
422 $max_holds = undef if $max_holds !~ /^\d+/;
424 if ($branch eq "*") {
425 if ($categorycode eq "*") {
426 Koha::CirculationRules->set_rules(
428 categorycode => undef,
431 max_holds => $max_holds,
432 patron_maxissueqty => $patron_maxissueqty,
433 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
438 Koha::CirculationRules->set_rules(
440 categorycode => $categorycode,
443 max_holds => $max_holds,
444 patron_maxissueqty => $patron_maxissueqty,
445 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
450 } elsif ($categorycode eq "*") {
451 Koha::CirculationRules->set_rules(
453 categorycode => undef,
454 branchcode => $branch,
456 max_holds => $max_holds,
457 patron_maxissueqty => $patron_maxissueqty,
458 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
463 Koha::CirculationRules->set_rules(
465 categorycode => $categorycode,
466 branchcode => $branch,
468 max_holds => $max_holds,
469 patron_maxissueqty => $patron_maxissueqty,
470 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
476 elsif ($op eq "add-branch-item") {
477 my $itemtype = $input->param('itemtype');
478 my $holdallowed = $input->param('holdallowed');
479 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
480 my $returnbranch = $input->param('returnbranch');
482 $holdallowed =~ s/\s//g;
483 $holdallowed = undef if $holdallowed !~ /^\d+/;
485 if ($branch eq "*") {
486 if ($itemtype eq "*") {
487 Koha::CirculationRules->set_rules(
492 holdallowed => $holdallowed,
493 hold_fulfillment_policy => $hold_fulfillment_policy,
494 returnbranch => $returnbranch,
499 Koha::CirculationRules->set_rules(
501 itemtype => $itemtype,
504 holdallowed => $holdallowed,
505 hold_fulfillment_policy => $hold_fulfillment_policy,
506 returnbranch => $returnbranch,
511 } elsif ($itemtype eq "*") {
512 Koha::CirculationRules->set_rules(
515 branchcode => $branch,
517 holdallowed => $holdallowed,
518 hold_fulfillment_policy => $hold_fulfillment_policy,
519 returnbranch => $returnbranch,
524 Koha::CirculationRules->set_rules(
526 itemtype => $itemtype,
527 branchcode => $branch,
529 holdallowed => $holdallowed,
530 hold_fulfillment_policy => $hold_fulfillment_policy,
531 returnbranch => $returnbranch,
537 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
539 my $refund = $input->param('refund');
541 if ( $refund eq '*' ) {
542 if ( $branch ne '*' ) {
543 # only do something for $refund eq '*' if branch-specific
544 Koha::CirculationRules->set_rules(
546 branchcode => $branch,
554 Koha::CirculationRules->set_rules(
565 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef : $branch });
567 refundLostItemFeeRule => $refundLostItemFeeRule,
568 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
571 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
573 my $itemtypes = Koha::ItemTypes->search_with_localization;
575 $template->param(show_branch_cat_rule_form => 1);
578 patron_categories => $patron_categories,
579 itemtypeloop => $itemtypes,
580 humanbranch => ( $branch ne '*' ? $branch : undef ),
581 current_branch => $branch,
583 output_html_with_http_headers $input, $cookie, $template->output;
587 # sort by patron category, then item type, putting
588 # default entries at the bottom
589 sub by_category_and_itemtype {
590 unless (by_category($a, $b)) {
591 return by_itemtype($a, $b);
597 if ($a->{'default_humancategorycode'}) {
598 return ($b->{'default_humancategorycode'} ? 0 : 1);
599 } elsif ($b->{'default_humancategorycode'}) {
602 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
608 if ($a->{default_translated_description}) {
609 return ($b->{'default_translated_description'} ? 0 : 1);
610 } elsif ($b->{'default_translated_description'}) {
613 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};