Bug 9302: Use patron-title.inc
[koha.git] / admin / smart-rules.pl
blobfe0c9c29f112093757e9613d88fbb24f4d9da124
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::Patron::Categories;
37 my $input = CGI->new;
38 my $dbh = C4::Context->dbh;
40 # my $flagsrequired;
41 # $flagsrequired->{circulation}=1;
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user({template_name => "admin/smart-rules.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => {parameters => 'manage_circ_rules'},
48 debug => 1,
49 });
51 my $type=$input->param('type');
53 my $branch = $input->param('branch');
54 unless ( $branch ) {
55 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
56 $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
58 else {
59 $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
62 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
64 my $op = $input->param('op') || q{};
65 my $language = C4::Languages::getlanguage();
67 if ($op eq 'delete') {
68 my $itemtype = $input->param('itemtype');
69 my $categorycode = $input->param('categorycode');
70 $debug and warn "deleting $1 $2 $branch";
72 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
73 $sth_Idelete->execute($branch, $categorycode, $itemtype);
75 elsif ($op eq 'delete-branch-cat') {
76 my $categorycode = $input->param('categorycode');
77 if ($branch eq "*") {
78 if ($categorycode eq "*") {
79 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
80 $sth_delete->execute();
81 } else {
82 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
83 WHERE categorycode = ?");
84 $sth_delete->execute($categorycode);
86 } elsif ($categorycode eq "*") {
87 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
88 WHERE branchcode = ?");
89 $sth_delete->execute($branch);
90 } else {
91 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
92 WHERE branchcode = ?
93 AND categorycode = ?");
94 $sth_delete->execute($branch, $categorycode);
97 elsif ($op eq 'delete-branch-item') {
98 my $itemtype = $input->param('itemtype');
99 if ($branch eq "*") {
100 if ($itemtype eq "*") {
101 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
102 $sth_delete->execute();
103 } else {
104 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
105 WHERE itemtype = ?");
106 $sth_delete->execute($itemtype);
108 } elsif ($itemtype eq "*") {
109 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
110 WHERE branchcode = ?");
111 $sth_delete->execute($branch);
112 } else {
113 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
114 WHERE branchcode = ?
115 AND itemtype = ?");
116 $sth_delete->execute($branch, $itemtype);
119 # save the values entered
120 elsif ($op eq 'add') {
121 my $br = $branch; # branch
122 my $bor = $input->param('categorycode'); # borrower category
123 my $itemtype = $input->param('itemtype'); # item type
124 my $fine = $input->param('fine');
125 my $finedays = $input->param('finedays');
126 my $maxsuspensiondays = $input->param('maxsuspensiondays');
127 $maxsuspensiondays = undef if $maxsuspensiondays eq q||;
128 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
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 suspension_chargeperiod => $suspension_chargeperiod,
173 firstremind => $firstremind,
174 chargeperiod => $chargeperiod,
175 chargeperiod_charge_at => $chargeperiod_charge_at,
176 maxissueqty => $maxissueqty,
177 maxonsiteissueqty => $maxonsiteissueqty,
178 renewalsallowed => $renewalsallowed,
179 renewalperiod => $renewalperiod,
180 norenewalbefore => $norenewalbefore,
181 auto_renew => $auto_renew,
182 no_auto_renewal_after => $no_auto_renewal_after,
183 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
184 reservesallowed => $reservesallowed,
185 holds_per_record => $holds_per_record,
186 issuelength => $issuelength,
187 lengthunit => $lengthunit,
188 hardduedate => $hardduedate,
189 hardduedatecompare => $hardduedatecompare,
190 rentaldiscount => $rentaldiscount,
191 onshelfholds => $onshelfholds,
192 opacitemholds => $opacitemholds,
193 overduefinescap => $overduefinescap,
194 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
195 article_requests => $article_requests,
198 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
199 if ($issuingrule) {
200 $issuingrule->set($params)->store();
201 } else {
202 Koha::IssuingRule->new()->set($params)->store();
206 elsif ($op eq "set-branch-defaults") {
207 my $categorycode = $input->param('categorycode');
208 my $maxissueqty = $input->param('maxissueqty');
209 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
210 my $holdallowed = $input->param('holdallowed');
211 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
212 my $returnbranch = $input->param('returnbranch');
213 $maxissueqty =~ s/\s//g;
214 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
215 $maxonsiteissueqty =~ s/\s//g;
216 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
217 $holdallowed =~ s/\s//g;
218 $holdallowed = undef if $holdallowed !~ /^\d+/;
220 if ($branch eq "*") {
221 my $sth_search = $dbh->prepare("SELECT count(*) AS total
222 FROM default_circ_rules");
223 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
224 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
225 VALUES (?, ?, ?, ?, ?)");
226 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
227 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
229 $sth_search->execute();
230 my $res = $sth_search->fetchrow_hashref();
231 if ($res->{total}) {
232 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
233 } else {
234 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
236 } else {
237 my $sth_search = $dbh->prepare("SELECT count(*) AS total
238 FROM default_branch_circ_rules
239 WHERE branchcode = ?");
240 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
241 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
242 VALUES (?, ?, ?, ?, ?, ?)");
243 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
244 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
245 WHERE branchcode = ?");
246 $sth_search->execute($branch);
247 my $res = $sth_search->fetchrow_hashref();
248 if ($res->{total}) {
249 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
250 } else {
251 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
255 elsif ($op eq "add-branch-cat") {
256 my $categorycode = $input->param('categorycode');
257 my $maxissueqty = $input->param('maxissueqty');
258 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
259 $maxissueqty =~ s/\s//g;
260 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
261 $maxonsiteissueqty =~ s/\s//g;
262 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
264 if ($branch eq "*") {
265 if ($categorycode eq "*") {
266 my $sth_search = $dbh->prepare("SELECT count(*) AS total
267 FROM default_circ_rules");
268 my $sth_insert = $dbh->prepare(q|
269 INSERT INTO default_circ_rules
270 (maxissueqty, maxonsiteissueqty)
271 VALUES (?, ?)
273 my $sth_update = $dbh->prepare(q|
274 UPDATE default_circ_rules
275 SET maxissueqty = ?,
276 maxonsiteissueqty = ?
279 $sth_search->execute();
280 my $res = $sth_search->fetchrow_hashref();
281 if ($res->{total}) {
282 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
283 } else {
284 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
286 } else {
287 my $sth_search = $dbh->prepare("SELECT count(*) AS total
288 FROM default_borrower_circ_rules
289 WHERE categorycode = ?");
290 my $sth_insert = $dbh->prepare(q|
291 INSERT INTO default_borrower_circ_rules
292 (categorycode, maxissueqty, maxonsiteissueqty)
293 VALUES (?, ?, ?)
295 my $sth_update = $dbh->prepare(q|
296 UPDATE default_borrower_circ_rules
297 SET maxissueqty = ?,
298 maxonsiteissueqty = ?
299 WHERE categorycode = ?
301 $sth_search->execute($categorycode);
302 my $res = $sth_search->fetchrow_hashref();
303 if ($res->{total}) {
304 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
305 } else {
306 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
309 } elsif ($categorycode eq "*") {
310 my $sth_search = $dbh->prepare("SELECT count(*) AS total
311 FROM default_branch_circ_rules
312 WHERE branchcode = ?");
313 my $sth_insert = $dbh->prepare(q|
314 INSERT INTO default_branch_circ_rules
315 (branchcode, maxissueqty, maxonsiteissueqty)
316 VALUES (?, ?, ?)
318 my $sth_update = $dbh->prepare(q|
319 UPDATE default_branch_circ_rules
320 SET maxissueqty = ?,
321 maxonsiteissueqty = ?
322 WHERE branchcode = ?
324 $sth_search->execute($branch);
325 my $res = $sth_search->fetchrow_hashref();
326 if ($res->{total}) {
327 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
328 } else {
329 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
331 } else {
332 my $sth_search = $dbh->prepare("SELECT count(*) AS total
333 FROM branch_borrower_circ_rules
334 WHERE branchcode = ?
335 AND categorycode = ?");
336 my $sth_insert = $dbh->prepare(q|
337 INSERT INTO branch_borrower_circ_rules
338 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
339 VALUES (?, ?, ?, ?)
341 my $sth_update = $dbh->prepare(q|
342 UPDATE branch_borrower_circ_rules
343 SET maxissueqty = ?,
344 maxonsiteissueqty = ?
345 WHERE branchcode = ?
346 AND categorycode = ?
349 $sth_search->execute($branch, $categorycode);
350 my $res = $sth_search->fetchrow_hashref();
351 if ($res->{total}) {
352 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
353 } else {
354 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
358 elsif ($op eq "add-branch-item") {
359 my $itemtype = $input->param('itemtype');
360 my $holdallowed = $input->param('holdallowed');
361 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
362 my $returnbranch = $input->param('returnbranch');
364 $holdallowed =~ s/\s//g;
365 $holdallowed = undef if $holdallowed !~ /^\d+/;
367 if ($branch eq "*") {
368 if ($itemtype eq "*") {
369 my $sth_search = $dbh->prepare("SELECT count(*) AS total
370 FROM default_circ_rules");
371 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
372 (holdallowed, hold_fulfillment_policy, returnbranch)
373 VALUES (?, ?, ?)");
374 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
375 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
377 $sth_search->execute();
378 my $res = $sth_search->fetchrow_hashref();
379 if ($res->{total}) {
380 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
381 } else {
382 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
384 } else {
385 my $sth_search = $dbh->prepare("SELECT count(*) AS total
386 FROM default_branch_item_rules
387 WHERE itemtype = ?");
388 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
389 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
390 VALUES (?, ?, ?, ?)");
391 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
392 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
393 WHERE itemtype = ?");
394 $sth_search->execute($itemtype);
395 my $res = $sth_search->fetchrow_hashref();
396 if ($res->{total}) {
397 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
398 } else {
399 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
402 } elsif ($itemtype eq "*") {
403 my $sth_search = $dbh->prepare("SELECT count(*) AS total
404 FROM default_branch_circ_rules
405 WHERE branchcode = ?");
406 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
407 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
408 VALUES (?, ?, ?, ?)");
409 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
410 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
411 WHERE branchcode = ?");
412 $sth_search->execute($branch);
413 my $res = $sth_search->fetchrow_hashref();
414 if ($res->{total}) {
415 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
416 } else {
417 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
419 } else {
420 my $sth_search = $dbh->prepare("SELECT count(*) AS total
421 FROM branch_item_rules
422 WHERE branchcode = ?
423 AND itemtype = ?");
424 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
425 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
426 VALUES (?, ?, ?, ?, ?)");
427 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
428 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
429 WHERE branchcode = ?
430 AND itemtype = ?");
432 $sth_search->execute($branch, $itemtype);
433 my $res = $sth_search->fetchrow_hashref();
434 if ($res->{total}) {
435 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
436 } else {
437 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
441 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
443 my $refund = $input->param('refund');
445 if ( $refund eq '*' ) {
446 if ( $branch ne '*' ) {
447 # only do something for $refund eq '*' if branch-specific
448 eval {
449 # Delete it so it picks the default
450 Koha::RefundLostItemFeeRules->find({
451 branchcode => $branch
452 })->delete;
455 } else {
456 my $refundRule =
457 Koha::RefundLostItemFeeRules->find({
458 branchcode => $branch
459 }) // Koha::RefundLostItemFeeRule->new;
460 $refundRule->set({
461 branchcode => $branch,
462 refund => $refund
463 })->store;
467 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
468 $template->param(
469 refundLostItemFeeRule => $refundLostItemFeeRule,
470 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
473 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
475 my @row_loop;
476 my $itemtypes = Koha::ItemTypes->search_with_localization;
478 my $sth2 = $dbh->prepare("
479 SELECT issuingrules.*,
480 itemtypes.description AS humanitemtype,
481 categories.description AS humancategorycode,
482 COALESCE( localization.translation, itemtypes.description ) AS translated_description
483 FROM issuingrules
484 LEFT JOIN itemtypes
485 ON (itemtypes.itemtype = issuingrules.itemtype)
486 LEFT JOIN categories
487 ON (categories.categorycode = issuingrules.categorycode)
488 LEFT JOIN localization ON issuingrules.itemtype = localization.code
489 AND localization.entity = 'itemtypes'
490 AND localization.lang = ?
491 WHERE issuingrules.branchcode = ?
493 $sth2->execute($language, $branch);
495 while (my $row = $sth2->fetchrow_hashref) {
496 $row->{'current_branch'} ||= $row->{'branchcode'};
497 $row->{humanitemtype} ||= $row->{itemtype};
498 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
499 $row->{'humancategorycode'} ||= $row->{'categorycode'};
500 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
501 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
502 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
503 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
504 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
505 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
506 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
507 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
508 } else {
509 $row->{'hardduedate'} = 0;
511 if ($row->{no_auto_renewal_after_hard_limit}) {
512 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
513 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
516 push @row_loop, $row;
519 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
521 my $sth_branch_cat;
522 if ($branch eq "*") {
523 $sth_branch_cat = $dbh->prepare("
524 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
525 FROM default_borrower_circ_rules
526 JOIN categories USING (categorycode)
529 $sth_branch_cat->execute();
530 } else {
531 $sth_branch_cat = $dbh->prepare("
532 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
533 FROM branch_borrower_circ_rules
534 JOIN categories USING (categorycode)
535 WHERE branch_borrower_circ_rules.branchcode = ?
537 $sth_branch_cat->execute($branch);
540 my @branch_cat_rules = ();
541 while (my $row = $sth_branch_cat->fetchrow_hashref) {
542 push @branch_cat_rules, $row;
544 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
546 # note undef maxissueqty so that template can deal with them
547 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
548 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
549 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
552 @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);
594 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
596 my $sth_defaults;
597 if ($branch eq "*") {
598 $sth_defaults = $dbh->prepare("
599 SELECT *
600 FROM default_circ_rules
602 $sth_defaults->execute();
603 } else {
604 $sth_defaults = $dbh->prepare("
605 SELECT *
606 FROM default_branch_circ_rules
607 WHERE branchcode = ?
609 $sth_defaults->execute($branch);
612 my $defaults = $sth_defaults->fetchrow_hashref;
614 if ($defaults) {
615 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
616 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
617 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
618 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
619 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
620 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
621 $template->param( default_returnbranch => $defaults->{returnbranch} );
624 $template->param(default_rules => ($defaults ? 1 : 0));
626 $template->param(
627 patron_categories => $patron_categories,
628 itemtypeloop => $itemtypes,
629 rules => \@sorted_row_loop,
630 humanbranch => ($branch ne '*' ? $branch : ''),
631 current_branch => $branch,
632 definedbranch => scalar(@sorted_row_loop)>0
634 output_html_with_http_headers $input, $cookie, $template->output;
636 exit 0;
638 # sort by patron category, then item type, putting
639 # default entries at the bottom
640 sub by_category_and_itemtype {
641 unless (by_category($a, $b)) {
642 return by_itemtype($a, $b);
646 sub by_category {
647 my ($a, $b) = @_;
648 if ($a->{'default_humancategorycode'}) {
649 return ($b->{'default_humancategorycode'} ? 0 : 1);
650 } elsif ($b->{'default_humancategorycode'}) {
651 return -1;
652 } else {
653 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
657 sub by_itemtype {
658 my ($a, $b) = @_;
659 if ($a->{default_translated_description}) {
660 return ($b->{'default_translated_description'} ? 0 : 1);
661 } elsif ($b->{'default_translated_description'}) {
662 return -1;
663 } else {
664 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};