View | Details | Raw Unified | Return to bug 26132
Collapse All | Expand All

(-)a/C4/Circulation.pm (-68 / +47 lines)
Lines 432-512 sub TooMany { Link Here
432
    # rule
432
    # rule
433
    if (defined($maxissueqty_rule) and $maxissueqty_rule->rule_value ne "") {
433
    if (defined($maxissueqty_rule) and $maxissueqty_rule->rule_value ne "") {
434
434
435
        my @bind_params;
435
        my $patron = Koha::Patrons->find($borrower->{borrowernumber});
436
        my $count_query = "";
436
        my $checkouts = $patron->checkouts->search( {}, { prefetch => 'item' } );
437
437
        if ( $maxissueqty_rule->branchcode ) {
438
        if (C4::Context->preference('item-level_itypes')) {
438
            if ( C4::Context->preference('CircControl') eq 'PickupLibrary' ) {
439
            $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
                $checkouts = $checkouts->search({ 'me.branchcode' => $maxissueqty_rule->branchcode });
440
        } else{
440
            } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
441
            $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
                ; # if branch is the patron's home branch, then count all loans by patron
442
        }
443
444
        $count_query .= q|
445
            FROM issues
446
            JOIN items USING (itemnumber)
447
        |;
448
449
        my $rule_itemtype = $maxissueqty_rule->itemtype;
450
        unless ($rule_itemtype) {
451
            # matching rule has the default item type, so count only
452
            # those existing loans that don't fall under a more
453
            # specific rule
454
            my $issuing_itemtypes_query  = q{
455
                SELECT itemtype FROM circulation_rules
456
                WHERE branchcode = ?
457
                AND   (categorycode = ? OR categorycode = ?)
458
                AND   itemtype IS NOT NULL
459
                AND   rule_name = 'maxissueqty'
460
            };
461
            if (C4::Context->preference('item-level_itypes')) {
462
                $count_query .= " WHERE items.itype NOT IN ( $issuing_itemtypes_query )";
463
            } else {
442
            } else {
464
                $count_query .= " JOIN biblioitems USING (biblionumber)
443
                $checkouts = $checkouts->search({ 'item.homebranch' => $maxissueqty_rule->branchcode });
465
                                  WHERE biblioitems.itemtype NOT IN ( $issuing_itemtypes_query )";
466
            }
444
            }
467
            push @bind_params, $maxissueqty_rule->branchcode;
468
            push @bind_params, $maxissueqty_rule->categorycode;
469
            push @bind_params, $cat_borrower;
470
        } else {
471
            my @types;
472
            if ( $parent_maxissueqty_rule ) {
473
            # if we have a parent item type then we count loans of the
474
            # specific item type or its siblings or parent
475
                my $children = Koha::ItemTypes->search({ parent_type => $parent_type });
476
                @types = $children->get_column('itemtype');
477
                push @types, $parent_type;
478
            } elsif ( $child_types ) {
479
            # If we are a parent type, we need to count all child types and our own type
480
                @types = $child_types->get_column('itemtype');
481
                push @types, $type; # And don't forget to count our own types
482
            } else { push @types, $type; } # Otherwise only count the specific itemtype
483
            my $types_param = ( '?,' ) x @types;
484
            $types_param =~ s/,$//;
485
            if (C4::Context->preference('item-level_itypes')) {
486
                $count_query .= " WHERE items.itype IN (" . $types_param . ")";
487
            } else { 
488
                $count_query .= " JOIN  biblioitems USING (biblionumber) 
489
                                  WHERE biblioitems.itemtype IN (" . $types_param . ")";
490
            }
491
            push @bind_params, @types;
492
        }
445
        }
446
        my $sum_checkouts;
447
        my $rule_itemtype = $maxissueqty_rule->itemtype;
448
        while ( my $c = $checkouts->next ) {
449
            my $itemtype = $c->item->effective_itemtype;
450
            my @types;
451
            unless ( $rule_itemtype ) {
452
                # matching rule has the default item type, so count only
453
                # those existing loans that don't fall under a more
454
                # specific rule
455
                @types = Koha::CirculationRules->search(
456
                    {
457
                        branchcode => $maxissueqty_rule->branchcode,
458
                        categorycode => [ $maxissueqty_rule->categorycode, $cat_borrower ],
459
                        itemtype  => { '!=' => undef },
460
                        rule_name => 'maxissueqty'
461
                    }
462
                )->get_column('itemtype');
493
463
494
        $count_query .= " AND borrowernumber = ? ";
464
                next if grep {$_ eq $itemtype} @types;
495
        push @bind_params, $borrower->{'borrowernumber'};
496
        my $rule_branch = $maxissueqty_rule->branchcode;
497
        if ($rule_branch) {
498
            if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
499
                $count_query .= " AND issues.branchcode = ? ";
500
                push @bind_params, $rule_branch;
501
            } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
502
                ; # if branch is the patron's home branch, then count all loans by patron
503
            } else {
465
            } else {
504
                $count_query .= " AND items.homebranch = ? ";
466
                my @types;
505
                push @bind_params, $rule_branch;
467
                if ( $parent_maxissueqty_rule ) {
468
                # if we have a parent item type then we count loans of the
469
                # specific item type or its siblings or parent
470
                    my $children = Koha::ItemTypes->search({ parent_type => $parent_type });
471
                    @types = $children->get_column('itemtype');
472
                    push @types, $parent_type;
473
                } elsif ( $child_types ) {
474
                # If we are a parent type, we need to count all child types and our own type
475
                    @types = $child_types->get_column('itemtype');
476
                    push @types, $type; # And don't forget to count our own types
477
                } else { push @types, $type; } # Otherwise only count the specific itemtype
478
479
                next unless grep {$_ eq $itemtype} @types;
506
            }
480
            }
481
            $sum_checkouts->{total}++;
482
            $sum_checkouts->{onsite_checkouts}++ if $c->onsite_checkout;
483
            $sum_checkouts->{itemtype}->{$itemtype}++;
507
        }
484
        }
