Adding RIS And BibTex Export Followup
[koha.git] / admin / smart-rules.pl
blobc3300f369b385023d2dcc3cba9c852cb75cd17e2
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, renewalsallowed, reservesallowed, issuelength, fine, finedays, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
103 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, 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 $finedays = $input->param('finedays');
110 my $firstremind = $input->param('firstremind');
111 my $chargeperiod = $input->param('chargeperiod');
112 my $maxissueqty = $input->param('maxissueqty');
113 my $renewalsallowed = $input->param('renewalsallowed');
114 my $reservesallowed = $input->param('reservesallowed');
115 $maxissueqty =~ s/\s//g;
116 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
117 my $issuelength = $input->param('issuelength');
118 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
120 $sth_search->execute($br,$bor,$cat);
121 my $res = $sth_search->fetchrow_hashref();
122 if ($res->{total}) {
123 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$br,$bor,$cat);
124 } else {
125 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$fine,$finedays,$firstremind,$chargeperiod);
128 elsif ($op eq "set-branch-defaults") {
129 my $categorycode = $input->param('categorycode');
130 my $maxissueqty = $input->param('maxissueqty');
131 my $holdallowed = $input->param('holdallowed');
132 $maxissueqty =~ s/\s//g;
133 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
134 $holdallowed =~ s/\s//g;
135 $holdallowed = undef if $holdallowed !~ /^\d+/;
137 if ($branch eq "*") {
138 my $sth_search = $dbh->prepare("SELECT count(*) AS total
139 FROM default_circ_rules");
140 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
141 (maxissueqty, holdallowed)
142 VALUES (?, ?)");
143 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
144 SET maxissueqty = ?, holdallowed = ?");
146 $sth_search->execute();
147 my $res = $sth_search->fetchrow_hashref();
148 if ($res->{total}) {
149 $sth_update->execute($maxissueqty, $holdallowed);
150 } else {
151 $sth_insert->execute($maxissueqty, $holdallowed);
153 } else {
154 my $sth_search = $dbh->prepare("SELECT count(*) AS total
155 FROM default_branch_circ_rules
156 WHERE branchcode = ?");
157 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
158 (branchcode, maxissueqty, holdallowed)
159 VALUES (?, ?, ?)");
160 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
161 SET maxissueqty = ?, holdallowed = ?
162 WHERE branchcode = ?");
163 $sth_search->execute($branch);
164 my $res = $sth_search->fetchrow_hashref();
165 if ($res->{total}) {
166 $sth_update->execute($maxissueqty, $holdallowed, $branch);
167 } else {
168 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
172 elsif ($op eq "add-branch-cat") {
173 my $categorycode = $input->param('categorycode');
174 my $maxissueqty = $input->param('maxissueqty');
175 $maxissueqty =~ s/\s//g;
176 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
178 if ($branch eq "*") {
179 if ($categorycode eq "*") {
180 my $sth_search = $dbh->prepare("SELECT count(*) AS total
181 FROM default_circ_rules");
182 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
183 (maxissueqty)
184 VALUES (?)");
185 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
186 SET maxissueqty = ?");
188 $sth_search->execute();
189 my $res = $sth_search->fetchrow_hashref();
190 if ($res->{total}) {
191 $sth_update->execute($maxissueqty);
192 } else {
193 $sth_insert->execute($maxissueqty);
195 } else {
196 my $sth_search = $dbh->prepare("SELECT count(*) AS total
197 FROM default_borrower_circ_rules
198 WHERE categorycode = ?");
199 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
200 (categorycode, maxissueqty)
201 VALUES (?, ?)");
202 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
203 SET maxissueqty = ?
204 WHERE categorycode = ?");
205 $sth_search->execute($branch);
206 my $res = $sth_search->fetchrow_hashref();
207 if ($res->{total}) {
208 $sth_update->execute($maxissueqty, $categorycode);
209 } else {
210 $sth_insert->execute($categorycode, $maxissueqty);
213 } elsif ($categorycode eq "*") {
214 my $sth_search = $dbh->prepare("SELECT count(*) AS total
215 FROM default_branch_circ_rules
216 WHERE branchcode = ?");
217 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
218 (branchcode, maxissueqty)
219 VALUES (?, ?)");
220 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
221 SET maxissueqty = ?
222 WHERE branchcode = ?");
223 $sth_search->execute($branch);
224 my $res = $sth_search->fetchrow_hashref();
225 if ($res->{total}) {
226 $sth_update->execute($maxissueqty, $branch);
227 } else {
228 $sth_insert->execute($branch, $maxissueqty);
230 } else {
231 my $sth_search = $dbh->prepare("SELECT count(*) AS total
232 FROM branch_borrower_circ_rules
233 WHERE branchcode = ?
234 AND categorycode = ?");
235 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
236 (branchcode, categorycode, maxissueqty)
237 VALUES (?, ?, ?)");
238 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
239 SET maxissueqty = ?
240 WHERE branchcode = ?
241 AND categorycode = ?");
243 $sth_search->execute($branch, $categorycode);
244 my $res = $sth_search->fetchrow_hashref();
245 if ($res->{total}) {
246 $sth_update->execute($maxissueqty, $branch, $categorycode);
247 } else {
248 $sth_insert->execute($branch, $categorycode, $maxissueqty);
252 elsif ($op eq "add-branch-item") {
253 my $itemtype = $input->param('itemtype');
254 my $holdallowed = $input->param('holdallowed');
255 $holdallowed =~ s/\s//g;
256 $holdallowed = undef if $holdallowed !~ /^\d+/;
258 if ($branch eq "*") {
259 if ($itemtype eq "*") {
260 my $sth_search = $dbh->prepare("SELECT count(*) AS total
261 FROM default_circ_rules");
262 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
263 (holdallowed)
264 VALUES (?)");
265 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
266 SET holdallowed = ?");
268 $sth_search->execute();
269 my $res = $sth_search->fetchrow_hashref();
270 if ($res->{total}) {
271 $sth_update->execute($holdallowed);
272 } else {
273 $sth_insert->execute($holdallowed);
275 } else {
276 my $sth_search = $dbh->prepare("SELECT count(*) AS total
277 FROM default_branch_item_rules
278 WHERE itemtype = ?");
279 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
280 (itemtype, holdallowed)
281 VALUES (?, ?)");
282 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
283 SET holdallowed = ?
284 WHERE itemtype = ?");
285 $sth_search->execute($itemtype);
286 my $res = $sth_search->fetchrow_hashref();
287 if ($res->{total}) {
288 $sth_update->execute($holdallowed, $itemtype);
289 } else {
290 $sth_insert->execute($itemtype, $holdallowed);
293 } elsif ($itemtype eq "*") {
294 my $sth_search = $dbh->prepare("SELECT count(*) AS total
295 FROM default_branch_circ_rules
296 WHERE branchcode = ?");
297 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
298 (branchcode, holdallowed)
299 VALUES (?, ?)");
300 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
301 SET holdallowed = ?
302 WHERE branchcode = ?");
303 $sth_search->execute($branch);
304 my $res = $sth_search->fetchrow_hashref();
305 if ($res->{total}) {
306 $sth_update->execute($holdallowed, $branch);
307 } else {
308 $sth_insert->execute($branch, $holdallowed);
310 } else {
311 my $sth_search = $dbh->prepare("SELECT count(*) AS total
312 FROM branch_item_rules
313 WHERE branchcode = ?
314 AND itemtype = ?");
315 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
316 (branchcode, itemtype, holdallowed)
317 VALUES (?, ?, ?)");
318 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
319 SET holdallowed = ?
320 WHERE branchcode = ?
321 AND itemtype = ?");
323 $sth_search->execute($branch, $itemtype);
324 my $res = $sth_search->fetchrow_hashref();
325 if ($res->{total}) {
326 $sth_update->execute($holdallowed, $branch, $itemtype);
327 } else {
328 $sth_insert->execute($branch, $itemtype, $holdallowed);
333 my $branches = GetBranches();
334 my @branchloop;
335 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
336 my $selected = 1 if $thisbranch eq $branch;
337 my %row =(value => $thisbranch,
338 selected => $selected,
339 branchname => $branches->{$thisbranch}->{'branchname'},
341 push @branchloop, \%row;
344 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
345 $sth->execute;
346 my @category_loop;
347 while (my $data=$sth->fetchrow_hashref){
348 push @category_loop,$data;
351 $sth->finish;
352 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
353 $sth->execute;
354 # $i=0;
355 my @row_loop;
356 my @itemtypes;
357 while (my $row=$sth->fetchrow_hashref){
358 push @itemtypes,$row;
361 my $sth2 = $dbh->prepare("
362 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
363 FROM issuingrules
364 LEFT JOIN itemtypes
365 ON (itemtypes.itemtype = issuingrules.itemtype)
366 LEFT JOIN categories
367 ON (categories.categorycode = issuingrules.categorycode)
368 WHERE issuingrules.branchcode = ?
370 $sth2->execute($branch);
372 while (my $row = $sth2->fetchrow_hashref) {
373 $row->{'humanitemtype'} ||= $row->{'itemtype'};
374 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
375 $row->{'humancategorycode'} ||= $row->{'categorycode'};
376 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
377 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
378 push @row_loop, $row;
380 $sth->finish;
382 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
384 my $sth_branch_cat;
385 if ($branch eq "*") {
386 $sth_branch_cat = $dbh->prepare("
387 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
388 FROM default_borrower_circ_rules
389 JOIN categories USING (categorycode)
392 $sth_branch_cat->execute();
393 } else {
394 $sth_branch_cat = $dbh->prepare("
395 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
396 FROM branch_borrower_circ_rules
397 JOIN categories USING (categorycode)
398 WHERE branch_borrower_circ_rules.branchcode = ?
400 $sth_branch_cat->execute($branch);
403 my @branch_cat_rules = ();
404 while (my $row = $sth_branch_cat->fetchrow_hashref) {
405 push @branch_cat_rules, $row;
407 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
409 # note undef maxissueqty so that template can deal with them
410 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
411 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
414 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
416 my $sth_branch_item;
417 if ($branch eq "*") {
418 $sth_branch_item = $dbh->prepare("
419 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
420 FROM default_branch_item_rules
421 JOIN itemtypes USING (itemtype)
423 $sth_branch_item->execute();
424 } else {
425 $sth_branch_item = $dbh->prepare("
426 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
427 FROM branch_item_rules
428 JOIN itemtypes USING (itemtype)
429 WHERE branch_item_rules.branchcode = ?
431 $sth_branch_item->execute($branch);
434 my @branch_item_rules = ();
435 while (my $row = $sth_branch_item->fetchrow_hashref) {
436 push @branch_item_rules, $row;
438 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
440 # note undef holdallowed so that template can deal with them
441 foreach my $entry (@sorted_branch_item_rules) {
442 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
443 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
446 $template->param(show_branch_cat_rule_form => 1);
447 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
448 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
450 my $sth_defaults;
451 if ($branch eq "*") {
452 $sth_defaults = $dbh->prepare("
453 SELECT *
454 FROM default_circ_rules
456 $sth_defaults->execute();
457 } else {
458 $sth_defaults = $dbh->prepare("
459 SELECT *
460 FROM default_branch_circ_rules
461 WHERE branchcode = ?
463 $sth_defaults->execute($branch);
466 my $defaults = $sth_defaults->fetchrow_hashref;
468 if ($defaults) {
469 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
470 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
471 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
472 $template->param(default_maxissueqty => $defaults->{maxissueqty});
475 $template->param(default_rules => ($defaults ? 1 : 0));
477 $template->param(categoryloop => \@category_loop,
478 itemtypeloop => \@itemtypes,
479 rules => \@sorted_row_loop,
480 branchloop => \@branchloop,
481 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
482 branch => $branch,
483 definedbranch => $branch eq '*'
485 output_html_with_http_headers $input, $cookie, $template->output;
487 exit 0;
489 # sort by patron category, then item type, putting
490 # default entries at the bottom
491 sub by_category_and_itemtype {
492 unless (by_category($a, $b)) {
493 return by_itemtype($a, $b);
497 sub by_category {
498 my ($a, $b) = @_;
499 if ($a->{'default_humancategorycode'}) {
500 return ($b->{'default_humancategorycode'} ? 0 : 1);
501 } elsif ($b->{'default_humancategorycode'}) {
502 return -1;
503 } else {
504 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
508 sub by_itemtype {
509 my ($a, $b) = @_;
510 if ($a->{'default_humanitemtype'}) {
511 return ($b->{'default_humanitemtype'} ? 0 : 1);
512 } elsif ($b->{'default_humanitemtype'}) {
513 return -1;
514 } else {
515 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};