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');
47 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
48 my $op = $input->param('op') || q{};
50 if ($op eq 'delete') {
51 my $itemtype = $input->param('itemtype');
52 my $categorycode = $input->param('categorycode');
53 $debug and warn "deleting $1 $2 $branch";
55 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
56 $sth_Idelete->execute($branch, $categorycode, $itemtype);
58 elsif ($op eq 'delete-branch-cat') {
59 my $categorycode = $input->param('categorycode');
61 if ($categorycode eq "*") {
62 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
63 $sth_delete->execute();
65 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
66 WHERE categorycode = ?");
67 $sth_delete->execute($categorycode);
69 } elsif ($categorycode eq "*") {
70 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
71 WHERE branchcode = ?");
72 $sth_delete->execute($branch);
74 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
76 AND categorycode = ?");
77 $sth_delete->execute($branch, $categorycode);
80 elsif ($op eq 'delete-branch-item') {
81 my $itemtype = $input->param('itemtype');
83 if ($itemtype eq "*") {
84 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
85 $sth_delete->execute();
87 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
89 $sth_delete->execute($itemtype);
91 } elsif ($itemtype eq "*") {
92 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
93 WHERE branchcode = ?");
94 $sth_delete->execute($branch);
96 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
99 $sth_delete->execute($branch, $itemtype);
102 # save the values entered
103 elsif ($op eq 'add') {
104 my $br = $branch; # branch
105 my $bor = $input->param('categorycode'); # borrower category
106 my $itemtype = $input->param('itemtype'); # item type
107 my $fine = $input->param('fine');
108 my $finedays = $input->param('finedays');
109 my $maxsuspensiondays = $input->param('maxsuspensiondays');
110 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
111 my $firstremind = $input->param('firstremind');
112 my $chargeperiod = $input->param('chargeperiod');
113 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
114 my $maxissueqty = $input->param('maxissueqty');
115 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
116 my $renewalsallowed = $input->param('renewalsallowed');
117 my $renewalperiod = $input->param('renewalperiod');
118 my $norenewalbefore = $input->param('norenewalbefore');
119 $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
120 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
121 my $reservesallowed = $input->param('reservesallowed');
122 my $onshelfholds = $input->param('onshelfholds') || 0;
123 $maxissueqty =~ s/\s//g;
124 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
125 $maxonsiteissueqty =~ s/\s//g;
126 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
127 my $issuelength = $input->param('issuelength');
128 my $lengthunit = $input->param('lengthunit');
129 my $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) };
130 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
131 my $hardduedatecompare = $input->param('hardduedatecompare');
132 my $rentaldiscount = $input->param('rentaldiscount');
133 my $opacitemholds = $input->param('opacitemholds') || 0;
134 my $overduefinescap = $input->param('overduefinescap') || undef;
135 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
137 my $schema = Koha::Database->new()->schema();
138 my $rs = $schema->resultset('Issuingrule');
142 categorycode => $bor,
143 itemtype => $itemtype,
145 finedays => $finedays,
146 maxsuspensiondays => $maxsuspensiondays,
147 firstremind => $firstremind,
148 chargeperiod => $chargeperiod,
149 chargeperiod_charge_at => $chargeperiod_charge_at,
150 maxissueqty => $maxissueqty,
151 maxonsiteissueqty => $maxonsiteissueqty,
152 renewalsallowed => $renewalsallowed,
153 renewalperiod => $renewalperiod,
154 norenewalbefore => $norenewalbefore,
155 auto_renew => $auto_renew,
156 reservesallowed => $reservesallowed,
157 issuelength => $issuelength,
158 lengthunit => $lengthunit,
159 hardduedate => $hardduedate,
160 hardduedatecompare => $hardduedatecompare,
161 rentaldiscount => $rentaldiscount,
162 onshelfholds => $onshelfholds,
163 opacitemholds => $opacitemholds,
164 overduefinescap => $overduefinescap,
167 $rs->update_or_create($params);
170 elsif ($op eq "set-branch-defaults") {
171 my $categorycode = $input->param('categorycode');
172 my $maxissueqty = $input->param('maxissueqty');
173 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
174 my $holdallowed = $input->param('holdallowed');
175 my $returnbranch = $input->param('returnbranch');
176 $maxissueqty =~ s/\s//g;
177 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
178 $maxonsiteissueqty =~ s/\s//g;
179 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
180 $holdallowed =~ s/\s//g;
181 $holdallowed = undef if $holdallowed !~ /^\d+/;
183 if ($branch eq "*") {
184 my $sth_search = $dbh->prepare("SELECT count(*) AS total
185 FROM default_circ_rules");
186 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
187 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
188 VALUES (?, ?, ?, ?)");
189 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
190 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
192 $sth_search->execute();
193 my $res = $sth_search->fetchrow_hashref();
195 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
197 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
200 my $sth_search = $dbh->prepare("SELECT count(*) AS total
201 FROM default_branch_circ_rules
202 WHERE branchcode = ?");
203 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
204 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
205 VALUES (?, ?, ?, ?, ?)");
206 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
207 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
208 WHERE branchcode = ?");
209 $sth_search->execute($branch);
210 my $res = $sth_search->fetchrow_hashref();
212 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
214 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
218 elsif ($op eq "add-branch-cat") {
219 my $categorycode = $input->param('categorycode');
220 my $maxissueqty = $input->param('maxissueqty');
221 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
222 $maxissueqty =~ s/\s//g;
223 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
224 $maxonsiteissueqty =~ s/\s//g;
225 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
227 if ($branch eq "*") {
228 if ($categorycode eq "*") {
229 my $sth_search = $dbh->prepare("SELECT count(*) AS total
230 FROM default_circ_rules");
231 my $sth_insert = $dbh->prepare(q|
232 INSERT INTO default_circ_rules
233 (maxissueqty, maxonsiteissueqty)
236 my $sth_update = $dbh->prepare(q|
237 UPDATE default_circ_rules
239 maxonsiteissueqty = ?
242 $sth_search->execute();
243 my $res = $sth_search->fetchrow_hashref();
245 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
247 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
250 my $sth_search = $dbh->prepare("SELECT count(*) AS total
251 FROM default_borrower_circ_rules
252 WHERE categorycode = ?");
253 my $sth_insert = $dbh->prepare(q|
254 INSERT INTO default_borrower_circ_rules
255 (categorycode, maxissueqty, maxonsiteissueqty)
258 my $sth_update = $dbh->prepare(q|
259 UPDATE default_borrower_circ_rules
261 maxonsiteissueqty = ?
262 WHERE categorycode = ?
264 $sth_search->execute($branch);
265 my $res = $sth_search->fetchrow_hashref();
267 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
269 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
272 } elsif ($categorycode eq "*") {
273 my $sth_search = $dbh->prepare("SELECT count(*) AS total
274 FROM default_branch_circ_rules
275 WHERE branchcode = ?");
276 my $sth_insert = $dbh->prepare(q|
277 INSERT INTO default_branch_circ_rules
278 (branchcode, maxissueqty, maxonsiteissueqty)
281 my $sth_update = $dbh->prepare(q|
282 UPDATE default_branch_circ_rules
284 maxonsiteissueqty = ?
287 $sth_search->execute($branch);
288 my $res = $sth_search->fetchrow_hashref();
290 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
292 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
295 my $sth_search = $dbh->prepare("SELECT count(*) AS total
296 FROM branch_borrower_circ_rules
298 AND categorycode = ?");
299 my $sth_insert = $dbh->prepare(q|
300 INSERT INTO branch_borrower_circ_rules
301 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
304 my $sth_update = $dbh->prepare(q|
305 UPDATE branch_borrower_circ_rules
307 maxonsiteissueqty = ?
312 $sth_search->execute($branch, $categorycode);
313 my $res = $sth_search->fetchrow_hashref();
315 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
317 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
321 elsif ($op eq "add-branch-item") {
322 my $itemtype = $input->param('itemtype');
323 my $holdallowed = $input->param('holdallowed');
324 my $returnbranch = $input->param('returnbranch');
325 $holdallowed =~ s/\s//g;
326 $holdallowed = undef if $holdallowed !~ /^\d+/;
328 if ($branch eq "*") {
329 if ($itemtype eq "*") {
330 my $sth_search = $dbh->prepare("SELECT count(*) AS total
331 FROM default_circ_rules");
332 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
333 (holdallowed, returnbranch)
335 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
336 SET holdallowed = ?, returnbranch = ?");
338 $sth_search->execute();
339 my $res = $sth_search->fetchrow_hashref();
341 $sth_update->execute($holdallowed, $returnbranch);
343 $sth_insert->execute($holdallowed, $returnbranch);
346 my $sth_search = $dbh->prepare("SELECT count(*) AS total
347 FROM default_branch_item_rules
348 WHERE itemtype = ?");
349 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
350 (itemtype, holdallowed, returnbranch)
352 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
353 SET holdallowed = ?, returnbranch = ?
354 WHERE itemtype = ?");
355 $sth_search->execute($itemtype);
356 my $res = $sth_search->fetchrow_hashref();
358 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
360 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
363 } elsif ($itemtype eq "*") {
364 my $sth_search = $dbh->prepare("SELECT count(*) AS total
365 FROM default_branch_circ_rules
366 WHERE branchcode = ?");
367 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
368 (branchcode, holdallowed, returnbranch)
370 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
371 SET holdallowed = ?, returnbranch = ?
372 WHERE branchcode = ?");
373 $sth_search->execute($branch);
374 my $res = $sth_search->fetchrow_hashref();
376 $sth_update->execute($holdallowed, $returnbranch, $branch);
378 $sth_insert->execute($branch, $holdallowed, $returnbranch);
381 my $sth_search = $dbh->prepare("SELECT count(*) AS total
382 FROM branch_item_rules
385 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
386 (branchcode, itemtype, holdallowed, returnbranch)
387 VALUES (?, ?, ?, ?)");
388 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
389 SET holdallowed = ?, returnbranch = ?
393 $sth_search->execute($branch, $itemtype);
394 my $res = $sth_search->fetchrow_hashref();
396 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
398 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
403 my $branches = GetBranches();
405 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
407 value => $thisbranch,
408 selected => $thisbranch eq $branch,
409 branchname => $branches->{$thisbranch}->{'branchname'},
413 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
416 while (my $data=$sth->fetchrow_hashref){
417 push @category_loop,$data;
421 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
426 while (my $row=$sth->fetchrow_hashref){
427 push @itemtypes,$row;
430 my $sth2 = $dbh->prepare("
431 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
434 ON (itemtypes.itemtype = issuingrules.itemtype)
436 ON (categories.categorycode = issuingrules.categorycode)
437 WHERE issuingrules.branchcode = ?
439 $sth2->execute($branch);
441 while (my $row = $sth2->fetchrow_hashref) {
442 $row->{'current_branch'} ||= $row->{'branchcode'};
443 $row->{'humanitemtype'} ||= $row->{'itemtype'};
444 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
445 $row->{'humancategorycode'} ||= $row->{'categorycode'};
446 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
447 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
448 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
449 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
450 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
451 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
452 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
453 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
455 $row->{'hardduedate'} = 0;
457 push @row_loop, $row;
461 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
464 if ($branch eq "*") {
465 $sth_branch_cat = $dbh->prepare("
466 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
467 FROM default_borrower_circ_rules
468 JOIN categories USING (categorycode)
471 $sth_branch_cat->execute();
473 $sth_branch_cat = $dbh->prepare("
474 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
475 FROM branch_borrower_circ_rules
476 JOIN categories USING (categorycode)
477 WHERE branch_borrower_circ_rules.branchcode = ?
479 $sth_branch_cat->execute($branch);
482 my @branch_cat_rules = ();
483 while (my $row = $sth_branch_cat->fetchrow_hashref) {
484 push @branch_cat_rules, $row;
486 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
488 # note undef maxissueqty so that template can deal with them
489 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
490 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
491 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
494 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
497 if ($branch eq "*") {
498 $sth_branch_item = $dbh->prepare("
499 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
500 FROM default_branch_item_rules
501 JOIN itemtypes USING (itemtype)
503 $sth_branch_item->execute();
505 $sth_branch_item = $dbh->prepare("
506 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
507 FROM branch_item_rules
508 JOIN itemtypes USING (itemtype)
509 WHERE branch_item_rules.branchcode = ?
511 $sth_branch_item->execute($branch);
514 my @branch_item_rules = ();
515 while (my $row = $sth_branch_item->fetchrow_hashref) {
516 push @branch_item_rules, $row;
518 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
520 # note undef holdallowed so that template can deal with them
521 foreach my $entry (@sorted_branch_item_rules) {
522 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
523 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
526 $template->param(show_branch_cat_rule_form => 1);
527 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
528 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
531 if ($branch eq "*") {
532 $sth_defaults = $dbh->prepare("
534 FROM default_circ_rules
536 $sth_defaults->execute();
538 $sth_defaults = $dbh->prepare("
540 FROM default_branch_circ_rules
543 $sth_defaults->execute($branch);
546 my $defaults = $sth_defaults->fetchrow_hashref;
549 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
550 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
551 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
552 $template->param(default_maxissueqty => $defaults->{maxissueqty});
553 $template->param(default_maxonsiteissueqty => $defaults->{maxonsiteissueqty});
554 $template->param(default_returnbranch => $defaults->{returnbranch});
557 $template->param(default_rules => ($defaults ? 1 : 0));
559 $template->param(categoryloop => \@category_loop,
560 itemtypeloop => \@itemtypes,
561 rules => \@sorted_row_loop,
562 branchloop => \@branchloop,
563 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
564 current_branch => $branch,
565 definedbranch => scalar(@sorted_row_loop)>0
567 output_html_with_http_headers $input, $cookie, $template->output;
571 # sort by patron category, then item type, putting
572 # default entries at the bottom
573 sub by_category_and_itemtype {
574 unless (by_category($a, $b)) {
575 return by_itemtype($a, $b);
581 if ($a->{'default_humancategorycode'}) {
582 return ($b->{'default_humancategorycode'} ? 0 : 1);
583 } elsif ($b->{'default_humancategorycode'}) {
586 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
592 if ($a->{'default_humanitemtype'}) {
593 return ($b->{'default_humanitemtype'} ? 0 : 1);
594 } elsif ($b->{'default_humanitemtype'}) {
597 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};