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
31 use Koha
::IssuingRule
;
32 use Koha
::IssuingRules
;
36 my $dbh = C4
::Context
->dbh;
39 # $flagsrequired->{circulation}=1;
40 my ($template, $loggedinuser, $cookie)
41 = get_template_and_user
({template_name
=> "admin/smart-rules.tt",
45 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
49 my $type=$input->param('type');
51 my $branch = $input->param('branch');
53 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
54 $branch = Koha
::Libraries
->search->count() == 1 ?
undef : C4
::Branch
::mybranch
();
57 $branch = C4
::Branch
::onlymine
() ?
( C4
::Branch
::mybranch
() || '*' ) : '*';
60 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
62 my $op = $input->param('op') || q{};
63 my $language = C4
::Languages
::getlanguage
();
65 if ($op eq 'delete') {
66 my $itemtype = $input->param('itemtype');
67 my $categorycode = $input->param('categorycode');
68 $debug and warn "deleting $1 $2 $branch";
70 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
71 $sth_Idelete->execute($branch, $categorycode, $itemtype);
73 elsif ($op eq 'delete-branch-cat') {
74 my $categorycode = $input->param('categorycode');
76 if ($categorycode eq "*") {
77 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
78 $sth_delete->execute();
80 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
81 WHERE categorycode = ?");
82 $sth_delete->execute($categorycode);
84 } elsif ($categorycode eq "*") {
85 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
86 WHERE branchcode = ?");
87 $sth_delete->execute($branch);
89 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
91 AND categorycode = ?");
92 $sth_delete->execute($branch, $categorycode);
95 elsif ($op eq 'delete-branch-item') {
96 my $itemtype = $input->param('itemtype');
98 if ($itemtype eq "*") {
99 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
100 $sth_delete->execute();
102 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
103 WHERE itemtype = ?");
104 $sth_delete->execute($itemtype);
106 } elsif ($itemtype eq "*") {
107 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
108 WHERE branchcode = ?");
109 $sth_delete->execute($branch);
111 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
114 $sth_delete->execute($branch, $itemtype);
117 # save the values entered
118 elsif ($op eq 'add') {
119 my $br = $branch; # branch
120 my $bor = $input->param('categorycode'); # borrower category
121 my $itemtype = $input->param('itemtype'); # item type
122 my $fine = $input->param('fine');
123 my $finedays = $input->param('finedays');
124 my $maxsuspensiondays = $input->param('maxsuspensiondays');
125 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
126 my $firstremind = $input->param('firstremind');
127 my $chargeperiod = $input->param('chargeperiod');
128 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
129 my $maxissueqty = $input->param('maxissueqty');
130 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
131 my $renewalsallowed = $input->param('renewalsallowed');
132 my $renewalperiod = $input->param('renewalperiod');
133 my $norenewalbefore = $input->param('norenewalbefore');
134 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
135 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
136 my $reservesallowed = $input->param('reservesallowed');
137 my $onshelfholds = $input->param('onshelfholds') || 0;
138 $maxissueqty =~ s/\s//g;
139 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
140 $maxonsiteissueqty =~ s/\s//g;
141 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
142 my $issuelength = $input->param('issuelength');
143 my $lengthunit = $input->param('lengthunit');
144 my $hardduedate = $input->param('hardduedate') || undef;
145 $hardduedate = eval { dt_from_string
( $input->param('hardduedate') ) } if ( $hardduedate );
146 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
147 my $hardduedatecompare = $input->param('hardduedatecompare');
148 my $rentaldiscount = $input->param('rentaldiscount');
149 my $opacitemholds = $input->param('opacitemholds') || 0;
150 my $overduefinescap = $input->param('overduefinescap') || undef;
151 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
152 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
156 categorycode
=> $bor,
157 itemtype
=> $itemtype,
159 finedays
=> $finedays,
160 maxsuspensiondays
=> $maxsuspensiondays,
161 firstremind
=> $firstremind,
162 chargeperiod
=> $chargeperiod,
163 chargeperiod_charge_at
=> $chargeperiod_charge_at,
164 maxissueqty
=> $maxissueqty,
165 maxonsiteissueqty
=> $maxonsiteissueqty,
166 renewalsallowed
=> $renewalsallowed,
167 renewalperiod
=> $renewalperiod,
168 norenewalbefore
=> $norenewalbefore,
169 auto_renew
=> $auto_renew,
170 reservesallowed
=> $reservesallowed,
171 issuelength
=> $issuelength,
172 lengthunit
=> $lengthunit,
173 hardduedate
=> $hardduedate,
174 hardduedatecompare
=> $hardduedatecompare,
175 rentaldiscount
=> $rentaldiscount,
176 onshelfholds
=> $onshelfholds,
177 opacitemholds
=> $opacitemholds,
178 overduefinescap
=> $overduefinescap,
179 cap_fine_to_replacement_price
=> $cap_fine_to_replacement_price,
182 my $issuingrule = Koha
::IssuingRules
->find({categorycode
=> $bor, itemtype
=> $itemtype, branchcode
=> $br});
184 $issuingrule->set($params)->store();
186 Koha
::IssuingRule
->new()->set($params)->store();
190 elsif ($op eq "set-branch-defaults") {
191 my $categorycode = $input->param('categorycode');
192 my $maxissueqty = $input->param('maxissueqty');
193 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
194 my $holdallowed = $input->param('holdallowed');
195 my $returnbranch = $input->param('returnbranch');
196 $maxissueqty =~ s/\s//g;
197 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
198 $maxonsiteissueqty =~ s/\s//g;
199 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
200 $holdallowed =~ s/\s//g;
201 $holdallowed = undef if $holdallowed !~ /^\d+/;
203 if ($branch eq "*") {
204 my $sth_search = $dbh->prepare("SELECT count(*) AS total
205 FROM default_circ_rules");
206 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
207 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
208 VALUES (?, ?, ?, ?)");
209 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
210 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
212 $sth_search->execute();
213 my $res = $sth_search->fetchrow_hashref();
215 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
217 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
220 my $sth_search = $dbh->prepare("SELECT count(*) AS total
221 FROM default_branch_circ_rules
222 WHERE branchcode = ?");
223 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
224 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
225 VALUES (?, ?, ?, ?, ?)");
226 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
227 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
228 WHERE branchcode = ?");
229 $sth_search->execute($branch);
230 my $res = $sth_search->fetchrow_hashref();
232 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
234 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
238 elsif ($op eq "add-branch-cat") {
239 my $categorycode = $input->param('categorycode');
240 my $maxissueqty = $input->param('maxissueqty');
241 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
242 $maxissueqty =~ s/\s//g;
243 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
244 $maxonsiteissueqty =~ s/\s//g;
245 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
247 if ($branch eq "*") {
248 if ($categorycode eq "*") {
249 my $sth_search = $dbh->prepare("SELECT count(*) AS total
250 FROM default_circ_rules");
251 my $sth_insert = $dbh->prepare(q
|
252 INSERT INTO default_circ_rules
253 (maxissueqty
, maxonsiteissueqty
)
256 my $sth_update = $dbh->prepare(q
|
257 UPDATE default_circ_rules
259 maxonsiteissueqty
= ?
262 $sth_search->execute();
263 my $res = $sth_search->fetchrow_hashref();
265 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
267 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
270 my $sth_search = $dbh->prepare("SELECT count(*) AS total
271 FROM default_borrower_circ_rules
272 WHERE categorycode = ?");
273 my $sth_insert = $dbh->prepare(q
|
274 INSERT INTO default_borrower_circ_rules
275 (categorycode
, maxissueqty
, maxonsiteissueqty
)
278 my $sth_update = $dbh->prepare(q
|
279 UPDATE default_borrower_circ_rules
281 maxonsiteissueqty
= ?
282 WHERE categorycode
= ?
284 $sth_search->execute($branch);
285 my $res = $sth_search->fetchrow_hashref();
287 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
289 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
292 } elsif ($categorycode eq "*") {
293 my $sth_search = $dbh->prepare("SELECT count(*) AS total
294 FROM default_branch_circ_rules
295 WHERE branchcode = ?");
296 my $sth_insert = $dbh->prepare(q
|
297 INSERT INTO default_branch_circ_rules
298 (branchcode
, maxissueqty
, maxonsiteissueqty
)
301 my $sth_update = $dbh->prepare(q
|
302 UPDATE default_branch_circ_rules
304 maxonsiteissueqty
= ?
307 $sth_search->execute($branch);
308 my $res = $sth_search->fetchrow_hashref();
310 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
312 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
315 my $sth_search = $dbh->prepare("SELECT count(*) AS total
316 FROM branch_borrower_circ_rules
318 AND categorycode = ?");
319 my $sth_insert = $dbh->prepare(q
|
320 INSERT INTO branch_borrower_circ_rules
321 (branchcode
, categorycode
, maxissueqty
, maxonsiteissueqty
)
324 my $sth_update = $dbh->prepare(q
|
325 UPDATE branch_borrower_circ_rules
327 maxonsiteissueqty
= ?
332 $sth_search->execute($branch, $categorycode);
333 my $res = $sth_search->fetchrow_hashref();
335 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
337 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
341 elsif ($op eq "add-branch-item") {
342 my $itemtype = $input->param('itemtype');
343 my $holdallowed = $input->param('holdallowed');
344 my $returnbranch = $input->param('returnbranch');
345 $holdallowed =~ s/\s//g;
346 $holdallowed = undef if $holdallowed !~ /^\d+/;
348 if ($branch eq "*") {
349 if ($itemtype eq "*") {
350 my $sth_search = $dbh->prepare("SELECT count(*) AS total
351 FROM default_circ_rules");
352 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
353 (holdallowed, returnbranch)
355 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
356 SET holdallowed = ?, returnbranch = ?");
358 $sth_search->execute();
359 my $res = $sth_search->fetchrow_hashref();
361 $sth_update->execute($holdallowed, $returnbranch);
363 $sth_insert->execute($holdallowed, $returnbranch);
366 my $sth_search = $dbh->prepare("SELECT count(*) AS total
367 FROM default_branch_item_rules
368 WHERE itemtype = ?");
369 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
370 (itemtype, holdallowed, returnbranch)
372 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
373 SET holdallowed = ?, returnbranch = ?
374 WHERE itemtype = ?");
375 $sth_search->execute($itemtype);
376 my $res = $sth_search->fetchrow_hashref();
378 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
380 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
383 } elsif ($itemtype eq "*") {
384 my $sth_search = $dbh->prepare("SELECT count(*) AS total
385 FROM default_branch_circ_rules
386 WHERE branchcode = ?");
387 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
388 (branchcode, holdallowed, returnbranch)
390 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
391 SET holdallowed = ?, returnbranch = ?
392 WHERE branchcode = ?");
393 $sth_search->execute($branch);
394 my $res = $sth_search->fetchrow_hashref();
396 $sth_update->execute($holdallowed, $returnbranch, $branch);
398 $sth_insert->execute($branch, $holdallowed, $returnbranch);
401 my $sth_search = $dbh->prepare("SELECT count(*) AS total
402 FROM branch_item_rules
405 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
406 (branchcode, itemtype, holdallowed, returnbranch)
407 VALUES (?, ?, ?, ?)");
408 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
409 SET holdallowed = ?, returnbranch = ?
413 $sth_search->execute($branch, $itemtype);
414 my $res = $sth_search->fetchrow_hashref();
416 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
418 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
423 my $branches = GetBranches
();
425 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
427 value
=> $thisbranch,
428 selected
=> $thisbranch eq $branch,
429 branchname
=> $branches->{$thisbranch}->{'branchname'},
433 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
436 while (my $data=$sth->fetchrow_hashref){
437 push @category_loop,$data;
442 my @itemtypes = @
{ GetItemTypes
( style
=> 'array' ) };
443 @itemtypes = sort { lc $a->{translated_description
} cmp lc $b->{translated_description
} } @itemtypes;
445 my $sth2 = $dbh->prepare("
446 SELECT issuingrules.*,
447 itemtypes.description AS humanitemtype,
448 categories.description AS humancategorycode,
449 COALESCE( localization.translation, itemtypes.description ) AS translated_description
452 ON (itemtypes.itemtype = issuingrules.itemtype)
454 ON (categories.categorycode = issuingrules.categorycode)
455 LEFT JOIN localization ON issuingrules.itemtype = localization.code
456 AND localization.entity = 'itemtypes'
457 AND localization.lang = ?
458 WHERE issuingrules.branchcode = ?
460 $sth2->execute($language, $branch);
462 while (my $row = $sth2->fetchrow_hashref) {
463 $row->{'current_branch'} ||= $row->{'branchcode'};
464 $row->{humanitemtype
} ||= $row->{itemtype
};
465 $row->{default_translated_description
} = 1 if $row->{humanitemtype
} eq '*';
466 $row->{'humancategorycode'} ||= $row->{'categorycode'};
467 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
468 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
469 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
470 my $harddue_dt = eval { dt_from_string
( $row->{'hardduedate'} ) };
471 $row->{'hardduedate'} = eval { output_pref
( { dt
=> $harddue_dt, dateonly
=> 1 } ) } if ( $harddue_dt );
472 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
473 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
474 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
476 $row->{'hardduedate'} = 0;
478 push @row_loop, $row;
482 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
485 if ($branch eq "*") {
486 $sth_branch_cat = $dbh->prepare("
487 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
488 FROM default_borrower_circ_rules
489 JOIN categories USING (categorycode)
492 $sth_branch_cat->execute();
494 $sth_branch_cat = $dbh->prepare("
495 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
496 FROM branch_borrower_circ_rules
497 JOIN categories USING (categorycode)
498 WHERE branch_borrower_circ_rules.branchcode = ?
500 $sth_branch_cat->execute($branch);
503 my @branch_cat_rules = ();
504 while (my $row = $sth_branch_cat->fetchrow_hashref) {
505 push @branch_cat_rules, $row;
507 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
509 # note undef maxissueqty so that template can deal with them
510 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
511 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
512 $entry->{unlimited_maxonsiteissueqty
} = 1 unless defined($entry->{maxonsiteissueqty
});
515 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
518 if ($branch eq "*") {
519 $sth_branch_item = $dbh->prepare("
520 SELECT default_branch_item_rules.*,
521 COALESCE( localization.translation, itemtypes.description ) AS translated_description
522 FROM default_branch_item_rules
523 JOIN itemtypes USING (itemtype)
524 LEFT JOIN localization ON itemtypes.itemtype = localization.code
525 AND localization.entity = 'itemtypes'
526 AND localization.lang = ?
528 $sth_branch_item->execute($language);
530 $sth_branch_item = $dbh->prepare("
531 SELECT branch_item_rules.*,
532 COALESCE( localization.translation, itemtypes.description ) AS translated_description
533 FROM branch_item_rules
534 JOIN itemtypes USING (itemtype)
535 LEFT JOIN localization ON itemtypes.itemtype = localization.code
536 AND localization.entity = 'itemtypes'
537 AND localization.lang = ?
538 WHERE branch_item_rules.branchcode = ?
540 $sth_branch_item->execute($language, $branch);
543 my @branch_item_rules = ();
544 while (my $row = $sth_branch_item->fetchrow_hashref) {
545 push @branch_item_rules, $row;
547 my @sorted_branch_item_rules = sort { lc $a->{translated_description
} cmp lc $b->{translated_description
} } @branch_item_rules;
549 # note undef holdallowed so that template can deal with them
550 foreach my $entry (@sorted_branch_item_rules) {
551 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
552 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
555 $template->param(show_branch_cat_rule_form
=> 1);
556 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
557 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
560 if ($branch eq "*") {
561 $sth_defaults = $dbh->prepare("
563 FROM default_circ_rules
565 $sth_defaults->execute();
567 $sth_defaults = $dbh->prepare("
569 FROM default_branch_circ_rules
572 $sth_defaults->execute($branch);
575 my $defaults = $sth_defaults->fetchrow_hashref;
578 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
579 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
580 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
581 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
582 $template->param(default_maxonsiteissueqty
=> $defaults->{maxonsiteissueqty
});
583 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
586 $template->param(default_rules
=> ($defaults ?
1 : 0));
588 $template->param(categoryloop
=> \
@category_loop,
589 itemtypeloop
=> \
@itemtypes,
590 rules
=> \
@sorted_row_loop,
591 branchloop
=> \
@branchloop,
592 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
593 current_branch
=> $branch,
594 definedbranch
=> scalar(@sorted_row_loop)>0
596 output_html_with_http_headers
$input, $cookie, $template->output;
600 # sort by patron category, then item type, putting
601 # default entries at the bottom
602 sub by_category_and_itemtype
{
603 unless (by_category
($a, $b)) {
604 return by_itemtype
($a, $b);
610 if ($a->{'default_humancategorycode'}) {
611 return ($b->{'default_humancategorycode'} ?
0 : 1);
612 } elsif ($b->{'default_humancategorycode'}) {
615 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
621 if ($a->{default_translated_description
}) {
622 return ($b->{'default_translated_description'} ?
0 : 1);
623 } elsif ($b->{'default_translated_description'}) {
626 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};