From 63ac909e33f25ecb90782c40e07f7d33d4ce3761 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. --- 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 c5c9701201..765c17316e 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -3642,7 +3642,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.2