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{};
49 my $language = C4
::Languages
::getlanguage
();
51 if ($op eq 'delete') {
52 my $itemtype = $input->param('itemtype');
53 my $categorycode = $input->param('categorycode');
54 $debug and warn "deleting $1 $2 $branch";
56 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
57 $sth_Idelete->execute($branch, $categorycode, $itemtype);
59 elsif ($op eq 'delete-branch-cat') {
60 my $categorycode = $input->param('categorycode');
62 if ($categorycode eq "*") {
63 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
64 $sth_delete->execute();
66 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
67 WHERE categorycode = ?");
68 $sth_delete->execute($categorycode);
70 } elsif ($categorycode eq "*") {
71 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
72 WHERE branchcode = ?");
73 $sth_delete->execute($branch);
75 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
77 AND categorycode = ?");
78 $sth_delete->execute($branch, $categorycode);
81 elsif ($op eq 'delete-branch-item') {
82 my $itemtype = $input->param('itemtype');
84 if ($itemtype eq "*") {
85 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
86 $sth_delete->execute();
88 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
90 $sth_delete->execute($itemtype);
92 } elsif ($itemtype eq "*") {
93 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
94 WHERE branchcode = ?");
95 $sth_delete->execute($branch);
97 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
100 $sth_delete->execute($branch, $itemtype);
103 # save the values entered
104 elsif ($op eq 'add') {
105 my $br = $branch; # branch
106 my $bor = $input->param('categorycode'); # borrower category
107 my $itemtype = $input->param('itemtype'); # item type
108 my $fine = $input->param('fine');
109 my $finedays = $input->param('finedays');
110 my $maxsuspensiondays = $input->param('maxsuspensiondays');
111 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
112 my $firstremind = $input->param('firstremind');
113 my $chargeperiod = $input->param('chargeperiod');
114 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
115 my $maxissueqty = $input->param('maxissueqty');
116 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
117 my $renewalsallowed = $input->param('renewalsallowed');
118 my $renewalperiod = $input->param('renewalperiod');
119 my $norenewalbefore = $input->param('norenewalbefore');
120 $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
121 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
122 my $reservesallowed = $input->param('reservesallowed');
123 my $onshelfholds = $input->param('onshelfholds') || 0;
124 $maxissueqty =~ s/\s//g;
125 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
126 $maxonsiteissueqty =~ s/\s//g;
127 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
128 my $issuelength = $input->param('issuelength');
129 my $lengthunit = $input->param('lengthunit');
130 my $hardduedate = $input->param('hardduedate');
131 $hardduedate = eval { dt_from_string
( $input->param('hardduedate') ) } if ( $hardduedate );
132 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
133 my $hardduedatecompare = $input->param('hardduedatecompare');
134 my $rentaldiscount = $input->param('rentaldiscount');
135 my $opacitemholds = $input->param('opacitemholds') || 0;
136 my $overduefinescap = $input->param('overduefinescap') || undef;
137 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
139 my $schema = Koha
::Database
->new()->schema();
140 my $rs = $schema->resultset('Issuingrule');
144 categorycode
=> $bor,
145 itemtype
=> $itemtype,
147 finedays
=> $finedays,
148 maxsuspensiondays
=> $maxsuspensiondays,
149 firstremind
=> $firstremind,
150 chargeperiod
=> $chargeperiod,
151 chargeperiod_charge_at
=> $chargeperiod_charge_at,
152 maxissueqty
=> $maxissueqty,
153 maxonsiteissueqty
=> $maxonsiteissueqty,
154 renewalsallowed
=> $renewalsallowed,
155 renewalperiod
=> $renewalperiod,
156 norenewalbefore
=> $norenewalbefore,
157 auto_renew
=> $auto_renew,
158 reservesallowed
=> $reservesallowed,
159 issuelength
=> $issuelength,
160 lengthunit
=> $lengthunit,
161 hardduedate
=> $hardduedate,
162 hardduedatecompare
=> $hardduedatecompare,
163 rentaldiscount
=> $rentaldiscount,
164 onshelfholds
=> $onshelfholds,
165 opacitemholds
=> $opacitemholds,
166 overduefinescap
=> $overduefinescap,
169 $rs->update_or_create($params);
172 elsif ($op eq "set-branch-defaults") {
173 my $categorycode = $input->param('categorycode');
174 my $maxissueqty = $input->param('maxissueqty');
175 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
176 my $holdallowed = $input->param('holdallowed');
177 my $returnbranch = $input->param('returnbranch');
178 $maxissueqty =~ s/\s//g;
179 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
180 $maxonsiteissueqty =~ s/\s//g;
181 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
182 $holdallowed =~ s/\s//g;
183 $holdallowed = undef if $holdallowed !~ /^\d+/;
185 if ($branch eq "*") {
186 my $sth_search = $dbh->prepare("SELECT count(*) AS total
187 FROM default_circ_rules");
188 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
189 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
190 VALUES (?, ?, ?, ?)");
191 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
192 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
194 $sth_search->execute();
195 my $res = $sth_search->fetchrow_hashref();
197 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
199 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
202 my $sth_search = $dbh->prepare("SELECT count(*) AS total
203 FROM default_branch_circ_rules
204 WHERE branchcode = ?");
205 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
206 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
207 VALUES (?, ?, ?, ?, ?)");
208 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
209 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
210 WHERE branchcode = ?");
211 $sth_search->execute($branch);
212 my $res = $sth_search->fetchrow_hashref();
214 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
216 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
220 elsif ($op eq "add-branch-cat") {
221 my $categorycode = $input->param('categorycode');
222 my $maxissueqty = $input->param('maxissueqty');
223 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
224 $maxissueqty =~ s/\s//g;
225 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
226 $maxonsiteissueqty =~ s/\s//g;
227 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
229 if ($branch eq "*") {
230 if ($categorycode eq "*") {
231 my $sth_search = $dbh->prepare("SELECT count(*) AS total
232 FROM default_circ_rules");
233 my $sth_insert = $dbh->prepare(q
|
234 INSERT INTO default_circ_rules
235 (maxissueqty
, maxonsiteissueqty
)
238 my $sth_update = $dbh->prepare(q
|
239 UPDATE default_circ_rules
241 maxonsiteissueqty
= ?
244 $sth_search->execute();
245 my $res = $sth_search->fetchrow_hashref();
247 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
249 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
252 my $sth_search = $dbh->prepare("SELECT count(*) AS total
253 FROM default_borrower_circ_rules
254 WHERE categorycode = ?");
255 my $sth_insert = $dbh->prepare(q
|
256 INSERT INTO default_borrower_circ_rules
257 (categorycode
, maxissueqty
, maxonsiteissueqty
)
260 my $sth_update = $dbh->prepare(q
|
261 UPDATE default_borrower_circ_rules
263 maxonsiteissueqty
= ?
264 WHERE categorycode
= ?
266 $sth_search->execute($branch);
267 my $res = $sth_search->fetchrow_hashref();
269 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
271 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
274 } elsif ($categorycode eq "*") {
275 my $sth_search = $dbh->prepare("SELECT count(*) AS total
276 FROM default_branch_circ_rules
277 WHERE branchcode = ?");
278 my $sth_insert = $dbh->prepare(q
|
279 INSERT INTO default_branch_circ_rules
280 (branchcode
, maxissueqty
, maxonsiteissueqty
)
283 my $sth_update = $dbh->prepare(q
|
284 UPDATE default_branch_circ_rules
286 maxonsiteissueqty
= ?
289 $sth_search->execute($branch);
290 my $res = $sth_search->fetchrow_hashref();
292 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
294 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
297 my $sth_search = $dbh->prepare("SELECT count(*) AS total
298 FROM branch_borrower_circ_rules
300 AND categorycode = ?");
301 my $sth_insert = $dbh->prepare(q
|
302 INSERT INTO branch_borrower_circ_rules
303 (branchcode
, categorycode
, maxissueqty
, maxonsiteissueqty
)
306 my $sth_update = $dbh->prepare(q
|
307 UPDATE branch_borrower_circ_rules
309 maxonsiteissueqty
= ?
314 $sth_search->execute($branch, $categorycode);
315 my $res = $sth_search->fetchrow_hashref();
317 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
319 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
323 elsif ($op eq "add-branch-item") {
324 my $itemtype = $input->param('itemtype');
325 my $holdallowed = $input->param('holdallowed');
326 my $returnbranch = $input->param('returnbranch');
327 $holdallowed =~ s/\s//g;
328 $holdallowed = undef if $holdallowed !~ /^\d+/;
330 if ($branch eq "*") {
331 if ($itemtype eq "*") {
332 my $sth_search = $dbh->prepare("SELECT count(*) AS total
333 FROM default_circ_rules");
334 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
335 (holdallowed, returnbranch)
337 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
338 SET holdallowed = ?, returnbranch = ?");
340 $sth_search->execute();
341 my $res = $sth_search->fetchrow_hashref();
343 $sth_update->execute($holdallowed, $returnbranch);
345 $sth_insert->execute($holdallowed, $returnbranch);
348 my $sth_search = $dbh->prepare("SELECT count(*) AS total
349 FROM default_branch_item_rules
350 WHERE itemtype = ?");
351 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
352 (itemtype, holdallowed, returnbranch)
354 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
355 SET holdallowed = ?, returnbranch = ?
356 WHERE itemtype = ?");
357 $sth_search->execute($itemtype);
358 my $res = $sth_search->fetchrow_hashref();
360 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
362 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
365 } elsif ($itemtype eq "*") {
366 my $sth_search = $dbh->prepare("SELECT count(*) AS total
367 FROM default_branch_circ_rules
368 WHERE branchcode = ?");
369 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
370 (branchcode, holdallowed, returnbranch)
372 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
373 SET holdallowed = ?, returnbranch = ?
374 WHERE branchcode = ?");
375 $sth_search->execute($branch);
376 my $res = $sth_search->fetchrow_hashref();
378 $sth_update->execute($holdallowed, $returnbranch, $branch);
380 $sth_insert->execute($branch, $holdallowed, $returnbranch);
383 my $sth_search = $dbh->prepare("SELECT count(*) AS total
384 FROM branch_item_rules
387 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
388 (branchcode, itemtype, holdallowed, returnbranch)
389 VALUES (?, ?, ?, ?)");
390 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
391 SET holdallowed = ?, returnbranch = ?
395 $sth_search->execute($branch, $itemtype);
396 my $res = $sth_search->fetchrow_hashref();
398 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
400 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
405 my $branches = GetBranches
();
407 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
409 value
=> $thisbranch,
410 selected
=> $thisbranch eq $branch,
411 branchname
=> $branches->{$thisbranch}->{'branchname'},
415 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
418 while (my $data=$sth->fetchrow_hashref){
419 push @category_loop,$data;
424 my @itemtypes = @
{ GetItemTypes
( style
=> 'array' ) };
426 my $sth2 = $dbh->prepare("
427 SELECT issuingrules.*,
428 itemtypes.description AS humanitemtype,
429 categories.description AS humancategorycode,
430 COALESCE( localization.translation, itemtypes.description ) AS translated_description
433 ON (itemtypes.itemtype = issuingrules.itemtype)
435 ON (categories.categorycode = issuingrules.categorycode)
436 LEFT JOIN localization ON issuingrules.itemtype = localization.code
437 AND localization.entity = 'itemtypes'
438 AND localization.lang = ?
439 WHERE issuingrules.branchcode = ?
441 $sth2->execute($language, $branch);
443 while (my $row = $sth2->fetchrow_hashref) {
444 $row->{'current_branch'} ||= $row->{'branchcode'};
445 $row->{'humanitemtype'} ||= $row->{'itemtype'};
446 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
447 $row->{'humancategorycode'} ||= $row->{'categorycode'};
448 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
449 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
450 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
451 my $harddue_dt = eval { dt_from_string
( $row->{'hardduedate'} ) };
452 $row->{'hardduedate'} = eval { output_pref
( { dt
=> $harddue_dt, dateonly
=> 1 } ) } if ( $harddue_dt );
453 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
454 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
455 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
457 $row->{'hardduedate'} = 0;
459 push @row_loop, $row;
463 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
466 if ($branch eq "*") {
467 $sth_branch_cat = $dbh->prepare("
468 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
469 FROM default_borrower_circ_rules
470 JOIN categories USING (categorycode)
473 $sth_branch_cat->execute();
475 $sth_branch_cat = $dbh->prepare("
476 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
477 FROM branch_borrower_circ_rules
478 JOIN categories USING (categorycode)
479 WHERE branch_borrower_circ_rules.branchcode = ?
481 $sth_branch_cat->execute($branch);
484 my @branch_cat_rules = ();
485 while (my $row = $sth_branch_cat->fetchrow_hashref) {
486 push @branch_cat_rules, $row;
488 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
490 # note undef maxissueqty so that template can deal with them
491 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
492 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
493 $entry->{unlimited_maxonsiteissueqty
} = 1 unless defined($entry->{maxonsiteissueqty
});
496 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
499 if ($branch eq "*") {
500 $sth_branch_item = $dbh->prepare("
501 SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
502 FROM default_branch_item_rules
503 JOIN itemtypes USING (itemtype)
505 $sth_branch_item->execute();
507 $sth_branch_item = $dbh->prepare("
508 SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
509 FROM branch_item_rules
510 JOIN itemtypes USING (itemtype)
511 WHERE branch_item_rules.branchcode = ?
513 $sth_branch_item->execute($branch);
516 my @branch_item_rules = ();
517 while (my $row = $sth_branch_item->fetchrow_hashref) {
518 push @branch_item_rules, $row;
520 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
522 # note undef holdallowed so that template can deal with them
523 foreach my $entry (@sorted_branch_item_rules) {
524 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
525 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
528 $template->param(show_branch_cat_rule_form
=> 1);
529 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
530 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
533 if ($branch eq "*") {
534 $sth_defaults = $dbh->prepare("
536 FROM default_circ_rules
538 $sth_defaults->execute();
540 $sth_defaults = $dbh->prepare("
542 FROM default_branch_circ_rules
545 $sth_defaults->execute($branch);
548 my $defaults = $sth_defaults->fetchrow_hashref;
551 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
552 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
553 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
554 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
555 $template->param(default_maxonsiteissueqty
=> $defaults->{maxonsiteissueqty
});
556 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
559 $template->param(default_rules
=> ($defaults ?
1 : 0));
561 $template->param(categoryloop
=> \
@category_loop,
562 itemtypeloop
=> \
@itemtypes,
563 rules
=> \
@sorted_row_loop,
564 branchloop
=> \
@branchloop,
565 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
566 current_branch
=> $branch,
567 definedbranch
=> scalar(@sorted_row_loop)>0
569 output_html_with_http_headers
$input, $cookie, $template->output;
573 # sort by patron category, then item type, putting
574 # default entries at the bottom
575 sub by_category_and_itemtype
{
576 unless (by_category
($a, $b)) {
577 return by_itemtype
($a, $b);
583 if ($a->{'default_humancategorycode'}) {
584 return ($b->{'default_humancategorycode'} ?
0 : 1);
585 } elsif ($b->{'default_humancategorycode'}) {
588 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
594 if ($a->{'default_humanitemtype'}) {
595 return ($b->{'default_humanitemtype'} ?
0 : 1);
596 } elsif ($b->{'default_humanitemtype'}) {
599 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};