Bug 19804: Add a 'Fine charging interval' for suspension days
[koha.git] / admin / smart-rules.pl
blob718b68235c3dd07759527b19d992373950a39344
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 $firstremind = $input->param('firstremind');
129 my $chargeperiod = $input->param('chargeperiod');
130 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
131 my $maxissueqty = $input->param('maxissueqty');
132 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
133 my $renewalsallowed = $input->param('renewalsallowed');
134 my $renewalperiod = $input->param('renewalperiod');
135 my $norenewalbefore = $input->param('norenewalbefore');
136 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
137 my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
138 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
139 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
140 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
141 $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 );
142 $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 );
143 my $reservesallowed = $input->param('reservesallowed');
144 my $holds_per_record = $input->param('holds_per_record');
145 my $onshelfholds = $input->param('onshelfholds') || 0;
146 $maxissueqty =~ s/\s//g;
147 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
148 $maxonsiteissueqty =~ s/\s//g;
149 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
150 my $issuelength = $input->param('issuelength');
151 $issuelength = $issuelength eq q{} ? undef : $issuelength;
152 my $lengthunit = $input->param('lengthunit');
153 my $hardduedate = $input->param('hardduedate') || undef;
154 $hardduedate = eval { dt_from_string( $input->param('hardduedate') ) } if ( $hardduedate );
155 $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
156 my $hardduedatecompare = $input->param('hardduedatecompare');
157 my $rentaldiscount = $input->param('rentaldiscount');
158 my $opacitemholds = $input->param('opacitemholds') || 0;
159 my $article_requests = $input->param('article_requests') || 'no';
160 my $overduefinescap = $input->param('overduefinescap') || undef;
161 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
162 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
164 my $params = {
165 branchcode => $br,
166 categorycode => $bor,
167 itemtype => $itemtype,
168 fine => $fine,
169 finedays => $finedays,
170 maxsuspensiondays => $maxsuspensiondays,
171 firstremind => $firstremind,
172 chargeperiod => $chargeperiod,
173 chargeperiod_charge_at => $chargeperiod_charge_at,
174 maxissueqty => $maxissueqty,
175 maxonsiteissueqty => $maxonsiteissueqty,
176 renewalsallowed => $renewalsallowed,
177 renewalperiod => $renewalperiod,
178 norenewalbefore => $norenewalbefore,
179 auto_renew => $auto_renew,
180 no_auto_renewal_after => $no_auto_renewal_after,
181 no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
182 reservesallowed => $reservesallowed,
183 holds_per_record => $holds_per_record,
184 issuelength => $issuelength,
185 lengthunit => $lengthunit,
186 hardduedate => $hardduedate,
187 hardduedatecompare => $hardduedatecompare,
188 rentaldiscount => $rentaldiscount,
189 onshelfholds => $onshelfholds,
190 opacitemholds => $opacitemholds,
191 overduefinescap => $overduefinescap,
192 cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
193 article_requests => $article_requests,
196 my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype, branchcode => $br});
197 if ($issuingrule) {
198 $issuingrule->set($params)->store();
199 } else {
200 Koha::IssuingRule->new()->set($params)->store();
204 elsif ($op eq "set-branch-defaults") {
205 my $categorycode = $input->param('categorycode');
206 my $maxissueqty = $input->param('maxissueqty');
207 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
208 my $holdallowed = $input->param('holdallowed');
209 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
210 my $returnbranch = $input->param('returnbranch');
211 $maxissueqty =~ s/\s//g;
212 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
213 $maxonsiteissueqty =~ s/\s//g;
214 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
215 $holdallowed =~ s/\s//g;
216 $holdallowed = undef if $holdallowed !~ /^\d+/;
218 if ($branch eq "*") {
219 my $sth_search = $dbh->prepare("SELECT count(*) AS total
220 FROM default_circ_rules");
221 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
222 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
223 VALUES (?, ?, ?, ?, ?)");
224 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
225 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
227 $sth_search->execute();
228 my $res = $sth_search->fetchrow_hashref();
229 if ($res->{total}) {
230 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
231 } else {
232 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
234 } else {
235 my $sth_search = $dbh->prepare("SELECT count(*) AS total
236 FROM default_branch_circ_rules
237 WHERE branchcode = ?");
238 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
239 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
240 VALUES (?, ?, ?, ?, ?, ?)");
241 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
242 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
243 WHERE branchcode = ?");
244 $sth_search->execute($branch);
245 my $res = $sth_search->fetchrow_hashref();
246 if ($res->{total}) {
247 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
248 } else {
249 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
253 elsif ($op eq "add-branch-cat") {
254 my $categorycode = $input->param('categorycode');
255 my $maxissueqty = $input->param('maxissueqty');
256 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
257 $maxissueqty =~ s/\s//g;
258 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
259 $maxonsiteissueqty =~ s/\s//g;
260 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
262 if ($branch eq "*") {
263 if ($categorycode eq "*") {
264 my $sth_search = $dbh->prepare("SELECT count(*) AS total
265 FROM default_circ_rules");
266 my $sth_insert = $dbh->prepare(q|
267 INSERT INTO default_circ_rules
268 (maxissueqty, maxonsiteissueqty)
269 VALUES (?, ?)
271 my $sth_update = $dbh->prepare(q|
272 UPDATE default_circ_rules
273 SET maxissueqty = ?,
274 maxonsiteissueqty = ?
277 $sth_search->execute();
278 my $res = $sth_search->fetchrow_hashref();
279 if ($res->{total}) {
280 $sth_update->execute($maxissueqty, $maxonsiteissueqty);
281 } else {
282 $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
284 } else {
285 my $sth_search = $dbh->prepare("SELECT count(*) AS total
286 FROM default_borrower_circ_rules
287 WHERE categorycode = ?");
288 my $sth_insert = $dbh->prepare(q|
289 INSERT INTO default_borrower_circ_rules
290 (categorycode, maxissueqty, maxonsiteissueqty)
291 VALUES (?, ?, ?)
293 my $sth_update = $dbh->prepare(q|
294 UPDATE default_borrower_circ_rules
295 SET maxissueqty = ?,
296 maxonsiteissueqty = ?
297 WHERE categorycode = ?
299 $sth_search->execute($categorycode);
300 my $res = $sth_search->fetchrow_hashref();
301 if ($res->{total}) {
302 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
303 } else {
304 $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
307 } elsif ($categorycode eq "*") {
308 my $sth_search = $dbh->prepare("SELECT count(*) AS total
309 FROM default_branch_circ_rules
310 WHERE branchcode = ?");
311 my $sth_insert = $dbh->prepare(q|
312 INSERT INTO default_branch_circ_rules
313 (branchcode, maxissueqty, maxonsiteissueqty)
314 VALUES (?, ?, ?)
316 my $sth_update = $dbh->prepare(q|
317 UPDATE default_branch_circ_rules
318 SET maxissueqty = ?,
319 maxonsiteissueqty = ?
320 WHERE branchcode = ?
322 $sth_search->execute($branch);
323 my $res = $sth_search->fetchrow_hashref();
324 if ($res->{total}) {
325 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
326 } else {
327 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
329 } else {
330 my $sth_search = $dbh->prepare("SELECT count(*) AS total
331 FROM branch_borrower_circ_rules
332 WHERE branchcode = ?
333 AND categorycode = ?");
334 my $sth_insert = $dbh->prepare(q|
335 INSERT INTO branch_borrower_circ_rules
336 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
337 VALUES (?, ?, ?, ?)
339 my $sth_update = $dbh->prepare(q|
340 UPDATE branch_borrower_circ_rules
341 SET maxissueqty = ?,
342 maxonsiteissueqty = ?
343 WHERE branchcode = ?
344 AND categorycode = ?
347 $sth_search->execute($branch, $categorycode);
348 my $res = $sth_search->fetchrow_hashref();
349 if ($res->{total}) {
350 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
351 } else {
352 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
356 elsif ($op eq "add-branch-item") {
357 my $itemtype = $input->param('itemtype');
358 my $holdallowed = $input->param('holdallowed');
359 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
360 my $returnbranch = $input->param('returnbranch');
362 $holdallowed =~ s/\s//g;
363 $holdallowed = undef if $holdallowed !~ /^\d+/;
365 if ($branch eq "*") {
366 if ($itemtype eq "*") {
367 my $sth_search = $dbh->prepare("SELECT count(*) AS total
368 FROM default_circ_rules");
369 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
370 (holdallowed, hold_fulfillment_policy, returnbranch)
371 VALUES (?, ?, ?)");
372 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
373 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
375 $sth_search->execute();
376 my $res = $sth_search->fetchrow_hashref();
377 if ($res->{total}) {
378 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
379 } else {
380 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
382 } else {
383 my $sth_search = $dbh->prepare("SELECT count(*) AS total
384 FROM default_branch_item_rules
385 WHERE itemtype = ?");
386 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
387 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
388 VALUES (?, ?, ?, ?)");
389 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
390 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
391 WHERE itemtype = ?");
392 $sth_search->execute($itemtype);
393 my $res = $sth_search->fetchrow_hashref();
394 if ($res->{total}) {
395 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
396 } else {
397 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
400 } elsif ($itemtype eq "*") {
401 my $sth_search = $dbh->prepare("SELECT count(*) AS total
402 FROM default_branch_circ_rules
403 WHERE branchcode = ?");
404 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
405 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
406 VALUES (?, ?, ?, ?)");
407 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
408 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
409 WHERE branchcode = ?");
410 $sth_search->execute($branch);
411 my $res = $sth_search->fetchrow_hashref();
412 if ($res->{total}) {
413 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
414 } else {
415 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
417 } else {
418 my $sth_search = $dbh->prepare("SELECT count(*) AS total
419 FROM branch_item_rules
420 WHERE branchcode = ?
421 AND itemtype = ?");
422 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
423 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
424 VALUES (?, ?, ?, ?, ?)");
425 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
426 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
427 WHERE branchcode = ?
428 AND itemtype = ?");
430 $sth_search->execute($branch, $itemtype);
431 my $res = $sth_search->fetchrow_hashref();
432 if ($res->{total}) {
433 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
434 } else {
435 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
439 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
441 my $refund = $input->param('refund');
443 if ( $refund eq '*' ) {
444 if ( $branch ne '*' ) {
445 # only do something for $refund eq '*' if branch-specific
446 eval {
447 # Delete it so it picks the default
448 Koha::RefundLostItemFeeRules->find({
449 branchcode => $branch
450 })->delete;
453 } else {
454 my $refundRule =
455 Koha::RefundLostItemFeeRules->find({
456 branchcode => $branch
457 }) // Koha::RefundLostItemFeeRule->new;
458 $refundRule->set({
459 branchcode => $branch,
460 refund => $refund
461 })->store;
465 my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({ branchcode => $branch });
466 $template->param(
467 refundLostItemFeeRule => $refundLostItemFeeRule,
468 defaultRefundRule => Koha::RefundLostItemFeeRules->_default_rule
471 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
473 my @row_loop;
474 my $itemtypes = Koha::ItemTypes->search_with_localization;
476 my $sth2 = $dbh->prepare("
477 SELECT issuingrules.*,
478 itemtypes.description AS humanitemtype,
479 categories.description AS humancategorycode,
480 COALESCE( localization.translation, itemtypes.description ) AS translated_description
481 FROM issuingrules
482 LEFT JOIN itemtypes
483 ON (itemtypes.itemtype = issuingrules.itemtype)
484 LEFT JOIN categories
485 ON (categories.categorycode = issuingrules.categorycode)
486 LEFT JOIN localization ON issuingrules.itemtype = localization.code
487 AND localization.entity = 'itemtypes'
488 AND localization.lang = ?
489 WHERE issuingrules.branchcode = ?
491 $sth2->execute($language, $branch);
493 while (my $row = $sth2->fetchrow_hashref) {
494 $row->{'current_branch'} ||= $row->{'branchcode'};
495 $row->{humanitemtype} ||= $row->{itemtype};
496 $row->{default_translated_description} = 1 if $row->{humanitemtype} eq '*';
497 $row->{'humancategorycode'} ||= $row->{'categorycode'};
498 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
499 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
500 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
501 my $harddue_dt = eval { dt_from_string( $row->{'hardduedate'} ) };
502 $row->{'hardduedate'} = eval { output_pref( { dt => $harddue_dt, dateonly => 1 } ) } if ( $harddue_dt );
503 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
504 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
505 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
506 } else {
507 $row->{'hardduedate'} = 0;
509 if ($row->{no_auto_renewal_after_hard_limit}) {
510 my $dt = eval { dt_from_string( $row->{no_auto_renewal_after_hard_limit} ) };
511 $row->{no_auto_renewal_after_hard_limit} = eval { output_pref( { dt => $dt, dateonly => 1 } ) } if $dt;
514 push @row_loop, $row;
517 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
519 my $sth_branch_cat;
520 if ($branch eq "*") {
521 $sth_branch_cat = $dbh->prepare("
522 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
523 FROM default_borrower_circ_rules
524 JOIN categories USING (categorycode)
527 $sth_branch_cat->execute();
528 } else {
529 $sth_branch_cat = $dbh->prepare("
530 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
531 FROM branch_borrower_circ_rules
532 JOIN categories USING (categorycode)
533 WHERE branch_borrower_circ_rules.branchcode = ?
535 $sth_branch_cat->execute($branch);
538 my @branch_cat_rules = ();
539 while (my $row = $sth_branch_cat->fetchrow_hashref) {
540 push @branch_cat_rules, $row;
542 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
544 # note undef maxissueqty so that template can deal with them
545 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
546 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
547 $entry->{unlimited_maxonsiteissueqty} = 1 unless defined($entry->{maxonsiteissueqty});
550 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
552 my $sth_branch_item;
553 if ($branch eq "*") {
554 $sth_branch_item = $dbh->prepare("
555 SELECT default_branch_item_rules.*,
556 COALESCE( localization.translation, itemtypes.description ) AS translated_description
557 FROM default_branch_item_rules
558 JOIN itemtypes USING (itemtype)
559 LEFT JOIN localization ON itemtypes.itemtype = localization.code
560 AND localization.entity = 'itemtypes'
561 AND localization.lang = ?
563 $sth_branch_item->execute($language);
564 } else {
565 $sth_branch_item = $dbh->prepare("
566 SELECT branch_item_rules.*,
567 COALESCE( localization.translation, itemtypes.description ) AS translated_description
568 FROM branch_item_rules
569 JOIN itemtypes USING (itemtype)
570 LEFT JOIN localization ON itemtypes.itemtype = localization.code
571 AND localization.entity = 'itemtypes'
572 AND localization.lang = ?
573 WHERE branch_item_rules.branchcode = ?
575 $sth_branch_item->execute($language, $branch);
578 my @branch_item_rules = ();
579 while (my $row = $sth_branch_item->fetchrow_hashref) {
580 push @branch_item_rules, $row;
582 my @sorted_branch_item_rules = sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @branch_item_rules;
584 # note undef holdallowed so that template can deal with them
585 foreach my $entry (@sorted_branch_item_rules) {
586 $entry->{holdallowed_any} = 1 if ( $entry->{holdallowed} == 2 );
587 $entry->{holdallowed_same} = 1 if ( $entry->{holdallowed} == 1 );
590 $template->param(show_branch_cat_rule_form => 1);
591 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
592 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
594 my $sth_defaults;
595 if ($branch eq "*") {
596 $sth_defaults = $dbh->prepare("
597 SELECT *
598 FROM default_circ_rules
600 $sth_defaults->execute();
601 } else {
602 $sth_defaults = $dbh->prepare("
603 SELECT *
604 FROM default_branch_circ_rules
605 WHERE branchcode = ?
607 $sth_defaults->execute($branch);
610 my $defaults = $sth_defaults->fetchrow_hashref;
612 if ($defaults) {
613 $template->param( default_holdallowed_none => 1 ) if ( $defaults->{holdallowed} == 0 );
614 $template->param( default_holdallowed_same => 1 ) if ( $defaults->{holdallowed} == 1 );
615 $template->param( default_holdallowed_any => 1 ) if ( $defaults->{holdallowed} == 2 );
616 $template->param( default_hold_fulfillment_policy => $defaults->{hold_fulfillment_policy} );
617 $template->param( default_maxissueqty => $defaults->{maxissueqty} );
618 $template->param( default_maxonsiteissueqty => $defaults->{maxonsiteissueqty} );
619 $template->param( default_returnbranch => $defaults->{returnbranch} );
622 $template->param(default_rules => ($defaults ? 1 : 0));
624 $template->param(
625 patron_categories => $patron_categories,
626 itemtypeloop => $itemtypes,
627 rules => \@sorted_row_loop,
628 humanbranch => ($branch ne '*' ? $branch : ''),
629 current_branch => $branch,
630 definedbranch => scalar(@sorted_row_loop)>0
632 output_html_with_http_headers $input, $cookie, $template->output;
634 exit 0;
636 # sort by patron category, then item type, putting
637 # default entries at the bottom
638 sub by_category_and_itemtype {
639 unless (by_category($a, $b)) {
640 return by_itemtype($a, $b);
644 sub by_category {
645 my ($a, $b) = @_;
646 if ($a->{'default_humancategorycode'}) {
647 return ($b->{'default_humancategorycode'} ? 0 : 1);
648 } elsif ($b->{'default_humancategorycode'}) {
649 return -1;
650 } else {
651 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
655 sub by_itemtype {
656 my ($a, $b) = @_;
657 if ($a->{default_translated_description}) {
658 return ($b->{'default_translated_description'} ? 0 : 1);
659 } elsif ($b->{'default_translated_description'}) {
660 return -1;
661 } else {
662 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};