Bug 20434: Update UNIMARC framework - auth (SNC)
[koha.git] / admin / smart-rules.pl
blob403f425e54dce0317cba8592d2eb4c2f92d2e5cb
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::IssuingRule;
30 use Koha::IssuingRules;
31 use Koha::Logger;
32 use Koha::RefundLostItemFeeRules;
33 use Koha::Libraries;
34 use Koha::CirculationRules;
35 use Koha::Patron::Categories;
36 use Koha::Caches;
37 use Koha::Patrons;
39 my $input = CGI->new;
40 my $dbh = C4::Context->dbh;
42 # my $flagsrequired;
43 # $flagsrequired->{circulation}=1;
44 my ($template, $loggedinuser, $cookie)
45 = get_template_and_user({template_name => "admin/smart-rules.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => {parameters => 'manage_circ_rules'},
50 debug => 1,
51 });
53 my $type=$input->param('type');
55 my $branch = $input->param('branch');
56 unless ( $branch ) {
57 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
58 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
60 else {
61 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
65 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
67 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
68 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
69 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
71 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
73 my $op = $input->param('op') || q{};
74 my $language = C4::Languages::getlanguage();
76 my $cache = Koha::Caches->get_instance;
77 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
79 if ($op eq 'delete') {
80 my $itemtype = $input->param('itemtype');
81 my $categorycode = $input->param('categorycode');
82 $debug and warn "deleting $1 $2 $branch";
84 Koha::IssuingRules->find({
85 branchcode => $branch,
86 categorycode => $categorycode,
87 itemtype => $itemtype
88 })->delete;
91 elsif ($op eq 'delete-branch-cat') {
92 my $categorycode = $input->param('categorycode');
93 if ($branch eq "*") {
94 if ($categorycode eq "*") {
95 Koha::CirculationRules->set_rules(
97 categorycode => undef,
98 branchcode => undef,
99 itemtype => undef,
100 rules => {
101 patron_maxissueqty => undef,
102 patron_maxonsiteissueqty => undef,
103 holdallowed => undef,
104 hold_fulfillment_policy => undef,
105 returnbranch => undef,
106 max_holds => undef,
110 } else {
111 Koha::CirculationRules->set_rules(
113 categorycode => $categorycode,
114 branchcode => undef,
115 itemtype => undef,
116 rules => {
117 max_holds => undef,
118 patron_maxissueqty => undef,
119 patron_maxonsiteissueqty => undef,
124 } elsif ($categorycode eq "*") {
125 Koha::CirculationRules->set_rules(
127 categorycode => undef,
128 branchcode => $branch,
129 itemtype => undef,
130 rules => {
131 patron_maxissueqty => undef,
132 patron_maxonsiteissueqty => undef,
133 holdallowed => undef,
134 hold_fulfillment_policy => undef,
135 returnbranch => undef,
136 max_holds => undef,
140 } else {
141 Koha::CirculationRules->set_rules(
143 categorycode => $categorycode,
144 branchcode => $branch,
145 itemtype => undef,
146 rules => {
147 max_holds => undef,
148 patron_maxissueqty => undef,
149 patron_maxonsiteissueqty => undef,
155 elsif ($op eq 'delete-branch-item') {
156 my $itemtype = $input->param('itemtype');
157 if ($branch eq "*") {
158 if ($itemtype eq "*") {
159 Koha::CirculationRules->set_rules(
161 categorycode => undef,
162 branchcode => undef,
163 itemtype => undef,
164 rules => {
165 patron_maxissueqty => undef,
166 patron_maxonsiteissueqty => undef,
167 holdallowed => undef,
168 hold_fulfillment_policy => undef,
169 returnbranch => undef,
173 } else {
174 Koha::CirculationRules->set_rules(
176 categorycode => undef,
177 branchcode => undef,
178 itemtype => $itemtype,
179 rules => {
180 holdallowed => undef,
181 hold_fulfillment_policy => undef,
182 returnbranch => undef,
187 } elsif ($itemtype eq "*") {
188 Koha::CirculationRules->set_rules(
190 categorycode => undef,
191 branchcode => $branch,
192 itemtype => undef,
193 rules => {
194 maxissueqty => undef,
195 maxonsiteissueqty => undef,
196 holdallowed => undef,
197 hold_fulfillment_policy => undef,
198 returnbranch => undef,
202 } else {
203 Koha::CirculationRules->set_rules(
205 categorycode => undef,
206 branchcode => $branch,
207 itemtype => $itemtype,
208 rules => {
209 holdallowed => undef,
210 hold_fulfillment_policy => undef,
211 returnbranch => undef,
217 # save the values entered
218 elsif ($op eq 'add') {
219 my $br = $branch; # branch
220 my $bor = $input->param('categorycode'); # borrower category
221 my $itemtype = $input->param('itemtype'); # item type
222 my $fine = $input->param('fine');
223 my $finedays = $input->param('finedays');
224 my $maxsuspensiondays = $input->param('maxsuspensiondays');
225 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
226 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
227 my $firstremind = $input->param('firstremind');
228 my $chargeperiod = $input->param('chargeperiod');
229 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
230 my $maxissueqty = $input->param('maxissueqty');
231 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
232 my $renewalsallowed = $input->param('renewalsallowed');
233 my $renewalperiod = $input->param('renewalperiod');
234 my $norenewalbefore = $input->param('norenewalbefore');
235 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
236 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
237 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
238 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
239 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
240 $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 );
241 $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 );
242 my $reservesallowed = $input->param('reservesallowed');
243 my $holds_per_record = $input->param('holds_per_record');
244 my $holds_per_day = $input->param('holds_per_day');
245 $holds_per_day =~ s/\s//g;
246 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
247 my $onshelfholds = $input->param('onshelfholds') || 0;
248 $maxissueqty =~ s/\s//g;
249 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
250 $maxonsiteissueqty =~ s/\s//g;
251 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
252 my $issuelength = $input->param('issuelength');
253 $issuelength = $issuelength eq q{} ? undef : $issuelength;
254 my $lengthunit = $input->param('lengthunit');
255 my $hardduedate = $input->param('hardduedate') || undef;
256 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
257 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
258 my $hardduedatecompare = $input->param('hardduedatecompare');
259 my $rentaldiscount = $input->param('rentaldiscount');
260 my $opacitemholds = $input->param('opacitemholds') || 0;
261 my $article_requests = $input->param('article_requests') || 'no';
262 my $overduefinescap = $input->param('overduefinescap') || undef;
263 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
264 my $note = $input->param('note');
265 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
267 my $params = {
268 branchcode => $br,
269 categorycode => $bor,
270 itemtype => $itemtype,
271 fine => $fine,
272 finedays => $finedays,
273 maxsuspensiondays => $maxsuspensiondays,
274 suspension_chargeperiod => $suspension_chargeperiod,
275 firstremind => $firstremind,
276 chargeperiod => $chargeperiod,
277 chargeperiod_charge_at => $chargeperiod_charge_at,
278 renewalsallowed => $renewalsallowed,
279 renewalperiod => $renewalperiod,
280 norenewalbefore => $norenewalbefore,
281 auto_renew => $auto_renew,
282 no_auto_renewal_after => $no_auto_renewal_after,
283 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
284 reservesallowed => $reservesallowed,
285 holds_per_record => $holds_per_record,
286 holds_per_day => $holds_per_day,
287 issuelength => $issuelength,
288 lengthunit => $lengthunit,
289 hardduedate => $hardduedate,
290 hardduedatecompare => $hardduedatecompare,
291 rentaldiscount => $rentaldiscount,
292 onshelfholds => $onshelfholds,
293 opacitemholds => $opacitemholds,
294 overduefinescap => $overduefinescap,
295 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
296 article_requests => $article_requests,
297 note => $note,
300 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
301 if ($issuingrule) {
302 $issuingrule->set($params)->store();
303 } else {
304 Koha::IssuingRule->new()->set($params)->store();
306 Koha::CirculationRules->set_rules(
308 categorycode => $bor,
309 itemtype => $itemtype,
310 branchcode => $br,
311 rules => {
312 maxissueqty => $maxissueqty,
313 maxonsiteissueqty => $maxonsiteissueqty,
319 elsif ($op eq "set-branch-defaults") {
320 my $categorycode = $input->param('categorycode');
321 my $patron_maxissueqty = $input->param('patron_maxissueqty');
322 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
323 my $holdallowed = $input->param('holdallowed');
324 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
325 my $returnbranch = $input->param('returnbranch');
326 my $max_holds = $input->param('max_holds');
327 $patron_maxissueqty =~ s/\s//g;
328 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
329 $patron_maxonsiteissueqty =~ s/\s//g;
330 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
331 $holdallowed =~ s/\s//g;
332 $holdallowed = undef if $holdallowed !~ /^\d+/;
333 $max_holds =~ s/\s//g;
334 $max_holds = '' if $max_holds !~ /^\d+/;
336 if ($branch eq "*") {
337 Koha::CirculationRules->set_rules(
339 categorycode => undef,
340 itemtype => undef,
341 branchcode => undef,
342 rules => {
343 patron_maxissueqty => $patron_maxissueqty,
344 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
345 holdallowed => $holdallowed,
346 hold_fulfillment_policy => $hold_fulfillment_policy,
347 returnbranch => $returnbranch,
351 } else {
352 Koha::CirculationRules->set_rules(
354 categorycode => undef,
355 itemtype => undef,
356 branchcode => $branch,
357 rules => {
358 patron_maxissueqty => $patron_maxissueqty,
359 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
360 holdallowed => $holdallowed,
361 hold_fulfillment_policy => $hold_fulfillment_policy,
362 returnbranch => $returnbranch,
367 Koha::CirculationRules->set_rule(
369 branchcode => $branch,
370 categorycode => undef,
371 itemtype => undef,
372 rule_name => 'max_holds',
373 rule_value => $max_holds,
377 elsif ($op eq "add-branch-cat") {
378 my $categorycode = $input->param('categorycode');
379 my $patron_maxissueqty = $input->param('patron_maxissueqty');
380 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
381 my $max_holds = $input->param('max_holds');
382 $patron_maxissueqty =~ s/\s//g;
383 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
384 $patron_maxonsiteissueqty =~ s/\s//g;
385 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
386 $max_holds =~ s/\s//g;
387 $max_holds = undef if $max_holds !~ /^\d+/;
389 if ($branch eq "*") {
390 if ($categorycode eq "*") {
391 Koha::CirculationRules->set_rules(
393 categorycode => undef,
394 itemtype => undef,
395 branchcode => undef,
396 rules => {
397 max_holds => $max_holds,
398 patron_maxissueqty => $patron_maxissueqty,
399 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
403 } else {
404 Koha::CirculationRules->set_rules(
406 branchcode => undef,
407 categorycode => $categorycode,
408 itemtype => undef,
409 rules => {
410 max_holds => $max_holds,
411 patron_maxissueqty => $patron_maxissueqty,
412 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
417 } elsif ($categorycode eq "*") {
418 Koha::CirculationRules->set_rules(
420 categorycode => undef,
421 itemtype => undef,
422 branchcode => $branch,
423 rules => {
424 max_holds => $max_holds,
425 patron_maxissueqty => $patron_maxissueqty,
426 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
430 } else {
431 Koha::CirculationRules->set_rules(
433 categorycode => $categorycode,
434 itemtype => undef,
435 branchcode => $branch,
436 rules => {
437 max_holds => $max_holds,
438 patron_maxissueqty => $patron_maxissueqty,
439 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
445 elsif ($op eq "add-branch-item") {
446 my $itemtype = $input->param('itemtype');
447 my $holdallowed = $input->param('holdallowed');
448 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
449 my $returnbranch = $input->param('returnbranch');
451 $holdallowed =~ s/\s//g;
452 $holdallowed = undef if $holdallowed !~ /^\d+/;
454 if ($branch eq "*") {
455 if ($itemtype eq "*") {
456 Koha::CirculationRules->set_rules(
458 categorycode => undef,
459 itemtype => undef,
460 branchcode => undef,
461 rules => {
462 holdallowed => $holdallowed,
463 hold_fulfillment_policy => $hold_fulfillment_policy,
464 returnbranch => $returnbranch,
468 } else {
469 Koha::CirculationRules->set_rules(
471 categorycode => undef,
472 itemtype => $itemtype,
473 branchcode => undef,
474 rules => {
475 holdallowed => $holdallowed,
476 hold_fulfillment_policy => $hold_fulfillment_policy,
477 returnbranch => $returnbranch,
482 } elsif ($itemtype eq "*") {
483 Koha::CirculationRules->set_rules(
485 categorycode => undef,
486 itemtype => undef,
487 branchcode => $branch,
488 rules => {
489 holdallowed => $holdallowed,
490 hold_fulfillment_policy => $hold_fulfillment_policy,
491 returnbranch => $returnbranch,
495 } else {
496 Koha::CirculationRules->set_rules(
498 categorycode => undef,
499 itemtype => $itemtype,
500 branchcode => $branch,
501 rules => {
502 holdallowed => $holdallowed,
503 hold_fulfillment_policy => $hold_fulfillment_policy,
504 returnbranch => $returnbranch,
510 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
512 my $refund = $input->param('refund');
514 if ( $refund eq '*' ) {
515 if ( $branch ne '*' ) {
516 # only do something for $refund eq '*' if branch-specific
517 Koha::CirculationRules->set_rules(
519 categorycode => undef,
520 itemtype => undef,
521 branchcode => $branch,
522 rules => {
523 refund => undef
528 } else {
529 Koha::CirculationRules->set_rules(
531 categorycode => undef,
532 itemtype => undef,
533 branchcode => $branch,
534 rules => {
535 refund => $refund
542 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => ($branch eq '*') ? undef:$branch });
544 $template->param(
545 refundLostItemFeeRule => $refundLostItemFeeRule,
546 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
549 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
551 my @row_loop;
552 my $itemtypes = Koha::ItemTypes->search_with_localization;
554 my $sth2 = $dbh->prepare("
555 SELECT issuingrules.*,
556 itemtypes.description AS humanitemtype,
557 categories.description AS humancategorycode,
558 COALESCE( localization.translation, itemtypes.description ) AS translated_description
559 FROM issuingrules
560 LEFT JOIN itemtypes
561 ON (itemtypes.itemtype = issuingrules.itemtype)
562 LEFT JOIN categories
563 ON (categories.categorycode = issuingrules.categorycode)
564 LEFT JOIN localization ON issuingrules.itemtype = localization.code
565 AND localization.entity = 'itemtypes'
566 AND localization.lang = ?
567 WHERE issuingrules.branchcode = ?
569 $sth2->execute($language, $branch);
571 while (my $row = $sth2->fetchrow_hashref) {
572 $row->{'current_branch'} ||= $row->{'branchcode'};
573 $row->{humanitemtype} ||= $row->{itemtype};
574 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
575 $row->{'humancategorycode'} ||= $row->{'categorycode'};
576 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
577 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
578 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
579 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
580 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
581 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
582 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
583 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
584 } else {
585 $row->{'hardduedate'} = 0;
587 if ($row->{no_auto_renewal_after_hard_limit}) {
588 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
589 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
592 push @row_loop, $row;
595 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
597 $template->param(show_branch_cat_rule_form => 1);
599 $template->param(
600 patron_categories => $patron_categories,
601 itemtypeloop => $itemtypes,
602 rules => \@sorted_row_loop,
603 humanbranch => ($branch ne '*' ? $branch : ''),
604 current_branch => $branch,
605 definedbranch => scalar(@sorted_row_loop)>0
607 output_html_with_http_headers $input, $cookie, $template->output;
609 exit 0;
611 # sort by patron category, then item type, putting
612 # default entries at the bottom
613 sub by_category_and_itemtype {
614 unless (by_category($a, $b)) {
615 return by_itemtype($a, $b);
619 sub by_category {
620 my ($a, $b) = @_;
621 if ($a->{'default_humancategorycode'}) {
622 return ($b->{'default_humancategorycode'} ? 0 : 1);
623 } elsif ($b->{'default_humancategorycode'}) {
624 return -1;
625 } else {
626 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
630 sub by_itemtype {
631 my ($a, $b) = @_;
632 if ($a->{default_translated_description}) {
633 return ($b->{'default_translated_description'} ? 0 : 1);
634 } elsif ($b->{'default_translated_description'}) {
635 return -1;
636 } else {
637 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};