Bug 19439: (follow-up) Remove four spaces from lines 133-149
[koha.git] / admin / smart-rules.pl
blob6a55b7bde4a1498c9bd936b75dbb873abf2e7516
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 strict;
21 use warnings;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Debug;
28 use Koha::DateUtils;
29 use Koha::Database;
30 use Koha::IssuingRule;
31 use Koha::IssuingRules;
32 use Koha::Logger;
33 use Koha::RefundLostItemFeeRule;
34 use Koha::RefundLostItemFeeRules;
35 use Koha::Libraries;
36 use Koha::Patron::Categories;
38 my $input = CGI->new;
39 my $dbh = C4::Context->dbh;
41 # my $flagsrequired;
42 # $flagsrequired->{circulation}=1;
43 my ($template, $loggedinuser, $cookie)
44 = get_template_and_user({template_name => "admin/smart-rules.tt",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => {parameters => 'manage_circ_rules'},
49 debug => 1,
50 });
52 my $type=$input->param('type');
54 my $branch = $input->param('branch');
55 unless ( $branch ) {
56 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
57 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
59 else {
60 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
63 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
65 my $op = $input->param('op') || q{};
66 my $language = C4::Languages::getlanguage();
68 if ($op eq 'delete') {
69 my $itemtype = $input->param('itemtype');
70 my $categorycode = $input->param('categorycode');
71 $debug and warn "deleting $1 $2 $branch";
73 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
74 $sth_Idelete->execute($branch, $categorycode, $itemtype);
76 elsif ($op eq 'delete-branch-cat') {
77 my $categorycode = $input->param('categorycode');
78 if ($branch eq "*") {
79 if ($categorycode eq "*") {
80 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
81 $sth_delete->execute();
82 } else {
83 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
84 WHERE categorycode = ?");
85 $sth_delete->execute($categorycode);
87 } elsif ($categorycode eq "*") {
88 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
89 WHERE branchcode = ?");
90 $sth_delete->execute($branch);
91 } else {
92 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
93 WHERE branchcode = ?
94 AND categorycode = ?");
95 $sth_delete->execute($branch, $categorycode);
98 elsif ($op eq 'delete-branch-item') {
99 my $itemtype = $input->param('itemtype');
100 if ($branch eq "*") {
101 if ($itemtype eq "*") {
102 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
103 $sth_delete->execute();
104 } else {
105 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
106 WHERE itemtype = ?");
107 $sth_delete->execute($itemtype);
109 } elsif ($itemtype eq "*") {
110 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
111 WHERE branchcode = ?");
112 $sth_delete->execute($branch);
113 } else {
114 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
115 WHERE branchcode = ?
116 AND itemtype = ?");
117 $sth_delete->execute($branch, $itemtype);
120 # save the values entered
121 elsif ($op eq 'add') {
122 my $br = $branch; # branch
123 my $bor = $input->param('categorycode'); # borrower category
124 my $itemtype = $input->param('itemtype'); # item type
125 my $fine = $input->param('fine');
126 my $finedays = $input->param('finedays');
127 my $maxsuspensiondays = $input->param('maxsuspensiondays');
128 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
129 my $firstremind = $input->param('firstremind');
130 my $chargeperiod = $input->param('chargeperiod');
131 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
132 my $maxissueqty = $input->param('maxissueqty');
133 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
134 my $renewalsallowed = $input->param('renewalsallowed');
135 my $renewalperiod = $input->param('renewalperiod');
136 my $norenewalbefore = $input->param('norenewalbefore');
137 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
138 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
139 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
140 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
141 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
142 $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 );
143 $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 );
144 my $reservesallowed = $input->param('reservesallowed');
145 my $holds_per_record = $input->param('holds_per_record');
146 my $onshelfholds = $input->param('onshelfholds') || 0;
147 $maxissueqty =~ s/\s//g;
148 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
149 $maxonsiteissueqty =~ s/\s//g;
150 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
151 my $issuelength = $input->param('issuelength');
152 $issuelength = $issuelength eq q{} ? undef : $issuelength;
153 my $lengthunit = $input->param('lengthunit');
154 my $hardduedate = $input->param('hardduedate') || undef;
155 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
156 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
157 my $hardduedatecompare = $input->param('hardduedatecompare');
158 my $rentaldiscount = $input->param('rentaldiscount');
159 my $opacitemholds = $input->param('opacitemholds') || 0;
160 my $article_requests = $input->param('article_requests') || 'no';
161 my $overduefinescap = $input->param('overduefinescap') || undef;
162 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
163 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
165 my $params = {
166 branchcode => $br,
167 categorycode => $bor,
168 itemtype => $itemtype,
169 fine => $fine,
170 finedays => $finedays,
171 maxsuspensiondays => $maxsuspensiondays,
172 firstremind => $firstremind,
173 chargeperiod => $chargeperiod,
174 chargeperiod_charge_at => $chargeperiod_charge_at,
175 maxissueqty => $maxissueqty,
176 maxonsiteissueqty => $maxonsiteissueqty,
177 renewalsallowed => $renewalsallowed,
178 renewalperiod => $renewalperiod,
179 norenewalbefore => $norenewalbefore,
180 auto_renew => $auto_renew,
181 no_auto_renewal_after => $no_auto_renewal_after,
182 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
183 reservesallowed => $reservesallowed,
184 holds_per_record => $holds_per_record,
185 issuelength => $issuelength,
186 lengthunit => $lengthunit,
187 hardduedate => $hardduedate,
188 hardduedatecompare => $hardduedatecompare,
189 rentaldiscount => $rentaldiscount,
190 onshelfholds => $onshelfholds,
191 opacitemholds => $opacitemholds,
192 overduefinescap => $overduefinescap,
193 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
194 article_requests => $article_requests,
197 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
198 if ($issuingrule) {
199 $issuingrule->set($params)->store();
200 } else {
201 Koha::IssuingRule->new()->set($params)->store();
205 elsif ($op eq "set-branch-defaults") {
206 my $categorycode = $input->param('categorycode');
207 my $maxissueqty = $input->param('maxissueqty');
208 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
209 my $holdallowed = $input->param('holdallowed');
210 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
211 my $returnbranch = $input->param('returnbranch');
212 $maxissueqty =~ s/\s//g;
213 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
214 $maxonsiteissueqty =~ s/\s//g;
215 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
216 $holdallowed =~ s/\s//g;
217 $holdallowed = undef if $holdallowed !~ /^\d+/;
219 if ($branch eq "*") {
220 my $sth_search = $dbh->prepare("SELECT count(*) AS total
221 FROM default_circ_rules");
222 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
223 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
224 VALUES (?, ?, ?, ?, ?)");
225 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
226 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
228 $sth_search->execute();
229 my $res = $sth_search->fetchrow_hashref();
230 if ($res->{total}) {
231 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
232 } else {
233 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
235 } else {
236 my $sth_search = $dbh->prepare("SELECT count(*) AS total
237 FROM default_branch_circ_rules
238 WHERE branchcode = ?");
239 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
240 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
241 VALUES (?, ?, ?, ?, ?, ?)");
242 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
243 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
244 WHERE branchcode = ?");
245 $sth_search->execute($branch);
246 my $res = $sth_search->fetchrow_hashref();
247 if ($res->{total}) {
248 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
249 } else {
250 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
254 elsif ($op eq "add-branch-cat") {
255 my $categorycode = $input->param('categorycode');
256 my $maxissueqty = $input->param('maxissueqty');
257 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
258 $maxissueqty =~ s/\s//g;
259 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
260 $maxonsiteissueqty =~ s/\s//g;
261 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
263 if ($branch eq "*") {
264 if ($categorycode eq "*") {
265 my $sth_search = $dbh->prepare("SELECT count(*) AS total
266 FROM default_circ_rules");
267 my $sth_insert = $dbh->prepare(q|
268 INSERT INTO default_circ_rules
269 (maxissueqty, maxonsiteissueqty)
270 VALUES (?, ?)
272 my $sth_update = $dbh->prepare(q|
273 UPDATE default_circ_rules
274 SET maxissueqty = ?,
275 maxonsiteissueqty = ?
278 $sth_search->execute();
279 my $res = $sth_search->fetchrow_hashref();
280 if ($res->{total}) {
281 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
282 } else {
283 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
285 } else {
286 my $sth_search = $dbh->prepare("SELECT count(*) AS total
287 FROM default_borrower_circ_rules
288 WHERE categorycode = ?");
289 my $sth_insert = $dbh->prepare(q|
290 INSERT INTO default_borrower_circ_rules
291 (categorycode, maxissueqty, maxonsiteissueqty)
292 VALUES (?, ?, ?)
294 my $sth_update = $dbh->prepare(q|
295 UPDATE default_borrower_circ_rules
296 SET maxissueqty = ?,
297 maxonsiteissueqty = ?
298 WHERE categorycode = ?
300 $sth_search->execute($branch);
301 my $res = $sth_search->fetchrow_hashref();
302 if ($res->{total}) {
303 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
304 } else {
305 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
308 } elsif ($categorycode eq "*") {
309 my $sth_search = $dbh->prepare("SELECT count(*) AS total
310 FROM default_branch_circ_rules
311 WHERE branchcode = ?");
312 my $sth_insert = $dbh->prepare(q|
313 INSERT INTO default_branch_circ_rules
314 (branchcode, maxissueqty, maxonsiteissueqty)
315 VALUES (?, ?, ?)
317 my $sth_update = $dbh->prepare(q|
318 UPDATE default_branch_circ_rules
319 SET maxissueqty = ?,
320 maxonsiteissueqty = ?
321 WHERE branchcode = ?
323 $sth_search->execute($branch);
324 my $res = $sth_search->fetchrow_hashref();
325 if ($res->{total}) {
326 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
327 } else {
328 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
330 } else {
331 my $sth_search = $dbh->prepare("SELECT count(*) AS total
332 FROM branch_borrower_circ_rules
333 WHERE branchcode = ?
334 AND categorycode = ?");
335 my $sth_insert = $dbh->prepare(q|
336 INSERT INTO branch_borrower_circ_rules
337 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
338 VALUES (?, ?, ?, ?)
340 my $sth_update = $dbh->prepare(q|
341 UPDATE branch_borrower_circ_rules
342 SET maxissueqty = ?,
343 maxonsiteissueqty = ?
344 WHERE branchcode = ?
345 AND categorycode = ?
348 $sth_search->execute($branch, $categorycode);
349 my $res = $sth_search->fetchrow_hashref();
350 if ($res->{total}) {
351 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
352 } else {
353 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
357 elsif ($op eq "add-branch-item") {
358 my $itemtype = $input->param('itemtype');
359 my $holdallowed = $input->param('holdallowed');
360 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
361 my $returnbranch = $input->param('returnbranch');
363 $holdallowed =~ s/\s//g;
364 $holdallowed = undef if $holdallowed !~ /^\d+/;
366 if ($branch eq "*") {
367 if ($itemtype eq "*") {
368 my $sth_search = $dbh->prepare("SELECT count(*) AS total
369 FROM default_circ_rules");
370 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
371 (holdallowed, hold_fulfillment_policy, returnbranch)
372 VALUES (?, ?, ?)");
373 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
374 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
376 $sth_search->execute();
377 my $res = $sth_search->fetchrow_hashref();
378 if ($res->{total}) {
379 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
380 } else {
381 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
383 } else {
384 my $sth_search = $dbh->prepare("SELECT count(*) AS total
385 FROM default_branch_item_rules
386 WHERE itemtype = ?");
387 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
388 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
389 VALUES (?, ?, ?, ?)");
390 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
391 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
392 WHERE itemtype = ?");
393 $sth_search->execute($itemtype);
394 my $res = $sth_search->fetchrow_hashref();
395 if ($res->{total}) {
396 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
397 } else {
398 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
401 } elsif ($itemtype eq "*") {
402 my $sth_search = $dbh->prepare("SELECT count(*) AS total
403 FROM default_branch_circ_rules
404 WHERE branchcode = ?");
405 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
406 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
407 VALUES (?, ?, ?, ?)");
408 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
409 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
410 WHERE branchcode = ?");
411 $sth_search->execute($branch);
412 my $res = $sth_search->fetchrow_hashref();
413 if ($res->{total}) {
414 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
415 } else {
416 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
418 } else {
419 my $sth_search = $dbh->prepare("SELECT count(*) AS total
420 FROM branch_item_rules
421 WHERE branchcode = ?
422 AND itemtype = ?");
423 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
424 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
425 VALUES (?, ?, ?, ?, ?)");
426 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
427 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
428 WHERE branchcode = ?
429 AND itemtype = ?");
431 $sth_search->execute($branch, $itemtype);
432 my $res = $sth_search->fetchrow_hashref();
433 if ($res->{total}) {
434 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
435 } else {
436 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
440 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
442 my $refund = $input->param('refund');
444 if ( $refund eq '*' ) {
445 if ( $branch ne '*' ) {
446 # only do something for $refund eq '*' if branch-specific
447 eval {
448 # Delete it so it picks the default
449 Koha::RefundLostItemFeeRules->find({
450 branchcode => $branch
451 })->delete;
454 } else {
455 my $refundRule =
456 Koha::RefundLostItemFeeRules->find({
457 branchcode => $branch
458 }) // Koha::RefundLostItemFeeRule->new;
459 $refundRule->set({
460 branchcode => $branch,
461 refund => $refund
462 })->store;
466 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
467 $template->param(
468 refundLostItemFeeRule => $refundLostItemFeeRule,
469 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
472 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
474 my @row_loop;
475 my $itemtypes = Koha::ItemTypes->search_with_localization;
477 my $sth2 = $dbh->prepare("
478 SELECT issuingrules.*,
479 itemtypes.description AS humanitemtype,
480 categories.description AS humancategorycode,
481 COALESCE( localization.translation, itemtypes.description ) AS translated_description
482 FROM issuingrules
483 LEFT JOIN itemtypes
484 ON (itemtypes.itemtype = issuingrules.itemtype)
485 LEFT JOIN categories
486 ON (categories.categorycode = issuingrules.categorycode)
487 LEFT JOIN localization ON issuingrules.itemtype = localization.code
488 AND localization.entity = 'itemtypes'
489 AND localization.lang = ?
490 WHERE issuingrules.branchcode = ?
492 $sth2->execute($language, $branch);
494 while (my $row = $sth2->fetchrow_hashref) {
495 $row->{'current_branch'} ||= $row->{'branchcode'};
496 $row->{humanitemtype} ||= $row->{itemtype};
497 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
498 $row->{'humancategorycode'} ||= $row->{'categorycode'};
499 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
500 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
501 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
502 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
503 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
504 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
505 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
506 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
507 } else {
508 $row->{'hardduedate'} = 0;
510 if ($row->{no_auto_renewal_after_hard_limit}) {
511 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
512 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
515 push @row_loop, $row;
518 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
520 my $sth_branch_cat;
521 if ($branch eq "*") {
522 $sth_branch_cat = $dbh->prepare("
523 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
524 FROM default_borrower_circ_rules
525 JOIN categories USING (categorycode)
528 $sth_branch_cat->execute();
529 } else {
530 $sth_branch_cat = $dbh->prepare("
531 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
532 FROM branch_borrower_circ_rules
533 JOIN categories USING (categorycode)
534 WHERE branch_borrower_circ_rules.branchcode = ?
536 $sth_branch_cat->execute($branch);
539 my @branch_cat_rules = ();
540 while (my $row = $sth_branch_cat->fetchrow_hashref) {
541 push @branch_cat_rules, $row;
543 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
545 # note undef maxissueqty so that template can deal with them
546 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
547 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
548 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
551 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
553 my $sth_branch_item;
554 if ($branch eq "*") {
555 $sth_branch_item = $dbh->prepare("
556 SELECT default_branch_item_rules.*,
557 COALESCE( localization.translation, itemtypes.description ) AS translated_description
558 FROM default_branch_item_rules
559 JOIN itemtypes USING (itemtype)
560 LEFT JOIN localization ON itemtypes.itemtype = localization.code
561 AND localization.entity = 'itemtypes'
562 AND localization.lang = ?
564 $sth_branch_item->execute($language);
565 } else {
566 $sth_branch_item = $dbh->prepare("
567 SELECT branch_item_rules.*,
568 COALESCE( localization.translation, itemtypes.description ) AS translated_description
569 FROM branch_item_rules
570 JOIN itemtypes USING (itemtype)
571 LEFT JOIN localization ON itemtypes.itemtype = localization.code
572 AND localization.entity = 'itemtypes'
573 AND localization.lang = ?
574 WHERE branch_item_rules.branchcode = ?
576 $sth_branch_item->execute($language, $branch);
579 my @branch_item_rules = ();
580 while (my $row = $sth_branch_item->fetchrow_hashref) {
581 push @branch_item_rules, $row;
583 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
585 # note undef holdallowed so that template can deal with them
586 foreach my $entry (@sorted_branch_item_rules) {
587 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
588 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
591 $template->param(show_branch_cat_rule_form => 1);
592 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
593 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_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'};