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>.
29 use Koha::IssuingRule;
30 use Koha::IssuingRules;
32 use Koha::RefundLostItemFeeRule;
33 use Koha::RefundLostItemFeeRules;
35 use Koha::CirculationRules;
36 use Koha::Patron::Categories;
41 my $dbh = C4::Context->dbh;
44 # $flagsrequired->{circulation}=1;
45 my ($template, $loggedinuser, $cookie)
46 = get_template_and_user({template_name => "admin/smart-rules.tt",
50 flagsrequired => {parameters => 'manage_circ_rules'},
54 my $type=$input->param('type');
56 my $branch = $input->param('branch');
58 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
59 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
62 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
66 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
68 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
69 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
70 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
72 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
74 my $op = $input->param('op') || q{};
75 my $language = C4::Languages::getlanguage();
77 my $cache = Koha::Caches->get_instance;
78 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
80 if ($op eq 'delete') {
81 my $itemtype = $input->param('itemtype');
82 my $categorycode = $input->param('categorycode');
83 $debug and warn "deleting $1 $2 $branch";
85 Koha::IssuingRules->find({
86 branchcode => $branch,
87 categorycode => $categorycode,
92 elsif ($op eq 'delete-branch-cat') {
93 my $categorycode = $input->param('categorycode');
95 if ($categorycode eq "*") {
96 Koha::CirculationRules->set_rules(
98 categorycode => undef,
102 patron_maxissueqty => undef,
103 patron_maxonsiteissueqty => undef,
104 holdallowed => undef,
105 hold_fulfillment_policy => undef,
106 returnbranch => undef,
111 Koha::CirculationRules->set_rules(
113 categorycode => $categorycode,
118 patron_maxissueqty => undef,
119 patron_maxonsiteissueqty => undef,
124 } elsif ($categorycode eq "*") {
125 Koha::CirculationRules->set_rules(
127 categorycode => undef,
128 branchcode => $branch,
131 patron_maxissueqty => undef,
132 patron_maxonsiteissueqty => undef,
133 holdallowed => undef,
134 hold_fulfillment_policy => undef,
135 returnbranch => undef,
140 Koha::CirculationRules->set_rules(
142 categorycode => $categorycode,
143 branchcode => $branch,
147 patron_maxissueqty => undef,
148 patron_maxonsiteissueqty => undef,
154 elsif ($op eq 'delete-branch-item') {
155 my $itemtype = $input->param('itemtype');
156 if ($branch eq "*") {
157 if ($itemtype eq "*") {
158 Koha::CirculationRules->set_rules(
160 categorycode => undef,
164 patron_maxissueqty => undef,
165 patron_maxonsiteissueqty => undef,
166 holdallowed => undef,
167 hold_fulfillment_policy => undef,
168 returnbranch => undef,
173 Koha::CirculationRules->set_rules(
175 categorycode => undef,
177 itemtype => $itemtype,
179 holdallowed => undef,
180 hold_fulfillment_policy => undef,
181 returnbranch => undef,
186 } elsif ($itemtype eq "*") {
187 Koha::CirculationRules->set_rules(
189 categorycode => undef,
190 branchcode => $branch,
193 maxissueqty => undef,
194 maxonsiteissueqty => undef,
195 holdallowed => undef,
196 hold_fulfillment_policy => undef,
197 returnbranch => undef,
202 Koha::CirculationRules->set_rules(
204 categorycode => undef,
205 branchcode => $branch,
206 itemtype => $itemtype,
208 holdallowed => undef,
209 hold_fulfillment_policy => undef,
210 returnbranch => undef,
216 # save the values entered
217 elsif ($op eq 'add') {
218 my $br = $branch; # branch
219 my $bor = $input->param('categorycode'); # borrower category
220 my $itemtype = $input->param('itemtype'); # item type
221 my $fine = $input->param('fine');
222 my $finedays = $input->param('finedays');
223 my $maxsuspensiondays = $input->param('maxsuspensiondays');
224 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
225 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
226 my $firstremind = $input->param('firstremind');
227 my $chargeperiod = $input->param('chargeperiod');
228 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
229 my $maxissueqty = $input->param('maxissueqty');
230 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
231 my $renewalsallowed = $input->param('renewalsallowed');
232 my $renewalperiod = $input->param('renewalperiod');
233 my $norenewalbefore = $input->param('norenewalbefore');
234 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
235 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
236 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
237 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
238 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
239 $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 );
240 $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 );
241 my $reservesallowed = $input->param('reservesallowed');
242 my $holds_per_record = $input->param('holds_per_record');
243 my $holds_per_day = $input->param('holds_per_day');
244 $holds_per_day =~ s/\s//g;
245 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
246 my $onshelfholds = $input->param('onshelfholds') || 0;
247 $maxissueqty =~ s/\s//g;
248 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
249 $maxonsiteissueqty =~ s/\s//g;
250 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
251 my $issuelength = $input->param('issuelength');
252 $issuelength = $issuelength eq q{} ? undef : $issuelength;
253 my $lengthunit = $input->param('lengthunit');
254 my $hardduedate = $input->param('hardduedate') || undef;
255 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
256 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
257 my $hardduedatecompare = $input->param('hardduedatecompare');
258 my $rentaldiscount = $input->param('rentaldiscount');
259 my $opacitemholds = $input->param('opacitemholds') || 0;
260 my $article_requests = $input->param('article_requests') || 'no';
261 my $overduefinescap = $input->param('overduefinescap') || undef;
262 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
263 my $note = $input->param('note');
264 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
268 categorycode => $bor,
269 itemtype => $itemtype,
271 finedays => $finedays,
272 maxsuspensiondays => $maxsuspensiondays,
273 suspension_chargeperiod => $suspension_chargeperiod,
274 firstremind => $firstremind,
275 chargeperiod => $chargeperiod,
276 chargeperiod_charge_at => $chargeperiod_charge_at,
277 renewalsallowed => $renewalsallowed,
278 renewalperiod => $renewalperiod,
279 norenewalbefore => $norenewalbefore,
280 auto_renew => $auto_renew,
281 no_auto_renewal_after => $no_auto_renewal_after,
282 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
283 reservesallowed => $reservesallowed,
284 holds_per_record => $holds_per_record,
285 holds_per_day => $holds_per_day,
286 issuelength => $issuelength,
287 lengthunit => $lengthunit,
288 hardduedate => $hardduedate,
289 hardduedatecompare => $hardduedatecompare,
290 rentaldiscount => $rentaldiscount,
291 onshelfholds => $onshelfholds,
292 opacitemholds => $opacitemholds,
293 overduefinescap => $overduefinescap,
294 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
295 article_requests => $article_requests,
299 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
301 $issuingrule->set($params)->store();
303 Koha::IssuingRule->new()->set($params)->store();
305 Koha::CirculationRules->set_rules(
307 categorycode => $bor,
308 itemtype => $itemtype,
311 maxissueqty => $maxissueqty,
312 maxonsiteissueqty => $maxonsiteissueqty,
318 elsif ($op eq "set-branch-defaults") {
319 my $categorycode = $input->param('categorycode');
320 my $patron_maxissueqty = $input->param('patron_maxissueqty');
321 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
322 my $holdallowed = $input->param('holdallowed');
323 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
324 my $returnbranch = $input->param('returnbranch');
325 my $max_holds = $input->param('max_holds');
326 $patron_maxissueqty =~ s/\s//g;
327 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
328 $patron_maxonsiteissueqty =~ s/\s//g;
329 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
330 $holdallowed =~ s/\s//g;
331 $holdallowed = undef if $holdallowed !~ /^\d+/;
332 $max_holds =~ s/\s//g;
333 $max_holds = '' if $max_holds !~ /^\d+/;
335 if ($branch eq "*") {
336 Koha::CirculationRules->set_rules(
338 categorycode => undef,
342 patron_maxissueqty => $patron_maxissueqty,
343 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
344 holdallowed => $holdallowed,
345 hold_fulfillment_policy => $hold_fulfillment_policy,
346 returnbranch => $returnbranch,
351 Koha::CirculationRules->set_rules(
353 categorycode => undef,
355 branchcode => $branch,
357 patron_maxissueqty => $patron_maxissueqty,
358 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
359 holdallowed => $holdallowed,
360 hold_fulfillment_policy => $hold_fulfillment_policy,
361 returnbranch => $returnbranch,
366 Koha::CirculationRules->set_rule(
368 branchcode => $branch,
369 categorycode => undef,
371 rule_name => 'max_holds',
372 rule_value => $max_holds,
376 elsif ($op eq "add-branch-cat") {
377 my $categorycode = $input->param('categorycode');
378 my $patron_maxissueqty = $input->param('patron_maxissueqty');
379 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
380 my $max_holds = $input->param('max_holds');
381 $patron_maxissueqty =~ s/\s//g;
382 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
383 $patron_maxonsiteissueqty =~ s/\s//g;
384 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
385 $max_holds =~ s/\s//g;
386 $max_holds = undef if $max_holds !~ /^\d+/;
388 if ($branch eq "*") {
389 if ($categorycode eq "*") {
390 Koha::CirculationRules->set_rules(
392 categorycode => undef,
396 max_holds => $max_holds,
397 patron_maxissueqty => $patron_maxissueqty,
398 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
403 Koha::CirculationRules->set_rules(
406 categorycode => $categorycode,
409 max_holds => $max_holds,
410 patron_maxissueqty => $patron_maxissueqty,
411 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
416 } elsif ($categorycode eq "*") {
417 Koha::CirculationRules->set_rules(
419 categorycode => undef,
421 branchcode => $branch,
423 max_holds => $max_holds,
424 patron_maxissueqty => $patron_maxissueqty,
425 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
430 Koha::CirculationRules->set_rules(
432 categorycode => $categorycode,
434 branchcode => $branch,
436 max_holds => $max_holds,
437 patron_maxissueqty => $patron_maxissueqty,
438 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
444 elsif ($op eq "add-branch-item") {
445 my $itemtype = $input->param('itemtype');
446 my $holdallowed = $input->param('holdallowed');
447 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
448 my $returnbranch = $input->param('returnbranch');
450 $holdallowed =~ s/\s//g;
451 $holdallowed = undef if $holdallowed !~ /^\d+/;
453 if ($branch eq "*") {
454 if ($itemtype eq "*") {
455 Koha::CirculationRules->set_rules(
457 categorycode => undef,
461 holdallowed => $holdallowed,
462 hold_fulfillment_policy => $hold_fulfillment_policy,
463 returnbranch => $returnbranch,
468 Koha::CirculationRules->set_rules(
470 categorycode => undef,
471 itemtype => $itemtype,
474 holdallowed => $holdallowed,
475 hold_fulfillment_policy => $hold_fulfillment_policy,
476 returnbranch => $returnbranch,
481 } elsif ($itemtype eq "*") {
482 Koha::CirculationRules->set_rules(
484 categorycode => undef,
486 branchcode => $branch,
488 holdallowed => $holdallowed,
489 hold_fulfillment_policy => $hold_fulfillment_policy,
490 returnbranch => $returnbranch,
495 Koha::CirculationRules->set_rules(
497 categorycode => undef,
498 itemtype => $itemtype,
499 branchcode => $branch,
501 holdallowed => $holdallowed,
502 hold_fulfillment_policy => $hold_fulfillment_policy,
503 returnbranch => $returnbranch,
509 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
511 my $refund = $input->param('refund');
513 if ( $refund eq '*' ) {
514 if ( $branch ne '*' ) {
515 # only do something for $refund eq '*' if branch-specific
517 # Delete it so it picks the default
518 Koha::RefundLostItemFeeRules->find({
519 branchcode => $branch
525 Koha::RefundLostItemFeeRules->find({
526 branchcode => $branch
527 }) // Koha::RefundLostItemFeeRule->new;
529 branchcode => $branch,
535 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
537 refundLostItemFeeRule => $refundLostItemFeeRule,
538 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
541 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
544 my $itemtypes = Koha::ItemTypes->search_with_localization;
546 my $sth2 = $dbh->prepare("
547 SELECT issuingrules.*,
548 itemtypes.description AS humanitemtype,
549 categories.description AS humancategorycode,
550 COALESCE( localization.translation, itemtypes.description ) AS translated_description
553 ON (itemtypes.itemtype = issuingrules.itemtype)
555 ON (categories.categorycode = issuingrules.categorycode)
556 LEFT JOIN localization ON issuingrules.itemtype = localization.code
557 AND localization.entity = 'itemtypes'
558 AND localization.lang = ?
559 WHERE issuingrules.branchcode = ?
561 $sth2->execute($language, $branch);
563 while (my $row = $sth2->fetchrow_hashref) {
564 $row->{'current_branch'} ||= $row->{'branchcode'};
565 $row->{humanitemtype} ||= $row->{itemtype};
566 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
567 $row->{'humancategorycode'} ||= $row->{'categorycode'};
568 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
569 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
570 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
571 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
572 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
573 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
574 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
575 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
577 $row->{'hardduedate'} = 0;
579 if ($row->{no_auto_renewal_after_hard_limit}) {
580 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
581 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
584 push @row_loop, $row;
587 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
589 $template->param(show_branch_cat_rule_form => 1);
592 patron_categories => $patron_categories,
593 itemtypeloop => $itemtypes,
594 rules => \@sorted_row_loop,
595 humanbranch => ($branch ne '*' ? $branch : ''),
596 current_branch => $branch,
597 definedbranch => scalar(@sorted_row_loop)>0
599 output_html_with_http_headers $input, $cookie, $template->output;
603 # sort by patron category, then item type, putting
604 # default entries at the bottom
605 sub by_category_and_itemtype {
606 unless (by_category($a, $b)) {
607 return by_itemtype($a, $b);
613 if ($a->{'default_humancategorycode'}) {
614 return ($b->{'default_humancategorycode'} ? 0 : 1);
615 } elsif ($b->{'default_humancategorycode'}) {
618 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
624 if ($a->{default_translated_description}) {
625 return ($b->{'default_translated_description'} ? 0 : 1);
626 } elsif ($b->{'default_translated_description'}) {
629 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};