Fixes bug 3619: _send_message_by_email not respecting AutoEmailPrimaryAddress = ...
authorIan Walls <ian.walls@bywatersolutions.com>
Mon, 14 Jun 2010 14:26:09 +0000 (14:26 +0000)
committerGalen Charlton <gmcharlt@gmail.com>
Sat, 19 Jun 2010 12:14:27 +0000 (08:14 -0400)
Signed-off-by: Galen Charlton <gmcharlt@gmail.com>
C4/Letters.pm
C4/Members.pm

index 47abf6c..3f634e1 100644 (file)
@@ -803,7 +803,12 @@ sub _send_message_by_email ($;$$$) {
             return;
         }
         my $which_address = C4::Context->preference('AutoEmailPrimaryAddress');
-        $to_address = $member->{$which_address};
+        # If the system preference is set to 'first valid' (value == OFF), look up email address
+        if ($which_address eq 'OFF') {
+            $to_address = GetFirstValidEmailAddress( $message->{'borrowernumber'} );
+        } else {
+            $to_address = $member->{$which_address};
+        }
         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?
index eba71f5..cddbf41 100644 (file)
@@ -56,6 +56,8 @@ BEGIN {
                &getzipnamecity 
                &getidcity
 
+                &GetFirstValidEmailAddress
+
                &GetAge 
                &GetCities 
                &GetRoadTypes 
@@ -1247,6 +1249,33 @@ sub getidcity {
     return $data;
 }
 
+=head2 GetFirstValidEmailAddress
+
+  $email = GetFirstValidEmailAddress($borrowernumber);
+
+Return the first valid email address for a borrower, given the borrowernumber.  For now, the order 
+is defined as email, emailpro, B_email.  Returns the empty string if the borrower has no email 
+addresses.
+
+=cut
+
+sub GetFirstValidEmailAddress {
+    my $borrowernumber = shift;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare( "SELECT email, emailpro, B_email FROM borrowers where borrowernumber = ? ");
+    $sth->execute( $borrowernumber );
+    my $data = $sth->fetchrow_hashref;
+
+    if ($data->{'email'}) {
+       return $data->{'email'};
+    } elsif ($data->{'emailpro'}) {
+       return $data->{'emailpro'};
+    } elsif ($data->{'B_email'}) {
+       return $data->{'B_email'};
+    } else {
+       return '';
+    }
+}
 
 =head2 GetExpiryDate