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.tmpl",
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, 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();
132 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
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)
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();
159 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
161 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
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();
176 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
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
195 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
196 SET maxissueqty = ?");
198 $sth_search->execute();
199 my $res = $sth_search->fetchrow_hashref();
201 $sth_update->execute($maxissueqty);
203 $sth_insert->execute($maxissueqty);
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)
212 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
214 WHERE categorycode = ?");
215 $sth_search->execute($branch);
216 my $res = $sth_search->fetchrow_hashref();
218 $sth_update->execute($maxissueqty, $categorycode);
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)
230 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
232 WHERE branchcode = ?");
233 $sth_search->execute($branch);
234 my $res = $sth_search->fetchrow_hashref();
236 $sth_update->execute($maxissueqty, $branch);
238 $sth_insert->execute($branch, $maxissueqty);
241 my $sth_search = $dbh->prepare("SELECT count(*) AS total
242 FROM branch_borrower_circ_rules
244 AND categorycode = ?");
245 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
246 (branchcode, categorycode, maxissueqty)
248 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
251 AND categorycode = ?");
253 $sth_search->execute($branch, $categorycode);
254 my $res = $sth_search->fetchrow_hashref();
256 $sth_update->execute($maxissueqty, $branch, $categorycode);
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)
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();
282 $sth_update->execute($holdallowed, $returnbranch);
284 $sth_insert->execute($holdallowed, $returnbranch);
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)
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();
299 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
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)
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();
317 $sth_update->execute($holdallowed, $returnbranch, $branch);
319 $sth_insert->execute($branch, $holdallowed, $returnbranch);
322 my $sth_search = $dbh->prepare("SELECT count(*) AS total
323 FROM branch_item_rules
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 = ?
334 $sth_search->execute($branch, $itemtype);
335 my $res = $sth_search->fetchrow_hashref();
337 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
339 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
344 my $branches = GetBranches
();
346 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
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");
357 while (my $data=$sth->fetchrow_hashref){
358 push @category_loop,$data;
362 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
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
375 ON (itemtypes.itemtype = issuingrules.itemtype)
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);
395 $row->{'hardduedate'} = 0;
397 push @row_loop, $row;
401 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
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();
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;
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();
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);
470 if ($branch eq "*") {
471 $sth_defaults = $dbh->prepare("
473 FROM default_circ_rules
475 $sth_defaults->execute();
477 $sth_defaults = $dbh->prepare("
479 FROM default_branch_circ_rules
482 $sth_defaults->execute($branch);
485 my $defaults = $sth_defaults->fetchrow_hashref;
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;
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);
519 if ($a->{'default_humancategorycode'}) {
520 return ($b->{'default_humancategorycode'} ?
0 : 1);
521 } elsif ($b->{'default_humancategorycode'}) {
524 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
530 if ($a->{'default_humanitemtype'}) {
531 return ($b->{'default_humanitemtype'} ?
0 : 1);
532 } elsif ($b->{'default_humanitemtype'}) {
535 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};