summaryrefslogtreecommitdiff
path: root/FS/FS/cust_location.pm
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2013-05-22 16:58:31 -0700
committerMark Wells <mark@freeside.biz>2013-05-22 16:58:31 -0700
commite6210bad824b2efc0a2d54abe8fc79093e7cc5ed (patch)
treeb74167e7763e96762e65ba32f27bd2a86987d41a /FS/FS/cust_location.pm
parent2d2fad4dc5654636abf690e0980b851540f64a5b (diff)
fully automate coordinate lookups, #23040
Diffstat (limited to 'FS/FS/cust_location.pm')
-rw-r--r--FS/FS/cust_location.pm43
1 files changed, 43 insertions, 0 deletions
diff --git a/FS/FS/cust_location.pm b/FS/FS/cust_location.pm
index 1cb5e52..d772dab 100644
--- a/FS/FS/cust_location.pm
+++ b/FS/FS/cust_location.pm
@@ -322,6 +322,11 @@ sub check {
} );
}
+ # set coordinates, unless we already have them
+ if (!$import and !$self->latitude and !$self->longitude) {
+ $self->set_coord;
+ }
+
$self->SUPER::check;
}
@@ -638,6 +643,44 @@ sub in_county_sql {
}
}
+sub process_set_coord {
+ my $job = shift;
+ # avoid starting multiple instances of this job
+ my @others = qsearch('queue', {
+ 'status' => 'locked',
+ 'job' => $job->job,
+ 'jobnum' => {op=>'!=', value=>$job->jobnum},
+ });
+ return if @others;
+
+ $job->update_statustext('finding locations to update');
+ my @missing_coords = qsearch('cust_location', {
+ 'disabled' => '',
+ 'latitude' => '',
+ 'longitude' => '',
+ });
+ my $i = 0;
+ my $n = scalar @missing_coords;
+ for my $cust_location (@missing_coords) {
+ $cust_location->set_coord;
+ my $error = $cust_location->replace;
+ if ( $error ) {
+ warn "error geocoding location#".$cust_location->locationnum.": $error\n";
+ } else {
+ $i++;
+ $job->update_statustext("updated $i / $n locations");
+ dbh->commit; # so that we don't have to wait for the whole thing to finish
+ # Rate-limit to stay under the Google Maps usage limit (2500/day).
+ # 86,400 / 35 = 2,468 lookups per day.
+ }
+ sleep 35;
+ }
+ if ( $i < $n ) {
+ die "failed to update ".$n-$i." locations\n";
+ }
+ return;
+}
+
=head1 BUGS
=head1 SEE ALSO