From f7cfe3a705b4c594e1e9b70c1e7e1ffa0b39a073 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 10 Mar 2023 10:02:19 +0000 Subject: [PATCH] Bug 18398: Update C4::Message enqueue to use $patron->notice_email_address This patch updates the enque method in C4::Message to expect a Koha::Patron object in the parameters and then uses that patron object to select the correct email address for notices as defined by AutoEmailPrimaryAddress. Signed-off-by: Caroline Cyr La Rose Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- C4/Circulation.pm | 3 ++- C4/Message.pm | 45 ++++++++++++++++++++++++--------------------- Koha/Recall.pm | 2 +- Koha/Recalls.pm | 2 +- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 237181fe1c..fe1991f2d1 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -3657,7 +3657,8 @@ sub SendCirculationAlert { my $message = C4::Message->find_last_message($borrower, $type, $mtt); unless ( $message ) { C4::Context->dbh->do(q|UNLOCK TABLES|) unless $do_not_lock; - C4::Message->enqueue($letter, $borrower, $mtt); + my $patron = Koha::Patrons->find($borrower->{borrowernumber}); + C4::Message->enqueue($letter, $patron, $mtt); } else { $message->append($letter); $message->update; diff --git a/C4/Message.pm b/C4/Message.pm index 7a55152e05..8bf1a05b0a 100644 --- a/C4/Message.pm +++ b/C4/Message.pm @@ -157,41 +157,44 @@ the message. # C4::Message->enqueue($letter, $borrower, $transport) sub enqueue { - my ($class, $letter, $borrower, $transport) = @_; + my ( $class, $letter, $patron, $transport ) = @_; my $metadata = _metadata($letter); - my $to_address = _to_address($borrower, $transport); + my $to_address = _to_address( $patron, $transport ); # Same as render_metadata my $format ||= sub { $_[0] || "" }; - my $body = join('', map { $format->($_) } @{$metadata->{body}}); + my $body = join( '', map { $format->($_) } @{ $metadata->{body} } ); $letter->{content} = $metadata->{header} . $body . $metadata->{footer}; - $letter->{metadata} = Encode::decode_utf8(Dump($metadata)); - C4::Letters::EnqueueLetter({ - letter => $letter, - borrowernumber => $borrower->{borrowernumber}, - message_transport_type => $transport, - to_address => $to_address, - }); + $letter->{metadata} = Encode::decode_utf8( Dump($metadata) ); + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->id, + message_transport_type => $transport, + to_address => $to_address, + } + ); } # based on message $transport, pick an appropriate address to send to sub _to_address { - my ($borrower, $transport) = @_; + my ( $patron, $transport ) = @_; my $address; - if ($transport eq 'email') { - $address = $borrower->{email} - || $borrower->{emailpro} - || $borrower->{B_email}; - } elsif ($transport eq 'sms') { - $address = $borrower->{smsalertnumber}; - } else { + if ( $transport eq 'email' ) { + $address = $patron->notice_email_address; + } + elsif ( $transport eq 'sms' ) { + $address = $patron->smsalertnumber; + } + else { warn "'$transport' is an unknown message transport."; } - if (not defined $address) { + if ( not defined $address ) { warn "An appropriate $transport address " - . "for borrower $borrower->{userid} " - . "could not be found."; + . "for borrower " + . $patron->userid + . "could not be found."; } return $address; } diff --git a/Koha/Recall.pm b/Koha/Recall.pm index c993bb7fda..ab390962b2 100644 --- a/Koha/Recall.pm +++ b/Koha/Recall.pm @@ -358,7 +358,7 @@ sub set_waiting { }, ); - C4::Message->enqueue($letter, $self->patron->unblessed, 'email'); + C4::Message->enqueue($letter, $self->patron, 'email'); return $self; } diff --git a/Koha/Recalls.pm b/Koha/Recalls.pm index 36d891d195..e81802b191 100644 --- a/Koha/Recalls.pm +++ b/Koha/Recalls.pm @@ -161,7 +161,7 @@ sub add_recall { }, ); - C4::Message->enqueue( $letter, $checkout->patron->unblessed, 'email' ); + C4::Message->enqueue( $letter, $checkout->patron, 'email' ); $item = Koha::Items->find( $itemnumber ); # add to statistics table -- 2.39.5