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 # save the values entered
78 elsif ($op eq 'add') {
79 my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
80 my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, issuelength, fine, finedays, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?)");
81 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
83 my $br = $branch; # branch
84 my $bor = $input->param('categorycode'); # borrower category
85 my $cat = $input->param('itemtype'); # item type
86 my $fine = $input->param('fine');
87 my $finedays = $input->param('finedays');
88 my $firstremind = $input->param('firstremind');
89 my $chargeperiod = $input->param('chargeperiod');
90 my $maxissueqty = $input->param('maxissueqty');
91 $maxissueqty =~ s/\s//g;
92 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
93 my $issuelength = $input->param('issuelength');
94 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
96 $sth_search->execute($br,$bor,$cat);
97 my $res = $sth_search->fetchrow_hashref();
99 $sth_update->execute($fine, $finedays, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
101 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$finedays,$firstremind,$chargeperiod);
104 elsif ($op eq "add-branch-cat") {
105 my $categorycode = $input->param('categorycode');
106 my $maxissueqty = $input->param('maxissueqty');
107 $maxissueqty =~ s/\s//g;
108 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
110 if ($branch eq "*") {
111 if ($categorycode eq "*") {
112 my $sth_search = $dbh->prepare("SELECT count(*) AS total
113 FROM default_circ_rules");
114 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
117 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
118 SET maxissueqty = ?");
120 $sth_search->execute();
121 my $res = $sth_search->fetchrow_hashref();
123 $sth_update->execute($maxissueqty);
125 $sth_insert->execute($maxissueqty);
128 my $sth_search = $dbh->prepare("SELECT count(*) AS total
129 FROM default_borrower_circ_rules
130 WHERE categorycode = ?");
131 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
132 (categorycode, maxissueqty)
134 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
136 WHERE categorycode = ?");
137 $sth_search->execute($branch);
138 my $res = $sth_search->fetchrow_hashref();
140 $sth_update->execute($maxissueqty, $categorycode);
142 $sth_insert->execute($categorycode, $maxissueqty);
145 } elsif ($categorycode eq "*") {
146 my $sth_search = $dbh->prepare("SELECT count(*) AS total
147 FROM default_branch_circ_rules
148 WHERE branchcode = ?");
149 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
150 (branchcode, maxissueqty)
152 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
154 WHERE branchcode = ?");
155 $sth_search->execute($branch);
156 my $res = $sth_search->fetchrow_hashref();
158 $sth_update->execute($maxissueqty, $branch);
160 $sth_insert->execute($branch, $maxissueqty);
163 my $sth_search = $dbh->prepare("SELECT count(*) AS total
164 FROM branch_borrower_circ_rules
166 AND categorycode = ?");
167 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
168 (branchcode, categorycode, maxissueqty)
170 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
173 AND categorycode = ?");
175 $sth_search->execute($branch, $categorycode);
176 my $res = $sth_search->fetchrow_hashref();
178 $sth_update->execute($maxissueqty, $branch, $categorycode);
180 $sth_insert->execute($branch, $categorycode, $maxissueqty);
184 elsif ($op eq "add-branch-item") {
185 my $itemtype = $input->param('itemtype');
186 my $holdallowed = $input->param('holdallowed');
187 $holdallowed =~ s/\s//g;
188 $holdallowed = undef if $holdallowed !~ /^\d+/;
190 if ($branch eq "*") {
191 if ($itemtype eq "*") {
192 my $sth_search = $dbh->prepare("SELECT count(*) AS total
193 FROM default_circ_rules");
194 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
197 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
198 SET holdallowed = ?");
200 $sth_search->execute();
201 my $res = $sth_search->fetchrow_hashref();
203 $sth_update->execute($holdallowed);
205 $sth_insert->execute($holdallowed);
208 my $sth_search = $dbh->prepare("SELECT count(*) AS total
209 FROM default_branch_item_rules
210 WHERE itemtype = ?");
211 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
212 (itemtype, holdallowed)
214 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
216 WHERE itemtype = ?");
217 $sth_search->execute($itemtype);
218 my $res = $sth_search->fetchrow_hashref();
220 $sth_update->execute($holdallowed, $itemtype);
222 $sth_insert->execute($itemtype, $holdallowed);
225 } elsif ($itemtype eq "*") {
226 my $sth_search = $dbh->prepare("SELECT count(*) AS total
227 FROM default_branch_circ_rules
228 WHERE branchcode = ?");
229 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
230 (branchcode, holdallowed)
232 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
234 WHERE branchcode = ?");
235 $sth_search->execute($branch);
236 my $res = $sth_search->fetchrow_hashref();
238 $sth_update->execute($holdallowed, $branch);
240 $sth_insert->execute($branch, $holdallowed);
243 my $sth_search = $dbh->prepare("SELECT count(*) AS total
244 FROM branch_item_rules
247 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
248 (branchcode, itemtype, holdallowed)
250 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
255 $sth_search->execute($branch, $itemtype);
256 my $res = $sth_search->fetchrow_hashref();
258 $sth_update->execute($holdallowed, $branch, $itemtype);
260 $sth_insert->execute($branch, $itemtype, $holdallowed);
265 my $branches = GetBranches();
267 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
268 my $selected = 1 if $thisbranch eq $branch;
269 my %row =(value => $thisbranch,
270 selected => $selected,
271 branchname => $branches->{$thisbranch}->{'branchname'},
273 push @branchloop, \%row;
276 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
279 while (my $data=$sth->fetchrow_hashref){
280 push @category_loop,$data;
284 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
289 while (my $row=$sth->fetchrow_hashref){
290 push @itemtypes,$row;
293 my $sth2 = $dbh->prepare("
294 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
297 ON (itemtypes.itemtype = issuingrules.itemtype)
299 ON (categories.categorycode = issuingrules.categorycode)
300 WHERE issuingrules.branchcode = ?
302 $sth2->execute($branch);
304 while (my $row = $sth2->fetchrow_hashref) {
305 $row->{'humanitemtype'} ||= $row->{'itemtype'};
306 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
307 $row->{'humancategorycode'} ||= $row->{'categorycode'};
308 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
309 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
310 push @row_loop, $row;
314 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
317 if ($branch eq "*") {
318 $sth_branch_cat = $dbh->prepare("
319 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
320 FROM default_borrower_circ_rules
321 JOIN categories USING (categorycode)
324 $sth_branch_cat->execute();
326 $sth_branch_cat = $dbh->prepare("
327 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
328 FROM branch_borrower_circ_rules
329 JOIN categories USING (categorycode)
330 WHERE branch_borrower_circ_rules.branchcode = ?
332 $sth_branch_cat->execute($branch);
335 my @branch_cat_rules = ();
336 while (my $row = $sth_branch_cat->fetchrow_hashref) {
337 push @branch_cat_rules, $row;
339 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
341 my $sth_branch_default;
342 if ($branch eq "*") {
344 $sth_branch_default = $dbh->prepare("SELECT maxissueqty
345 FROM default_circ_rules");
346 $sth_branch_default->execute();
348 # add default for branch
349 $sth_branch_default = $dbh->prepare("SELECT maxissueqty
350 FROM default_branch_circ_rules
351 WHERE branchcode = ?");
352 $sth_branch_default->execute($branch);
355 if (my ($default_maxissueqty) = $sth_branch_default->fetchrow_array()) {
356 push @sorted_branch_cat_rules, {
357 default_humancategorycode => 1,
359 maxissueqty => $default_maxissueqty,
363 # note undef maxissueqty so that template can deal with them
364 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
365 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
369 if ($branch eq "*") {
370 $sth_branch_item = $dbh->prepare("
371 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
372 FROM default_branch_item_rules
373 JOIN itemtypes USING (itemtype)
375 $sth_branch_item->execute();
377 $sth_branch_item = $dbh->prepare("
378 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
379 FROM branch_item_rules
380 JOIN itemtypes USING (itemtype)
381 WHERE branch_item_rules.branchcode = ?
383 $sth_branch_item->execute($branch);
386 my @branch_item_rules = ();
387 while (my $row = $sth_branch_item->fetchrow_hashref) {
388 push @branch_item_rules, $row;
390 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
392 # note undef holdallowed so that template can deal with them
393 foreach my $entry (@sorted_branch_item_rules) {
394 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
395 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
398 $template->param(show_branch_cat_rule_form => 1);
399 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
400 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
402 $template->param(categoryloop => \@category_loop,
403 itemtypeloop => \@itemtypes,
404 rules => \@sorted_row_loop,
405 branchloop => \@branchloop,
406 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
409 output_html_with_http_headers $input, $cookie, $template->output;
413 # sort by patron category, then item type, putting
414 # default entries at the bottom
415 sub by_category_and_itemtype {
416 unless (by_category($a, $b)) {
417 return by_itemtype($a, $b);
423 if ($a->{'default_humancategorycode'}) {
424 return ($b->{'default_humancategorycode'} ? 0 : 1);
425 } elsif ($b->{'default_humancategorycode'}) {
428 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
434 if ($a->{'default_humanitemtype'}) {
435 return ($b->{'default_humanitemtype'} ? 0 : 1);
436 } elsif ($b->{'default_humanitemtype'}) {
439 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};