Bug 16621: Translatability: Issues in opac-user.tt (sentence splitting)
[koha.git] / admin / smart-rules.pl
blob9d2d0fdf10486ec9d467860507fcf5839415ae7f
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 C4::Branch; # GetBranches
29 use Koha::DateUtils;
30 use Koha::Database;
31 use Koha::IssuingRule;
32 use Koha::IssuingRules;
33 use Koha::Logger;
34 use Koha::RefundLostItemFeeRule;
35 use Koha::RefundLostItemFeeRules;
36 use Koha::Libraries;
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::Branch::mybranch();
59 else {
60 $branch = C4::Branch::onlymine() ? ( C4::Branch::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 $reservesallowed = $input->param('reservesallowed');
140 my $onshelfholds = $input->param('onshelfholds') || 0;
141 $maxissueqty =~ s/\s//g;
142 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
143 $maxonsiteissueqty =~ s/\s//g;
144 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
145 my $issuelength = $input->param('issuelength');
146 $issuelength = $issuelength eq q{} ? undef : $issuelength;
147 my $lengthunit = $input->param('lengthunit');
148 my $hardduedate = $input->param('hardduedate') || undef;
149 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
150 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
151 my $hardduedatecompare = $input->param('hardduedatecompare');
152 my $rentaldiscount = $input->param('rentaldiscount');
153 my $opacitemholds = $input->param('opacitemholds') || 0;
154 my $overduefinescap = $input->param('overduefinescap') || undef;
155 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
156 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
158 my $params = {
159 branchcode => $br,
160 categorycode => $bor,
161 itemtype => $itemtype,
162 fine => $fine,
163 finedays => $finedays,
164 maxsuspensiondays => $maxsuspensiondays,
165 firstremind => $firstremind,
166 chargeperiod => $chargeperiod,
167 chargeperiod_charge_at => $chargeperiod_charge_at,
168 maxissueqty => $maxissueqty,
169 maxonsiteissueqty => $maxonsiteissueqty,
170 renewalsallowed => $renewalsallowed,
171 renewalperiod => $renewalperiod,
172 norenewalbefore => $norenewalbefore,
173 auto_renew => $auto_renew,
174 reservesallowed => $reservesallowed,
175 issuelength => $issuelength,
176 lengthunit => $lengthunit,
177 hardduedate => $hardduedate,
178 hardduedatecompare => $hardduedatecompare,
179 rentaldiscount => $rentaldiscount,
180 onshelfholds => $onshelfholds,
181 opacitemholds => $opacitemholds,
182 overduefinescap => $overduefinescap,
183 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
186 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
187 if ($issuingrule) {
188 $issuingrule->set($params)->store();
189 } else {
190 Koha::IssuingRule->new()->set($params)->store();
194 elsif ($op eq "set-branch-defaults") {
195 my $categorycode = $input->param('categorycode');
196 my $maxissueqty = $input->param('maxissueqty');
197 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
198 my $holdallowed = $input->param('holdallowed');
199 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
200 my $returnbranch = $input->param('returnbranch');
201 $maxissueqty =~ s/\s//g;
202 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
203 $maxonsiteissueqty =~ s/\s//g;
204 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
205 $holdallowed =~ s/\s//g;
206 $holdallowed = undef if $holdallowed !~ /^\d+/;
208 if ($branch eq "*") {
209 my $sth_search = $dbh->prepare("SELECT count(*) AS total
210 FROM default_circ_rules");
211 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
212 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
213 VALUES (?, ?, ?, ?, ?)");
214 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
215 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
217 $sth_search->execute();
218 my $res = $sth_search->fetchrow_hashref();
219 if ($res->{total}) {
220 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
221 } else {
222 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
224 } else {
225 my $sth_search = $dbh->prepare("SELECT count(*) AS total
226 FROM default_branch_circ_rules
227 WHERE branchcode = ?");
228 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
229 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
230 VALUES (?, ?, ?, ?, ?, ?)");
231 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
232 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
233 WHERE branchcode = ?");
234 $sth_search->execute($branch);
235 my $res = $sth_search->fetchrow_hashref();
236 if ($res->{total}) {
237 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
238 } else {
239 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
243 elsif ($op eq "add-branch-cat") {
244 my $categorycode = $input->param('categorycode');
245 my $maxissueqty = $input->param('maxissueqty');
246 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
247 $maxissueqty =~ s/\s//g;
248 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
249 $maxonsiteissueqty =~ s/\s//g;
250 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
252 if ($branch eq "*") {
253 if ($categorycode eq "*") {
254 my $sth_search = $dbh->prepare("SELECT count(*) AS total
255 FROM default_circ_rules");
256 my $sth_insert = $dbh->prepare(q|
257 INSERT INTO default_circ_rules
258 (maxissueqty, maxonsiteissueqty)
259 VALUES (?, ?)
261 my $sth_update = $dbh->prepare(q|
262 UPDATE default_circ_rules
263 SET maxissueqty = ?,
264 maxonsiteissueqty = ?
267 $sth_search->execute();
268 my $res = $sth_search->fetchrow_hashref();
269 if ($res->{total}) {
270 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
271 } else {
272 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
274 } else {
275 my $sth_search = $dbh->prepare("SELECT count(*) AS total
276 FROM default_borrower_circ_rules
277 WHERE categorycode = ?");
278 my $sth_insert = $dbh->prepare(q|
279 INSERT INTO default_borrower_circ_rules
280 (categorycode, maxissueqty, maxonsiteissueqty)
281 VALUES (?, ?, ?)
283 my $sth_update = $dbh->prepare(q|
284 UPDATE default_borrower_circ_rules
285 SET maxissueqty = ?,
286 maxonsiteissueqty = ?
287 WHERE categorycode = ?
289 $sth_search->execute($branch);
290 my $res = $sth_search->fetchrow_hashref();
291 if ($res->{total}) {
292 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
293 } else {
294 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
297 } elsif ($categorycode eq "*") {
298 my $sth_search = $dbh->prepare("SELECT count(*) AS total
299 FROM default_branch_circ_rules
300 WHERE branchcode = ?");
301 my $sth_insert = $dbh->prepare(q|
302 INSERT INTO default_branch_circ_rules
303 (branchcode, maxissueqty, maxonsiteissueqty)
304 VALUES (?, ?, ?)
306 my $sth_update = $dbh->prepare(q|
307 UPDATE default_branch_circ_rules
308 SET maxissueqty = ?,
309 maxonsiteissueqty = ?
310 WHERE branchcode = ?
312 $sth_search->execute($branch);
313 my $res = $sth_search->fetchrow_hashref();
314 if ($res->{total}) {
315 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
316 } else {
317 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
319 } else {
320 my $sth_search = $dbh->prepare("SELECT count(*) AS total
321 FROM branch_borrower_circ_rules
322 WHERE branchcode = ?
323 AND categorycode = ?");
324 my $sth_insert = $dbh->prepare(q|
325 INSERT INTO branch_borrower_circ_rules
326 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
327 VALUES (?, ?, ?, ?)
329 my $sth_update = $dbh->prepare(q|
330 UPDATE branch_borrower_circ_rules
331 SET maxissueqty = ?,
332 maxonsiteissueqty = ?
333 WHERE branchcode = ?
334 AND categorycode = ?
337 $sth_search->execute($branch, $categorycode);
338 my $res = $sth_search->fetchrow_hashref();
339 if ($res->{total}) {
340 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
341 } else {
342 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
346 elsif ($op eq "add-branch-item") {
347 my $itemtype = $input->param('itemtype');
348 my $holdallowed = $input->param('holdallowed');
349 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
350 my $returnbranch = $input->param('returnbranch');
352 $holdallowed =~ s/\s//g;
353 $holdallowed = undef if $holdallowed !~ /^\d+/;
355 if ($branch eq "*") {
356 if ($itemtype eq "*") {
357 my $sth_search = $dbh->prepare("SELECT count(*) AS total
358 FROM default_circ_rules");
359 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
360 (holdallowed, hold_fulfillment_policy, returnbranch)
361 VALUES (?, ?, ?)");
362 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
363 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
365 $sth_search->execute();
366 my $res = $sth_search->fetchrow_hashref();
367 if ($res->{total}) {
368 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
369 } else {
370 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
372 } else {
373 my $sth_search = $dbh->prepare("SELECT count(*) AS total
374 FROM default_branch_item_rules
375 WHERE itemtype = ?");
376 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
377 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
378 VALUES (?, ?, ?, ?)");
379 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
380 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
381 WHERE itemtype = ?");
382 $sth_search->execute($itemtype);
383 my $res = $sth_search->fetchrow_hashref();
384 if ($res->{total}) {
385 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
386 } else {
387 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
390 } elsif ($itemtype eq "*") {
391 my $sth_search = $dbh->prepare("SELECT count(*) AS total
392 FROM default_branch_circ_rules
393 WHERE branchcode = ?");
394 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
395 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
396 VALUES (?, ?, ?, ?)");
397 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
398 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
399 WHERE branchcode = ?");
400 $sth_search->execute($branch);
401 my $res = $sth_search->fetchrow_hashref();
402 if ($res->{total}) {
403 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
404 } else {
405 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
407 } else {
408 my $sth_search = $dbh->prepare("SELECT count(*) AS total
409 FROM branch_item_rules
410 WHERE branchcode = ?
411 AND itemtype = ?");
412 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
413 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
414 VALUES (?, ?, ?, ?, ?)");
415 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
416 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
417 WHERE branchcode = ?
418 AND itemtype = ?");
420 $sth_search->execute($branch, $itemtype);
421 my $res = $sth_search->fetchrow_hashref();
422 if ($res->{total}) {
423 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
424 } else {
425 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
429 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
431 my $refund = $input->param('refund');
433 if ( $refund eq '*' ) {
434 if ( $branch ne '*' ) {
435 # only do something for $refund eq '*' if branch-specific
436 eval {
437 # Delete it so it picks the default
438 Koha::RefundLostItemFeeRules->find({
439 branchcode => $branch
440 })->delete;
443 } else {
444 my $refundRule =
445 Koha::RefundLostItemFeeRules->find({
446 branchcode => $branch
447 }) // Koha::RefundLostItemFeeRule->new;
448 $refundRule->set({
449 branchcode => $branch,
450 refund => $refund
451 })->store;
455 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
456 $template->param(
457 refundLostItemFeeRule => $refundLostItemFeeRule,
458 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
461 my $branches = GetBranches();
462 my @branchloop;
463 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
464 push @branchloop, {
465 value => $thisbranch,
466 selected => $thisbranch eq $branch,
467 branchname => $branches->{$thisbranch}->{'branchname'},
471 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
472 $sth->execute;
473 my @category_loop;
474 while (my $data=$sth->fetchrow_hashref){
475 push @category_loop,$data;
478 $sth->finish;
479 my @row_loop;
480 my @itemtypes = @{ GetItemTypes( style => 'array' ) };
481 @itemtypes = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @itemtypes;
483 my $sth2 = $dbh->prepare("
484 SELECT issuingrules.*,
485 itemtypes.description AS humanitemtype,
486 categories.description AS humancategorycode,
487 COALESCE( localization.translation, itemtypes.description ) AS translated_description
488 FROM issuingrules
489 LEFT JOIN itemtypes
490 ON (itemtypes.itemtype = issuingrules.itemtype)
491 LEFT JOIN categories
492 ON (categories.categorycode = issuingrules.categorycode)
493 LEFT JOIN localization ON issuingrules.itemtype = localization.code
494 AND localization.entity = 'itemtypes'
495 AND localization.lang = ?
496 WHERE issuingrules.branchcode = ?
498 $sth2->execute($language, $branch);
500 while (my $row = $sth2->fetchrow_hashref) {
501 $row->{'current_branch'} ||= $row->{'branchcode'};
502 $row->{humanitemtype} ||= $row->{itemtype};
503 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
504 $row->{'humancategorycode'} ||= $row->{'categorycode'};
505 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
506 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
507 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
508 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
509 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
510 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
511 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
512 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
513 } else {
514 $row->{'hardduedate'} = 0;
516 push @row_loop, $row;
518 $sth->finish;
520 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
522 my $sth_branch_cat;
523 if ($branch eq "*") {
524 $sth_branch_cat = $dbh->prepare("
525 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
526 FROM default_borrower_circ_rules
527 JOIN categories USING (categorycode)
530 $sth_branch_cat->execute();
531 } else {
532 $sth_branch_cat = $dbh->prepare("
533 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
534 FROM branch_borrower_circ_rules
535 JOIN categories USING (categorycode)
536 WHERE branch_borrower_circ_rules.branchcode = ?
538 $sth_branch_cat->execute($branch);
541 my @branch_cat_rules = ();
542 while (my $row = $sth_branch_cat->fetchrow_hashref) {
543 push @branch_cat_rules, $row;
545 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
547 # note undef maxissueqty so that template can deal with them
548 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
549 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
550 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
553 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
555 my $sth_branch_item;
556 if ($branch eq "*") {
557 $sth_branch_item = $dbh->prepare("
558 SELECT default_branch_item_rules.*,
559 COALESCE( localization.translation, itemtypes.description ) AS translated_description
560 FROM default_branch_item_rules
561 JOIN itemtypes USING (itemtype)
562 LEFT JOIN localization ON itemtypes.itemtype = localization.code
563 AND localization.entity = 'itemtypes'
564 AND localization.lang = ?
566 $sth_branch_item->execute($language);
567 } else {
568 $sth_branch_item = $dbh->prepare("
569 SELECT branch_item_rules.*,
570 COALESCE( localization.translation, itemtypes.description ) AS translated_description
571 FROM branch_item_rules
572 JOIN itemtypes USING (itemtype)
573 LEFT JOIN localization ON itemtypes.itemtype = localization.code
574 AND localization.entity = 'itemtypes'
575 AND localization.lang = ?
576 WHERE branch_item_rules.branchcode = ?
578 $sth_branch_item->execute($language, $branch);
581 my @branch_item_rules = ();
582 while (my $row = $sth_branch_item->fetchrow_hashref) {
583 push @branch_item_rules, $row;
585 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
587 # note undef holdallowed so that template can deal with them
588 foreach my $entry (@sorted_branch_item_rules) {
589 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
590 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
593 $template->param(show_branch_cat_rule_form => 1);
594 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
595 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
597 my $sth_defaults;
598 if ($branch eq "*") {
599 $sth_defaults = $dbh->prepare("
600 SELECT *
601 FROM default_circ_rules
603 $sth_defaults->execute();
604 } else {
605 $sth_defaults = $dbh->prepare("
606 SELECT *
607 FROM default_branch_circ_rules
608 WHERE branchcode = ?
610 $sth_defaults->execute($branch);
613 my $defaults = $sth_defaults->fetchrow_hashref;
615 if ($defaults) {
616 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
617 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
618 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
619 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
620 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
621 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
622 $template->param( default_returnbranch => $defaults->{returnbranch} );
625 $template->param(default_rules => ($defaults ? 1 : 0));
627 $template->param(categoryloop => \@category_loop,
628 itemtypeloop => \@itemtypes,
629 rules => \@sorted_row_loop,
630 branchloop => \@branchloop,
631 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
632 current_branch => $branch,
633 definedbranch => scalar(@sorted_row_loop)>0
635 output_html_with_http_headers $input, $cookie, $template->output;
637 exit 0;
639 # sort by patron category, then item type, putting
640 # default entries at the bottom
641 sub by_category_and_itemtype {
642 unless (by_category($a, $b)) {
643 return by_itemtype($a, $b);
647 sub by_category {
648 my ($a, $b) = @_;
649 if ($a->{'default_humancategorycode'}) {
650 return ($b->{'default_humancategorycode'} ? 0 : 1);
651 } elsif ($b->{'default_humancategorycode'}) {
652 return -1;
653 } else {
654 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
658 sub by_itemtype {
659 my ($a, $b) = @_;
660 if ($a->{default_translated_description}) {
661 return ($b->{'default_translated_description'} ? 0 : 1);
662 } elsif ($b->{'default_translated_description'}) {
663 return -1;
664 } else {
665 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};