don't look up or display census tracts for non-U.S. addresses, #32249
[freeside.git] / FS / FS / Misc / Geo.pm
index 9f6b89b..a387aca 100644 (file)
@@ -42,6 +42,10 @@ sub get_censustract_ffiec {
   my $location = shift;
   my $year  = shift;
 
+  if ( length($location->{country}) and uc($location->{country}) ne 'US' ) {
+    return '';
+  }
+
   warn Dumper($location, $year) if $DEBUG;
 
   my $url = 'http://www.ffiec.gov/Geocode/default.aspx';
@@ -333,87 +337,6 @@ sub standardize_usps {
     addr_clean=> 'Y' }
 }
 
-my %ezlocate_error = ( # USA_Geo_002 documentation
-  10  => 'State not found',
-  11  => 'City not found',
-  12  => 'Invalid street address',
-  14  => 'Street name not found',
-  15  => 'Address range does not exist',
-  16  => 'Ambiguous address',
-  17  => 'Intersection not found', #unused?
-);
-
-sub standardize_ezlocate {
-  my $self = shift;
-  my $location = shift;
-  my $class;
-  #if ( $location->{country} eq 'US' ) {
-  #  $class = 'USA_Geo_004Tool';
-  #}
-  #elsif ( $location->{country} eq 'CA' ) {
-  #  $class = 'CAN_Geo_001Tool';
-  #}
-  #else { # shouldn't be a fatal error, just pass through unverified address
-  #  warn "standardize_teleatlas: address lookup in '".$location->{country}.
-  #       "' not available\n";
-  #  return $location;
-  #}
-  #my $path = $conf->config('teleatlas-path') || '';
-  #local @INC = (@INC, $path);
-  #eval "use $class;";
-  #if ( $@ ) {
-  #  die "Loading $class failed:\n$@".
-  #      "\nMake sure the TeleAtlas Perl SDK is installed correctly.\n";
-  #}
-
-  $class = 'Geo::EZLocate'; # use our own library
-  eval "use $class 0.02"; #Geo::EZLocate 0.02 for error handling
-  die $@ if $@;
-
-  my $userid = $conf->config('ezlocate-userid')
-    or die "no ezlocate-userid configured\n";
-  my $password = $conf->config('ezlocate-password')
-    or die "no ezlocate-password configured\n";
-  
-  my $tool = $class->new($userid, $password);
-  my $match = $tool->findAddress(
-    $location->{address1},
-    $location->{city},
-    $location->{state},
-    $location->{zip}, #12345-6789 format is allowed
-  );
-  warn "ezlocate returned match:\n".Dumper($match) if $DEBUG > 1;
-  # error handling - B codes indicate success
-  die $ezlocate_error{$match->{MAT_STAT}}."\n"
-    unless $match->{MAT_STAT} =~ /^B\d$/;
-
-  my %result = (
-    address1    => $match->{MAT_ADDR},
-    address2    => $location->{address2},
-    city        => $match->{MAT_CITY},
-    state       => $match->{MAT_ST},
-    country     => $location->{country},
-    zip         => $match->{MAT_ZIP},
-    latitude    => $match->{MAT_LAT},
-    longitude   => $match->{MAT_LON},
-    censustract => $match->{FIPS_ST}.$match->{FIPS_CTY}.
-                   sprintf('%07.2f',$match->{CEN_TRCT}),
-    addr_clean  => 'Y',
-  );
-  if ( $match->{STD_ADDR} ) {
-    # then they have a postal standardized address for us
-    %result = ( %result,
-      address1    => $match->{STD_ADDR},
-      address2    => $location->{address2},
-      city        => $match->{STD_CITY},
-      state       => $match->{STD_ST},
-      zip         => $match->{STD_ZIP}.'-'.$match->{STD_P4},
-    );
-  }
-
-  \%result;
-}
-
 sub _tomtom_query { # helper method for the below
   my %args = @_;
   my $result = Geo::TomTom::Geocoding->query(%args);
@@ -652,6 +575,57 @@ sub subloc_address2 {
   ($subloc, $addr2);
 }
 
+sub standardize_melissa {
+  my $class = shift;
+  my $location = shift;
+
+  local $@;
+  eval "use Geo::Melissa::WebSmart";
+  die $@ if $@;
+
+  my $id = $conf->config('melissa-userid')
+    or die "no melissa-userid configured\n";
+  my $geocode = $conf->exists('melissa-enable_geocoding') ? 1 : 0;
+
+  my $request = {
+    id      => $id,
+    a1      => $location->{address1},
+    a2      => $location->{address2},
+    city    => $location->{city},
+    state   => $location->{state},
+    ctry    => $location->{country},
+    zip     => $location->{zip},
+    geocode => $geocode,
+  };
+  my $result = Geo::Melissa::WebSmart->query($request);
+  if ( $result->code =~ /AS01/ ) { # always present on success
+    my $addr = $result->address;
+    warn Dumper $addr if $DEBUG > 1;
+    my $out = {
+      address1    => $addr->{Address1},
+      address2    => $addr->{Address2},
+      city        => $addr->{City}->{Name},
+      state       => $addr->{State}->{Abbreviation},
+      country     => $addr->{Country}->{Abbreviation},
+      zip         => $addr->{Zip},
+      latitude    => $addr->{Latitude},
+      longitude   => $addr->{Longitude},
+      addr_clean  => 'Y',
+    };
+    if ( $addr->{Census}->{Tract} ) {
+      my $censustract = $addr->{County}->{Fips} . $addr->{Census}->{Tract};
+      # insert decimal point two digits from the end
+      $censustract =~ s/(\d\d)$/\.$1/;
+      $out->{censustract} = $censustract;
+      $out->{censusyear} = $conf->config('census_year');
+    }
+    # we could do a lot more nuanced reporting of the warning/status codes,
+    # but the UI doesn't support that yet.
+    return $out;
+  } else {
+    die $result->status_message;
+  }
+}
 
 =back