Bug 5770: (QA follow-up) Adapt for replyto handling
[koha.git] / admin / smart-rules.pl
blob927533d14da1a283a03479711c236b88e945c18b
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 Koha::IssuingRules->find({
86 branchcode => $branch,
87 categorycode => $categorycode,
88 itemtype => $itemtype
89 })->delete;
92 elsif ($op eq 'delete-branch-cat') {
93 my $categorycode = $input->param('categorycode');
94 if ($branch eq "*") {
95 if ($categorycode eq "*") {
96 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
97 $sth_delete->execute();
99 } elsif ($categorycode eq "*") {
100 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
101 WHERE branchcode = ?");
102 $sth_delete->execute($branch);
104 Koha::CirculationRules->set_rules(
106 categorycode => $categorycode eq '*' ? undef : $categorycode,
107 branchcode => $branch eq '*' ? undef : $branch,
108 itemtype => undef,
109 rules => {
110 max_holds => undef,
111 patron_maxissueqty => undef,
112 patron_maxonsiteissueqty => undef,
117 elsif ($op eq 'delete-branch-item') {
118 my $itemtype = $input->param('itemtype');
119 if ($branch eq "*") {
120 if ($itemtype eq "*") {
121 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
122 $sth_delete->execute();
123 } else {
124 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
125 WHERE itemtype = ?");
126 $sth_delete->execute($itemtype);
128 } elsif ($itemtype eq "*") {
129 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
130 WHERE branchcode = ?");
131 $sth_delete->execute($branch);
132 } else {
133 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
134 WHERE branchcode = ?
135 AND itemtype = ?");
136 $sth_delete->execute($branch, $itemtype);
139 # save the values entered
140 elsif ($op eq 'add') {
141 my $br = $branch; # branch
142 my $bor = $input->param('categorycode'); # borrower category
143 my $itemtype = $input->param('itemtype'); # item type
144 my $fine = $input->param('fine');
145 my $finedays = $input->param('finedays');
146 my $maxsuspensiondays = $input->param('maxsuspensiondays');
147 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
148 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
149 my $firstremind = $input->param('firstremind');
150 my $chargeperiod = $input->param('chargeperiod');
151 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
152 my $maxissueqty = $input->param('maxissueqty');
153 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
154 my $renewalsallowed = $input->param('renewalsallowed');
155 my $renewalperiod = $input->param('renewalperiod');
156 my $norenewalbefore = $input->param('norenewalbefore');
157 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
158 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
159 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
160 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
161 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
162 $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 );
163 $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 );
164 my $reservesallowed = $input->param('reservesallowed');
165 my $holds_per_record = $input->param('holds_per_record');
166 my $holds_per_day = $input->param('holds_per_day');
167 $holds_per_day =~ s/\s//g;
168 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
169 my $onshelfholds = $input->param('onshelfholds') || 0;
170 $maxissueqty =~ s/\s//g;
171 $maxissueqty = '' if $maxissueqty !~ /^\d+/;
172 $maxonsiteissueqty =~ s/\s//g;
173 $maxonsiteissueqty = '' if $maxonsiteissueqty !~ /^\d+/;
174 my $issuelength = $input->param('issuelength');
175 $issuelength = $issuelength eq q{} ? undef : $issuelength;
176 my $lengthunit = $input->param('lengthunit');
177 my $hardduedate = $input->param('hardduedate') || undef;
178 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
179 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
180 my $hardduedatecompare = $input->param('hardduedatecompare');
181 my $rentaldiscount = $input->param('rentaldiscount');
182 my $opacitemholds = $input->param('opacitemholds') || 0;
183 my $article_requests = $input->param('article_requests') || 'no';
184 my $overduefinescap = $input->param('overduefinescap') || undef;
185 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
186 my $note = $input->param('note');
187 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
189 my $params = {
190 branchcode => $br,
191 categorycode => $bor,
192 itemtype => $itemtype,
193 fine => $fine,
194 finedays => $finedays,
195 maxsuspensiondays => $maxsuspensiondays,
196 suspension_chargeperiod => $suspension_chargeperiod,
197 firstremind => $firstremind,
198 chargeperiod => $chargeperiod,
199 chargeperiod_charge_at => $chargeperiod_charge_at,
200 renewalsallowed => $renewalsallowed,
201 renewalperiod => $renewalperiod,
202 norenewalbefore => $norenewalbefore,
203 auto_renew => $auto_renew,
204 no_auto_renewal_after => $no_auto_renewal_after,
205 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
206 reservesallowed => $reservesallowed,
207 holds_per_record => $holds_per_record,
208 holds_per_day => $holds_per_day,
209 issuelength => $issuelength,
210 lengthunit => $lengthunit,
211 hardduedate => $hardduedate,
212 hardduedatecompare => $hardduedatecompare,
213 rentaldiscount => $rentaldiscount,
214 onshelfholds => $onshelfholds,
215 opacitemholds => $opacitemholds,
216 overduefinescap => $overduefinescap,
217 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
218 article_requests => $article_requests,
219 note => $note,
222 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
223 if ($issuingrule) {
224 $issuingrule->set($params)->store();
225 } else {
226 Koha::IssuingRule->new()->set($params)->store();
228 Koha::CirculationRules->set_rules(
230 categorycode => $bor,
231 itemtype => $itemtype,
232 branchcode => $br,
233 rules => {
234 maxissueqty => $maxissueqty,
235 maxonsiteissueqty => $maxonsiteissueqty,
241 elsif ($op eq "set-branch-defaults") {
242 my $categorycode = $input->param('categorycode');
243 my $patron_maxissueqty = $input->param('patron_maxissueqty');
244 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
245 my $holdallowed = $input->param('holdallowed');
246 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
247 my $returnbranch = $input->param('returnbranch');
248 my $max_holds = $input->param('max_holds');
249 $patron_maxissueqty =~ s/\s//g;
250 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
251 $patron_maxonsiteissueqty =~ s/\s//g;
252 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
253 $holdallowed =~ s/\s//g;
254 $holdallowed = undef if $holdallowed !~ /^\d+/;
255 $max_holds =~ s/\s//g;
256 $max_holds = '' if $max_holds !~ /^\d+/;
258 if ($branch eq "*") {
259 my $sth_search = $dbh->prepare("SELECT count(*) AS total
260 FROM default_circ_rules");
261 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
262 (holdallowed, hold_fulfillment_policy, returnbranch)
263 VALUES (?, ?, ?)");
264 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
265 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
267 $sth_search->execute();
268 my $res = $sth_search->fetchrow_hashref();
269 if ($res->{total}) {
270 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
271 } else {
272 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
275 Koha::CirculationRules->set_rules(
277 categorycode => undef,
278 itemtype => undef,
279 branchcode => undef,
280 rules => {
281 patron_maxissueqty => $patron_maxissueqty,
282 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
286 } else {
287 my $sth_search = $dbh->prepare("SELECT count(*) AS total
288 FROM default_branch_circ_rules
289 WHERE branchcode = ?");
290 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
291 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
292 VALUES (?, ?, ?, ?)");
293 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
294 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
295 WHERE branchcode = ?");
296 $sth_search->execute($branch);
297 my $res = $sth_search->fetchrow_hashref();
298 if ($res->{total}) {
299 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
300 } else {
301 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
304 Koha::CirculationRules->set_rules(
306 categorycode => undef,
307 itemtype => undef,
308 branchcode => $branch,
309 rules => {
310 patron_maxissueqty => $patron_maxissueqty,
311 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
316 Koha::CirculationRules->set_rule(
318 branchcode => $branch,
319 categorycode => undef,
320 itemtype => undef,
321 rule_name => 'max_holds',
322 rule_value => $max_holds,
326 elsif ($op eq "add-branch-cat") {
327 my $categorycode = $input->param('categorycode');
328 my $patron_maxissueqty = $input->param('patron_maxissueqty');
329 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
330 my $max_holds = $input->param('max_holds');
331 $patron_maxissueqty =~ s/\s//g;
332 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
333 $patron_maxonsiteissueqty =~ s/\s//g;
334 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
335 $max_holds =~ s/\s//g;
336 $max_holds = undef if $max_holds !~ /^\d+/;
338 if ($branch eq "*") {
339 if ($categorycode eq "*") {
340 Koha::CirculationRules->set_rules(
342 categorycode => undef,
343 itemtype => undef,
344 branchcode => undef,
345 rules => {
346 max_holds => $max_holds,
347 patron_maxissueqty => $patron_maxissueqty,
348 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
352 } else {
353 Koha::CirculationRules->set_rules(
355 branchcode => undef,
356 categorycode => $categorycode,
357 itemtype => undef,
358 rules => {
359 max_holds => $max_holds,
360 patron_maxissueqty => $patron_maxissueqty,
361 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
366 } elsif ($categorycode eq "*") {
367 Koha::CirculationRules->set_rules(
369 categorycode => undef,
370 itemtype => undef,
371 branchcode => $branch,
372 rules => {
373 max_holds => $max_holds,
374 patron_maxissueqty => $patron_maxissueqty,
375 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
379 } else {
380 Koha::CirculationRules->set_rules(
382 categorycode => $categorycode,
383 itemtype => undef,
384 branchcode => $branch,
385 rules => {
386 max_holds => $max_holds,
387 patron_maxissueqty => $patron_maxissueqty,
388 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
394 elsif ($op eq "add-branch-item") {
395 my $itemtype = $input->param('itemtype');
396 my $holdallowed = $input->param('holdallowed');
397 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
398 my $returnbranch = $input->param('returnbranch');
400 $holdallowed =~ s/\s//g;
401 $holdallowed = undef if $holdallowed !~ /^\d+/;
403 if ($branch eq "*") {
404 if ($itemtype eq "*") {
405 my $sth_search = $dbh->prepare("SELECT count(*) AS total
406 FROM default_circ_rules");
407 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
408 (holdallowed, hold_fulfillment_policy, returnbranch)
409 VALUES (?, ?, ?)");
410 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
411 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
413 $sth_search->execute();
414 my $res = $sth_search->fetchrow_hashref();
415 if ($res->{total}) {
416 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
417 } else {
418 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
420 } else {
421 my $sth_search = $dbh->prepare("SELECT count(*) AS total
422 FROM default_branch_item_rules
423 WHERE itemtype = ?");
424 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
425 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
426 VALUES (?, ?, ?, ?)");
427 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
428 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
429 WHERE itemtype = ?");
430 $sth_search->execute($itemtype);
431 my $res = $sth_search->fetchrow_hashref();
432 if ($res->{total}) {
433 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
434 } else {
435 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
438 } elsif ($itemtype eq "*") {
439 my $sth_search = $dbh->prepare("SELECT count(*) AS total
440 FROM default_branch_circ_rules
441 WHERE branchcode = ?");
442 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
443 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
444 VALUES (?, ?, ?, ?)");
445 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
446 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
447 WHERE branchcode = ?");
448 $sth_search->execute($branch);
449 my $res = $sth_search->fetchrow_hashref();
450 if ($res->{total}) {
451 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
452 } else {
453 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
455 } else {
456 my $sth_search = $dbh->prepare("SELECT count(*) AS total
457 FROM branch_item_rules
458 WHERE branchcode = ?
459 AND itemtype = ?");
460 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
461 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
462 VALUES (?, ?, ?, ?, ?)");
463 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
464 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
465 WHERE branchcode = ?
466 AND itemtype = ?");
468 $sth_search->execute($branch, $itemtype);
469 my $res = $sth_search->fetchrow_hashref();
470 if ($res->{total}) {
471 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
472 } else {
473 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
477 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
479 my $refund = $input->param('refund');
481 if ( $refund eq '*' ) {
482 if ( $branch ne '*' ) {
483 # only do something for $refund eq '*' if branch-specific
484 eval {
485 # Delete it so it picks the default
486 Koha::RefundLostItemFeeRules->find({
487 branchcode => $branch
488 })->delete;
491 } else {
492 my $refundRule =
493 Koha::RefundLostItemFeeRules->find({
494 branchcode => $branch
495 }) // Koha::RefundLostItemFeeRule->new;
496 $refundRule->set({
497 branchcode => $branch,
498 refund => $refund
499 })->store;
503 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
504 $template->param(
505 refundLostItemFeeRule => $refundLostItemFeeRule,
506 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
509 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
511 my @row_loop;
512 my $itemtypes = Koha::ItemTypes->search_with_localization;
514 my $sth2 = $dbh->prepare("
515 SELECT issuingrules.*,
516 itemtypes.description AS humanitemtype,
517 categories.description AS humancategorycode,
518 COALESCE( localization.translation, itemtypes.description ) AS translated_description
519 FROM issuingrules
520 LEFT JOIN itemtypes
521 ON (itemtypes.itemtype = issuingrules.itemtype)
522 LEFT JOIN categories
523 ON (categories.categorycode = issuingrules.categorycode)
524 LEFT JOIN localization ON issuingrules.itemtype = localization.code
525 AND localization.entity = 'itemtypes'
526 AND localization.lang = ?
527 WHERE issuingrules.branchcode = ?
529 $sth2->execute($language, $branch);
531 while (my $row = $sth2->fetchrow_hashref) {
532 $row->{'current_branch'} ||= $row->{'branchcode'};
533 $row->{humanitemtype} ||= $row->{itemtype};
534 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
535 $row->{'humancategorycode'} ||= $row->{'categorycode'};
536 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
537 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
538 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
539 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
540 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
541 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
542 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
543 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
544 } else {
545 $row->{'hardduedate'} = 0;
547 if ($row->{no_auto_renewal_after_hard_limit}) {
548 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
549 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
552 push @row_loop, $row;
555 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
557 my $sth_branch_item;
558 if ($branch eq "*") {
559 $sth_branch_item = $dbh->prepare("
560 SELECT default_branch_item_rules.*,
561 COALESCE( localization.translation, itemtypes.description ) AS translated_description
562 FROM default_branch_item_rules
563 JOIN itemtypes USING (itemtype)
564 LEFT JOIN localization ON itemtypes.itemtype = localization.code
565 AND localization.entity = 'itemtypes'
566 AND localization.lang = ?
568 $sth_branch_item->execute($language);
569 } else {
570 $sth_branch_item = $dbh->prepare("
571 SELECT branch_item_rules.*,
572 COALESCE( localization.translation, itemtypes.description ) AS translated_description
573 FROM branch_item_rules
574 JOIN itemtypes USING (itemtype)
575 LEFT JOIN localization ON itemtypes.itemtype = localization.code
576 AND localization.entity = 'itemtypes'
577 AND localization.lang = ?
578 WHERE branch_item_rules.branchcode = ?
580 $sth_branch_item->execute($language, $branch);
583 my @branch_item_rules = ();
584 while (my $row = $sth_branch_item->fetchrow_hashref) {
585 push @branch_item_rules, $row;
587 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
589 # note undef holdallowed so that template can deal with them
590 foreach my $entry (@sorted_branch_item_rules) {
591 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
592 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
595 $template->param(show_branch_cat_rule_form => 1);
596 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
598 my $sth_defaults;
599 if ($branch eq "*") {
600 $sth_defaults = $dbh->prepare("
601 SELECT *
602 FROM default_circ_rules
604 $sth_defaults->execute();
605 } else {
606 $sth_defaults = $dbh->prepare("
607 SELECT *
608 FROM default_branch_circ_rules
609 WHERE branchcode = ?
611 $sth_defaults->execute($branch);
614 my $defaults = $sth_defaults->fetchrow_hashref;
616 if ($defaults) {
617 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
618 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
619 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
620 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
621 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
622 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
623 $template->param( default_returnbranch => $defaults->{returnbranch} );
626 $template->param(default_rules => ($defaults ? 1 : 0));
628 $template->param(
629 patron_categories => $patron_categories,
630 itemtypeloop => $itemtypes,
631 rules => \@sorted_row_loop,
632 humanbranch => ($branch ne '*' ? $branch : ''),
633 current_branch => $branch,
634 definedbranch => scalar(@sorted_row_loop)>0
636 output_html_with_http_headers $input, $cookie, $template->output;
638 exit 0;
640 # sort by patron category, then item type, putting
641 # default entries at the bottom
642 sub by_category_and_itemtype {
643 unless (by_category($a, $b)) {
644 return by_itemtype($a, $b);
648 sub by_category {
649 my ($a, $b) = @_;
650 if ($a->{'default_humancategorycode'}) {
651 return ($b->{'default_humancategorycode'} ? 0 : 1);
652 } elsif ($b->{'default_humancategorycode'}) {
653 return -1;
654 } else {
655 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
659 sub by_itemtype {
660 my ($a, $b) = @_;
661 if ($a->{default_translated_description}) {
662 return ($b->{'default_translated_description'} ? 0 : 1);
663 } elsif ($b->{'default_translated_description'}) {
664 return -1;
665 } else {
666 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};