Bug 26922: Regression tests
[koha.git] / admin / smart-rules.pl
blobb1fa57b4da1bbc10b5b6aba30fbf91ddc7be942e
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Debug;
27 use Koha::DateUtils;
28 use Koha::Database;
29 use Koha::Logger;
30 use Koha::Libraries;
31 use Koha::CirculationRules;
32 use Koha::Patron::Categories;
33 use Koha::Caches;
34 use Koha::Patrons;
36 my $input = CGI->new;
37 my $dbh = C4::Context->dbh;
39 # my $flagsrequired;
40 # $flagsrequired->{circulation}=1;
41 my ($template, $loggedinuser, $cookie)
42 = get_template_and_user({template_name => "admin/smart-rules.tt",
43 query => $input,
44 type => "intranet",
45 flagsrequired => {parameters => 'manage_circ_rules'},
46 debug => 1,
47 });
49 my $type=$input->param('type');
51 my $branch = $input->param('branch');
52 unless ( $branch ) {
53 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
54 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
56 else {
57 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
61 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
63 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
64 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
65 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
67 my $op = $input->param('op') || q{};
68 my $language = C4::Languages::getlanguage();
70 my $cache = Koha::Caches->get_instance;
71 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
73 if ($op eq 'delete') {
74 my $itemtype = $input->param('itemtype');
75 my $categorycode = $input->param('categorycode');
76 $debug and warn "deleting $1 $2 $branch";
78 Koha::CirculationRules->set_rules(
80 categorycode => $categorycode eq '*' ? undef : $categorycode,
81 branchcode => $branch eq '*' ? undef : $branch,
82 itemtype => $itemtype eq '*' ? undef : $itemtype,
83 rules => {
84 maxissueqty => undef,
85 maxonsiteissueqty => undef,
86 rentaldiscount => undef,
87 fine => undef,
88 finedays => undef,
89 maxsuspensiondays => undef,
90 suspension_chargeperiod => undef,
91 firstremind => undef,
92 chargeperiod => undef,
93 chargeperiod_charge_at => undef,
94 issuelength => undef,
95 daysmode => undef,
96 lengthunit => undef,
97 hardduedate => undef,
98 hardduedatecompare => undef,
99 renewalsallowed => undef,
100 unseen_renewals_allowed => undef,
101 renewalperiod => undef,
102 norenewalbefore => undef,
103 auto_renew => undef,
104 no_auto_renewal_after => undef,
105 no_auto_renewal_after_hard_limit => undef,
106 reservesallowed => undef,
107 holds_per_record => undef,
108 holds_per_day => undef,
109 onshelfholds => undef,
110 opacitemholds => undef,
111 overduefinescap => undef,
112 cap_fine_to_replacement_price => undef,
113 article_requests => undef,
114 note => undef,
119 elsif ($op eq 'delete-branch-cat') {
120 my $categorycode = $input->param('categorycode');
121 if ($branch eq "*") {
122 if ($categorycode eq "*") {
123 Koha::CirculationRules->set_rules(
125 branchcode => undef,
126 categorycode => undef,
127 rules => {
128 max_holds => undef,
129 patron_maxissueqty => undef,
130 patron_maxonsiteissueqty => undef,
134 Koha::CirculationRules->set_rules(
136 branchcode => undef,
137 itemtype => undef,
138 rules => {
139 holdallowed => undef,
140 hold_fulfillment_policy => undef,
141 returnbranch => undef,
145 } else {
146 Koha::CirculationRules->set_rules(
148 categorycode => $categorycode,
149 branchcode => undef,
150 rules => {
151 max_holds => undef,
152 patron_maxissueqty => undef,
153 patron_maxonsiteissueqty => undef,
158 } elsif ($categorycode eq "*") {
159 Koha::CirculationRules->set_rules(
161 branchcode => $branch,
162 categorycode => undef,
163 rules => {
164 max_holds => undef,
165 patron_maxissueqty => undef,
166 patron_maxonsiteissueqty => undef,
170 Koha::CirculationRules->set_rules(
172 branchcode => $branch,
173 itemtype => undef,
174 rules => {
175 holdallowed => undef,
176 hold_fulfillment_policy => undef,
177 returnbranch => undef,
181 } else {
182 Koha::CirculationRules->set_rules(
184 categorycode => $categorycode,
185 branchcode => $branch,
186 rules => {
187 max_holds => undef,
188 patron_maxissueqty => undef,
189 patron_maxonsiteissueqty => undef,
195 elsif ($op eq 'delete-branch-item') {
196 my $itemtype = $input->param('itemtype');
197 if ($branch eq "*") {
198 if ($itemtype eq "*") {
199 Koha::CirculationRules->set_rules(
201 branchcode => undef,
202 itemtype => undef,
203 rules => {
204 holdallowed => undef,
205 hold_fulfillment_policy => undef,
206 returnbranch => undef,
210 } else {
211 Koha::CirculationRules->set_rules(
213 branchcode => undef,
214 itemtype => $itemtype,
215 rules => {
216 holdallowed => undef,
217 hold_fulfillment_policy => undef,
218 returnbranch => undef,
223 } elsif ($itemtype eq "*") {
224 Koha::CirculationRules->set_rules(
226 branchcode => $branch,
227 itemtype => undef,
228 rules => {
229 holdallowed => undef,
230 hold_fulfillment_policy => undef,
231 returnbranch => undef,
235 } else {
236 Koha::CirculationRules->set_rules(
238 branchcode => $branch,
239 itemtype => $itemtype,
240 rules => {
241 holdallowed => undef,
242 hold_fulfillment_policy => undef,
243 returnbranch => undef,
249 # save the values entered
250 elsif ($op eq 'add') {
251 my $br = $branch; # branch
252 my $bor = $input->param('categorycode'); # borrower category
253 my $itemtype = $input->param('itemtype'); # item type
254 my $fine = $input->param('fine');
255 my $finedays = $input->param('finedays');
256 my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
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 = strip_non_numeric( scalar $input->param('maxissueqty') );
262 my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
263 my $renewalsallowed = $input->param('renewalsallowed');
264 my $unseen_renewals_allowed = $input->param('unseen_renewals_allowed');
265 my $renewalperiod = $input->param('renewalperiod');
266 my $norenewalbefore = $input->param('norenewalbefore');
267 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
268 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
269 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
270 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
271 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
272 $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
273 $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 );
274 my $reservesallowed = strip_non_numeric( scalar $input->param('reservesallowed') );
275 my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
276 my $holds_per_day = strip_non_numeric( scalar $input->param('holds_per_day') );
277 my $onshelfholds = $input->param('onshelfholds') || 0;
278 my $issuelength = $input->param('issuelength');
279 $issuelength = $issuelength eq q{} ? undef : $issuelength;
280 my $daysmode = $input->param('daysmode');
281 my $lengthunit = $input->param('lengthunit');
282 my $hardduedate = $input->param('hardduedate') || undef;
283 $hardduedate = eval { dt_from_string( scalar $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') || '';
290 my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
291 my $note = $input->param('note');
292 my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
293 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
295 my $rules = {
296 maxissueqty => $maxissueqty,
297 maxonsiteissueqty => $maxonsiteissueqty,
298 rentaldiscount => $rentaldiscount,
299 fine => $fine,
300 finedays => $finedays,
301 maxsuspensiondays => $maxsuspensiondays,
302 suspension_chargeperiod => $suspension_chargeperiod,
303 firstremind => $firstremind,
304 chargeperiod => $chargeperiod,
305 chargeperiod_charge_at => $chargeperiod_charge_at,
306 issuelength => $issuelength,
307 daysmode => $daysmode,
308 lengthunit => $lengthunit,
309 hardduedate => $hardduedate,
310 hardduedatecompare => $hardduedatecompare,
311 renewalsallowed => $renewalsallowed,
312 unseen_renewals_allowed => $unseen_renewals_allowed,
313 renewalperiod => $renewalperiod,
314 norenewalbefore => $norenewalbefore,
315 auto_renew => $auto_renew,
316 no_auto_renewal_after => $no_auto_renewal_after,
317 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
318 reservesallowed => $reservesallowed,
319 holds_per_record => $holds_per_record,
320 holds_per_day => $holds_per_day,
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,
326 note => $note,
327 decreaseloanholds => $decreaseloanholds,
330 Koha::CirculationRules->set_rules(
332 categorycode => $bor eq '*' ? undef : $bor,
333 itemtype => $itemtype eq '*' ? undef : $itemtype,
334 branchcode => $br eq '*' ? undef : $br,
335 rules => $rules,
340 elsif ($op eq "set-branch-defaults") {
341 my $categorycode = $input->param('categorycode');
342 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
343 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
344 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
345 my $holdallowed = $input->param('holdallowed');
346 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
347 my $returnbranch = $input->param('returnbranch');
348 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
350 if ($branch eq "*") {
351 Koha::CirculationRules->set_rules(
353 itemtype => undef,
354 branchcode => undef,
355 rules => {
356 holdallowed => $holdallowed,
357 hold_fulfillment_policy => $hold_fulfillment_policy,
358 returnbranch => $returnbranch,
362 Koha::CirculationRules->set_rules(
364 categorycode => undef,
365 branchcode => undef,
366 rules => {
367 patron_maxissueqty => $patron_maxissueqty,
368 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
372 } else {
373 Koha::CirculationRules->set_rules(
375 itemtype => undef,
376 branchcode => $branch,
377 rules => {
378 holdallowed => $holdallowed,
379 hold_fulfillment_policy => $hold_fulfillment_policy,
380 returnbranch => $returnbranch,
384 Koha::CirculationRules->set_rules(
386 categorycode => undef,
387 branchcode => $branch,
388 rules => {
389 patron_maxissueqty => $patron_maxissueqty,
390 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
395 Koha::CirculationRules->set_rule(
397 branchcode => $branch,
398 categorycode => undef,
399 rule_name => 'max_holds',
400 rule_value => $max_holds,
404 elsif ($op eq "add-branch-cat") {
405 my $categorycode = $input->param('categorycode');
406 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
407 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
408 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
409 my $max_holds = $input->param('max_holds');
410 $max_holds =~ s/\s//g;
411 $max_holds = undef if $max_holds !~ /^\d+/;
413 if ($branch eq "*") {
414 if ($categorycode eq "*") {
415 Koha::CirculationRules->set_rules(
417 categorycode => undef,
418 branchcode => undef,
419 rules => {
420 max_holds => $max_holds,
421 patron_maxissueqty => $patron_maxissueqty,
422 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
426 } else {
427 Koha::CirculationRules->set_rules(
429 categorycode => $categorycode,
430 branchcode => undef,
431 rules => {
432 max_holds => $max_holds,
433 patron_maxissueqty => $patron_maxissueqty,
434 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
439 } elsif ($categorycode eq "*") {
440 Koha::CirculationRules->set_rules(
442 categorycode => undef,
443 branchcode => $branch,
444 rules => {
445 max_holds => $max_holds,
446 patron_maxissueqty => $patron_maxissueqty,
447 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
451 } else {
452 Koha::CirculationRules->set_rules(
454 categorycode => $categorycode,
455 branchcode => $branch,
456 rules => {
457 max_holds => $max_holds,
458 patron_maxissueqty => $patron_maxissueqty,
459 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
465 elsif ($op eq "add-branch-item") {
466 my $itemtype = $input->param('itemtype');
467 my $holdallowed = $input->param('holdallowed');
468 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
469 my $returnbranch = $input->param('returnbranch');
471 if ($branch eq "*") {
472 if ($itemtype eq "*") {
473 Koha::CirculationRules->set_rules(
475 itemtype => undef,
476 branchcode => undef,
477 rules => {
478 holdallowed => $holdallowed,
479 hold_fulfillment_policy => $hold_fulfillment_policy,
480 returnbranch => $returnbranch,
484 } else {
485 Koha::CirculationRules->set_rules(
487 itemtype => $itemtype,
488 branchcode => undef,
489 rules => {
490 holdallowed => $holdallowed,
491 hold_fulfillment_policy => $hold_fulfillment_policy,
492 returnbranch => $returnbranch,
497 } elsif ($itemtype eq "*") {
498 Koha::CirculationRules->set_rules(
500 itemtype => undef,
501 branchcode => $branch,
502 rules => {
503 holdallowed => $holdallowed,
504 hold_fulfillment_policy => $hold_fulfillment_policy,
505 returnbranch => $returnbranch,
509 } else {
510 Koha::CirculationRules->set_rules(
512 itemtype => $itemtype,
513 branchcode => $branch,
514 rules => {
515 holdallowed => $holdallowed,
516 hold_fulfillment_policy => $hold_fulfillment_policy,
517 returnbranch => $returnbranch,
523 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
525 my $lostreturn = $input->param('lostreturn');
527 if ( $lostreturn eq '*' ) {
528 if ( $branch ne '*' ) {
529 # only do something for $lostreturn eq '*' if branch-specific
530 Koha::CirculationRules->set_rules(
532 branchcode => $branch,
533 rules => {
534 lostreturn => undef
539 } else {
540 Koha::CirculationRules->set_rules(
542 branchcode => $branch,
543 rules => {
544 lostreturn => $lostreturn
551 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
552 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
553 $template->param(
554 refundLostItemFeeRule => $refundLostItemFeeRule,
555 defaultRefundRule => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
558 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
560 my $itemtypes = Koha::ItemTypes->search_with_localization;
562 my $humanbranch = ( $branch ne '*' ? $branch : undef );
564 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
565 my $definedbranch = $all_rules->count ? 1 : 0;
567 my $rules = {};
568 while ( my $r = $all_rules->next ) {
569 $r = $r->unblessed;
570 $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
573 $template->param(show_branch_cat_rule_form => 1);
575 $template->param(
576 patron_categories => $patron_categories,
577 itemtypeloop => $itemtypes,
578 humanbranch => $humanbranch,
579 current_branch => $branch,
580 definedbranch => $definedbranch,
581 all_rules => $rules,
583 output_html_with_http_headers $input, $cookie, $template->output;
585 exit 0;
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);
595 sub by_category {
596 my ($a, $b) = @_;
597 if ($a->{'default_humancategorycode'}) {
598 return ($b->{'default_humancategorycode'} ? 0 : 1);
599 } elsif ($b->{'default_humancategorycode'}) {
600 return -1;
601 } else {
602 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
606 sub by_itemtype {
607 my ($a, $b) = @_;
608 if ($a->{default_translated_description}) {
609 return ($b->{'default_translated_description'} ? 0 : 1);
610 } elsif ($b->{'default_translated_description'}) {
611 return -1;
612 } else {
613 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
617 sub strip_non_numeric {
618 my $string = shift;
619 $string =~ s/\s//g;
620 $string = '' if $string !~ /^\d+/;
621 return $string;