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;
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) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
103 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, 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 my $reservesallowed = $input->param('reservesallowed');
115 $maxissueqty =~ s/\s//g;
116 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
117 my $issuelength = $input->param('issuelength');
118 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
120 $sth_search->execute($br,$bor,$cat);
121 my $res = $sth_search->fetchrow_hashref();
123 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$br,$bor,$cat);
125 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$fine,$finedays,$firstremind,$chargeperiod);
128 elsif ($op eq "set-branch-defaults") {
129 my $categorycode = $input->param('categorycode');
130 my $maxissueqty = $input->param('maxissueqty');
131 my $holdallowed = $input->param('holdallowed');
132 $maxissueqty =~ s/\s//g;
133 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
134 $holdallowed =~ s/\s//g;
135 $holdallowed = undef if $holdallowed !~ /^\d+/;
137 if ($branch eq "*") {
138 my $sth_search = $dbh->prepare("SELECT count(*) AS total
139 FROM default_circ_rules");
140 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
141 (maxissueqty, holdallowed)
143 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
144 SET maxissueqty = ?, holdallowed = ?");
146 $sth_search->execute();
147 my $res = $sth_search->fetchrow_hashref();
149 $sth_update->execute($maxissueqty, $holdallowed);
151 $sth_insert->execute($maxissueqty, $holdallowed);
154 my $sth_search = $dbh->prepare("SELECT count(*) AS total
155 FROM default_branch_circ_rules
156 WHERE branchcode = ?");
157 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
158 (branchcode, maxissueqty, holdallowed)
160 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
161 SET maxissueqty = ?, holdallowed = ?
162 WHERE branchcode = ?");
163 $sth_search->execute($branch);
164 my $res = $sth_search->fetchrow_hashref();
166 $sth_update->execute($maxissueqty, $holdallowed, $branch);
168 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
172 elsif ($op eq "add-branch-cat") {
173 my $categorycode = $input->param('categorycode');
174 my $maxissueqty = $input->param('maxissueqty');
175 $maxissueqty =~ s/\s//g;
176 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
178 if ($branch eq "*") {
179 if ($categorycode eq "*") {
180 my $sth_search = $dbh->prepare("SELECT count(*) AS total
181 FROM default_circ_rules");
182 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
185 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
186 SET maxissueqty = ?");
188 $sth_search->execute();
189 my $res = $sth_search->fetchrow_hashref();
191 $sth_update->execute($maxissueqty);
193 $sth_insert->execute($maxissueqty);
196 my $sth_search = $dbh->prepare("SELECT count(*) AS total
197 FROM default_borrower_circ_rules
198 WHERE categorycode = ?");
199 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
200 (categorycode, maxissueqty)
202 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
204 WHERE categorycode = ?");
205 $sth_search->execute($branch);
206 my $res = $sth_search->fetchrow_hashref();
208 $sth_update->execute($maxissueqty, $categorycode);
210 $sth_insert->execute($categorycode, $maxissueqty);
213 } elsif ($categorycode eq "*") {
214 my $sth_search = $dbh->prepare("SELECT count(*) AS total
215 FROM default_branch_circ_rules
216 WHERE branchcode = ?");
217 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
218 (branchcode, maxissueqty)
220 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
222 WHERE branchcode = ?");
223 $sth_search->execute($branch);
224 my $res = $sth_search->fetchrow_hashref();
226 $sth_update->execute($maxissueqty, $branch);
228 $sth_insert->execute($branch, $maxissueqty);
231 my $sth_search = $dbh->prepare("SELECT count(*) AS total
232 FROM branch_borrower_circ_rules
234 AND categorycode = ?");
235 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
236 (branchcode, categorycode, maxissueqty)
238 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
241 AND categorycode = ?");
243 $sth_search->execute($branch, $categorycode);
244 my $res = $sth_search->fetchrow_hashref();
246 $sth_update->execute($maxissueqty, $branch, $categorycode);
248 $sth_insert->execute($branch, $categorycode, $maxissueqty);
252 elsif ($op eq "add-branch-item") {
253 my $itemtype = $input->param('itemtype');
254 my $holdallowed = $input->param('holdallowed');
255 $holdallowed =~ s/\s//g;
256 $holdallowed = undef if $holdallowed !~ /^\d+/;
258 if ($branch eq "*") {
259 if ($itemtype eq "*") {
260 my $sth_search = $dbh->prepare("SELECT count(*) AS total
261 FROM default_circ_rules");
262 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
265 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
266 SET holdallowed = ?");
268 $sth_search->execute();
269 my $res = $sth_search->fetchrow_hashref();
271 $sth_update->execute($holdallowed);
273 $sth_insert->execute($holdallowed);
276 my $sth_search = $dbh->prepare("SELECT count(*) AS total
277 FROM default_branch_item_rules
278 WHERE itemtype = ?");
279 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
280 (itemtype, holdallowed)
282 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
284 WHERE itemtype = ?");
285 $sth_search->execute($itemtype);
286 my $res = $sth_search->fetchrow_hashref();
288 $sth_update->execute($holdallowed, $itemtype);
290 $sth_insert->execute($itemtype, $holdallowed);
293 } elsif ($itemtype eq "*") {
294 my $sth_search = $dbh->prepare("SELECT count(*) AS total
295 FROM default_branch_circ_rules
296 WHERE branchcode = ?");
297 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
298 (branchcode, holdallowed)
300 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
302 WHERE branchcode = ?");
303 $sth_search->execute($branch);
304 my $res = $sth_search->fetchrow_hashref();
306 $sth_update->execute($holdallowed, $branch);
308 $sth_insert->execute($branch, $holdallowed);
311 my $sth_search = $dbh->prepare("SELECT count(*) AS total
312 FROM branch_item_rules
315 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
316 (branchcode, itemtype, holdallowed)
318 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
323 $sth_search->execute($branch, $itemtype);
324 my $res = $sth_search->fetchrow_hashref();
326 $sth_update->execute($holdallowed, $branch, $itemtype);
328 $sth_insert->execute($branch, $itemtype, $holdallowed);
333 my $branches = GetBranches();
335 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
336 my $selected = 1 if $thisbranch eq $branch;
337 my %row =(value => $thisbranch,
338 selected => $selected,
339 branchname => $branches->{$thisbranch}->{'branchname'},
341 push @branchloop, \%row;
344 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
347 while (my $data=$sth->fetchrow_hashref){
348 push @category_loop,$data;
352 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
357 while (my $row=$sth->fetchrow_hashref){
358 push @itemtypes,$row;
361 my $sth2 = $dbh->prepare("
362 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
365 ON (itemtypes.itemtype = issuingrules.itemtype)
367 ON (categories.categorycode = issuingrules.categorycode)
368 WHERE issuingrules.branchcode = ?
370 $sth2->execute($branch);
372 while (my $row = $sth2->fetchrow_hashref) {
373 $row->{'humanitemtype'} ||= $row->{'itemtype'};
374 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
375 $row->{'humancategorycode'} ||= $row->{'categorycode'};
376 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
377 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
378 push @row_loop, $row;
382 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
385 if ($branch eq "*") {
386 $sth_branch_cat = $dbh->prepare("
387 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
388 FROM default_borrower_circ_rules
389 JOIN categories USING (categorycode)
392 $sth_branch_cat->execute();
394 $sth_branch_cat = $dbh->prepare("
395 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
396 FROM branch_borrower_circ_rules
397 JOIN categories USING (categorycode)
398 WHERE branch_borrower_circ_rules.branchcode = ?
400 $sth_branch_cat->execute($branch);
403 my @branch_cat_rules = ();
404 while (my $row = $sth_branch_cat->fetchrow_hashref) {
405 push @branch_cat_rules, $row;
407 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
409 # note undef maxissueqty so that template can deal with them
410 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
411 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
414 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
417 if ($branch eq "*") {
418 $sth_branch_item = $dbh->prepare("
419 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
420 FROM default_branch_item_rules
421 JOIN itemtypes USING (itemtype)
423 $sth_branch_item->execute();
425 $sth_branch_item = $dbh->prepare("
426 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
427 FROM branch_item_rules
428 JOIN itemtypes USING (itemtype)
429 WHERE branch_item_rules.branchcode = ?
431 $sth_branch_item->execute($branch);
434 my @branch_item_rules = ();
435 while (my $row = $sth_branch_item->fetchrow_hashref) {
436 push @branch_item_rules, $row;
438 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
440 # note undef holdallowed so that template can deal with them
441 foreach my $entry (@sorted_branch_item_rules) {
442 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
443 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
446 $template->param(show_branch_cat_rule_form => 1);
447 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
448 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
451 if ($branch eq "*") {
452 $sth_defaults = $dbh->prepare("
454 FROM default_circ_rules
456 $sth_defaults->execute();
458 $sth_defaults = $dbh->prepare("
460 FROM default_branch_circ_rules
463 $sth_defaults->execute($branch);
466 my $defaults = $sth_defaults->fetchrow_hashref;
469 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
470 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
471 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
472 $template->param(default_maxissueqty => $defaults->{maxissueqty});
475 $template->param(default_rules => ($defaults ? 1 : 0));
477 $template->param(categoryloop => \@category_loop,
478 itemtypeloop => \@itemtypes,
479 rules => \@sorted_row_loop,
480 branchloop => \@branchloop,
481 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
483 definedbranch => scalar(@sorted_row_loop)>0
485 output_html_with_http_headers $input, $cookie, $template->output;
489 # sort by patron category, then item type, putting
490 # default entries at the bottom
491 sub by_category_and_itemtype {
492 unless (by_category($a, $b)) {
493 return by_itemtype($a, $b);
499 if ($a->{'default_humancategorycode'}) {
500 return ($b->{'default_humancategorycode'} ? 0 : 1);
501 } elsif ($b->{'default_humancategorycode'}) {
504 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
510 if ($a->{'default_humanitemtype'}) {
511 return ($b->{'default_humanitemtype'} ? 0 : 1);
512 } elsif ($b->{'default_humanitemtype'}) {
515 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};