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, norenewalbefore, 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=?, 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 $reservesallowed = $input->param('reservesallowed');
122 $maxissueqty =~ s/\s//g;
123 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
124 my $issuelength = $input->param('issuelength');
125 my $lengthunit = $input->param('lengthunit');
126 my $hardduedate = $input->param('hardduedate');
127 $hardduedate = format_date_in_iso
($hardduedate);
128 my $hardduedatecompare = $input->param('hardduedatecompare');
129 my $rentaldiscount = $input->param('rentaldiscount');
130 my $overduefinescap = $input->param('overduefinescap') || undef;
131 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
133 $sth_search->execute($br,$bor,$cat);
134 my $res = $sth_search->fetchrow_hashref();
136 $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
138 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
141 elsif ($op eq "set-branch-defaults") {
142 my $categorycode = $input->param('categorycode');
143 my $maxissueqty = $input->param('maxissueqty');
144 my $holdallowed = $input->param('holdallowed');
145 my $returnbranch = $input->param('returnbranch');
146 $maxissueqty =~ s/\s//g;
147 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
148 $holdallowed =~ s/\s//g;
149 $holdallowed = undef if $holdallowed !~ /^\d+/;
151 if ($branch eq "*") {
152 my $sth_search = $dbh->prepare("SELECT count(*) AS total
153 FROM default_circ_rules");
154 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
155 (maxissueqty, holdallowed, returnbranch)
157 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
158 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
160 $sth_search->execute();
161 my $res = $sth_search->fetchrow_hashref();
163 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
165 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
168 my $sth_search = $dbh->prepare("SELECT count(*) AS total
169 FROM default_branch_circ_rules
170 WHERE branchcode = ?");
171 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
172 (branchcode, maxissueqty, holdallowed, returnbranch)
173 VALUES (?, ?, ?, ?)");
174 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
175 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
176 WHERE branchcode = ?");
177 $sth_search->execute($branch);
178 my $res = $sth_search->fetchrow_hashref();
180 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
182 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
186 elsif ($op eq "add-branch-cat") {
187 my $categorycode = $input->param('categorycode');
188 my $maxissueqty = $input->param('maxissueqty');
189 $maxissueqty =~ s/\s//g;
190 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
192 if ($branch eq "*") {
193 if ($categorycode eq "*") {
194 my $sth_search = $dbh->prepare("SELECT count(*) AS total
195 FROM default_circ_rules");
196 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
199 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
200 SET maxissueqty = ?");
202 $sth_search->execute();
203 my $res = $sth_search->fetchrow_hashref();
205 $sth_update->execute($maxissueqty);
207 $sth_insert->execute($maxissueqty);
210 my $sth_search = $dbh->prepare("SELECT count(*) AS total
211 FROM default_borrower_circ_rules
212 WHERE categorycode = ?");
213 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
214 (categorycode, maxissueqty)
216 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
218 WHERE categorycode = ?");
219 $sth_search->execute($branch);
220 my $res = $sth_search->fetchrow_hashref();
222 $sth_update->execute($maxissueqty, $categorycode);
224 $sth_insert->execute($categorycode, $maxissueqty);
227 } elsif ($categorycode eq "*") {
228 my $sth_search = $dbh->prepare("SELECT count(*) AS total
229 FROM default_branch_circ_rules
230 WHERE branchcode = ?");
231 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
232 (branchcode, maxissueqty)
234 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
236 WHERE branchcode = ?");
237 $sth_search->execute($branch);
238 my $res = $sth_search->fetchrow_hashref();
240 $sth_update->execute($maxissueqty, $branch);
242 $sth_insert->execute($branch, $maxissueqty);
245 my $sth_search = $dbh->prepare("SELECT count(*) AS total
246 FROM branch_borrower_circ_rules
248 AND categorycode = ?");
249 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
250 (branchcode, categorycode, maxissueqty)
252 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
255 AND categorycode = ?");
257 $sth_search->execute($branch, $categorycode);
258 my $res = $sth_search->fetchrow_hashref();
260 $sth_update->execute($maxissueqty, $branch, $categorycode);
262 $sth_insert->execute($branch, $categorycode, $maxissueqty);
266 elsif ($op eq "add-branch-item") {
267 my $itemtype = $input->param('itemtype');
268 my $holdallowed = $input->param('holdallowed');
269 my $returnbranch = $input->param('returnbranch');
270 $holdallowed =~ s/\s//g;
271 $holdallowed = undef if $holdallowed !~ /^\d+/;
273 if ($branch eq "*") {
274 if ($itemtype eq "*") {
275 my $sth_search = $dbh->prepare("SELECT count(*) AS total
276 FROM default_circ_rules");
277 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
278 (holdallowed, returnbranch)
280 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
281 SET holdallowed = ?, returnbranch = ?");
283 $sth_search->execute();
284 my $res = $sth_search->fetchrow_hashref();
286 $sth_update->execute($holdallowed, $returnbranch);
288 $sth_insert->execute($holdallowed, $returnbranch);
291 my $sth_search = $dbh->prepare("SELECT count(*) AS total
292 FROM default_branch_item_rules
293 WHERE itemtype = ?");
294 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
295 (itemtype, holdallowed, returnbranch)
297 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
298 SET holdallowed = ?, returnbranch = ?
299 WHERE itemtype = ?");
300 $sth_search->execute($itemtype);
301 my $res = $sth_search->fetchrow_hashref();
303 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
305 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
308 } elsif ($itemtype eq "*") {
309 my $sth_search = $dbh->prepare("SELECT count(*) AS total
310 FROM default_branch_circ_rules
311 WHERE branchcode = ?");
312 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
313 (branchcode, holdallowed, returnbranch)
315 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
316 SET holdallowed = ?, returnbranch = ?
317 WHERE branchcode = ?");
318 $sth_search->execute($branch);
319 my $res = $sth_search->fetchrow_hashref();
321 $sth_update->execute($holdallowed, $returnbranch, $branch);
323 $sth_insert->execute($branch, $holdallowed, $returnbranch);
326 my $sth_search = $dbh->prepare("SELECT count(*) AS total
327 FROM branch_item_rules
330 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
331 (branchcode, itemtype, holdallowed, returnbranch)
332 VALUES (?, ?, ?, ?)");
333 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
334 SET holdallowed = ?, returnbranch = ?
338 $sth_search->execute($branch, $itemtype);
339 my $res = $sth_search->fetchrow_hashref();
341 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
343 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
348 my $branches = GetBranches
();
350 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
352 value
=> $thisbranch,
353 selected
=> $thisbranch eq $branch,
354 branchname
=> $branches->{$thisbranch}->{'branchname'},
358 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
361 while (my $data=$sth->fetchrow_hashref){
362 push @category_loop,$data;
366 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
371 while (my $row=$sth->fetchrow_hashref){
372 push @itemtypes,$row;
375 my $sth2 = $dbh->prepare("
376 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
379 ON (itemtypes.itemtype = issuingrules.itemtype)
381 ON (categories.categorycode = issuingrules.categorycode)
382 WHERE issuingrules.branchcode = ?
384 $sth2->execute($branch);
386 while (my $row = $sth2->fetchrow_hashref) {
387 $row->{'current_branch'} ||= $row->{'branchcode'};
388 $row->{'humanitemtype'} ||= $row->{'itemtype'};
389 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
390 $row->{'humancategorycode'} ||= $row->{'categorycode'};
391 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
392 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
393 if ($row->{'hardduedate'} ne '0000-00-00') {
394 $row->{'hardduedate'} = format_date
( $row->{'hardduedate'});
395 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
396 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
397 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
399 $row->{'hardduedate'} = 0;
401 push @row_loop, $row;
405 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
408 if ($branch eq "*") {
409 $sth_branch_cat = $dbh->prepare("
410 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
411 FROM default_borrower_circ_rules
412 JOIN categories USING (categorycode)
415 $sth_branch_cat->execute();
417 $sth_branch_cat = $dbh->prepare("
418 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
419 FROM branch_borrower_circ_rules
420 JOIN categories USING (categorycode)
421 WHERE branch_borrower_circ_rules.branchcode = ?
423 $sth_branch_cat->execute($branch);
426 my @branch_cat_rules = ();
427 while (my $row = $sth_branch_cat->fetchrow_hashref) {
428 push @branch_cat_rules, $row;
430 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
432 # note undef maxissueqty so that template can deal with them
433 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
434 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
437 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
440 if ($branch eq "*") {
441 $sth_branch_item = $dbh->prepare("
442 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
443 FROM default_branch_item_rules
444 JOIN itemtypes USING (itemtype)
446 $sth_branch_item->execute();
448 $sth_branch_item = $dbh->prepare("
449 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
450 FROM branch_item_rules
451 JOIN itemtypes USING (itemtype)
452 WHERE branch_item_rules.branchcode = ?
454 $sth_branch_item->execute($branch);
457 my @branch_item_rules = ();
458 while (my $row = $sth_branch_item->fetchrow_hashref) {
459 push @branch_item_rules, $row;
461 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
463 # note undef holdallowed so that template can deal with them
464 foreach my $entry (@sorted_branch_item_rules) {
465 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
466 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
469 $template->param(show_branch_cat_rule_form
=> 1);
470 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
471 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
474 if ($branch eq "*") {
475 $sth_defaults = $dbh->prepare("
477 FROM default_circ_rules
479 $sth_defaults->execute();
481 $sth_defaults = $dbh->prepare("
483 FROM default_branch_circ_rules
486 $sth_defaults->execute($branch);
489 my $defaults = $sth_defaults->fetchrow_hashref;
492 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
493 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
494 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
495 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
496 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
499 $template->param(default_rules
=> ($defaults ?
1 : 0));
501 $template->param(categoryloop
=> \
@category_loop,
502 itemtypeloop
=> \
@itemtypes,
503 rules
=> \
@sorted_row_loop,
504 branchloop
=> \
@branchloop,
505 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
506 current_branch
=> $branch,
507 definedbranch
=> scalar(@sorted_row_loop)>0
509 output_html_with_http_headers
$input, $cookie, $template->output;
513 # sort by patron category, then item type, putting
514 # default entries at the bottom
515 sub by_category_and_itemtype
{
516 unless (by_category
($a, $b)) {
517 return by_itemtype
($a, $b);
523 if ($a->{'default_humancategorycode'}) {
524 return ($b->{'default_humancategorycode'} ?
0 : 1);
525 } elsif ($b->{'default_humancategorycode'}) {
528 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
534 if ($a->{'default_humanitemtype'}) {
535 return ($b->{'default_humanitemtype'} ?
0 : 1);
536 } elsif ($b->{'default_humanitemtype'}) {
539 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};