From d875e5b9b6910e45137afbdb61c56fa5e60d9751 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Fri, 4 Jun 2021 16:08:45 +1200 Subject: [PATCH] Bug 15516: (follow-up) Count group holds always Signed-off-by: Michal Denar --- C4/Reserves.pm | 8 ++++---- Koha/Holds.pm | 25 ++++++++++++++++++------- opac/opac-reserve.pl | 2 +- reserve/request.pl | 2 +- t/db_dependent/Koha/Holds.t | 8 ++++---- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 593b9ddd7d..de0c1a67cd 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -522,18 +522,18 @@ sub CanItemBeReserved { biblionumber => $item->biblionumber, }; $search_params->{found} = undef if $params->{ignore_found_holds}; - my $holds = Koha::Holds->search($search_params); - return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record } if $holds->count() >= $holds_per_record; + my $holds_count = Koha::Holds->count_holds($search_params); + return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record } if $holds_count >= $holds_per_record; } } if (!$params->{ignore_hold_counts} && defined $holds_per_day && $holds_per_day ne '') { - my $today_holds = Koha::Holds->search({ + my $today_holds_count = Koha::Holds->count_holds({ borrowernumber => $patron->borrowernumber, reservedate => dt_from_string->date }); - return { status => 'tooManyReservesToday', limit => $holds_per_day } if $today_holds->count() >= $holds_per_day; + return { status => 'tooManyReservesToday', limit => $holds_per_day } if $today_holds_count >= $holds_per_day; } # we check if it's ok or not diff --git a/Koha/Holds.pm b/Koha/Holds.pm index 8ff2597c74..3924b2a866 100644 --- a/Koha/Holds.pm +++ b/Koha/Holds.pm @@ -178,18 +178,29 @@ sub filter_out_has_cancellation_requests { { join => 'cancellation_requests' } ); } -=head3 count_grouped +=head3 count_holds - $holds->count_grouped(); + $holds->count_holds( $search_params ); -Return number of hold, where hold group is counted as one hold +This overwrites the default count(). + +Return the number of holds, where a hold group is counted as one hold. =cut -sub count_grouped { - my ($self) = @_; - my $holds_without_group_count = $self->search({ hold_group_id => undef })->count(); - my $hold_groups_count = $self->search({ hold_group_id => { '!=' => undef }}, { group_by => 'me.hold_group_id' })->count(); +sub count_holds { + my ( $self, $search_params ) = @_; + + $search_params = { + hold_group_id => undef, + }; + my $holds_without_group_count = $self->search( $search_params )->count(); + + $search_params = { + hold_group_id => { '!=', undef }, + }; + my $hold_groups_count = $self->search( $search_params, { group_by => 'me.hold_group_id' })->count(); + return $holds_without_group_count + $hold_groups_count; } diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index 834b240aa2..68f8026873 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -193,7 +193,7 @@ foreach my $biblioNumber (@biblionumbers) { if ( $query->param('place_reserve') ) { my $reserve_cnt = 0; if ($maxreserves) { - $reserve_cnt = $patron->holds->count_grouped; + $reserve_cnt = $patron->holds->count_holds; } # List is composed of alternating biblio/item/branch diff --git a/reserve/request.pl b/reserve/request.pl index de39d144d2..b9f743c127 100755 --- a/reserve/request.pl +++ b/reserve/request.pl @@ -182,7 +182,7 @@ if ($borrowernumber_hold && !$action) { # we check the reserves of the user, and if they can reserve a document # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ... - my $reserves_count = $patron->holds->count; + my $reserves_count = $patron->holds->count_holds; my $new_reserves_count = scalar( @biblionumbers ); diff --git a/t/db_dependent/Koha/Holds.t b/t/db_dependent/Koha/Holds.t index a62f5f0cd0..4706ba22f6 100755 --- a/t/db_dependent/Koha/Holds.t +++ b/t/db_dependent/Koha/Holds.t @@ -641,7 +641,7 @@ subtest 'set_waiting+patron_expiration_date' => sub { }; }; -subtest 'count_grouped' => sub { +subtest 'count_holds' => sub { plan tests => 3; $schema->storage->txn_begin; @@ -658,7 +658,7 @@ subtest 'count_grouped' => sub { }, }); - is($patron->holds->count_grouped, 1, 'Test patron has 1 hold.'); + is($patron->holds->count_holds, 1, 'Test patron has 1 hold.'); my $hold_group = $builder->build_object({ class => 'Koha::HoldGroups', @@ -679,7 +679,7 @@ subtest 'count_grouped' => sub { } }); - is($patron->holds->count_grouped, 2, 'Test patron has 2 holds.'); + is($patron->holds->count_holds, 2, 'Test patron has 2 holds.'); my $hold_group2 = $builder->build_object({ class => 'Koha::HoldGroups', @@ -707,7 +707,7 @@ subtest 'count_grouped' => sub { } }); - is($patron->holds->count_grouped, 3, 'Test patron has 3 holds.'); + is($patron->holds->count_holds, 3, 'Test patron has 3 holds.'); $schema->storage->txn_rollback; }; -- 2.11.0