508
485
509
        my ( $checkout_count_type, $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $count_query, {}, @bind_params );
486
        my $checkout_count_type = $sum_checkouts->{itemtype}->{$type} || 0;
487
        my $checkout_count = $sum_checkouts->{total} || 0;
488
        my $onsite_checkout_count = $sum_checkouts->{onsite_checkouts} || 0;
510
489
511
        my $checkout_rules = {
490
        my $checkout_rules = {
512
            checkout_count               => $checkout_count,
491
            checkout_count               => $checkout_count,
Lines 611-617 sub _check_max_qty { Link Here
611
        }
590
        }
612
    } elsif ( not $onsite_checkout ) {
591
    } elsif ( not $onsite_checkout ) {
613
        if( $max_checkouts_allowed eq '' ){ return;}
592
        if( $max_checkouts_allowed eq '' ){ return;}
614
        if ( $checkout_count - $onsite_checkout_count >= $max_checkouts_allowed )  {
593
        if ( $checkout_count - $onsite_checkout_count >= $max_checkouts_allowed ) {
615
            return {
594
            return {
616
                reason => 'TOO_MANY_CHECKOUTS',
595
                reason => 'TOO_MANY_CHECKOUTS',
617
                count => $checkout_count - $onsite_checkout_count,
596
                count => $checkout_count - $onsite_checkout_count,
(-)a/t/db_dependent/Circulation/TooMany.t (-2 / +1 lines)
Lines 919-925 subtest 'itemtype group tests' => sub { Link Here
919
        undef, undef, undef );
919
        undef, undef, undef );
920
    like( $issue->issue_id, qr|^\d+$|, 'the issue should have been inserted' );
920
    like( $issue->issue_id, qr|^\d+$|, 'the issue should have been inserted' );
921
921
922
    #patron has 1 checkoout of childitype1 and 2 of childitype2
922
    #patron has 1 checkout of childitype1 and 2 of childitype2
923
923
924
    is(
924
    is(
925
        C4::Circulation::TooMany( $patron, $item_2 ),
925
        C4::Circulation::TooMany( $patron, $item_2 ),
926
- 

Return to bug 26132