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.
21 #use warnings; FIXME - Bug 2505
28 use C4
::Branch
; # GetBranches
31 my $dbh = C4
::Context
->dbh;
34 # $flagsrequired->{circulation}=1;
35 my ($template, $loggedinuser, $cookie)
36 = get_template_and_user
({template_name
=> "admin/smart-rules.tmpl",
40 flagsrequired
=> {parameters
=> 1},
44 my $type=$input->param('type');
45 my $branch = $input->param('branch') || ( C4
::Branch
::onlymine
() ?
( C4
::Branch
::mybranch
() || '*' ) : '*' );
46 my $op = $input->param('op');
48 if ($op eq 'delete') {
49 my $itemtype = $input->param('itemtype');
50 my $categorycode = $input->param('categorycode');
51 $debug and warn "deleting $1 $2 $branch";
53 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
54 $sth_Idelete->execute($branch, $categorycode, $itemtype);
56 elsif ($op eq 'delete-branch-cat') {
57 my $categorycode = $input->param('categorycode');
59 if ($categorycode eq "*") {
60 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
61 $sth_delete->execute();
63 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
64 WHERE categorycode = ?");
65 $sth_delete->execute($categorycode);
67 } elsif ($categorycode eq "*") {
68 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
69 WHERE branchcode = ?");
70 $sth_delete->execute($branch);
72 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
74 AND categorycode = ?");
75 $sth_delete->execute($branch, $categorycode);
78 elsif ($op eq 'delete-branch-item') {
79 my $itemtype = $input->param('itemtype');
81 if ($itemtype eq "*") {
82 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
83 $sth_delete->execute();
85 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
87 $sth_delete->execute($itemtype);
89 } elsif ($itemtype eq "*") {
90 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
91 WHERE branchcode = ?");
92 $sth_delete->execute($branch);
94 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
97 $sth_delete->execute($branch, $itemtype);
100 # save the values entered
101 elsif ($op eq 'add') {
102 my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
103 my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, reservesallowed, issuelength, fine, finedays, firstremind, chargeperiod,rentaldiscount) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
104 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, issuelength=?, rentaldiscount=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
106 my $br = $branch; # branch
107 my $bor = $input->param('categorycode'); # borrower category
108 my $cat = $input->param('itemtype'); # item type
109 my $fine = $input->param('fine');
110 my $finedays = $input->param('finedays');
111 my $firstremind = $input->param('firstremind');
112 my $chargeperiod = $input->param('chargeperiod');
113 my $maxissueqty = $input->param('maxissueqty');
114 my $renewalsallowed = $input->param('renewalsallowed');
115 my $reservesallowed = $input->param('reservesallowed');
116 $maxissueqty =~ s/\s//g;
117 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
118 my $issuelength = $input->param('issuelength');
119 my $rentaldiscount = $input->param('rentaldiscount');
120 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
122 $sth_search->execute($br,$bor,$cat);
123 my $res = $sth_search->fetchrow_hashref();
125 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$rentaldiscount, $br,$bor,$cat);
127 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount);
130 elsif ($op eq "set-branch-defaults") {
131 my $categorycode = $input->param('categorycode');
132 my $maxissueqty = $input->param('maxissueqty');
133 my $holdallowed = $input->param('holdallowed');
134 $maxissueqty =~ s/\s//g;
135 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
136 $holdallowed =~ s/\s//g;
137 $holdallowed = undef if $holdallowed !~ /^\d+/;
139 if ($branch eq "*") {
140 my $sth_search = $dbh->prepare("SELECT count(*) AS total
141 FROM default_circ_rules");
142 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
143 (maxissueqty, holdallowed)
145 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
146 SET maxissueqty = ?, holdallowed = ?");
148 $sth_search->execute();
149 my $res = $sth_search->fetchrow_hashref();
151 $sth_update->execute($maxissueqty, $holdallowed);
153 $sth_insert->execute($maxissueqty, $holdallowed);
156 my $sth_search = $dbh->prepare("SELECT count(*) AS total
157 FROM default_branch_circ_rules
158 WHERE branchcode = ?");
159 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
160 (branchcode, maxissueqty, holdallowed)
162 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
163 SET maxissueqty = ?, holdallowed = ?
164 WHERE branchcode = ?");
165 $sth_search->execute($branch);
166 my $res = $sth_search->fetchrow_hashref();
168 $sth_update->execute($maxissueqty, $holdallowed, $branch);
170 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
174 elsif ($op eq "add-branch-cat") {
175 my $categorycode = $input->param('categorycode');
176 my $maxissueqty = $input->param('maxissueqty');
177 $maxissueqty =~ s/\s//g;
178 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
180 if ($branch eq "*") {
181 if ($categorycode eq "*") {
182 my $sth_search = $dbh->prepare("SELECT count(*) AS total
183 FROM default_circ_rules");
184 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
187 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
188 SET maxissueqty = ?");
190 $sth_search->execute();
191 my $res = $sth_search->fetchrow_hashref();
193 $sth_update->execute($maxissueqty);
195 $sth_insert->execute($maxissueqty);
198 my $sth_search = $dbh->prepare("SELECT count(*) AS total
199 FROM default_borrower_circ_rules
200 WHERE categorycode = ?");
201 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
202 (categorycode, maxissueqty)
204 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
206 WHERE categorycode = ?");
207 $sth_search->execute($branch);
208 my $res = $sth_search->fetchrow_hashref();
210 $sth_update->execute($maxissueqty, $categorycode);
212 $sth_insert->execute($categorycode, $maxissueqty);
215 } elsif ($categorycode eq "*") {
216 my $sth_search = $dbh->prepare("SELECT count(*) AS total
217 FROM default_branch_circ_rules
218 WHERE branchcode = ?");
219 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
220 (branchcode, maxissueqty)
222 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
224 WHERE branchcode = ?");
225 $sth_search->execute($branch);
226 my $res = $sth_search->fetchrow_hashref();
228 $sth_update->execute($maxissueqty, $branch);
230 $sth_insert->execute($branch, $maxissueqty);
233 my $sth_search = $dbh->prepare("SELECT count(*) AS total
234 FROM branch_borrower_circ_rules
236 AND categorycode = ?");
237 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
238 (branchcode, categorycode, maxissueqty)
240 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
243 AND categorycode = ?");
245 $sth_search->execute($branch, $categorycode);
246 my $res = $sth_search->fetchrow_hashref();
248 $sth_update->execute($maxissueqty, $branch, $categorycode);
250 $sth_insert->execute($branch, $categorycode, $maxissueqty);
254 elsif ($op eq "add-branch-item") {
255 my $itemtype = $input->param('itemtype');
256 my $holdallowed = $input->param('holdallowed');
257 $holdallowed =~ s/\s//g;
258 $holdallowed = undef if $holdallowed !~ /^\d+/;
260 if ($branch eq "*") {
261 if ($itemtype eq "*") {
262 my $sth_search = $dbh->prepare("SELECT count(*) AS total
263 FROM default_circ_rules");
264 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
267 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
268 SET holdallowed = ?");
270 $sth_search->execute();
271 my $res = $sth_search->fetchrow_hashref();
273 $sth_update->execute($holdallowed);
275 $sth_insert->execute($holdallowed);
278 my $sth_search = $dbh->prepare("SELECT count(*) AS total
279 FROM default_branch_item_rules
280 WHERE itemtype = ?");
281 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
282 (itemtype, holdallowed)
284 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
286 WHERE itemtype = ?");
287 $sth_search->execute($itemtype);
288 my $res = $sth_search->fetchrow_hashref();
290 $sth_update->execute($holdallowed, $itemtype);
292 $sth_insert->execute($itemtype, $holdallowed);
295 } elsif ($itemtype eq "*") {
296 my $sth_search = $dbh->prepare("SELECT count(*) AS total
297 FROM default_branch_circ_rules
298 WHERE branchcode = ?");
299 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
300 (branchcode, holdallowed)
302 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
304 WHERE branchcode = ?");
305 $sth_search->execute($branch);
306 my $res = $sth_search->fetchrow_hashref();
308 $sth_update->execute($holdallowed, $branch);
310 $sth_insert->execute($branch, $holdallowed);
313 my $sth_search = $dbh->prepare("SELECT count(*) AS total
314 FROM branch_item_rules
317 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
318 (branchcode, itemtype, holdallowed)
320 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
325 $sth_search->execute($branch, $itemtype);
326 my $res = $sth_search->fetchrow_hashref();
328 $sth_update->execute($holdallowed, $branch, $itemtype);
330 $sth_insert->execute($branch, $itemtype, $holdallowed);
335 my $branches = GetBranches
();
337 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
338 my $selected = 1 if $thisbranch eq $branch;
339 my %row =(value
=> $thisbranch,
340 selected
=> $selected,
341 branchname
=> $branches->{$thisbranch}->{'branchname'},
343 push @branchloop, \
%row;
346 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
349 while (my $data=$sth->fetchrow_hashref){
350 push @category_loop,$data;
354 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
359 while (my $row=$sth->fetchrow_hashref){
360 push @itemtypes,$row;
363 my $sth2 = $dbh->prepare("
364 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
367 ON (itemtypes.itemtype = issuingrules.itemtype)
369 ON (categories.categorycode = issuingrules.categorycode)
370 WHERE issuingrules.branchcode = ?
372 $sth2->execute($branch);
374 while (my $row = $sth2->fetchrow_hashref) {
375 $row->{'humanitemtype'} ||= $row->{'itemtype'};
376 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
377 $row->{'humancategorycode'} ||= $row->{'categorycode'};
378 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
379 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
380 push @row_loop, $row;
384 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
387 if ($branch eq "*") {
388 $sth_branch_cat = $dbh->prepare("
389 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
390 FROM default_borrower_circ_rules
391 JOIN categories USING (categorycode)
394 $sth_branch_cat->execute();
396 $sth_branch_cat = $dbh->prepare("
397 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
398 FROM branch_borrower_circ_rules
399 JOIN categories USING (categorycode)
400 WHERE branch_borrower_circ_rules.branchcode = ?
402 $sth_branch_cat->execute($branch);
405 my @branch_cat_rules = ();
406 while (my $row = $sth_branch_cat->fetchrow_hashref) {
407 push @branch_cat_rules, $row;
409 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
411 # note undef maxissueqty so that template can deal with them
412 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
413 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
416 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
419 if ($branch eq "*") {
420 $sth_branch_item = $dbh->prepare("
421 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
422 FROM default_branch_item_rules
423 JOIN itemtypes USING (itemtype)
425 $sth_branch_item->execute();
427 $sth_branch_item = $dbh->prepare("
428 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
429 FROM branch_item_rules
430 JOIN itemtypes USING (itemtype)
431 WHERE branch_item_rules.branchcode = ?
433 $sth_branch_item->execute($branch);
436 my @branch_item_rules = ();
437 while (my $row = $sth_branch_item->fetchrow_hashref) {
438 push @branch_item_rules, $row;
440 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
442 # note undef holdallowed so that template can deal with them
443 foreach my $entry (@sorted_branch_item_rules) {
444 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
445 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
448 $template->param(show_branch_cat_rule_form
=> 1);
449 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
450 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
453 if ($branch eq "*") {
454 $sth_defaults = $dbh->prepare("
456 FROM default_circ_rules
458 $sth_defaults->execute();
460 $sth_defaults = $dbh->prepare("
462 FROM default_branch_circ_rules
465 $sth_defaults->execute($branch);
468 my $defaults = $sth_defaults->fetchrow_hashref;
471 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
472 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
473 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
474 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
477 $template->param(default_rules
=> ($defaults ?
1 : 0));
479 $template->param(categoryloop
=> \
@category_loop,
480 itemtypeloop
=> \
@itemtypes,
481 rules
=> \
@sorted_row_loop,
482 branchloop
=> \
@branchloop,
483 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
485 definedbranch
=> scalar(@sorted_row_loop)>0
487 output_html_with_http_headers
$input, $cookie, $template->output;
491 # sort by patron category, then item type, putting
492 # default entries at the bottom
493 sub by_category_and_itemtype
{
494 unless (by_category
($a, $b)) {
495 return by_itemtype
($a, $b);
501 if ($a->{'default_humancategorycode'}) {
502 return ($b->{'default_humancategorycode'} ?
0 : 1);
503 } elsif ($b->{'default_humancategorycode'}) {
506 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
512 if ($a->{'default_humanitemtype'}) {
513 return ($b->{'default_humanitemtype'} ?
0 : 1);
514 } elsif ($b->{'default_humanitemtype'}) {
517 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};