Bug 12481: Staff client detail-view "next" link is greyed out when the last search...
[koha.git] / admin / smart-rules.pl
blobb3c0727cac6f06224a326df5c1d0a2c9f89bb096
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # copyright 2010 BibLibre
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Debug;
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
31 my $input = CGI->new;
32 my $dbh = C4::Context->dbh;
34 # my $flagsrequired;
35 # $flagsrequired->{circulation}=1;
36 my ($template, $loggedinuser, $cookie)
37 = get_template_and_user({template_name => "admin/smart-rules.tmpl",
38 query => $input,
39 type => "intranet",
40 authnotrequired => 0,
41 flagsrequired => {parameters => 'manage_circ_rules'},
42 debug => 1,
43 });
45 my $type=$input->param('type');
46 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
47 my $op = $input->param('op') || q{};
49 if ($op eq 'delete') {
50 my $itemtype = $input->param('itemtype');
51 my $categorycode = $input->param('categorycode');
52 $debug and warn "deleting $1 $2 $branch";
54 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
55 $sth_Idelete->execute($branch, $categorycode, $itemtype);
57 elsif ($op eq 'delete-branch-cat') {
58 my $categorycode = $input->param('categorycode');
59 if ($branch eq "*") {
60 if ($categorycode eq "*") {
61 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
62 $sth_delete->execute();
63 } else {
64 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
65 WHERE categorycode = ?");
66 $sth_delete->execute($categorycode);
68 } elsif ($categorycode eq "*") {
69 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
70 WHERE branchcode = ?");
71 $sth_delete->execute($branch);
72 } else {
73 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
74 WHERE branchcode = ?
75 AND categorycode = ?");
76 $sth_delete->execute($branch, $categorycode);
79 elsif ($op eq 'delete-branch-item') {
80 my $itemtype = $input->param('itemtype');
81 if ($branch eq "*") {
82 if ($itemtype eq "*") {
83 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
84 $sth_delete->execute();
85 } else {
86 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
87 WHERE itemtype = ?");
88 $sth_delete->execute($itemtype);
90 } elsif ($itemtype eq "*") {
91 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
92 WHERE branchcode = ?");
93 $sth_delete->execute($branch);
94 } else {
95 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
96 WHERE branchcode = ?
97 AND itemtype = ?");
98 $sth_delete->execute($branch, $itemtype);
101 # save the values entered
102 elsif ($op eq 'add') {
103 my $sth_search = $dbh->prepare('SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?');
104 my $sth_insert = $dbh->prepare('INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, renewalperiod, norenewalbefore, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, maxsuspensiondays, firstremind, chargeperiod,rentaldiscount, overduefinescap) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
105 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, maxsuspensiondays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, renewalperiod=?, norenewalbefore=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=?, overduefinescap=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
107 my $br = $branch; # branch
108 my $bor = $input->param('categorycode'); # borrower category
109 my $cat = $input->param('itemtype'); # item type
110 my $fine = $input->param('fine');
111 my $finedays = $input->param('finedays');
112 my $maxsuspensiondays = $input->param('maxsuspensiondays');
113 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
114 my $firstremind = $input->param('firstremind');
115 my $chargeperiod = $input->param('chargeperiod');
116 my $maxissueqty = $input->param('maxissueqty');
117 my $renewalsallowed = $input->param('renewalsallowed');
118 my $renewalperiod = $input->param('renewalperiod');
119 my $norenewalbefore = $input->param('norenewalbefore');
120 $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
121 my $reservesallowed = $input->param('reservesallowed');
122 $maxissueqty =~ s/\s//g;
123 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
124 my $issuelength = $input->param('issuelength');
125 my $lengthunit = $input->param('lengthunit');
126 my $hardduedate = $input->param('hardduedate');
127 $hardduedate = format_date_in_iso($hardduedate);
128 my $hardduedatecompare = $input->param('hardduedatecompare');
129 my $rentaldiscount = $input->param('rentaldiscount');
130 my $overduefinescap = $input->param('overduefinescap') || undef;
131 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
133 $sth_search->execute($br,$bor,$cat);
134 my $res = $sth_search->fetchrow_hashref();
135 if ($res->{total}) {
136 $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
137 } else {
138 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
141 elsif ($op eq "set-branch-defaults") {
142 my $categorycode = $input->param('categorycode');
143 my $maxissueqty = $input->param('maxissueqty');
144 my $holdallowed = $input->param('holdallowed');
145 my $returnbranch = $input->param('returnbranch');
146 $maxissueqty =~ s/\s//g;
147 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
148 $holdallowed =~ s/\s//g;
149 $holdallowed = undef if $holdallowed !~ /^\d+/;
151 if ($branch eq "*") {
152 my $sth_search = $dbh->prepare("SELECT count(*) AS total
153 FROM default_circ_rules");
154 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
155 (maxissueqty, holdallowed, returnbranch)
156 VALUES (?, ?, ?)");
157 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
158 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
160 $sth_search->execute();
161 my $res = $sth_search->fetchrow_hashref();
162 if ($res->{total}) {
163 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
164 } else {
165 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
167 } else {
168 my $sth_search = $dbh->prepare("SELECT count(*) AS total
169 FROM default_branch_circ_rules
170 WHERE branchcode = ?");
171 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
172 (branchcode, maxissueqty, holdallowed, returnbranch)
173 VALUES (?, ?, ?, ?)");
174 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
175 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
176 WHERE branchcode = ?");
177 $sth_search->execute($branch);
178 my $res = $sth_search->fetchrow_hashref();
179 if ($res->{total}) {
180 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
181 } else {
182 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
186 elsif ($op eq "add-branch-cat") {
187 my $categorycode = $input->param('categorycode');
188 my $maxissueqty = $input->param('maxissueqty');
189 $maxissueqty =~ s/\s//g;
190 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
192 if ($branch eq "*") {
193 if ($categorycode eq "*") {
194 my $sth_search = $dbh->prepare("SELECT count(*) AS total
195 FROM default_circ_rules");
196 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
197 (maxissueqty)
198 VALUES (?)");
199 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
200 SET maxissueqty = ?");
202 $sth_search->execute();
203 my $res = $sth_search->fetchrow_hashref();
204 if ($res->{total}) {
205 $sth_update->execute($maxissueqty);
206 } else {
207 $sth_insert->execute($maxissueqty);
209 } else {
210 my $sth_search = $dbh->prepare("SELECT count(*) AS total
211 FROM default_borrower_circ_rules
212 WHERE categorycode = ?");
213 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
214 (categorycode, maxissueqty)
215 VALUES (?, ?)");
216 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
217 SET maxissueqty = ?
218 WHERE categorycode = ?");
219 $sth_search->execute($branch);
220 my $res = $sth_search->fetchrow_hashref();
221 if ($res->{total}) {
222 $sth_update->execute($maxissueqty, $categorycode);
223 } else {
224 $sth_insert->execute($categorycode, $maxissueqty);
227 } elsif ($categorycode eq "*") {
228 my $sth_search = $dbh->prepare("SELECT count(*) AS total
229 FROM default_branch_circ_rules
230 WHERE branchcode = ?");
231 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
232 (branchcode, maxissueqty)
233 VALUES (?, ?)");
234 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
235 SET maxissueqty = ?
236 WHERE branchcode = ?");
237 $sth_search->execute($branch);
238 my $res = $sth_search->fetchrow_hashref();
239 if ($res->{total}) {
240 $sth_update->execute($maxissueqty, $branch);
241 } else {
242 $sth_insert->execute($branch, $maxissueqty);
244 } else {
245 my $sth_search = $dbh->prepare("SELECT count(*) AS total
246 FROM branch_borrower_circ_rules
247 WHERE branchcode = ?
248 AND categorycode = ?");
249 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
250 (branchcode, categorycode, maxissueqty)
251 VALUES (?, ?, ?)");
252 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
253 SET maxissueqty = ?
254 WHERE branchcode = ?
255 AND categorycode = ?");
257 $sth_search->execute($branch, $categorycode);
258 my $res = $sth_search->fetchrow_hashref();
259 if ($res->{total}) {
260 $sth_update->execute($maxissueqty, $branch, $categorycode);
261 } else {
262 $sth_insert->execute($branch, $categorycode, $maxissueqty);
266 elsif ($op eq "add-branch-item") {
267 my $itemtype = $input->param('itemtype');
268 my $holdallowed = $input->param('holdallowed');
269 my $returnbranch = $input->param('returnbranch');
270 $holdallowed =~ s/\s//g;
271 $holdallowed = undef if $holdallowed !~ /^\d+/;
273 if ($branch eq "*") {
274 if ($itemtype eq "*") {
275 my $sth_search = $dbh->prepare("SELECT count(*) AS total
276 FROM default_circ_rules");
277 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
278 (holdallowed, returnbranch)
279 VALUES (?, ?)");
280 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
281 SET holdallowed = ?, returnbranch = ?");
283 $sth_search->execute();
284 my $res = $sth_search->fetchrow_hashref();
285 if ($res->{total}) {
286 $sth_update->execute($holdallowed, $returnbranch);
287 } else {
288 $sth_insert->execute($holdallowed, $returnbranch);
290 } else {
291 my $sth_search = $dbh->prepare("SELECT count(*) AS total
292 FROM default_branch_item_rules
293 WHERE itemtype = ?");
294 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
295 (itemtype, holdallowed, returnbranch)
296 VALUES (?, ?, ?)");
297 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
298 SET holdallowed = ?, returnbranch = ?
299 WHERE itemtype = ?");
300 $sth_search->execute($itemtype);
301 my $res = $sth_search->fetchrow_hashref();
302 if ($res->{total}) {
303 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
304 } else {
305 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
308 } elsif ($itemtype eq "*") {
309 my $sth_search = $dbh->prepare("SELECT count(*) AS total
310 FROM default_branch_circ_rules
311 WHERE branchcode = ?");
312 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
313 (branchcode, holdallowed, returnbranch)
314 VALUES (?, ?, ?)");
315 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
316 SET holdallowed = ?, returnbranch = ?
317 WHERE branchcode = ?");
318 $sth_search->execute($branch);
319 my $res = $sth_search->fetchrow_hashref();
320 if ($res->{total}) {
321 $sth_update->execute($holdallowed, $returnbranch, $branch);
322 } else {
323 $sth_insert->execute($branch, $holdallowed, $returnbranch);
325 } else {
326 my $sth_search = $dbh->prepare("SELECT count(*) AS total
327 FROM branch_item_rules
328 WHERE branchcode = ?
329 AND itemtype = ?");
330 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
331 (branchcode, itemtype, holdallowed, returnbranch)
332 VALUES (?, ?, ?, ?)");
333 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
334 SET holdallowed = ?, returnbranch = ?
335 WHERE branchcode = ?
336 AND itemtype = ?");
338 $sth_search->execute($branch, $itemtype);
339 my $res = $sth_search->fetchrow_hashref();
340 if ($res->{total}) {
341 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
342 } else {
343 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
348 my $branches = GetBranches();
349 my @branchloop;
350 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
351 push @branchloop, {
352 value => $thisbranch,
353 selected => $thisbranch eq $branch,
354 branchname => $branches->{$thisbranch}->{'branchname'},
358 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
359 $sth->execute;
360 my @category_loop;
361 while (my $data=$sth->fetchrow_hashref){
362 push @category_loop,$data;
365 $sth->finish;
366 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
367 $sth->execute;
368 # $i=0;
369 my @row_loop;
370 my @itemtypes;
371 while (my $row=$sth->fetchrow_hashref){
372 push @itemtypes,$row;
375 my $sth2 = $dbh->prepare("
376 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
377 FROM issuingrules
378 LEFT JOIN itemtypes
379 ON (itemtypes.itemtype = issuingrules.itemtype)
380 LEFT JOIN categories
381 ON (categories.categorycode = issuingrules.categorycode)
382 WHERE issuingrules.branchcode = ?
384 $sth2->execute($branch);
386 while (my $row = $sth2->fetchrow_hashref) {
387 $row->{'current_branch'} ||= $row->{'branchcode'};
388 $row->{'humanitemtype'} ||= $row->{'itemtype'};
389 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
390 $row->{'humancategorycode'} ||= $row->{'categorycode'};
391 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
392 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
393 if ($row->{'hardduedate'} ne '0000-00-00') {
394 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
395 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
396 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
397 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
398 } else {
399 $row->{'hardduedate'} = 0;
401 push @row_loop, $row;
403 $sth->finish;
405 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
407 my $sth_branch_cat;
408 if ($branch eq "*") {
409 $sth_branch_cat = $dbh->prepare("
410 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
411 FROM default_borrower_circ_rules
412 JOIN categories USING (categorycode)
415 $sth_branch_cat->execute();
416 } else {
417 $sth_branch_cat = $dbh->prepare("
418 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
419 FROM branch_borrower_circ_rules
420 JOIN categories USING (categorycode)
421 WHERE branch_borrower_circ_rules.branchcode = ?
423 $sth_branch_cat->execute($branch);
426 my @branch_cat_rules = ();
427 while (my $row = $sth_branch_cat->fetchrow_hashref) {
428 push @branch_cat_rules, $row;
430 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
432 # note undef maxissueqty so that template can deal with them
433 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
434 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
437 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
439 my $sth_branch_item;
440 if ($branch eq "*") {
441 $sth_branch_item = $dbh->prepare("
442 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
443 FROM default_branch_item_rules
444 JOIN itemtypes USING (itemtype)
446 $sth_branch_item->execute();
447 } else {
448 $sth_branch_item = $dbh->prepare("
449 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
450 FROM branch_item_rules
451 JOIN itemtypes USING (itemtype)
452 WHERE branch_item_rules.branchcode = ?
454 $sth_branch_item->execute($branch);
457 my @branch_item_rules = ();
458 while (my $row = $sth_branch_item->fetchrow_hashref) {
459 push @branch_item_rules, $row;
461 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
463 # note undef holdallowed so that template can deal with them
464 foreach my $entry (@sorted_branch_item_rules) {
465 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
466 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
469 $template->param(show_branch_cat_rule_form => 1);
470 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
471 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
473 my $sth_defaults;
474 if ($branch eq "*") {
475 $sth_defaults = $dbh->prepare("
476 SELECT *
477 FROM default_circ_rules
479 $sth_defaults->execute();
480 } else {
481 $sth_defaults = $dbh->prepare("
482 SELECT *
483 FROM default_branch_circ_rules
484 WHERE branchcode = ?
486 $sth_defaults->execute($branch);
489 my $defaults = $sth_defaults->fetchrow_hashref;
491 if ($defaults) {
492 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
493 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
494 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
495 $template->param(default_maxissueqty => $defaults->{maxissueqty});
496 $template->param(default_returnbranch => $defaults->{returnbranch});
499 $template->param(default_rules => ($defaults ? 1 : 0));
501 $template->param(categoryloop => \@category_loop,
502 itemtypeloop => \@itemtypes,
503 rules => \@sorted_row_loop,
504 branchloop => \@branchloop,
505 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
506 current_branch => $branch,
507 definedbranch => scalar(@sorted_row_loop)>0
509 output_html_with_http_headers $input, $cookie, $template->output;
511 exit 0;
513 # sort by patron category, then item type, putting
514 # default entries at the bottom
515 sub by_category_and_itemtype {
516 unless (by_category($a, $b)) {
517 return by_itemtype($a, $b);
521 sub by_category {
522 my ($a, $b) = @_;
523 if ($a->{'default_humancategorycode'}) {
524 return ($b->{'default_humancategorycode'} ? 0 : 1);
525 } elsif ($b->{'default_humancategorycode'}) {
526 return -1;
527 } else {
528 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
532 sub by_itemtype {
533 my ($a, $b) = @_;
534 if ($a->{'default_humanitemtype'}) {
535 return ($b->{'default_humanitemtype'} ? 0 : 1);
536 } elsif ($b->{'default_humanitemtype'}) {
537 return -1;
538 } else {
539 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};