Bug 4461: Checks on library email before enqueuing letter
[koha.git] / admin / smart-rules.pl
blob6bea920dd909b1fb45a1d8ade4aa2dfdb0cf11d7
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 lengthunit => undef,
98 hardduedate => undef,
99 hardduedatecompare => undef,
100 renewalsallowed => 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 patron_maxissueqty => undef,
165 patron_maxonsiteissueqty => undef,
169 Koha::CirculationRules->set_rules(
171 branchcode => $branch,
172 rules => {
173 holdallowed => undef,
174 hold_fulfillment_policy => undef,
175 returnbranch => undef,
176 max_holds => undef,
180 } else {
181 Koha::CirculationRules->set_rules(
183 categorycode => $categorycode,
184 branchcode => $branch,
185 rules => {
186 max_holds => undef,
187 patron_maxissueqty => undef,
188 patron_maxonsiteissueqty => undef,
194 elsif ($op eq 'delete-branch-item') {
195 my $itemtype = $input->param('itemtype');
196 if ($branch eq "*") {
197 if ($itemtype eq "*") {
198 Koha::CirculationRules->set_rules(
200 branchcode => undef,
201 itemtype => undef,
202 rules => {
203 holdallowed => undef,
204 hold_fulfillment_policy => undef,
205 returnbranch => undef,
209 } else {
210 Koha::CirculationRules->set_rules(
212 branchcode => undef,
213 itemtype => $itemtype,
214 rules => {
215 holdallowed => undef,
216 hold_fulfillment_policy => undef,
217 returnbranch => undef,
222 } elsif ($itemtype eq "*") {
223 Koha::CirculationRules->set_rules(
225 branchcode => $branch,
226 itemtype => undef,
227 rules => {
228 holdallowed => undef,
229 hold_fulfillment_policy => undef,
230 returnbranch => undef,
234 } else {
235 Koha::CirculationRules->set_rules(
237 branchcode => $branch,
238 itemtype => $itemtype,
239 rules => {
240 holdallowed => undef,
241 hold_fulfillment_policy => undef,
242 returnbranch => undef,
248 # save the values entered
249 elsif ($op eq 'add') {
250 my $br = $branch; # branch
251 my $bor = $input->param('categorycode'); # borrower category
252 my $itemtype = $input->param('itemtype'); # item type
253 my $fine = $input->param('fine');
254 my $finedays = $input->param('finedays');
255 my $maxsuspensiondays = $input->param('maxsuspensiondays');
256 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
257 $maxsuspensiondays = '' if $maxsuspensiondays eq q||;
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($input->param('maxissueqty'));
263 my $maxonsiteissueqty = strip_non_numeric($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( $input->param('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($input->param('reservesallowed'));
275 my $holds_per_record = strip_non_numeric($input->param('holds_per_record'));
276 my $holds_per_day = strip_non_numeric($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 $lengthunit = $input->param('lengthunit');
281 my $hardduedate = $input->param('hardduedate') || undef;
282 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
283 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
284 my $hardduedatecompare = $input->param('hardduedatecompare');
285 my $rentaldiscount = $input->param('rentaldiscount');
286 my $opacitemholds = $input->param('opacitemholds') || 0;
287 my $article_requests = $input->param('article_requests') || 'no';
288 my $overduefinescap = $input->param('overduefinescap') || '';
289 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
290 my $note = $input->param('note');
291 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
293 my $rules = {
294 maxissueqty => $maxissueqty,
295 maxonsiteissueqty => $maxonsiteissueqty,
296 rentaldiscount => $rentaldiscount,
297 fine => $fine,
298 finedays => $finedays,
299 maxsuspensiondays => $maxsuspensiondays,
300 suspension_chargeperiod => $suspension_chargeperiod,
301 firstremind => $firstremind,
302 chargeperiod => $chargeperiod,
303 chargeperiod_charge_at => $chargeperiod_charge_at,
304 issuelength => $issuelength,
305 lengthunit => $lengthunit,
306 hardduedate => $hardduedate,
307 hardduedatecompare => $hardduedatecompare,
308 renewalsallowed => $renewalsallowed,
309 renewalperiod => $renewalperiod,
310 norenewalbefore => $norenewalbefore,
311 auto_renew => $auto_renew,
312 no_auto_renewal_after => $no_auto_renewal_after,
313 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
314 reservesallowed => $reservesallowed,
315 holds_per_record => $holds_per_record,
316 holds_per_day => $holds_per_day,
317 onshelfholds => $onshelfholds,
318 opacitemholds => $opacitemholds,
319 overduefinescap => $overduefinescap,
320 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
321 article_requests => $article_requests,
322 note => $note,
325 Koha::CirculationRules->set_rules(
327 categorycode => $bor eq '*' ? undef : $bor,
328 itemtype => $itemtype eq '*' ? undef : $itemtype,
329 branchcode => $br eq '*' ? undef : $br,
330 rules => $rules,
335 elsif ($op eq "set-branch-defaults") {
336 my $categorycode = $input->param('categorycode');
337 my $patron_maxissueqty = strip_non_numeric($input->param('patron_maxissueqty'));
338 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
339 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
340 my $holdallowed = $input->param('holdallowed');
341 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
342 my $returnbranch = $input->param('returnbranch');
343 my $max_holds = strip_non_numeric($input->param('max_holds'));
344 $holdallowed =~ s/\s//g;
345 $holdallowed = undef if $holdallowed !~ /^\d+/;
347 if ($branch eq "*") {
348 Koha::CirculationRules->set_rules(
350 itemtype => undef,
351 branchcode => undef,
352 rules => {
353 holdallowed => $holdallowed,
354 hold_fulfillment_policy => $hold_fulfillment_policy,
355 returnbranch => $returnbranch,
359 Koha::CirculationRules->set_rules(
361 categorycode => undef,
362 branchcode => undef,
363 rules => {
364 patron_maxissueqty => $patron_maxissueqty,
365 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
369 } else {
370 Koha::CirculationRules->set_rules(
372 itemtype => undef,
373 branchcode => $branch,
374 rules => {
375 holdallowed => $holdallowed,
376 hold_fulfillment_policy => $hold_fulfillment_policy,
377 returnbranch => $returnbranch,
381 Koha::CirculationRules->set_rules(
383 categorycode => undef,
384 branchcode => $branch,
385 rules => {
386 patron_maxissueqty => $patron_maxissueqty,
387 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
392 Koha::CirculationRules->set_rule(
394 branchcode => $branch,
395 categorycode => undef,
396 rule_name => 'max_holds',
397 rule_value => $max_holds,
401 elsif ($op eq "add-branch-cat") {
402 my $categorycode = $input->param('categorycode');
403 my $patron_maxissueqty = strip_non_numeric($input->param('patron_maxissueqty'));
404 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
405 $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
406 my $max_holds = $input->param('max_holds');
407 $max_holds =~ s/\s//g;
408 $max_holds = undef if $max_holds !~ /^\d+/;
410 if ($branch eq "*") {
411 if ($categorycode eq "*") {
412 Koha::CirculationRules->set_rules(
414 categorycode => undef,
415 branchcode => undef,
416 rules => {
417 max_holds => $max_holds,
418 patron_maxissueqty => $patron_maxissueqty,
419 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
423 } else {
424 Koha::CirculationRules->set_rules(
426 categorycode => $categorycode,
427 branchcode => undef,
428 rules => {
429 max_holds => $max_holds,
430 patron_maxissueqty => $patron_maxissueqty,
431 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
436 } elsif ($categorycode eq "*") {
437 Koha::CirculationRules->set_rules(
439 categorycode => undef,
440 branchcode => $branch,
441 rules => {
442 max_holds => $max_holds,
443 patron_maxissueqty => $patron_maxissueqty,
444 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
448 } else {
449 Koha::CirculationRules->set_rules(
451 categorycode => $categorycode,
452 branchcode => $branch,
453 rules => {
454 max_holds => $max_holds,
455 patron_maxissueqty => $patron_maxissueqty,
456 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
462 elsif ($op eq "add-branch-item") {
463 my $itemtype = $input->param('itemtype');
464 my $holdallowed = $input->param('holdallowed');
465 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
466 my $returnbranch = $input->param('returnbranch');
468 $holdallowed =~ s/\s//g;
469 $holdallowed = undef if $holdallowed !~ /^\d+/;
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 $refund = $input->param('refund');
527 if ( $refund eq '*' ) {
528 if ( $branch ne '*' ) {
529 # only do something for $refund eq '*' if branch-specific
530 Koha::CirculationRules->set_rules(
532 branchcode => $branch,
533 rules => {
534 refund => undef
539 } else {
540 Koha::CirculationRules->set_rules(
542 branchcode => $branch,
543 rules => {
544 refund => $refund
551 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef : $branch });
552 $template->param(
553 refundLostItemFeeRule => $refundLostItemFeeRule,
554 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
557 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
559 my $itemtypes = Koha::ItemTypes->search_with_localization;
561 my $humanbranch = ( $branch ne '*' ? $branch : undef );
563 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
564 my $definedbranch = $all_rules->count ? 1 : 0;
566 my $rules = {};
567 while ( my $r = $all_rules->next ) {
568 $r = $r->unblessed;
569 $rules->{ $r->{categorycode} }->{ $r->{itemtype} }->{ $r->{rule_name} } = $r->{rule_value};
572 $template->param(show_branch_cat_rule_form => 1);
574 $template->param(
575 patron_categories => $patron_categories,
576 itemtypeloop => $itemtypes,
577 humanbranch => $humanbranch,
578 current_branch => $branch,
579 definedbranch => $definedbranch,
580 all_rules => $rules,
582 output_html_with_http_headers $input, $cookie, $template->output;
584 exit 0;
586 # sort by patron category, then item type, putting
587 # default entries at the bottom
588 sub by_category_and_itemtype {
589 unless (by_category($a, $b)) {
590 return by_itemtype($a, $b);
594 sub by_category {
595 my ($a, $b) = @_;
596 if ($a->{'default_humancategorycode'}) {
597 return ($b->{'default_humancategorycode'} ? 0 : 1);
598 } elsif ($b->{'default_humancategorycode'}) {
599 return -1;
600 } else {
601 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
605 sub by_itemtype {
606 my ($a, $b) = @_;
607 if ($a->{default_translated_description}) {
608 return ($b->{'default_translated_description'} ? 0 : 1);
609 } elsif ($b->{'default_translated_description'}) {
610 return -1;
611 } else {
612 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
616 sub strip_non_numeric {
617 my $string = shift;
618 $string =~ s/\s//g;
619 $string = '' if $string !~ /^\d+/;
620 return $string;