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>.
28 use C4::Branch; # GetBranches
31 use Koha::IssuingRule;
32 use Koha::IssuingRules;
35 my $dbh = C4::Context->dbh;
38 # $flagsrequired->{circulation}=1;
39 my ($template, $loggedinuser, $cookie)
40 = get_template_and_user({template_name => "admin/smart-rules.tt",
44 flagsrequired => {parameters => 'manage_circ_rules'},
48 my $type=$input->param('type');
51 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
52 $branch = $input->param('branch') || GetBranchesCount() == 1 ? undef : C4::Branch::mybranch();
55 $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
57 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
59 my $op = $input->param('op') || q{};
60 my $language = C4::Languages::getlanguage();
62 if ($op eq 'delete') {
63 my $itemtype = $input->param('itemtype');
64 my $categorycode = $input->param('categorycode');
65 $debug and warn "deleting $1 $2 $branch";
67 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
68 $sth_Idelete->execute($branch, $categorycode, $itemtype);
70 elsif ($op eq 'delete-branch-cat') {
71 my $categorycode = $input->param('categorycode');
73 if ($categorycode eq "*") {
74 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
75 $sth_delete->execute();
77 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
78 WHERE categorycode = ?");
79 $sth_delete->execute($categorycode);
81 } elsif ($categorycode eq "*") {
82 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
83 WHERE branchcode = ?");
84 $sth_delete->execute($branch);
86 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
88 AND categorycode = ?");
89 $sth_delete->execute($branch, $categorycode);
92 elsif ($op eq 'delete-branch-item') {
93 my $itemtype = $input->param('itemtype');
95 if ($itemtype eq "*") {
96 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
97 $sth_delete->execute();
99 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
100 WHERE itemtype = ?");
101 $sth_delete->execute($itemtype);
103 } elsif ($itemtype eq "*") {
104 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
105 WHERE branchcode = ?");
106 $sth_delete->execute($branch);
108 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
111 $sth_delete->execute($branch, $itemtype);
114 # save the values entered
115 elsif ($op eq 'add') {
116 my $br = $branch; # branch
117 my $bor = $input->param('categorycode'); # borrower category
118 my $itemtype = $input->param('itemtype'); # item type
119 my $fine = $input->param('fine');
120 my $finedays = $input->param('finedays');
121 my $maxsuspensiondays = $input->param('maxsuspensiondays');
122 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
123 my $firstremind = $input->param('firstremind');
124 my $chargeperiod = $input->param('chargeperiod');
125 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
126 my $maxissueqty = $input->param('maxissueqty');
127 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
128 my $renewalsallowed = $input->param('renewalsallowed');
129 my $renewalperiod = $input->param('renewalperiod');
130 my $norenewalbefore = $input->param('norenewalbefore');
131 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
132 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
133 my $reservesallowed = $input->param('reservesallowed');
134 my $onshelfholds = $input->param('onshelfholds') || 0;
135 $maxissueqty =~ s/\s//g;
136 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
137 $maxonsiteissueqty =~ s/\s//g;
138 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
139 my $issuelength = $input->param('issuelength');
140 my $lengthunit = $input->param('lengthunit');
141 my $hardduedate = $input->param('hardduedate') || undef;
142 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
143 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
144 my $hardduedatecompare = $input->param('hardduedatecompare');
145 my $rentaldiscount = $input->param('rentaldiscount');
146 my $opacitemholds = $input->param('opacitemholds') || 0;
147 my $overduefinescap = $input->param('overduefinescap') || undef;
148 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
149 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
153 categorycode => $bor,
154 itemtype => $itemtype,
156 finedays => $finedays,
157 maxsuspensiondays => $maxsuspensiondays,
158 firstremind => $firstremind,
159 chargeperiod => $chargeperiod,
160 chargeperiod_charge_at => $chargeperiod_charge_at,
161 maxissueqty => $maxissueqty,
162 maxonsiteissueqty => $maxonsiteissueqty,
163 renewalsallowed => $renewalsallowed,
164 renewalperiod => $renewalperiod,
165 norenewalbefore => $norenewalbefore,
166 auto_renew => $auto_renew,
167 reservesallowed => $reservesallowed,
168 issuelength => $issuelength,
169 lengthunit => $lengthunit,
170 hardduedate => $hardduedate,
171 hardduedatecompare => $hardduedatecompare,
172 rentaldiscount => $rentaldiscount,
173 onshelfholds => $onshelfholds,
174 opacitemholds => $opacitemholds,
175 overduefinescap => $overduefinescap,
176 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
179 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
181 $issuingrule->set($params)->store();
183 Koha::IssuingRule->new()->set($params)->store();
187 elsif ($op eq "set-branch-defaults") {
188 my $categorycode = $input->param('categorycode');
189 my $maxissueqty = $input->param('maxissueqty');
190 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
191 my $holdallowed = $input->param('holdallowed');
192 my $returnbranch = $input->param('returnbranch');
193 $maxissueqty =~ s/\s//g;
194 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
195 $maxonsiteissueqty =~ s/\s//g;
196 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
197 $holdallowed =~ s/\s//g;
198 $holdallowed = undef if $holdallowed !~ /^\d+/;
200 if ($branch eq "*") {
201 my $sth_search = $dbh->prepare("SELECT count(*) AS total
202 FROM default_circ_rules");
203 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
204 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
205 VALUES (?, ?, ?, ?)");
206 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
207 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
209 $sth_search->execute();
210 my $res = $sth_search->fetchrow_hashref();
212 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
214 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
217 my $sth_search = $dbh->prepare("SELECT count(*) AS total
218 FROM default_branch_circ_rules
219 WHERE branchcode = ?");
220 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
221 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
222 VALUES (?, ?, ?, ?, ?)");
223 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
224 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
225 WHERE branchcode = ?");
226 $sth_search->execute($branch);
227 my $res = $sth_search->fetchrow_hashref();
229 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
231 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
235 elsif ($op eq "add-branch-cat") {
236 my $categorycode = $input->param('categorycode');
237 my $maxissueqty = $input->param('maxissueqty');
238 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
239 $maxissueqty =~ s/\s//g;
240 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
241 $maxonsiteissueqty =~ s/\s//g;
242 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
244 if ($branch eq "*") {
245 if ($categorycode eq "*") {
246 my $sth_search = $dbh->prepare("SELECT count(*) AS total
247 FROM default_circ_rules");
248 my $sth_insert = $dbh->prepare(q|
249 INSERT INTO default_circ_rules
250 (maxissueqty, maxonsiteissueqty)
253 my $sth_update = $dbh->prepare(q|
254 UPDATE default_circ_rules
256 maxonsiteissueqty = ?
259 $sth_search->execute();
260 my $res = $sth_search->fetchrow_hashref();
262 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
264 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
267 my $sth_search = $dbh->prepare("SELECT count(*) AS total
268 FROM default_borrower_circ_rules
269 WHERE categorycode = ?");
270 my $sth_insert = $dbh->prepare(q|
271 INSERT INTO default_borrower_circ_rules
272 (categorycode, maxissueqty, maxonsiteissueqty)
275 my $sth_update = $dbh->prepare(q|
276 UPDATE default_borrower_circ_rules
278 maxonsiteissueqty = ?
279 WHERE categorycode = ?
281 $sth_search->execute($branch);
282 my $res = $sth_search->fetchrow_hashref();
284 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
286 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
289 } elsif ($categorycode eq "*") {
290 my $sth_search = $dbh->prepare("SELECT count(*) AS total
291 FROM default_branch_circ_rules
292 WHERE branchcode = ?");
293 my $sth_insert = $dbh->prepare(q|
294 INSERT INTO default_branch_circ_rules
295 (branchcode, maxissueqty, maxonsiteissueqty)
298 my $sth_update = $dbh->prepare(q|
299 UPDATE default_branch_circ_rules
301 maxonsiteissueqty = ?
304 $sth_search->execute($branch);
305 my $res = $sth_search->fetchrow_hashref();
307 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
309 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
312 my $sth_search = $dbh->prepare("SELECT count(*) AS total
313 FROM branch_borrower_circ_rules
315 AND categorycode = ?");
316 my $sth_insert = $dbh->prepare(q|
317 INSERT INTO branch_borrower_circ_rules
318 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
321 my $sth_update = $dbh->prepare(q|
322 UPDATE branch_borrower_circ_rules
324 maxonsiteissueqty = ?
329 $sth_search->execute($branch, $categorycode);
330 my $res = $sth_search->fetchrow_hashref();
332 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
334 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
338 elsif ($op eq "add-branch-item") {
339 my $itemtype = $input->param('itemtype');
340 my $holdallowed = $input->param('holdallowed');
341 my $returnbranch = $input->param('returnbranch');
342 $holdallowed =~ s/\s//g;
343 $holdallowed = undef if $holdallowed !~ /^\d+/;
345 if ($branch eq "*") {
346 if ($itemtype eq "*") {
347 my $sth_search = $dbh->prepare("SELECT count(*) AS total
348 FROM default_circ_rules");
349 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
350 (holdallowed, returnbranch)
352 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
353 SET holdallowed = ?, returnbranch = ?");
355 $sth_search->execute();
356 my $res = $sth_search->fetchrow_hashref();
358 $sth_update->execute($holdallowed, $returnbranch);
360 $sth_insert->execute($holdallowed, $returnbranch);
363 my $sth_search = $dbh->prepare("SELECT count(*) AS total
364 FROM default_branch_item_rules
365 WHERE itemtype = ?");
366 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
367 (itemtype, holdallowed, returnbranch)
369 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
370 SET holdallowed = ?, returnbranch = ?
371 WHERE itemtype = ?");
372 $sth_search->execute($itemtype);
373 my $res = $sth_search->fetchrow_hashref();
375 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
377 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
380 } elsif ($itemtype eq "*") {
381 my $sth_search = $dbh->prepare("SELECT count(*) AS total
382 FROM default_branch_circ_rules
383 WHERE branchcode = ?");
384 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
385 (branchcode, holdallowed, returnbranch)
387 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
388 SET holdallowed = ?, returnbranch = ?
389 WHERE branchcode = ?");
390 $sth_search->execute($branch);
391 my $res = $sth_search->fetchrow_hashref();
393 $sth_update->execute($holdallowed, $returnbranch, $branch);
395 $sth_insert->execute($branch, $holdallowed, $returnbranch);
398 my $sth_search = $dbh->prepare("SELECT count(*) AS total
399 FROM branch_item_rules
402 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
403 (branchcode, itemtype, holdallowed, returnbranch)
404 VALUES (?, ?, ?, ?)");
405 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
406 SET holdallowed = ?, returnbranch = ?
410 $sth_search->execute($branch, $itemtype);
411 my $res = $sth_search->fetchrow_hashref();
413 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
415 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
420 my $branches = GetBranches();
422 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
424 value => $thisbranch,
425 selected => $thisbranch eq $branch,
426 branchname => $branches->{$thisbranch}->{'branchname'},
430 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
433 while (my $data=$sth->fetchrow_hashref){
434 push @category_loop,$data;
439 my @itemtypes = @{ GetItemTypes( style => 'array' ) };
441 my $sth2 = $dbh->prepare("
442 SELECT issuingrules.*,
443 itemtypes.description AS humanitemtype,
444 categories.description AS humancategorycode,
445 COALESCE( localization.translation, itemtypes.description ) AS translated_description
448 ON (itemtypes.itemtype = issuingrules.itemtype)
450 ON (categories.categorycode = issuingrules.categorycode)
451 LEFT JOIN localization ON issuingrules.itemtype = localization.code
452 AND localization.entity = 'itemtypes'
453 AND localization.lang = ?
454 WHERE issuingrules.branchcode = ?
456 $sth2->execute($language, $branch);
458 while (my $row = $sth2->fetchrow_hashref) {
459 $row->{'current_branch'} ||= $row->{'branchcode'};
460 $row->{'humanitemtype'} ||= $row->{'itemtype'};
461 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
462 $row->{'humancategorycode'} ||= $row->{'categorycode'};
463 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
464 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
465 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
466 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
467 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
468 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
469 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
470 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
472 $row->{'hardduedate'} = 0;
474 push @row_loop, $row;
478 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
481 if ($branch eq "*") {
482 $sth_branch_cat = $dbh->prepare("
483 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
484 FROM default_borrower_circ_rules
485 JOIN categories USING (categorycode)
488 $sth_branch_cat->execute();
490 $sth_branch_cat = $dbh->prepare("
491 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
492 FROM branch_borrower_circ_rules
493 JOIN categories USING (categorycode)
494 WHERE branch_borrower_circ_rules.branchcode = ?
496 $sth_branch_cat->execute($branch);
499 my @branch_cat_rules = ();
500 while (my $row = $sth_branch_cat->fetchrow_hashref) {
501 push @branch_cat_rules, $row;
503 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
505 # note undef maxissueqty so that template can deal with them
506 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
507 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
508 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
511 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
514 if ($branch eq "*") {
515 $sth_branch_item = $dbh->prepare("
516 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
517 FROM default_branch_item_rules
518 JOIN itemtypes USING (itemtype)
520 $sth_branch_item->execute();
522 $sth_branch_item = $dbh->prepare("
523 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
524 FROM branch_item_rules
525 JOIN itemtypes USING (itemtype)
526 WHERE branch_item_rules.branchcode = ?
528 $sth_branch_item->execute($branch);
531 my @branch_item_rules = ();
532 while (my $row = $sth_branch_item->fetchrow_hashref) {
533 push @branch_item_rules, $row;
535 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
537 # note undef holdallowed so that template can deal with them
538 foreach my $entry (@sorted_branch_item_rules) {
539 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
540 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
543 $template->param(show_branch_cat_rule_form => 1);
544 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
545 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
548 if ($branch eq "*") {
549 $sth_defaults = $dbh->prepare("
551 FROM default_circ_rules
553 $sth_defaults->execute();
555 $sth_defaults = $dbh->prepare("
557 FROM default_branch_circ_rules
560 $sth_defaults->execute($branch);
563 my $defaults = $sth_defaults->fetchrow_hashref;
566 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
567 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
568 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
569 $template->param(default_maxissueqty => $defaults->{maxissueqty});
570 $template->param(default_maxonsiteissueqty => $defaults->{maxonsiteissueqty});
571 $template->param(default_returnbranch => $defaults->{returnbranch});
574 $template->param(default_rules => ($defaults ? 1 : 0));
576 $template->param(categoryloop => \@category_loop,
577 itemtypeloop => \@itemtypes,
578 rules => \@sorted_row_loop,
579 branchloop => \@branchloop,
580 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
581 current_branch => $branch,
582 definedbranch => scalar(@sorted_row_loop)>0
584 output_html_with_http_headers $input, $cookie, $template->output;
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);
598 if ($a->{'default_humancategorycode'}) {
599 return ($b->{'default_humancategorycode'} ? 0 : 1);
600 } elsif ($b->{'default_humancategorycode'}) {
603 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
609 if ($a->{'default_humanitemtype'}) {
610 return ($b->{'default_humanitemtype'} ? 0 : 1);
611 } elsif ($b->{'default_humanitemtype'}) {
614 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};