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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 use C4
::Branch
; # GetBranches
29 use C4
::Dates qw
/format_date format_date_in_iso/;
32 my $dbh = C4
::Context
->dbh;
35 # $flagsrequired->{circulation}=1;
36 my ($template, $loggedinuser, $cookie)
37 = get_template_and_user
({template_name
=> "admin/smart-rules.tt",
41 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
45 my $type=$input->param('type');
46 my $branch = $input->param('branch') || ( C4
::Branch
::onlymine
() ?
( C4
::Branch
::mybranch
() || '*' ) : '*' );
47 my $op = $input->param('op') || q{};
49 if ($op eq 'delete') {
50 my $itemtype = $input->param('itemtype');
51 my $categorycode = $input->param('categorycode');
52 $debug and warn "deleting $1 $2 $branch";
54 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
55 $sth_Idelete->execute($branch, $categorycode, $itemtype);
57 elsif ($op eq 'delete-branch-cat') {
58 my $categorycode = $input->param('categorycode');
60 if ($categorycode eq "*") {
61 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
62 $sth_delete->execute();
64 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
65 WHERE categorycode = ?");
66 $sth_delete->execute($categorycode);
68 } elsif ($categorycode eq "*") {
69 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
70 WHERE branchcode = ?");
71 $sth_delete->execute($branch);
73 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
75 AND categorycode = ?");
76 $sth_delete->execute($branch, $categorycode);
79 elsif ($op eq 'delete-branch-item') {
80 my $itemtype = $input->param('itemtype');
82 if ($itemtype eq "*") {
83 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
84 $sth_delete->execute();
86 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
88 $sth_delete->execute($itemtype);
90 } elsif ($itemtype eq "*") {
91 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
92 WHERE branchcode = ?");
93 $sth_delete->execute($branch);
95 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
98 $sth_delete->execute($branch, $itemtype);
101 # save the values entered
102 elsif ($op eq 'add') {
103 my $sth_search = $dbh->prepare('SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?');
104 my $sth_insert = $dbh->prepare('INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, renewalperiod, norenewalbefore, auto_renew, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, maxsuspensiondays, firstremind, chargeperiod,rentaldiscount, overduefinescap) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
105 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, maxsuspensiondays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, renewalperiod=?, norenewalbefore=?, auto_renew=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=?, overduefinescap=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
107 my $br = $branch; # branch
108 my $bor = $input->param('categorycode'); # borrower category
109 my $cat = $input->param('itemtype'); # item type
110 my $fine = $input->param('fine');
111 my $finedays = $input->param('finedays');
112 my $maxsuspensiondays = $input->param('maxsuspensiondays');
113 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
114 my $firstremind = $input->param('firstremind');
115 my $chargeperiod = $input->param('chargeperiod');
116 my $maxissueqty = $input->param('maxissueqty');
117 my $renewalsallowed = $input->param('renewalsallowed');
118 my $renewalperiod = $input->param('renewalperiod');
119 my $norenewalbefore = $input->param('norenewalbefore');
120 $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
121 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
122 my $reservesallowed = $input->param('reservesallowed');
123 $maxissueqty =~ s/\s//g;
124 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
125 my $issuelength = $input->param('issuelength');
126 my $lengthunit = $input->param('lengthunit');
127 my $hardduedate = $input->param('hardduedate');
128 $hardduedate = format_date_in_iso
($hardduedate);
129 my $hardduedatecompare = $input->param('hardduedatecompare');
130 my $rentaldiscount = $input->param('rentaldiscount');
131 my $overduefinescap = $input->param('overduefinescap') || undef;
132 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
134 $sth_search->execute($br,$bor,$cat);
135 my $res = $sth_search->fetchrow_hashref();
137 $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
139 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
142 elsif ($op eq "set-branch-defaults") {
143 my $categorycode = $input->param('categorycode');
144 my $maxissueqty = $input->param('maxissueqty');
145 my $holdallowed = $input->param('holdallowed');
146 my $returnbranch = $input->param('returnbranch');
147 $maxissueqty =~ s/\s//g;
148 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
149 $holdallowed =~ s/\s//g;
150 $holdallowed = undef if $holdallowed !~ /^\d+/;
152 if ($branch eq "*") {
153 my $sth_search = $dbh->prepare("SELECT count(*) AS total
154 FROM default_circ_rules");
155 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
156 (maxissueqty, holdallowed, returnbranch)
158 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
159 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
161 $sth_search->execute();
162 my $res = $sth_search->fetchrow_hashref();
164 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
166 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
169 my $sth_search = $dbh->prepare("SELECT count(*) AS total
170 FROM default_branch_circ_rules
171 WHERE branchcode = ?");
172 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
173 (branchcode, maxissueqty, holdallowed, returnbranch)
174 VALUES (?, ?, ?, ?)");
175 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
176 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
177 WHERE branchcode = ?");
178 $sth_search->execute($branch);
179 my $res = $sth_search->fetchrow_hashref();
181 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
183 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
187 elsif ($op eq "add-branch-cat") {
188 my $categorycode = $input->param('categorycode');
189 my $maxissueqty = $input->param('maxissueqty');
190 $maxissueqty =~ s/\s//g;
191 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
193 if ($branch eq "*") {
194 if ($categorycode eq "*") {
195 my $sth_search = $dbh->prepare("SELECT count(*) AS total
196 FROM default_circ_rules");
197 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
200 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
201 SET maxissueqty = ?");
203 $sth_search->execute();
204 my $res = $sth_search->fetchrow_hashref();
206 $sth_update->execute($maxissueqty);
208 $sth_insert->execute($maxissueqty);
211 my $sth_search = $dbh->prepare("SELECT count(*) AS total
212 FROM default_borrower_circ_rules
213 WHERE categorycode = ?");
214 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
215 (categorycode, maxissueqty)
217 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
219 WHERE categorycode = ?");
220 $sth_search->execute($branch);
221 my $res = $sth_search->fetchrow_hashref();
223 $sth_update->execute($maxissueqty, $categorycode);
225 $sth_insert->execute($categorycode, $maxissueqty);
228 } elsif ($categorycode eq "*") {
229 my $sth_search = $dbh->prepare("SELECT count(*) AS total
230 FROM default_branch_circ_rules
231 WHERE branchcode = ?");
232 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
233 (branchcode, maxissueqty)
235 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
237 WHERE branchcode = ?");
238 $sth_search->execute($branch);
239 my $res = $sth_search->fetchrow_hashref();
241 $sth_update->execute($maxissueqty, $branch);
243 $sth_insert->execute($branch, $maxissueqty);
246 my $sth_search = $dbh->prepare("SELECT count(*) AS total
247 FROM branch_borrower_circ_rules
249 AND categorycode = ?");
250 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
251 (branchcode, categorycode, maxissueqty)
253 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
256 AND categorycode = ?");
258 $sth_search->execute($branch, $categorycode);
259 my $res = $sth_search->fetchrow_hashref();
261 $sth_update->execute($maxissueqty, $branch, $categorycode);
263 $sth_insert->execute($branch, $categorycode, $maxissueqty);
267 elsif ($op eq "add-branch-item") {
268 my $itemtype = $input->param('itemtype');
269 my $holdallowed = $input->param('holdallowed');
270 my $returnbranch = $input->param('returnbranch');
271 $holdallowed =~ s/\s//g;
272 $holdallowed = undef if $holdallowed !~ /^\d+/;
274 if ($branch eq "*") {
275 if ($itemtype eq "*") {
276 my $sth_search = $dbh->prepare("SELECT count(*) AS total
277 FROM default_circ_rules");
278 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
279 (holdallowed, returnbranch)
281 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
282 SET holdallowed = ?, returnbranch = ?");
284 $sth_search->execute();
285 my $res = $sth_search->fetchrow_hashref();
287 $sth_update->execute($holdallowed, $returnbranch);
289 $sth_insert->execute($holdallowed, $returnbranch);
292 my $sth_search = $dbh->prepare("SELECT count(*) AS total
293 FROM default_branch_item_rules
294 WHERE itemtype = ?");
295 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
296 (itemtype, holdallowed, returnbranch)
298 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
299 SET holdallowed = ?, returnbranch = ?
300 WHERE itemtype = ?");
301 $sth_search->execute($itemtype);
302 my $res = $sth_search->fetchrow_hashref();
304 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
306 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
309 } elsif ($itemtype eq "*") {
310 my $sth_search = $dbh->prepare("SELECT count(*) AS total
311 FROM default_branch_circ_rules
312 WHERE branchcode = ?");
313 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
314 (branchcode, holdallowed, returnbranch)
316 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
317 SET holdallowed = ?, returnbranch = ?
318 WHERE branchcode = ?");
319 $sth_search->execute($branch);
320 my $res = $sth_search->fetchrow_hashref();
322 $sth_update->execute($holdallowed, $returnbranch, $branch);
324 $sth_insert->execute($branch, $holdallowed, $returnbranch);
327 my $sth_search = $dbh->prepare("SELECT count(*) AS total
328 FROM branch_item_rules
331 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
332 (branchcode, itemtype, holdallowed, returnbranch)
333 VALUES (?, ?, ?, ?)");
334 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
335 SET holdallowed = ?, returnbranch = ?
339 $sth_search->execute($branch, $itemtype);
340 my $res = $sth_search->fetchrow_hashref();
342 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
344 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
349 my $branches = GetBranches
();
351 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
353 value
=> $thisbranch,
354 selected
=> $thisbranch eq $branch,
355 branchname
=> $branches->{$thisbranch}->{'branchname'},
359 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
362 while (my $data=$sth->fetchrow_hashref){
363 push @category_loop,$data;
367 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
372 while (my $row=$sth->fetchrow_hashref){
373 push @itemtypes,$row;
376 my $sth2 = $dbh->prepare("
377 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
380 ON (itemtypes.itemtype = issuingrules.itemtype)
382 ON (categories.categorycode = issuingrules.categorycode)
383 WHERE issuingrules.branchcode = ?
385 $sth2->execute($branch);
387 while (my $row = $sth2->fetchrow_hashref) {
388 $row->{'current_branch'} ||= $row->{'branchcode'};
389 $row->{'humanitemtype'} ||= $row->{'itemtype'};
390 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
391 $row->{'humancategorycode'} ||= $row->{'categorycode'};
392 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
393 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
394 if ($row->{'hardduedate'} ne '0000-00-00') {
395 $row->{'hardduedate'} = format_date
( $row->{'hardduedate'});
396 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
397 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
398 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
400 $row->{'hardduedate'} = 0;
402 push @row_loop, $row;
406 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
409 if ($branch eq "*") {
410 $sth_branch_cat = $dbh->prepare("
411 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
412 FROM default_borrower_circ_rules
413 JOIN categories USING (categorycode)
416 $sth_branch_cat->execute();
418 $sth_branch_cat = $dbh->prepare("
419 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
420 FROM branch_borrower_circ_rules
421 JOIN categories USING (categorycode)
422 WHERE branch_borrower_circ_rules.branchcode = ?
424 $sth_branch_cat->execute($branch);
427 my @branch_cat_rules = ();
428 while (my $row = $sth_branch_cat->fetchrow_hashref) {
429 push @branch_cat_rules, $row;
431 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
433 # note undef maxissueqty so that template can deal with them
434 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
435 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
438 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
441 if ($branch eq "*") {
442 $sth_branch_item = $dbh->prepare("
443 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
444 FROM default_branch_item_rules
445 JOIN itemtypes USING (itemtype)
447 $sth_branch_item->execute();
449 $sth_branch_item = $dbh->prepare("
450 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
451 FROM branch_item_rules
452 JOIN itemtypes USING (itemtype)
453 WHERE branch_item_rules.branchcode = ?
455 $sth_branch_item->execute($branch);
458 my @branch_item_rules = ();
459 while (my $row = $sth_branch_item->fetchrow_hashref) {
460 push @branch_item_rules, $row;
462 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
464 # note undef holdallowed so that template can deal with them
465 foreach my $entry (@sorted_branch_item_rules) {
466 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
467 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
470 $template->param(show_branch_cat_rule_form
=> 1);
471 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
472 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
475 if ($branch eq "*") {
476 $sth_defaults = $dbh->prepare("
478 FROM default_circ_rules
480 $sth_defaults->execute();
482 $sth_defaults = $dbh->prepare("
484 FROM default_branch_circ_rules
487 $sth_defaults->execute($branch);
490 my $defaults = $sth_defaults->fetchrow_hashref;
493 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
494 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
495 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
496 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
497 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
500 $template->param(default_rules
=> ($defaults ?
1 : 0));
502 $template->param(categoryloop
=> \
@category_loop,
503 itemtypeloop
=> \
@itemtypes,
504 rules
=> \
@sorted_row_loop,
505 branchloop
=> \
@branchloop,
506 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
507 current_branch
=> $branch,
508 definedbranch
=> scalar(@sorted_row_loop)>0
510 output_html_with_http_headers
$input, $cookie, $template->output;
514 # sort by patron category, then item type, putting
515 # default entries at the bottom
516 sub by_category_and_itemtype
{
517 unless (by_category
($a, $b)) {
518 return by_itemtype
($a, $b);
524 if ($a->{'default_humancategorycode'}) {
525 return ($b->{'default_humancategorycode'} ?
0 : 1);
526 } elsif ($b->{'default_humancategorycode'}) {
529 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
535 if ($a->{'default_humanitemtype'}) {
536 return ($b->{'default_humanitemtype'} ?
0 : 1);
537 } elsif ($b->{'default_humanitemtype'}) {
540 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};