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
33 my $dbh = C4::Context->dbh;
36 # $flagsrequired->{circulation}=1;
37 my ($template, $loggedinuser, $cookie)
38 = get_template_and_user({template_name => "admin/smart-rules.tt",
42 flagsrequired => {parameters => 'manage_circ_rules'},
46 my $type=$input->param('type');
49 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
50 $branch = $input->param('branch') || GetBranchesCount() == 1 ? undef : C4::Branch::mybranch();
53 $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
55 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
57 my $op = $input->param('op') || q{};
58 my $language = C4::Languages::getlanguage();
60 if ($op eq 'delete') {
61 my $itemtype = $input->param('itemtype');
62 my $categorycode = $input->param('categorycode');
63 $debug and warn "deleting $1 $2 $branch";
65 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
66 $sth_Idelete->execute($branch, $categorycode, $itemtype);
68 elsif ($op eq 'delete-branch-cat') {
69 my $categorycode = $input->param('categorycode');
71 if ($categorycode eq "*") {
72 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
73 $sth_delete->execute();
75 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
76 WHERE categorycode = ?");
77 $sth_delete->execute($categorycode);
79 } elsif ($categorycode eq "*") {
80 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
81 WHERE branchcode = ?");
82 $sth_delete->execute($branch);
84 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
86 AND categorycode = ?");
87 $sth_delete->execute($branch, $categorycode);
90 elsif ($op eq 'delete-branch-item') {
91 my $itemtype = $input->param('itemtype');
93 if ($itemtype eq "*") {
94 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
95 $sth_delete->execute();
97 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
99 $sth_delete->execute($itemtype);
101 } elsif ($itemtype eq "*") {
102 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
103 WHERE branchcode = ?");
104 $sth_delete->execute($branch);
106 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
109 $sth_delete->execute($branch, $itemtype);
112 # save the values entered
113 elsif ($op eq 'add') {
114 my $br = $branch; # branch
115 my $bor = $input->param('categorycode'); # borrower category
116 my $itemtype = $input->param('itemtype'); # item type
117 my $fine = $input->param('fine');
118 my $finedays = $input->param('finedays');
119 my $maxsuspensiondays = $input->param('maxsuspensiondays');
120 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
121 my $firstremind = $input->param('firstremind');
122 my $chargeperiod = $input->param('chargeperiod');
123 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
124 my $maxissueqty = $input->param('maxissueqty');
125 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
126 my $renewalsallowed = $input->param('renewalsallowed');
127 my $renewalperiod = $input->param('renewalperiod');
128 my $norenewalbefore = $input->param('norenewalbefore');
129 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
130 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
131 my $reservesallowed = $input->param('reservesallowed');
132 my $onshelfholds = $input->param('onshelfholds') || 0;
133 $maxissueqty =~ s/\s//g;
134 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
135 $maxonsiteissueqty =~ s/\s//g;
136 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
137 my $issuelength = $input->param('issuelength');
138 my $lengthunit = $input->param('lengthunit');
139 my $hardduedate = $input->param('hardduedate') || undef;
140 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
141 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
142 my $hardduedatecompare = $input->param('hardduedatecompare');
143 my $rentaldiscount = $input->param('rentaldiscount');
144 my $opacitemholds = $input->param('opacitemholds') || 0;
145 my $overduefinescap = $input->param('overduefinescap') || undef;
146 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
147 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
149 my $schema = Koha::Database->new()->schema();
150 my $rs = $schema->resultset('Issuingrule');
154 categorycode => $bor,
155 itemtype => $itemtype,
157 finedays => $finedays,
158 maxsuspensiondays => $maxsuspensiondays,
159 firstremind => $firstremind,
160 chargeperiod => $chargeperiod,
161 chargeperiod_charge_at => $chargeperiod_charge_at,
162 maxissueqty => $maxissueqty,
163 maxonsiteissueqty => $maxonsiteissueqty,
164 renewalsallowed => $renewalsallowed,
165 renewalperiod => $renewalperiod,
166 norenewalbefore => $norenewalbefore,
167 auto_renew => $auto_renew,
168 reservesallowed => $reservesallowed,
169 issuelength => $issuelength,
170 lengthunit => $lengthunit,
171 hardduedate => $hardduedate,
172 hardduedatecompare => $hardduedatecompare,
173 rentaldiscount => $rentaldiscount,
174 onshelfholds => $onshelfholds,
175 opacitemholds => $opacitemholds,
176 overduefinescap => $overduefinescap,
177 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
180 $rs->update_or_create($params);
183 elsif ($op eq "set-branch-defaults") {
184 my $categorycode = $input->param('categorycode');
185 my $maxissueqty = $input->param('maxissueqty');
186 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
187 my $holdallowed = $input->param('holdallowed');
188 my $returnbranch = $input->param('returnbranch');
189 $maxissueqty =~ s/\s//g;
190 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
191 $maxonsiteissueqty =~ s/\s//g;
192 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
193 $holdallowed =~ s/\s//g;
194 $holdallowed = undef if $holdallowed !~ /^\d+/;
196 if ($branch eq "*") {
197 my $sth_search = $dbh->prepare("SELECT count(*) AS total
198 FROM default_circ_rules");
199 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
200 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
201 VALUES (?, ?, ?, ?)");
202 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
203 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
205 $sth_search->execute();
206 my $res = $sth_search->fetchrow_hashref();
208 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
210 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
213 my $sth_search = $dbh->prepare("SELECT count(*) AS total
214 FROM default_branch_circ_rules
215 WHERE branchcode = ?");
216 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
217 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
218 VALUES (?, ?, ?, ?, ?)");
219 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
220 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
221 WHERE branchcode = ?");
222 $sth_search->execute($branch);
223 my $res = $sth_search->fetchrow_hashref();
225 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
227 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
231 elsif ($op eq "add-branch-cat") {
232 my $categorycode = $input->param('categorycode');
233 my $maxissueqty = $input->param('maxissueqty');
234 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
235 $maxissueqty =~ s/\s//g;
236 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
237 $maxonsiteissueqty =~ s/\s//g;
238 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
240 if ($branch eq "*") {
241 if ($categorycode eq "*") {
242 my $sth_search = $dbh->prepare("SELECT count(*) AS total
243 FROM default_circ_rules");
244 my $sth_insert = $dbh->prepare(q|
245 INSERT INTO default_circ_rules
246 (maxissueqty, maxonsiteissueqty)
249 my $sth_update = $dbh->prepare(q|
250 UPDATE default_circ_rules
252 maxonsiteissueqty = ?
255 $sth_search->execute();
256 my $res = $sth_search->fetchrow_hashref();
258 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
260 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
263 my $sth_search = $dbh->prepare("SELECT count(*) AS total
264 FROM default_borrower_circ_rules
265 WHERE categorycode = ?");
266 my $sth_insert = $dbh->prepare(q|
267 INSERT INTO default_borrower_circ_rules
268 (categorycode, maxissueqty, maxonsiteissueqty)
271 my $sth_update = $dbh->prepare(q|
272 UPDATE default_borrower_circ_rules
274 maxonsiteissueqty = ?
275 WHERE categorycode = ?
277 $sth_search->execute($branch);
278 my $res = $sth_search->fetchrow_hashref();
280 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
282 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
285 } elsif ($categorycode eq "*") {
286 my $sth_search = $dbh->prepare("SELECT count(*) AS total
287 FROM default_branch_circ_rules
288 WHERE branchcode = ?");
289 my $sth_insert = $dbh->prepare(q|
290 INSERT INTO default_branch_circ_rules
291 (branchcode, maxissueqty, maxonsiteissueqty)
294 my $sth_update = $dbh->prepare(q|
295 UPDATE default_branch_circ_rules
297 maxonsiteissueqty = ?
300 $sth_search->execute($branch);
301 my $res = $sth_search->fetchrow_hashref();
303 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
305 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
308 my $sth_search = $dbh->prepare("SELECT count(*) AS total
309 FROM branch_borrower_circ_rules
311 AND categorycode = ?");
312 my $sth_insert = $dbh->prepare(q|
313 INSERT INTO branch_borrower_circ_rules
314 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
317 my $sth_update = $dbh->prepare(q|
318 UPDATE branch_borrower_circ_rules
320 maxonsiteissueqty = ?
325 $sth_search->execute($branch, $categorycode);
326 my $res = $sth_search->fetchrow_hashref();
328 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
330 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
334 elsif ($op eq "add-branch-item") {
335 my $itemtype = $input->param('itemtype');
336 my $holdallowed = $input->param('holdallowed');
337 my $returnbranch = $input->param('returnbranch');
338 $holdallowed =~ s/\s//g;
339 $holdallowed = undef if $holdallowed !~ /^\d+/;
341 if ($branch eq "*") {
342 if ($itemtype eq "*") {
343 my $sth_search = $dbh->prepare("SELECT count(*) AS total
344 FROM default_circ_rules");
345 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
346 (holdallowed, returnbranch)
348 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
349 SET holdallowed = ?, returnbranch = ?");
351 $sth_search->execute();
352 my $res = $sth_search->fetchrow_hashref();
354 $sth_update->execute($holdallowed, $returnbranch);
356 $sth_insert->execute($holdallowed, $returnbranch);
359 my $sth_search = $dbh->prepare("SELECT count(*) AS total
360 FROM default_branch_item_rules
361 WHERE itemtype = ?");
362 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
363 (itemtype, holdallowed, returnbranch)
365 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
366 SET holdallowed = ?, returnbranch = ?
367 WHERE itemtype = ?");
368 $sth_search->execute($itemtype);
369 my $res = $sth_search->fetchrow_hashref();
371 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
373 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
376 } elsif ($itemtype eq "*") {
377 my $sth_search = $dbh->prepare("SELECT count(*) AS total
378 FROM default_branch_circ_rules
379 WHERE branchcode = ?");
380 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
381 (branchcode, holdallowed, returnbranch)
383 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
384 SET holdallowed = ?, returnbranch = ?
385 WHERE branchcode = ?");
386 $sth_search->execute($branch);
387 my $res = $sth_search->fetchrow_hashref();
389 $sth_update->execute($holdallowed, $returnbranch, $branch);
391 $sth_insert->execute($branch, $holdallowed, $returnbranch);
394 my $sth_search = $dbh->prepare("SELECT count(*) AS total
395 FROM branch_item_rules
398 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
399 (branchcode, itemtype, holdallowed, returnbranch)
400 VALUES (?, ?, ?, ?)");
401 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
402 SET holdallowed = ?, returnbranch = ?
406 $sth_search->execute($branch, $itemtype);
407 my $res = $sth_search->fetchrow_hashref();
409 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
411 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
416 my $branches = GetBranches();
418 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
420 value => $thisbranch,
421 selected => $thisbranch eq $branch,
422 branchname => $branches->{$thisbranch}->{'branchname'},
426 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
429 while (my $data=$sth->fetchrow_hashref){
430 push @category_loop,$data;
435 my @itemtypes = @{ GetItemTypes( style => 'array' ) };
437 my $sth2 = $dbh->prepare("
438 SELECT issuingrules.*,
439 itemtypes.description AS humanitemtype,
440 categories.description AS humancategorycode,
441 COALESCE( localization.translation, itemtypes.description ) AS translated_description
444 ON (itemtypes.itemtype = issuingrules.itemtype)
446 ON (categories.categorycode = issuingrules.categorycode)
447 LEFT JOIN localization ON issuingrules.itemtype = localization.code
448 AND localization.entity = 'itemtypes'
449 AND localization.lang = ?
450 WHERE issuingrules.branchcode = ?
452 $sth2->execute($language, $branch);
454 while (my $row = $sth2->fetchrow_hashref) {
455 $row->{'current_branch'} ||= $row->{'branchcode'};
456 $row->{'humanitemtype'} ||= $row->{'itemtype'};
457 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
458 $row->{'humancategorycode'} ||= $row->{'categorycode'};
459 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
460 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
461 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
462 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
463 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
464 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
465 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
466 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
468 $row->{'hardduedate'} = 0;
470 push @row_loop, $row;
474 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
477 if ($branch eq "*") {
478 $sth_branch_cat = $dbh->prepare("
479 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
480 FROM default_borrower_circ_rules
481 JOIN categories USING (categorycode)
484 $sth_branch_cat->execute();
486 $sth_branch_cat = $dbh->prepare("
487 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
488 FROM branch_borrower_circ_rules
489 JOIN categories USING (categorycode)
490 WHERE branch_borrower_circ_rules.branchcode = ?
492 $sth_branch_cat->execute($branch);
495 my @branch_cat_rules = ();
496 while (my $row = $sth_branch_cat->fetchrow_hashref) {
497 push @branch_cat_rules, $row;
499 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
501 # note undef maxissueqty so that template can deal with them
502 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
503 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
504 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
507 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
510 if ($branch eq "*") {
511 $sth_branch_item = $dbh->prepare("
512 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
513 FROM default_branch_item_rules
514 JOIN itemtypes USING (itemtype)
516 $sth_branch_item->execute();
518 $sth_branch_item = $dbh->prepare("
519 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
520 FROM branch_item_rules
521 JOIN itemtypes USING (itemtype)
522 WHERE branch_item_rules.branchcode = ?
524 $sth_branch_item->execute($branch);
527 my @branch_item_rules = ();
528 while (my $row = $sth_branch_item->fetchrow_hashref) {
529 push @branch_item_rules, $row;
531 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
533 # note undef holdallowed so that template can deal with them
534 foreach my $entry (@sorted_branch_item_rules) {
535 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
536 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
539 $template->param(show_branch_cat_rule_form => 1);
540 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
541 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
544 if ($branch eq "*") {
545 $sth_defaults = $dbh->prepare("
547 FROM default_circ_rules
549 $sth_defaults->execute();
551 $sth_defaults = $dbh->prepare("
553 FROM default_branch_circ_rules
556 $sth_defaults->execute($branch);
559 my $defaults = $sth_defaults->fetchrow_hashref;
562 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
563 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
564 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
565 $template->param(default_maxissueqty => $defaults->{maxissueqty});
566 $template->param(default_maxonsiteissueqty => $defaults->{maxonsiteissueqty});
567 $template->param(default_returnbranch => $defaults->{returnbranch});
570 $template->param(default_rules => ($defaults ? 1 : 0));
572 $template->param(categoryloop => \@category_loop,
573 itemtypeloop => \@itemtypes,
574 rules => \@sorted_row_loop,
575 branchloop => \@branchloop,
576 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
577 current_branch => $branch,
578 definedbranch => scalar(@sorted_row_loop)>0
580 output_html_with_http_headers $input, $cookie, $template->output;
584 # sort by patron category, then item type, putting
585 # default entries at the bottom
586 sub by_category_and_itemtype {
587 unless (by_category($a, $b)) {
588 return by_itemtype($a, $b);
594 if ($a->{'default_humancategorycode'}) {
595 return ($b->{'default_humancategorycode'} ? 0 : 1);
596 } elsif ($b->{'default_humancategorycode'}) {
599 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
605 if ($a->{'default_humanitemtype'}) {
606 return ($b->{'default_humanitemtype'} ? 0 : 1);
607 } elsif ($b->{'default_humanitemtype'}) {
610 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};