From 8db1a87b10a4847a5531f4c00d6a345bf654d31a Mon Sep 17 00:00:00 2001
From: Lucas Gass <lucas@bywatersolutions.com>
Date: Tue, 10 Sep 2024 15:38:16 +0000
Subject: [PATCH] Bug 37883: Add logged in branch available, other, and onloan
 counts

---
 C4/Search.pm | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)

diff --git a/C4/Search.pm b/C4/Search.pm
index ca8e79ab0f1..801210d5ba6 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -1705,6 +1705,7 @@ sub searchResults {
     # set stuff for XSLT processing here once, not later again for every record we retrieved
 
     my $userenv = C4::Context->userenv;
+    my $userenv_branch = $userenv->{'branch'};
     my $logged_in_user
         = ( defined $userenv and $userenv->{number} )
         ? Koha::Patrons->find( $userenv->{number} )
@@ -1793,21 +1794,24 @@ sub searchResults {
         my $onloan_items;
         my $other_items;
 
-        my $ordered_count         = 0;
-        my $available_count       = 0;
-        my $onloan_count          = 0;
-        my $longoverdue_count     = 0;
-        my $other_count           = 0;
+        my $ordered_count          = 0;
+        my $available_count        = 0;
+        my $branch_available_count = 0;
+        my $onloan_count           = 0;
+        my $branch_onloan_count    = 0;
+        my $longoverdue_count      = 0;
+        my $other_count            = 0;
+        my $branch_other_count     = 0;
         my $withdrawn_count        = 0;
-        my $itemlost_count        = 0;
-        my $hideatopac_count      = 0;
-        my $itembinding_count     = 0;
-        my $itemdamaged_count     = 0;
-        my $item_in_transit_count = 0;
-        my $item_onhold_count     = 0;
-        my $notforloan_count      = 0;
-        my $item_recalled_count   = 0;
-        my $items_count           = scalar(@fields);
+        my $itemlost_count         = 0;
+        my $hideatopac_count       = 0;
+        my $itembinding_count      = 0;
+        my $itemdamaged_count      = 0;
+        my $item_in_transit_count  = 0;
+        my $item_onhold_count      = 0;
+        my $notforloan_count       = 0;
+        my $item_recalled_count    = 0;
+        my $items_count            = scalar(@fields);
         my $maxitems_pref = C4::Context->preference('maxItemsinSearchResults');
         my $maxitems = $maxitems_pref ? $maxitems_pref - 1 : 1;
         my @hiddenitems; # hidden itemnumbers based on OpacHiddenItems syspref
@@ -1864,7 +1868,9 @@ sub searchResults {
                 and !( $patron_category_hide_lost_items and $item->{itemlost} ) )
             {
                 $onloan_count++;
+                $branch_onloan_count++ if $item->{'branchcode'} eq $userenv_branch;
                 my $key = $prefix . $item->{onloan} . $item->{barcode};
+                $onloan_items->{$key}->{branchonloancount} = $branch_onloan_count;
                 $onloan_items->{$key}->{due_date} = $item->{onloan};
                 $onloan_items->{$key}->{count}++ if $item->{$hbranch};
                 $onloan_items->{$key}->{branchname}     = $item->{branchname};
@@ -1961,11 +1967,12 @@ sub searchResults {
                     $item->{status} = ($item->{withdrawn}//q{}) . "-" . ($item->{itemlost}//q{}) . "-" . ($item->{damaged}//q{}) . "-" . ($item->{notforloan}//q{});
 
                     $other_count++;
-
+                    $branch_other_count++ if $item->{branchcode} eq $userenv_branch;
                     my $key = $prefix . $item->{status};
                     foreach (qw(withdrawn itemlost damaged branchname itemcallnumber)) {
                         $other_items->{$key}->{$_} = $item->{$_};
                     }
+                    $other_items->{$key}->{branchothercount} = $branch_other_count;
                     $other_items->{$key}->{branchcode}     = $item->{branchcode};
                     $other_items->{$key}->{intransit} = ( $transfertwhen ne '' ) ? 1 : 0;
                     $other_items->{$key}->{recalled} = ($recallstatus) ? 1 : 0;
@@ -1980,10 +1987,12 @@ sub searchResults {
                 # item is available
                 else {
                     $available_count++;
+                    $branch_available_count++ if $item->{branchcode} eq $userenv_branch;
                     $available_items->{$prefix}->{count}++ if $item->{$hbranch};
                     foreach (qw(branchname itemcallnumber description)) {
                         $available_items->{$prefix}->{$_} = $item->{$_};
                     }
+                    $available_items->{$prefix}->{branchavailablecount} = $branch_available_count;
                     $available_items->{$prefix}->{branchcode} = $item->{branchcode};
                     $available_items->{$prefix}->{location} = $shelflocations->{ $item->{location} } if $item->{location};
                     $available_items->{$prefix}->{imageurl} = getitemtypeimagelocation( $search_context->{'interface'}, $itemtypes{ $item->{itype}//q{} }->{imageurl} );
@@ -2036,7 +2045,7 @@ sub searchResults {
                 }
             );
         }
-
+        my $branch_count = $branch_available_count + $branch_onloan_count + $branch_other_count || 0;
         my $biblio_object = Koha::Biblios->find( $oldbiblio->{biblionumber} );
         $oldbiblio->{biblio_object} = $biblio_object;
         $oldbiblio->{coins} = eval { $biblio_object->get_coins }
@@ -2059,6 +2068,12 @@ sub searchResults {
         $oldbiblio->{onloan_items_loop}    = \@onloan_items_loop;
         $oldbiblio->{other_items_loop}     = \@other_items_loop;
         $oldbiblio->{availablecount}       = $available_count;
+
+        $oldbiblio->{branchavailablecount} = $branch_available_count || 0;
+        $oldbiblio->{branchonloancount}    = $branch_onloan_count     || 0;
+        $oldbiblio->{branchothercount}     = $branch_other_count      || 0;
+        $oldbiblio->{branchtotalcount}     = $branch_count            || 0;
+
         $oldbiblio->{availableplural}      = 1 if $available_count > 1;
         $oldbiblio->{onloancount}          = $onloan_count;
         $oldbiblio->{onloanplural}         = 1 if $onloan_count > 1;
-- 
2.39.2