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
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.
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
32 my $dbh = C4::Context->dbh;
35 # $flagsrequired->{circulation}=1;
36 my ($template, $loggedinuser, $cookie)
37 = get_template_and_user({template_name => "admin/smart-rules.tt",
41 flagsrequired => {parameters => 'manage_circ_rules'},
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');
60 if ($categorycode eq "*") {
61 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
62 $sth_delete->execute();
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);
73 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
75 AND categorycode = ?");
76 $sth_delete->execute($branch, $categorycode);
79 elsif ($op eq 'delete-branch-item') {
80 my $itemtype = $input->param('itemtype');
82 if ($itemtype eq "*") {
83 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
84 $sth_delete->execute();
86 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
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);
95 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
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, norenewalbefore, auto_renew, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, maxsuspensiondays, firstremind, chargeperiod,rentaldiscount, onshelfholds, opacitemholds, 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=?, onshelfholds=?, opacitemholds=?, overduefinescap=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
107 my $br = $branch; # branch
108 my $bor = $input->param('categorycode'); # borrower category
109 my $itemtype = $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 my $onshelfholds = $input->param('onshelfholds') || 0;
124 $maxissueqty =~ s/\s//g;
125 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
126 my $issuelength = $input->param('issuelength');
127 my $lengthunit = $input->param('lengthunit');
128 my $hardduedate = $input->param('hardduedate');
129 $hardduedate = format_date_in_iso($hardduedate);
130 my $hardduedatecompare = $input->param('hardduedatecompare');
131 my $rentaldiscount = $input->param('rentaldiscount');
132 my $opacitemholds = $input->param('opacitemholds') || 0;
133 my $overduefinescap = $input->param('overduefinescap') || undef;
134 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty";
136 $sth_search->execute($br,$bor,$itemtype);
137 my $res = $sth_search->fetchrow_hashref();
139 $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount, $onshelfholds, $opacitemholds, $overduefinescap, $br,$bor,$itemtype);
141 $sth_insert->execute($br,$bor,$itemtype,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$onshelfholds,$opacitemholds,$overduefinescap);
144 elsif ($op eq "set-branch-defaults") {
145 my $categorycode = $input->param('categorycode');
146 my $maxissueqty = $input->param('maxissueqty');
147 my $holdallowed = $input->param('holdallowed');
148 my $returnbranch = $input->param('returnbranch');
149 $maxissueqty =~ s/\s//g;
150 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
151 $holdallowed =~ s/\s//g;
152 $holdallowed = undef if $holdallowed !~ /^\d+/;
154 if ($branch eq "*") {
155 my $sth_search = $dbh->prepare("SELECT count(*) AS total
156 FROM default_circ_rules");
157 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
158 (maxissueqty, holdallowed, returnbranch)
160 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
161 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
163 $sth_search->execute();
164 my $res = $sth_search->fetchrow_hashref();
166 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
168 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
171 my $sth_search = $dbh->prepare("SELECT count(*) AS total
172 FROM default_branch_circ_rules
173 WHERE branchcode = ?");
174 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
175 (branchcode, maxissueqty, holdallowed, returnbranch)
176 VALUES (?, ?, ?, ?)");
177 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
178 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
179 WHERE branchcode = ?");
180 $sth_search->execute($branch);
181 my $res = $sth_search->fetchrow_hashref();
183 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
185 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
189 elsif ($op eq "add-branch-cat") {
190 my $categorycode = $input->param('categorycode');
191 my $maxissueqty = $input->param('maxissueqty');
192 $maxissueqty =~ s/\s//g;
193 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
195 if ($branch eq "*") {
196 if ($categorycode eq "*") {
197 my $sth_search = $dbh->prepare("SELECT count(*) AS total
198 FROM default_circ_rules");
199 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
202 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
203 SET maxissueqty = ?");
205 $sth_search->execute();
206 my $res = $sth_search->fetchrow_hashref();
208 $sth_update->execute($maxissueqty);
210 $sth_insert->execute($maxissueqty);
213 my $sth_search = $dbh->prepare("SELECT count(*) AS total
214 FROM default_borrower_circ_rules
215 WHERE categorycode = ?");
216 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
217 (categorycode, maxissueqty)
219 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
221 WHERE categorycode = ?");
222 $sth_search->execute($branch);
223 my $res = $sth_search->fetchrow_hashref();
225 $sth_update->execute($maxissueqty, $categorycode);
227 $sth_insert->execute($categorycode, $maxissueqty);
230 } elsif ($categorycode eq "*") {
231 my $sth_search = $dbh->prepare("SELECT count(*) AS total
232 FROM default_branch_circ_rules
233 WHERE branchcode = ?");
234 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
235 (branchcode, maxissueqty)
237 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
239 WHERE branchcode = ?");
240 $sth_search->execute($branch);
241 my $res = $sth_search->fetchrow_hashref();
243 $sth_update->execute($maxissueqty, $branch);
245 $sth_insert->execute($branch, $maxissueqty);
248 my $sth_search = $dbh->prepare("SELECT count(*) AS total
249 FROM branch_borrower_circ_rules
251 AND categorycode = ?");
252 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
253 (branchcode, categorycode, maxissueqty)
255 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
258 AND categorycode = ?");
260 $sth_search->execute($branch, $categorycode);
261 my $res = $sth_search->fetchrow_hashref();
263 $sth_update->execute($maxissueqty, $branch, $categorycode);
265 $sth_insert->execute($branch, $categorycode, $maxissueqty);
269 elsif ($op eq "add-branch-item") {
270 my $itemtype = $input->param('itemtype');
271 my $holdallowed = $input->param('holdallowed');
272 my $returnbranch = $input->param('returnbranch');
273 $holdallowed =~ s/\s//g;
274 $holdallowed = undef if $holdallowed !~ /^\d+/;
276 if ($branch eq "*") {
277 if ($itemtype eq "*") {
278 my $sth_search = $dbh->prepare("SELECT count(*) AS total
279 FROM default_circ_rules");
280 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
281 (holdallowed, returnbranch)
283 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
284 SET holdallowed = ?, returnbranch = ?");
286 $sth_search->execute();
287 my $res = $sth_search->fetchrow_hashref();
289 $sth_update->execute($holdallowed, $returnbranch);
291 $sth_insert->execute($holdallowed, $returnbranch);
294 my $sth_search = $dbh->prepare("SELECT count(*) AS total
295 FROM default_branch_item_rules
296 WHERE itemtype = ?");
297 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
298 (itemtype, holdallowed, returnbranch)
300 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
301 SET holdallowed = ?, returnbranch = ?
302 WHERE itemtype = ?");
303 $sth_search->execute($itemtype);
304 my $res = $sth_search->fetchrow_hashref();
306 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
308 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
311 } elsif ($itemtype eq "*") {
312 my $sth_search = $dbh->prepare("SELECT count(*) AS total
313 FROM default_branch_circ_rules
314 WHERE branchcode = ?");
315 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
316 (branchcode, holdallowed, returnbranch)
318 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
319 SET holdallowed = ?, returnbranch = ?
320 WHERE branchcode = ?");
321 $sth_search->execute($branch);
322 my $res = $sth_search->fetchrow_hashref();
324 $sth_update->execute($holdallowed, $returnbranch, $branch);
326 $sth_insert->execute($branch, $holdallowed, $returnbranch);
329 my $sth_search = $dbh->prepare("SELECT count(*) AS total
330 FROM branch_item_rules
333 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
334 (branchcode, itemtype, holdallowed, returnbranch)
335 VALUES (?, ?, ?, ?)");
336 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
337 SET holdallowed = ?, returnbranch = ?
341 $sth_search->execute($branch, $itemtype);
342 my $res = $sth_search->fetchrow_hashref();
344 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
346 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
351 my $branches = GetBranches();
353 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
355 value => $thisbranch,
356 selected => $thisbranch eq $branch,
357 branchname => $branches->{$thisbranch}->{'branchname'},
361 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
364 while (my $data=$sth->fetchrow_hashref){
365 push @category_loop,$data;
369 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
374 while (my $row=$sth->fetchrow_hashref){
375 push @itemtypes,$row;
378 my $sth2 = $dbh->prepare("
379 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
382 ON (itemtypes.itemtype = issuingrules.itemtype)
384 ON (categories.categorycode = issuingrules.categorycode)
385 WHERE issuingrules.branchcode = ?
387 $sth2->execute($branch);
389 while (my $row = $sth2->fetchrow_hashref) {
390 $row->{'current_branch'} ||= $row->{'branchcode'};
391 $row->{'humanitemtype'} ||= $row->{'itemtype'};
392 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
393 $row->{'humancategorycode'} ||= $row->{'categorycode'};
394 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
395 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
396 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
397 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
398 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
399 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
400 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
402 $row->{'hardduedate'} = 0;
404 push @row_loop, $row;
408 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
411 if ($branch eq "*") {
412 $sth_branch_cat = $dbh->prepare("
413 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
414 FROM default_borrower_circ_rules
415 JOIN categories USING (categorycode)
418 $sth_branch_cat->execute();
420 $sth_branch_cat = $dbh->prepare("
421 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
422 FROM branch_borrower_circ_rules
423 JOIN categories USING (categorycode)
424 WHERE branch_borrower_circ_rules.branchcode = ?
426 $sth_branch_cat->execute($branch);
429 my @branch_cat_rules = ();
430 while (my $row = $sth_branch_cat->fetchrow_hashref) {
431 push @branch_cat_rules, $row;
433 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
435 # note undef maxissueqty so that template can deal with them
436 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
437 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
440 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
443 if ($branch eq "*") {
444 $sth_branch_item = $dbh->prepare("
445 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
446 FROM default_branch_item_rules
447 JOIN itemtypes USING (itemtype)
449 $sth_branch_item->execute();
451 $sth_branch_item = $dbh->prepare("
452 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
453 FROM branch_item_rules
454 JOIN itemtypes USING (itemtype)
455 WHERE branch_item_rules.branchcode = ?
457 $sth_branch_item->execute($branch);
460 my @branch_item_rules = ();
461 while (my $row = $sth_branch_item->fetchrow_hashref) {
462 push @branch_item_rules, $row;
464 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
466 # note undef holdallowed so that template can deal with them
467 foreach my $entry (@sorted_branch_item_rules) {
468 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
469 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
472 $template->param(show_branch_cat_rule_form => 1);
473 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
474 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
477 if ($branch eq "*") {
478 $sth_defaults = $dbh->prepare("
480 FROM default_circ_rules
482 $sth_defaults->execute();
484 $sth_defaults = $dbh->prepare("
486 FROM default_branch_circ_rules
489 $sth_defaults->execute($branch);
492 my $defaults = $sth_defaults->fetchrow_hashref;
495 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
496 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
497 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
498 $template->param(default_maxissueqty => $defaults->{maxissueqty});
499 $template->param(default_returnbranch => $defaults->{returnbranch});
502 $template->param(default_rules => ($defaults ? 1 : 0));
504 $template->param(categoryloop => \@category_loop,
505 itemtypeloop => \@itemtypes,
506 rules => \@sorted_row_loop,
507 branchloop => \@branchloop,
508 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
509 current_branch => $branch,
510 definedbranch => scalar(@sorted_row_loop)>0
512 output_html_with_http_headers $input, $cookie, $template->output;
516 # sort by patron category, then item type, putting
517 # default entries at the bottom
518 sub by_category_and_itemtype {
519 unless (by_category($a, $b)) {
520 return by_itemtype($a, $b);
526 if ($a->{'default_humancategorycode'}) {
527 return ($b->{'default_humancategorycode'} ? 0 : 1);
528 } elsif ($b->{'default_humancategorycode'}) {
531 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
537 if ($a->{'default_humanitemtype'}) {
538 return ($b->{'default_humanitemtype'} ? 0 : 1);
539 } elsif ($b->{'default_humanitemtype'}) {
542 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};