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,
112 Koha
::CirculationRules
->set_rules(
114 categorycode
=> $categorycode,
119 patron_maxissueqty
=> undef,
120 patron_maxonsiteissueqty
=> undef,
125 } elsif ($categorycode eq "*") {
126 Koha
::CirculationRules
->set_rules(
128 categorycode
=> undef,
129 branchcode
=> $branch,
132 patron_maxissueqty
=> undef,
133 patron_maxonsiteissueqty
=> undef,
134 holdallowed
=> undef,
135 hold_fulfillment_policy
=> undef,
136 returnbranch
=> undef,
142 Koha
::CirculationRules
->set_rules(
144 categorycode
=> $categorycode,
145 branchcode
=> $branch,
149 patron_maxissueqty
=> undef,
150 patron_maxonsiteissueqty
=> undef,
156 elsif ($op eq 'delete-branch-item') {
157 my $itemtype = $input->param('itemtype');
158 if ($branch eq "*") {
159 if ($itemtype eq "*") {
160 Koha
::CirculationRules
->set_rules(
162 categorycode
=> undef,
166 patron_maxissueqty
=> undef,
167 patron_maxonsiteissueqty
=> undef,
168 holdallowed
=> undef,
169 hold_fulfillment_policy
=> undef,
170 returnbranch
=> undef,
175 Koha
::CirculationRules
->set_rules(
177 categorycode
=> undef,
179 itemtype
=> $itemtype,
181 holdallowed
=> undef,
182 hold_fulfillment_policy
=> undef,
183 returnbranch
=> undef,
188 } elsif ($itemtype eq "*") {
189 Koha
::CirculationRules
->set_rules(
191 categorycode
=> undef,
192 branchcode
=> $branch,
195 maxissueqty
=> undef,
196 maxonsiteissueqty
=> undef,
197 holdallowed
=> undef,
198 hold_fulfillment_policy
=> undef,
199 returnbranch
=> undef,
204 Koha
::CirculationRules
->set_rules(
206 categorycode
=> undef,
207 branchcode
=> $branch,
208 itemtype
=> $itemtype,
210 holdallowed
=> undef,
211 hold_fulfillment_policy
=> undef,
212 returnbranch
=> undef,
218 # save the values entered
219 elsif ($op eq 'add') {
220 my $br = $branch; # branch
221 my $bor = $input->param('categorycode'); # borrower category
222 my $itemtype = $input->param('itemtype'); # item type
223 my $fine = $input->param('fine');
224 my $finedays = $input->param('finedays');
225 my $maxsuspensiondays = $input->param('maxsuspensiondays');
226 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
227 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
228 my $firstremind = $input->param('firstremind');
229 my $chargeperiod = $input->param('chargeperiod');
230 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
231 my $maxissueqty = $input->param('maxissueqty');
232 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
233 my $renewalsallowed = $input->param('renewalsallowed');
234 my $renewalperiod = $input->param('renewalperiod');
235 my $norenewalbefore = $input->param('norenewalbefore');
236 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
237 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
238 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
239 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
240 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
241 $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 );
242 $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 );
243 my $reservesallowed = $input->param('reservesallowed');
244 my $holds_per_record = $input->param('holds_per_record');
245 my $holds_per_day = $input->param('holds_per_day');
246 $holds_per_day =~ s/\s//g;
247 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
248 my $onshelfholds = $input->param('onshelfholds') || 0;
249 $maxissueqty =~ s/\s//g;
250 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
251 $maxonsiteissueqty =~ s/\s//g;
252 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
253 my $issuelength = $input->param('issuelength');
254 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
255 my $lengthunit = $input->param('lengthunit');
256 my $hardduedate = $input->param('hardduedate') || undef;
257 $hardduedate = eval { dt_from_string
( $input->param('hardduedate') ) } if ( $hardduedate );
258 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
259 my $hardduedatecompare = $input->param('hardduedatecompare');
260 my $rentaldiscount = $input->param('rentaldiscount');
261 my $opacitemholds = $input->param('opacitemholds') || 0;
262 my $article_requests = $input->param('article_requests') || 'no';
263 my $overduefinescap = $input->param('overduefinescap') || undef;
264 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
265 my $note = $input->param('note');
266 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
270 categorycode
=> $bor,
271 itemtype
=> $itemtype,
273 finedays
=> $finedays,
274 maxsuspensiondays
=> $maxsuspensiondays,
275 suspension_chargeperiod
=> $suspension_chargeperiod,
276 firstremind
=> $firstremind,
277 chargeperiod
=> $chargeperiod,
278 chargeperiod_charge_at
=> $chargeperiod_charge_at,
279 renewalsallowed
=> $renewalsallowed,
280 renewalperiod
=> $renewalperiod,
281 norenewalbefore
=> $norenewalbefore,
282 auto_renew
=> $auto_renew,
283 no_auto_renewal_after
=> $no_auto_renewal_after,
284 no_auto_renewal_after_hard_limit
=> $no_auto_renewal_after_hard_limit,
285 reservesallowed
=> $reservesallowed,
286 holds_per_record
=> $holds_per_record,
287 holds_per_day
=> $holds_per_day,
288 issuelength
=> $issuelength,
289 lengthunit
=> $lengthunit,
290 hardduedate
=> $hardduedate,
291 hardduedatecompare
=> $hardduedatecompare,
292 rentaldiscount
=> $rentaldiscount,
293 onshelfholds
=> $onshelfholds,
294 opacitemholds
=> $opacitemholds,
295 overduefinescap
=> $overduefinescap,
296 cap_fine_to_replacement_price
=> $cap_fine_to_replacement_price,
297 article_requests
=> $article_requests,
301 my $issuingrule = Koha
::IssuingRules
->find({categorycode
=> $bor, itemtype
=> $itemtype, branchcode
=> $br});
303 $issuingrule->set($params)->store();
305 Koha
::IssuingRule
->new()->set($params)->store();
307 Koha
::CirculationRules
->set_rules(
309 categorycode
=> $bor,
310 itemtype
=> $itemtype,
313 maxissueqty
=> $maxissueqty,
314 maxonsiteissueqty
=> $maxonsiteissueqty,
320 elsif ($op eq "set-branch-defaults") {
321 my $categorycode = $input->param('categorycode');
322 my $patron_maxissueqty = $input->param('patron_maxissueqty');
323 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
324 my $holdallowed = $input->param('holdallowed');
325 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
326 my $returnbranch = $input->param('returnbranch');
327 my $max_holds = $input->param('max_holds');
328 $patron_maxissueqty =~ s/\s//g;
329 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
330 $patron_maxonsiteissueqty =~ s/\s//g;
331 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
332 $holdallowed =~ s/\s//g;
333 $holdallowed = undef if $holdallowed !~ /^\d+/;
334 $max_holds =~ s/\s//g;
335 $max_holds = '' if $max_holds !~ /^\d+/;
337 if ($branch eq "*") {
338 Koha
::CirculationRules
->set_rules(
340 categorycode
=> undef,
344 patron_maxissueqty
=> $patron_maxissueqty,
345 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
346 holdallowed
=> $holdallowed,
347 hold_fulfillment_policy
=> $hold_fulfillment_policy,
348 returnbranch
=> $returnbranch,
353 Koha
::CirculationRules
->set_rules(
355 categorycode
=> undef,
357 branchcode
=> $branch,
359 patron_maxissueqty
=> $patron_maxissueqty,
360 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
361 holdallowed
=> $holdallowed,
362 hold_fulfillment_policy
=> $hold_fulfillment_policy,
363 returnbranch
=> $returnbranch,
368 Koha
::CirculationRules
->set_rule(
370 branchcode
=> $branch,
371 categorycode
=> undef,
373 rule_name
=> 'max_holds',
374 rule_value
=> $max_holds,
378 elsif ($op eq "add-branch-cat") {
379 my $categorycode = $input->param('categorycode');
380 my $patron_maxissueqty = $input->param('patron_maxissueqty');
381 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
382 my $max_holds = $input->param('max_holds');
383 $patron_maxissueqty =~ s/\s//g;
384 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
385 $patron_maxonsiteissueqty =~ s/\s//g;
386 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
387 $max_holds =~ s/\s//g;
388 $max_holds = undef if $max_holds !~ /^\d+/;
390 if ($branch eq "*") {
391 if ($categorycode eq "*") {
392 Koha
::CirculationRules
->set_rules(
394 categorycode
=> undef,
398 max_holds
=> $max_holds,
399 patron_maxissueqty
=> $patron_maxissueqty,
400 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
405 Koha
::CirculationRules
->set_rules(
408 categorycode
=> $categorycode,
411 max_holds
=> $max_holds,
412 patron_maxissueqty
=> $patron_maxissueqty,
413 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
418 } elsif ($categorycode eq "*") {
419 Koha
::CirculationRules
->set_rules(
421 categorycode
=> undef,
423 branchcode
=> $branch,
425 max_holds
=> $max_holds,
426 patron_maxissueqty
=> $patron_maxissueqty,
427 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
432 Koha
::CirculationRules
->set_rules(
434 categorycode
=> $categorycode,
436 branchcode
=> $branch,
438 max_holds
=> $max_holds,
439 patron_maxissueqty
=> $patron_maxissueqty,
440 patron_maxonsiteissueqty
=> $patron_maxonsiteissueqty,
446 elsif ($op eq "add-branch-item") {
447 my $itemtype = $input->param('itemtype');
448 my $holdallowed = $input->param('holdallowed');
449 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
450 my $returnbranch = $input->param('returnbranch');
452 $holdallowed =~ s/\s//g;
453 $holdallowed = undef if $holdallowed !~ /^\d+/;
455 if ($branch eq "*") {
456 if ($itemtype eq "*") {
457 Koha
::CirculationRules
->set_rules(
459 categorycode
=> undef,
463 holdallowed
=> $holdallowed,
464 hold_fulfillment_policy
=> $hold_fulfillment_policy,
465 returnbranch
=> $returnbranch,
470 Koha
::CirculationRules
->set_rules(
472 categorycode
=> undef,
473 itemtype
=> $itemtype,
476 holdallowed
=> $holdallowed,
477 hold_fulfillment_policy
=> $hold_fulfillment_policy,
478 returnbranch
=> $returnbranch,
483 } elsif ($itemtype eq "*") {
484 Koha
::CirculationRules
->set_rules(
486 categorycode
=> undef,
488 branchcode
=> $branch,
490 holdallowed
=> $holdallowed,
491 hold_fulfillment_policy
=> $hold_fulfillment_policy,
492 returnbranch
=> $returnbranch,
497 Koha
::CirculationRules
->set_rules(
499 categorycode
=> undef,
500 itemtype
=> $itemtype,
501 branchcode
=> $branch,
503 holdallowed
=> $holdallowed,
504 hold_fulfillment_policy
=> $hold_fulfillment_policy,
505 returnbranch
=> $returnbranch,
511 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
513 my $refund = $input->param('refund');
515 if ( $refund eq '*' ) {
516 if ( $branch ne '*' ) {
517 # only do something for $refund eq '*' if branch-specific
519 # Delete it so it picks the default
520 Koha
::RefundLostItemFeeRules
->find({
521 branchcode
=> $branch
527 Koha
::RefundLostItemFeeRules
->find({
528 branchcode
=> $branch
529 }) // Koha
::RefundLostItemFeeRule
->new;
531 branchcode
=> $branch,
537 my $refundLostItemFeeRule = Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $branch });
539 refundLostItemFeeRule
=> $refundLostItemFeeRule,
540 defaultRefundRule
=> Koha
::RefundLostItemFeeRules
->_default_rule
543 my $patron_categories = Koha
::Patron
::Categories
->search({}, { order_by
=> ['description'] });
546 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
548 my $sth2 = $dbh->prepare("
549 SELECT issuingrules.*,
550 itemtypes.description AS humanitemtype,
551 categories.description AS humancategorycode,
552 COALESCE( localization.translation, itemtypes.description ) AS translated_description
555 ON (itemtypes.itemtype = issuingrules.itemtype)
557 ON (categories.categorycode = issuingrules.categorycode)
558 LEFT JOIN localization ON issuingrules.itemtype = localization.code
559 AND localization.entity = 'itemtypes'
560 AND localization.lang = ?
561 WHERE issuingrules.branchcode = ?
563 $sth2->execute($language, $branch);
565 while (my $row = $sth2->fetchrow_hashref) {
566 $row->{'current_branch'} ||= $row->{'branchcode'};
567 $row->{humanitemtype
} ||= $row->{itemtype
};
568 $row->{default_translated_description
} = 1 if $row->{humanitemtype
} eq '*';
569 $row->{'humancategorycode'} ||= $row->{'categorycode'};
570 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
571 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
572 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
573 my $harddue_dt = eval { dt_from_string
( $row->{'hardduedate'} ) };
574 $row->{'hardduedate'} = eval { output_pref
( { dt
=> $harddue_dt, dateonly
=> 1 } ) } if ( $harddue_dt );
575 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
576 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
577 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
579 $row->{'hardduedate'} = 0;
581 if ($row->{no_auto_renewal_after_hard_limit
}) {
582 my $dt = eval { dt_from_string
( $row->{no_auto_renewal_after_hard_limit
} ) };
583 $row->{no_auto_renewal_after_hard_limit
} = eval { output_pref
( { dt
=> $dt, dateonly
=> 1 } ) } if $dt;
586 push @row_loop, $row;
589 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
591 $template->param(show_branch_cat_rule_form
=> 1);
594 patron_categories
=> $patron_categories,
595 itemtypeloop
=> $itemtypes,
596 rules
=> \
@sorted_row_loop,
597 humanbranch
=> ($branch ne '*' ?
$branch : ''),
598 current_branch
=> $branch,
599 definedbranch
=> scalar(@sorted_row_loop)>0
601 output_html_with_http_headers
$input, $cookie, $template->output;
605 # sort by patron category, then item type, putting
606 # default entries at the bottom
607 sub by_category_and_itemtype
{
608 unless (by_category
($a, $b)) {
609 return by_itemtype
($a, $b);
615 if ($a->{'default_humancategorycode'}) {
616 return ($b->{'default_humancategorycode'} ?
0 : 1);
617 } elsif ($b->{'default_humancategorycode'}) {
620 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
626 if ($a->{default_translated_description
}) {
627 return ($b->{'default_translated_description'} ?
0 : 1);
628 } elsif ($b->{'default_translated_description'}) {
631 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};