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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use C4::Branch; # GetBranches
33 my $dbh = C4::Context->dbh;
36 # $flagsrequired->{circulation}=1;
37 my ($template, $loggedinuser, $cookie)
38 = get_template_and_user({template_name => "admin/smart-rules.tt",
42 flagsrequired => {parameters => 'manage_circ_rules'},
46 my $type=$input->param('type');
47 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
48 my $op = $input->param('op') || q{};
50 if ($op eq 'delete') {
51 my $itemtype = $input->param('itemtype');
52 my $categorycode = $input->param('categorycode');
53 $debug and warn "deleting $1 $2 $branch";
55 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
56 $sth_Idelete->execute($branch, $categorycode, $itemtype);
58 elsif ($op eq 'delete-branch-cat') {
59 my $categorycode = $input->param('categorycode');
61 if ($categorycode eq "*") {
62 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
63 $sth_delete->execute();
65 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
66 WHERE categorycode = ?");
67 $sth_delete->execute($categorycode);
69 } elsif ($categorycode eq "*") {
70 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
71 WHERE branchcode = ?");
72 $sth_delete->execute($branch);
74 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
76 AND categorycode = ?");
77 $sth_delete->execute($branch, $categorycode);
80 elsif ($op eq 'delete-branch-item') {
81 my $itemtype = $input->param('itemtype');
83 if ($itemtype eq "*") {
84 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
85 $sth_delete->execute();
87 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
89 $sth_delete->execute($itemtype);
91 } elsif ($itemtype eq "*") {
92 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
93 WHERE branchcode = ?");
94 $sth_delete->execute($branch);
96 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
99 $sth_delete->execute($branch, $itemtype);
102 # save the values entered
103 elsif ($op eq 'add') {
104 my $br = $branch; # branch
105 my $bor = $input->param('categorycode'); # borrower category
106 my $itemtype = $input->param('itemtype'); # item type
107 my $fine = $input->param('fine');
108 my $finedays = $input->param('finedays');
109 my $maxsuspensiondays = $input->param('maxsuspensiondays');
110 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
111 my $firstremind = $input->param('firstremind');
112 my $chargeperiod = $input->param('chargeperiod');
113 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
114 my $maxissueqty = $input->param('maxissueqty');
115 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
116 my $renewalsallowed = $input->param('renewalsallowed');
117 my $renewalperiod = $input->param('renewalperiod');
118 my $norenewalbefore = $input->param('norenewalbefore');
119 $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
120 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
121 my $reservesallowed = $input->param('reservesallowed');
122 my $onshelfholds = $input->param('onshelfholds') || 0;
123 $maxissueqty =~ s/\s//g;
124 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
125 $maxonsiteissueqty =~ s/\s//g;
126 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
127 my $issuelength = $input->param('issuelength');
128 my $lengthunit = $input->param('lengthunit');
129 my $hardduedate = $input->param('hardduedate');
130 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
131 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
132 my $hardduedatecompare = $input->param('hardduedatecompare');
133 my $rentaldiscount = $input->param('rentaldiscount');
134 my $opacitemholds = $input->param('opacitemholds') || 0;
135 my $overduefinescap = $input->param('overduefinescap') || undef;
136 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
138 my $schema = Koha::Database->new()->schema();
139 my $rs = $schema->resultset('Issuingrule');
143 categorycode => $bor,
144 itemtype => $itemtype,
146 finedays => $finedays,
147 maxsuspensiondays => $maxsuspensiondays,
148 firstremind => $firstremind,
149 chargeperiod => $chargeperiod,
150 chargeperiod_charge_at => $chargeperiod_charge_at,
151 maxissueqty => $maxissueqty,
152 maxonsiteissueqty => $maxonsiteissueqty,
153 renewalsallowed => $renewalsallowed,
154 renewalperiod => $renewalperiod,
155 norenewalbefore => $norenewalbefore,
156 auto_renew => $auto_renew,
157 reservesallowed => $reservesallowed,
158 issuelength => $issuelength,
159 lengthunit => $lengthunit,
160 hardduedate => $hardduedate,
161 hardduedatecompare => $hardduedatecompare,
162 rentaldiscount => $rentaldiscount,
163 onshelfholds => $onshelfholds,
164 opacitemholds => $opacitemholds,
165 overduefinescap => $overduefinescap,
168 $rs->update_or_create($params);
171 elsif ($op eq "set-branch-defaults") {
172 my $categorycode = $input->param('categorycode');
173 my $maxissueqty = $input->param('maxissueqty');
174 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
175 my $holdallowed = $input->param('holdallowed');
176 my $returnbranch = $input->param('returnbranch');
177 $maxissueqty =~ s/\s//g;
178 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
179 $maxonsiteissueqty =~ s/\s//g;
180 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
181 $holdallowed =~ s/\s//g;
182 $holdallowed = undef if $holdallowed !~ /^\d+/;
184 if ($branch eq "*") {
185 my $sth_search = $dbh->prepare("SELECT count(*) AS total
186 FROM default_circ_rules");
187 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
188 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
189 VALUES (?, ?, ?, ?)");
190 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
191 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
193 $sth_search->execute();
194 my $res = $sth_search->fetchrow_hashref();
196 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
198 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
201 my $sth_search = $dbh->prepare("SELECT count(*) AS total
202 FROM default_branch_circ_rules
203 WHERE branchcode = ?");
204 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
205 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
206 VALUES (?, ?, ?, ?, ?)");
207 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
208 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
209 WHERE branchcode = ?");
210 $sth_search->execute($branch);
211 my $res = $sth_search->fetchrow_hashref();
213 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
215 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
219 elsif ($op eq "add-branch-cat") {
220 my $categorycode = $input->param('categorycode');
221 my $maxissueqty = $input->param('maxissueqty');
222 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
223 $maxissueqty =~ s/\s//g;
224 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
225 $maxonsiteissueqty =~ s/\s//g;
226 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
228 if ($branch eq "*") {
229 if ($categorycode eq "*") {
230 my $sth_search = $dbh->prepare("SELECT count(*) AS total
231 FROM default_circ_rules");
232 my $sth_insert = $dbh->prepare(q|
233 INSERT INTO default_circ_rules
234 (maxissueqty, maxonsiteissueqty)
237 my $sth_update = $dbh->prepare(q|
238 UPDATE default_circ_rules
240 maxonsiteissueqty = ?
243 $sth_search->execute();
244 my $res = $sth_search->fetchrow_hashref();
246 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
248 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
251 my $sth_search = $dbh->prepare("SELECT count(*) AS total
252 FROM default_borrower_circ_rules
253 WHERE categorycode = ?");
254 my $sth_insert = $dbh->prepare(q|
255 INSERT INTO default_borrower_circ_rules
256 (categorycode, maxissueqty, maxonsiteissueqty)
259 my $sth_update = $dbh->prepare(q|
260 UPDATE default_borrower_circ_rules
262 maxonsiteissueqty = ?
263 WHERE categorycode = ?
265 $sth_search->execute($branch);
266 my $res = $sth_search->fetchrow_hashref();
268 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
270 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
273 } elsif ($categorycode eq "*") {
274 my $sth_search = $dbh->prepare("SELECT count(*) AS total
275 FROM default_branch_circ_rules
276 WHERE branchcode = ?");
277 my $sth_insert = $dbh->prepare(q|
278 INSERT INTO default_branch_circ_rules
279 (branchcode, maxissueqty, maxonsiteissueqty)
282 my $sth_update = $dbh->prepare(q|
283 UPDATE default_branch_circ_rules
285 maxonsiteissueqty = ?
288 $sth_search->execute($branch);
289 my $res = $sth_search->fetchrow_hashref();
291 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
293 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
296 my $sth_search = $dbh->prepare("SELECT count(*) AS total
297 FROM branch_borrower_circ_rules
299 AND categorycode = ?");
300 my $sth_insert = $dbh->prepare(q|
301 INSERT INTO branch_borrower_circ_rules
302 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
305 my $sth_update = $dbh->prepare(q|
306 UPDATE branch_borrower_circ_rules
308 maxonsiteissueqty = ?
313 $sth_search->execute($branch, $categorycode);
314 my $res = $sth_search->fetchrow_hashref();
316 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
318 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
322 elsif ($op eq "add-branch-item") {
323 my $itemtype = $input->param('itemtype');
324 my $holdallowed = $input->param('holdallowed');
325 my $returnbranch = $input->param('returnbranch');
326 $holdallowed =~ s/\s//g;
327 $holdallowed = undef if $holdallowed !~ /^\d+/;
329 if ($branch eq "*") {
330 if ($itemtype eq "*") {
331 my $sth_search = $dbh->prepare("SELECT count(*) AS total
332 FROM default_circ_rules");
333 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
334 (holdallowed, returnbranch)
336 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
337 SET holdallowed = ?, returnbranch = ?");
339 $sth_search->execute();
340 my $res = $sth_search->fetchrow_hashref();
342 $sth_update->execute($holdallowed, $returnbranch);
344 $sth_insert->execute($holdallowed, $returnbranch);
347 my $sth_search = $dbh->prepare("SELECT count(*) AS total
348 FROM default_branch_item_rules
349 WHERE itemtype = ?");
350 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
351 (itemtype, holdallowed, returnbranch)
353 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
354 SET holdallowed = ?, returnbranch = ?
355 WHERE itemtype = ?");
356 $sth_search->execute($itemtype);
357 my $res = $sth_search->fetchrow_hashref();
359 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
361 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
364 } elsif ($itemtype eq "*") {
365 my $sth_search = $dbh->prepare("SELECT count(*) AS total
366 FROM default_branch_circ_rules
367 WHERE branchcode = ?");
368 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
369 (branchcode, holdallowed, returnbranch)
371 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
372 SET holdallowed = ?, returnbranch = ?
373 WHERE branchcode = ?");
374 $sth_search->execute($branch);
375 my $res = $sth_search->fetchrow_hashref();
377 $sth_update->execute($holdallowed, $returnbranch, $branch);
379 $sth_insert->execute($branch, $holdallowed, $returnbranch);
382 my $sth_search = $dbh->prepare("SELECT count(*) AS total
383 FROM branch_item_rules
386 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
387 (branchcode, itemtype, holdallowed, returnbranch)
388 VALUES (?, ?, ?, ?)");
389 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
390 SET holdallowed = ?, returnbranch = ?
394 $sth_search->execute($branch, $itemtype);
395 my $res = $sth_search->fetchrow_hashref();
397 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
399 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
404 my $branches = GetBranches();
406 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
408 value => $thisbranch,
409 selected => $thisbranch eq $branch,
410 branchname => $branches->{$thisbranch}->{'branchname'},
414 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
417 while (my $data=$sth->fetchrow_hashref){
418 push @category_loop,$data;
422 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
427 while (my $row=$sth->fetchrow_hashref){
428 push @itemtypes,$row;
431 my $sth2 = $dbh->prepare("
432 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
435 ON (itemtypes.itemtype = issuingrules.itemtype)
437 ON (categories.categorycode = issuingrules.categorycode)
438 WHERE issuingrules.branchcode = ?
440 $sth2->execute($branch);
442 while (my $row = $sth2->fetchrow_hashref) {
443 $row->{'current_branch'} ||= $row->{'branchcode'};
444 $row->{'humanitemtype'} ||= $row->{'itemtype'};
445 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
446 $row->{'humancategorycode'} ||= $row->{'categorycode'};
447 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
448 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
449 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
450 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
451 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
452 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
453 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
454 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
456 $row->{'hardduedate'} = 0;
458 push @row_loop, $row;
462 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
465 if ($branch eq "*") {
466 $sth_branch_cat = $dbh->prepare("
467 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
468 FROM default_borrower_circ_rules
469 JOIN categories USING (categorycode)
472 $sth_branch_cat->execute();
474 $sth_branch_cat = $dbh->prepare("
475 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
476 FROM branch_borrower_circ_rules
477 JOIN categories USING (categorycode)
478 WHERE branch_borrower_circ_rules.branchcode = ?
480 $sth_branch_cat->execute($branch);
483 my @branch_cat_rules = ();
484 while (my $row = $sth_branch_cat->fetchrow_hashref) {
485 push @branch_cat_rules, $row;
487 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
489 # note undef maxissueqty so that template can deal with them
490 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
491 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
492 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
495 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
498 if ($branch eq "*") {
499 $sth_branch_item = $dbh->prepare("
500 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
501 FROM default_branch_item_rules
502 JOIN itemtypes USING (itemtype)
504 $sth_branch_item->execute();
506 $sth_branch_item = $dbh->prepare("
507 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
508 FROM branch_item_rules
509 JOIN itemtypes USING (itemtype)
510 WHERE branch_item_rules.branchcode = ?
512 $sth_branch_item->execute($branch);
515 my @branch_item_rules = ();
516 while (my $row = $sth_branch_item->fetchrow_hashref) {
517 push @branch_item_rules, $row;
519 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
521 # note undef holdallowed so that template can deal with them
522 foreach my $entry (@sorted_branch_item_rules) {
523 $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
524 $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
527 $template->param(show_branch_cat_rule_form => 1);
528 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
529 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
532 if ($branch eq "*") {
533 $sth_defaults = $dbh->prepare("
535 FROM default_circ_rules
537 $sth_defaults->execute();
539 $sth_defaults = $dbh->prepare("
541 FROM default_branch_circ_rules
544 $sth_defaults->execute($branch);
547 my $defaults = $sth_defaults->fetchrow_hashref;
550 $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
551 $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
552 $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
553 $template->param(default_maxissueqty => $defaults->{maxissueqty});
554 $template->param(default_maxonsiteissueqty => $defaults->{maxonsiteissueqty});
555 $template->param(default_returnbranch => $defaults->{returnbranch});
558 $template->param(default_rules => ($defaults ? 1 : 0));
560 $template->param(categoryloop => \@category_loop,
561 itemtypeloop => \@itemtypes,
562 rules => \@sorted_row_loop,
563 branchloop => \@branchloop,
564 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
565 current_branch => $branch,
566 definedbranch => scalar(@sorted_row_loop)>0
568 output_html_with_http_headers $input, $cookie, $template->output;
572 # sort by patron category, then item type, putting
573 # default entries at the bottom
574 sub by_category_and_itemtype {
575 unless (by_category($a, $b)) {
576 return by_itemtype($a, $b);
582 if ($a->{'default_humancategorycode'}) {
583 return ($b->{'default_humancategorycode'} ? 0 : 1);
584 } elsif ($b->{'default_humancategorycode'}) {
587 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
593 if ($a->{'default_humanitemtype'}) {
594 return ($b->{'default_humanitemtype'} ? 0 : 1);
595 } elsif ($b->{'default_humanitemtype'}) {
598 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};