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
::Patron
::Categories
;
38 my $dbh = C4
::Context
->dbh;
41 # $flagsrequired->{circulation}=1;
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user
({template_name
=> "admin/smart-rules.tt",
47 flagsrequired
=> {parameters
=> 'manage_circ_rules'},
51 my $type=$input->param('type');
53 my $branch = $input->param('branch');
55 if ( C4
::Context
->preference('DefaultToLoggedInLibraryCircRules') ) {
56 $branch = Koha
::Libraries
->search->count() == 1 ?
undef : C4
::Context
::mybranch
();
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');
78 if ($categorycode eq "*") {
79 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
80 $sth_delete->execute();
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);
91 my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
93 AND categorycode = ?");
94 $sth_delete->execute($branch, $categorycode);
97 elsif ($op eq 'delete-branch-item') {
98 my $itemtype = $input->param('itemtype');
100 if ($itemtype eq "*") {
101 my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
102 $sth_delete->execute();
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);
113 my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
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";
167 categorycode
=> $bor,
168 itemtype
=> $itemtype,
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});
200 $issuingrule->set($params)->store();
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();
232 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
234 $sth_insert->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
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();
249 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
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
)
276 my $sth_update = $dbh->prepare(q
|
277 UPDATE default_circ_rules
279 maxonsiteissueqty
= ?
,
283 $sth_search->execute();
284 my $res = $sth_search->fetchrow_hashref();
286 $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $max_holds );
288 $sth_insert->execute( $maxissueqty, $maxonsiteissueqty, $max_holds );
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
)
299 my $sth_update = $dbh->prepare(q
|
300 UPDATE default_borrower_circ_rules
302 maxonsiteissueqty
= ?
,
304 WHERE categorycode
= ?
306 $sth_search->execute($categorycode);
307 my $res = $sth_search->fetchrow_hashref();
309 $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $max_holds, $categorycode );
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
)
323 my $sth_update = $dbh->prepare(q
|
324 UPDATE default_branch_circ_rules
326 maxonsiteissueqty
= ?
329 $sth_search->execute($branch);
330 my $res = $sth_search->fetchrow_hashref();
332 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch);
334 $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty);
337 my $sth_search = $dbh->prepare("SELECT count(*) AS total
338 FROM branch_borrower_circ_rules
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
349 maxonsiteissueqty
= ?
355 $sth_search->execute($branch, $categorycode);
356 my $res = $sth_search->fetchrow_hashref();
358 $sth_update->execute($maxissueqty, $maxonsiteissueqty, $max_holds, $branch, $categorycode);
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)
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();
386 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
388 $sth_insert->execute($holdallowed, $hold_fulfillment_policy, $returnbranch);
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();
403 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $itemtype);
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();
421 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch);
423 $sth_insert->execute($branch, $holdallowed, $hold_fulfillment_policy, $returnbranch);
426 my $sth_search = $dbh->prepare("SELECT count(*) AS total
427 FROM branch_item_rules
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 = ?
438 $sth_search->execute($branch, $itemtype);
439 my $res = $sth_search->fetchrow_hashref();
441 $sth_update->execute($holdallowed, $hold_fulfillment_policy, $returnbranch, $branch, $itemtype);
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
455 # Delete it so it picks the default
456 Koha
::RefundLostItemFeeRules
->find({
457 branchcode
=> $branch
463 Koha
::RefundLostItemFeeRules
->find({
464 branchcode
=> $branch
465 }) // Koha
::RefundLostItemFeeRule
->new;
467 branchcode
=> $branch,
473 my $refundLostItemFeeRule = Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $branch });
475 refundLostItemFeeRule
=> $refundLostItemFeeRule,
476 defaultRefundRule
=> Koha
::RefundLostItemFeeRules
->_default_rule
479 my $patron_categories = Koha
::Patron
::Categories
->search({}, { order_by
=> ['description'] });
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
491 ON (itemtypes.itemtype = issuingrules.itemtype)
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);
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;
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();
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;
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);
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);
604 if ($branch eq "*") {
605 $sth_defaults = $dbh->prepare("
607 FROM default_circ_rules
609 $sth_defaults->execute();
611 $sth_defaults = $dbh->prepare("
613 FROM default_branch_circ_rules
616 $sth_defaults->execute($branch);
619 my $defaults = $sth_defaults->fetchrow_hashref;
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));
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;
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);
655 if ($a->{'default_humancategorycode'}) {
656 return ($b->{'default_humancategorycode'} ?
0 : 1);
657 } elsif ($b->{'default_humancategorycode'}) {
660 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
666 if ($a->{default_translated_description
}) {
667 return ($b->{'default_translated_description'} ?
0 : 1);
668 } elsif ($b->{'default_translated_description'}) {
671 return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};