From 11e7f9c037f8ee57976030b633032501e196826f Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 5 Sep 2024 12:47:53 +0200
Subject: [PATCH] Bug 17729: Replace IsItemOnHoldAndFound

This subroutine can easily be replaced with
$item->holds->filter_by_found->count \o/

Test plan:
Confirm that the old sub and $item->holds->filter_by_found->count
produce the same query

Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
---
 C4/Circulation.pm |  4 ++--
 C4/Reserves.pm    | 30 ++----------------------------
 2 files changed, 4 insertions(+), 30 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 70ccd7fb54..373a614568 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -25,7 +25,7 @@ use Encode;
 
 use C4::Context;
 use C4::Stats qw( UpdateStats );
-use C4::Reserves qw( CheckReserves CanItemBeReserved MoveReserve ModReserve ModReserveMinusPriority RevertWaitingStatus IsItemOnHoldAndFound IsAvailableForItemLevelRequest );
+use C4::Reserves qw( CheckReserves CanItemBeReserved MoveReserve ModReserve ModReserveMinusPriority RevertWaitingStatus IsAvailableForItemLevelRequest );
 use C4::Biblio qw( UpdateTotalIssues );
 use C4::Items qw( ModItemTransfer ModDateLastSeen CartToShelf );
 use C4::Accounts;
@@ -3105,7 +3105,7 @@ sub CanBookBeRenewed {
 
                 foreach my $other_item (@other_items) {
                   next if defined $matched_items{$other_item->itemnumber};
-                  next if IsItemOnHoldAndFound( $other_item->itemnumber );
+                  next if $other_item->holds->filter_by_found->count;
                   next unless IsAvailableForItemLevelRequest($other_item, $patron_with_reserve, undef);
                   next unless CanItemBeReserved($patron_with_reserve,$other_item,undef,{ignore_hold_counts=>1})->{status} eq 'OK';
                   # NOTE: At checkin we call 'CheckReserves' which checks hold 'policy'
diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index bc96dc9c3c..6f4eb5f7ae 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -135,8 +135,6 @@ BEGIN {
 
       CalculatePriority
 
-      IsItemOnHoldAndFound
-
       GetMaxPatronHoldsForRecord
 
       MergeHolds
@@ -1380,7 +1378,7 @@ sub IsAvailableForItemLevelRequest {
         return $any_available ? 0 : 1;
 
     } else {  # on_shelf_holds == 0 "If any unavailable" (the description is rather cryptic and could still be improved)
-        return $item->notforloan < 0 || $item->onloan || IsItemOnHoldAndFound( $item->itemnumber );
+        return $item->notforloan < 0 || $item->onloan || $item->holds->filter_by_found->count;
     }
 }
 
@@ -1412,7 +1410,7 @@ sub ItemsAnyAvailableAndNotRestricted {
             || $i->notforloan # items with non-zero notforloan cannot be checked out
             || $i->withdrawn
             || $i->onloan
-            || IsItemOnHoldAndFound( $i->id )
+            || $i->holds->filter_by_found->count
             || ( $i->damaged
                  && ! C4::Context->preference('AllowHoldsOnDamagedItems') )
             || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan
@@ -2243,30 +2241,6 @@ sub CalculatePriority {
     return @row ? $row[0]+1 : 1;
 }
 
-=head2 IsItemOnHoldAndFound
-
-    my $bool = IsItemFoundHold( $itemnumber );
-
-    Returns true if the item is currently on hold
-    and that hold has a non-null found status ( W, T, etc. )
-
-=cut
-
-sub IsItemOnHoldAndFound {
-    my ($itemnumber) = @_;
-
-    my $rs = Koha::Database->new()->schema()->resultset('Reserve');
-
-    my $found = $rs->count(
-        {
-            itemnumber => $itemnumber,
-            found      => { '!=' => undef }
-        }
-    );
-
-    return $found;
-}
-
 =head2 GetMaxPatronHoldsForRecord
 
 my $holds_per_record = ReservesControlBranch( $borrowernumber, $biblionumber );
-- 
2.46.0