From fbfb60b9a54069246c39f18fc9222439c98ac244 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Mon, 23 Dec 2024 16:05:03 +0000
Subject: [PATCH] Bug 38777: Use branch reply to for HOLD messages

When sending a notice of a hold ready for pickup (not digest) we use the library where the hold is at.

We should also use this when setting the reply to.

To test:
1 - Set the email/replyto for a patron's library to be:
    patron@example.com
2 - Set the email/replyto for another branch to be:
    holdbranch@example.com
3 - Place a hold for the patron for pickup at the other branch
4 - Check in and set hold waiting
5 - SELECT * FROM message_queue
6 - Note the message is from the hold branch, but has a null replyto
    This means we will use patron branch when sending
7 - Apply patch
8 - Revert hold waiting status
9 - Checkin and confirm hold again
10  -Check message queue
11 - Hold now has reply address of branch

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Baptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
---
 C4/Reserves.pm            |  6 ++++--
 t/db_dependent/Reserves.t | 11 ++++++++---
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index 046bfd0235..e96e7ee5c6 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -1776,8 +1776,9 @@ sub _koha_notify_reserve {
             message_name => 'Hold_Filled'
     } );
 
-    my $library = Koha::Libraries->find( $hold->branchcode );
-    my $from_email_address = $library->from_email_address;
+    my $library             = Koha::Libraries->find( $hold->branchcode );
+    my $from_email_address  = $library->from_email_address;
+    my $reply_email_address = $library->inbound_email_address;
 
     my %letter_params = (
         module => 'reserves',
@@ -1812,6 +1813,7 @@ sub _koha_notify_reserve {
                     letter                 => $letter,
                     borrowernumber         => $borrowernumber,
                     from_address           => $from_email_address,
+                    reply_address          => $reply_email_address,
                     message_transport_type => $mtt,
                 }
             );
diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t
index 616506b9b7..12538a4888 100755
--- a/t/db_dependent/Reserves.t
+++ b/t/db_dependent/Reserves.t
@@ -715,7 +715,7 @@ $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
 
 subtest '_koha_notify_reserve() tests' => sub {
 
-    plan tests => 3;
+    plan tests => 4;
 
     my $branch = $builder->build_object({
         class => 'Koha::Libraries',
@@ -797,9 +797,14 @@ subtest '_koha_notify_reserve() tests' => sub {
             borrowernumber => $hold_borrower,
         })->next();
     my $email_to_address = $email->to_address();
-    is($email_to_address, undef ,"We should not populate the hold message with the email address, sending will do so");
+    is(
+        $email_to_address, undef,
+        "We should not populate the hold message with the email address, sending will do so"
+    );
     my $email_from_address = $email->from_address();
-    is($email_from_address,'branch@e.mail',"Library's from address is used for sending");
+    is( $email_from_address, 'branch@e.mail', "Library's from address is used for sending" );
+    my $email_reply_address = $email->reply_address();
+    is( $email_reply_address, 'branch@reply.to', "Library's reply address is used for reply" );
 
 };
 
-- 
2.39.5