Bug 25322: fix for not selected "relationship" defaults to father
[koha.git] / admin / smart-rules.pl
blob20da1b1d6d188b986ffcb02da3aa43c6baa35d90
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 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
259 $maxsuspensiondays = '' if $maxsuspensiondays eq q||;
260 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
261 my $firstremind = $input->param('firstremind');
262 my $chargeperiod = $input->param('chargeperiod');
263 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
264 my $maxissueqty = strip_non_numeric($input->param('maxissueqty'));
265 my $maxonsiteissueqty = strip_non_numeric($input->param('maxonsiteissueqty'));
266 my $renewalsallowed = $input->param('renewalsallowed');
267 my $renewalperiod = $input->param('renewalperiod');
268 my $norenewalbefore = $input->param('norenewalbefore');
269 $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
270 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
271 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
272 $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
273 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
274 $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 );
275 $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 );
276 my $reservesallowed = strip_non_numeric($input->param('reservesallowed'));
277 my $holds_per_record = strip_non_numeric($input->param('holds_per_record'));
278 my $holds_per_day = strip_non_numeric($input->param('holds_per_day'));
279 my $onshelfholds = $input->param('onshelfholds') || 0;
280 my $issuelength = $input->param('issuelength');
281 $issuelength = $issuelength eq q{} ? undef : $issuelength;
282 my $daysmode = $input->param('daysmode');
283 my $lengthunit = $input->param('lengthunit');
284 my $hardduedate = $input->param('hardduedate') || undef;
285 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
286 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
287 my $hardduedatecompare = $input->param('hardduedatecompare');
288 my $rentaldiscount = $input->param('rentaldiscount');
289 my $opacitemholds = $input->param('opacitemholds') || 0;
290 my $article_requests = $input->param('article_requests') || 'no';
291 my $overduefinescap = $input->param('overduefinescap') || '';
292 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
293 my $note = $input->param('note');
294 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
296 my $rules = {
297 maxissueqty => $maxissueqty,
298 maxonsiteissueqty => $maxonsiteissueqty,
299 rentaldiscount => $rentaldiscount,
300 fine => $fine,
301 finedays => $finedays,
302 maxsuspensiondays => $maxsuspensiondays,
303 suspension_chargeperiod => $suspension_chargeperiod,
304 firstremind => $firstremind,
305 chargeperiod => $chargeperiod,
306 chargeperiod_charge_at => $chargeperiod_charge_at,
307 issuelength => $issuelength,
308 daysmode => $daysmode,
309 lengthunit => $lengthunit,
310 hardduedate => $hardduedate,
311 hardduedatecompare => $hardduedatecompare,
312 renewalsallowed => $renewalsallowed,
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,
329 Koha::CirculationRules->set_rules(
331 categorycode => $bor eq '*' ? undef : $bor,
332 itemtype => $itemtype eq '*' ? undef : $itemtype,
333 branchcode => $br eq '*' ? undef : $br,
334 rules => $rules,
339 elsif ($op eq "set-branch-defaults") {
340 my $categorycode = $input->param('categorycode');
341 my $patron_maxissueqty = strip_non_numeric($input->param('patron_maxissueqty'));
342 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
343 $patron_maxonsiteissueqty = strip_non_numeric($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 = strip_non_numeric($input->param('max_holds'));
348 $holdallowed =~ s/\s//g;
349 $holdallowed = undef if $holdallowed !~ /^\d+/;
351 if ($branch eq "*") {
352 Koha::CirculationRules->set_rules(
354 itemtype => undef,
355 branchcode => undef,
356 rules => {
357 holdallowed => $holdallowed,
358 hold_fulfillment_policy => $hold_fulfillment_policy,
359 returnbranch => $returnbranch,
363 Koha::CirculationRules->set_rules(
365 categorycode => undef,
366 branchcode => undef,
367 rules => {
368 patron_maxissueqty => $patron_maxissueqty,
369 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
373 } else {
374 Koha::CirculationRules->set_rules(
376 itemtype => undef,
377 branchcode => $branch,
378 rules => {
379 holdallowed => $holdallowed,
380 hold_fulfillment_policy => $hold_fulfillment_policy,
381 returnbranch => $returnbranch,
385 Koha::CirculationRules->set_rules(
387 categorycode => undef,
388 branchcode => $branch,
389 rules => {
390 patron_maxissueqty => $patron_maxissueqty,
391 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
396 Koha::CirculationRules->set_rule(
398 branchcode => $branch,
399 categorycode => undef,
400 rule_name => 'max_holds',
401 rule_value => $max_holds,
405 elsif ($op eq "add-branch-cat") {
406 my $categorycode = $input->param('categorycode');
407 my $patron_maxissueqty = strip_non_numeric($input->param('patron_maxissueqty'));
408 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
409 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
410 my $max_holds = $input->param('max_holds');
411 $max_holds =~ s/\s//g;
412 $max_holds = undef if $max_holds !~ /^\d+/;
414 if ($branch eq "*") {
415 if ($categorycode eq "*") {
416 Koha::CirculationRules->set_rules(
418 categorycode => undef,
419 branchcode => undef,
420 rules => {
421 max_holds => $max_holds,
422 patron_maxissueqty => $patron_maxissueqty,
423 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
427 } else {
428 Koha::CirculationRules->set_rules(
430 categorycode => $categorycode,
431 branchcode => undef,
432 rules => {
433 max_holds => $max_holds,
434 patron_maxissueqty => $patron_maxissueqty,
435 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
440 } elsif ($categorycode eq "*") {
441 Koha::CirculationRules->set_rules(
443 categorycode => undef,
444 branchcode => $branch,
445 rules => {
446 max_holds => $max_holds,
447 patron_maxissueqty => $patron_maxissueqty,
448 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
452 } else {
453 Koha::CirculationRules->set_rules(
455 categorycode => $categorycode,
456 branchcode => $branch,
457 rules => {
458 max_holds => $max_holds,
459 patron_maxissueqty => $patron_maxissueqty,
460 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
466 elsif ($op eq "add-branch-item") {
467 my $itemtype = $input->param('itemtype');
468 my $holdallowed = $input->param('holdallowed');
469 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
470 my $returnbranch = $input->param('returnbranch');
472 $holdallowed =~ s/\s//g;
473 $holdallowed = undef if $holdallowed !~ /^\d+/;
475 if ($branch eq "*") {
476 if ($itemtype eq "*") {
477 Koha::CirculationRules->set_rules(
479 itemtype => undef,
480 branchcode => undef,
481 rules => {
482 holdallowed => $holdallowed,
483 hold_fulfillment_policy => $hold_fulfillment_policy,
484 returnbranch => $returnbranch,
488 } else {
489 Koha::CirculationRules->set_rules(
491 itemtype => $itemtype,
492 branchcode => undef,
493 rules => {
494 holdallowed => $holdallowed,
495 hold_fulfillment_policy => $hold_fulfillment_policy,
496 returnbranch => $returnbranch,
501 } elsif ($itemtype eq "*") {
502 Koha::CirculationRules->set_rules(
504 itemtype => undef,
505 branchcode => $branch,
506 rules => {
507 holdallowed => $holdallowed,
508 hold_fulfillment_policy => $hold_fulfillment_policy,
509 returnbranch => $returnbranch,
513 } else {
514 Koha::CirculationRules->set_rules(
516 itemtype => $itemtype,
517 branchcode => $branch,
518 rules => {
519 holdallowed => $holdallowed,
520 hold_fulfillment_policy => $hold_fulfillment_policy,
521 returnbranch => $returnbranch,
527 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
529 my $refund = $input->param('refund');
531 if ( $refund eq '*' ) {
532 if ( $branch ne '*' ) {
533 # only do something for $refund eq '*' if branch-specific
534 Koha::CirculationRules->set_rules(
536 branchcode => $branch,
537 rules => {
538 refund => undef
543 } else {
544 Koha::CirculationRules->set_rules(
546 branchcode => $branch,
547 rules => {
548 refund => $refund
555 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef : $branch });
556 $template->param(
557 refundLostItemFeeRule => $refundLostItemFeeRule,
558 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
561 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
563 my $itemtypes = Koha::ItemTypes->search_with_localization;
565 my $humanbranch = ( $branch ne '*' ? $branch : undef );
567 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
568 my $definedbranch = $all_rules->count ? 1 : 0;
570 my $rules = {};
571 while ( my $r = $all_rules->next ) {
572 $r = $r->unblessed;
573 $rules->{ $r->{categorycode} }->{ $r->{itemtype} }->{ $r->{rule_name} } = $r->{rule_value};
576 $template->param(show_branch_cat_rule_form => 1);
578 $template->param(
579 patron_categories => $patron_categories,
580 itemtypeloop => $itemtypes,
581 humanbranch => $humanbranch,
582 current_branch => $branch,
583 definedbranch => $definedbranch,
584 all_rules => $rules,
586 output_html_with_http_headers $input, $cookie, $template->output;
588 exit 0;
590 # sort by patron category, then item type, putting
591 # default entries at the bottom
592 sub by_category_and_itemtype {
593 unless (by_category($a, $b)) {
594 return by_itemtype($a, $b);
598 sub by_category {
599 my ($a, $b) = @_;
600 if ($a->{'default_humancategorycode'}) {
601 return ($b->{'default_humancategorycode'} ? 0 : 1);
602 } elsif ($b->{'default_humancategorycode'}) {
603 return -1;
604 } else {
605 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
609 sub by_itemtype {
610 my ($a, $b) = @_;
611 if ($a->{default_translated_description}) {
612 return ($b->{'default_translated_description'} ? 0 : 1);
613 } elsif ($b->{'default_translated_description'}) {
614 return -1;
615 } else {
616 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
620 sub strip_non_numeric {
621 my $string = shift;
622 $string =~ s/\s//g;
623 $string = '' if $string !~ /^\d+/;
624 return $string;