add latitude/longitude to prospects, customers and package locations, RT#15539
[freeside.git] / FS / FS / geocode_Mixin.pm
index cf45a92..9ac8e7a 100644 (file)
@@ -4,7 +4,9 @@ use strict;
 use vars qw( $DEBUG $me );
 use Carp;
 use Locale::Country;
+use Geo::Coder::Googlev3; #compile time for now, until others are supported
 use FS::Record qw( qsearchs qsearch );
+use FS::Conf;
 use FS::cust_pkg;
 use FS::cust_location;
 use FS::cust_tax_location;
@@ -37,7 +39,8 @@ and other location fields.
 =item location_hash
 
 Returns a list of key/value pairs, with the following keys: address1, address2,
-city, county, state, zip, country.  The shipping address is used if present.
+city, county, state, zip, country, geocode, location_type, location_number,
+location_kind.  The shipping address is used if present.
 
 =cut
 
@@ -50,7 +53,8 @@ sub location_hash {
   map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
         $_ => $self->get($method);
       }
-      qw( address1 address2 city county state zip country geocode );
+      qw( address1 address2 city county state zip country geocode 
+       location_type location_number location_kind );
 }
 
 =item location_label [ OPTION => VALUE ... ]
@@ -82,7 +86,7 @@ sub location_label {
   my $ds = $opt{double_space} || '  ';
   my $line = '';
   my $cydefault = 
-    $opt{'countrydefault'} || FS::conf->new->config('countrydefault') || 'US';
+    $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
   my $prefix = $self->has_ship_address ? 'ship_' : '';
 
   my $notfirst = 0;
@@ -92,6 +96,26 @@ sub location_label {
       if $self->$method;
     $notfirst++;
   }
+
+  my $lt = $self->get($prefix.'location_type');
+  if ( $lt ) {
+    my %location_type;
+    if ( 1 ) { #ikano, switch on via config
+      { no warnings 'void';
+        eval { 'use FS::part_export::ikano;' };
+        die $@ if $@;
+      }
+      %location_type = FS::part_export::ikano->location_types;
+    } else {
+      %location_type = (); #?
+    }
+
+    $line .= ' '.&$escape( $location_type{$lt} || $lt );
+  }
+
+  $line .= ' '. &$escape($self->get($prefix.'location_number'))
+    if $self->get($prefix.'location_number');
+
   $notfirst = 0;
   foreach (qw ( city county state zip ) ) {
     my $method = "$prefix$_";
@@ -108,6 +132,36 @@ sub location_label {
   $line;
 }
 
+=item set_coord
+
+=cut
+
+sub set_coord {
+  my $self = shift;
+  my $pre = scalar(@_) ? shift : '';
+
+  #my $module = FS::Conf->new->config('geocode_module') || 'Geo::Coder::Googlev3';
+
+  my $geocoder = Geo::Coder::Googlev3->new;
+  my $location = $geocoder->geocode( location =>
+    $self->get($pre.'address1'). ','.
+    ( $self->get($pre.'address2') ? $self->get($pre.'address2').',' : '' ).
+    $self->get($pre.'city'). ','.
+    $self->get($pre.'state'). ','.
+    code2country($self->get($pre.'country'))
+  );
+
+  #errors?
+
+  my $geo_loc = $location->{'geometry'}{'location'} or return;
+  if ( $geo_loc->{'lat'} && $geo_loc->{'lng'} ) {
+    $self->set($pre.'latitude',  $geo_loc->{'lat'} );
+    $self->set($pre.'longitude', $geo_loc->{'lng'} );
+    $self->set($pre.'coord_auto', 'Y');
+  }
+
+}
+
 =item geocode DATA_VENDOR
 
 Returns a value for the customer location as encoded by DATA_VENDOR.
@@ -122,7 +176,7 @@ sub geocode {
   return $geocode if $geocode;
 
   my $prefix =
-   ( FS::conf->new->exists('tax-ship_address') && $self->has_ship_address )
+   ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
    ? 'ship_'
    : '';
 
@@ -132,7 +186,8 @@ sub geocode {
   $zip ||= '';
   $plus4 ||= '';
   #CCH specific location stuff
-  my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
+  my $extra_sql = $plus4 ? "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'"
+                         : '';
 
   my @cust_tax_location =
     qsearch( {
@@ -145,6 +200,11 @@ sub geocode {
   $geocode = $cust_tax_location[0]->geocode
     if scalar(@cust_tax_location);
 
+  warn "WARNING: customer ". $self->custnum.
+       ": multiple locations for zip ". $self->get("${prefix}zip").
+       "; using arbitrary geocode $geocode\n"
+    if scalar(@cust_tax_location) > 1;
+
   $geocode;
 }