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.
21 #use warnings; FIXME - Bug 2505
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 => 1},
45 my $type=$input->param('type');
46 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
47 my $op = $input->param('op');
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) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
105 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=? 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 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
127 $sth_search->execute($br,$bor,$cat);
128 my $res = $sth_search->fetchrow_hashref();
130 $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount, $br,$bor,$cat);
132 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount);
135 elsif ($op eq "set-branch-defaults") {
136 my $categorycode = $input->param('categorycode');
137 my $maxissueqty = $input->param('maxissueqty');
138 my $holdallowed = $input->param('holdallowed');
139 $maxissueqty =~ s/\s//g;
140 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
141 $holdallowed =~ s/\s//g;
142 $holdallowed = undef if $holdallowed !~ /^\d+/;
144 if ($branch eq "*") {
145 my $sth_search = $dbh->prepare("SELECT count(*) AS total
146 FROM default_circ_rules");
147 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
148 (maxissueqty, holdallowed)
150 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
151 SET maxissueqty = ?, holdallowed = ?");
153 $sth_search->execute();
154 my $res = $sth_search->fetchrow_hashref();
156 $sth_update->execute($maxissueqty, $holdallowed);
158 $sth_insert->execute($maxissueqty, $holdallowed);
161 my $sth_search = $dbh->prepare("SELECT count(*) AS total
162 FROM default_branch_circ_rules
163 WHERE branchcode = ?");
164 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
165 (branchcode, maxissueqty, holdallowed)
167 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
168 SET maxissueqty = ?, holdallowed = ?
169 WHERE branchcode = ?");
170 $sth_search->execute($branch);
171 my $res = $sth_search->fetchrow_hashref();
173 $sth_update->execute($maxissueqty, $holdallowed, $branch);
175 $sth_insert->execute($branch, $maxissueqty, $holdallowed);
179 elsif ($op eq "add-branch-cat") {
180 my $categorycode = $input->param('categorycode');
181 my $maxissueqty = $input->param('maxissueqty');
182 $maxissueqty =~ s/\s//g;
183 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
185 if ($branch eq "*") {
186 if ($categorycode eq "*") {
187 my $sth_search = $dbh->prepare("SELECT count(*) AS total
188 FROM default_circ_rules");
189 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
192 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
193 SET maxissueqty = ?");
195 $sth_search->execute();
196 my $res = $sth_search->fetchrow_hashref();
198 $sth_update->execute($maxissueqty);
200 $sth_insert->execute($maxissueqty);
203 my $sth_search = $dbh->prepare("SELECT count(*) AS total
204 FROM default_borrower_circ_rules
205 WHERE categorycode = ?");
206 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
207 (categorycode, maxissueqty)
209 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
211 WHERE categorycode = ?");
212 $sth_search->execute($branch);
213 my $res = $sth_search->fetchrow_hashref();
215 $sth_update->execute($maxissueqty, $categorycode);
217 $sth_insert->execute($categorycode, $maxissueqty);
220 } elsif ($categorycode eq "*") {
221 my $sth_search = $dbh->prepare("SELECT count(*) AS total
222 FROM default_branch_circ_rules
223 WHERE branchcode = ?");
224 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
225 (branchcode, maxissueqty)
227 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
229 WHERE branchcode = ?");
230 $sth_search->execute($branch);
231 my $res = $sth_search->fetchrow_hashref();
233 $sth_update->execute($maxissueqty, $branch);
235 $sth_insert->execute($branch, $maxissueqty);
238 my $sth_search = $dbh->prepare("SELECT count(*) AS total
239 FROM branch_borrower_circ_rules
241 AND categorycode = ?");
242 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
243 (branchcode, categorycode, maxissueqty)
245 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
248 AND categorycode = ?");
250 $sth_search->execute($branch, $categorycode);
251 my $res = $sth_search->fetchrow_hashref();
253 $sth_update->execute($maxissueqty, $branch, $categorycode);
255 $sth_insert->execute($branch, $categorycode, $maxissueqty);
259 elsif ($op eq "add-branch-item") {
260 my $itemtype = $input->param('itemtype');
261 my $holdallowed = $input->param('holdallowed');
262 $holdallowed =~ s/\s//g;
263 $holdallowed = undef if $holdallowed !~ /^\d+/;
265 if ($branch eq "*") {
266 if ($itemtype eq "*") {
267 my $sth_search = $dbh->prepare("SELECT count(*) AS total
268 FROM default_circ_rules");
269 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
272 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
273 SET holdallowed = ?");
275 $sth_search->execute();
276 my $res = $sth_search->fetchrow_hashref();
278 $sth_update->execute($holdallowed);
280 $sth_insert->execute($holdallowed);
283 my $sth_search = $dbh->prepare("SELECT count(*) AS total
284 FROM default_branch_item_rules
285 WHERE itemtype = ?");
286 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
287 (itemtype, holdallowed)
289 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
291 WHERE itemtype = ?");
292 $sth_search->execute($itemtype);
293 my $res = $sth_search->fetchrow_hashref();
295 $sth_update->execute($holdallowed, $itemtype);
297 $sth_insert->execute($itemtype, $holdallowed);
300 } elsif ($itemtype eq "*") {
301 my $sth_search = $dbh->prepare("SELECT count(*) AS total
302 FROM default_branch_circ_rules
303 WHERE branchcode = ?");
304 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
305 (branchcode, holdallowed)
307 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
309 WHERE branchcode = ?");
310 $sth_search->execute($branch);
311 my $res = $sth_search->fetchrow_hashref();
313 $sth_update->execute($holdallowed, $branch);
315 $sth_insert->execute($branch, $holdallowed);
318 my $sth_search = $dbh->prepare("SELECT count(*) AS total
319 FROM branch_item_rules
322 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
323 (branchcode, itemtype, holdallowed)
325 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
330 $sth_search->execute($branch, $itemtype);
331 my $res = $sth_search->fetchrow_hashref();
333 $sth_update->execute($holdallowed, $branch, $itemtype);
335 $sth_insert->execute($branch, $itemtype, $holdallowed);
340 my $branches = GetBranches();
342 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
344 value => $thisbranch,
345 selected => $thisbranch eq $branch,
346 branchname => $branches->{$thisbranch}->{'branchname'},
350 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
353 while (my $data=$sth->fetchrow_hashref){
354 push @category_loop,$data;
358 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
363 while (my $row=$sth->fetchrow_hashref){
364 push @itemtypes,$row;
367 my $sth2 = $dbh->prepare("
368 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
371 ON (itemtypes.itemtype = issuingrules.itemtype)
373 ON (categories.categorycode = issuingrules.categorycode)
374 WHERE issuingrules.branchcode = ?
376 $sth2->execute($branch);
378 while (my $row = $sth2->fetchrow_hashref) {
379 $row->{'current_branch'} ||= $row->{'branchcode'};
380 $row->{'humanitemtype'} ||= $row->{'itemtype'};
381 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
382 $row->{'humancategorycode'} ||= $row->{'categorycode'};
383 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
384 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
385 if ($row->{'hardduedate'} ne '0000-00-00') {
386 $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
387 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
388 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
389 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
391 $row->{'hardduedate'} = 0;
393 push @row_loop, $row;
397 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
400 if ($branch eq "*") {
401 $sth_branch_cat = $dbh->prepare("
402 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
403 FROM default_borrower_circ_rules
404 JOIN categories USING (categorycode)
407 $sth_branch_cat->execute();
409 $sth_branch_cat = $dbh->prepare("
410 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
411 FROM branch_borrower_circ_rules
412 JOIN categories USING (categorycode)
413 WHERE branch_borrower_circ_rules.branchcode = ?
415 $sth_branch_cat->execute($branch);
418 my @branch_cat_rules = ();
419 while (my $row = $sth_branch_cat->fetchrow_hashref) {
420 push @branch_cat_rules, $row;
422 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
424 # note undef maxissueqty so that template can deal with them
425 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
426 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
429 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
432 if ($branch eq "*") {
433 $sth_branch_item = $dbh->prepare("
434 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
435 FROM default_branch_item_rules
436 JOIN itemtypes USING (itemtype)
438 $sth_branch_item->execute();
440 $sth_branch_item = $dbh->prepare("
441 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
442 FROM branch_item_rules
443 JOIN itemtypes USING (itemtype)
444 WHERE branch_item_rules.branchcode = ?
446 $sth_branch_item->execute($branch);
449 my @branch_item_rules = ();
450 while (my $row = $sth_branch_item->fetchrow_hashref) {
451 push @branch_item_rules, $row;
453 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
455 # note undef holdallowed so that template can deal with them
456 foreach my $entry (@sorted_branch_item_rules) {
457 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
458 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
461 $template->param(show_branch_cat_rule_form => 1);
462 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
463 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
466 if ($branch eq "*") {
467 $sth_defaults = $dbh->prepare("
469 FROM default_circ_rules
471 $sth_defaults->execute();
473 $sth_defaults = $dbh->prepare("
475 FROM default_branch_circ_rules
478 $sth_defaults->execute($branch);
481 my $defaults = $sth_defaults->fetchrow_hashref;
484 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
485 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
486 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
487 $template->param(default_maxissueqty => $defaults->{maxissueqty});
490 $template->param(default_rules => ($defaults ? 1 : 0));
492 $template->param(categoryloop => \@category_loop,
493 itemtypeloop => \@itemtypes,
494 rules => \@sorted_row_loop,
495 branchloop => \@branchloop,
496 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
497 current_branch => $branch,
498 definedbranch => scalar(@sorted_row_loop)>0
500 output_html_with_http_headers $input, $cookie, $template->output;
504 # sort by patron category, then item type, putting
505 # default entries at the bottom
506 sub by_category_and_itemtype {
507 unless (by_category($a, $b)) {
508 return by_itemtype($a, $b);
514 if ($a->{'default_humancategorycode'}) {
515 return ($b->{'default_humancategorycode'} ? 0 : 1);
516 } elsif ($b->{'default_humancategorycode'}) {
519 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
525 if ($a->{'default_humanitemtype'}) {
526 return ($b->{'default_humanitemtype'} ? 0 : 1);
527 } elsif ($b->{'default_humanitemtype'}) {
530 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};