From f3d60aea08d1cd852c68ef039245313ec4ea682e Mon Sep 17 00:00:00 2001 From: simith Date: Fri, 4 Jul 2014 10:59:38 -0400 Subject: [PATCH] Bug 12532 - Redirect guarantee email to guarantor email garantor Modified: C4/Letters.pm -- _send_message_by_email check guarantor C4/Members.pm -- GetNoticeEmailAddress return email guarantor Testing: 0) Enable EnhancedMessagingPreferences preference; 1) Select a patron with child; 2) Enable Item checkout -> email in patron messaging preferences; 3) Select the patron's child; 4) Checkout an item; 5) Valide child checkout message in patron's inbox; --- C4/Letters.pm | 30 ++++++++++++++++-------------- C4/Members.pm | 27 +++++++++++++++------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 1d916e0..78e32f8 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -926,23 +926,25 @@ sub _send_message_by_email { my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} ); my $to_address = $message->{'to_address'}; - unless ($to_address) { - unless ($member) { - warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})"; - _set_message_status( { message_id => $message->{'message_id'}, - status => 'failed' } ); - return; - } - $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} ); - unless ($to_address) { - # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})"; - # warning too verbose for this more common case? - _set_message_status( { message_id => $message->{'message_id'}, - status => 'failed' } ); - return; + + if ($member) { + my $guarantorid = $member->{'guarantorid'}; + if ($guarantorid){ + $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} ); + } else { + unless ($to_address) { + $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} ); + } } } + unless ($to_address) { + warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})"; + _set_message_status( { message_id => $message->{'message_id'}, + status => 'failed' } ); + return; + } + my $utf8 = decode('MIME-Header', $message->{'subject'} ); $message->{subject}= encode('MIME-Header', $utf8); my $subject = encode('utf8', $message->{'subject'}); diff --git a/C4/Members.pm b/C4/Members.pm index 13244e0..6a20a31 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -1497,22 +1497,25 @@ Returns the empty string if no email address. sub GetNoticeEmailAddress { my $borrowernumber = shift; + my $dbh = C4::Context->dbh(); + + my $member = C4::Members::GetMember( 'borrowernumber' => $borrowernumber ); + my $guarantorid = $member->{'guarantorid'}; + + my $borrowerNumberToSendEmail = $guarantorid ? $guarantorid : $borrowernumber; my $which_address = C4::Context->preference("AutoEmailPrimaryAddress"); # if syspref is set to 'first valid' (value == OFF), look up email address + my $to_address = $member->{$which_address}; if ( $which_address eq 'OFF' ) { - return GetFirstValidEmailAddress($borrowernumber); - } - # specified email address field - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare( qq{ - SELECT $which_address AS primaryemail - FROM borrowers - WHERE borrowernumber=? - } ); - $sth->execute($borrowernumber); - my $data = $sth->fetchrow_hashref; - return $data->{'primaryemail'} || ''; + $to_address = GetFirstValidEmailAddress($borrowerNumberToSendEmail); + } elsif( $guarantorid ) { + my $guarantor = C4::Members::GetMember( 'borrowernumber' => $guarantorid ); + if ( $guarantor ){ + $to_address = $guarantor->{ $which_address }; + } + } + return $to_address || ''; } =head2 GetExpiryDate -- 1.9.1