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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
27 use C4::Branch; # GetBranches
30 my $dbh = C4::Context->dbh;
32 my $type=$input->param('type');
33 my $branch = $input->param('branch') || '*';
34 my $op = $input->param('op');
37 # $flagsrequired->{circulation}=1;
38 my ($template, $loggedinuser, $cookie)
39 = get_template_and_user({template_name => "admin/smart-rules.tmpl",
43 flagsrequired => {parameters => 1},
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, issuelength, fine, finedays, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?)");
103 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? 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 $maxissueqty =~ s/\s//g;
114 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
115 my $issuelength = $input->param('issuelength');
116 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
118 $sth_search->execute($br,$bor,$cat);
119 my $res = $sth_search->fetchrow_hashref();
121 $sth_update->execute($fine, $finedays, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
123 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$finedays,$firstremind,$chargeperiod);
126 elsif ($op eq "set-branch-defaults") {
127 my $categorycode = $input->param('categorycode');
128 my $maxissueqty = $input->param('maxissueqty');
129 my $holdallowed = $input->param('holdallowed');
130 $maxissueqty =~ s/\s//g;
131 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
132 $holdallowed =~ s/\s//g;
133 $holdallowed = undef if $holdallowed !~ /^\d+/;
135 if ($branch eq "*") {
136 my $sth_search = $dbh->prepare("SELECT count(*) AS total
137 FROM default_circ_rules");
138 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
139 (maxissueqty, holdallowed)
141 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
142 SET maxissueqty = ?, holdallowed = ?");
144 $sth_search->execute();
145 my $res = $sth_search->fetchrow_hashref();
147 $sth_update->execute($maxissueqty, $holdallowed);
149 $sth_insert->execute($maxissueqty, $holdallowed);
152 my $sth_search = $dbh->prepare("SELECT count(*) AS total
153 FROM default_branch_circ_rules
154 WHERE branchcode = ?");
155 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
156 (branchcode, maxissueqty, holdallowed)
158 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
159 SET maxissueqty = ?, holdallowed = ?
160 WHERE branchcode = ?");
161 $sth_search->execute($branch);
162 my $res = $sth_search->fetchrow_hashref();
164 $sth_update->execute($maxissueqty, $holdallowed, $branch);
166 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
170 elsif ($op eq "add-branch-cat") {
171 my $categorycode = $input->param('categorycode');
172 my $maxissueqty = $input->param('maxissueqty');
173 $maxissueqty =~ s/\s//g;
174 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
176 if ($branch eq "*") {
177 if ($categorycode eq "*") {
178 my $sth_search = $dbh->prepare("SELECT count(*) AS total
179 FROM default_circ_rules");
180 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
183 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
184 SET maxissueqty = ?");
186 $sth_search->execute();
187 my $res = $sth_search->fetchrow_hashref();
189 $sth_update->execute($maxissueqty);
191 $sth_insert->execute($maxissueqty);
194 my $sth_search = $dbh->prepare("SELECT count(*) AS total
195 FROM default_borrower_circ_rules
196 WHERE categorycode = ?");
197 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
198 (categorycode, maxissueqty)
200 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
202 WHERE categorycode = ?");
203 $sth_search->execute($branch);
204 my $res = $sth_search->fetchrow_hashref();
206 $sth_update->execute($maxissueqty, $categorycode);
208 $sth_insert->execute($categorycode, $maxissueqty);
211 } elsif ($categorycode eq "*") {
212 my $sth_search = $dbh->prepare("SELECT count(*) AS total
213 FROM default_branch_circ_rules
214 WHERE branchcode = ?");
215 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
216 (branchcode, maxissueqty)
218 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
220 WHERE branchcode = ?");
221 $sth_search->execute($branch);
222 my $res = $sth_search->fetchrow_hashref();
224 $sth_update->execute($maxissueqty, $branch);
226 $sth_insert->execute($branch, $maxissueqty);
229 my $sth_search = $dbh->prepare("SELECT count(*) AS total
230 FROM branch_borrower_circ_rules
232 AND categorycode = ?");
233 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
234 (branchcode, categorycode, maxissueqty)
236 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
239 AND categorycode = ?");
241 $sth_search->execute($branch, $categorycode);
242 my $res = $sth_search->fetchrow_hashref();
244 $sth_update->execute($maxissueqty, $branch, $categorycode);
246 $sth_insert->execute($branch, $categorycode, $maxissueqty);
250 elsif ($op eq "add-branch-item") {
251 my $itemtype = $input->param('itemtype');
252 my $holdallowed = $input->param('holdallowed');
253 $holdallowed =~ s/\s//g;
254 $holdallowed = undef if $holdallowed !~ /^\d+/;
256 if ($branch eq "*") {
257 if ($itemtype eq "*") {
258 my $sth_search = $dbh->prepare("SELECT count(*) AS total
259 FROM default_circ_rules");
260 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
263 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
264 SET holdallowed = ?");
266 $sth_search->execute();
267 my $res = $sth_search->fetchrow_hashref();
269 $sth_update->execute($holdallowed);
271 $sth_insert->execute($holdallowed);
274 my $sth_search = $dbh->prepare("SELECT count(*) AS total
275 FROM default_branch_item_rules
276 WHERE itemtype = ?");
277 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
278 (itemtype, holdallowed)
280 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
282 WHERE itemtype = ?");
283 $sth_search->execute($itemtype);
284 my $res = $sth_search->fetchrow_hashref();
286 $sth_update->execute($holdallowed, $itemtype);
288 $sth_insert->execute($itemtype, $holdallowed);
291 } elsif ($itemtype eq "*") {
292 my $sth_search = $dbh->prepare("SELECT count(*) AS total
293 FROM default_branch_circ_rules
294 WHERE branchcode = ?");
295 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
296 (branchcode, holdallowed)
298 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
300 WHERE branchcode = ?");
301 $sth_search->execute($branch);
302 my $res = $sth_search->fetchrow_hashref();
304 $sth_update->execute($holdallowed, $branch);
306 $sth_insert->execute($branch, $holdallowed);
309 my $sth_search = $dbh->prepare("SELECT count(*) AS total
310 FROM branch_item_rules
313 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
314 (branchcode, itemtype, holdallowed)
316 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
321 $sth_search->execute($branch, $itemtype);
322 my $res = $sth_search->fetchrow_hashref();
324 $sth_update->execute($holdallowed, $branch, $itemtype);
326 $sth_insert->execute($branch, $itemtype, $holdallowed);
331 my $branches = GetBranches();
333 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
334 my $selected = 1 if $thisbranch eq $branch;
335 my %row =(value => $thisbranch,
336 selected => $selected,
337 branchname => $branches->{$thisbranch}->{'branchname'},
339 push @branchloop, \%row;
342 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
345 while (my $data=$sth->fetchrow_hashref){
346 push @category_loop,$data;
350 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
355 while (my $row=$sth->fetchrow_hashref){
356 push @itemtypes,$row;
359 my $sth2 = $dbh->prepare("
360 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
363 ON (itemtypes.itemtype = issuingrules.itemtype)
365 ON (categories.categorycode = issuingrules.categorycode)
366 WHERE issuingrules.branchcode = ?
368 $sth2->execute($branch);
370 while (my $row = $sth2->fetchrow_hashref) {
371 $row->{'humanitemtype'} ||= $row->{'itemtype'};
372 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
373 $row->{'humancategorycode'} ||= $row->{'categorycode'};
374 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
375 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
376 push @row_loop, $row;
380 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
383 if ($branch eq "*") {
384 $sth_branch_cat = $dbh->prepare("
385 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
386 FROM default_borrower_circ_rules
387 JOIN categories USING (categorycode)
390 $sth_branch_cat->execute();
392 $sth_branch_cat = $dbh->prepare("
393 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
394 FROM branch_borrower_circ_rules
395 JOIN categories USING (categorycode)
396 WHERE branch_borrower_circ_rules.branchcode = ?
398 $sth_branch_cat->execute($branch);
401 my @branch_cat_rules = ();
402 while (my $row = $sth_branch_cat->fetchrow_hashref) {
403 push @branch_cat_rules, $row;
405 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
407 # note undef maxissueqty so that template can deal with them
408 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
409 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
412 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
415 if ($branch eq "*") {
416 $sth_branch_item = $dbh->prepare("
417 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
418 FROM default_branch_item_rules
419 JOIN itemtypes USING (itemtype)
421 $sth_branch_item->execute();
423 $sth_branch_item = $dbh->prepare("
424 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
425 FROM branch_item_rules
426 JOIN itemtypes USING (itemtype)
427 WHERE branch_item_rules.branchcode = ?
429 $sth_branch_item->execute($branch);
432 my @branch_item_rules = ();
433 while (my $row = $sth_branch_item->fetchrow_hashref) {
434 push @branch_item_rules, $row;
436 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
438 # note undef holdallowed so that template can deal with them
439 foreach my $entry (@sorted_branch_item_rules) {
440 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
441 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
444 $template->param(show_branch_cat_rule_form => 1);
445 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
446 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
449 if ($branch eq "*") {
450 $sth_defaults = $dbh->prepare("
452 FROM default_circ_rules
454 $sth_defaults->execute();
456 $sth_defaults = $dbh->prepare("
458 FROM default_branch_circ_rules
461 $sth_defaults->execute($branch);
464 my $defaults = $sth_defaults->fetchrow_hashref;
467 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
468 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
469 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
470 $template->param(default_maxissueqty => $defaults->{maxissueqty});
473 $template->param(default_rules => ($defaults ? 1 : 0));
475 $template->param(categoryloop => \@category_loop,
476 itemtypeloop => \@itemtypes,
477 rules => \@sorted_row_loop,
478 branchloop => \@branchloop,
479 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
482 output_html_with_http_headers $input, $cookie, $template->output;
486 # sort by patron category, then item type, putting
487 # default entries at the bottom
488 sub by_category_and_itemtype {
489 unless (by_category($a, $b)) {
490 return by_itemtype($a, $b);
496 if ($a->{'default_humancategorycode'}) {
497 return ($b->{'default_humancategorycode'} ? 0 : 1);
498 } elsif ($b->{'default_humancategorycode'}) {
501 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
507 if ($a->{'default_humanitemtype'}) {
508 return ($b->{'default_humanitemtype'} ? 0 : 1);
509 } elsif ($b->{'default_humanitemtype'}) {
512 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};