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 my $lengthunit = $input->param('lengthunit');
130 my $hardduedate = $input->param('hardduedate') || undef;
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' ) };
425 @itemtypes = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @itemtypes;
427 my $sth2 = $dbh->prepare("
428 SELECT issuingrules.*,
429 itemtypes.description AS humanitemtype,
430 categories.description AS humancategorycode,
431 COALESCE( localization.translation, itemtypes.description ) AS translated_description
434 ON (itemtypes.itemtype = issuingrules.itemtype)
436 ON (categories.categorycode = issuingrules.categorycode)
437 LEFT JOIN localization ON issuingrules.itemtype = localization.code
438 AND localization.entity = 'itemtypes'
439 AND localization.lang = ?
440 WHERE issuingrules.branchcode = ?
442 $sth2->execute($language, $branch);
444 while (my $row = $sth2->fetchrow_hashref) {
445 $row->{'current_branch'} ||= $row->{'branchcode'};
446 $row->{translated_description} ||= $row->{translated_description};
447 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
448 $row->{'humancategorycode'} ||= $row->{'categorycode'};
449 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
450 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
451 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
452 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
453 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
454 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
455 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
456 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
458 $row->{'hardduedate'} = 0;
460 push @row_loop, $row;
464 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
467 if ($branch eq "*") {
468 $sth_branch_cat = $dbh->prepare("
469 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
470 FROM default_borrower_circ_rules
471 JOIN categories USING (categorycode)
474 $sth_branch_cat->execute();
476 $sth_branch_cat = $dbh->prepare("
477 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
478 FROM branch_borrower_circ_rules
479 JOIN categories USING (categorycode)
480 WHERE branch_borrower_circ_rules.branchcode = ?
482 $sth_branch_cat->execute($branch);
485 my @branch_cat_rules = ();
486 while (my $row = $sth_branch_cat->fetchrow_hashref) {
487 push @branch_cat_rules, $row;
489 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
491 # note undef maxissueqty so that template can deal with them
492 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
493 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
494 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
497 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
500 if ($branch eq "*") {
501 $sth_branch_item = $dbh->prepare("
502 SELECT default_branch_item_rules.*,
503 COALESCE( localization.translation, itemtypes.description ) AS translated_description
504 FROM default_branch_item_rules
505 JOIN itemtypes USING (itemtype)
506 LEFT JOIN localization ON itemtypes.itemtype = localization.code
507 AND localization.entity = 'itemtypes'
508 AND localization.lang = ?
510 $sth_branch_item->execute($language);
512 $sth_branch_item = $dbh->prepare("
513 SELECT branch_item_rules.*,
514 COALESCE( localization.translation, itemtypes.description ) AS translated_description
515 FROM branch_item_rules
516 JOIN itemtypes USING (itemtype)
517 LEFT JOIN localization ON itemtypes.itemtype = localization.code
518 AND localization.entity = 'itemtypes'
519 AND localization.lang = ?
520 WHERE branch_item_rules.branchcode = ?
522 $sth_branch_item->execute($language, $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 { lc $a->{translated_description} cmp lc $b->{translated_description} } @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_translated_description}) {
604 return ($b->{'default_translated_description'} ? 0 : 1);
605 } elsif ($b->{'default_translated_description'}) {
608 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};