From e6e6b70ea73bac562cf445c931a603f03a1d39f5 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 11 Feb 2022 19:21:22 +0000 Subject: [PATCH] Bug 30085: Reduce scope of holds count / today holds count We retrieve two counts that are only needed if rules for hold limits are defined. The DB counts should only be fetched once the rules are confirmed to exist Further improvement would be possiblke by allowing them to be passed in (or cached?) from CanBookBeReserved as they rely only on patron/biblionumber and not item specific information --- C4/Reserves.pm | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/C4/Reserves.pm b/C4/Reserves.pm index f780fa5aa4..9b39130e0e 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -458,31 +458,28 @@ sub CanItemBeReserved { my $holds_per_record = $rights->{holds_per_record} // 1; my $holds_per_day = $rights->{holds_per_day}; - my $search_params = { - borrowernumber => $patron->borrowernumber, - biblionumber => $item->biblionumber, - }; - $search_params->{found} = undef if $params->{ignore_found_holds}; - - my $holds = Koha::Holds->search($search_params); if ( defined $holds_per_record && $holds_per_record ne '' ){ if ( $holds_per_record == 0 ) { return { status => "noReservesAllowed" }; } - if ( !$params->{ignore_hold_counts} && $holds->count() >= $holds_per_record ) { - return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record }; + if ( !$params->{ignore_hold_counts} ) { + my $search_params = { + borrowernumber => $patron->borrowernumber, + 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 $today_holds = Koha::Holds->search({ - borrowernumber => $patron->borrowernumber, - reservedate => dt_from_string->date - }); - - if (!$params->{ignore_hold_counts} && defined $holds_per_day && $holds_per_day ne '' - && $today_holds->count() >= $holds_per_day ) + if (!$params->{ignore_hold_counts} && defined $holds_per_day && $holds_per_day ne '') { - return { status => 'tooManyReservesToday', limit => $holds_per_day }; + my $today_holds = Koha::Holds->search({ + borrowernumber => $patron->borrowernumber, + reservedate => dt_from_string->date + }); + return { status => 'tooManyReservesToday', limit => $holds_per_day } if $today_holds->count() >= $holds_per_day; } # we retrieve count -- 2.30.2