Bug 17032: Make sure Swagger object definition is up-to-date with Koha-object
[koha.git] / admin / smart-rules.pl
blob1aa6b895c833fabc970c5693ddcf49d8a0cd3ff8
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 $reservesallowed = $input->param('reservesallowed');
140 my $holds_per_record = $input->param('holds_per_record');
141 my $onshelfholds = $input->param('onshelfholds') || 0;
142 $maxissueqty =~ s/\s//g;
143 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
144 $maxonsiteissueqty =~ s/\s//g;
145 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
146 my $issuelength = $input->param('issuelength');
147 $issuelength = $issuelength eq q{} ? undef : $issuelength;
148 my $lengthunit = $input->param('lengthunit');
149 my $hardduedate = $input->param('hardduedate') || undef;
150 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
151 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
152 my $hardduedatecompare = $input->param('hardduedatecompare');
153 my $rentaldiscount = $input->param('rentaldiscount');
154 my $opacitemholds = $input->param('opacitemholds') || 0;
155 my $overduefinescap = $input->param('overduefinescap') || undef;
156 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
157 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
159 my $params = {
160 branchcode => $br,
161 categorycode => $bor,
162 itemtype => $itemtype,
163 fine => $fine,
164 finedays => $finedays,
165 maxsuspensiondays => $maxsuspensiondays,
166 firstremind => $firstremind,
167 chargeperiod => $chargeperiod,
168 chargeperiod_charge_at => $chargeperiod_charge_at,
169 maxissueqty => $maxissueqty,
170 maxonsiteissueqty => $maxonsiteissueqty,
171 renewalsallowed => $renewalsallowed,
172 renewalperiod => $renewalperiod,
173 norenewalbefore => $norenewalbefore,
174 auto_renew => $auto_renew,
175 reservesallowed => $reservesallowed,
176 holds_per_record => $holds_per_record,
177 issuelength => $issuelength,
178 lengthunit => $lengthunit,
179 hardduedate => $hardduedate,
180 hardduedatecompare => $hardduedatecompare,
181 rentaldiscount => $rentaldiscount,
182 onshelfholds => $onshelfholds,
183 opacitemholds => $opacitemholds,
184 overduefinescap => $overduefinescap,
185 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
188 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
189 if ($issuingrule) {
190 $issuingrule->set($params)->store();
191 } else {
192 Koha::IssuingRule->new()->set($params)->store();
196 elsif ($op eq "set-branch-defaults") {
197 my $categorycode = $input->param('categorycode');
198 my $maxissueqty = $input->param('maxissueqty');
199 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
200 my $holdallowed = $input->param('holdallowed');
201 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
202 my $returnbranch = $input->param('returnbranch');
203 $maxissueqty =~ s/\s//g;
204 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
205 $maxonsiteissueqty =~ s/\s//g;
206 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
207 $holdallowed =~ s/\s//g;
208 $holdallowed = undef if $holdallowed !~ /^\d+/;
210 if ($branch eq "*") {
211 my $sth_search = $dbh->prepare("SELECT count(*) AS total
212 FROM default_circ_rules");
213 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
214 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
215 VALUES (?, ?, ?, ?, ?)");
216 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
217 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
219 $sth_search->execute();
220 my $res = $sth_search->fetchrow_hashref();
221 if ($res->{total}) {
222 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
223 } else {
224 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
226 } else {
227 my $sth_search = $dbh->prepare("SELECT count(*) AS total
228 FROM default_branch_circ_rules
229 WHERE branchcode = ?");
230 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
231 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
232 VALUES (?, ?, ?, ?, ?, ?)");
233 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
234 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
235 WHERE branchcode = ?");
236 $sth_search->execute($branch);
237 my $res = $sth_search->fetchrow_hashref();
238 if ($res->{total}) {
239 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
240 } else {
241 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
245 elsif ($op eq "add-branch-cat") {
246 my $categorycode = $input->param('categorycode');
247 my $maxissueqty = $input->param('maxissueqty');
248 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
249 $maxissueqty =~ s/\s//g;
250 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
251 $maxonsiteissueqty =~ s/\s//g;
252 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
254 if ($branch eq "*") {
255 if ($categorycode eq "*") {
256 my $sth_search = $dbh->prepare("SELECT count(*) AS total
257 FROM default_circ_rules");
258 my $sth_insert = $dbh->prepare(q|
259 INSERT INTO default_circ_rules
260 (maxissueqty, maxonsiteissueqty)
261 VALUES (?, ?)
263 my $sth_update = $dbh->prepare(q|
264 UPDATE default_circ_rules
265 SET maxissueqty = ?,
266 maxonsiteissueqty = ?
269 $sth_search->execute();
270 my $res = $sth_search->fetchrow_hashref();
271 if ($res->{total}) {
272 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
273 } else {
274 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
276 } else {
277 my $sth_search = $dbh->prepare("SELECT count(*) AS total
278 FROM default_borrower_circ_rules
279 WHERE categorycode = ?");
280 my $sth_insert = $dbh->prepare(q|
281 INSERT INTO default_borrower_circ_rules
282 (categorycode, maxissueqty, maxonsiteissueqty)
283 VALUES (?, ?, ?)
285 my $sth_update = $dbh->prepare(q|
286 UPDATE default_borrower_circ_rules
287 SET maxissueqty = ?,
288 maxonsiteissueqty = ?
289 WHERE categorycode = ?
291 $sth_search->execute($branch);
292 my $res = $sth_search->fetchrow_hashref();
293 if ($res->{total}) {
294 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
295 } else {
296 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
299 } elsif ($categorycode eq "*") {
300 my $sth_search = $dbh->prepare("SELECT count(*) AS total
301 FROM default_branch_circ_rules
302 WHERE branchcode = ?");
303 my $sth_insert = $dbh->prepare(q|
304 INSERT INTO default_branch_circ_rules
305 (branchcode, maxissueqty, maxonsiteissueqty)
306 VALUES (?, ?, ?)
308 my $sth_update = $dbh->prepare(q|
309 UPDATE default_branch_circ_rules
310 SET maxissueqty = ?,
311 maxonsiteissueqty = ?
312 WHERE branchcode = ?
314 $sth_search->execute($branch);
315 my $res = $sth_search->fetchrow_hashref();
316 if ($res->{total}) {
317 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
318 } else {
319 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
321 } else {
322 my $sth_search = $dbh->prepare("SELECT count(*) AS total
323 FROM branch_borrower_circ_rules
324 WHERE branchcode = ?
325 AND categorycode = ?");
326 my $sth_insert = $dbh->prepare(q|
327 INSERT INTO branch_borrower_circ_rules
328 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
329 VALUES (?, ?, ?, ?)
331 my $sth_update = $dbh->prepare(q|
332 UPDATE branch_borrower_circ_rules
333 SET maxissueqty = ?,
334 maxonsiteissueqty = ?
335 WHERE branchcode = ?
336 AND categorycode = ?
339 $sth_search->execute($branch, $categorycode);
340 my $res = $sth_search->fetchrow_hashref();
341 if ($res->{total}) {
342 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
343 } else {
344 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
348 elsif ($op eq "add-branch-item") {
349 my $itemtype = $input->param('itemtype');
350 my $holdallowed = $input->param('holdallowed');
351 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
352 my $returnbranch = $input->param('returnbranch');
354 $holdallowed =~ s/\s//g;
355 $holdallowed = undef if $holdallowed !~ /^\d+/;
357 if ($branch eq "*") {
358 if ($itemtype eq "*") {
359 my $sth_search = $dbh->prepare("SELECT count(*) AS total
360 FROM default_circ_rules");
361 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
362 (holdallowed, hold_fulfillment_policy, returnbranch)
363 VALUES (?, ?, ?)");
364 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
365 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
367 $sth_search->execute();
368 my $res = $sth_search->fetchrow_hashref();
369 if ($res->{total}) {
370 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
371 } else {
372 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
374 } else {
375 my $sth_search = $dbh->prepare("SELECT count(*) AS total
376 FROM default_branch_item_rules
377 WHERE itemtype = ?");
378 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
379 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
380 VALUES (?, ?, ?, ?)");
381 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
382 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
383 WHERE itemtype = ?");
384 $sth_search->execute($itemtype);
385 my $res = $sth_search->fetchrow_hashref();
386 if ($res->{total}) {
387 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
388 } else {
389 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
392 } elsif ($itemtype eq "*") {
393 my $sth_search = $dbh->prepare("SELECT count(*) AS total
394 FROM default_branch_circ_rules
395 WHERE branchcode = ?");
396 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
397 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
398 VALUES (?, ?, ?, ?)");
399 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
400 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
401 WHERE branchcode = ?");
402 $sth_search->execute($branch);
403 my $res = $sth_search->fetchrow_hashref();
404 if ($res->{total}) {
405 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
406 } else {
407 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
409 } else {
410 my $sth_search = $dbh->prepare("SELECT count(*) AS total
411 FROM branch_item_rules
412 WHERE branchcode = ?
413 AND itemtype = ?");
414 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
415 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
416 VALUES (?, ?, ?, ?, ?)");
417 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
418 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
419 WHERE branchcode = ?
420 AND itemtype = ?");
422 $sth_search->execute($branch, $itemtype);
423 my $res = $sth_search->fetchrow_hashref();
424 if ($res->{total}) {
425 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
426 } else {
427 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
431 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
433 my $refund = $input->param('refund');
435 if ( $refund eq '*' ) {
436 if ( $branch ne '*' ) {
437 # only do something for $refund eq '*' if branch-specific
438 eval {
439 # Delete it so it picks the default
440 Koha::RefundLostItemFeeRules->find({
441 branchcode => $branch
442 })->delete;
445 } else {
446 my $refundRule =
447 Koha::RefundLostItemFeeRules->find({
448 branchcode => $branch
449 }) // Koha::RefundLostItemFeeRule->new;
450 $refundRule->set({
451 branchcode => $branch,
452 refund => $refund
453 })->store;
457 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
458 $template->param(
459 refundLostItemFeeRule => $refundLostItemFeeRule,
460 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
463 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
465 my @row_loop;
466 my @itemtypes = @{ GetItemTypes( style => 'array' ) };
467 @itemtypes = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @itemtypes;
469 my $sth2 = $dbh->prepare("
470 SELECT issuingrules.*,
471 itemtypes.description AS humanitemtype,
472 categories.description AS humancategorycode,
473 COALESCE( localization.translation, itemtypes.description ) AS translated_description
474 FROM issuingrules
475 LEFT JOIN itemtypes
476 ON (itemtypes.itemtype = issuingrules.itemtype)
477 LEFT JOIN categories
478 ON (categories.categorycode = issuingrules.categorycode)
479 LEFT JOIN localization ON issuingrules.itemtype = localization.code
480 AND localization.entity = 'itemtypes'
481 AND localization.lang = ?
482 WHERE issuingrules.branchcode = ?
484 $sth2->execute($language, $branch);
486 while (my $row = $sth2->fetchrow_hashref) {
487 $row->{'current_branch'} ||= $row->{'branchcode'};
488 $row->{humanitemtype} ||= $row->{itemtype};
489 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
490 $row->{'humancategorycode'} ||= $row->{'categorycode'};
491 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
492 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
493 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
494 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
495 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
496 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
497 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
498 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
499 } else {
500 $row->{'hardduedate'} = 0;
502 push @row_loop, $row;
505 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
507 my $sth_branch_cat;
508 if ($branch eq "*") {
509 $sth_branch_cat = $dbh->prepare("
510 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
511 FROM default_borrower_circ_rules
512 JOIN categories USING (categorycode)
515 $sth_branch_cat->execute();
516 } else {
517 $sth_branch_cat = $dbh->prepare("
518 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
519 FROM branch_borrower_circ_rules
520 JOIN categories USING (categorycode)
521 WHERE branch_borrower_circ_rules.branchcode = ?
523 $sth_branch_cat->execute($branch);
526 my @branch_cat_rules = ();
527 while (my $row = $sth_branch_cat->fetchrow_hashref) {
528 push @branch_cat_rules, $row;
530 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
532 # note undef maxissueqty so that template can deal with them
533 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
534 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
535 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
538 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
540 my $sth_branch_item;
541 if ($branch eq "*") {
542 $sth_branch_item = $dbh->prepare("
543 SELECT default_branch_item_rules.*,
544 COALESCE( localization.translation, itemtypes.description ) AS translated_description
545 FROM default_branch_item_rules
546 JOIN itemtypes USING (itemtype)
547 LEFT JOIN localization ON itemtypes.itemtype = localization.code
548 AND localization.entity = 'itemtypes'
549 AND localization.lang = ?
551 $sth_branch_item->execute($language);
552 } else {
553 $sth_branch_item = $dbh->prepare("
554 SELECT branch_item_rules.*,
555 COALESCE( localization.translation, itemtypes.description ) AS translated_description
556 FROM branch_item_rules
557 JOIN itemtypes USING (itemtype)
558 LEFT JOIN localization ON itemtypes.itemtype = localization.code
559 AND localization.entity = 'itemtypes'
560 AND localization.lang = ?
561 WHERE branch_item_rules.branchcode = ?
563 $sth_branch_item->execute($language, $branch);
566 my @branch_item_rules = ();
567 while (my $row = $sth_branch_item->fetchrow_hashref) {
568 push @branch_item_rules, $row;
570 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
572 # note undef holdallowed so that template can deal with them
573 foreach my $entry (@sorted_branch_item_rules) {
574 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
575 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
578 $template->param(show_branch_cat_rule_form => 1);
579 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
580 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
582 my $sth_defaults;
583 if ($branch eq "*") {
584 $sth_defaults = $dbh->prepare("
585 SELECT *
586 FROM default_circ_rules
588 $sth_defaults->execute();
589 } else {
590 $sth_defaults = $dbh->prepare("
591 SELECT *
592 FROM default_branch_circ_rules
593 WHERE branchcode = ?
595 $sth_defaults->execute($branch);
598 my $defaults = $sth_defaults->fetchrow_hashref;
600 if ($defaults) {
601 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
602 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
603 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
604 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
605 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
606 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
607 $template->param( default_returnbranch => $defaults->{returnbranch} );
610 $template->param(default_rules => ($defaults ? 1 : 0));
612 $template->param(
613 patron_categories => $patron_categories,
614 itemtypeloop => \@itemtypes,
615 rules => \@sorted_row_loop,
616 humanbranch => ($branch ne '*' ? $branch : ''),
617 current_branch => $branch,
618 definedbranch => scalar(@sorted_row_loop)>0
620 output_html_with_http_headers $input, $cookie, $template->output;
622 exit 0;
624 # sort by patron category, then item type, putting
625 # default entries at the bottom
626 sub by_category_and_itemtype {
627 unless (by_category($a, $b)) {
628 return by_itemtype($a, $b);
632 sub by_category {
633 my ($a, $b) = @_;
634 if ($a->{'default_humancategorycode'}) {
635 return ($b->{'default_humancategorycode'} ? 0 : 1);
636 } elsif ($b->{'default_humancategorycode'}) {
637 return -1;
638 } else {
639 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
643 sub by_itemtype {
644 my ($a, $b) = @_;
645 if ($a->{default_translated_description}) {
646 return ($b->{'default_translated_description'} ? 0 : 1);
647 } elsif ($b->{'default_translated_description'}) {
648 return -1;
649 } else {
650 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};