From 3e4bd08b23e962741e46c45894c65c2977bb8d0d Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Mon, 18 Nov 2024 18:26:34 -0800 Subject: [PATCH] Rework getBestPostalAddress to use new address format. --- Supporters/lib/Supporters.pm | 48 ++++++++++++++---------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index cace8d4..4cdf982 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -812,45 +812,33 @@ Arguments: =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. +Returns a hash that has the fields of the postal address from the database. =cut sub getBestPostalAddress($) { 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 $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 = map.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'), + 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, " . + "pa.city, pa.state_province_or_region, pa.postcode, pa.country, " . + "map.date_valid_from, at.name as type " . + "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 " . + " map.date_valid_to is NULL 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 (keys %$entries <= 0) { + carp "getBestPostalAddress: unable to find postal address for id, $id"; + return undef; + } elsif (keys %$entries > 1) { + carp "getBestPostalAddress: multiple postal address with date_valid_to as NULL for id, $id"; + return undef; } - 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}; + my($pid) = keys(%$entries); + return $entries->{$pid}; } ######################################################################