Add and change preferences, change tab names in sysprefs editor
[koha.git] / admin / smart-rules.pl
blob60866368591c2bca59d72c898df6b2b1a2d3f118
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 $flagsrequired;
33 # $flagsrequired->{circulation}=1;
34 my ($template, $loggedinuser, $cookie)
35 = get_template_and_user({template_name => "admin/smart-rules.tmpl",
36 query => $input,
37 type => "intranet",
38 authnotrequired => 0,
39 flagsrequired => {parameters => 1},
40 debug => 1,
41 });
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');
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 @row_loop;
353 my @itemtypes;
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
360 FROM issuingrules
361 LEFT JOIN itemtypes
362 ON (itemtypes.itemtype = issuingrules.itemtype)
363 LEFT JOIN categories
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;
377 $sth->finish;
379 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
381 my $sth_branch_cat;
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();
390 } else {
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;
413 my $sth_branch_item;
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();
421 } else {
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);
447 my $sth_defaults;
448 if ($branch eq "*") {
449 $sth_defaults = $dbh->prepare("
450 SELECT *
451 FROM default_circ_rules
453 $sth_defaults->execute();
454 } else {
455 $sth_defaults = $dbh->prepare("
456 SELECT *
457 FROM default_branch_circ_rules
458 WHERE branchcode = ?
460 $sth_defaults->execute($branch);
463 my $defaults = $sth_defaults->fetchrow_hashref;
465 if ($defaults) {
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} : ''),
479 branch => $branch
481 output_html_with_http_headers $input, $cookie, $template->output;
483 exit 0;
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);
493 sub by_category {
494 my ($a, $b) = @_;
495 if ($a->{'default_humancategorycode'}) {
496 return ($b->{'default_humancategorycode'} ? 0 : 1);
497 } elsif ($b->{'default_humancategorycode'}) {
498 return -1;
499 } else {
500 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
504 sub by_itemtype {
505 my ($a, $b) = @_;
506 if ($a->{'default_humanitemtype'}) {
507 return ($b->{'default_humanitemtype'} ? 0 : 1);
508 } elsif ($b->{'default_humanitemtype'}) {
509 return -1;
510 } else {
511 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};