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, renewalsallowed, issuelength, fine, finedays, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?)");
103 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, 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 my $renewalsallowed = $input->param('renewalsallowed');
114 $maxissueqty =~ s/\s//g;
115 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
116 my $issuelength = $input->param('issuelength');
117 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
119 $sth_search->execute($br,$bor,$cat);
120 my $res = $sth_search->fetchrow_hashref();
122 $sth_update->execute($fine, $finedays, $firstremind, $chargeperiod, $maxissueqty,$renewalsallowed,$issuelength,$br,$bor,$cat);
124 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$issuelength,$fine, $finedays,$firstremind,$chargeperiod);
127 elsif ($op eq "set-branch-defaults") {
128 my $categorycode = $input->param('categorycode');
129 my $maxissueqty = $input->param('maxissueqty');
130 my $holdallowed = $input->param('holdallowed');
131 $maxissueqty =~ s/\s//g;
132 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
133 $holdallowed =~ s/\s//g;
134 $holdallowed = undef if $holdallowed !~ /^\d+/;
136 if ($branch eq "*") {
137 my $sth_search = $dbh->prepare("SELECT count(*) AS total
138 FROM default_circ_rules");
139 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
140 (maxissueqty, holdallowed)
142 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
143 SET maxissueqty = ?, holdallowed = ?");
145 $sth_search->execute();
146 my $res = $sth_search->fetchrow_hashref();
148 $sth_update->execute($maxissueqty, $holdallowed);
150 $sth_insert->execute($maxissueqty, $holdallowed);
153 my $sth_search = $dbh->prepare("SELECT count(*) AS total
154 FROM default_branch_circ_rules
155 WHERE branchcode = ?");
156 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
157 (branchcode, maxissueqty, holdallowed)
159 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
160 SET maxissueqty = ?, holdallowed = ?
161 WHERE branchcode = ?");
162 $sth_search->execute($branch);
163 my $res = $sth_search->fetchrow_hashref();
165 $sth_update->execute($maxissueqty, $holdallowed, $branch);
167 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
171 elsif ($op eq "add-branch-cat") {
172 my $categorycode = $input->param('categorycode');
173 my $maxissueqty = $input->param('maxissueqty');
174 $maxissueqty =~ s/\s//g;
175 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
177 if ($branch eq "*") {
178 if ($categorycode eq "*") {
179 my $sth_search = $dbh->prepare("SELECT count(*) AS total
180 FROM default_circ_rules");
181 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
184 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
185 SET maxissueqty = ?");
187 $sth_search->execute();
188 my $res = $sth_search->fetchrow_hashref();
190 $sth_update->execute($maxissueqty);
192 $sth_insert->execute($maxissueqty);
195 my $sth_search = $dbh->prepare("SELECT count(*) AS total
196 FROM default_borrower_circ_rules
197 WHERE categorycode = ?");
198 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
199 (categorycode, maxissueqty)
201 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
203 WHERE categorycode = ?");
204 $sth_search->execute($branch);
205 my $res = $sth_search->fetchrow_hashref();
207 $sth_update->execute($maxissueqty, $categorycode);
209 $sth_insert->execute($categorycode, $maxissueqty);
212 } elsif ($categorycode eq "*") {
213 my $sth_search = $dbh->prepare("SELECT count(*) AS total
214 FROM default_branch_circ_rules
215 WHERE branchcode = ?");
216 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
217 (branchcode, maxissueqty)
219 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
221 WHERE branchcode = ?");
222 $sth_search->execute($branch);
223 my $res = $sth_search->fetchrow_hashref();
225 $sth_update->execute($maxissueqty, $branch);
227 $sth_insert->execute($branch, $maxissueqty);
230 my $sth_search = $dbh->prepare("SELECT count(*) AS total
231 FROM branch_borrower_circ_rules
233 AND categorycode = ?");
234 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
235 (branchcode, categorycode, maxissueqty)
237 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
240 AND categorycode = ?");
242 $sth_search->execute($branch, $categorycode);
243 my $res = $sth_search->fetchrow_hashref();
245 $sth_update->execute($maxissueqty, $branch, $categorycode);
247 $sth_insert->execute($branch, $categorycode, $maxissueqty);
251 elsif ($op eq "add-branch-item") {
252 my $itemtype = $input->param('itemtype');
253 my $holdallowed = $input->param('holdallowed');
254 $holdallowed =~ s/\s//g;
255 $holdallowed = undef if $holdallowed !~ /^\d+/;
257 if ($branch eq "*") {
258 if ($itemtype eq "*") {
259 my $sth_search = $dbh->prepare("SELECT count(*) AS total
260 FROM default_circ_rules");
261 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
264 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
265 SET holdallowed = ?");
267 $sth_search->execute();
268 my $res = $sth_search->fetchrow_hashref();
270 $sth_update->execute($holdallowed);
272 $sth_insert->execute($holdallowed);
275 my $sth_search = $dbh->prepare("SELECT count(*) AS total
276 FROM default_branch_item_rules
277 WHERE itemtype = ?");
278 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
279 (itemtype, holdallowed)
281 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
283 WHERE itemtype = ?");
284 $sth_search->execute($itemtype);
285 my $res = $sth_search->fetchrow_hashref();
287 $sth_update->execute($holdallowed, $itemtype);
289 $sth_insert->execute($itemtype, $holdallowed);
292 } elsif ($itemtype eq "*") {
293 my $sth_search = $dbh->prepare("SELECT count(*) AS total
294 FROM default_branch_circ_rules
295 WHERE branchcode = ?");
296 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
297 (branchcode, holdallowed)
299 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
301 WHERE branchcode = ?");
302 $sth_search->execute($branch);
303 my $res = $sth_search->fetchrow_hashref();
305 $sth_update->execute($holdallowed, $branch);
307 $sth_insert->execute($branch, $holdallowed);
310 my $sth_search = $dbh->prepare("SELECT count(*) AS total
311 FROM branch_item_rules
314 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
315 (branchcode, itemtype, holdallowed)
317 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
322 $sth_search->execute($branch, $itemtype);
323 my $res = $sth_search->fetchrow_hashref();
325 $sth_update->execute($holdallowed, $branch, $itemtype);
327 $sth_insert->execute($branch, $itemtype, $holdallowed);
332 my $branches = GetBranches();
334 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
335 my $selected = 1 if $thisbranch eq $branch;
336 my %row =(value => $thisbranch,
337 selected => $selected,
338 branchname => $branches->{$thisbranch}->{'branchname'},
340 push @branchloop, \%row;
343 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
346 while (my $data=$sth->fetchrow_hashref){
347 push @category_loop,$data;
351 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
356 while (my $row=$sth->fetchrow_hashref){
357 push @itemtypes,$row;
360 my $sth2 = $dbh->prepare("
361 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
364 ON (itemtypes.itemtype = issuingrules.itemtype)
366 ON (categories.categorycode = issuingrules.categorycode)
367 WHERE issuingrules.branchcode = ?
369 $sth2->execute($branch);
371 while (my $row = $sth2->fetchrow_hashref) {
372 $row->{'humanitemtype'} ||= $row->{'itemtype'};
373 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
374 $row->{'humancategorycode'} ||= $row->{'categorycode'};
375 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
376 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
377 push @row_loop, $row;
381 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
384 if ($branch eq "*") {
385 $sth_branch_cat = $dbh->prepare("
386 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
387 FROM default_borrower_circ_rules
388 JOIN categories USING (categorycode)
391 $sth_branch_cat->execute();
393 $sth_branch_cat = $dbh->prepare("
394 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
395 FROM branch_borrower_circ_rules
396 JOIN categories USING (categorycode)
397 WHERE branch_borrower_circ_rules.branchcode = ?
399 $sth_branch_cat->execute($branch);
402 my @branch_cat_rules = ();
403 while (my $row = $sth_branch_cat->fetchrow_hashref) {
404 push @branch_cat_rules, $row;
406 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
408 # note undef maxissueqty so that template can deal with them
409 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
410 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
413 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
416 if ($branch eq "*") {
417 $sth_branch_item = $dbh->prepare("
418 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
419 FROM default_branch_item_rules
420 JOIN itemtypes USING (itemtype)
422 $sth_branch_item->execute();
424 $sth_branch_item = $dbh->prepare("
425 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
426 FROM branch_item_rules
427 JOIN itemtypes USING (itemtype)
428 WHERE branch_item_rules.branchcode = ?
430 $sth_branch_item->execute($branch);
433 my @branch_item_rules = ();
434 while (my $row = $sth_branch_item->fetchrow_hashref) {
435 push @branch_item_rules, $row;
437 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
439 # note undef holdallowed so that template can deal with them
440 foreach my $entry (@sorted_branch_item_rules) {
441 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
442 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
445 $template->param(show_branch_cat_rule_form => 1);
446 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
447 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
450 if ($branch eq "*") {
451 $sth_defaults = $dbh->prepare("
453 FROM default_circ_rules
455 $sth_defaults->execute();
457 $sth_defaults = $dbh->prepare("
459 FROM default_branch_circ_rules
462 $sth_defaults->execute($branch);
465 my $defaults = $sth_defaults->fetchrow_hashref;
468 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
469 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
470 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
471 $template->param(default_maxissueqty => $defaults->{maxissueqty});
474 $template->param(default_rules => ($defaults ? 1 : 0));
476 $template->param(categoryloop => \@category_loop,
477 itemtypeloop => \@itemtypes,
478 rules => \@sorted_row_loop,
479 branchloop => \@branchloop,
480 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
483 output_html_with_http_headers $input, $cookie, $template->output;
487 # sort by patron category, then item type, putting
488 # default entries at the bottom
489 sub by_category_and_itemtype {
490 unless (by_category($a, $b)) {
491 return by_itemtype($a, $b);
497 if ($a->{'default_humancategorycode'}) {
498 return ($b->{'default_humancategorycode'} ? 0 : 1);
499 } elsif ($b->{'default_humancategorycode'}) {
502 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
508 if ($a->{'default_humanitemtype'}) {
509 return ($b->{'default_humanitemtype'} ? 0 : 1);
510 } elsif ($b->{'default_humanitemtype'}) {
513 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};