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, issuelength, fine, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?)");
103 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, 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 $firstremind = $input->param('firstremind');
110 my $chargeperiod = $input->param('chargeperiod');
111 my $maxissueqty = $input->param('maxissueqty');
112 $maxissueqty =~ s/\s//g;
113 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
114 my $issuelength = $input->param('issuelength');
115 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
117 $sth_search->execute($br,$bor,$cat);
118 my $res = $sth_search->fetchrow_hashref();
120 $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
122 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
125 elsif ($op eq "set-branch-defaults") {
126 my $categorycode = $input->param('categorycode');
127 my $maxissueqty = $input->param('maxissueqty');
128 my $holdallowed = $input->param('holdallowed');
129 $maxissueqty =~ s/\s//g;
130 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
131 $holdallowed =~ s/\s//g;
132 $holdallowed = undef if $holdallowed !~ /^\d+/;
134 if ($branch eq "*") {
135 my $sth_search = $dbh->prepare("SELECT count(*) AS total
136 FROM default_circ_rules");
137 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
138 (maxissueqty, holdallowed)
140 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
141 SET maxissueqty = ?, holdallowed = ?");
143 $sth_search->execute();
144 my $res = $sth_search->fetchrow_hashref();
146 $sth_update->execute($maxissueqty, $holdallowed);
148 $sth_insert->execute($maxissueqty, $holdallowed);
151 my $sth_search = $dbh->prepare("SELECT count(*) AS total
152 FROM default_branch_circ_rules
153 WHERE branchcode = ?");
154 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
155 (branchcode, maxissueqty, holdallowed)
157 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
158 SET maxissueqty = ?, holdallowed = ?
159 WHERE branchcode = ?");
160 $sth_search->execute($branch);
161 my $res = $sth_search->fetchrow_hashref();
163 $sth_update->execute($maxissueqty, $holdallowed, $branch);
165 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
169 elsif ($op eq "add-branch-cat") {
170 my $categorycode = $input->param('categorycode');
171 my $maxissueqty = $input->param('maxissueqty');
172 $maxissueqty =~ s/\s//g;
173 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
175 if ($branch eq "*") {
176 if ($categorycode eq "*") {
177 my $sth_search = $dbh->prepare("SELECT count(*) AS total
178 FROM default_circ_rules");
179 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
182 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
183 SET maxissueqty = ?");
185 $sth_search->execute();
186 my $res = $sth_search->fetchrow_hashref();
188 $sth_update->execute($maxissueqty);
190 $sth_insert->execute($maxissueqty);
193 my $sth_search = $dbh->prepare("SELECT count(*) AS total
194 FROM default_borrower_circ_rules
195 WHERE categorycode = ?");
196 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
197 (categorycode, maxissueqty)
199 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
201 WHERE categorycode = ?");
202 $sth_search->execute($branch);
203 my $res = $sth_search->fetchrow_hashref();
205 $sth_update->execute($maxissueqty, $categorycode);
207 $sth_insert->execute($categorycode, $maxissueqty);
210 } elsif ($categorycode eq "*") {
211 my $sth_search = $dbh->prepare("SELECT count(*) AS total
212 FROM default_branch_circ_rules
213 WHERE branchcode = ?");
214 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
215 (branchcode, maxissueqty)
217 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
219 WHERE branchcode = ?");
220 $sth_search->execute($branch);
221 my $res = $sth_search->fetchrow_hashref();
223 $sth_update->execute($maxissueqty, $branch);
225 $sth_insert->execute($branch, $maxissueqty);
228 my $sth_search = $dbh->prepare("SELECT count(*) AS total
229 FROM branch_borrower_circ_rules
231 AND categorycode = ?");
232 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
233 (branchcode, categorycode, maxissueqty)
235 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
238 AND categorycode = ?");
240 $sth_search->execute($branch, $categorycode);
241 my $res = $sth_search->fetchrow_hashref();
243 $sth_update->execute($maxissueqty, $branch, $categorycode);
245 $sth_insert->execute($branch, $categorycode, $maxissueqty);
249 elsif ($op eq "add-branch-item") {
250 my $itemtype = $input->param('itemtype');
251 my $holdallowed = $input->param('holdallowed');
252 $holdallowed =~ s/\s//g;
253 $holdallowed = undef if $holdallowed !~ /^\d+/;
255 if ($branch eq "*") {
256 if ($itemtype eq "*") {
257 my $sth_search = $dbh->prepare("SELECT count(*) AS total
258 FROM default_circ_rules");
259 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
262 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
263 SET holdallowed = ?");
265 $sth_search->execute();
266 my $res = $sth_search->fetchrow_hashref();
268 $sth_update->execute($holdallowed);
270 $sth_insert->execute($holdallowed);
273 my $sth_search = $dbh->prepare("SELECT count(*) AS total
274 FROM default_branch_item_rules
275 WHERE itemtype = ?");
276 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
277 (itemtype, holdallowed)
279 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
281 WHERE itemtype = ?");
282 $sth_search->execute($itemtype);
283 my $res = $sth_search->fetchrow_hashref();
285 $sth_update->execute($holdallowed, $itemtype);
287 $sth_insert->execute($itemtype, $holdallowed);
290 } elsif ($itemtype eq "*") {
291 my $sth_search = $dbh->prepare("SELECT count(*) AS total
292 FROM default_branch_circ_rules
293 WHERE branchcode = ?");
294 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
295 (branchcode, holdallowed)
297 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
299 WHERE branchcode = ?");
300 $sth_search->execute($branch);
301 my $res = $sth_search->fetchrow_hashref();
303 $sth_update->execute($holdallowed, $branch);
305 $sth_insert->execute($branch, $holdallowed);
308 my $sth_search = $dbh->prepare("SELECT count(*) AS total
309 FROM branch_item_rules
312 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
313 (branchcode, itemtype, holdallowed)
315 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
320 $sth_search->execute($branch, $itemtype);
321 my $res = $sth_search->fetchrow_hashref();
323 $sth_update->execute($holdallowed, $branch, $itemtype);
325 $sth_insert->execute($branch, $itemtype, $holdallowed);
330 my $branches = GetBranches
();
332 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
333 my $selected = 1 if $thisbranch eq $branch;
334 my %row =(value
=> $thisbranch,
335 selected
=> $selected,
336 branchname
=> $branches->{$thisbranch}->{'branchname'},
338 push @branchloop, \
%row;
341 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
344 while (my $data=$sth->fetchrow_hashref){
345 push @category_loop,$data;
349 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
354 while (my $row=$sth->fetchrow_hashref){
355 push @itemtypes,$row;
358 my $sth2 = $dbh->prepare("
359 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
362 ON (itemtypes.itemtype = issuingrules.itemtype)
364 ON (categories.categorycode = issuingrules.categorycode)
365 WHERE issuingrules.branchcode = ?
367 $sth2->execute($branch);
369 while (my $row = $sth2->fetchrow_hashref) {
370 $row->{'humanitemtype'} ||= $row->{'itemtype'};
371 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
372 $row->{'humancategorycode'} ||= $row->{'categorycode'};
373 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
374 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
375 push @row_loop, $row;
379 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
382 if ($branch eq "*") {
383 $sth_branch_cat = $dbh->prepare("
384 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
385 FROM default_borrower_circ_rules
386 JOIN categories USING (categorycode)
389 $sth_branch_cat->execute();
391 $sth_branch_cat = $dbh->prepare("
392 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
393 FROM branch_borrower_circ_rules
394 JOIN categories USING (categorycode)
395 WHERE branch_borrower_circ_rules.branchcode = ?
397 $sth_branch_cat->execute($branch);
400 my @branch_cat_rules = ();
401 while (my $row = $sth_branch_cat->fetchrow_hashref) {
402 push @branch_cat_rules, $row;
404 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
406 # note undef maxissueqty so that template can deal with them
407 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
408 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
411 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
414 if ($branch eq "*") {
415 $sth_branch_item = $dbh->prepare("
416 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
417 FROM default_branch_item_rules
418 JOIN itemtypes USING (itemtype)
420 $sth_branch_item->execute();
422 $sth_branch_item = $dbh->prepare("
423 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
424 FROM branch_item_rules
425 JOIN itemtypes USING (itemtype)
426 WHERE branch_item_rules.branchcode = ?
428 $sth_branch_item->execute($branch);
431 my @branch_item_rules = ();
432 while (my $row = $sth_branch_item->fetchrow_hashref) {
433 push @branch_item_rules, $row;
435 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
437 # note undef holdallowed so that template can deal with them
438 foreach my $entry (@sorted_branch_item_rules) {
439 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
440 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
443 $template->param(show_branch_cat_rule_form
=> 1);
444 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
445 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
448 if ($branch eq "*") {
449 $sth_defaults = $dbh->prepare("
451 FROM default_circ_rules
453 $sth_defaults->execute();
455 $sth_defaults = $dbh->prepare("
457 FROM default_branch_circ_rules
460 $sth_defaults->execute($branch);
463 my $defaults = $sth_defaults->fetchrow_hashref;
466 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
467 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
468 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
469 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
472 $template->param(default_rules
=> ($defaults ?
1 : 0));
474 $template->param(categoryloop
=> \
@category_loop,
475 itemtypeloop
=> \
@itemtypes,
476 rules
=> \
@sorted_row_loop,
477 branchloop
=> \
@branchloop,
478 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
481 output_html_with_http_headers
$input, $cookie, $template->output;
485 # sort by patron category, then item type, putting
486 # default entries at the bottom
487 sub by_category_and_itemtype
{
488 unless (by_category
($a, $b)) {
489 return by_itemtype
($a, $b);
495 if ($a->{'default_humancategorycode'}) {
496 return ($b->{'default_humancategorycode'} ?
0 : 1);
497 } elsif ($b->{'default_humancategorycode'}) {
500 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
506 if ($a->{'default_humanitemtype'}) {
507 return ($b->{'default_humanitemtype'} ?
0 : 1);
508 } elsif ($b->{'default_humanitemtype'}) {
511 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};