Add method: getBestPostalAddress

This commit is contained in:
Bradley M. Kuhn 2022-06-21 09:55:37 -07:00
parent 391fb0d3f2
commit 6d143082de

View file

@ -798,6 +798,59 @@ sub getPostalAddresses($) {
}
######################################################################
=begin getBestPostalAddress
Arguments:
=over
=item $id
Valid supporter id number currently in the database. die() will occur if
the id number is not in the database already as a supporter id.
=back
Returns a string that is the formatted_postal_address from the postal_address
table entry, and the formatted_postal_address address will be our "best
guess" of the best postal address. Note that the method will "confess"
various concerns it might have about determining the best postal address.
=cut
sub getBestPostalAddress($) {
my($self, $id) = @_;
die "getBestPostalAddress: invalid id, $id" unless $self->_verifyId($id);
my $pref = $self->getPreferredPostalAddress($id);
my $entries = $self->dbh()->selectall_hashref("SELECT pa.id, pa.formatted_address, at.name, pa.date_encountered " .
"FROM donor_postal_address_mapping map, address_type at, postal_address pa " .
"WHERE at.id = pa.type_id AND pa.id = map.postal_address_id AND " .
"(pa.invalid is NULL OR pa.invalid != 1) " .
"AND map.donor_id = " . $self->dbh->quote($id, 'SQL_INTEGER'),
'id');
my $newest;
my $otherSources = "";
foreach my $pid (keys %{$entries}) {
$newest = $entries->{$pid} unless defined $newest;
if ($newest->{date_encountered} lt $entries->{$pid}{date_encountered}) {
$newest = $entries->{$pid};
}
$otherSources .= " " . $entries->{$pid}{name} if defined $entries->{$pid}{name} and $entries->{$pid}{name} ne 'paypal';
}
if (defined $pref and $newest->{formatted_address} ne $pref) {
carp("$id: preferred address is different than the newest available address: preferred:\n$pref newest:\n $newest->{formatted_address}\n... returning newest");
} elsif ($newest->{name} eq 'paypal' and $otherSources ne "") {
carp("$id: newest address is from paypal, but we have addresses from other sources, namely, $otherSources that are older")
unless (defined $pref and $newest->{formatted_address} eq $pref);
}
return $newest->{formatted_address};
}
######################################################################
=begin getRequestType
Arguments: