Bug 20581: (follow-up) Fix fallback status_alias
[koha.git] / admin / smart-rules.pl
blobfcb5b7efb1c9d02f5ace74f0c9f017fd58138e20
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Debug;
27 use Koha::DateUtils;
28 use Koha::Database;
29 use Koha::IssuingRule;
30 use Koha::IssuingRules;
31 use Koha::Logger;
32 use Koha::RefundLostItemFeeRule;
33 use Koha::RefundLostItemFeeRules;
34 use Koha::Libraries;
35 use Koha::CirculationRules;
36 use Koha::Patron::Categories;
37 use Koha::Caches;
38 use Koha::Patrons;
40 my $input = CGI->new;
41 my $dbh = C4::Context->dbh;
43 # my $flagsrequired;
44 # $flagsrequired->{circulation}=1;
45 my ($template, $loggedinuser, $cookie)
46 = get_template_and_user({template_name => "admin/smart-rules.tt",
47 query => $input,
48 type => "intranet",
49 authnotrequired => 0,
50 flagsrequired => {parameters => 'manage_circ_rules'},
51 debug => 1,
52 });
54 my $type=$input->param('type');
56 my $branch = $input->param('branch');
57 unless ( $branch ) {
58 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
59 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
61 else {
62 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
66 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
68 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
69 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
70 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
72 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
74 my $op = $input->param('op') || q{};
75 my $language = C4::Languages::getlanguage();
77 my $cache = Koha::Caches->get_instance;
78 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
80 if ($op eq 'delete') {
81 my $itemtype = $input->param('itemtype');
82 my $categorycode = $input->param('categorycode');
83 $debug and warn "deleting $1 $2 $branch";
85 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
86 $sth_Idelete->execute($branch, $categorycode, $itemtype);
88 elsif ($op eq 'delete-branch-cat') {
89 my $categorycode = $input->param('categorycode');
90 if ($branch eq "*") {
91 if ($categorycode eq "*") {
92 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
93 $sth_delete->execute();
94 } else {
95 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
96 WHERE categorycode = ?");
97 $sth_delete->execute($categorycode);
99 } elsif ($categorycode eq "*") {
100 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
101 WHERE branchcode = ?");
102 $sth_delete->execute($branch);
103 } else {
104 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
105 WHERE branchcode = ?
106 AND categorycode = ?");
107 $sth_delete->execute($branch, $categorycode);
109 Koha::CirculationRules->set_rule(
111 branchcode => $branch,
112 categorycode => $categorycode,
113 itemtype => undef,
114 rule_name => 'max_holds',
115 rule_value => undef,
119 elsif ($op eq 'delete-branch-item') {
120 my $itemtype = $input->param('itemtype');
121 if ($branch eq "*") {
122 if ($itemtype eq "*") {
123 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
124 $sth_delete->execute();
125 } else {
126 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
127 WHERE itemtype = ?");
128 $sth_delete->execute($itemtype);
130 } elsif ($itemtype eq "*") {
131 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
132 WHERE branchcode = ?");
133 $sth_delete->execute($branch);
134 } else {
135 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
136 WHERE branchcode = ?
137 AND itemtype = ?");
138 $sth_delete->execute($branch, $itemtype);
141 # save the values entered
142 elsif ($op eq 'add') {
143 my $br = $branch; # branch
144 my $bor = $input->param('categorycode'); # borrower category
145 my $itemtype = $input->param('itemtype'); # item type
146 my $fine = $input->param('fine');
147 my $finedays = $input->param('finedays');
148 my $maxsuspensiondays = $input->param('maxsuspensiondays');
149 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
150 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
151 my $firstremind = $input->param('firstremind');
152 my $chargeperiod = $input->param('chargeperiod');
153 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
154 my $maxissueqty = $input->param('maxissueqty');
155 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
156 my $renewalsallowed = $input->param('renewalsallowed');
157 my $renewalperiod = $input->param('renewalperiod');
158 my $norenewalbefore = $input->param('norenewalbefore');
159 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
160 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
161 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
162 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
163 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
164 $no_auto_renewal_after_hard_limit = eval { dt_from_string( $input->param('no_auto_renewal_after_hard_limit') ) } if ( $no_auto_renewal_after_hard_limit );
165 $no_auto_renewal_after_hard_limit = output_pref( { dt => $no_auto_renewal_after_hard_limit, dateonly => 1, dateformat => 'iso' } ) if ( $no_auto_renewal_after_hard_limit );
166 my $reservesallowed = $input->param('reservesallowed');
167 my $holds_per_record = $input->param('holds_per_record');
168 my $holds_per_day = $input->param('holds_per_day');
169 $holds_per_day =~ s/\s//g;
170 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
171 my $onshelfholds = $input->param('onshelfholds') || 0;
172 $maxissueqty =~ s/\s//g;
173 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
174 $maxonsiteissueqty =~ s/\s//g;
175 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
176 my $issuelength = $input->param('issuelength');
177 $issuelength = $issuelength eq q{} ? undef : $issuelength;
178 my $lengthunit = $input->param('lengthunit');
179 my $hardduedate = $input->param('hardduedate') || undef;
180 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
181 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
182 my $hardduedatecompare = $input->param('hardduedatecompare');
183 my $rentaldiscount = $input->param('rentaldiscount');
184 my $opacitemholds = $input->param('opacitemholds') || 0;
185 my $article_requests = $input->param('article_requests') || 'no';
186 my $overduefinescap = $input->param('overduefinescap') || undef;
187 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
188 my $note = $input->param('note');
189 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
191 my $params = {
192 branchcode => $br,
193 categorycode => $bor,
194 itemtype => $itemtype,
195 fine => $fine,
196 finedays => $finedays,
197 maxsuspensiondays => $maxsuspensiondays,
198 suspension_chargeperiod => $suspension_chargeperiod,
199 firstremind => $firstremind,
200 chargeperiod => $chargeperiod,
201 chargeperiod_charge_at => $chargeperiod_charge_at,
202 maxissueqty => $maxissueqty,
203 maxonsiteissueqty => $maxonsiteissueqty,
204 renewalsallowed => $renewalsallowed,
205 renewalperiod => $renewalperiod,
206 norenewalbefore => $norenewalbefore,
207 auto_renew => $auto_renew,
208 no_auto_renewal_after => $no_auto_renewal_after,
209 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
210 reservesallowed => $reservesallowed,
211 holds_per_record => $holds_per_record,
212 holds_per_day => $holds_per_day,
213 issuelength => $issuelength,
214 lengthunit => $lengthunit,
215 hardduedate => $hardduedate,
216 hardduedatecompare => $hardduedatecompare,
217 rentaldiscount => $rentaldiscount,
218 onshelfholds => $onshelfholds,
219 opacitemholds => $opacitemholds,
220 overduefinescap => $overduefinescap,
221 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
222 article_requests => $article_requests,
223 note => $note,
226 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
227 if ($issuingrule) {
228 $issuingrule->set($params)->store();
229 } else {
230 Koha::IssuingRule->new()->set($params)->store();
234 elsif ($op eq "set-branch-defaults") {
235 my $categorycode = $input->param('categorycode');
236 my $maxissueqty = $input->param('maxissueqty');
237 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
238 my $holdallowed = $input->param('holdallowed');
239 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
240 my $returnbranch = $input->param('returnbranch');
241 my $max_holds = $input->param('max_holds');
242 $maxissueqty =~ s/\s//g;
243 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
244 $maxonsiteissueqty =~ s/\s//g;
245 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
246 $holdallowed =~ s/\s//g;
247 $holdallowed = undef if $holdallowed !~ /^\d+/;
248 $max_holds =~ s/\s//g;
249 $max_holds = '' if $max_holds !~ /^\d+/;
251 if ($branch eq "*") {
252 my $sth_search = $dbh->prepare("SELECT count(*) AS total
253 FROM default_circ_rules");
254 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
255 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
256 VALUES (?, ?, ?, ?, ?)");
257 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
258 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
260 $sth_search->execute();
261 my $res = $sth_search->fetchrow_hashref();
262 if ($res->{total}) {
263 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
264 } else {
265 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
267 } else {
268 my $sth_search = $dbh->prepare("SELECT count(*) AS total
269 FROM default_branch_circ_rules
270 WHERE branchcode = ?");
271 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
272 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
273 VALUES (?, ?, ?, ?, ?, ?)");
274 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
275 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
276 WHERE branchcode = ?");
277 $sth_search->execute($branch);
278 my $res = $sth_search->fetchrow_hashref();
279 if ($res->{total}) {
280 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
281 } else {
282 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
285 Koha::CirculationRules->set_rule(
287 branchcode => $branch,
288 categorycode => undef,
289 itemtype => undef,
290 rule_name => 'max_holds',
291 rule_value => $max_holds,
295 elsif ($op eq "add-branch-cat") {
296 my $categorycode = $input->param('categorycode');
297 my $maxissueqty = $input->param('maxissueqty');
298 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
299 my $max_holds = $input->param('max_holds');
300 $maxissueqty =~ s/\s//g;
301 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
302 $maxonsiteissueqty =~ s/\s//g;
303 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
304 $max_holds =~ s/\s//g;
305 $max_holds = undef if $max_holds !~ /^\d+/;
307 if ($branch eq "*") {
308 if ($categorycode eq "*") {
309 #FIXME This block is will probably be never used
310 my $sth_search = $dbh->prepare("SELECT count(*) AS total
311 FROM default_circ_rules");
312 my $sth_insert = $dbh->prepare(q|
313 INSERT INTO default_circ_rules
314 (maxissueqty, maxonsiteissueqty)
315 VALUES (?, ?)
317 my $sth_update = $dbh->prepare(q|
318 UPDATE default_circ_rules
319 SET maxissueqty = ?,
320 maxonsiteissueqty = ?,
323 $sth_search->execute();
324 my $res = $sth_search->fetchrow_hashref();
325 if ($res->{total}) {
326 $sth_update->execute( $maxissueqty, $maxonsiteissueqty );
327 } else {
328 $sth_insert->execute( $maxissueqty, $maxonsiteissueqty );
331 Koha::CirculationRules->set_rule(
333 branchcode => undef,
334 categorycode => undef,
335 itemtype => undef,
336 rule_name => 'max_holds',
337 rule_value => $max_holds,
340 } else {
341 my $sth_search = $dbh->prepare("SELECT count(*) AS total
342 FROM default_borrower_circ_rules
343 WHERE categorycode = ?");
344 my $sth_insert = $dbh->prepare(q|
345 INSERT INTO default_borrower_circ_rules
346 (categorycode, maxissueqty, maxonsiteissueqty)
347 VALUES ( ?, ?, ?)
349 my $sth_update = $dbh->prepare(q|
350 UPDATE default_borrower_circ_rules
351 SET maxissueqty = ?,
352 maxonsiteissueqty = ?,
353 WHERE categorycode = ?
355 $sth_search->execute($categorycode);
356 my $res = $sth_search->fetchrow_hashref();
357 if ($res->{total}) {
358 $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $categorycode );
359 } else {
360 $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty );
363 Koha::CirculationRules->set_rule(
365 branchcode => undef,
366 categorycode => $categorycode,
367 itemtype => undef,
368 rule_name => 'max_holds',
369 rule_value => $max_holds,
373 } elsif ($categorycode eq "*") {
374 my $sth_search = $dbh->prepare("SELECT count(*) AS total
375 FROM default_branch_circ_rules
376 WHERE branchcode = ?");
377 my $sth_insert = $dbh->prepare(q|
378 INSERT INTO default_branch_circ_rules
379 (branchcode, maxissueqty, maxonsiteissueqty)
380 VALUES (?, ?, ?)
382 my $sth_update = $dbh->prepare(q|
383 UPDATE default_branch_circ_rules
384 SET maxissueqty = ?,
385 maxonsiteissueqty = ?
386 WHERE branchcode = ?
388 $sth_search->execute($branch);
389 my $res = $sth_search->fetchrow_hashref();
390 if ($res->{total}) {
391 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
392 } else {
393 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
395 } else {
396 my $sth_search = $dbh->prepare("SELECT count(*) AS total
397 FROM branch_borrower_circ_rules
398 WHERE branchcode = ?
399 AND categorycode = ?");
400 my $sth_insert = $dbh->prepare(q|
401 INSERT INTO branch_borrower_circ_rules
402 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
403 VALUES (?, ?, ?, ?)
405 my $sth_update = $dbh->prepare(q|
406 UPDATE branch_borrower_circ_rules
407 SET maxissueqty = ?,
408 maxonsiteissueqty = ?
409 WHERE branchcode = ?
410 AND categorycode = ?
413 $sth_search->execute($branch, $categorycode);
414 my $res = $sth_search->fetchrow_hashref();
415 if ($res->{total}) {
416 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
417 } else {
418 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
421 Koha::CirculationRules->set_rule(
423 branchcode => $branch,
424 categorycode => $categorycode,
425 itemtype => undef,
426 rule_name => 'max_holds',
427 rule_value => $max_holds,
432 elsif ($op eq "add-branch-item") {
433 my $itemtype = $input->param('itemtype');
434 my $holdallowed = $input->param('holdallowed');
435 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
436 my $returnbranch = $input->param('returnbranch');
438 $holdallowed =~ s/\s//g;
439 $holdallowed = undef if $holdallowed !~ /^\d+/;
441 if ($branch eq "*") {
442 if ($itemtype eq "*") {
443 my $sth_search = $dbh->prepare("SELECT count(*) AS total
444 FROM default_circ_rules");
445 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
446 (holdallowed, hold_fulfillment_policy, returnbranch)
447 VALUES (?, ?, ?)");
448 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
449 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
451 $sth_search->execute();
452 my $res = $sth_search->fetchrow_hashref();
453 if ($res->{total}) {
454 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
455 } else {
456 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
458 } else {
459 my $sth_search = $dbh->prepare("SELECT count(*) AS total
460 FROM default_branch_item_rules
461 WHERE itemtype = ?");
462 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
463 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
464 VALUES (?, ?, ?, ?)");
465 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
466 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
467 WHERE itemtype = ?");
468 $sth_search->execute($itemtype);
469 my $res = $sth_search->fetchrow_hashref();
470 if ($res->{total}) {
471 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
472 } else {
473 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
476 } elsif ($itemtype eq "*") {
477 my $sth_search = $dbh->prepare("SELECT count(*) AS total
478 FROM default_branch_circ_rules
479 WHERE branchcode = ?");
480 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
481 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
482 VALUES (?, ?, ?, ?)");
483 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
484 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
485 WHERE branchcode = ?");
486 $sth_search->execute($branch);
487 my $res = $sth_search->fetchrow_hashref();
488 if ($res->{total}) {
489 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
490 } else {
491 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
493 } else {
494 my $sth_search = $dbh->prepare("SELECT count(*) AS total
495 FROM branch_item_rules
496 WHERE branchcode = ?
497 AND itemtype = ?");
498 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
499 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
500 VALUES (?, ?, ?, ?, ?)");
501 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
502 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
503 WHERE branchcode = ?
504 AND itemtype = ?");
506 $sth_search->execute($branch, $itemtype);
507 my $res = $sth_search->fetchrow_hashref();
508 if ($res->{total}) {
509 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
510 } else {
511 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
515 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
517 my $refund = $input->param('refund');
519 if ( $refund eq '*' ) {
520 if ( $branch ne '*' ) {
521 # only do something for $refund eq '*' if branch-specific
522 eval {
523 # Delete it so it picks the default
524 Koha::RefundLostItemFeeRules->find({
525 branchcode => $branch
526 })->delete;
529 } else {
530 my $refundRule =
531 Koha::RefundLostItemFeeRules->find({
532 branchcode => $branch
533 }) // Koha::RefundLostItemFeeRule->new;
534 $refundRule->set({
535 branchcode => $branch,
536 refund => $refund
537 })->store;
541 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
542 $template->param(
543 refundLostItemFeeRule => $refundLostItemFeeRule,
544 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
547 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
549 my @row_loop;
550 my $itemtypes = Koha::ItemTypes->search_with_localization;
552 my $sth2 = $dbh->prepare("
553 SELECT issuingrules.*,
554 itemtypes.description AS humanitemtype,
555 categories.description AS humancategorycode,
556 COALESCE( localization.translation, itemtypes.description ) AS translated_description
557 FROM issuingrules
558 LEFT JOIN itemtypes
559 ON (itemtypes.itemtype = issuingrules.itemtype)
560 LEFT JOIN categories
561 ON (categories.categorycode = issuingrules.categorycode)
562 LEFT JOIN localization ON issuingrules.itemtype = localization.code
563 AND localization.entity = 'itemtypes'
564 AND localization.lang = ?
565 WHERE issuingrules.branchcode = ?
567 $sth2->execute($language, $branch);
569 while (my $row = $sth2->fetchrow_hashref) {
570 $row->{'current_branch'} ||= $row->{'branchcode'};
571 $row->{humanitemtype} ||= $row->{itemtype};
572 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
573 $row->{'humancategorycode'} ||= $row->{'categorycode'};
574 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
575 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
576 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
577 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
578 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
579 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
580 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
581 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
582 } else {
583 $row->{'hardduedate'} = 0;
585 if ($row->{no_auto_renewal_after_hard_limit}) {
586 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
587 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
590 push @row_loop, $row;
593 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
595 my $sth_branch_cat;
596 if ($branch eq "*") {
597 $sth_branch_cat = $dbh->prepare("
598 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
599 FROM default_borrower_circ_rules
600 JOIN categories USING (categorycode)
603 $sth_branch_cat->execute();
604 } else {
605 $sth_branch_cat = $dbh->prepare("
606 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
607 FROM branch_borrower_circ_rules
608 JOIN categories USING (categorycode)
609 WHERE branch_borrower_circ_rules.branchcode = ?
611 $sth_branch_cat->execute($branch);
614 my @branch_cat_rules = ();
615 while (my $row = $sth_branch_cat->fetchrow_hashref) {
616 push @branch_cat_rules, $row;
618 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
620 # note undef maxissueqty so that template can deal with them
621 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
622 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
623 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
624 $entry->{unlimited_max_holds} = 1 unless defined($entry->{max_holds});
625 $entry->{unlimited_holds_per_day} = 1 unless defined($entry->{holds_per_day});
628 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
630 my $sth_branch_item;
631 if ($branch eq "*") {
632 $sth_branch_item = $dbh->prepare("
633 SELECT default_branch_item_rules.*,
634 COALESCE( localization.translation, itemtypes.description ) AS translated_description
635 FROM default_branch_item_rules
636 JOIN itemtypes USING (itemtype)
637 LEFT JOIN localization ON itemtypes.itemtype = localization.code
638 AND localization.entity = 'itemtypes'
639 AND localization.lang = ?
641 $sth_branch_item->execute($language);
642 } else {
643 $sth_branch_item = $dbh->prepare("
644 SELECT branch_item_rules.*,
645 COALESCE( localization.translation, itemtypes.description ) AS translated_description
646 FROM branch_item_rules
647 JOIN itemtypes USING (itemtype)
648 LEFT JOIN localization ON itemtypes.itemtype = localization.code
649 AND localization.entity = 'itemtypes'
650 AND localization.lang = ?
651 WHERE branch_item_rules.branchcode = ?
653 $sth_branch_item->execute($language, $branch);
656 my @branch_item_rules = ();
657 while (my $row = $sth_branch_item->fetchrow_hashref) {
658 push @branch_item_rules, $row;
660 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
662 # note undef holdallowed so that template can deal with them
663 foreach my $entry (@sorted_branch_item_rules) {
664 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
665 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
668 $template->param(show_branch_cat_rule_form => 1);
669 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
670 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
672 my $sth_defaults;
673 if ($branch eq "*") {
674 $sth_defaults = $dbh->prepare("
675 SELECT *
676 FROM default_circ_rules
678 $sth_defaults->execute();
679 } else {
680 $sth_defaults = $dbh->prepare("
681 SELECT *
682 FROM default_branch_circ_rules
683 WHERE branchcode = ?
685 $sth_defaults->execute($branch);
688 my $defaults = $sth_defaults->fetchrow_hashref;
690 if ($defaults) {
691 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
692 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
693 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
694 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
695 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
696 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
697 $template->param( default_returnbranch => $defaults->{returnbranch} );
700 $template->param(default_rules => ($defaults ? 1 : 0));
702 $template->param(
703 patron_categories => $patron_categories,
704 itemtypeloop => $itemtypes,
705 rules => \@sorted_row_loop,
706 humanbranch => ($branch ne '*' ? $branch : ''),
707 current_branch => $branch,
708 definedbranch => scalar(@sorted_row_loop)>0
710 output_html_with_http_headers $input, $cookie, $template->output;
712 exit 0;
714 # sort by patron category, then item type, putting
715 # default entries at the bottom
716 sub by_category_and_itemtype {
717 unless (by_category($a, $b)) {
718 return by_itemtype($a, $b);
722 sub by_category {
723 my ($a, $b) = @_;
724 if ($a->{'default_humancategorycode'}) {
725 return ($b->{'default_humancategorycode'} ? 0 : 1);
726 } elsif ($b->{'default_humancategorycode'}) {
727 return -1;
728 } else {
729 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
733 sub by_itemtype {
734 my ($a, $b) = @_;
735 if ($a->{default_translated_description}) {
736 return ($b->{'default_translated_description'} ? 0 : 1);
737 } elsif ($b->{'default_translated_description'}) {
738 return -1;
739 } else {
740 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};