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 =~ /^\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 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
130 my $lengthunit = $input->param('lengthunit');
131 my $hardduedate = $input->param('hardduedate') || undef;
132 $hardduedate = eval { dt_from_string
( $input->param('hardduedate') ) } if ( $hardduedate );
133 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
134 my $hardduedatecompare = $input->param('hardduedatecompare');
135 my $rentaldiscount = $input->param('rentaldiscount');
136 my $opacitemholds = $input->param('opacitemholds') || 0;
137 my $overduefinescap = $input->param('overduefinescap') || undef;
138 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty";
140 my $schema = Koha
::Database
->new()->schema();
141 my $rs = $schema->resultset('Issuingrule');
145 categorycode
=> $bor,
146 itemtype
=> $itemtype,
148 finedays
=> $finedays,
149 maxsuspensiondays
=> $maxsuspensiondays,
150 firstremind
=> $firstremind,
151 chargeperiod
=> $chargeperiod,
152 chargeperiod_charge_at
=> $chargeperiod_charge_at,
153 maxissueqty
=> $maxissueqty,
154 maxonsiteissueqty
=> $maxonsiteissueqty,
155 renewalsallowed
=> $renewalsallowed,
156 renewalperiod
=> $renewalperiod,
157 norenewalbefore
=> $norenewalbefore,
158 auto_renew
=> $auto_renew,
159 reservesallowed
=> $reservesallowed,
160 issuelength
=> $issuelength,
161 lengthunit
=> $lengthunit,
162 hardduedate
=> $hardduedate,
163 hardduedatecompare
=> $hardduedatecompare,
164 rentaldiscount
=> $rentaldiscount,
165 onshelfholds
=> $onshelfholds,
166 opacitemholds
=> $opacitemholds,
167 overduefinescap
=> $overduefinescap,
170 $rs->update_or_create($params);
173 elsif ($op eq "set-branch-defaults") {
174 my $categorycode = $input->param('categorycode');
175 my $maxissueqty = $input->param('maxissueqty');
176 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
177 my $holdallowed = $input->param('holdallowed');
178 my $returnbranch = $input->param('returnbranch');
179 $maxissueqty =~ s/\s//g;
180 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
181 $maxonsiteissueqty =~ s/\s//g;
182 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
183 $holdallowed =~ s/\s//g;
184 $holdallowed = undef if $holdallowed !~ /^\d+/;
186 if ($branch 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
190 (maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
191 VALUES (?, ?, ?, ?)");
192 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
193 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?");
195 $sth_search->execute();
196 my $res = $sth_search->fetchrow_hashref();
198 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
200 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
203 my $sth_search = $dbh->prepare("SELECT count(*) AS total
204 FROM default_branch_circ_rules
205 WHERE branchcode = ?");
206 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
207 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
208 VALUES (?, ?, ?, ?, ?)");
209 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
210 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, returnbranch = ?
211 WHERE branchcode = ?");
212 $sth_search->execute($branch);
213 my $res = $sth_search->fetchrow_hashref();
215 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch, $branch);
217 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $returnbranch);
221 elsif ($op eq "add-branch-cat") {
222 my $categorycode = $input->param('categorycode');
223 my $maxissueqty = $input->param('maxissueqty');
224 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
225 $maxissueqty =~ s/\s//g;
226 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
227 $maxonsiteissueqty =~ s/\s//g;
228 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
230 if ($branch eq "*") {
231 if ($categorycode eq "*") {
232 my $sth_search = $dbh->prepare("SELECT count(*) AS total
233 FROM default_circ_rules");
234 my $sth_insert = $dbh->prepare(q
|
235 INSERT INTO default_circ_rules
236 (maxissueqty
, maxonsiteissueqty
)
239 my $sth_update = $dbh->prepare(q
|
240 UPDATE default_circ_rules
242 maxonsiteissueqty
= ?
245 $sth_search->execute();
246 my $res = $sth_search->fetchrow_hashref();
248 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
250 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
253 my $sth_search = $dbh->prepare("SELECT count(*) AS total
254 FROM default_borrower_circ_rules
255 WHERE categorycode = ?");
256 my $sth_insert = $dbh->prepare(q
|
257 INSERT INTO default_borrower_circ_rules
258 (categorycode
, maxissueqty
, maxonsiteissueqty
)
261 my $sth_update = $dbh->prepare(q
|
262 UPDATE default_borrower_circ_rules
264 maxonsiteissueqty
= ?
265 WHERE categorycode
= ?
267 $sth_search->execute($branch);
268 my $res = $sth_search->fetchrow_hashref();
270 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
272 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
275 } elsif ($categorycode eq "*") {
276 my $sth_search = $dbh->prepare("SELECT count(*) AS total
277 FROM default_branch_circ_rules
278 WHERE branchcode = ?");
279 my $sth_insert = $dbh->prepare(q
|
280 INSERT INTO default_branch_circ_rules
281 (branchcode
, maxissueqty
, maxonsiteissueqty
)
284 my $sth_update = $dbh->prepare(q
|
285 UPDATE default_branch_circ_rules
287 maxonsiteissueqty
= ?
290 $sth_search->execute($branch);
291 my $res = $sth_search->fetchrow_hashref();
293 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
295 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
298 my $sth_search = $dbh->prepare("SELECT count(*) AS total
299 FROM branch_borrower_circ_rules
301 AND categorycode = ?");
302 my $sth_insert = $dbh->prepare(q
|
303 INSERT INTO branch_borrower_circ_rules
304 (branchcode
, categorycode
, maxissueqty
, maxonsiteissueqty
)
307 my $sth_update = $dbh->prepare(q
|
308 UPDATE branch_borrower_circ_rules
310 maxonsiteissueqty
= ?
315 $sth_search->execute($branch, $categorycode);
316 my $res = $sth_search->fetchrow_hashref();
318 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
320 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
324 elsif ($op eq "add-branch-item") {
325 my $itemtype = $input->param('itemtype');
326 my $holdallowed = $input->param('holdallowed');
327 my $returnbranch = $input->param('returnbranch');
328 $holdallowed =~ s/\s//g;
329 $holdallowed = undef if $holdallowed !~ /^\d+/;
331 if ($branch eq "*") {
332 if ($itemtype eq "*") {
333 my $sth_search = $dbh->prepare("SELECT count(*) AS total
334 FROM default_circ_rules");
335 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
336 (holdallowed, returnbranch)
338 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
339 SET holdallowed = ?, returnbranch = ?");
341 $sth_search->execute();
342 my $res = $sth_search->fetchrow_hashref();
344 $sth_update->execute($holdallowed, $returnbranch);
346 $sth_insert->execute($holdallowed, $returnbranch);
349 my $sth_search = $dbh->prepare("SELECT count(*) AS total
350 FROM default_branch_item_rules
351 WHERE itemtype = ?");
352 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
353 (itemtype, holdallowed, returnbranch)
355 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
356 SET holdallowed = ?, returnbranch = ?
357 WHERE itemtype = ?");
358 $sth_search->execute($itemtype);
359 my $res = $sth_search->fetchrow_hashref();
361 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
363 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
366 } elsif ($itemtype eq "*") {
367 my $sth_search = $dbh->prepare("SELECT count(*) AS total
368 FROM default_branch_circ_rules
369 WHERE branchcode = ?");
370 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
371 (branchcode, holdallowed, returnbranch)
373 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
374 SET holdallowed = ?, returnbranch = ?
375 WHERE branchcode = ?");
376 $sth_search->execute($branch);
377 my $res = $sth_search->fetchrow_hashref();
379 $sth_update->execute($holdallowed, $returnbranch, $branch);
381 $sth_insert->execute($branch, $holdallowed, $returnbranch);
384 my $sth_search = $dbh->prepare("SELECT count(*) AS total
385 FROM branch_item_rules
388 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
389 (branchcode, itemtype, holdallowed, returnbranch)
390 VALUES (?, ?, ?, ?)");
391 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
392 SET holdallowed = ?, returnbranch = ?
396 $sth_search->execute($branch, $itemtype);
397 my $res = $sth_search->fetchrow_hashref();
399 $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
401 $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
406 my $branches = GetBranches
();
408 for my $thisbranch (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
410 value
=> $thisbranch,
411 selected
=> $thisbranch eq $branch,
412 branchname
=> $branches->{$thisbranch}->{'branchname'},
416 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
419 while (my $data=$sth->fetchrow_hashref){
420 push @category_loop,$data;
425 my @itemtypes = @
{ GetItemTypes
( style
=> 'array' ) };
426 @itemtypes = sort { lc $a->{translated_description
} cmp lc $b->{translated_description
} } @itemtypes;
428 my $sth2 = $dbh->prepare("
429 SELECT issuingrules.*,
430 itemtypes.description AS humanitemtype,
431 categories.description AS humancategorycode,
432 COALESCE( localization.translation, itemtypes.description ) AS translated_description
435 ON (itemtypes.itemtype = issuingrules.itemtype)
437 ON (categories.categorycode = issuingrules.categorycode)
438 LEFT JOIN localization ON issuingrules.itemtype = localization.code
439 AND localization.entity = 'itemtypes'
440 AND localization.lang = ?
441 WHERE issuingrules.branchcode = ?
443 $sth2->execute($language, $branch);
445 while (my $row = $sth2->fetchrow_hashref) {
446 $row->{'current_branch'} ||= $row->{'branchcode'};
447 $row->{humanitemtype
} ||= $row->{itemtype
};
448 $row->{default_translated_description
} = 1 if $row->{humanitemtype
} eq '*';
449 $row->{'humancategorycode'} ||= $row->{'categorycode'};
450 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
451 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
452 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
453 my $harddue_dt = eval { dt_from_string
( $row->{'hardduedate'} ) };
454 $row->{'hardduedate'} = eval { output_pref
( { dt
=> $harddue_dt, dateonly
=> 1 } ) } if ( $harddue_dt );
455 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
456 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
457 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
459 $row->{'hardduedate'} = 0;
461 push @row_loop, $row;
465 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
468 if ($branch eq "*") {
469 $sth_branch_cat = $dbh->prepare("
470 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
471 FROM default_borrower_circ_rules
472 JOIN categories USING (categorycode)
475 $sth_branch_cat->execute();
477 $sth_branch_cat = $dbh->prepare("
478 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
479 FROM branch_borrower_circ_rules
480 JOIN categories USING (categorycode)
481 WHERE branch_borrower_circ_rules.branchcode = ?
483 $sth_branch_cat->execute($branch);
486 my @branch_cat_rules = ();
487 while (my $row = $sth_branch_cat->fetchrow_hashref) {
488 push @branch_cat_rules, $row;
490 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
492 # note undef maxissueqty so that template can deal with them
493 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
494 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
495 $entry->{unlimited_maxonsiteissueqty
} = 1 unless defined($entry->{maxonsiteissueqty
});
498 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
501 if ($branch eq "*") {
502 $sth_branch_item = $dbh->prepare("
503 SELECT default_branch_item_rules.*,
504 COALESCE( localization.translation, itemtypes.description ) AS translated_description
505 FROM default_branch_item_rules
506 JOIN itemtypes USING (itemtype)
507 LEFT JOIN localization ON itemtypes.itemtype = localization.code
508 AND localization.entity = 'itemtypes'
509 AND localization.lang = ?
511 $sth_branch_item->execute($language);
513 $sth_branch_item = $dbh->prepare("
514 SELECT branch_item_rules.*,
515 COALESCE( localization.translation, itemtypes.description ) AS translated_description
516 FROM branch_item_rules
517 JOIN itemtypes USING (itemtype)
518 LEFT JOIN localization ON itemtypes.itemtype = localization.code
519 AND localization.entity = 'itemtypes'
520 AND localization.lang = ?
521 WHERE branch_item_rules.branchcode = ?
523 $sth_branch_item->execute($language, $branch);
526 my @branch_item_rules = ();
527 while (my $row = $sth_branch_item->fetchrow_hashref) {
528 push @branch_item_rules, $row;
530 my @sorted_branch_item_rules = sort { lc $a->{translated_description
} cmp lc $b->{translated_description
} } @branch_item_rules;
532 # note undef holdallowed so that template can deal with them
533 foreach my $entry (@sorted_branch_item_rules) {
534 $entry->{holdallowed_any
} = 1 if($entry->{holdallowed
} == 2);
535 $entry->{holdallowed_same
} = 1 if($entry->{holdallowed
} == 1);
538 $template->param(show_branch_cat_rule_form
=> 1);
539 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
540 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
543 if ($branch eq "*") {
544 $sth_defaults = $dbh->prepare("
546 FROM default_circ_rules
548 $sth_defaults->execute();
550 $sth_defaults = $dbh->prepare("
552 FROM default_branch_circ_rules
555 $sth_defaults->execute($branch);
558 my $defaults = $sth_defaults->fetchrow_hashref;
561 $template->param(default_holdallowed_none
=> 1) if($defaults->{holdallowed
} == 0);
562 $template->param(default_holdallowed_same
=> 1) if($defaults->{holdallowed
} == 1);
563 $template->param(default_holdallowed_any
=> 1) if($defaults->{holdallowed
} == 2);
564 $template->param(default_maxissueqty
=> $defaults->{maxissueqty
});
565 $template->param(default_maxonsiteissueqty
=> $defaults->{maxonsiteissueqty
});
566 $template->param(default_returnbranch
=> $defaults->{returnbranch
});
569 $template->param(default_rules
=> ($defaults ?
1 : 0));
571 $template->param(categoryloop
=> \
@category_loop,
572 itemtypeloop
=> \
@itemtypes,
573 rules
=> \
@sorted_row_loop,
574 branchloop
=> \
@branchloop,
575 humanbranch
=> ($branch ne '*' ?
$branches->{$branch}->{branchname
} : ''),
576 current_branch
=> $branch,
577 definedbranch
=> scalar(@sorted_row_loop)>0
579 output_html_with_http_headers
$input, $cookie, $template->output;
583 # sort by patron category, then item type, putting
584 # default entries at the bottom
585 sub by_category_and_itemtype
{
586 unless (by_category
($a, $b)) {
587 return by_itemtype
($a, $b);
593 if ($a->{'default_humancategorycode'}) {
594 return ($b->{'default_humancategorycode'} ?
0 : 1);
595 } elsif ($b->{'default_humancategorycode'}) {
598 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
604 if ($a->{default_translated_description
}) {
605 return ($b->{'default_translated_description'} ?
0 : 1);
606 } elsif ($b->{'default_translated_description'}) {
609 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};