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');
49 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
50 $branch = $input->param('branch') || GetBranchesCount
() == 1 ?
undef : C4
::Branch
::mybranch
();
53 $branch = $input->param('branch') || ( C4
::Branch
::onlymine
() ?
( C4
::Branch
::mybranch
() || '*' ) : '*' );
55 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
57 my $op = $input->param('op') || q{};
58 my $language = C4
::Languages
::getlanguage
();
60 if ($op eq 'delete') {
61 my $itemtype = $input->param('itemtype');
62 my $categorycode = $input->param('categorycode');
63 $debug and warn "deleting $1 $2 $branch";
65 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
66 $sth_Idelete->execute($branch, $categorycode, $itemtype);
68 elsif ($op eq 'delete-branch-cat') {
69 my $categorycode = $input->param('categorycode');
71 if ($categorycode eq "*") {
72 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
73 $sth_delete->execute();
75 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
76 WHERE categorycode = ?");
77 $sth_delete->execute($categorycode);
79 } elsif ($categorycode eq "*") {
80 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
81 WHERE branchcode = ?");
82 $sth_delete->execute($branch);
84 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
86 AND categorycode = ?");
87 $sth_delete->execute($branch, $categorycode);
90 elsif ($op eq 'delete-branch-item') {
91 my $itemtype = $input->param('itemtype');
93 if ($itemtype eq "*") {
94 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
95 $sth_delete->execute();
97 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
99 $sth_delete->execute($itemtype);
101 } elsif ($itemtype eq "*") {
102 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
103 WHERE branchcode = ?");
104 $sth_delete->execute($branch);
106 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
109 $sth_delete->execute($branch, $itemtype);
112 # save the values entered
113 elsif ($op eq 'add') {
114 my $br = $branch; # branch
115 my $bor = $input->param('categorycode'); # borrower category
116 my $itemtype = $input->param('itemtype'); # item type
117 my $fine = $input->param('fine');
118 my $finedays = $input->param('finedays');
119 my $maxsuspensiondays = $input->param('maxsuspensiondays');
120 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
121 my $firstremind = $input->param('firstremind');
122 my $chargeperiod = $input->param('chargeperiod');
123 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
124 my $maxissueqty = $input->param('maxissueqty');
125 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
126 my $renewalsallowed = $input->param('renewalsallowed');
127 my $renewalperiod = $input->param('renewalperiod');
128 my $norenewalbefore = $input->param('norenewalbefore');
129 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
130 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
131 my $reservesallowed = $input->param('reservesallowed');
132 my $onshelfholds = $input->param('onshelfholds') || 0;
133 $maxissueqty =~ s/\s//g;
134 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
135 $maxonsiteissueqty =~ s/\s//g;
136 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
137 my $issuelength = $input->param('issuelength');
138 my $lengthunit = $input->param('lengthunit');
139 my $hardduedate = $input->param('hardduedate') || undef;
140 $hardduedate = eval { dt_from_string
( $input->param('hardduedate') ) } if ( $hardduedate );
141 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
142 my $hardduedatecompare = $input->param('hardduedatecompare');
143 my $rentaldiscount = $input->param('rentaldiscount');
144 my $opacitemholds = $input->param('opacitemholds') || 0;
145 my $overduefinescap = $input->param('overduefinescap') || undef;
146 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
148 my $schema = Koha
::Database
->new()->schema();
149 my $rs = $schema->resultset('Issuingrule');
153 categorycode
=> $bor,
154 itemtype
=> $itemtype,
156 finedays
=> $finedays,
157 maxsuspensiondays
=> $maxsuspensiondays,
158 firstremind
=> $firstremind,
159 chargeperiod
=> $chargeperiod,
160 chargeperiod_charge_at
=> $chargeperiod_charge_at,
161 maxissueqty
=> $maxissueqty,
162 maxonsiteissueqty
=> $maxonsiteissueqty,
163 renewalsallowed
=> $renewalsallowed,
164 renewalperiod
=> $renewalperiod,
165 norenewalbefore
=> $norenewalbefore,
166 auto_renew
=> $auto_renew,
167 reservesallowed
=> $reservesallowed,
168 issuelength
=> $issuelength,
169 lengthunit
=> $lengthunit,
170 hardduedate
=> $hardduedate,
171 hardduedatecompare
=> $hardduedatecompare,
172 rentaldiscount
=> $rentaldiscount,
173 onshelfholds
=> $onshelfholds,
174 opacitemholds
=> $opacitemholds,
175 overduefinescap
=> $overduefinescap,
178 $rs->update_or_create($params);
181 elsif ($op eq "set-branch-defaults") {
182 my $categorycode = $input->param('categorycode');
183 my $maxissueqty = $input->param('maxissueqty');
184 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
185 my $holdallowed = $input->param('holdallowed');
186 my $returnbranch = $input->param('returnbranch');
187 $maxissueqty =~ s/\s//g;
188 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
189 $maxonsiteissueqty =~ s/\s//g;
190 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
191 $holdallowed =~ s/\s//g;
192 $holdallowed = undef if $holdallowed !~ /^\d+/;
194 if ($branch eq "*") {
195 my $sth_search = $dbh->prepare("SELECT count(*) AS total
196 FROM default_circ_rules");
197 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
198 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
199 VALUES (?, ?, ?, ?)");
200 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
201 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
203 $sth_search->execute();
204 my $res = $sth_search->fetchrow_hashref();
206 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
208 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
211 my $sth_search = $dbh->prepare("SELECT count(*) AS total
212 FROM default_branch_circ_rules
213 WHERE branchcode = ?");
214 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
215 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
216 VALUES (?, ?, ?, ?, ?)");
217 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
218 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
219 WHERE branchcode = ?");
220 $sth_search->execute($branch);
221 my $res = $sth_search->fetchrow_hashref();
223 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
225 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
229 elsif ($op eq "add-branch-cat") {
230 my $categorycode = $input->param('categorycode');
231 my $maxissueqty = $input->param('maxissueqty');
232 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
233 $maxissueqty =~ s/\s//g;
234 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
235 $maxonsiteissueqty =~ s/\s//g;
236 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
238 if ($branch eq "*") {
239 if ($categorycode eq "*") {
240 my $sth_search = $dbh->prepare("SELECT count(*) AS total
241 FROM default_circ_rules");
242 my $sth_insert = $dbh->prepare(q
|
243 INSERT INTO default_circ_rules
244 (maxissueqty
, maxonsiteissueqty
)
247 my $sth_update = $dbh->prepare(q
|
248 UPDATE default_circ_rules
250 maxonsiteissueqty
= ?
253 $sth_search->execute();
254 my $res = $sth_search->fetchrow_hashref();
256 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
258 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
261 my $sth_search = $dbh->prepare("SELECT count(*) AS total
262 FROM default_borrower_circ_rules
263 WHERE categorycode = ?");
264 my $sth_insert = $dbh->prepare(q
|
265 INSERT INTO default_borrower_circ_rules
266 (categorycode
, maxissueqty
, maxonsiteissueqty
)
269 my $sth_update = $dbh->prepare(q
|
270 UPDATE default_borrower_circ_rules
272 maxonsiteissueqty
= ?
273 WHERE categorycode
= ?
275 $sth_search->execute($branch);
276 my $res = $sth_search->fetchrow_hashref();
278 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
280 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
283 } elsif ($categorycode eq "*") {
284 my $sth_search = $dbh->prepare("SELECT count(*) AS total
285 FROM default_branch_circ_rules
286 WHERE branchcode = ?");
287 my $sth_insert = $dbh->prepare(q
|
288 INSERT INTO default_branch_circ_rules
289 (branchcode
, maxissueqty
, maxonsiteissueqty
)
292 my $sth_update = $dbh->prepare(q
|
293 UPDATE default_branch_circ_rules
295 maxonsiteissueqty
= ?
298 $sth_search->execute($branch);
299 my $res = $sth_search->fetchrow_hashref();
301 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
303 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
306 my $sth_search = $dbh->prepare("SELECT count(*) AS total
307 FROM branch_borrower_circ_rules
309 AND categorycode = ?");
310 my $sth_insert = $dbh->prepare(q
|
311 INSERT INTO branch_borrower_circ_rules
312 (branchcode
, categorycode
, maxissueqty
, maxonsiteissueqty
)
315 my $sth_update = $dbh->prepare(q
|
316 UPDATE branch_borrower_circ_rules
318 maxonsiteissueqty
= ?
323 $sth_search->execute($branch, $categorycode);
324 my $res = $sth_search->fetchrow_hashref();
326 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
328 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
332 elsif ($op eq "add-branch-item") {
333 my $itemtype = $input->param('itemtype');
334 my $holdallowed = $input->param('holdallowed');
335 my $returnbranch = $input->param('returnbranch');
336 $holdallowed =~ s/\s//g;
337 $holdallowed = undef if $holdallowed !~ /^\d+/;
339 if ($branch eq "*") {
340 if ($itemtype eq "*") {
341 my $sth_search = $dbh->prepare("SELECT count(*) AS total
342 FROM default_circ_rules");
343 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
344 (holdallowed, returnbranch)
346 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
347 SET holdallowed = ?, returnbranch = ?");
349 $sth_search->execute();
350 my $res = $sth_search->fetchrow_hashref();
352 $sth_update->execute($holdallowed, $returnbranch);
354 $sth_insert->execute($holdallowed, $returnbranch);
357 my $sth_search = $dbh->prepare("SELECT count(*) AS total
358 FROM default_branch_item_rules
359 WHERE itemtype = ?");
360 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
361 (itemtype, holdallowed, returnbranch)
363 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
364 SET holdallowed = ?, returnbranch = ?
365 WHERE itemtype = ?");
366 $sth_search->execute($itemtype);
367 my $res = $sth_search->fetchrow_hashref();
369 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
371 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
374 } elsif ($itemtype eq "*") {
375 my $sth_search = $dbh->prepare("SELECT count(*) AS total
376 FROM default_branch_circ_rules
377 WHERE branchcode = ?");
378 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
379 (branchcode, holdallowed, returnbranch)
381 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
382 SET holdallowed = ?, returnbranch = ?
383 WHERE branchcode = ?");
384 $sth_search->execute($branch);
385 my $res = $sth_search->fetchrow_hashref();
387 $sth_update->execute($holdallowed, $returnbranch, $branch);
389 $sth_insert->execute($branch, $holdallowed, $returnbranch);
392 my $sth_search = $dbh->prepare("SELECT count(*) AS total
393 FROM branch_item_rules
396 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
397 (branchcode, itemtype, holdallowed, returnbranch)
398 VALUES (?, ?, ?, ?)");
399 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
400 SET holdallowed = ?, returnbranch = ?
404 $sth_search->execute($branch, $itemtype);
405 my $res = $sth_search->fetchrow_hashref();
407 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
409 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
414 my $branches = GetBranches
();
416 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
418 value
=> $thisbranch,
419 selected
=> $thisbranch eq $branch,
420 branchname
=> $branches->{$thisbranch}->{'branchname'},
424 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
427 while (my $data=$sth->fetchrow_hashref){
428 push @category_loop,$data;
433 my @itemtypes = @
{ GetItemTypes
( style
=> 'array' ) };
435 my $sth2 = $dbh->prepare("
436 SELECT issuingrules.*,
437 itemtypes.description AS humanitemtype,
438 categories.description AS humancategorycode,
439 COALESCE( localization.translation, itemtypes.description ) AS translated_description
442 ON (itemtypes.itemtype = issuingrules.itemtype)
444 ON (categories.categorycode = issuingrules.categorycode)
445 LEFT JOIN localization ON issuingrules.itemtype = localization.code
446 AND localization.entity = 'itemtypes'
447 AND localization.lang = ?
448 WHERE issuingrules.branchcode = ?
450 $sth2->execute($language, $branch);
452 while (my $row = $sth2->fetchrow_hashref) {
453 $row->{'current_branch'} ||= $row->{'branchcode'};
454 $row->{'humanitemtype'} ||= $row->{'itemtype'};
455 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
456 $row->{'humancategorycode'} ||= $row->{'categorycode'};
457 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
458 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
459 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
460 my $harddue_dt = eval { dt_from_string
( $row->{'hardduedate'} ) };
461 $row->{'hardduedate'} = eval { output_pref
( { dt
=> $harddue_dt, dateonly
=> 1 } ) } if ( $harddue_dt );
462 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
463 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
464 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
466 $row->{'hardduedate'} = 0;
468 push @row_loop, $row;
472 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
475 if ($branch eq "*") {
476 $sth_branch_cat = $dbh->prepare("
477 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
478 FROM default_borrower_circ_rules
479 JOIN categories USING (categorycode)
482 $sth_branch_cat->execute();
484 $sth_branch_cat = $dbh->prepare("
485 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
486 FROM branch_borrower_circ_rules
487 JOIN categories USING (categorycode)
488 WHERE branch_borrower_circ_rules.branchcode = ?
490 $sth_branch_cat->execute($branch);
493 my @branch_cat_rules = ();
494 while (my $row = $sth_branch_cat->fetchrow_hashref) {
495 push @branch_cat_rules, $row;
497 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
499 # note undef maxissueqty so that template can deal with them
500 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
501 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
502 $entry->{unlimited_maxonsiteissueqty
} = 1 unless defined($entry->{maxonsiteissueqty
});
505 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
508 if ($branch eq "*") {
509 $sth_branch_item = $dbh->prepare("
510 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
511 FROM default_branch_item_rules
512 JOIN itemtypes USING (itemtype)
514 $sth_branch_item->execute();
516 $sth_branch_item = $dbh->prepare("
517 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
518 FROM branch_item_rules
519 JOIN itemtypes USING (itemtype)
520 WHERE branch_item_rules.branchcode = ?
522 $sth_branch_item->execute($branch);
525 my @branch_item_rules = ();
526 while (my $row = $sth_branch_item->fetchrow_hashref) {
527 push @branch_item_rules, $row;
529 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
531 # note undef holdallowed so that template can deal with them
532 foreach my $entry (@sorted_branch_item_rules) {
533 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
534 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
537 $template->param(show_branch_cat_rule_form
=> 1);
538 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
539 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
542 if ($branch eq "*") {
543 $sth_defaults = $dbh->prepare("
545 FROM default_circ_rules
547 $sth_defaults->execute();
549 $sth_defaults = $dbh->prepare("
551 FROM default_branch_circ_rules
554 $sth_defaults->execute($branch);
557 my $defaults = $sth_defaults->fetchrow_hashref;
560 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
561 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
562 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
563 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
564 $template->param(default_maxonsiteissueqty
=> $defaults->{maxonsiteissueqty
});
565 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
568 $template->param(default_rules
=> ($defaults ?
1 : 0));
570 $template->param(categoryloop
=> \
@category_loop,
571 itemtypeloop
=> \
@itemtypes,
572 rules
=> \
@sorted_row_loop,
573 branchloop
=> \
@branchloop,
574 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
575 current_branch
=> $branch,
576 definedbranch
=> scalar(@sorted_row_loop)>0
578 output_html_with_http_headers
$input, $cookie, $template->output;
582 # sort by patron category, then item type, putting
583 # default entries at the bottom
584 sub by_category_and_itemtype
{
585 unless (by_category
($a, $b)) {
586 return by_itemtype
($a, $b);
592 if ($a->{'default_humancategorycode'}) {
593 return ($b->{'default_humancategorycode'} ?
0 : 1);
594 } elsif ($b->{'default_humancategorycode'}) {
597 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
603 if ($a->{'default_humanitemtype'}) {
604 return ($b->{'default_humanitemtype'} ?
0 : 1);
605 } elsif ($b->{'default_humanitemtype'}) {
608 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};