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>.
29 use Koha
::IssuingRule
;
30 use Koha
::IssuingRules
;
32 use Koha
::RefundLostItemFeeRule
;
33 use Koha
::RefundLostItemFeeRules
;
35 use Koha
::CirculationRules
;
36 use Koha
::Patron
::Categories
;
41 my $dbh = C4
::Context
->dbh;
44 # $flagsrequired->{circulation}=1;
45 my ($template, $loggedinuser, $cookie)
46 = get_template_and_user
({template_name
=> "admin/smart-rules.tt",
50 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
54 my $type=$input->param('type');
56 my $branch = $input->param('branch');
58 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
59 $branch = Koha
::Libraries
->search->count() == 1 ?
undef : C4
::Context
::mybranch
();
62 $branch = C4
::Context
::only_my_library
() ?
( C4
::Context
::mybranch
() || '*' ) : '*';
66 my $logged_in_patron = Koha
::Patrons
->find( $loggedinuser );
68 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters
=> 'manage_circ_rules_from_any_libraries' } );
69 $template->param( restricted_to_own_library
=> not $can_edit_from_any_library );
70 $branch = C4
::Context
::mybranch
() unless $can_edit_from_any_library;
72 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
74 my $op = $input->param('op') || q{};
75 my $language = C4
::Languages
::getlanguage
();
77 my $cache = Koha
::Caches
->get_instance;
78 $cache->clear_from_cache( Koha
::IssuingRules
::GUESSED_ITEMTYPES_KEY
);
80 if ($op eq 'delete') {
81 my $itemtype = $input->param('itemtype');
82 my $categorycode = $input->param('categorycode');
83 $debug and warn "deleting $1 $2 $branch";
85 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
86 $sth_Idelete->execute($branch, $categorycode, $itemtype);
88 elsif ($op eq 'delete-branch-cat') {
89 my $categorycode = $input->param('categorycode');
91 if ($categorycode eq "*") {
92 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
93 $sth_delete->execute();
95 my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
96 WHERE categorycode = ?");
97 $sth_delete->execute($categorycode);
99 } elsif ($categorycode eq "*") {
100 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
101 WHERE branchcode = ?");
102 $sth_delete->execute($branch);
104 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
106 AND categorycode = ?");
107 $sth_delete->execute($branch, $categorycode);
109 Koha
::CirculationRules
->set_rule(
111 branchcode
=> $branch,
112 categorycode
=> $categorycode,
114 rule_name
=> 'max_holds',
119 elsif ($op eq 'delete-branch-item') {
120 my $itemtype = $input->param('itemtype');
121 if ($branch eq "*") {
122 if ($itemtype eq "*") {
123 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
124 $sth_delete->execute();
126 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
127 WHERE itemtype = ?");
128 $sth_delete->execute($itemtype);
130 } elsif ($itemtype eq "*") {
131 my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
132 WHERE branchcode = ?");
133 $sth_delete->execute($branch);
135 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
138 $sth_delete->execute($branch, $itemtype);
141 # save the values entered
142 elsif ($op eq 'add') {
143 my $br = $branch; # branch
144 my $bor = $input->param('categorycode'); # borrower category
145 my $itemtype = $input->param('itemtype'); # item type
146 my $fine = $input->param('fine');
147 my $finedays = $input->param('finedays');
148 my $maxsuspensiondays = $input->param('maxsuspensiondays');
149 $maxsuspensiondays = undef if $maxsuspensiondays eq q
||;
150 my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
151 my $firstremind = $input->param('firstremind');
152 my $chargeperiod = $input->param('chargeperiod');
153 my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
154 my $maxissueqty = $input->param('maxissueqty');
155 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
156 my $renewalsallowed = $input->param('renewalsallowed');
157 my $renewalperiod = $input->param('renewalperiod');
158 my $norenewalbefore = $input->param('norenewalbefore');
159 $norenewalbefore = undef if $norenewalbefore =~ /^\s*$/;
160 my $auto_renew = $input->param('auto_renew') eq 'yes' ?
1 : 0;
161 my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
162 $no_auto_renewal_after = undef if $no_auto_renewal_after =~ /^\s*$/;
163 my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || undef;
164 $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 );
165 $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 );
166 my $reservesallowed = $input->param('reservesallowed');
167 my $holds_per_record = $input->param('holds_per_record');
168 my $holds_per_day = $input->param('holds_per_day');
169 $holds_per_day =~ s/\s//g;
170 $holds_per_day = undef if $holds_per_day !~ /^\d+/;
171 my $onshelfholds = $input->param('onshelfholds') || 0;
172 $maxissueqty =~ s/\s//g;
173 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
174 $maxonsiteissueqty =~ s/\s//g;
175 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
176 my $issuelength = $input->param('issuelength');
177 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
178 my $lengthunit = $input->param('lengthunit');
179 my $hardduedate = $input->param('hardduedate') || undef;
180 $hardduedate = eval { dt_from_string
( $input->param('hardduedate') ) } if ( $hardduedate );
181 $hardduedate = output_pref
( { dt
=> $hardduedate, dateonly
=> 1, dateformat
=> 'iso' } ) if ( $hardduedate );
182 my $hardduedatecompare = $input->param('hardduedatecompare');
183 my $rentaldiscount = $input->param('rentaldiscount');
184 my $opacitemholds = $input->param('opacitemholds') || 0;
185 my $article_requests = $input->param('article_requests') || 'no';
186 my $overduefinescap = $input->param('overduefinescap') || undef;
187 my $cap_fine_to_replacement_price = $input->param('cap_fine_to_replacement_price') eq 'on';
188 my $note = $input->param('note');
189 $debug and warn "Adding $br, $bor, $itemtype, $fine, $maxissueqty, $maxonsiteissueqty, $cap_fine_to_replacement_price";
193 categorycode
=> $bor,
194 itemtype
=> $itemtype,
196 finedays
=> $finedays,
197 maxsuspensiondays
=> $maxsuspensiondays,
198 suspension_chargeperiod
=> $suspension_chargeperiod,
199 firstremind
=> $firstremind,
200 chargeperiod
=> $chargeperiod,
201 chargeperiod_charge_at
=> $chargeperiod_charge_at,
202 maxissueqty
=> $maxissueqty,
203 maxonsiteissueqty
=> $maxonsiteissueqty,
204 renewalsallowed
=> $renewalsallowed,
205 renewalperiod
=> $renewalperiod,
206 norenewalbefore
=> $norenewalbefore,
207 auto_renew
=> $auto_renew,
208 no_auto_renewal_after
=> $no_auto_renewal_after,
209 no_auto_renewal_after_hard_limit
=> $no_auto_renewal_after_hard_limit,
210 reservesallowed
=> $reservesallowed,
211 holds_per_record
=> $holds_per_record,
212 holds_per_day
=> $holds_per_day,
213 issuelength
=> $issuelength,
214 lengthunit
=> $lengthunit,
215 hardduedate
=> $hardduedate,
216 hardduedatecompare
=> $hardduedatecompare,
217 rentaldiscount
=> $rentaldiscount,
218 onshelfholds
=> $onshelfholds,
219 opacitemholds
=> $opacitemholds,
220 overduefinescap
=> $overduefinescap,
221 cap_fine_to_replacement_price
=> $cap_fine_to_replacement_price,
222 article_requests
=> $article_requests,
226 my $issuingrule = Koha
::IssuingRules
->find({categorycode
=> $bor, itemtype
=> $itemtype, branchcode
=> $br});
228 $issuingrule->set($params)->store();
230 Koha
::IssuingRule
->new()->set($params)->store();
234 elsif ($op eq "set-branch-defaults") {
235 my $categorycode = $input->param('categorycode');
236 my $maxissueqty = $input->param('maxissueqty');
237 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
238 my $holdallowed = $input->param('holdallowed');
239 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
240 my $returnbranch = $input->param('returnbranch');
241 my $max_holds = $input->param('max_holds');
242 $maxissueqty =~ s/\s//g;
243 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
244 $maxonsiteissueqty =~ s/\s//g;
245 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
246 $holdallowed =~ s/\s//g;
247 $holdallowed = undef if $holdallowed !~ /^\d+/;
248 $max_holds =~ s/\s//g;
249 $max_holds = '' if $max_holds !~ /^\d+/;
251 if ($branch eq "*") {
252 my $sth_search = $dbh->prepare("SELECT count(*) AS total
253 FROM default_circ_rules");
254 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
255 (maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
256 VALUES (?, ?, ?, ?, ?)");
257 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
258 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
260 $sth_search->execute();
261 my $res = $sth_search->fetchrow_hashref();
263 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
265 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
268 my $sth_search = $dbh->prepare("SELECT count(*) AS total
269 FROM default_branch_circ_rules
270 WHERE branchcode = ?");
271 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
272 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch)
273 VALUES (?, ?, ?, ?, ?, ?)");
274 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
275 SET maxissueqty = ?, maxonsiteissueqty = ?, holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
276 WHERE branchcode = ?");
277 $sth_search->execute($branch);
278 my $res = $sth_search->fetchrow_hashref();
280 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
282 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
285 Koha
::CirculationRules
->set_rule(
287 branchcode
=> $branch,
288 categorycode
=> undef,
290 rule_name
=> 'max_holds',
291 rule_value
=> $max_holds,
295 elsif ($op eq "add-branch-cat") {
296 my $categorycode = $input->param('categorycode');
297 my $maxissueqty = $input->param('maxissueqty');
298 my $maxonsiteissueqty = $input->param('maxonsiteissueqty');
299 my $max_holds = $input->param('max_holds');
300 $maxissueqty =~ s/\s//g;
301 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
302 $maxonsiteissueqty =~ s/\s//g;
303 $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
304 $max_holds =~ s/\s//g;
305 $max_holds = undef if $max_holds !~ /^\d+/;
307 if ($branch eq "*") {
308 if ($categorycode eq "*") {
309 #FIXME This block is will probably be never used
310 my $sth_search = $dbh->prepare("SELECT count(*) AS total
311 FROM default_circ_rules");
312 my $sth_insert = $dbh->prepare(q
|
313 INSERT INTO default_circ_rules
314 (maxissueqty
, maxonsiteissueqty
)
317 my $sth_update = $dbh->prepare(q
|
318 UPDATE default_circ_rules
320 maxonsiteissueqty
= ?
,
323 $sth_search->execute();
324 my $res = $sth_search->fetchrow_hashref();
326 $sth_update->execute( $maxissueqty, $maxonsiteissueqty );
328 $sth_insert->execute( $maxissueqty, $maxonsiteissueqty );
331 Koha
::CirculationRules
->set_rule(
334 categorycode
=> undef,
336 rule_name
=> 'max_holds',
337 rule_value
=> $max_holds,
341 my $sth_search = $dbh->prepare("SELECT count(*) AS total
342 FROM default_borrower_circ_rules
343 WHERE categorycode = ?");
344 my $sth_insert = $dbh->prepare(q
|
345 INSERT INTO default_borrower_circ_rules
346 (categorycode
, maxissueqty
, maxonsiteissueqty
)
349 my $sth_update = $dbh->prepare(q
|
350 UPDATE default_borrower_circ_rules
352 maxonsiteissueqty
= ?
,
353 WHERE categorycode
= ?
355 $sth_search->execute($categorycode);
356 my $res = $sth_search->fetchrow_hashref();
358 $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $categorycode );
360 $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty );
363 Koha
::CirculationRules
->set_rule(
366 categorycode
=> $categorycode,
368 rule_name
=> 'max_holds',
369 rule_value
=> $max_holds,
373 } elsif ($categorycode eq "*") {
374 my $sth_search = $dbh->prepare("SELECT count(*) AS total
375 FROM default_branch_circ_rules
376 WHERE branchcode = ?");
377 my $sth_insert = $dbh->prepare(q
|
378 INSERT INTO default_branch_circ_rules
379 (branchcode
, maxissueqty
, maxonsiteissueqty
)
382 my $sth_update = $dbh->prepare(q
|
383 UPDATE default_branch_circ_rules
385 maxonsiteissueqty
= ?
388 $sth_search->execute($branch);
389 my $res = $sth_search->fetchrow_hashref();
391 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
393 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
396 my $sth_search = $dbh->prepare("SELECT count(*) AS total
397 FROM branch_borrower_circ_rules
399 AND categorycode = ?");
400 my $sth_insert = $dbh->prepare(q
|
401 INSERT INTO branch_borrower_circ_rules
402 (branchcode
, categorycode
, maxissueqty
, maxonsiteissueqty
)
405 my $sth_update = $dbh->prepare(q
|
406 UPDATE branch_borrower_circ_rules
408 maxonsiteissueqty
= ?
413 $sth_search->execute($branch, $categorycode);
414 my $res = $sth_search->fetchrow_hashref();
416 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
418 $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
421 Koha
::CirculationRules
->set_rule(
423 branchcode
=> $branch,
424 categorycode
=> $categorycode,
426 rule_name
=> 'max_holds',
427 rule_value
=> $max_holds,
432 elsif ($op eq "add-branch-item") {
433 my $itemtype = $input->param('itemtype');
434 my $holdallowed = $input->param('holdallowed');
435 my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
436 my $returnbranch = $input->param('returnbranch');
438 $holdallowed =~ s/\s//g;
439 $holdallowed = undef if $holdallowed !~ /^\d+/;
441 if ($branch eq "*") {
442 if ($itemtype eq "*") {
443 my $sth_search = $dbh->prepare("SELECT count(*) AS total
444 FROM default_circ_rules");
445 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
446 (holdallowed, hold_fulfillment_policy, returnbranch)
448 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
449 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?");
451 $sth_search->execute();
452 my $res = $sth_search->fetchrow_hashref();
454 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
456 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
459 my $sth_search = $dbh->prepare("SELECT count(*) AS total
460 FROM default_branch_item_rules
461 WHERE itemtype = ?");
462 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
463 (itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
464 VALUES (?, ?, ?, ?)");
465 my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
466 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
467 WHERE itemtype = ?");
468 $sth_search->execute($itemtype);
469 my $res = $sth_search->fetchrow_hashref();
471 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
473 $sth_insert->execute($itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
476 } elsif ($itemtype eq "*") {
477 my $sth_search = $dbh->prepare("SELECT count(*) AS total
478 FROM default_branch_circ_rules
479 WHERE branchcode = ?");
480 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
481 (branchcode, holdallowed, hold_fulfillment_policy, returnbranch)
482 VALUES (?, ?, ?, ?)");
483 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
484 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
485 WHERE branchcode = ?");
486 $sth_search->execute($branch);
487 my $res = $sth_search->fetchrow_hashref();
489 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
491 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
494 my $sth_search = $dbh->prepare("SELECT count(*) AS total
495 FROM branch_item_rules
498 my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
499 (branchcode, itemtype, holdallowed, hold_fulfillment_policy, returnbranch)
500 VALUES (?, ?, ?, ?, ?)");
501 my $sth_update = $dbh->prepare("UPDATE branch_item_rules
502 SET holdallowed = ?, hold_fulfillment_policy = ?, returnbranch = ?
506 $sth_search->execute($branch, $itemtype);
507 my $res = $sth_search->fetchrow_hashref();
509 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
511 $sth_insert->execute($branch, $itemtype, $holdallowed, $hold_fulfillment_policy, $returnbranch);
515 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
517 my $refund = $input->param('refund');
519 if ( $refund eq '*' ) {
520 if ( $branch ne '*' ) {
521 # only do something for $refund eq '*' if branch-specific
523 # Delete it so it picks the default
524 Koha
::RefundLostItemFeeRules
->find({
525 branchcode
=> $branch
531 Koha
::RefundLostItemFeeRules
->find({
532 branchcode
=> $branch
533 }) // Koha
::RefundLostItemFeeRule
->new;
535 branchcode
=> $branch,
541 my $refundLostItemFeeRule = Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $branch });
543 refundLostItemFeeRule
=> $refundLostItemFeeRule,
544 defaultRefundRule
=> Koha
::RefundLostItemFeeRules
->_default_rule
547 my $patron_categories = Koha
::Patron
::Categories
->search({}, { order_by
=> ['description'] });
550 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
552 my $sth2 = $dbh->prepare("
553 SELECT issuingrules.*,
554 itemtypes.description AS humanitemtype,
555 categories.description AS humancategorycode,
556 COALESCE( localization.translation, itemtypes.description ) AS translated_description
559 ON (itemtypes.itemtype = issuingrules.itemtype)
561 ON (categories.categorycode = issuingrules.categorycode)
562 LEFT JOIN localization ON issuingrules.itemtype = localization.code
563 AND localization.entity = 'itemtypes'
564 AND localization.lang = ?
565 WHERE issuingrules.branchcode = ?
567 $sth2->execute($language, $branch);
569 while (my $row = $sth2->fetchrow_hashref) {
570 $row->{'current_branch'} ||= $row->{'branchcode'};
571 $row->{humanitemtype
} ||= $row->{itemtype
};
572 $row->{default_translated_description
} = 1 if $row->{humanitemtype
} eq '*';
573 $row->{'humancategorycode'} ||= $row->{'categorycode'};
574 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
575 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
576 if ($row->{'hardduedate'} && $row->{'hardduedate'} ne '0000-00-00') {
577 my $harddue_dt = eval { dt_from_string
( $row->{'hardduedate'} ) };
578 $row->{'hardduedate'} = eval { output_pref
( { dt
=> $harddue_dt, dateonly
=> 1 } ) } if ( $harddue_dt );
579 $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
580 $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0);
581 $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1);
583 $row->{'hardduedate'} = 0;
585 if ($row->{no_auto_renewal_after_hard_limit
}) {
586 my $dt = eval { dt_from_string
( $row->{no_auto_renewal_after_hard_limit
} ) };
587 $row->{no_auto_renewal_after_hard_limit
} = eval { output_pref
( { dt
=> $dt, dateonly
=> 1 } ) } if $dt;
590 push @row_loop, $row;
593 my @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
596 if ($branch eq "*") {
597 $sth_branch_cat = $dbh->prepare("
598 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
599 FROM default_borrower_circ_rules
600 JOIN categories USING (categorycode)
603 $sth_branch_cat->execute();
605 $sth_branch_cat = $dbh->prepare("
606 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
607 FROM branch_borrower_circ_rules
608 JOIN categories USING (categorycode)
609 WHERE branch_borrower_circ_rules.branchcode = ?
611 $sth_branch_cat->execute($branch);
614 my @branch_cat_rules = ();
615 while (my $row = $sth_branch_cat->fetchrow_hashref) {
616 push @branch_cat_rules, $row;
618 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
620 # note undef maxissueqty so that template can deal with them
621 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
622 $entry->{unlimited_maxissueqty
} = 1 unless defined($entry->{maxissueqty
});
623 $entry->{unlimited_maxonsiteissueqty
} = 1 unless defined($entry->{maxonsiteissueqty
});
624 $entry->{unlimited_max_holds
} = 1 unless defined($entry->{max_holds
});
625 $entry->{unlimited_holds_per_day
} = 1 unless defined($entry->{holds_per_day
});
628 @sorted_row_loop = sort by_category_and_itemtype
@row_loop;
631 if ($branch eq "*") {
632 $sth_branch_item = $dbh->prepare("
633 SELECT default_branch_item_rules.*,
634 COALESCE( localization.translation, itemtypes.description ) AS translated_description
635 FROM default_branch_item_rules
636 JOIN itemtypes USING (itemtype)
637 LEFT JOIN localization ON itemtypes.itemtype = localization.code
638 AND localization.entity = 'itemtypes'
639 AND localization.lang = ?
641 $sth_branch_item->execute($language);
643 $sth_branch_item = $dbh->prepare("
644 SELECT branch_item_rules.*,
645 COALESCE( localization.translation, itemtypes.description ) AS translated_description
646 FROM branch_item_rules
647 JOIN itemtypes USING (itemtype)
648 LEFT JOIN localization ON itemtypes.itemtype = localization.code
649 AND localization.entity = 'itemtypes'
650 AND localization.lang = ?
651 WHERE branch_item_rules.branchcode = ?
653 $sth_branch_item->execute($language, $branch);
656 my @branch_item_rules = ();
657 while (my $row = $sth_branch_item->fetchrow_hashref) {
658 push @branch_item_rules, $row;
660 my @sorted_branch_item_rules = sort { lc $a->{translated_description
} cmp lc $b->{translated_description
} } @branch_item_rules;
662 # note undef holdallowed so that template can deal with them
663 foreach my $entry (@sorted_branch_item_rules) {
664 $entry->{holdallowed_any
} = 1 if ( $entry->{holdallowed
} == 2 );
665 $entry->{holdallowed_same
} = 1 if ( $entry->{holdallowed
} == 1 );
668 $template->param(show_branch_cat_rule_form
=> 1);
669 $template->param(branch_item_rule_loop
=> \
@sorted_branch_item_rules);
670 $template->param(branch_cat_rule_loop
=> \
@sorted_branch_cat_rules);
673 if ($branch eq "*") {
674 $sth_defaults = $dbh->prepare("
676 FROM default_circ_rules
678 $sth_defaults->execute();
680 $sth_defaults = $dbh->prepare("
682 FROM default_branch_circ_rules
685 $sth_defaults->execute($branch);
688 my $defaults = $sth_defaults->fetchrow_hashref;
691 $template->param( default_holdallowed_none
=> 1 ) if ( $defaults->{holdallowed
} == 0 );
692 $template->param( default_holdallowed_same
=> 1 ) if ( $defaults->{holdallowed
} == 1 );
693 $template->param( default_holdallowed_any
=> 1 ) if ( $defaults->{holdallowed
} == 2 );
694 $template->param( default_hold_fulfillment_policy
=> $defaults->{hold_fulfillment_policy
} );
695 $template->param( default_maxissueqty
=> $defaults->{maxissueqty
} );
696 $template->param( default_maxonsiteissueqty
=> $defaults->{maxonsiteissueqty
} );
697 $template->param( default_returnbranch
=> $defaults->{returnbranch
} );
700 $template->param(default_rules
=> ($defaults ?
1 : 0));
703 patron_categories
=> $patron_categories,
704 itemtypeloop
=> $itemtypes,
705 rules
=> \
@sorted_row_loop,
706 humanbranch
=> ($branch ne '*' ?
$branch : ''),
707 current_branch
=> $branch,
708 definedbranch
=> scalar(@sorted_row_loop)>0
710 output_html_with_http_headers
$input, $cookie, $template->output;
714 # sort by patron category, then item type, putting
715 # default entries at the bottom
716 sub by_category_and_itemtype
{
717 unless (by_category
($a, $b)) {
718 return by_itemtype
($a, $b);
724 if ($a->{'default_humancategorycode'}) {
725 return ($b->{'default_humancategorycode'} ?
0 : 1);
726 } elsif ($b->{'default_humancategorycode'}) {
729 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
735 if ($a->{default_translated_description
}) {
736 return ($b->{'default_translated_description'} ?
0 : 1);
737 } elsif ($b->{'default_translated_description'}) {
740 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};