Bug 18837: (follow-up) Add additional tests
[koha.git] / admin / smart-rules.pl
blobe9c1bf121d075e6ed4b167b1c0288ea02d0e0552
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();
95 } elsif ($categorycode eq "*") {
96 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
97 WHERE branchcode = ?");
98 $sth_delete->execute($branch);
100 Koha::CirculationRules->set_rules(
102 categorycode => $categorycode eq '*' ? undef : $categorycode,
103 branchcode => $branch eq '*' ? undef : $branch,
104 itemtype => undef,
105 rules => {
106 max_holds => undef,
107 patron_maxissueqty => undef,
108 patron_maxonsiteissueqty => undef,
113 elsif ($op eq 'delete-branch-item') {
114 my $itemtype = $input->param('itemtype');
115 if ($branch eq "*") {
116 if ($itemtype eq "*") {
117 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
118 $sth_delete->execute();
119 } else {
120 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
121 WHERE itemtype = ?");
122 $sth_delete->execute($itemtype);
124 } elsif ($itemtype eq "*") {
125 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
126 WHERE branchcode = ?");
127 $sth_delete->execute($branch);
128 } else {
129 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
130 WHERE branchcode = ?
131 AND itemtype = ?");
132 $sth_delete->execute($branch, $itemtype);
135 # save the values entered
136 elsif ($op eq 'add') {
137 my $br = $branch; # branch
138 my $bor = $input->param('categorycode'); # borrower category
139 my $itemtype = $input->param('itemtype'); # item type
140 my $fine = $input->param('fine');
141 my $finedays = $input->param('finedays');
142 my $maxsuspensiondays = $input->param('maxsuspensiondays');
143 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
144 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
145 my $firstremind = $input->param('firstremind');
146 my $chargeperiod = $input->param('chargeperiod');
147 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
148 my $maxissueqty = $input->param('maxissueqty');
149 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
150 my $renewalsallowed = $input->param('renewalsallowed');
151 my $renewalperiod = $input->param('renewalperiod');
152 my $norenewalbefore = $input->param('norenewalbefore');
153 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
154 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
155 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
156 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
157 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
158 $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 );
159 $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 );
160 my $reservesallowed = $input->param('reservesallowed');
161 my $holds_per_record = $input->param('holds_per_record');
162 my $holds_per_day = $input->param('holds_per_day');
163 $holds_per_day =~ s/\s//g;
164 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
165 my $onshelfholds = $input->param('onshelfholds') || 0;
166 $maxissueqty =~ s/\s//g;
167 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
168 $maxonsiteissueqty =~ s/\s//g;
169 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
170 my $issuelength = $input->param('issuelength');
171 $issuelength = $issuelength eq q{} ? undef : $issuelength;
172 my $lengthunit = $input->param('lengthunit');
173 my $hardduedate = $input->param('hardduedate') || undef;
174 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
175 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
176 my $hardduedatecompare = $input->param('hardduedatecompare');
177 my $rentaldiscount = $input->param('rentaldiscount');
178 my $opacitemholds = $input->param('opacitemholds') || 0;
179 my $article_requests = $input->param('article_requests') || 'no';
180 my $overduefinescap = $input->param('overduefinescap') || undef;
181 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
182 my $note = $input->param('note');
183 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
185 my $params = {
186 branchcode => $br,
187 categorycode => $bor,
188 itemtype => $itemtype,
189 fine => $fine,
190 finedays => $finedays,
191 maxsuspensiondays => $maxsuspensiondays,
192 suspension_chargeperiod => $suspension_chargeperiod,
193 firstremind => $firstremind,
194 chargeperiod => $chargeperiod,
195 chargeperiod_charge_at => $chargeperiod_charge_at,
196 renewalsallowed => $renewalsallowed,
197 renewalperiod => $renewalperiod,
198 norenewalbefore => $norenewalbefore,
199 auto_renew => $auto_renew,
200 no_auto_renewal_after => $no_auto_renewal_after,
201 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
202 reservesallowed => $reservesallowed,
203 holds_per_record => $holds_per_record,
204 holds_per_day => $holds_per_day,
205 issuelength => $issuelength,
206 lengthunit => $lengthunit,
207 hardduedate => $hardduedate,
208 hardduedatecompare => $hardduedatecompare,
209 rentaldiscount => $rentaldiscount,
210 onshelfholds => $onshelfholds,
211 opacitemholds => $opacitemholds,
212 overduefinescap => $overduefinescap,
213 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
214 article_requests => $article_requests,
215 note => $note,
218 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
219 if ($issuingrule) {
220 $issuingrule->set($params)->store();
221 } else {
222 Koha::IssuingRule->new()->set($params)->store();
225 Koha::CirculationRules->set_rules(
227 categorycode => $bor,
228 itemtype => $itemtype,
229 branchcode => $br,
230 rules => {
231 maxissueqty => $maxissueqty,
232 maxonsiteissueqty => $maxonsiteissueqty,
238 elsif ($op eq "set-branch-defaults") {
239 my $categorycode = $input->param('categorycode');
240 my $patron_maxissueqty = $input->param('patron_maxissueqty');
241 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
242 my $holdallowed = $input->param('holdallowed');
243 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
244 my $returnbranch = $input->param('returnbranch');
245 my $max_holds = $input->param('max_holds');
246 $patron_maxissueqty =~ s/\s//g;
247 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
248 $patron_maxonsiteissueqty =~ s/\s//g;
249 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
250 $holdallowed =~ s/\s//g;
251 $holdallowed = undef if $holdallowed !~ /^\d+/;
252 $max_holds =~ s/\s//g;
253 $max_holds = '' if $max_holds !~ /^\d+/;
255 if ($branch eq "*") {
256 my $sth_search = $dbh->prepare("SELECT count(*) AS total
257 FROM default_circ_rules");
258 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
259 (holdallowed, hold_fulfillment_policy, returnbranch)
260 VALUES (?, ?, ?)");
261 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
262 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
264 $sth_search->execute();
265 my $res = $sth_search->fetchrow_hashref();
266 if ($res->{total}) {
267 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
268 } else {
269 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
272 Koha::CirculationRules->set_rules(
274 categorycode => undef,
275 itemtype => undef,
276 branchcode => undef,
277 rules => {
278 patron_maxissueqty => $patron_maxissueqty,
279 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
283 } else {
284 my $sth_search = $dbh->prepare("SELECT count(*) AS total
285 FROM default_branch_circ_rules
286 WHERE branchcode = ?");
287 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
288 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
289 VALUES (?, ?, ?, ?)");
290 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
291 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
292 WHERE branchcode = ?");
293 $sth_search->execute($branch);
294 my $res = $sth_search->fetchrow_hashref();
295 if ($res->{total}) {
296 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
297 } else {
298 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
301 Koha::CirculationRules->set_rules(
303 categorycode => undef,
304 itemtype => undef,
305 branchcode => $branch,
306 rules => {
307 patron_maxissueqty => $patron_maxissueqty,
308 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
313 Koha::CirculationRules->set_rule(
315 branchcode => $branch,
316 categorycode => undef,
317 itemtype => undef,
318 rule_name => 'max_holds',
319 rule_value => $max_holds,
323 elsif ($op eq "add-branch-cat") {
324 my $categorycode = $input->param('categorycode');
325 my $patron_maxissueqty = $input->param('patron_maxissueqty');
326 my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
327 my $max_holds = $input->param('max_holds');
328 $patron_maxissueqty =~ s/\s//g;
329 $patron_maxissueqty = '' if $patron_maxissueqty !~ /^\d+/;
330 $patron_maxonsiteissueqty =~ s/\s//g;
331 $patron_maxonsiteissueqty = '' if $patron_maxonsiteissueqty !~ /^\d+/;
332 $max_holds =~ s/\s//g;
333 $max_holds = undef if $max_holds !~ /^\d+/;
335 if ($branch eq "*") {
336 if ($categorycode eq "*") {
337 Koha::CirculationRules->set_rules(
339 categorycode => undef,
340 itemtype => undef,
341 branchcode => undef,
342 rules => {
343 max_holds => $max_holds,
344 patron_maxissueqty => $patron_maxissueqty,
345 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
349 } else {
350 Koha::CirculationRules->set_rules(
352 branchcode => undef,
353 categorycode => $categorycode,
354 itemtype => undef,
355 rules => {
356 max_holds => $max_holds,
357 patron_maxissueqty => $patron_maxissueqty,
358 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
363 } elsif ($categorycode eq "*") {
364 Koha::CirculationRules->set_rules(
366 categorycode => undef,
367 itemtype => undef,
368 branchcode => $branch,
369 rules => {
370 max_holds => $max_holds,
371 patron_maxissueqty => $patron_maxissueqty,
372 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
376 } else {
377 Koha::CirculationRules->set_rules(
379 categorycode => $categorycode,
380 itemtype => undef,
381 branchcode => $branch,
382 rules => {
383 max_holds => $max_holds,
384 patron_maxissueqty => $patron_maxissueqty,
385 patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
391 elsif ($op eq "add-branch-item") {
392 my $itemtype = $input->param('itemtype');
393 my $holdallowed = $input->param('holdallowed');
394 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
395 my $returnbranch = $input->param('returnbranch');
397 $holdallowed =~ s/\s//g;
398 $holdallowed = undef if $holdallowed !~ /^\d+/;
400 if ($branch eq "*") {
401 if ($itemtype eq "*") {
402 my $sth_search = $dbh->prepare("SELECT count(*) AS total
403 FROM default_circ_rules");
404 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
405 (holdallowed, hold_fulfillment_policy, returnbranch)
406 VALUES (?, ?, ?)");
407 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
408 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
410 $sth_search->execute();
411 my $res = $sth_search->fetchrow_hashref();
412 if ($res->{total}) {
413 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
414 } else {
415 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
417 } else {
418 my $sth_search = $dbh->prepare("SELECT count(*) AS total
419 FROM default_branch_item_rules
420 WHERE itemtype = ?");
421 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
422 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
423 VALUES (?, ?, ?, ?)");
424 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
425 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
426 WHERE itemtype = ?");
427 $sth_search->execute($itemtype);
428 my $res = $sth_search->fetchrow_hashref();
429 if ($res->{total}) {
430 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
431 } else {
432 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
435 } elsif ($itemtype eq "*") {
436 my $sth_search = $dbh->prepare("SELECT count(*) AS total
437 FROM default_branch_circ_rules
438 WHERE branchcode = ?");
439 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
440 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
441 VALUES (?, ?, ?, ?)");
442 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
443 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
444 WHERE branchcode = ?");
445 $sth_search->execute($branch);
446 my $res = $sth_search->fetchrow_hashref();
447 if ($res->{total}) {
448 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
449 } else {
450 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
452 } else {
453 my $sth_search = $dbh->prepare("SELECT count(*) AS total
454 FROM branch_item_rules
455 WHERE branchcode = ?
456 AND itemtype = ?");
457 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
458 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
459 VALUES (?, ?, ?, ?, ?)");
460 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
461 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
462 WHERE branchcode = ?
463 AND itemtype = ?");
465 $sth_search->execute($branch, $itemtype);
466 my $res = $sth_search->fetchrow_hashref();
467 if ($res->{total}) {
468 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
469 } else {
470 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
474 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
476 my $refund = $input->param('refund');
478 if ( $refund eq '*' ) {
479 if ( $branch ne '*' ) {
480 # only do something for $refund eq '*' if branch-specific
481 eval {
482 # Delete it so it picks the default
483 Koha::RefundLostItemFeeRules->find({
484 branchcode => $branch
485 })->delete;
488 } else {
489 my $refundRule =
490 Koha::RefundLostItemFeeRules->find({
491 branchcode => $branch
492 }) // Koha::RefundLostItemFeeRule->new;
493 $refundRule->set({
494 branchcode => $branch,
495 refund => $refund
496 })->store;
500 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
501 $template->param(
502 refundLostItemFeeRule => $refundLostItemFeeRule,
503 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
506 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
508 my @row_loop;
509 my $itemtypes = Koha::ItemTypes->search_with_localization;
511 my $sth2 = $dbh->prepare("
512 SELECT issuingrules.*,
513 itemtypes.description AS humanitemtype,
514 categories.description AS humancategorycode,
515 COALESCE( localization.translation, itemtypes.description ) AS translated_description
516 FROM issuingrules
517 LEFT JOIN itemtypes
518 ON (itemtypes.itemtype = issuingrules.itemtype)
519 LEFT JOIN categories
520 ON (categories.categorycode = issuingrules.categorycode)
521 LEFT JOIN localization ON issuingrules.itemtype = localization.code
522 AND localization.entity = 'itemtypes'
523 AND localization.lang = ?
524 WHERE issuingrules.branchcode = ?
526 $sth2->execute($language, $branch);
528 while (my $row = $sth2->fetchrow_hashref) {
529 $row->{'current_branch'} ||= $row->{'branchcode'};
530 $row->{humanitemtype} ||= $row->{itemtype};
531 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
532 $row->{'humancategorycode'} ||= $row->{'categorycode'};
533 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
534 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
535 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
536 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
537 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
538 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
539 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
540 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
541 } else {
542 $row->{'hardduedate'} = 0;
544 if ($row->{no_auto_renewal_after_hard_limit}) {
545 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
546 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
549 push @row_loop, $row;
552 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
554 my $sth_branch_item;
555 if ($branch eq "*") {
556 $sth_branch_item = $dbh->prepare("
557 SELECT default_branch_item_rules.*,
558 COALESCE( localization.translation, itemtypes.description ) AS translated_description
559 FROM default_branch_item_rules
560 JOIN itemtypes USING (itemtype)
561 LEFT JOIN localization ON itemtypes.itemtype = localization.code
562 AND localization.entity = 'itemtypes'
563 AND localization.lang = ?
565 $sth_branch_item->execute($language);
566 } else {
567 $sth_branch_item = $dbh->prepare("
568 SELECT branch_item_rules.*,
569 COALESCE( localization.translation, itemtypes.description ) AS translated_description
570 FROM branch_item_rules
571 JOIN itemtypes USING (itemtype)
572 LEFT JOIN localization ON itemtypes.itemtype = localization.code
573 AND localization.entity = 'itemtypes'
574 AND localization.lang = ?
575 WHERE branch_item_rules.branchcode = ?
577 $sth_branch_item->execute($language, $branch);
580 my @branch_item_rules = ();
581 while (my $row = $sth_branch_item->fetchrow_hashref) {
582 push @branch_item_rules, $row;
584 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
586 # note undef holdallowed so that template can deal with them
587 foreach my $entry (@sorted_branch_item_rules) {
588 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
589 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
592 $template->param(show_branch_cat_rule_form => 1);
593 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
595 my $sth_defaults;
596 if ($branch eq "*") {
597 $sth_defaults = $dbh->prepare("
598 SELECT *
599 FROM default_circ_rules
601 $sth_defaults->execute();
602 } else {
603 $sth_defaults = $dbh->prepare("
604 SELECT *
605 FROM default_branch_circ_rules
606 WHERE branchcode = ?
608 $sth_defaults->execute($branch);
611 my $defaults = $sth_defaults->fetchrow_hashref;
613 if ($defaults) {
614 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
615 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
616 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
617 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
618 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
619 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
620 $template->param( default_returnbranch => $defaults->{returnbranch} );
623 $template->param(default_rules => ($defaults ? 1 : 0));
625 $template->param(
626 patron_categories => $patron_categories,
627 itemtypeloop => $itemtypes,
628 rules => \@sorted_row_loop,
629 humanbranch => ($branch ne '*' ? $branch : ''),
630 current_branch => $branch,
631 definedbranch => scalar(@sorted_row_loop)>0
633 output_html_with_http_headers $input, $cookie, $template->output;
635 exit 0;
637 # sort by patron category, then item type, putting
638 # default entries at the bottom
639 sub by_category_and_itemtype {
640 unless (by_category($a, $b)) {
641 return by_itemtype($a, $b);
645 sub by_category {
646 my ($a, $b) = @_;
647 if ($a->{'default_humancategorycode'}) {
648 return ($b->{'default_humancategorycode'} ? 0 : 1);
649 } elsif ($b->{'default_humancategorycode'}) {
650 return -1;
651 } else {
652 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
656 sub by_itemtype {
657 my ($a, $b) = @_;
658 if ($a->{default_translated_description}) {
659 return ($b->{'default_translated_description'} ? 0 : 1);
660 } elsif ($b->{'default_translated_description'}) {
661 return -1;
662 } else {
663 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};