bug-2923: replaces 'renew/not renew' text with nice 'renew/return 'yui buttons.
[koha.git] / admin / smart-rules.pl
blobce7dc9dbd3839b00913100d1a923a949935ff9ca
1 #!/usr/bin/perl
2 # vim: et ts=4 sw=4
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
10 # version.
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
20 use strict;
21 use CGI;
22 use C4::Context;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Debug;
27 use C4::Branch; # GetBranches
29 my $input = new CGI;
30 my $dbh = C4::Context->dbh;
32 my $type=$input->param('type');
33 my $branch = $input->param('branch') || '*';
34 my $op = $input->param('op');
36 # my $flagsrequired;
37 # $flagsrequired->{circulation}=1;
38 my ($template, $loggedinuser, $cookie)
39 = get_template_and_user({template_name => "admin/smart-rules.tmpl",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => {parameters => 1},
44 debug => 1,
45 });
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');
57 if ($branch eq "*") {
58 if ($categorycode eq "*") {
59 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
60 $sth_delete->execute();
61 } else {
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);
70 } else {
71 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
72 WHERE branchcode = ?
73 AND categorycode = ?");
74 $sth_delete->execute($branch, $categorycode);
77 elsif ($op eq 'delete-branch-item') {
78 my $itemtype = $input->param('itemtype');
79 if ($branch eq "*") {
80 if ($itemtype eq "*") {
81 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
82 $sth_delete->execute();
83 } else {
84 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
85 WHERE itemtype = ?");
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);
92 } else {
93 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
94 WHERE branchcode = ?
95 AND itemtype = ?");
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();
119 if ($res->{total}) {
120 $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
121 } else {
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)
139 VALUES (?, ?)");
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();
145 if ($res->{total}) {
146 $sth_update->execute($maxissueqty, $holdallowed);
147 } else {
148 $sth_insert->execute($maxissueqty, $holdallowed);
150 } else {
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)
156 VALUES (?, ?, ?)");
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();
162 if ($res->{total}) {
163 $sth_update->execute($maxissueqty, $holdallowed, $branch);
164 } else {
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
180 (maxissueqty)
181 VALUES (?)");
182 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
183 SET maxissueqty = ?");
185 $sth_search->execute();
186 my $res = $sth_search->fetchrow_hashref();
187 if ($res->{total}) {
188 $sth_update->execute($maxissueqty);
189 } else {
190 $sth_insert->execute($maxissueqty);
192 } else {
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)
198 VALUES (?, ?)");
199 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
200 SET maxissueqty = ?
201 WHERE categorycode = ?");
202 $sth_search->execute($branch);
203 my $res = $sth_search->fetchrow_hashref();
204 if ($res->{total}) {
205 $sth_update->execute($maxissueqty, $categorycode);
206 } else {
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)
216 VALUES (?, ?)");
217 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
218 SET maxissueqty = ?
219 WHERE branchcode = ?");
220 $sth_search->execute($branch);
221 my $res = $sth_search->fetchrow_hashref();
222 if ($res->{total}) {
223 $sth_update->execute($maxissueqty, $branch);
224 } else {
225 $sth_insert->execute($branch, $maxissueqty);
227 } else {
228 my $sth_search = $dbh->prepare("SELECT count(*) AS total
229 FROM branch_borrower_circ_rules
230 WHERE branchcode = ?
231 AND categorycode = ?");
232 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
233 (branchcode, categorycode, maxissueqty)
234 VALUES (?, ?, ?)");
235 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
236 SET maxissueqty = ?
237 WHERE branchcode = ?
238 AND categorycode = ?");
240 $sth_search->execute($branch, $categorycode);
241 my $res = $sth_search->fetchrow_hashref();
242 if ($res->{total}) {
243 $sth_update->execute($maxissueqty, $branch, $categorycode);
244 } else {
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
260 (holdallowed)
261 VALUES (?)");
262 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
263 SET holdallowed = ?");
265 $sth_search->execute();
266 my $res = $sth_search->fetchrow_hashref();
267 if ($res->{total}) {
268 $sth_update->execute($holdallowed);
269 } else {
270 $sth_insert->execute($holdallowed);
272 } else {
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)
278 VALUES (?, ?)");
279 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
280 SET holdallowed = ?
281 WHERE itemtype = ?");
282 $sth_search->execute($itemtype);
283 my $res = $sth_search->fetchrow_hashref();
284 if ($res->{total}) {
285 $sth_update->execute($holdallowed, $itemtype);
286 } else {
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)
296 VALUES (?, ?)");
297 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
298 SET holdallowed = ?
299 WHERE branchcode = ?");
300 $sth_search->execute($branch);
301 my $res = $sth_search->fetchrow_hashref();
302 if ($res->{total}) {
303 $sth_update->execute($holdallowed, $branch);
304 } else {
305 $sth_insert->execute($branch, $holdallowed);
307 } else {
308 my $sth_search = $dbh->prepare("SELECT count(*) AS total
309 FROM branch_item_rules
310 WHERE branchcode = ?
311 AND itemtype = ?");
312 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
313 (branchcode, itemtype, holdallowed)
314 VALUES (?, ?, ?)");
315 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
316 SET holdallowed = ?
317 WHERE branchcode = ?
318 AND itemtype = ?");
320 $sth_search->execute($branch, $itemtype);
321 my $res = $sth_search->fetchrow_hashref();
322 if ($res->{total}) {
323 $sth_update->execute($holdallowed, $branch, $itemtype);
324 } else {
325 $sth_insert->execute($branch, $itemtype, $holdallowed);
330 my $branches = GetBranches();
331 my @branchloop;
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");
342 $sth->execute;
343 my @category_loop;
344 while (my $data=$sth->fetchrow_hashref){
345 push @category_loop,$data;
348 $sth->finish;
349 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
350 $sth->execute;
351 # $i=0;
352 my $toggle= 1;
353 my @row_loop;
354 my @itemtypes;
355 while (my $row=$sth->fetchrow_hashref){
356 push @itemtypes,$row;
359 my $sth2 = $dbh->prepare("
360 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
361 FROM issuingrules
362 LEFT JOIN itemtypes
363 ON (itemtypes.itemtype = issuingrules.itemtype)
364 LEFT JOIN categories
365 ON (categories.categorycode = issuingrules.categorycode)
366 WHERE issuingrules.branchcode = ?
368 $sth2->execute($branch);
370 while (my $row = $sth2->fetchrow_hashref) {
371 $row->{'humanitemtype'} ||= $row->{'itemtype'};
372 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
373 $row->{'humancategorycode'} ||= $row->{'categorycode'};
374 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
375 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
376 push @row_loop, $row;
378 $sth->finish;
380 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
382 my $sth_branch_cat;
383 if ($branch eq "*") {
384 $sth_branch_cat = $dbh->prepare("
385 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
386 FROM default_borrower_circ_rules
387 JOIN categories USING (categorycode)
390 $sth_branch_cat->execute();
391 } else {
392 $sth_branch_cat = $dbh->prepare("
393 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
394 FROM branch_borrower_circ_rules
395 JOIN categories USING (categorycode)
396 WHERE branch_borrower_circ_rules.branchcode = ?
398 $sth_branch_cat->execute($branch);
401 my @branch_cat_rules = ();
402 while (my $row = $sth_branch_cat->fetchrow_hashref) {
403 push @branch_cat_rules, $row;
405 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
407 # note undef maxissueqty so that template can deal with them
408 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
409 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
412 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
414 my $sth_branch_item;
415 if ($branch eq "*") {
416 $sth_branch_item = $dbh->prepare("
417 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
418 FROM default_branch_item_rules
419 JOIN itemtypes USING (itemtype)
421 $sth_branch_item->execute();
422 } else {
423 $sth_branch_item = $dbh->prepare("
424 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
425 FROM branch_item_rules
426 JOIN itemtypes USING (itemtype)
427 WHERE branch_item_rules.branchcode = ?
429 $sth_branch_item->execute($branch);
432 my @branch_item_rules = ();
433 while (my $row = $sth_branch_item->fetchrow_hashref) {
434 push @branch_item_rules, $row;
436 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
438 # note undef holdallowed so that template can deal with them
439 foreach my $entry (@sorted_branch_item_rules) {
440 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
441 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
444 $template->param(show_branch_cat_rule_form => 1);
445 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
446 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
448 my $sth_defaults;
449 if ($branch eq "*") {
450 $sth_defaults = $dbh->prepare("
451 SELECT *
452 FROM default_circ_rules
454 $sth_defaults->execute();
455 } else {
456 $sth_defaults = $dbh->prepare("
457 SELECT *
458 FROM default_branch_circ_rules
459 WHERE branchcode = ?
461 $sth_defaults->execute($branch);
464 my $defaults = $sth_defaults->fetchrow_hashref;
466 if ($defaults) {
467 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
468 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
469 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
470 $template->param(default_maxissueqty => $defaults->{maxissueqty});
473 $template->param(default_rules => ($defaults ? 1 : 0));
475 $template->param(categoryloop => \@category_loop,
476 itemtypeloop => \@itemtypes,
477 rules => \@sorted_row_loop,
478 branchloop => \@branchloop,
479 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
480 branch => $branch
482 output_html_with_http_headers $input, $cookie, $template->output;
484 exit 0;
486 # sort by patron category, then item type, putting
487 # default entries at the bottom
488 sub by_category_and_itemtype {
489 unless (by_category($a, $b)) {
490 return by_itemtype($a, $b);
494 sub by_category {
495 my ($a, $b) = @_;
496 if ($a->{'default_humancategorycode'}) {
497 return ($b->{'default_humancategorycode'} ? 0 : 1);
498 } elsif ($b->{'default_humancategorycode'}) {
499 return -1;
500 } else {
501 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
505 sub by_itemtype {
506 my ($a, $b) = @_;
507 if ($a->{'default_humanitemtype'}) {
508 return ($b->{'default_humanitemtype'} ? 0 : 1);
509 } elsif ($b->{'default_humanitemtype'}) {
510 return -1;
511 } else {
512 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};