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, 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=?, 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 $reservesallowed = $input->param('reservesallowed');
117 $maxissueqty =~ s/\s//g;
118 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
119 my $issuelength = $input->param('issuelength');
120 my $lengthunit = $input->param('lengthunit');
121 my $hardduedate = $input->param('hardduedate');
122 $hardduedate = format_date_in_iso
($hardduedate);
123 my $hardduedatecompare = $input->param('hardduedatecompare');
124 my $rentaldiscount = $input->param('rentaldiscount');
125 my $overduefinescap = $input->param('overduefinescap') || undef;
126 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
128 $sth_search->execute($br,$bor,$cat);
129 my $res = $sth_search->fetchrow_hashref();
131 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
133 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
136 elsif ($op eq "set-branch-defaults") {
137 my $categorycode = $input->param('categorycode');
138 my $maxissueqty = $input->param('maxissueqty');
139 my $holdallowed = $input->param('holdallowed');
140 my $returnbranch = $input->param('returnbranch');
141 $maxissueqty =~ s/\s//g;
142 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
143 $holdallowed =~ s/\s//g;
144 $holdallowed = undef if $holdallowed !~ /^\d+/;
146 if ($branch eq "*") {
147 my $sth_search = $dbh->prepare("SELECT count(*) AS total
148 FROM default_circ_rules");
149 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
150 (maxissueqty, holdallowed, returnbranch)
152 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
153 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
155 $sth_search->execute();
156 my $res = $sth_search->fetchrow_hashref();
158 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
160 $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
163 my $sth_search = $dbh->prepare("SELECT count(*) AS total
164 FROM default_branch_circ_rules
165 WHERE branchcode = ?");
166 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
167 (branchcode, maxissueqty, holdallowed, returnbranch)
168 VALUES (?, ?, ?, ?)");
169 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
170 SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
171 WHERE branchcode = ?");
172 $sth_search->execute($branch);
173 my $res = $sth_search->fetchrow_hashref();
175 $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
177 $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
181 elsif ($op eq "add-branch-cat") {
182 my $categorycode = $input->param('categorycode');
183 my $maxissueqty = $input->param('maxissueqty');
184 $maxissueqty =~ s/\s//g;
185 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
187 if ($branch eq "*") {
188 if ($categorycode eq "*") {
189 my $sth_search = $dbh->prepare("SELECT count(*) AS total
190 FROM default_circ_rules");
191 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
194 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
195 SET maxissueqty = ?");
197 $sth_search->execute();
198 my $res = $sth_search->fetchrow_hashref();
200 $sth_update->execute($maxissueqty);
202 $sth_insert->execute($maxissueqty);
205 my $sth_search = $dbh->prepare("SELECT count(*) AS total
206 FROM default_borrower_circ_rules
207 WHERE categorycode = ?");
208 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
209 (categorycode, maxissueqty)
211 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
213 WHERE categorycode = ?");
214 $sth_search->execute($branch);
215 my $res = $sth_search->fetchrow_hashref();
217 $sth_update->execute($maxissueqty, $categorycode);
219 $sth_insert->execute($categorycode, $maxissueqty);
222 } elsif ($categorycode eq "*") {
223 my $sth_search = $dbh->prepare("SELECT count(*) AS total
224 FROM default_branch_circ_rules
225 WHERE branchcode = ?");
226 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
227 (branchcode, maxissueqty)
229 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
231 WHERE branchcode = ?");
232 $sth_search->execute($branch);
233 my $res = $sth_search->fetchrow_hashref();
235 $sth_update->execute($maxissueqty, $branch);
237 $sth_insert->execute($branch, $maxissueqty);
240 my $sth_search = $dbh->prepare("SELECT count(*) AS total
241 FROM branch_borrower_circ_rules
243 AND categorycode = ?");
244 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
245 (branchcode, categorycode, maxissueqty)
247 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
250 AND categorycode = ?");
252 $sth_search->execute($branch, $categorycode);
253 my $res = $sth_search->fetchrow_hashref();
255 $sth_update->execute($maxissueqty, $branch, $categorycode);
257 $sth_insert->execute($branch, $categorycode, $maxissueqty);
261 elsif ($op eq "add-branch-item") {
262 my $itemtype = $input->param('itemtype');
263 my $holdallowed = $input->param('holdallowed');
264 my $returnbranch = $input->param('returnbranch');
265 $holdallowed =~ s/\s//g;
266 $holdallowed = undef if $holdallowed !~ /^\d+/;
268 if ($branch eq "*") {
269 if ($itemtype eq "*") {
270 my $sth_search = $dbh->prepare("SELECT count(*) AS total
271 FROM default_circ_rules");
272 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
273 (holdallowed, returnbranch)
275 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
276 SET holdallowed = ?, returnbranch = ?");
278 $sth_search->execute();
279 my $res = $sth_search->fetchrow_hashref();
281 $sth_update->execute($holdallowed, $returnbranch);
283 $sth_insert->execute($holdallowed, $returnbranch);
286 my $sth_search = $dbh->prepare("SELECT count(*) AS total
287 FROM default_branch_item_rules
288 WHERE itemtype = ?");
289 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
290 (itemtype, holdallowed, returnbranch)
292 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
293 SET holdallowed = ?, returnbranch = ?
294 WHERE itemtype = ?");
295 $sth_search->execute($itemtype);
296 my $res = $sth_search->fetchrow_hashref();
298 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
300 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
303 } elsif ($itemtype eq "*") {
304 my $sth_search = $dbh->prepare("SELECT count(*) AS total
305 FROM default_branch_circ_rules
306 WHERE branchcode = ?");
307 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
308 (branchcode, holdallowed, returnbranch)
310 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
311 SET holdallowed = ?, returnbranch = ?
312 WHERE branchcode = ?");
313 $sth_search->execute($branch);
314 my $res = $sth_search->fetchrow_hashref();
316 $sth_update->execute($holdallowed, $returnbranch, $branch);
318 $sth_insert->execute($branch, $holdallowed, $returnbranch);
321 my $sth_search = $dbh->prepare("SELECT count(*) AS total
322 FROM branch_item_rules
325 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
326 (branchcode, itemtype, holdallowed, returnbranch)
327 VALUES (?, ?, ?, ?)");
328 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
329 SET holdallowed = ?, returnbranch = ?
333 $sth_search->execute($branch, $itemtype);
334 my $res = $sth_search->fetchrow_hashref();
336 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
338 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
343 my $branches = GetBranches
();
345 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
347 value
=> $thisbranch,
348 selected
=> $thisbranch eq $branch,
349 branchname
=> $branches->{$thisbranch}->{'branchname'},
353 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
356 while (my $data=$sth->fetchrow_hashref){
357 push @category_loop,$data;
361 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
366 while (my $row=$sth->fetchrow_hashref){
367 push @itemtypes,$row;
370 my $sth2 = $dbh->prepare("
371 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
374 ON (itemtypes.itemtype = issuingrules.itemtype)
376 ON (categories.categorycode = issuingrules.categorycode)
377 WHERE issuingrules.branchcode = ?
379 $sth2->execute($branch);
381 while (my $row = $sth2->fetchrow_hashref) {
382 $row->{'current_branch'} ||= $row->{'branchcode'};
383 $row->{'humanitemtype'} ||= $row->{'itemtype'};
384 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
385 $row->{'humancategorycode'} ||= $row->{'categorycode'};
386 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
387 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
388 if ($row->{'hardduedate'} ne '0000-00-00') {
389 $row->{'hardduedate'} = format_date
( $row->{'hardduedate'});
390 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
391 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
392 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
394 $row->{'hardduedate'} = 0;
396 push @row_loop, $row;
400 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
403 if ($branch eq "*") {
404 $sth_branch_cat = $dbh->prepare("
405 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
406 FROM default_borrower_circ_rules
407 JOIN categories USING (categorycode)
410 $sth_branch_cat->execute();
412 $sth_branch_cat = $dbh->prepare("
413 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
414 FROM branch_borrower_circ_rules
415 JOIN categories USING (categorycode)
416 WHERE branch_borrower_circ_rules.branchcode = ?
418 $sth_branch_cat->execute($branch);
421 my @branch_cat_rules = ();
422 while (my $row = $sth_branch_cat->fetchrow_hashref) {
423 push @branch_cat_rules, $row;
425 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
427 # note undef maxissueqty so that template can deal with them
428 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
429 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
432 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
435 if ($branch eq "*") {
436 $sth_branch_item = $dbh->prepare("
437 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
438 FROM default_branch_item_rules
439 JOIN itemtypes USING (itemtype)
441 $sth_branch_item->execute();
443 $sth_branch_item = $dbh->prepare("
444 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
445 FROM branch_item_rules
446 JOIN itemtypes USING (itemtype)
447 WHERE branch_item_rules.branchcode = ?
449 $sth_branch_item->execute($branch);
452 my @branch_item_rules = ();
453 while (my $row = $sth_branch_item->fetchrow_hashref) {
454 push @branch_item_rules, $row;
456 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
458 # note undef holdallowed so that template can deal with them
459 foreach my $entry (@sorted_branch_item_rules) {
460 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
461 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
464 $template->param(show_branch_cat_rule_form
=> 1);
465 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
466 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
469 if ($branch eq "*") {
470 $sth_defaults = $dbh->prepare("
472 FROM default_circ_rules
474 $sth_defaults->execute();
476 $sth_defaults = $dbh->prepare("
478 FROM default_branch_circ_rules
481 $sth_defaults->execute($branch);
484 my $defaults = $sth_defaults->fetchrow_hashref;
487 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
488 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
489 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
490 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
491 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
494 $template->param(default_rules
=> ($defaults ?
1 : 0));
496 $template->param(categoryloop
=> \
@category_loop,
497 itemtypeloop
=> \
@itemtypes,
498 rules
=> \
@sorted_row_loop,
499 branchloop
=> \
@branchloop,
500 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
501 current_branch
=> $branch,
502 definedbranch
=> scalar(@sorted_row_loop)>0
504 output_html_with_http_headers
$input, $cookie, $template->output;
508 # sort by patron category, then item type, putting
509 # default entries at the bottom
510 sub by_category_and_itemtype
{
511 unless (by_category
($a, $b)) {
512 return by_itemtype
($a, $b);
518 if ($a->{'default_humancategorycode'}) {
519 return ($b->{'default_humancategorycode'} ?
0 : 1);
520 } elsif ($b->{'default_humancategorycode'}) {
523 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
529 if ($a->{'default_humanitemtype'}) {
530 return ($b->{'default_humanitemtype'} ?
0 : 1);
531 } elsif ($b->{'default_humanitemtype'}) {
534 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};