Rework getBestPostalAddress to use new address format.

This commit is contained in:
Bradley M. Kuhn 2024-11-18 18:26:34 -08:00
parent d8da546803
commit 3e4bd08b23

View file

@ -812,45 +812,33 @@ Arguments:
=back =back
Returns a string that is the formatted_postal_address from the postal_address Returns a hash that has the fields of the postal address from the database.
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 =cut
sub getBestPostalAddress($) { sub getBestPostalAddress($) {
my($self, $id) = @_; my($self, $id) = @_;
die "Postal address stuff not fixed yet"; confess "getBestPostalAddress: invalid id, $id" unless $self->_verifyId($id);
die "getBestPostalAddress: invalid id, $id" unless $self->_verifyId($id); my $entries = $self->dbh()->selectall_hashref("SELECT pa.id, pa.first_name, pa.middle_name, pa.last_name, " .
"pa.organization, pa.address_1, pa.address_2, pa.address_3, " .
my $pref = $self->getPreferredPostalAddress($id); "pa.city, pa.state_province_or_region, pa.postcode, pa.country, " .
"map.date_valid_from, at.name as type " .
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 " .
"FROM donor_postal_address_mapping map, address_type at, postal_address pa " . "WHERE at.id = map.type_id AND pa.id = map.postal_address_id AND " .
"WHERE at.id = map.type_id AND pa.id = map.postal_address_id AND " . " map.date_valid_to is NULL AND map.donor_id = " .
"(pa.invalid is NULL OR pa.invalid != 1) " . $self->dbh->quote($id, 'SQL_INTEGER'),
"AND map.donor_id = " . $self->dbh->quote($id, 'SQL_INTEGER'),
'id'); 'id');
my $newest; if (keys %$entries <= 0) {
my $otherSources = ""; carp "getBestPostalAddress: unable to find postal address for id, $id";
foreach my $pid (keys %{$entries}) { return undef;
$newest = $entries->{$pid} unless defined $newest; } elsif (keys %$entries > 1) {
if ($newest->{date_encountered} lt $entries->{$pid}{date_encountered}) { carp "getBestPostalAddress: multiple postal address with date_valid_to as NULL for id, $id";
$newest = $entries->{$pid}; return undef;
}
$otherSources .= " " . $entries->{$pid}{name} if defined $entries->{$pid}{name} and $entries->{$pid}{name} ne 'paypal';
} }
if (defined $pref and $newest->{formatted_address} ne $pref) { my($pid) = keys(%$entries);
carp("$id: preferred address is different than the newest available address: preferred:\n$pref newest:\n $newest->{formatted_address}\n... returning newest"); return $entries->{$pid};
} 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};
} }
###################################################################### ######################################################################