Bug 14646: (regression test) Koha::RecordProcessor only accepts one filter at a time
[koha.git] / admin / smart-rules.pl
blob9ef6ef6b9f3763cc1ab15f2dc60a518e8c3a5925
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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 );
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/;
30 use Koha::Database;
32 my $input = CGI->new;
33 my $dbh = C4::Context->dbh;
35 # my $flagsrequired;
36 # $flagsrequired->{circulation}=1;
37 my ($template, $loggedinuser, $cookie)
38 = get_template_and_user({template_name => "admin/smart-rules.tt",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => {parameters => 'manage_circ_rules'},
43 debug => 1,
44 });
46 my $type=$input->param('type');
47 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
48 my $op = $input->param('op') || q{};
50 if ($op eq 'delete') {
51 my $itemtype = $input->param('itemtype');
52 my $categorycode = $input->param('categorycode');
53 $debug and warn "deleting $1 $2 $branch";
55 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
56 $sth_Idelete->execute($branch, $categorycode, $itemtype);
58 elsif ($op eq 'delete-branch-cat') {
59 my $categorycode = $input->param('categorycode');
60 if ($branch eq "*") {
61 if ($categorycode eq "*") {
62 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
63 $sth_delete->execute();
64 } else {
65 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
66 WHERE categorycode = ?");
67 $sth_delete->execute($categorycode);
69 } elsif ($categorycode eq "*") {
70 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
71 WHERE branchcode = ?");
72 $sth_delete->execute($branch);
73 } else {
74 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
75 WHERE branchcode = ?
76 AND categorycode = ?");
77 $sth_delete->execute($branch, $categorycode);
80 elsif ($op eq 'delete-branch-item') {
81 my $itemtype = $input->param('itemtype');
82 if ($branch eq "*") {
83 if ($itemtype eq "*") {
84 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
85 $sth_delete->execute();
86 } else {
87 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
88 WHERE itemtype = ?");
89 $sth_delete->execute($itemtype);
91 } elsif ($itemtype eq "*") {
92 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
93 WHERE branchcode = ?");
94 $sth_delete->execute($branch);
95 } else {
96 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
97 WHERE branchcode = ?
98 AND itemtype = ?");
99 $sth_delete->execute($branch, $itemtype);
102 # save the values entered
103 elsif ($op eq 'add') {
104 my $br = $branch; # branch
105 my $bor = $input->param('categorycode'); # borrower category
106 my $itemtype = $input->param('itemtype'); # item type
107 my $fine = $input->param('fine');
108 my $finedays = $input->param('finedays');
109 my $maxsuspensiondays = $input->param('maxsuspensiondays');
110 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
111 my $firstremind = $input->param('firstremind');
112 my $chargeperiod = $input->param('chargeperiod');
113 my $maxissueqty = $input->param('maxissueqty');
114 my $renewalsallowed = $input->param('renewalsallowed');
115 my $renewalperiod = $input->param('renewalperiod');
116 my $norenewalbefore = $input->param('norenewalbefore');
117 $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
118 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
119 my $reservesallowed = $input->param('reservesallowed');
120 my $onshelfholds = $input->param('onshelfholds') || 0;
121 $maxissueqty =~ s/\s//g;
122 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
123 my $issuelength = $input->param('issuelength');
124 my $lengthunit = $input->param('lengthunit');
125 my $hardduedate = $input->param('hardduedate');
126 $hardduedate = format_date_in_iso($hardduedate);
127 my $hardduedatecompare = $input->param('hardduedatecompare');
128 my $rentaldiscount = $input->param('rentaldiscount');
129 my $opacitemholds = $input->param('opacitemholds') || 0;
130 my $overduefinescap = $input->param('overduefinescap') || undef;
131 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty";
133 my $schema = Koha::Database->new()->schema();
134 my $rs = $schema->resultset('Issuingrule');
136 my $params = {
137 branchcode => $br,
138 categorycode => $bor,
139 itemtype => $itemtype,
140 fine => $fine,
141 finedays => $finedays,
142 maxsuspensiondays => $maxsuspensiondays,
143 firstremind => $firstremind,
144 chargeperiod => $chargeperiod,
145 maxissueqty => $maxissueqty,
146 renewalsallowed => $renewalsallowed,
147 renewalperiod => $renewalperiod,
148 norenewalbefore => $norenewalbefore,
149 auto_renew => $auto_renew,
150 reservesallowed => $reservesallowed,
151 issuelength => $issuelength,
152 lengthunit => $lengthunit,
153 hardduedate => $hardduedate,
154 hardduedatecompare => $hardduedatecompare,
155 rentaldiscount => $rentaldiscount,
156 onshelfholds => $onshelfholds,
157 opacitemholds => $opacitemholds,
158 overduefinescap => $overduefinescap,
161 $rs->update_or_create($params);
164 elsif ($op eq "set-branch-defaults") {
165 my $categorycode = $input->param('categorycode');
166 my $maxissueqty = $input->param('maxissueqty');
167 my $holdallowed = $input->param('holdallowed');
168 my $returnbranch = $input->param('returnbranch');
169 $maxissueqty =~ s/\s//g;
170 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
171 $holdallowed =~ s/\s//g;
172 $holdallowed = undef if $holdallowed !~ /^\d+/;
174 if ($branch eq "*") {
175 my $sth_search = $dbh->prepare("SELECT count(*) AS total
176 FROM default_circ_rules");
177 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
178 (maxissueqty, holdallowed, returnbranch)
179 VALUES (?, ?, ?)");
180 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
181 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
183 $sth_search->execute();
184 my $res = $sth_search->fetchrow_hashref();
185 if ($res->{total}) {
186 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
187 } else {
188 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
190 } else {
191 my $sth_search = $dbh->prepare("SELECT count(*) AS total
192 FROM default_branch_circ_rules
193 WHERE branchcode = ?");
194 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
195 (branchcode, maxissueqty, holdallowed, returnbranch)
196 VALUES (?, ?, ?, ?)");
197 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
198 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
199 WHERE branchcode = ?");
200 $sth_search->execute($branch);
201 my $res = $sth_search->fetchrow_hashref();
202 if ($res->{total}) {
203 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
204 } else {
205 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
209 elsif ($op eq "add-branch-cat") {
210 my $categorycode = $input->param('categorycode');
211 my $maxissueqty = $input->param('maxissueqty');
212 $maxissueqty =~ s/\s//g;
213 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
215 if ($branch eq "*") {
216 if ($categorycode eq "*") {
217 my $sth_search = $dbh->prepare("SELECT count(*) AS total
218 FROM default_circ_rules");
219 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
220 (maxissueqty)
221 VALUES (?)");
222 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
223 SET maxissueqty = ?");
225 $sth_search->execute();
226 my $res = $sth_search->fetchrow_hashref();
227 if ($res->{total}) {
228 $sth_update->execute($maxissueqty);
229 } else {
230 $sth_insert->execute($maxissueqty);
232 } else {
233 my $sth_search = $dbh->prepare("SELECT count(*) AS total
234 FROM default_borrower_circ_rules
235 WHERE categorycode = ?");
236 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
237 (categorycode, maxissueqty)
238 VALUES (?, ?)");
239 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
240 SET maxissueqty = ?
241 WHERE categorycode = ?");
242 $sth_search->execute($branch);
243 my $res = $sth_search->fetchrow_hashref();
244 if ($res->{total}) {
245 $sth_update->execute($maxissueqty, $categorycode);
246 } else {
247 $sth_insert->execute($categorycode, $maxissueqty);
250 } elsif ($categorycode eq "*") {
251 my $sth_search = $dbh->prepare("SELECT count(*) AS total
252 FROM default_branch_circ_rules
253 WHERE branchcode = ?");
254 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
255 (branchcode, maxissueqty)
256 VALUES (?, ?)");
257 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
258 SET maxissueqty = ?
259 WHERE branchcode = ?");
260 $sth_search->execute($branch);
261 my $res = $sth_search->fetchrow_hashref();
262 if ($res->{total}) {
263 $sth_update->execute($maxissueqty, $branch);
264 } else {
265 $sth_insert->execute($branch, $maxissueqty);
267 } else {
268 my $sth_search = $dbh->prepare("SELECT count(*) AS total
269 FROM branch_borrower_circ_rules
270 WHERE branchcode = ?
271 AND categorycode = ?");
272 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
273 (branchcode, categorycode, maxissueqty)
274 VALUES (?, ?, ?)");
275 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
276 SET maxissueqty = ?
277 WHERE branchcode = ?
278 AND categorycode = ?");
280 $sth_search->execute($branch, $categorycode);
281 my $res = $sth_search->fetchrow_hashref();
282 if ($res->{total}) {
283 $sth_update->execute($maxissueqty, $branch, $categorycode);
284 } else {
285 $sth_insert->execute($branch, $categorycode, $maxissueqty);
289 elsif ($op eq "add-branch-item") {
290 my $itemtype = $input->param('itemtype');
291 my $holdallowed = $input->param('holdallowed');
292 my $returnbranch = $input->param('returnbranch');
293 $holdallowed =~ s/\s//g;
294 $holdallowed = undef if $holdallowed !~ /^\d+/;
296 if ($branch eq "*") {
297 if ($itemtype eq "*") {
298 my $sth_search = $dbh->prepare("SELECT count(*) AS total
299 FROM default_circ_rules");
300 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
301 (holdallowed, returnbranch)
302 VALUES (?, ?)");
303 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
304 SET holdallowed = ?, returnbranch = ?");
306 $sth_search->execute();
307 my $res = $sth_search->fetchrow_hashref();
308 if ($res->{total}) {
309 $sth_update->execute($holdallowed, $returnbranch);
310 } else {
311 $sth_insert->execute($holdallowed, $returnbranch);
313 } else {
314 my $sth_search = $dbh->prepare("SELECT count(*) AS total
315 FROM default_branch_item_rules
316 WHERE itemtype = ?");
317 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
318 (itemtype, holdallowed, returnbranch)
319 VALUES (?, ?, ?)");
320 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
321 SET holdallowed = ?, returnbranch = ?
322 WHERE itemtype = ?");
323 $sth_search->execute($itemtype);
324 my $res = $sth_search->fetchrow_hashref();
325 if ($res->{total}) {
326 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
327 } else {
328 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
331 } elsif ($itemtype eq "*") {
332 my $sth_search = $dbh->prepare("SELECT count(*) AS total
333 FROM default_branch_circ_rules
334 WHERE branchcode = ?");
335 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
336 (branchcode, holdallowed, returnbranch)
337 VALUES (?, ?, ?)");
338 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
339 SET holdallowed = ?, returnbranch = ?
340 WHERE branchcode = ?");
341 $sth_search->execute($branch);
342 my $res = $sth_search->fetchrow_hashref();
343 if ($res->{total}) {
344 $sth_update->execute($holdallowed, $returnbranch, $branch);
345 } else {
346 $sth_insert->execute($branch, $holdallowed, $returnbranch);
348 } else {
349 my $sth_search = $dbh->prepare("SELECT count(*) AS total
350 FROM branch_item_rules
351 WHERE branchcode = ?
352 AND itemtype = ?");
353 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
354 (branchcode, itemtype, holdallowed, returnbranch)
355 VALUES (?, ?, ?, ?)");
356 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
357 SET holdallowed = ?, returnbranch = ?
358 WHERE branchcode = ?
359 AND itemtype = ?");
361 $sth_search->execute($branch, $itemtype);
362 my $res = $sth_search->fetchrow_hashref();
363 if ($res->{total}) {
364 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
365 } else {
366 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
371 my $branches = GetBranches();
372 my @branchloop;
373 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
374 push @branchloop, {
375 value => $thisbranch,
376 selected => $thisbranch eq $branch,
377 branchname => $branches->{$thisbranch}->{'branchname'},
381 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
382 $sth->execute;
383 my @category_loop;
384 while (my $data=$sth->fetchrow_hashref){
385 push @category_loop,$data;
388 $sth->finish;
389 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
390 $sth->execute;
391 # $i=0;
392 my @row_loop;
393 my @itemtypes;
394 while (my $row=$sth->fetchrow_hashref){
395 push @itemtypes,$row;
398 my $sth2 = $dbh->prepare("
399 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
400 FROM issuingrules
401 LEFT JOIN itemtypes
402 ON (itemtypes.itemtype = issuingrules.itemtype)
403 LEFT JOIN categories
404 ON (categories.categorycode = issuingrules.categorycode)
405 WHERE issuingrules.branchcode = ?
407 $sth2->execute($branch);
409 while (my $row = $sth2->fetchrow_hashref) {
410 $row->{'current_branch'} ||= $row->{'branchcode'};
411 $row->{'humanitemtype'} ||= $row->{'itemtype'};
412 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
413 $row->{'humancategorycode'} ||= $row->{'categorycode'};
414 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
415 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
416 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
417 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
418 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
419 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
420 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
421 } else {
422 $row->{'hardduedate'} = 0;
424 push @row_loop, $row;
426 $sth->finish;
428 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
430 my $sth_branch_cat;
431 if ($branch eq "*") {
432 $sth_branch_cat = $dbh->prepare("
433 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
434 FROM default_borrower_circ_rules
435 JOIN categories USING (categorycode)
438 $sth_branch_cat->execute();
439 } else {
440 $sth_branch_cat = $dbh->prepare("
441 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
442 FROM branch_borrower_circ_rules
443 JOIN categories USING (categorycode)
444 WHERE branch_borrower_circ_rules.branchcode = ?
446 $sth_branch_cat->execute($branch);
449 my @branch_cat_rules = ();
450 while (my $row = $sth_branch_cat->fetchrow_hashref) {
451 push @branch_cat_rules, $row;
453 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
455 # note undef maxissueqty so that template can deal with them
456 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
457 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
460 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
462 my $sth_branch_item;
463 if ($branch eq "*") {
464 $sth_branch_item = $dbh->prepare("
465 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
466 FROM default_branch_item_rules
467 JOIN itemtypes USING (itemtype)
469 $sth_branch_item->execute();
470 } else {
471 $sth_branch_item = $dbh->prepare("
472 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
473 FROM branch_item_rules
474 JOIN itemtypes USING (itemtype)
475 WHERE branch_item_rules.branchcode = ?
477 $sth_branch_item->execute($branch);
480 my @branch_item_rules = ();
481 while (my $row = $sth_branch_item->fetchrow_hashref) {
482 push @branch_item_rules, $row;
484 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
486 # note undef holdallowed so that template can deal with them
487 foreach my $entry (@sorted_branch_item_rules) {
488 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
489 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
492 $template->param(show_branch_cat_rule_form => 1);
493 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
494 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
496 my $sth_defaults;
497 if ($branch eq "*") {
498 $sth_defaults = $dbh->prepare("
499 SELECT *
500 FROM default_circ_rules
502 $sth_defaults->execute();
503 } else {
504 $sth_defaults = $dbh->prepare("
505 SELECT *
506 FROM default_branch_circ_rules
507 WHERE branchcode = ?
509 $sth_defaults->execute($branch);
512 my $defaults = $sth_defaults->fetchrow_hashref;
514 if ($defaults) {
515 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
516 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
517 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
518 $template->param(default_maxissueqty => $defaults->{maxissueqty});
519 $template->param(default_returnbranch => $defaults->{returnbranch});
522 $template->param(default_rules => ($defaults ? 1 : 0));
524 $template->param(categoryloop => \@category_loop,
525 itemtypeloop => \@itemtypes,
526 rules => \@sorted_row_loop,
527 branchloop => \@branchloop,
528 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
529 current_branch => $branch,
530 definedbranch => scalar(@sorted_row_loop)>0
532 output_html_with_http_headers $input, $cookie, $template->output;
534 exit 0;
536 # sort by patron category, then item type, putting
537 # default entries at the bottom
538 sub by_category_and_itemtype {
539 unless (by_category($a, $b)) {
540 return by_itemtype($a, $b);
544 sub by_category {
545 my ($a, $b) = @_;
546 if ($a->{'default_humancategorycode'}) {
547 return ($b->{'default_humancategorycode'} ? 0 : 1);
548 } elsif ($b->{'default_humancategorycode'}) {
549 return -1;
550 } else {
551 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
555 sub by_itemtype {
556 my ($a, $b) = @_;
557 if ($a->{'default_humanitemtype'}) {
558 return ($b->{'default_humanitemtype'} ? 0 : 1);
559 } elsif ($b->{'default_humanitemtype'}) {
560 return -1;
561 } else {
562 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};