3 # Copyright 2000-2002 Katipo Communications
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.
27 use C4::Branch; # GetBranches
30 my $dbh = C4::Context->dbh;
33 # $flagsrequired->{circulation}=1;
34 my ($template, $loggedinuser, $cookie)
35 = get_template_and_user({template_name => "admin/smart-rules.tmpl",
39 flagsrequired => {parameters => 1},
43 my $type=$input->param('type');
44 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
45 my $op = $input->param('op');
47 if ($op eq 'delete') {
48 my $itemtype = $input->param('itemtype');
49 my $categorycode = $input->param('categorycode');
50 $debug and warn "deleting $1 $2 $branch";
52 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
53 $sth_Idelete->execute($branch, $categorycode, $itemtype);
55 elsif ($op eq 'delete-branch-cat') {
56 my $categorycode = $input->param('categorycode');
58 if ($categorycode eq "*") {
59 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
60 $sth_delete->execute();
62 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
63 WHERE categorycode = ?");
64 $sth_delete->execute($categorycode);
66 } elsif ($categorycode eq "*") {
67 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
68 WHERE branchcode = ?");
69 $sth_delete->execute($branch);
71 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
73 AND categorycode = ?");
74 $sth_delete->execute($branch, $categorycode);
77 elsif ($op eq 'delete-branch-item') {
78 my $itemtype = $input->param('itemtype');
80 if ($itemtype eq "*") {
81 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
82 $sth_delete->execute();
84 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
86 $sth_delete->execute($itemtype);
88 } elsif ($itemtype eq "*") {
89 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
90 WHERE branchcode = ?");
91 $sth_delete->execute($branch);
93 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
96 $sth_delete->execute($branch, $itemtype);
99 # save the values entered
100 elsif ($op eq 'add') {
101 my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
102 my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, reservesallowed, issuelength, fine, finedays, firstremind, chargeperiod,rentaldiscount) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
103 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, issuelength=?, rentaldiscount=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
105 my $br = $branch; # branch
106 my $bor = $input->param('categorycode'); # borrower category
107 my $cat = $input->param('itemtype'); # item type
108 my $fine = $input->param('fine');
109 my $finedays = $input->param('finedays');
110 my $firstremind = $input->param('firstremind');
111 my $chargeperiod = $input->param('chargeperiod');
112 my $maxissueqty = $input->param('maxissueqty');
113 my $renewalsallowed = $input->param('renewalsallowed');
114 my $reservesallowed = $input->param('reservesallowed');
115 $maxissueqty =~ s/\s//g;
116 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
117 my $issuelength = $input->param('issuelength');
118 my $rentaldiscount = $input->param('rentaldiscount');
119 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
121 $sth_search->execute($br,$bor,$cat);
122 my $res = $sth_search->fetchrow_hashref();
124 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$rentaldiscount, $br,$bor,$cat);
126 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount);
129 elsif ($op eq "set-branch-defaults") {
130 my $categorycode = $input->param('categorycode');
131 my $maxissueqty = $input->param('maxissueqty');
132 my $holdallowed = $input->param('holdallowed');
133 $maxissueqty =~ s/\s//g;
134 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
135 $holdallowed =~ s/\s//g;
136 $holdallowed = undef if $holdallowed !~ /^\d+/;
138 if ($branch eq "*") {
139 my $sth_search = $dbh->prepare("SELECT count(*) AS total
140 FROM default_circ_rules");
141 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
142 (maxissueqty, holdallowed)
144 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
145 SET maxissueqty = ?, holdallowed = ?");
147 $sth_search->execute();
148 my $res = $sth_search->fetchrow_hashref();
150 $sth_update->execute($maxissueqty, $holdallowed);
152 $sth_insert->execute($maxissueqty, $holdallowed);
155 my $sth_search = $dbh->prepare("SELECT count(*) AS total
156 FROM default_branch_circ_rules
157 WHERE branchcode = ?");
158 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
159 (branchcode, maxissueqty, holdallowed)
161 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
162 SET maxissueqty = ?, holdallowed = ?
163 WHERE branchcode = ?");
164 $sth_search->execute($branch);
165 my $res = $sth_search->fetchrow_hashref();
167 $sth_update->execute($maxissueqty, $holdallowed, $branch);
169 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
173 elsif ($op eq "add-branch-cat") {
174 my $categorycode = $input->param('categorycode');
175 my $maxissueqty = $input->param('maxissueqty');
176 $maxissueqty =~ s/\s//g;
177 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
179 if ($branch eq "*") {
180 if ($categorycode eq "*") {
181 my $sth_search = $dbh->prepare("SELECT count(*) AS total
182 FROM default_circ_rules");
183 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
186 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
187 SET maxissueqty = ?");
189 $sth_search->execute();
190 my $res = $sth_search->fetchrow_hashref();
192 $sth_update->execute($maxissueqty);
194 $sth_insert->execute($maxissueqty);
197 my $sth_search = $dbh->prepare("SELECT count(*) AS total
198 FROM default_borrower_circ_rules
199 WHERE categorycode = ?");
200 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
201 (categorycode, maxissueqty)
203 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
205 WHERE categorycode = ?");
206 $sth_search->execute($branch);
207 my $res = $sth_search->fetchrow_hashref();
209 $sth_update->execute($maxissueqty, $categorycode);
211 $sth_insert->execute($categorycode, $maxissueqty);
214 } elsif ($categorycode eq "*") {
215 my $sth_search = $dbh->prepare("SELECT count(*) AS total
216 FROM default_branch_circ_rules
217 WHERE branchcode = ?");
218 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
219 (branchcode, maxissueqty)
221 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
223 WHERE branchcode = ?");
224 $sth_search->execute($branch);
225 my $res = $sth_search->fetchrow_hashref();
227 $sth_update->execute($maxissueqty, $branch);
229 $sth_insert->execute($branch, $maxissueqty);
232 my $sth_search = $dbh->prepare("SELECT count(*) AS total
233 FROM branch_borrower_circ_rules
235 AND categorycode = ?");
236 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
237 (branchcode, categorycode, maxissueqty)
239 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
242 AND categorycode = ?");
244 $sth_search->execute($branch, $categorycode);
245 my $res = $sth_search->fetchrow_hashref();
247 $sth_update->execute($maxissueqty, $branch, $categorycode);
249 $sth_insert->execute($branch, $categorycode, $maxissueqty);
253 elsif ($op eq "add-branch-item") {
254 my $itemtype = $input->param('itemtype');
255 my $holdallowed = $input->param('holdallowed');
256 $holdallowed =~ s/\s//g;
257 $holdallowed = undef if $holdallowed !~ /^\d+/;
259 if ($branch eq "*") {
260 if ($itemtype eq "*") {
261 my $sth_search = $dbh->prepare("SELECT count(*) AS total
262 FROM default_circ_rules");
263 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
266 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
267 SET holdallowed = ?");
269 $sth_search->execute();
270 my $res = $sth_search->fetchrow_hashref();
272 $sth_update->execute($holdallowed);
274 $sth_insert->execute($holdallowed);
277 my $sth_search = $dbh->prepare("SELECT count(*) AS total
278 FROM default_branch_item_rules
279 WHERE itemtype = ?");
280 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
281 (itemtype, holdallowed)
283 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
285 WHERE itemtype = ?");
286 $sth_search->execute($itemtype);
287 my $res = $sth_search->fetchrow_hashref();
289 $sth_update->execute($holdallowed, $itemtype);
291 $sth_insert->execute($itemtype, $holdallowed);
294 } elsif ($itemtype eq "*") {
295 my $sth_search = $dbh->prepare("SELECT count(*) AS total
296 FROM default_branch_circ_rules
297 WHERE branchcode = ?");
298 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
299 (branchcode, holdallowed)
301 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
303 WHERE branchcode = ?");
304 $sth_search->execute($branch);
305 my $res = $sth_search->fetchrow_hashref();
307 $sth_update->execute($holdallowed, $branch);
309 $sth_insert->execute($branch, $holdallowed);
312 my $sth_search = $dbh->prepare("SELECT count(*) AS total
313 FROM branch_item_rules
316 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
317 (branchcode, itemtype, holdallowed)
319 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
324 $sth_search->execute($branch, $itemtype);
325 my $res = $sth_search->fetchrow_hashref();
327 $sth_update->execute($holdallowed, $branch, $itemtype);
329 $sth_insert->execute($branch, $itemtype, $holdallowed);
334 my $branches = GetBranches();
336 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
337 my $selected = 1 if $thisbranch eq $branch;
338 my %row =(value => $thisbranch,
339 selected => $selected,
340 branchname => $branches->{$thisbranch}->{'branchname'},
342 push @branchloop, \%row;
345 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
348 while (my $data=$sth->fetchrow_hashref){
349 push @category_loop,$data;
353 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
358 while (my $row=$sth->fetchrow_hashref){
359 push @itemtypes,$row;
362 my $sth2 = $dbh->prepare("
363 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
366 ON (itemtypes.itemtype = issuingrules.itemtype)
368 ON (categories.categorycode = issuingrules.categorycode)
369 WHERE issuingrules.branchcode = ?
371 $sth2->execute($branch);
373 while (my $row = $sth2->fetchrow_hashref) {
374 $row->{'humanitemtype'} ||= $row->{'itemtype'};
375 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
376 $row->{'humancategorycode'} ||= $row->{'categorycode'};
377 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
378 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
379 push @row_loop, $row;
383 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
386 if ($branch eq "*") {
387 $sth_branch_cat = $dbh->prepare("
388 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
389 FROM default_borrower_circ_rules
390 JOIN categories USING (categorycode)
393 $sth_branch_cat->execute();
395 $sth_branch_cat = $dbh->prepare("
396 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
397 FROM branch_borrower_circ_rules
398 JOIN categories USING (categorycode)
399 WHERE branch_borrower_circ_rules.branchcode = ?
401 $sth_branch_cat->execute($branch);
404 my @branch_cat_rules = ();
405 while (my $row = $sth_branch_cat->fetchrow_hashref) {
406 push @branch_cat_rules, $row;
408 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
410 # note undef maxissueqty so that template can deal with them
411 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
412 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
415 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
418 if ($branch eq "*") {
419 $sth_branch_item = $dbh->prepare("
420 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
421 FROM default_branch_item_rules
422 JOIN itemtypes USING (itemtype)
424 $sth_branch_item->execute();
426 $sth_branch_item = $dbh->prepare("
427 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
428 FROM branch_item_rules
429 JOIN itemtypes USING (itemtype)
430 WHERE branch_item_rules.branchcode = ?
432 $sth_branch_item->execute($branch);
435 my @branch_item_rules = ();
436 while (my $row = $sth_branch_item->fetchrow_hashref) {
437 push @branch_item_rules, $row;
439 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
441 # note undef holdallowed so that template can deal with them
442 foreach my $entry (@sorted_branch_item_rules) {
443 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
444 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
447 $template->param(show_branch_cat_rule_form => 1);
448 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
449 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
452 if ($branch eq "*") {
453 $sth_defaults = $dbh->prepare("
455 FROM default_circ_rules
457 $sth_defaults->execute();
459 $sth_defaults = $dbh->prepare("
461 FROM default_branch_circ_rules
464 $sth_defaults->execute($branch);
467 my $defaults = $sth_defaults->fetchrow_hashref;
470 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
471 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
472 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
473 $template->param(default_maxissueqty => $defaults->{maxissueqty});
476 $template->param(default_rules => ($defaults ? 1 : 0));
478 $template->param(categoryloop => \@category_loop,
479 itemtypeloop => \@itemtypes,
480 rules => \@sorted_row_loop,
481 branchloop => \@branchloop,
482 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
484 definedbranch => scalar(@sorted_row_loop)>0
486 output_html_with_http_headers $input, $cookie, $template->output;
490 # sort by patron category, then item type, putting
491 # default entries at the bottom
492 sub by_category_and_itemtype {
493 unless (by_category($a, $b)) {
494 return by_itemtype($a, $b);
500 if ($a->{'default_humancategorycode'}) {
501 return ($b->{'default_humancategorycode'} ? 0 : 1);
502 } elsif ($b->{'default_humancategorycode'}) {
505 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
511 if ($a->{'default_humanitemtype'}) {
512 return ($b->{'default_humanitemtype'} ? 0 : 1);
513 } elsif ($b->{'default_humanitemtype'}) {
516 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};