Bug 9454: Use placeholders when adding basket
[koha.git] / admin / smart-rules.pl
blobc221d7107a8dcef76bf4fee022d4d758f320a4e6
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, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, firstremind, chargeperiod,rentaldiscount, overduefinescap) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
105 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, 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 $firstremind = $input->param('firstremind');
113 my $chargeperiod = $input->param('chargeperiod');
114 my $maxissueqty = $input->param('maxissueqty');
115 my $renewalsallowed = $input->param('renewalsallowed');
116 my $reservesallowed = $input->param('reservesallowed');
117 $maxissueqty =~ s/\s//g;
118 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
119 my $issuelength = $input->param('issuelength');
120 my $lengthunit = $input->param('lengthunit');
121 my $hardduedate = $input->param('hardduedate');
122 $hardduedate = format_date_in_iso($hardduedate);
123 my $hardduedatecompare = $input->param('hardduedatecompare');
124 my $rentaldiscount = $input->param('rentaldiscount');
125 my $overduefinescap = $input->param('overduefinescap') || undef;
126 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
128 $sth_search->execute($br,$bor,$cat);
129 my $res = $sth_search->fetchrow_hashref();
130 if ($res->{total}) {
131 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
132 } else {
133 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
136 elsif ($op eq "set-branch-defaults") {
137 my $categorycode = $input->param('categorycode');
138 my $maxissueqty = $input->param('maxissueqty');
139 my $holdallowed = $input->param('holdallowed');
140 my $returnbranch = $input->param('returnbranch');
141 $maxissueqty =~ s/\s//g;
142 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
143 $holdallowed =~ s/\s//g;
144 $holdallowed = undef if $holdallowed !~ /^\d+/;
146 if ($branch eq "*") {
147 my $sth_search = $dbh->prepare("SELECT count(*) AS total
148 FROM default_circ_rules");
149 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
150 (maxissueqty, holdallowed, returnbranch)
151 VALUES (?, ?, ?)");
152 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
153 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
155 $sth_search->execute();
156 my $res = $sth_search->fetchrow_hashref();
157 if ($res->{total}) {
158 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
159 } else {
160 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
162 } else {
163 my $sth_search = $dbh->prepare("SELECT count(*) AS total
164 FROM default_branch_circ_rules
165 WHERE branchcode = ?");
166 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
167 (branchcode, maxissueqty, holdallowed, returnbranch)
168 VALUES (?, ?, ?, ?)");
169 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
170 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
171 WHERE branchcode = ?");
172 $sth_search->execute($branch);
173 my $res = $sth_search->fetchrow_hashref();
174 if ($res->{total}) {
175 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
176 } else {
177 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
181 elsif ($op eq "add-branch-cat") {
182 my $categorycode = $input->param('categorycode');
183 my $maxissueqty = $input->param('maxissueqty');
184 $maxissueqty =~ s/\s//g;
185 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
187 if ($branch eq "*") {
188 if ($categorycode eq "*") {
189 my $sth_search = $dbh->prepare("SELECT count(*) AS total
190 FROM default_circ_rules");
191 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
192 (maxissueqty)
193 VALUES (?)");
194 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
195 SET maxissueqty = ?");
197 $sth_search->execute();
198 my $res = $sth_search->fetchrow_hashref();
199 if ($res->{total}) {
200 $sth_update->execute($maxissueqty);
201 } else {
202 $sth_insert->execute($maxissueqty);
204 } else {
205 my $sth_search = $dbh->prepare("SELECT count(*) AS total
206 FROM default_borrower_circ_rules
207 WHERE categorycode = ?");
208 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
209 (categorycode, maxissueqty)
210 VALUES (?, ?)");
211 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
212 SET maxissueqty = ?
213 WHERE categorycode = ?");
214 $sth_search->execute($branch);
215 my $res = $sth_search->fetchrow_hashref();
216 if ($res->{total}) {
217 $sth_update->execute($maxissueqty, $categorycode);
218 } else {
219 $sth_insert->execute($categorycode, $maxissueqty);
222 } elsif ($categorycode eq "*") {
223 my $sth_search = $dbh->prepare("SELECT count(*) AS total
224 FROM default_branch_circ_rules
225 WHERE branchcode = ?");
226 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
227 (branchcode, maxissueqty)
228 VALUES (?, ?)");
229 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
230 SET maxissueqty = ?
231 WHERE branchcode = ?");
232 $sth_search->execute($branch);
233 my $res = $sth_search->fetchrow_hashref();
234 if ($res->{total}) {
235 $sth_update->execute($maxissueqty, $branch);
236 } else {
237 $sth_insert->execute($branch, $maxissueqty);
239 } else {
240 my $sth_search = $dbh->prepare("SELECT count(*) AS total
241 FROM branch_borrower_circ_rules
242 WHERE branchcode = ?
243 AND categorycode = ?");
244 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
245 (branchcode, categorycode, maxissueqty)
246 VALUES (?, ?, ?)");
247 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
248 SET maxissueqty = ?
249 WHERE branchcode = ?
250 AND categorycode = ?");
252 $sth_search->execute($branch, $categorycode);
253 my $res = $sth_search->fetchrow_hashref();
254 if ($res->{total}) {
255 $sth_update->execute($maxissueqty, $branch, $categorycode);
256 } else {
257 $sth_insert->execute($branch, $categorycode, $maxissueqty);
261 elsif ($op eq "add-branch-item") {
262 my $itemtype = $input->param('itemtype');
263 my $holdallowed = $input->param('holdallowed');
264 my $returnbranch = $input->param('returnbranch');
265 $holdallowed =~ s/\s//g;
266 $holdallowed = undef if $holdallowed !~ /^\d+/;
268 if ($branch eq "*") {
269 if ($itemtype eq "*") {
270 my $sth_search = $dbh->prepare("SELECT count(*) AS total
271 FROM default_circ_rules");
272 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
273 (holdallowed, returnbranch)
274 VALUES (?, ?)");
275 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
276 SET holdallowed = ?, returnbranch = ?");
278 $sth_search->execute();
279 my $res = $sth_search->fetchrow_hashref();
280 if ($res->{total}) {
281 $sth_update->execute($holdallowed, $returnbranch);
282 } else {
283 $sth_insert->execute($holdallowed, $returnbranch);
285 } else {
286 my $sth_search = $dbh->prepare("SELECT count(*) AS total
287 FROM default_branch_item_rules
288 WHERE itemtype = ?");
289 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
290 (itemtype, holdallowed, returnbranch)
291 VALUES (?, ?, ?)");
292 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
293 SET holdallowed = ?, returnbranch = ?
294 WHERE itemtype = ?");
295 $sth_search->execute($itemtype);
296 my $res = $sth_search->fetchrow_hashref();
297 if ($res->{total}) {
298 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
299 } else {
300 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
303 } elsif ($itemtype eq "*") {
304 my $sth_search = $dbh->prepare("SELECT count(*) AS total
305 FROM default_branch_circ_rules
306 WHERE branchcode = ?");
307 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
308 (branchcode, holdallowed, returnbranch)
309 VALUES (?, ?, ?)");
310 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
311 SET holdallowed = ?, returnbranch = ?
312 WHERE branchcode = ?");
313 $sth_search->execute($branch);
314 my $res = $sth_search->fetchrow_hashref();
315 if ($res->{total}) {
316 $sth_update->execute($holdallowed, $returnbranch, $branch);
317 } else {
318 $sth_insert->execute($branch, $holdallowed, $returnbranch);
320 } else {
321 my $sth_search = $dbh->prepare("SELECT count(*) AS total
322 FROM branch_item_rules
323 WHERE branchcode = ?
324 AND itemtype = ?");
325 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
326 (branchcode, itemtype, holdallowed, returnbranch)
327 VALUES (?, ?, ?, ?)");
328 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
329 SET holdallowed = ?, returnbranch = ?
330 WHERE branchcode = ?
331 AND itemtype = ?");
333 $sth_search->execute($branch, $itemtype);
334 my $res = $sth_search->fetchrow_hashref();
335 if ($res->{total}) {
336 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
337 } else {
338 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
343 my $branches = GetBranches();
344 my @branchloop;
345 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
346 push @branchloop, {
347 value => $thisbranch,
348 selected => $thisbranch eq $branch,
349 branchname => $branches->{$thisbranch}->{'branchname'},
353 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
354 $sth->execute;
355 my @category_loop;
356 while (my $data=$sth->fetchrow_hashref){
357 push @category_loop,$data;
360 $sth->finish;
361 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
362 $sth->execute;
363 # $i=0;
364 my @row_loop;
365 my @itemtypes;
366 while (my $row=$sth->fetchrow_hashref){
367 push @itemtypes,$row;
370 my $sth2 = $dbh->prepare("
371 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
372 FROM issuingrules
373 LEFT JOIN itemtypes
374 ON (itemtypes.itemtype = issuingrules.itemtype)
375 LEFT JOIN categories
376 ON (categories.categorycode = issuingrules.categorycode)
377 WHERE issuingrules.branchcode = ?
379 $sth2->execute($branch);
381 while (my $row = $sth2->fetchrow_hashref) {
382 $row->{'current_branch'} ||= $row->{'branchcode'};
383 $row->{'humanitemtype'} ||= $row->{'itemtype'};
384 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
385 $row->{'humancategorycode'} ||= $row->{'categorycode'};
386 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
387 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
388 if ($row->{'hardduedate'} ne '0000-00-00') {
389 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
390 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
391 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
392 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
393 } else {
394 $row->{'hardduedate'} = 0;
396 push @row_loop, $row;
398 $sth->finish;
400 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
402 my $sth_branch_cat;
403 if ($branch eq "*") {
404 $sth_branch_cat = $dbh->prepare("
405 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
406 FROM default_borrower_circ_rules
407 JOIN categories USING (categorycode)
410 $sth_branch_cat->execute();
411 } else {
412 $sth_branch_cat = $dbh->prepare("
413 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
414 FROM branch_borrower_circ_rules
415 JOIN categories USING (categorycode)
416 WHERE branch_borrower_circ_rules.branchcode = ?
418 $sth_branch_cat->execute($branch);
421 my @branch_cat_rules = ();
422 while (my $row = $sth_branch_cat->fetchrow_hashref) {
423 push @branch_cat_rules, $row;
425 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
427 # note undef maxissueqty so that template can deal with them
428 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
429 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
432 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
434 my $sth_branch_item;
435 if ($branch eq "*") {
436 $sth_branch_item = $dbh->prepare("
437 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
438 FROM default_branch_item_rules
439 JOIN itemtypes USING (itemtype)
441 $sth_branch_item->execute();
442 } else {
443 $sth_branch_item = $dbh->prepare("
444 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
445 FROM branch_item_rules
446 JOIN itemtypes USING (itemtype)
447 WHERE branch_item_rules.branchcode = ?
449 $sth_branch_item->execute($branch);
452 my @branch_item_rules = ();
453 while (my $row = $sth_branch_item->fetchrow_hashref) {
454 push @branch_item_rules, $row;
456 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
458 # note undef holdallowed so that template can deal with them
459 foreach my $entry (@sorted_branch_item_rules) {
460 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
461 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
464 $template->param(show_branch_cat_rule_form => 1);
465 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
466 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
468 my $sth_defaults;
469 if ($branch eq "*") {
470 $sth_defaults = $dbh->prepare("
471 SELECT *
472 FROM default_circ_rules
474 $sth_defaults->execute();
475 } else {
476 $sth_defaults = $dbh->prepare("
477 SELECT *
478 FROM default_branch_circ_rules
479 WHERE branchcode = ?
481 $sth_defaults->execute($branch);
484 my $defaults = $sth_defaults->fetchrow_hashref;
486 if ($defaults) {
487 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
488 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
489 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
490 $template->param(default_maxissueqty => $defaults->{maxissueqty});
491 $template->param(default_returnbranch => $defaults->{returnbranch});
494 $template->param(default_rules => ($defaults ? 1 : 0));
496 $template->param(categoryloop => \@category_loop,
497 itemtypeloop => \@itemtypes,
498 rules => \@sorted_row_loop,
499 branchloop => \@branchloop,
500 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
501 current_branch => $branch,
502 definedbranch => scalar(@sorted_row_loop)>0
504 output_html_with_http_headers $input, $cookie, $template->output;
506 exit 0;
508 # sort by patron category, then item type, putting
509 # default entries at the bottom
510 sub by_category_and_itemtype {
511 unless (by_category($a, $b)) {
512 return by_itemtype($a, $b);
516 sub by_category {
517 my ($a, $b) = @_;
518 if ($a->{'default_humancategorycode'}) {
519 return ($b->{'default_humancategorycode'} ? 0 : 1);
520 } elsif ($b->{'default_humancategorycode'}) {
521 return -1;
522 } else {
523 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
527 sub by_itemtype {
528 my ($a, $b) = @_;
529 if ($a->{'default_humanitemtype'}) {
530 return ($b->{'default_humanitemtype'} ? 0 : 1);
531 } elsif ($b->{'default_humanitemtype'}) {
532 return -1;
533 } else {
534 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};