Bug 24031: Add safety checks in Koha::Plugins::call
[koha.git] / admin / smart-rules.pl
blob0a3349362389ac4582490e2ebb51a98873206a60
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::RefundLostItemFeeRules;
31 use Koha::Libraries;
32 use Koha::CirculationRules;
33 use Koha::Patron::Categories;
34 use Koha::Caches;
35 use Koha::Patrons;
37 my $input = CGI->new;
38 my $dbh = C4::Context->dbh;
40 # my $flagsrequired;
41 # $flagsrequired->{circulation}=1;
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user({template_name => "admin/smart-rules.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => {parameters => 'manage_circ_rules'},
48 debug => 1,
49 });
51 my $type=$input->param('type');
53 my $branch = $input->param('branch');
54 unless ( $branch ) {
55 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
56 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
58 else {
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 my $op = $input->param('op') || q{};
70 my $language = C4::Languages::getlanguage();
72 my $cache = Koha::Caches->get_instance;
73 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
75 if ($op eq 'delete') {
76 my $itemtype = $input->param('itemtype');
77 my $categorycode = $input->param('categorycode');
78 $debug and warn "deleting $1 $2 $branch";
80 Koha::CirculationRules->set_rules(
82 categorycode => $categorycode eq '*' ? undef : $categorycode,
83 branchcode => $branch eq '*' ? undef : $branch,
84 itemtype => $itemtype eq '*' ? undef : $itemtype,
85 rules => {
86 maxissueqty => undef,
87 maxonsiteissueqty => undef,
88 rentaldiscount => undef,
89 fine => undef,
90 finedays => undef,
91 maxsuspensiondays => undef,
92 suspension_chargeperiod => undef,
93 firstremind => undef,
94 chargeperiod => undef,
95 chargeperiod_charge_at => undef,
96 issuelength => undef,
97 daysmode => undef,
98 lengthunit => undef,
99 hardduedate => undef,
100 hardduedatecompare => undef,
101 renewalsallowed => undef,
102 renewalperiod => undef,
103 norenewalbefore => undef,
104 auto_renew => undef,
105 no_auto_renewal_after => undef,
106 no_auto_renewal_after_hard_limit => undef,
107 reservesallowed => undef,
108 holds_per_record => undef,
109 holds_per_day => undef,
110 onshelfholds => undef,
111 opacitemholds => undef,
112 overduefinescap => undef,
113 cap_fine_to_replacement_price => undef,
114 article_requests => undef,
115 note => undef,
120 elsif ($op eq 'delete-branch-cat') {
121 my $categorycode = $input->param('categorycode');
122 if ($branch eq "*") {
123 if ($categorycode eq "*") {
124 Koha::CirculationRules->set_rules(
126 branchcode => undef,
127 categorycode => undef,
128 rules => {
129 max_holds => undef,
130 patron_maxissueqty => undef,
131 patron_maxonsiteissueqty => undef,
135 Koha::CirculationRules->set_rules(
137 branchcode => undef,
138 itemtype => undef,
139 rules => {
140 holdallowed => undef,
141 hold_fulfillment_policy => undef,
142 returnbranch => undef,
146 } else {
147 Koha::CirculationRules->set_rules(
149 categorycode => $categorycode,
150 branchcode => undef,
151 rules => {
152 max_holds => undef,
153 patron_maxissueqty => undef,
154 patron_maxonsiteissueqty => undef,
159 } elsif ($categorycode eq "*") {
160 Koha::CirculationRules->set_rules(
162 branchcode => $branch,
163 categorycode => undef,
164 rules => {
165 max_holds => undef,
166 patron_maxissueqty => undef,
167 patron_maxonsiteissueqty => undef,
171 Koha::CirculationRules->set_rules(
173 branchcode => $branch,
174 itemtype => undef,
175 rules => {
176 holdallowed => undef,
177 hold_fulfillment_policy => undef,
178 returnbranch => undef,
182 } else {
183 Koha::CirculationRules->set_rules(
185 categorycode => $categorycode,
186 branchcode => $branch,
187 rules => {
188 max_holds => undef,
189 patron_maxissueqty => undef,
190 patron_maxonsiteissueqty => undef,
196 elsif ($op eq 'delete-branch-item') {
197 my $itemtype = $input->param('itemtype');
198 if ($branch eq "*") {
199 if ($itemtype eq "*") {
200 Koha::CirculationRules->set_rules(
202 branchcode => undef,
203 itemtype => undef,
204 rules => {
205 holdallowed => undef,
206 hold_fulfillment_policy => undef,
207 returnbranch => undef,
211 } else {
212 Koha::CirculationRules->set_rules(
214 branchcode => undef,
215 itemtype => $itemtype,
216 rules => {
217 holdallowed => undef,
218 hold_fulfillment_policy => undef,
219 returnbranch => undef,
224 } elsif ($itemtype eq "*") {
225 Koha::CirculationRules->set_rules(
227 branchcode => $branch,
228 itemtype => undef,
229 rules => {
230 holdallowed => undef,
231 hold_fulfillment_policy => undef,
232 returnbranch => undef,
236 } else {
237 Koha::CirculationRules->set_rules(
239 branchcode => $branch,
240 itemtype => $itemtype,
241 rules => {
242 holdallowed => undef,
243 hold_fulfillment_policy => undef,
244 returnbranch => undef,
250 # save the values entered
251 elsif ($op eq 'add') {
252 my $br = $branch; # branch
253 my $bor = $input->param('categorycode'); # borrower category
254 my $itemtype = $input->param('itemtype'); # item type
255 my $fine = $input->param('fine');
256 my $finedays = $input->param('finedays');
257 my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
258 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
259 my $firstremind = $input->param('firstremind');
260 my $chargeperiod = $input->param('chargeperiod');
261 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
262 my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
263 my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
264 my $renewalsallowed = $input->param('renewalsallowed');
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 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
294 my $rules = {
295 maxissueqty => $maxissueqty,
296 maxonsiteissueqty => $maxonsiteissueqty,
297 rentaldiscount => $rentaldiscount,
298 fine => $fine,
299 finedays => $finedays,
300 maxsuspensiondays => $maxsuspensiondays,
301 suspension_chargeperiod => $suspension_chargeperiod,
302 firstremind => $firstremind,
303 chargeperiod => $chargeperiod,
304 chargeperiod_charge_at => $chargeperiod_charge_at,
305 issuelength => $issuelength,
306 daysmode => $daysmode,
307 lengthunit => $lengthunit,
308 hardduedate => $hardduedate,
309 hardduedatecompare => $hardduedatecompare,
310 renewalsallowed => $renewalsallowed,
311 renewalperiod => $renewalperiod,
312 norenewalbefore => $norenewalbefore,
313 auto_renew => $auto_renew,
314 no_auto_renewal_after => $no_auto_renewal_after,
315 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
316 reservesallowed => $reservesallowed,
317 holds_per_record => $holds_per_record,
318 holds_per_day => $holds_per_day,
319 onshelfholds => $onshelfholds,
320 opacitemholds => $opacitemholds,
321 overduefinescap => $overduefinescap,
322 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
323 article_requests => $article_requests,
324 note => $note,
327 Koha::CirculationRules->set_rules(
329 categorycode => $bor eq '*' ? undef : $bor,
330 itemtype => $itemtype eq '*' ? undef : $itemtype,
331 branchcode => $br eq '*' ? undef : $br,
332 rules => $rules,
337 elsif ($op eq "set-branch-defaults") {
338 my $categorycode = $input->param('categorycode');
339 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
340 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
341 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
342 my $holdallowed = $input->param('holdallowed');
343 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
344 my $returnbranch = $input->param('returnbranch');
345 my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
346 $holdallowed =~ s/\s//g;
347 $holdallowed = undef if $holdallowed !~ /^\d+/;
349 if ($branch eq "*") {
350 Koha::CirculationRules->set_rules(
352 itemtype => undef,
353 branchcode => undef,
354 rules => {
355 holdallowed => $holdallowed,
356 hold_fulfillment_policy => $hold_fulfillment_policy,
357 returnbranch => $returnbranch,
361 Koha::CirculationRules->set_rules(
363 categorycode => undef,
364 branchcode => undef,
365 rules => {
366 patron_maxissueqty => $patron_maxissueqty,
367 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
371 } else {
372 Koha::CirculationRules->set_rules(
374 itemtype => undef,
375 branchcode => $branch,
376 rules => {
377 holdallowed => $holdallowed,
378 hold_fulfillment_policy => $hold_fulfillment_policy,
379 returnbranch => $returnbranch,
383 Koha::CirculationRules->set_rules(
385 categorycode => undef,
386 branchcode => $branch,
387 rules => {
388 patron_maxissueqty => $patron_maxissueqty,
389 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
394 Koha::CirculationRules->set_rule(
396 branchcode => $branch,
397 categorycode => undef,
398 rule_name => 'max_holds',
399 rule_value => $max_holds,
403 elsif ($op eq "add-branch-cat") {
404 my $categorycode = $input->param('categorycode');
405 my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
406 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
407 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
408 my $max_holds = $input->param('max_holds');
409 $max_holds =~ s/\s//g;
410 $max_holds = undef if $max_holds !~ /^\d+/;
412 if ($branch eq "*") {
413 if ($categorycode eq "*") {
414 Koha::CirculationRules->set_rules(
416 categorycode => undef,
417 branchcode => undef,
418 rules => {
419 max_holds => $max_holds,
420 patron_maxissueqty => $patron_maxissueqty,
421 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
425 } else {
426 Koha::CirculationRules->set_rules(
428 categorycode => $categorycode,
429 branchcode => undef,
430 rules => {
431 max_holds => $max_holds,
432 patron_maxissueqty => $patron_maxissueqty,
433 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
438 } elsif ($categorycode eq "*") {
439 Koha::CirculationRules->set_rules(
441 categorycode => undef,
442 branchcode => $branch,
443 rules => {
444 max_holds => $max_holds,
445 patron_maxissueqty => $patron_maxissueqty,
446 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
450 } else {
451 Koha::CirculationRules->set_rules(
453 categorycode => $categorycode,
454 branchcode => $branch,
455 rules => {
456 max_holds => $max_holds,
457 patron_maxissueqty => $patron_maxissueqty,
458 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
464 elsif ($op eq "add-branch-item") {
465 my $itemtype = $input->param('itemtype');
466 my $holdallowed = $input->param('holdallowed');
467 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
468 my $returnbranch = $input->param('returnbranch');
470 $holdallowed =~ s/\s//g;
471 $holdallowed = undef if $holdallowed !~ /^\d+/;
473 if ($branch eq "*") {
474 if ($itemtype eq "*") {
475 Koha::CirculationRules->set_rules(
477 itemtype => undef,
478 branchcode => undef,
479 rules => {
480 holdallowed => $holdallowed,
481 hold_fulfillment_policy => $hold_fulfillment_policy,
482 returnbranch => $returnbranch,
486 } else {
487 Koha::CirculationRules->set_rules(
489 itemtype => $itemtype,
490 branchcode => undef,
491 rules => {
492 holdallowed => $holdallowed,
493 hold_fulfillment_policy => $hold_fulfillment_policy,
494 returnbranch => $returnbranch,
499 } elsif ($itemtype eq "*") {
500 Koha::CirculationRules->set_rules(
502 itemtype => undef,
503 branchcode => $branch,
504 rules => {
505 holdallowed => $holdallowed,
506 hold_fulfillment_policy => $hold_fulfillment_policy,
507 returnbranch => $returnbranch,
511 } else {
512 Koha::CirculationRules->set_rules(
514 itemtype => $itemtype,
515 branchcode => $branch,
516 rules => {
517 holdallowed => $holdallowed,
518 hold_fulfillment_policy => $hold_fulfillment_policy,
519 returnbranch => $returnbranch,
525 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
527 my $refund = $input->param('refund');
529 if ( $refund eq '*' ) {
530 if ( $branch ne '*' ) {
531 # only do something for $refund eq '*' if branch-specific
532 Koha::CirculationRules->set_rules(
534 branchcode => $branch,
535 rules => {
536 refund => undef
541 } else {
542 Koha::CirculationRules->set_rules(
544 branchcode => $branch,
545 rules => {
546 refund => $refund
553 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef : $branch });
554 $template->param(
555 refundLostItemFeeRule => $refundLostItemFeeRule,
556 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
559 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
561 my $itemtypes = Koha::ItemTypes->search_with_localization;
563 my $humanbranch = ( $branch ne '*' ? $branch : undef );
565 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
566 my $definedbranch = $all_rules->count ? 1 : 0;
568 my $rules = {};
569 while ( my $r = $all_rules->next ) {
570 $r = $r->unblessed;
571 $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
574 $template->param(show_branch_cat_rule_form => 1);
576 $template->param(
577 patron_categories => $patron_categories,
578 itemtypeloop => $itemtypes,
579 humanbranch => $humanbranch,
580 current_branch => $branch,
581 definedbranch => $definedbranch,
582 all_rules => $rules,
584 output_html_with_http_headers $input, $cookie, $template->output;
586 exit 0;
588 # sort by patron category, then item type, putting
589 # default entries at the bottom
590 sub by_category_and_itemtype {
591 unless (by_category($a, $b)) {
592 return by_itemtype($a, $b);
596 sub by_category {
597 my ($a, $b) = @_;
598 if ($a->{'default_humancategorycode'}) {
599 return ($b->{'default_humancategorycode'} ? 0 : 1);
600 } elsif ($b->{'default_humancategorycode'}) {
601 return -1;
602 } else {
603 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
607 sub by_itemtype {
608 my ($a, $b) = @_;
609 if ($a->{default_translated_description}) {
610 return ($b->{'default_translated_description'} ? 0 : 1);
611 } elsif ($b->{'default_translated_description'}) {
612 return -1;
613 } else {
614 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
618 sub strip_non_numeric {
619 my $string = shift;
620 $string =~ s/\s//g;
621 $string = '' if $string !~ /^\d+/;
622 return $string;