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.tmpl",
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, 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=?, 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 my $firstremind = $input->param('firstremind');
114 my $chargeperiod = $input->param('chargeperiod');
115 my $maxissueqty = $input->param('maxissueqty');
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 $reservesallowed = $input->param('reservesallowed');
121 $maxissueqty =~ s/\s//g;
122 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
123 my $issuelength = $input->param('issuelength');
124 my $lengthunit = $input->param('lengthunit');
125 my $hardduedate = $input->param('hardduedate');
126 $hardduedate = format_date_in_iso($hardduedate);
127 my $hardduedatecompare = $input->param('hardduedatecompare');
128 my $rentaldiscount = $input->param('rentaldiscount');
129 my $overduefinescap = $input->param('overduefinescap') || undef;
130 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
132 $sth_search->execute($br,$bor,$cat);
133 my $res = $sth_search->fetchrow_hashref();
135 $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
137 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
140 elsif ($op eq "set-branch-defaults") {
141 my $categorycode = $input->param('categorycode');
142 my $maxissueqty = $input->param('maxissueqty');
143 my $holdallowed = $input->param('holdallowed');
144 my $returnbranch = $input->param('returnbranch');
145 $maxissueqty =~ s/\s//g;
146 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
147 $holdallowed =~ s/\s//g;
148 $holdallowed = undef if $holdallowed !~ /^\d+/;
150 if ($branch eq "*") {
151 my $sth_search = $dbh->prepare("SELECT count(*) AS total
152 FROM default_circ_rules");
153 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
154 (maxissueqty, holdallowed, returnbranch)
156 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
157 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
159 $sth_search->execute();
160 my $res = $sth_search->fetchrow_hashref();
162 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
164 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
167 my $sth_search = $dbh->prepare("SELECT count(*) AS total
168 FROM default_branch_circ_rules
169 WHERE branchcode = ?");
170 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
171 (branchcode, maxissueqty, holdallowed, returnbranch)
172 VALUES (?, ?, ?, ?)");
173 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
174 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
175 WHERE branchcode = ?");
176 $sth_search->execute($branch);
177 my $res = $sth_search->fetchrow_hashref();
179 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
181 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
185 elsif ($op eq "add-branch-cat") {
186 my $categorycode = $input->param('categorycode');
187 my $maxissueqty = $input->param('maxissueqty');
188 $maxissueqty =~ s/\s//g;
189 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
191 if ($branch eq "*") {
192 if ($categorycode eq "*") {
193 my $sth_search = $dbh->prepare("SELECT count(*) AS total
194 FROM default_circ_rules");
195 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
198 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
199 SET maxissueqty = ?");
201 $sth_search->execute();
202 my $res = $sth_search->fetchrow_hashref();
204 $sth_update->execute($maxissueqty);
206 $sth_insert->execute($maxissueqty);
209 my $sth_search = $dbh->prepare("SELECT count(*) AS total
210 FROM default_borrower_circ_rules
211 WHERE categorycode = ?");
212 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
213 (categorycode, maxissueqty)
215 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
217 WHERE categorycode = ?");
218 $sth_search->execute($branch);
219 my $res = $sth_search->fetchrow_hashref();
221 $sth_update->execute($maxissueqty, $categorycode);
223 $sth_insert->execute($categorycode, $maxissueqty);
226 } elsif ($categorycode eq "*") {
227 my $sth_search = $dbh->prepare("SELECT count(*) AS total
228 FROM default_branch_circ_rules
229 WHERE branchcode = ?");
230 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
231 (branchcode, maxissueqty)
233 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
235 WHERE branchcode = ?");
236 $sth_search->execute($branch);
237 my $res = $sth_search->fetchrow_hashref();
239 $sth_update->execute($maxissueqty, $branch);
241 $sth_insert->execute($branch, $maxissueqty);
244 my $sth_search = $dbh->prepare("SELECT count(*) AS total
245 FROM branch_borrower_circ_rules
247 AND categorycode = ?");
248 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
249 (branchcode, categorycode, maxissueqty)
251 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
254 AND categorycode = ?");
256 $sth_search->execute($branch, $categorycode);
257 my $res = $sth_search->fetchrow_hashref();
259 $sth_update->execute($maxissueqty, $branch, $categorycode);
261 $sth_insert->execute($branch, $categorycode, $maxissueqty);
265 elsif ($op eq "add-branch-item") {
266 my $itemtype = $input->param('itemtype');
267 my $holdallowed = $input->param('holdallowed');
268 my $returnbranch = $input->param('returnbranch');
269 $holdallowed =~ s/\s//g;
270 $holdallowed = undef if $holdallowed !~ /^\d+/;
272 if ($branch eq "*") {
273 if ($itemtype eq "*") {
274 my $sth_search = $dbh->prepare("SELECT count(*) AS total
275 FROM default_circ_rules");
276 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
277 (holdallowed, returnbranch)
279 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
280 SET holdallowed = ?, returnbranch = ?");
282 $sth_search->execute();
283 my $res = $sth_search->fetchrow_hashref();
285 $sth_update->execute($holdallowed, $returnbranch);
287 $sth_insert->execute($holdallowed, $returnbranch);
290 my $sth_search = $dbh->prepare("SELECT count(*) AS total
291 FROM default_branch_item_rules
292 WHERE itemtype = ?");
293 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
294 (itemtype, holdallowed, returnbranch)
296 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
297 SET holdallowed = ?, returnbranch = ?
298 WHERE itemtype = ?");
299 $sth_search->execute($itemtype);
300 my $res = $sth_search->fetchrow_hashref();
302 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
304 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
307 } elsif ($itemtype eq "*") {
308 my $sth_search = $dbh->prepare("SELECT count(*) AS total
309 FROM default_branch_circ_rules
310 WHERE branchcode = ?");
311 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
312 (branchcode, holdallowed, returnbranch)
314 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
315 SET holdallowed = ?, returnbranch = ?
316 WHERE branchcode = ?");
317 $sth_search->execute($branch);
318 my $res = $sth_search->fetchrow_hashref();
320 $sth_update->execute($holdallowed, $returnbranch, $branch);
322 $sth_insert->execute($branch, $holdallowed, $returnbranch);
325 my $sth_search = $dbh->prepare("SELECT count(*) AS total
326 FROM branch_item_rules
329 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
330 (branchcode, itemtype, holdallowed, returnbranch)
331 VALUES (?, ?, ?, ?)");
332 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
333 SET holdallowed = ?, returnbranch = ?
337 $sth_search->execute($branch, $itemtype);
338 my $res = $sth_search->fetchrow_hashref();
340 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
342 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
347 my $branches = GetBranches();
349 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
351 value => $thisbranch,
352 selected => $thisbranch eq $branch,
353 branchname => $branches->{$thisbranch}->{'branchname'},
357 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
360 while (my $data=$sth->fetchrow_hashref){
361 push @category_loop,$data;
365 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
370 while (my $row=$sth->fetchrow_hashref){
371 push @itemtypes,$row;
374 my $sth2 = $dbh->prepare("
375 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
378 ON (itemtypes.itemtype = issuingrules.itemtype)
380 ON (categories.categorycode = issuingrules.categorycode)
381 WHERE issuingrules.branchcode = ?
383 $sth2->execute($branch);
385 while (my $row = $sth2->fetchrow_hashref) {
386 $row->{'current_branch'} ||= $row->{'branchcode'};
387 $row->{'humanitemtype'} ||= $row->{'itemtype'};
388 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
389 $row->{'humancategorycode'} ||= $row->{'categorycode'};
390 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
391 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
392 if ($row->{'hardduedate'} ne '0000-00-00') {
393 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
394 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
395 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
396 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
398 $row->{'hardduedate'} = 0;
400 push @row_loop, $row;
404 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
407 if ($branch eq "*") {
408 $sth_branch_cat = $dbh->prepare("
409 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
410 FROM default_borrower_circ_rules
411 JOIN categories USING (categorycode)
414 $sth_branch_cat->execute();
416 $sth_branch_cat = $dbh->prepare("
417 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
418 FROM branch_borrower_circ_rules
419 JOIN categories USING (categorycode)
420 WHERE branch_borrower_circ_rules.branchcode = ?
422 $sth_branch_cat->execute($branch);
425 my @branch_cat_rules = ();
426 while (my $row = $sth_branch_cat->fetchrow_hashref) {
427 push @branch_cat_rules, $row;
429 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
431 # note undef maxissueqty so that template can deal with them
432 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
433 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
436 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
439 if ($branch eq "*") {
440 $sth_branch_item = $dbh->prepare("
441 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
442 FROM default_branch_item_rules
443 JOIN itemtypes USING (itemtype)
445 $sth_branch_item->execute();
447 $sth_branch_item = $dbh->prepare("
448 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
449 FROM branch_item_rules
450 JOIN itemtypes USING (itemtype)
451 WHERE branch_item_rules.branchcode = ?
453 $sth_branch_item->execute($branch);
456 my @branch_item_rules = ();
457 while (my $row = $sth_branch_item->fetchrow_hashref) {
458 push @branch_item_rules, $row;
460 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
462 # note undef holdallowed so that template can deal with them
463 foreach my $entry (@sorted_branch_item_rules) {
464 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
465 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
468 $template->param(show_branch_cat_rule_form => 1);
469 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
470 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
473 if ($branch eq "*") {
474 $sth_defaults = $dbh->prepare("
476 FROM default_circ_rules
478 $sth_defaults->execute();
480 $sth_defaults = $dbh->prepare("
482 FROM default_branch_circ_rules
485 $sth_defaults->execute($branch);
488 my $defaults = $sth_defaults->fetchrow_hashref;
491 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
492 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
493 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
494 $template->param(default_maxissueqty => $defaults->{maxissueqty});
495 $template->param(default_returnbranch => $defaults->{returnbranch});
498 $template->param(default_rules => ($defaults ? 1 : 0));
500 $template->param(categoryloop => \@category_loop,
501 itemtypeloop => \@itemtypes,
502 rules => \@sorted_row_loop,
503 branchloop => \@branchloop,
504 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
505 current_branch => $branch,
506 definedbranch => scalar(@sorted_row_loop)>0
508 output_html_with_http_headers $input, $cookie, $template->output;
512 # sort by patron category, then item type, putting
513 # default entries at the bottom
514 sub by_category_and_itemtype {
515 unless (by_category($a, $b)) {
516 return by_itemtype($a, $b);
522 if ($a->{'default_humancategorycode'}) {
523 return ($b->{'default_humancategorycode'} ? 0 : 1);
524 } elsif ($b->{'default_humancategorycode'}) {
527 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
533 if ($a->{'default_humanitemtype'}) {
534 return ($b->{'default_humanitemtype'} ? 0 : 1);
535 } elsif ($b->{'default_humanitemtype'}) {
538 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};