[freeside-commits] branch master updated. 27bace7e53b5259e4694d8a5aea394fd1d88dc42

Mitch Jackson mitch at freeside.biz
Fri Nov 30 22:02:50 PST 2018


The branch, master has been updated
       via  27bace7e53b5259e4694d8a5aea394fd1d88dc42 (commit)
       via  de7f38b04c7aa21aabb9aa6d6e65b35e02b96006 (commit)
      from  9bf940754c8cfc9404a49965ea3e4e8f0d4cd646 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 27bace7e53b5259e4694d8a5aea394fd1d88dc42
Author: Mitch Jackson <mitch at freeside.biz>
Date:   Sat Dec 1 01:01:03 2018 -0500

    RT# 81830 Critical log event for corrupted invoice data

diff --git a/FS/FS/Template_Mixin.pm b/FS/FS/Template_Mixin.pm
index 9a84b43c9..675dcfacb 100644
--- a/FS/FS/Template_Mixin.pm
+++ b/FS/FS/Template_Mixin.pm
@@ -26,6 +26,7 @@ use FS::pkg_category;
 use FS::pkg_class;
 use FS::invoice_mode;
 use FS::L10N;
+use FS::Log;
 
 $DEBUG = 0;
 $me = '[FS::Template_Mixin]';
@@ -3461,6 +3462,27 @@ sub _items_cust_bill_pkg {
           if $DEBUG > 1;
 
         my $cust_pkg = $cust_bill_pkg->cust_pkg;
+
+        unless ( $cust_pkg ) {
+          # There is no related row in cust_pkg for this cust_bill_pkg.pkgnum.
+          # This invoice may have been broken by an unusual combination
+          # of manually editing package dates, and aborted package changes
+          # when the manually edited dates used are nonsensical.
+
+          my $error = sprintf
+            'cust_bill_pkg(billpkgnum:%s) '.
+            'is missing related row in cust_pkg(pkgnum:%s)! '.
+            'cust_bill(invnum:%s) is corrupted by bad database data, '.
+            'and should be investigated',
+              $cust_bill_pkg->billpkgnum,
+              $cust_bill_pkg->pkgnum,
+              $cust_bill_pkg->invnum;
+
+          FS::Log->new('FS::cust_bill_pkg')->critical( $error );
+          warn $error;
+          next;
+        }
+
         my $part_pkg = $cust_pkg->part_pkg;
 
         # which pkgpart to show for display purposes?

commit de7f38b04c7aa21aabb9aa6d6e65b35e02b96006
Author: Mitch Jackson <mitch at freeside.biz>
Date:   Sat Dec 1 00:04:04 2018 -0500

    RT# 81574 Update Google Geocode API that sets location coordinates

diff --git a/FS/FS/geocode_Mixin.pm b/FS/FS/geocode_Mixin.pm
index b999be0ab..87ad9acfd 100644
--- a/FS/FS/geocode_Mixin.pm
+++ b/FS/FS/geocode_Mixin.pm
@@ -3,8 +3,11 @@ package FS::geocode_Mixin;
 use strict;
 use vars qw( $DEBUG $me );
 use Carp;
+use Cpanel::JSON::XS;
+use Data::Dumper;
 use Locale::Country ();
-use Geo::Coder::Googlev3; #compile time for now, until others are supported
+use LWP::UserAgent;
+use URI::Escape;
 use FS::Record qw( qsearchs qsearch );
 use FS::Conf;
 use FS::cust_pkg;
@@ -163,31 +166,66 @@ API and set the 'latitude' and 'longitude' fields accordingly.
 sub set_coord {
   my $self = shift;
 
-  #my $module = FS::Conf->new->config('geocode_module') || 'Geo::Coder::Googlev3';
-
-  my $geocoder = Geo::Coder::Googlev3->new;
-
-  my $location = eval {
-    $geocoder->geocode( location =>
-      $self->get('address1'). ','.
-      ( $self->get('address2') ? $self->get('address2').',' : '' ).
-      $self->get('city'). ','.
-      $self->get('state'). ','.
-      $self->country_full
-    );
-  };
-  if ( $@ ) {
-    warn "geocoding error: $@\n";
+  # Google documetnation:
+  # https://developers.google.com/maps/documentation/geocoding/start
+
+
+  my $api_key = FS::Conf->new->config('google_maps_api_key');
+
+  unless ( $api_key ) {
+    # Google API now requires a valid key with a payment method attached
+    warn 'Geocoding unavailable, install a google_maps_api_key';
     return;
   }
 
-  my $geo_loc = $location->{'geometry'}{'location'} or return;
-  if ( $geo_loc->{'lat'} && $geo_loc->{'lng'} ) {
-    $self->set('latitude',  $geo_loc->{'lat'} );
-    $self->set('longitude', $geo_loc->{'lng'} );
-    $self->set('coord_auto', 'Y');
+  my $google_api_url = 'https://maps.googleapis.com/maps/api/geocode/json';
+
+  my $address =
+    join ',',
+    map { $self->$_ ? uri_escape( $self->get( $_ ) ) : () }
+    qw( address1 address2 city state zip country_full );
+
+  my $query_url = sprintf
+    '%s?address=%s&key=%s',
+    $google_api_url, $address, $api_key;
+
+  my $ua = LWP::UserAgent->new;
+  $ua->timeout(10);
+  my $res = $ua->get( $query_url );
+  my $json_res = decode_json( $res->decoded_content );
+  my $json_error = $json_res->{error_message}
+    if ref $json_res && $json_res->{error_message};
+
+  if ( $DEBUG ) {
+    warn "\$query_url: $query_url\n";
+    warn "\$json_error: $json_error\n";
+    warn Dumper( $json_res || $res->decoded_content )."\n";
   }
 
+  if ( !$res->is_success || $json_error ) {
+    warn "Error using google GeoCoding API";
+    warn Dumper( $json_res || $res->decoded_content );
+    return;
+  }
+  
+  if (
+       ref $json_res
+    && ref $json_res->{results}
+    && ref $json_res->{results}->[0]
+    && ref $json_res->{results}->[0]->{geometry}
+    && ref $json_res->{results}->[0]->{geometry}->{location}
+  ) {
+    my $location = $json_res->{results}->[0]->{geometry}->{location};
+    if ( $location->{lat} && $location->{lng} ) {
+      $self->set( latitude   => $location->{lat} );
+      $self->set( longitude  => $location->{lng} );
+      $self->set( coord_auto => 'Y' );
+    }
+  } else {
+    # If google changes the API response structure, warnings abound
+    warn "No location match found using google GeoCoding API for $address";
+    warn Dumper( $json_res || $res->decoded_content );
+  }
 }
 
 =item geocode DATA_VENDOR

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Template_Mixin.pm | 22 ++++++++++++++
 FS/FS/geocode_Mixin.pm  | 80 ++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 81 insertions(+), 21 deletions(-)




More information about the freeside-commits mailing list