Bug 11312: make facets selectable using an ID [opac-prog]
[koha.git] / admin / smart-rules.pl
blob5d4166ddaa4640633b8a9299fa0c8f416c1e2412
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, 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=?, renewalperiod=?, 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 $renewalperiod = $input->param('renewalperiod');
117 my $reservesallowed = $input->param('reservesallowed');
118 $maxissueqty =~ s/\s//g;
119 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
120 my $issuelength = $input->param('issuelength');
121 my $lengthunit = $input->param('lengthunit');
122 my $hardduedate = $input->param('hardduedate');
123 $hardduedate = format_date_in_iso($hardduedate);
124 my $hardduedatecompare = $input->param('hardduedatecompare');
125 my $rentaldiscount = $input->param('rentaldiscount');
126 my $overduefinescap = $input->param('overduefinescap') || undef;
127 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
129 $sth_search->execute($br,$bor,$cat);
130 my $res = $sth_search->fetchrow_hashref();
131 if ($res->{total}) {
132 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
133 } else {
134 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
137 elsif ($op eq "set-branch-defaults") {
138 my $categorycode = $input->param('categorycode');
139 my $maxissueqty = $input->param('maxissueqty');
140 my $holdallowed = $input->param('holdallowed');
141 my $returnbranch = $input->param('returnbranch');
142 $maxissueqty =~ s/\s//g;
143 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
144 $holdallowed =~ s/\s//g;
145 $holdallowed = undef if $holdallowed !~ /^\d+/;
147 if ($branch eq "*") {
148 my $sth_search = $dbh->prepare("SELECT count(*) AS total
149 FROM default_circ_rules");
150 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
151 (maxissueqty, holdallowed, returnbranch)
152 VALUES (?, ?, ?)");
153 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
154 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
156 $sth_search->execute();
157 my $res = $sth_search->fetchrow_hashref();
158 if ($res->{total}) {
159 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
160 } else {
161 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
163 } else {
164 my $sth_search = $dbh->prepare("SELECT count(*) AS total
165 FROM default_branch_circ_rules
166 WHERE branchcode = ?");
167 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
168 (branchcode, maxissueqty, holdallowed, returnbranch)
169 VALUES (?, ?, ?, ?)");
170 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
171 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
172 WHERE branchcode = ?");
173 $sth_search->execute($branch);
174 my $res = $sth_search->fetchrow_hashref();
175 if ($res->{total}) {
176 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
177 } else {
178 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
182 elsif ($op eq "add-branch-cat") {
183 my $categorycode = $input->param('categorycode');
184 my $maxissueqty = $input->param('maxissueqty');
185 $maxissueqty =~ s/\s//g;
186 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
188 if ($branch eq "*") {
189 if ($categorycode eq "*") {
190 my $sth_search = $dbh->prepare("SELECT count(*) AS total
191 FROM default_circ_rules");
192 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
193 (maxissueqty)
194 VALUES (?)");
195 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
196 SET maxissueqty = ?");
198 $sth_search->execute();
199 my $res = $sth_search->fetchrow_hashref();
200 if ($res->{total}) {
201 $sth_update->execute($maxissueqty);
202 } else {
203 $sth_insert->execute($maxissueqty);
205 } else {
206 my $sth_search = $dbh->prepare("SELECT count(*) AS total
207 FROM default_borrower_circ_rules
208 WHERE categorycode = ?");
209 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
210 (categorycode, maxissueqty)
211 VALUES (?, ?)");
212 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
213 SET maxissueqty = ?
214 WHERE categorycode = ?");
215 $sth_search->execute($branch);
216 my $res = $sth_search->fetchrow_hashref();
217 if ($res->{total}) {
218 $sth_update->execute($maxissueqty, $categorycode);
219 } else {
220 $sth_insert->execute($categorycode, $maxissueqty);
223 } elsif ($categorycode eq "*") {
224 my $sth_search = $dbh->prepare("SELECT count(*) AS total
225 FROM default_branch_circ_rules
226 WHERE branchcode = ?");
227 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
228 (branchcode, maxissueqty)
229 VALUES (?, ?)");
230 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
231 SET maxissueqty = ?
232 WHERE branchcode = ?");
233 $sth_search->execute($branch);
234 my $res = $sth_search->fetchrow_hashref();
235 if ($res->{total}) {
236 $sth_update->execute($maxissueqty, $branch);
237 } else {
238 $sth_insert->execute($branch, $maxissueqty);
240 } else {
241 my $sth_search = $dbh->prepare("SELECT count(*) AS total
242 FROM branch_borrower_circ_rules
243 WHERE branchcode = ?
244 AND categorycode = ?");
245 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
246 (branchcode, categorycode, maxissueqty)
247 VALUES (?, ?, ?)");
248 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
249 SET maxissueqty = ?
250 WHERE branchcode = ?
251 AND categorycode = ?");
253 $sth_search->execute($branch, $categorycode);
254 my $res = $sth_search->fetchrow_hashref();
255 if ($res->{total}) {
256 $sth_update->execute($maxissueqty, $branch, $categorycode);
257 } else {
258 $sth_insert->execute($branch, $categorycode, $maxissueqty);
262 elsif ($op eq "add-branch-item") {
263 my $itemtype = $input->param('itemtype');
264 my $holdallowed = $input->param('holdallowed');
265 my $returnbranch = $input->param('returnbranch');
266 $holdallowed =~ s/\s//g;
267 $holdallowed = undef if $holdallowed !~ /^\d+/;
269 if ($branch eq "*") {
270 if ($itemtype eq "*") {
271 my $sth_search = $dbh->prepare("SELECT count(*) AS total
272 FROM default_circ_rules");
273 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
274 (holdallowed, returnbranch)
275 VALUES (?, ?)");
276 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
277 SET holdallowed = ?, returnbranch = ?");
279 $sth_search->execute();
280 my $res = $sth_search->fetchrow_hashref();
281 if ($res->{total}) {
282 $sth_update->execute($holdallowed, $returnbranch);
283 } else {
284 $sth_insert->execute($holdallowed, $returnbranch);
286 } else {
287 my $sth_search = $dbh->prepare("SELECT count(*) AS total
288 FROM default_branch_item_rules
289 WHERE itemtype = ?");
290 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
291 (itemtype, holdallowed, returnbranch)
292 VALUES (?, ?, ?)");
293 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
294 SET holdallowed = ?, returnbranch = ?
295 WHERE itemtype = ?");
296 $sth_search->execute($itemtype);
297 my $res = $sth_search->fetchrow_hashref();
298 if ($res->{total}) {
299 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
300 } else {
301 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
304 } elsif ($itemtype eq "*") {
305 my $sth_search = $dbh->prepare("SELECT count(*) AS total
306 FROM default_branch_circ_rules
307 WHERE branchcode = ?");
308 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
309 (branchcode, holdallowed, returnbranch)
310 VALUES (?, ?, ?)");
311 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
312 SET holdallowed = ?, returnbranch = ?
313 WHERE branchcode = ?");
314 $sth_search->execute($branch);
315 my $res = $sth_search->fetchrow_hashref();
316 if ($res->{total}) {
317 $sth_update->execute($holdallowed, $returnbranch, $branch);
318 } else {
319 $sth_insert->execute($branch, $holdallowed, $returnbranch);
321 } else {
322 my $sth_search = $dbh->prepare("SELECT count(*) AS total
323 FROM branch_item_rules
324 WHERE branchcode = ?
325 AND itemtype = ?");
326 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
327 (branchcode, itemtype, holdallowed, returnbranch)
328 VALUES (?, ?, ?, ?)");
329 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
330 SET holdallowed = ?, returnbranch = ?
331 WHERE branchcode = ?
332 AND itemtype = ?");
334 $sth_search->execute($branch, $itemtype);
335 my $res = $sth_search->fetchrow_hashref();
336 if ($res->{total}) {
337 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
338 } else {
339 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
344 my $branches = GetBranches();
345 my @branchloop;
346 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
347 push @branchloop, {
348 value => $thisbranch,
349 selected => $thisbranch eq $branch,
350 branchname => $branches->{$thisbranch}->{'branchname'},
354 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
355 $sth->execute;
356 my @category_loop;
357 while (my $data=$sth->fetchrow_hashref){
358 push @category_loop,$data;
361 $sth->finish;
362 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
363 $sth->execute;
364 # $i=0;
365 my @row_loop;
366 my @itemtypes;
367 while (my $row=$sth->fetchrow_hashref){
368 push @itemtypes,$row;
371 my $sth2 = $dbh->prepare("
372 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
373 FROM issuingrules
374 LEFT JOIN itemtypes
375 ON (itemtypes.itemtype = issuingrules.itemtype)
376 LEFT JOIN categories
377 ON (categories.categorycode = issuingrules.categorycode)
378 WHERE issuingrules.branchcode = ?
380 $sth2->execute($branch);
382 while (my $row = $sth2->fetchrow_hashref) {
383 $row->{'current_branch'} ||= $row->{'branchcode'};
384 $row->{'humanitemtype'} ||= $row->{'itemtype'};
385 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
386 $row->{'humancategorycode'} ||= $row->{'categorycode'};
387 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
388 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
389 if ($row->{'hardduedate'} ne '0000-00-00') {
390 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
391 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
392 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
393 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
394 } else {
395 $row->{'hardduedate'} = 0;
397 push @row_loop, $row;
399 $sth->finish;
401 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
403 my $sth_branch_cat;
404 if ($branch eq "*") {
405 $sth_branch_cat = $dbh->prepare("
406 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
407 FROM default_borrower_circ_rules
408 JOIN categories USING (categorycode)
411 $sth_branch_cat->execute();
412 } else {
413 $sth_branch_cat = $dbh->prepare("
414 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
415 FROM branch_borrower_circ_rules
416 JOIN categories USING (categorycode)
417 WHERE branch_borrower_circ_rules.branchcode = ?
419 $sth_branch_cat->execute($branch);
422 my @branch_cat_rules = ();
423 while (my $row = $sth_branch_cat->fetchrow_hashref) {
424 push @branch_cat_rules, $row;
426 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
428 # note undef maxissueqty so that template can deal with them
429 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
430 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
433 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
435 my $sth_branch_item;
436 if ($branch eq "*") {
437 $sth_branch_item = $dbh->prepare("
438 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
439 FROM default_branch_item_rules
440 JOIN itemtypes USING (itemtype)
442 $sth_branch_item->execute();
443 } else {
444 $sth_branch_item = $dbh->prepare("
445 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
446 FROM branch_item_rules
447 JOIN itemtypes USING (itemtype)
448 WHERE branch_item_rules.branchcode = ?
450 $sth_branch_item->execute($branch);
453 my @branch_item_rules = ();
454 while (my $row = $sth_branch_item->fetchrow_hashref) {
455 push @branch_item_rules, $row;
457 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
459 # note undef holdallowed so that template can deal with them
460 foreach my $entry (@sorted_branch_item_rules) {
461 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
462 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
465 $template->param(show_branch_cat_rule_form => 1);
466 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
467 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
469 my $sth_defaults;
470 if ($branch eq "*") {
471 $sth_defaults = $dbh->prepare("
472 SELECT *
473 FROM default_circ_rules
475 $sth_defaults->execute();
476 } else {
477 $sth_defaults = $dbh->prepare("
478 SELECT *
479 FROM default_branch_circ_rules
480 WHERE branchcode = ?
482 $sth_defaults->execute($branch);
485 my $defaults = $sth_defaults->fetchrow_hashref;
487 if ($defaults) {
488 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
489 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
490 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
491 $template->param(default_maxissueqty => $defaults->{maxissueqty});
492 $template->param(default_returnbranch => $defaults->{returnbranch});
495 $template->param(default_rules => ($defaults ? 1 : 0));
497 $template->param(categoryloop => \@category_loop,
498 itemtypeloop => \@itemtypes,
499 rules => \@sorted_row_loop,
500 branchloop => \@branchloop,
501 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
502 current_branch => $branch,
503 definedbranch => scalar(@sorted_row_loop)>0
505 output_html_with_http_headers $input, $cookie, $template->output;
507 exit 0;
509 # sort by patron category, then item type, putting
510 # default entries at the bottom
511 sub by_category_and_itemtype {
512 unless (by_category($a, $b)) {
513 return by_itemtype($a, $b);
517 sub by_category {
518 my ($a, $b) = @_;
519 if ($a->{'default_humancategorycode'}) {
520 return ($b->{'default_humancategorycode'} ? 0 : 1);
521 } elsif ($b->{'default_humancategorycode'}) {
522 return -1;
523 } else {
524 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
528 sub by_itemtype {
529 my ($a, $b) = @_;
530 if ($a->{'default_humanitemtype'}) {
531 return ($b->{'default_humanitemtype'} ? 0 : 1);
532 } elsif ($b->{'default_humanitemtype'}) {
533 return -1;
534 } else {
535 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};