Bug 20743: Update two-column templates with Bootstrap grid: Administration part 7
[koha.git] / admin / smart-rules.pl
blobeb4ef7b54f66ce1773b0ad20e51a16022fc833b8
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 my $max_holds = $input->param('max_holds');
260 $maxissueqty =~ s/\s//g;
261 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
262 $maxonsiteissueqty =~ s/\s//g;
263 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
264 $max_holds =~ s/\s//g;
265 $max_holds = undef if $max_holds !~ /^\d+/;
267 if ($branch eq "*") {
268 if ($categorycode eq "*") {
269 my $sth_search = $dbh->prepare("SELECT count(*) AS total
270 FROM default_circ_rules");
271 my $sth_insert = $dbh->prepare(q|
272 INSERT INTO default_circ_rules
273 (maxissueqty, maxonsiteissueqty, max_holds)
274 VALUES (?, ?, ?)
276 my $sth_update = $dbh->prepare(q|
277 UPDATE default_circ_rules
278 SET maxissueqty = ?,
279 maxonsiteissueqty = ?,
280 max_holds = ?
283 $sth_search->execute();
284 my $res = $sth_search->fetchrow_hashref();
285 if ($res->{total}) {
286 $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $max_holds );
287 } else {
288 $sth_insert->execute( $maxissueqty, $maxonsiteissueqty, $max_holds );
290 } else {
291 my $sth_search = $dbh->prepare("SELECT count(*) AS total
292 FROM default_borrower_circ_rules
293 WHERE categorycode = ?");
294 my $sth_insert = $dbh->prepare(q|
295 INSERT INTO default_borrower_circ_rules
296 (categorycode, maxissueqty, maxonsiteissueqty, max_holds)
297 VALUES (?, ?, ?, ?)
299 my $sth_update = $dbh->prepare(q|
300 UPDATE default_borrower_circ_rules
301 SET maxissueqty = ?,
302 maxonsiteissueqty = ?,
303 max_holds = ?
304 WHERE categorycode = ?
306 $sth_search->execute($categorycode);
307 my $res = $sth_search->fetchrow_hashref();
308 if ($res->{total}) {
309 $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $max_holds, $categorycode );
310 } else {
311 $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty, $max_holds );
314 } elsif ($categorycode eq "*") {
315 my $sth_search = $dbh->prepare("SELECT count(*) AS total
316 FROM default_branch_circ_rules
317 WHERE branchcode = ?");
318 my $sth_insert = $dbh->prepare(q|
319 INSERT INTO default_branch_circ_rules
320 (branchcode, maxissueqty, maxonsiteissueqty)
321 VALUES (?, ?, ?)
323 my $sth_update = $dbh->prepare(q|
324 UPDATE default_branch_circ_rules
325 SET maxissueqty = ?,
326 maxonsiteissueqty = ?
327 WHERE branchcode = ?
329 $sth_search->execute($branch);
330 my $res = $sth_search->fetchrow_hashref();
331 if ($res->{total}) {
332 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
333 } else {
334 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
336 } else {
337 my $sth_search = $dbh->prepare("SELECT count(*) AS total
338 FROM branch_borrower_circ_rules
339 WHERE branchcode = ?
340 AND categorycode = ?");
341 my $sth_insert = $dbh->prepare(q|
342 INSERT INTO branch_borrower_circ_rules
343 (branchcode, categorycode, maxissueqty, maxonsiteissueqty, max_holds)
344 VALUES (?, ?, ?, ?, ?)
346 my $sth_update = $dbh->prepare(q|
347 UPDATE branch_borrower_circ_rules
348 SET maxissueqty = ?,
349 maxonsiteissueqty = ?
350 max_holds = ?
351 WHERE branchcode = ?
352 AND categorycode = ?
355 $sth_search->execute($branch, $categorycode);
356 my $res = $sth_search->fetchrow_hashref();
357 if ($res->{total}) {
358 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $max_holds, $branch, $categorycode);
359 } else {
360 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty, $max_holds);
364 elsif ($op eq "add-branch-item") {
365 my $itemtype = $input->param('itemtype');
366 my $holdallowed = $input->param('holdallowed');
367 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
368 my $returnbranch = $input->param('returnbranch');
370 $holdallowed =~ s/\s//g;
371 $holdallowed = undef if $holdallowed !~ /^\d+/;
373 if ($branch eq "*") {
374 if ($itemtype eq "*") {
375 my $sth_search = $dbh->prepare("SELECT count(*) AS total
376 FROM default_circ_rules");
377 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
378 (holdallowed, hold_fulfillment_policy, returnbranch)
379 VALUES (?, ?, ?)");
380 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
381 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
383 $sth_search->execute();
384 my $res = $sth_search->fetchrow_hashref();
385 if ($res->{total}) {
386 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
387 } else {
388 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
390 } else {
391 my $sth_search = $dbh->prepare("SELECT count(*) AS total
392 FROM default_branch_item_rules
393 WHERE itemtype = ?");
394 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
395 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
396 VALUES (?, ?, ?, ?)");
397 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
398 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
399 WHERE itemtype = ?");
400 $sth_search->execute($itemtype);
401 my $res = $sth_search->fetchrow_hashref();
402 if ($res->{total}) {
403 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
404 } else {
405 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
408 } elsif ($itemtype eq "*") {
409 my $sth_search = $dbh->prepare("SELECT count(*) AS total
410 FROM default_branch_circ_rules
411 WHERE branchcode = ?");
412 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
413 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
414 VALUES (?, ?, ?, ?)");
415 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
416 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
417 WHERE branchcode = ?");
418 $sth_search->execute($branch);
419 my $res = $sth_search->fetchrow_hashref();
420 if ($res->{total}) {
421 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
422 } else {
423 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
425 } else {
426 my $sth_search = $dbh->prepare("SELECT count(*) AS total
427 FROM branch_item_rules
428 WHERE branchcode = ?
429 AND itemtype = ?");
430 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
431 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
432 VALUES (?, ?, ?, ?, ?)");
433 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
434 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
435 WHERE branchcode = ?
436 AND itemtype = ?");
438 $sth_search->execute($branch, $itemtype);
439 my $res = $sth_search->fetchrow_hashref();
440 if ($res->{total}) {
441 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
442 } else {
443 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
447 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
449 my $refund = $input->param('refund');
451 if ( $refund eq '*' ) {
452 if ( $branch ne '*' ) {
453 # only do something for $refund eq '*' if branch-specific
454 eval {
455 # Delete it so it picks the default
456 Koha::RefundLostItemFeeRules->find({
457 branchcode => $branch
458 })->delete;
461 } else {
462 my $refundRule =
463 Koha::RefundLostItemFeeRules->find({
464 branchcode => $branch
465 }) // Koha::RefundLostItemFeeRule->new;
466 $refundRule->set({
467 branchcode => $branch,
468 refund => $refund
469 })->store;
473 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
474 $template->param(
475 refundLostItemFeeRule => $refundLostItemFeeRule,
476 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
479 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
481 my @row_loop;
482 my $itemtypes = Koha::ItemTypes->search_with_localization;
484 my $sth2 = $dbh->prepare("
485 SELECT issuingrules.*,
486 itemtypes.description AS humanitemtype,
487 categories.description AS humancategorycode,
488 COALESCE( localization.translation, itemtypes.description ) AS translated_description
489 FROM issuingrules
490 LEFT JOIN itemtypes
491 ON (itemtypes.itemtype = issuingrules.itemtype)
492 LEFT JOIN categories
493 ON (categories.categorycode = issuingrules.categorycode)
494 LEFT JOIN localization ON issuingrules.itemtype = localization.code
495 AND localization.entity = 'itemtypes'
496 AND localization.lang = ?
497 WHERE issuingrules.branchcode = ?
499 $sth2->execute($language, $branch);
501 while (my $row = $sth2->fetchrow_hashref) {
502 $row->{'current_branch'} ||= $row->{'branchcode'};
503 $row->{humanitemtype} ||= $row->{itemtype};
504 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
505 $row->{'humancategorycode'} ||= $row->{'categorycode'};
506 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
507 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
508 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
509 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
510 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
511 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
512 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
513 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
514 } else {
515 $row->{'hardduedate'} = 0;
517 if ($row->{no_auto_renewal_after_hard_limit}) {
518 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
519 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
522 push @row_loop, $row;
525 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
527 my $sth_branch_cat;
528 if ($branch eq "*") {
529 $sth_branch_cat = $dbh->prepare("
530 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
531 FROM default_borrower_circ_rules
532 JOIN categories USING (categorycode)
535 $sth_branch_cat->execute();
536 } else {
537 $sth_branch_cat = $dbh->prepare("
538 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
539 FROM branch_borrower_circ_rules
540 JOIN categories USING (categorycode)
541 WHERE branch_borrower_circ_rules.branchcode = ?
543 $sth_branch_cat->execute($branch);
546 my @branch_cat_rules = ();
547 while (my $row = $sth_branch_cat->fetchrow_hashref) {
548 push @branch_cat_rules, $row;
550 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
552 # note undef maxissueqty so that template can deal with them
553 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
554 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
555 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
556 $entry->{unlimited_max_holds} = 1 unless defined($entry->{max_holds});
559 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
561 my $sth_branch_item;
562 if ($branch eq "*") {
563 $sth_branch_item = $dbh->prepare("
564 SELECT default_branch_item_rules.*,
565 COALESCE( localization.translation, itemtypes.description ) AS translated_description
566 FROM default_branch_item_rules
567 JOIN itemtypes USING (itemtype)
568 LEFT JOIN localization ON itemtypes.itemtype = localization.code
569 AND localization.entity = 'itemtypes'
570 AND localization.lang = ?
572 $sth_branch_item->execute($language);
573 } else {
574 $sth_branch_item = $dbh->prepare("
575 SELECT branch_item_rules.*,
576 COALESCE( localization.translation, itemtypes.description ) AS translated_description
577 FROM branch_item_rules
578 JOIN itemtypes USING (itemtype)
579 LEFT JOIN localization ON itemtypes.itemtype = localization.code
580 AND localization.entity = 'itemtypes'
581 AND localization.lang = ?
582 WHERE branch_item_rules.branchcode = ?
584 $sth_branch_item->execute($language, $branch);
587 my @branch_item_rules = ();
588 while (my $row = $sth_branch_item->fetchrow_hashref) {
589 push @branch_item_rules, $row;
591 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
593 # note undef holdallowed so that template can deal with them
594 foreach my $entry (@sorted_branch_item_rules) {
595 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
596 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
599 $template->param(show_branch_cat_rule_form => 1);
600 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
601 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
603 my $sth_defaults;
604 if ($branch eq "*") {
605 $sth_defaults = $dbh->prepare("
606 SELECT *
607 FROM default_circ_rules
609 $sth_defaults->execute();
610 } else {
611 $sth_defaults = $dbh->prepare("
612 SELECT *
613 FROM default_branch_circ_rules
614 WHERE branchcode = ?
616 $sth_defaults->execute($branch);
619 my $defaults = $sth_defaults->fetchrow_hashref;
621 if ($defaults) {
622 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
623 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
624 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
625 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
626 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
627 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
628 $template->param( default_returnbranch => $defaults->{returnbranch} );
631 $template->param(default_rules => ($defaults ? 1 : 0));
633 $template->param(
634 patron_categories => $patron_categories,
635 itemtypeloop => $itemtypes,
636 rules => \@sorted_row_loop,
637 humanbranch => ($branch ne '*' ? $branch : ''),
638 current_branch => $branch,
639 definedbranch => scalar(@sorted_row_loop)>0
641 output_html_with_http_headers $input, $cookie, $template->output;
643 exit 0;
645 # sort by patron category, then item type, putting
646 # default entries at the bottom
647 sub by_category_and_itemtype {
648 unless (by_category($a, $b)) {
649 return by_itemtype($a, $b);
653 sub by_category {
654 my ($a, $b) = @_;
655 if ($a->{'default_humancategorycode'}) {
656 return ($b->{'default_humancategorycode'} ? 0 : 1);
657 } elsif ($b->{'default_humancategorycode'}) {
658 return -1;
659 } else {
660 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
664 sub by_itemtype {
665 my ($a, $b) = @_;
666 if ($a->{default_translated_description}) {
667 return ($b->{'default_translated_description'} ? 0 : 1);
668 } elsif ($b->{'default_translated_description'}) {
669 return -1;
670 } else {
671 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};