Bug 12901: (consistency followup) add buildBiblioDefaultViewURL to MARC21 XSLT
[koha.git] / admin / smart-rules.pl
blobeb3e8757d54252387290603c6aa8cebe795b18ff
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.tt",
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, auto_renew, 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=?, auto_renew=?, 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 $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
122 my $reservesallowed = $input->param('reservesallowed');
123 $maxissueqty =~ s/\s//g;
124 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
125 my $issuelength = $input->param('issuelength');
126 my $lengthunit = $input->param('lengthunit');
127 my $hardduedate = $input->param('hardduedate');
128 $hardduedate = format_date_in_iso($hardduedate);
129 my $hardduedatecompare = $input->param('hardduedatecompare');
130 my $rentaldiscount = $input->param('rentaldiscount');
131 my $overduefinescap = $input->param('overduefinescap') || undef;
132 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
134 $sth_search->execute($br,$bor,$cat);
135 my $res = $sth_search->fetchrow_hashref();
136 if ($res->{total}) {
137 $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
138 } else {
139 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
142 elsif ($op eq "set-branch-defaults") {
143 my $categorycode = $input->param('categorycode');
144 my $maxissueqty = $input->param('maxissueqty');
145 my $holdallowed = $input->param('holdallowed');
146 my $returnbranch = $input->param('returnbranch');
147 $maxissueqty =~ s/\s//g;
148 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
149 $holdallowed =~ s/\s//g;
150 $holdallowed = undef if $holdallowed !~ /^\d+/;
152 if ($branch eq "*") {
153 my $sth_search = $dbh->prepare("SELECT count(*) AS total
154 FROM default_circ_rules");
155 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
156 (maxissueqty, holdallowed, returnbranch)
157 VALUES (?, ?, ?)");
158 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
159 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
161 $sth_search->execute();
162 my $res = $sth_search->fetchrow_hashref();
163 if ($res->{total}) {
164 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
165 } else {
166 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
168 } else {
169 my $sth_search = $dbh->prepare("SELECT count(*) AS total
170 FROM default_branch_circ_rules
171 WHERE branchcode = ?");
172 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
173 (branchcode, maxissueqty, holdallowed, returnbranch)
174 VALUES (?, ?, ?, ?)");
175 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
176 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
177 WHERE branchcode = ?");
178 $sth_search->execute($branch);
179 my $res = $sth_search->fetchrow_hashref();
180 if ($res->{total}) {
181 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
182 } else {
183 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
187 elsif ($op eq "add-branch-cat") {
188 my $categorycode = $input->param('categorycode');
189 my $maxissueqty = $input->param('maxissueqty');
190 $maxissueqty =~ s/\s//g;
191 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
193 if ($branch eq "*") {
194 if ($categorycode eq "*") {
195 my $sth_search = $dbh->prepare("SELECT count(*) AS total
196 FROM default_circ_rules");
197 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
198 (maxissueqty)
199 VALUES (?)");
200 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
201 SET maxissueqty = ?");
203 $sth_search->execute();
204 my $res = $sth_search->fetchrow_hashref();
205 if ($res->{total}) {
206 $sth_update->execute($maxissueqty);
207 } else {
208 $sth_insert->execute($maxissueqty);
210 } else {
211 my $sth_search = $dbh->prepare("SELECT count(*) AS total
212 FROM default_borrower_circ_rules
213 WHERE categorycode = ?");
214 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
215 (categorycode, maxissueqty)
216 VALUES (?, ?)");
217 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
218 SET maxissueqty = ?
219 WHERE categorycode = ?");
220 $sth_search->execute($branch);
221 my $res = $sth_search->fetchrow_hashref();
222 if ($res->{total}) {
223 $sth_update->execute($maxissueqty, $categorycode);
224 } else {
225 $sth_insert->execute($categorycode, $maxissueqty);
228 } elsif ($categorycode eq "*") {
229 my $sth_search = $dbh->prepare("SELECT count(*) AS total
230 FROM default_branch_circ_rules
231 WHERE branchcode = ?");
232 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
233 (branchcode, maxissueqty)
234 VALUES (?, ?)");
235 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
236 SET maxissueqty = ?
237 WHERE branchcode = ?");
238 $sth_search->execute($branch);
239 my $res = $sth_search->fetchrow_hashref();
240 if ($res->{total}) {
241 $sth_update->execute($maxissueqty, $branch);
242 } else {
243 $sth_insert->execute($branch, $maxissueqty);
245 } else {
246 my $sth_search = $dbh->prepare("SELECT count(*) AS total
247 FROM branch_borrower_circ_rules
248 WHERE branchcode = ?
249 AND categorycode = ?");
250 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
251 (branchcode, categorycode, maxissueqty)
252 VALUES (?, ?, ?)");
253 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
254 SET maxissueqty = ?
255 WHERE branchcode = ?
256 AND categorycode = ?");
258 $sth_search->execute($branch, $categorycode);
259 my $res = $sth_search->fetchrow_hashref();
260 if ($res->{total}) {
261 $sth_update->execute($maxissueqty, $branch, $categorycode);
262 } else {
263 $sth_insert->execute($branch, $categorycode, $maxissueqty);
267 elsif ($op eq "add-branch-item") {
268 my $itemtype = $input->param('itemtype');
269 my $holdallowed = $input->param('holdallowed');
270 my $returnbranch = $input->param('returnbranch');
271 $holdallowed =~ s/\s//g;
272 $holdallowed = undef if $holdallowed !~ /^\d+/;
274 if ($branch eq "*") {
275 if ($itemtype eq "*") {
276 my $sth_search = $dbh->prepare("SELECT count(*) AS total
277 FROM default_circ_rules");
278 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
279 (holdallowed, returnbranch)
280 VALUES (?, ?)");
281 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
282 SET holdallowed = ?, returnbranch = ?");
284 $sth_search->execute();
285 my $res = $sth_search->fetchrow_hashref();
286 if ($res->{total}) {
287 $sth_update->execute($holdallowed, $returnbranch);
288 } else {
289 $sth_insert->execute($holdallowed, $returnbranch);
291 } else {
292 my $sth_search = $dbh->prepare("SELECT count(*) AS total
293 FROM default_branch_item_rules
294 WHERE itemtype = ?");
295 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
296 (itemtype, holdallowed, returnbranch)
297 VALUES (?, ?, ?)");
298 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
299 SET holdallowed = ?, returnbranch = ?
300 WHERE itemtype = ?");
301 $sth_search->execute($itemtype);
302 my $res = $sth_search->fetchrow_hashref();
303 if ($res->{total}) {
304 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
305 } else {
306 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
309 } elsif ($itemtype eq "*") {
310 my $sth_search = $dbh->prepare("SELECT count(*) AS total
311 FROM default_branch_circ_rules
312 WHERE branchcode = ?");
313 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
314 (branchcode, holdallowed, returnbranch)
315 VALUES (?, ?, ?)");
316 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
317 SET holdallowed = ?, returnbranch = ?
318 WHERE branchcode = ?");
319 $sth_search->execute($branch);
320 my $res = $sth_search->fetchrow_hashref();
321 if ($res->{total}) {
322 $sth_update->execute($holdallowed, $returnbranch, $branch);
323 } else {
324 $sth_insert->execute($branch, $holdallowed, $returnbranch);
326 } else {
327 my $sth_search = $dbh->prepare("SELECT count(*) AS total
328 FROM branch_item_rules
329 WHERE branchcode = ?
330 AND itemtype = ?");
331 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
332 (branchcode, itemtype, holdallowed, returnbranch)
333 VALUES (?, ?, ?, ?)");
334 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
335 SET holdallowed = ?, returnbranch = ?
336 WHERE branchcode = ?
337 AND itemtype = ?");
339 $sth_search->execute($branch, $itemtype);
340 my $res = $sth_search->fetchrow_hashref();
341 if ($res->{total}) {
342 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
343 } else {
344 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
349 my $branches = GetBranches();
350 my @branchloop;
351 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
352 push @branchloop, {
353 value => $thisbranch,
354 selected => $thisbranch eq $branch,
355 branchname => $branches->{$thisbranch}->{'branchname'},
359 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
360 $sth->execute;
361 my @category_loop;
362 while (my $data=$sth->fetchrow_hashref){
363 push @category_loop,$data;
366 $sth->finish;
367 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
368 $sth->execute;
369 # $i=0;
370 my @row_loop;
371 my @itemtypes;
372 while (my $row=$sth->fetchrow_hashref){
373 push @itemtypes,$row;
376 my $sth2 = $dbh->prepare("
377 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
378 FROM issuingrules
379 LEFT JOIN itemtypes
380 ON (itemtypes.itemtype = issuingrules.itemtype)
381 LEFT JOIN categories
382 ON (categories.categorycode = issuingrules.categorycode)
383 WHERE issuingrules.branchcode = ?
385 $sth2->execute($branch);
387 while (my $row = $sth2->fetchrow_hashref) {
388 $row->{'current_branch'} ||= $row->{'branchcode'};
389 $row->{'humanitemtype'} ||= $row->{'itemtype'};
390 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
391 $row->{'humancategorycode'} ||= $row->{'categorycode'};
392 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
393 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
394 if ($row->{'hardduedate'} ne '0000-00-00') {
395 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
396 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
397 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
398 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
399 } else {
400 $row->{'hardduedate'} = 0;
402 push @row_loop, $row;
404 $sth->finish;
406 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
408 my $sth_branch_cat;
409 if ($branch eq "*") {
410 $sth_branch_cat = $dbh->prepare("
411 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
412 FROM default_borrower_circ_rules
413 JOIN categories USING (categorycode)
416 $sth_branch_cat->execute();
417 } else {
418 $sth_branch_cat = $dbh->prepare("
419 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
420 FROM branch_borrower_circ_rules
421 JOIN categories USING (categorycode)
422 WHERE branch_borrower_circ_rules.branchcode = ?
424 $sth_branch_cat->execute($branch);
427 my @branch_cat_rules = ();
428 while (my $row = $sth_branch_cat->fetchrow_hashref) {
429 push @branch_cat_rules, $row;
431 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
433 # note undef maxissueqty so that template can deal with them
434 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
435 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
438 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
440 my $sth_branch_item;
441 if ($branch eq "*") {
442 $sth_branch_item = $dbh->prepare("
443 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
444 FROM default_branch_item_rules
445 JOIN itemtypes USING (itemtype)
447 $sth_branch_item->execute();
448 } else {
449 $sth_branch_item = $dbh->prepare("
450 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
451 FROM branch_item_rules
452 JOIN itemtypes USING (itemtype)
453 WHERE branch_item_rules.branchcode = ?
455 $sth_branch_item->execute($branch);
458 my @branch_item_rules = ();
459 while (my $row = $sth_branch_item->fetchrow_hashref) {
460 push @branch_item_rules, $row;
462 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
464 # note undef holdallowed so that template can deal with them
465 foreach my $entry (@sorted_branch_item_rules) {
466 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
467 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
470 $template->param(show_branch_cat_rule_form => 1);
471 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
472 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
474 my $sth_defaults;
475 if ($branch eq "*") {
476 $sth_defaults = $dbh->prepare("
477 SELECT *
478 FROM default_circ_rules
480 $sth_defaults->execute();
481 } else {
482 $sth_defaults = $dbh->prepare("
483 SELECT *
484 FROM default_branch_circ_rules
485 WHERE branchcode = ?
487 $sth_defaults->execute($branch);
490 my $defaults = $sth_defaults->fetchrow_hashref;
492 if ($defaults) {
493 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
494 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
495 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
496 $template->param(default_maxissueqty => $defaults->{maxissueqty});
497 $template->param(default_returnbranch => $defaults->{returnbranch});
500 $template->param(default_rules => ($defaults ? 1 : 0));
502 $template->param(categoryloop => \@category_loop,
503 itemtypeloop => \@itemtypes,
504 rules => \@sorted_row_loop,
505 branchloop => \@branchloop,
506 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
507 current_branch => $branch,
508 definedbranch => scalar(@sorted_row_loop)>0
510 output_html_with_http_headers $input, $cookie, $template->output;
512 exit 0;
514 # sort by patron category, then item type, putting
515 # default entries at the bottom
516 sub by_category_and_itemtype {
517 unless (by_category($a, $b)) {
518 return by_itemtype($a, $b);
522 sub by_category {
523 my ($a, $b) = @_;
524 if ($a->{'default_humancategorycode'}) {
525 return ($b->{'default_humancategorycode'} ? 0 : 1);
526 } elsif ($b->{'default_humancategorycode'}) {
527 return -1;
528 } else {
529 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
533 sub by_itemtype {
534 my ($a, $b) = @_;
535 if ($a->{'default_humanitemtype'}) {
536 return ($b->{'default_humanitemtype'} ? 0 : 1);
537 } elsif ($b->{'default_humanitemtype'}) {
538 return -1;
539 } else {
540 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};