Lines 386-393
sub TooMany {
Link Here
|
386 |
$branch = _GetCircControlBranch($item_object->unblessed,$borrower); |
386 |
$branch = _GetCircControlBranch($item_object->unblessed,$borrower); |
387 |
my $type = $item_object->effective_itemtype; |
387 |
my $type = $item_object->effective_itemtype; |
388 |
|
388 |
|
|
|
389 |
my ($type_object, $parent_type, $parent_maxissueqty_rule); |
390 |
$type_object = Koha::ItemTypes->find( $type ); |
391 |
$parent_type = $type_object->parent_type if $type_object; |
392 |
my $child_types = Koha::ItemTypes->search({ parent_type => $type }); |
393 |
# Find any children if we are a parent_type; |
394 |
|
389 |
# given branch, patron category, and item type, determine |
395 |
# given branch, patron category, and item type, determine |
390 |
# applicable issuing rule |
396 |
# applicable issuing rule |
|
|
397 |
|
398 |
$parent_maxissueqty_rule = Koha::CirculationRules->get_effective_rule( |
399 |
{ |
400 |
categorycode => $cat_borrower, |
401 |
itemtype => $parent_type, |
402 |
branchcode => $branch, |
403 |
rule_name => 'maxissueqty', |
404 |
} |
405 |
) if $parent_type; |
406 |
# If the parent rule is for default type we discount it |
407 |
$parent_maxissueqty_rule = undef if $parent_maxissueqty_rule && !defined $parent_maxissueqty_rule->itemtype; |
408 |
|
391 |
my $maxissueqty_rule = Koha::CirculationRules->get_effective_rule( |
409 |
my $maxissueqty_rule = Koha::CirculationRules->get_effective_rule( |
392 |
{ |
410 |
{ |
393 |
categorycode => $cat_borrower, |
411 |
categorycode => $cat_borrower, |
Lines 396-401
sub TooMany {
Link Here
|
396 |
rule_name => 'maxissueqty', |
414 |
rule_name => 'maxissueqty', |
397 |
} |
415 |
} |
398 |
); |
416 |
); |
|
|
417 |
|
418 |
|
399 |
my $maxonsiteissueqty_rule = Koha::CirculationRules->get_effective_rule( |
419 |
my $maxonsiteissueqty_rule = Koha::CirculationRules->get_effective_rule( |
400 |
{ |
420 |
{ |
401 |
categorycode => $cat_borrower, |
421 |
categorycode => $cat_borrower, |
Lines 409-418
sub TooMany {
Link Here
|
409 |
# if a rule is found and has a loan limit set, count |
429 |
# if a rule is found and has a loan limit set, count |
410 |
# how many loans the patron already has that meet that |
430 |
# how many loans the patron already has that meet that |
411 |
# rule |
431 |
# rule |
412 |
if (defined($maxissueqty_rule) and $maxissueqty_rule->rule_value ne '') { |
432 |
if (defined($maxissueqty_rule) and defined($maxissueqty_rule->rule_value)) { |
|
|
433 |
|
413 |
my @bind_params; |
434 |
my @bind_params; |
414 |
my $count_query = q| |
435 |
my $count_query = ""; |
415 |
SELECT COUNT(*) AS total, COALESCE(SUM(onsite_checkout), 0) AS onsite_checkouts |
436 |
|
|
|
437 |
if (C4::Context->preference('item-level_itypes')) { |
438 |
$count_query .= q|SELECT COALESCE( SUM( IF(items.itype = '| .$type . q|',1,0) ), 0) as type_total, COUNT(*) AS total, COALESCE(SUM(onsite_checkout), 0) AS onsite_checkouts|; |
439 |
} else{ |
440 |
$count_query .= q|SELECT COALESCE(SUM( IF(biblioitems.itemtype = '| .$type . q|',1,0) ), 0) as type_total, COUNT(*) AS total, COALESCE(SUM(onsite_checkout), 0) AS onsite_checkouts|; |
441 |
} |
442 |
|
443 |
$count_query .= q| |
416 |
FROM issues |
444 |
FROM issues |
417 |
JOIN items USING (itemnumber) |
445 |
JOIN items USING (itemnumber) |
418 |
|; |
446 |
|; |
Lines 422-458
sub TooMany {
Link Here
|
422 |
# matching rule has the default item type, so count only |
450 |
# matching rule has the default item type, so count only |
423 |
# those existing loans that don't fall under a more |
451 |
# those existing loans that don't fall under a more |
424 |
# specific rule |
452 |
# specific rule |
|
|
453 |
my $issuing_itemtypes_query = q{ |
454 |
SELECT itemtype FROM circulation_rules |
455 |
WHERE branchcode = ? |
456 |
AND (categorycode = ? OR categorycode = ?) |
457 |
AND itemtype IS NOT NULL |
458 |
AND rule_name = 'maxissueqty' |
459 |
}; |
425 |
if (C4::Context->preference('item-level_itypes')) { |
460 |
if (C4::Context->preference('item-level_itypes')) { |
426 |
$count_query .= " WHERE items.itype NOT IN ( |
461 |
$count_query .= " WHERE items.itype NOT IN ( $issuing_itemtypes_query )"; |
427 |
SELECT itemtype FROM circulation_rules |
|
|
428 |
WHERE branchcode = ? |
429 |
AND (categorycode = ? OR categorycode = ?) |
430 |
AND itemtype IS NOT NULL |
431 |
AND rule_name = 'maxissueqty' |
432 |
) "; |
433 |
} else { |
462 |
} else { |
434 |
$count_query .= " JOIN biblioitems USING (biblionumber) |
463 |
$count_query .= " WHERE biblioitems.itemtype NOT IN ( $issuing_itemtypes_query )"; |
435 |
WHERE biblioitems.itemtype NOT IN ( |
|
|
436 |
SELECT itemtype FROM circulation_rules |
437 |
WHERE branchcode = ? |
438 |
AND (categorycode = ? OR categorycode = ?) |
439 |
AND itemtype IS NOT NULL |
440 |
AND rule_name = 'maxissueqty' |
441 |
) "; |
442 |
} |
464 |
} |
443 |
push @bind_params, $maxissueqty_rule->branchcode; |
465 |
push @bind_params, $maxissueqty_rule->branchcode; |
444 |
push @bind_params, $maxissueqty_rule->categorycode; |
466 |
push @bind_params, $maxissueqty_rule->categorycode; |
445 |
push @bind_params, $cat_borrower; |
467 |
push @bind_params, $cat_borrower; |
446 |
} else { |
468 |
} else { |
447 |
# rule has specific item type, so count loans of that |
469 |
my @types; |
448 |
# specific item type |
470 |
if ( $parent_maxissueqty_rule ) { |
|
|
471 |
# if we have a parent item type then we count loans of the |
472 |
# specific item type or its siblings or parent |
473 |
my $children = Koha::ItemTypes->search({ parent_type => $parent_type }); |
474 |
@types = $children->get_column('itemtype'); |
475 |
push @types, $parent_type; |
476 |
} elsif ( $child_types ) { |
477 |
# If we are a parent type, we need to count all child types and our own type |
478 |
@types = $child_types->get_column('itemtype'); |
479 |
push @types, $type; # And don't forget to count our own types |
480 |
} else { push @types, $type; } # Otherwise only count the specific itemtype |
481 |
my $types_param = ( '?,' ) x @types; |
482 |
$types_param =~ s/,$//; |
449 |
if (C4::Context->preference('item-level_itypes')) { |
483 |
if (C4::Context->preference('item-level_itypes')) { |
450 |
$count_query .= " WHERE items.itype = ? "; |
484 |
$count_query .= " WHERE items.itype IN (" . $types_param . ")"; |
451 |
} else { |
485 |
} else { |
452 |
$count_query .= " JOIN biblioitems USING (biblionumber) |
486 |
$count_query .= " JOIN biblioitems USING (biblionumber) |
453 |
WHERE biblioitems.itemtype= ? "; |
487 |
WHERE biblioitems.itemtype IN (" . $types_param . ")"; |
454 |
} |
488 |
} |
455 |
push @bind_params, $type; |
489 |
push @bind_params, @types; |
456 |
} |
490 |
} |
457 |
|
491 |
|
458 |
$count_query .= " AND borrowernumber = ? "; |
492 |
$count_query .= " AND borrowernumber = ? "; |
Lines 470-507
sub TooMany {
Link Here
|
470 |
} |
504 |
} |
471 |
} |
505 |
} |
472 |
|
506 |
|
473 |
my ( $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $count_query, {}, @bind_params ); |
507 |
my ( $checkout_count_type, $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $count_query, {}, @bind_params ); |
474 |
|
508 |
|
475 |
my $max_checkouts_allowed = $maxissueqty_rule ? $maxissueqty_rule->rule_value : undef; |
|
|
476 |
my $max_onsite_checkouts_allowed = $maxonsiteissueqty_rule ? $maxonsiteissueqty_rule->rule_value : undef; |
509 |
my $max_onsite_checkouts_allowed = $maxonsiteissueqty_rule ? $maxonsiteissueqty_rule->rule_value : undef; |
477 |
|
510 |
|
478 |
if ( $onsite_checkout and $max_onsite_checkouts_allowed ne '' ) { |
511 |
# If parent rules exists |
479 |
if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed ) { |
512 |
if ( defined($parent_maxissueqty_rule) and defined($parent_maxissueqty_rule->rule_value) ){ |
480 |
return { |
513 |
my $max_checkouts_allowed = $parent_maxissueqty_rule->rule_value; |
481 |
reason => 'TOO_MANY_ONSITE_CHECKOUTS', |
514 |
|
482 |
count => $onsite_checkout_count, |
515 |
my $qty_over = _check_max_qty({ |
483 |
max_allowed => $max_onsite_checkouts_allowed, |
516 |
checkout_count => $checkout_count, |
484 |
} |
517 |
onsite_checkout_count => $onsite_checkout_count, |
485 |
} |
518 |
onsite_checkout => $onsite_checkout, |
486 |
} |
519 |
max_checkouts_allowed => $max_checkouts_allowed, |
487 |
if ( C4::Context->preference('ConsiderOnSiteCheckoutsAsNormalCheckouts') ) { |
520 |
max_onsite_checkouts_allowed => $max_onsite_checkouts_allowed, |
488 |
my $delta = $switch_onsite_checkout ? 1 : 0; |
521 |
switch_onsite_checkout => $switch_onsite_checkout |
489 |
if ( $checkout_count >= $max_checkouts_allowed + $delta ) { |
522 |
}); |
490 |
return { |
523 |
return $qty_over if defined $qty_over; |
491 |
reason => 'TOO_MANY_CHECKOUTS', |
524 |
|
492 |
count => $checkout_count, |
525 |
|
493 |
max_allowed => $max_checkouts_allowed, |
526 |
# If the parent rule is less than or equal to the child, we only need check the parent |
494 |
}; |
527 |
if( $maxissueqty_rule->rule_value < $parent_maxissueqty_rule->rule_value && defined($maxissueqty_rule->itemtype) ) { |
495 |
} |
528 |
my $max_checkouts_allowed = $maxissueqty_rule->rule_value; |
496 |
} elsif ( not $onsite_checkout ) { |
529 |
my $qty_over = _check_max_qty({ |
497 |
if ( $checkout_count - $onsite_checkout_count >= $max_checkouts_allowed ) { |
530 |
checkout_count => $checkout_count_type, |
498 |
return { |
531 |
onsite_checkout_count => $onsite_checkout_count, |
499 |
reason => 'TOO_MANY_CHECKOUTS', |
532 |
onsite_checkout => $onsite_checkout, |
500 |
count => $checkout_count - $onsite_checkout_count, |
533 |
max_checkouts_allowed => $max_checkouts_allowed, |
501 |
max_allowed => $max_checkouts_allowed, |
534 |
max_onsite_checkouts_allowed => $max_onsite_checkouts_allowed, |
502 |
}; |
535 |
switch_onsite_checkout => $switch_onsite_checkout |
503 |
} |
536 |
}); |
|
|
537 |
return $qty_over if defined $qty_over; |
538 |
} |
539 |
|
540 |
} else { |
541 |
my $max_checkouts_allowed = $maxissueqty_rule->rule_value; |
542 |
my $qty_over = _check_max_qty({ |
543 |
checkout_count => $checkout_count, |
544 |
onsite_checkout_count => $onsite_checkout_count, |
545 |
onsite_checkout => $onsite_checkout, |
546 |
max_checkouts_allowed => $max_checkouts_allowed, |
547 |
max_onsite_checkouts_allowed => $max_onsite_checkouts_allowed, |
548 |
switch_onsite_checkout => $switch_onsite_checkout |
549 |
}); |
550 |
return $qty_over if defined $qty_over; |
504 |
} |
551 |
} |
|
|
552 |
|
553 |
|
505 |
} |
554 |
} |
506 |
|
555 |
|
507 |
# Now count total loans against the limit for the branch |
556 |
# Now count total loans against the limit for the branch |
Lines 527-561
sub TooMany {
Link Here
|
527 |
} |
576 |
} |
528 |
my ( $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $branch_count_query, {}, @bind_params ); |
577 |
my ( $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $branch_count_query, {}, @bind_params ); |
529 |
my $max_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxissueqty}; |
578 |
my $max_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxissueqty}; |
530 |
my $max_onsite_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxonsiteissueqty}; |
579 |
my $max_onsite_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxonsiteissueqty} || undef; |
531 |
|
580 |
|
532 |
if ( $onsite_checkout and $max_onsite_checkouts_allowed ne '' ) { |
581 |
my $qty_over = _check_max_qty({ |
533 |
if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed ) { |
582 |
checkout_count => $checkout_count, |
534 |
return { |
583 |
onsite_checkout_count => $onsite_checkout_count, |
535 |
reason => 'TOO_MANY_ONSITE_CHECKOUTS', |
584 |
onsite_checkout => $onsite_checkout, |
536 |
count => $onsite_checkout_count, |
585 |
max_checkouts_allowed => $max_checkouts_allowed, |
537 |
max_allowed => $max_onsite_checkouts_allowed, |
586 |
max_onsite_checkouts_allowed => $max_onsite_checkouts_allowed, |
538 |
} |
587 |
switch_onsite_checkout => $switch_onsite_checkout |
539 |
} |
588 |
}); |
540 |
} |
589 |
return $qty_over if defined $qty_over; |
541 |
if ( C4::Context->preference('ConsiderOnSiteCheckoutsAsNormalCheckouts') ) { |
590 |
|
542 |
my $delta = $switch_onsite_checkout ? 1 : 0; |
|
|
543 |
if ( $checkout_count >= $max_checkouts_allowed + $delta ) { |
544 |
return { |
545 |
reason => 'TOO_MANY_CHECKOUTS', |
546 |
count => $checkout_count, |
547 |
max_allowed => $max_checkouts_allowed, |
548 |
}; |
549 |
} |
550 |
} elsif ( not $onsite_checkout ) { |
551 |
if ( $checkout_count - $onsite_checkout_count >= $max_checkouts_allowed ) { |
552 |
return { |
553 |
reason => 'TOO_MANY_CHECKOUTS', |
554 |
count => $checkout_count - $onsite_checkout_count, |
555 |
max_allowed => $max_checkouts_allowed, |
556 |
}; |
557 |
} |
558 |
} |
559 |
} |
591 |
} |
560 |
|
592 |
|
561 |
if ( not defined( $maxissueqty_rule ) and not defined($branch_borrower_circ_rule->{patron_maxissueqty}) ) { |
593 |
if ( not defined( $maxissueqty_rule ) and not defined($branch_borrower_circ_rule->{patron_maxissueqty}) ) { |
Lines 566-571
sub TooMany {
Link Here
|
566 |
return; |
598 |
return; |
567 |
} |
599 |
} |
568 |
|
600 |
|
|
|
601 |
sub _check_max_qty { |
602 |
my $params = shift; |
603 |
my $checkout_count = $params->{checkout_count}; |
604 |
my $onsite_checkout_count = $params->{onsite_checkout_count}; |
605 |
my $onsite_checkout = $params->{onsite_checkout}; |
606 |
my $max_checkouts_allowed = $params->{max_checkouts_allowed}; |
607 |
my $max_onsite_checkouts_allowed = $params->{max_onsite_checkouts_allowed}; |
608 |
my $switch_onsite_checkout = $params->{switch_onsite_checkout}; |
609 |
|
610 |
if ( $onsite_checkout and defined $max_onsite_checkouts_allowed ) { |
611 |
if( $max_onsite_checkouts_allowed eq '' ){ return;} |
612 |
if ( $onsite_checkout_count >= $max_onsite_checkouts_allowed ) { |
613 |
return { |
614 |
reason => 'TOO_MANY_ONSITE_CHECKOUTS', |
615 |
count => $onsite_checkout_count, |
616 |
max_allowed => $max_onsite_checkouts_allowed, |
617 |
} |
618 |
} |
619 |
} |
620 |
if ( C4::Context->preference('ConsiderOnSiteCheckoutsAsNormalCheckouts') ) { |
621 |
if( $max_checkouts_allowed eq '' ){ return;} |
622 |
my $delta = $switch_onsite_checkout ? 1 : 0; |
623 |
if ( $checkout_count >= $max_checkouts_allowed + $delta ) { |
624 |
return { |
625 |
reason => 'TOO_MANY_CHECKOUTS', |
626 |
count => $checkout_count, |
627 |
max_allowed => $max_checkouts_allowed, |
628 |
}; |
629 |
} |
630 |
} elsif ( not $onsite_checkout ) { |
631 |
if( $max_checkouts_allowed eq '' ){ return;} |
632 |
if ( $checkout_count - $onsite_checkout_count >= $max_checkouts_allowed ) { |
633 |
return { |
634 |
reason => 'TOO_MANY_CHECKOUTS', |
635 |
count => $checkout_count - $onsite_checkout_count, |
636 |
max_allowed => $max_checkouts_allowed, |
637 |
}; |
638 |
} |
639 |
} |
640 |
|
641 |
return; |
642 |
} |
643 |
|
569 |
=head2 CanBookBeIssued |
644 |
=head2 CanBookBeIssued |
570 |
|
645 |
|
571 |
( $issuingimpossible, $needsconfirmation, [ $alerts ] ) = CanBookBeIssued( $patron, |
646 |
( $issuingimpossible, $needsconfirmation, [ $alerts ] ) = CanBookBeIssued( $patron, |