From 5740071275b0a3f190848654a31698c9ff2eb1a0 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Thu, 13 May 2021 12:04:41 -0400
Subject: [PATCH] Bug 14364: (QA follow-up) Generate message for transfers as
 well

---
 C4/Reserves.pm                                | 49 ++++++++++++++++++-
 t/db_dependent/Holds/ExpireReservesAutoFill.t | 16 +++---
 2 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index f58c406460..e40edd42f6 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -1188,12 +1188,14 @@ sub ModReserveAffect {
         $hold->set_processing();
     } else {
         $hold->set_waiting($desk_id);
-        _koha_notify_reserve( $hold->reserve_id, $notify_library ) unless $already_on_shelf;
+        _koha_notify_reserve( $hold->reserve_id ) unless $already_on_shelf;
         # Complete transfer if one exists
         my $transfer = $hold->item->get_transfer;
         $transfer->receive if $transfer;
     }
 
+    _koha_notify_hold_changed( $hold ) if $notify_library;
+
     _FixPriority( { biblionumber => $biblionumber } );
     my $item = Koha::Items->find($itemnumber);
     if ( $item->location && $item->location eq 'CART'
@@ -1915,6 +1917,51 @@ sub _koha_notify_reserve {
     }
 }
 
+=head2 _koha_notify_hold_changed
+
+  _koha_notify_hold_changed( $hold_object );
+
+=cut
+
+sub _koha_notify_hold_changed {
+    my $hold = shift;
+
+    my $patron = $hold->patron;
+    my $library = $hold->branch;
+
+    my $letter = C4::Letters::GetPreparedLetter(
+        module      => 'reserves',
+        letter_code => 'HOLD_CHANGED',
+        branchcode  => $hold->branchcode,
+        substitute  => { today => output_pref( dt_from_string ) },
+        tables      => {
+            'branches'    => $library->unblessed,
+            'borrowers'   => $patron->unblessed,
+            'biblio'      => $hold->biblionumber,
+            'biblioitems' => $hold->biblionumber,
+            'reserves'    => $hold->unblessed,
+            'items'       => $hold->itemnumber,
+        },
+    );
+
+    return unless $letter;
+
+    my $email =
+         C4::Context->preference('ExpireReservesAutoFillEmail')
+      || $library->branchemail
+      || C4::Context->preference('KohaAdminEmailAddress');
+
+    C4::Letters::EnqueueLetter(
+        {
+            letter                 => $letter,
+            borrowernumber         => $patron->id,
+            message_transport_type => 'email',
+            from_address           => $email,
+            to_address             => $email,
+        }
+    );
+}
+
 =head2 _ShiftPriorityByDateAndPriority
 
   $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
diff --git a/t/db_dependent/Holds/ExpireReservesAutoFill.t b/t/db_dependent/Holds/ExpireReservesAutoFill.t
index 610e3ba747..d805c50029 100755
--- a/t/db_dependent/Holds/ExpireReservesAutoFill.t
+++ b/t/db_dependent/Holds/ExpireReservesAutoFill.t
@@ -100,11 +100,11 @@ subtest 'Test automatically canceled expired waiting holds to fill the next hold
 
     CancelExpiredReserves();
 
-    my @holds = Koha::Holds->search( {}, { order_by => 'priority' } );
-    $hold_2 = $holds[0];
-    $hold_3 = $holds[1];
+    my $holds = Koha::Holds->search( {}, { order_by => 'priority' } );
+    $hold_2 = $holds->next;
+    $hold_3 = $holds->next;
 
-    is( @holds,            2,   'Found 2 holds' );
+    is( $holds->count,     2,   'Found 2 holds' );
     is( $hold_2->priority, 0,   'Next hold in line now has priority of 0' );
     is( $hold_2->found,    'W', 'Next hold in line is now set to waiting' );
 
@@ -117,10 +117,10 @@ subtest 'Test automatically canceled expired waiting holds to fill the next hold
 
     CancelExpiredReserves();
 
-    @holds = Koha::Holds->search( {}, { order_by => 'priority' } );
-    $hold_3 = $holds[0];
+    $holds = Koha::Holds->search( {}, { order_by => 'priority' } );
+    $hold_3 = $holds->next;
 
-    is( @holds,            1,   'Found 1 hold' );
+    is( $holds->count,     1,   'Found 1 hold' );
     is( $hold_3->priority, 0,   'Next hold in line now has priority of 0' );
     is( $hold_3->found,    'W', 'Next hold in line is now set to waiting' );
 
@@ -186,5 +186,5 @@ subtest 'Test automatically canceled expired waiting holds to fill the next hold
 
     my @messages = $schema->resultset('MessageQueue')
       ->search( { letter_code => 'HOLD_CHANGED' } );
-    is( @messages, 0, 'No messages in the message queue when generating transfer' );
+    is( @messages, 1, 'Nessage is generated in the message queue when generating transfer' );
 };
-- 
2.30.2