X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_location.pm;h=11e97ecfef9a31c67fd899cf19688153f8da88d0;hb=7d967f5ac6929fddc08cc077bcd44ea48a3937f2;hp=a834859baad33d08943c10dc19f22e1959e1d4e1;hpb=9f84e0efea1f0fcfce3139519f2b75898f3255b0;p=freeside.git diff --git a/FS/FS/cust_location.pm b/FS/FS/cust_location.pm index a834859ba..11e97ecfe 100644 --- a/FS/FS/cust_location.pm +++ b/FS/FS/cust_location.pm @@ -10,6 +10,8 @@ use FS::Conf; use FS::prospect_main; use FS::cust_main; use FS::cust_main_county; +use FS::GeocodeCache; +use Date::Format qw( time2str ); $import = 0; @@ -257,12 +259,12 @@ and replace methods. =cut -#some false laziness w/cust_main, but since it should eventually lose these -#fields anyway... sub check { my $self = shift; my $conf = new FS::Conf; + return '' if $self->disabled; # so that disabling locations never fails + my $error = $self->ut_numbern('locationnum') || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum') @@ -677,6 +679,13 @@ sub process_censustract_update { return; } +=item process_set_coord + +Queueable function to find and fill in coordinates for all locations that +lack them. Because this uses the Google Maps API, it's internally rate +limited and must run in a single process. + +=cut sub process_set_coord { my $job = shift; @@ -716,6 +725,67 @@ sub process_set_coord { return; } +=item process_standardize [ LOCATIONNUMS ] + +Performs address standardization on locations with unclean addresses, +using whatever method you have configured. If the standardize_* method +returns a I address match, the location will be updated. This is +always an in-place update (because the physical location is the same, +and is just being referred to by a more accurate name). + +Disabled locations will be skipped, as nobody cares. + +If any LOCATIONNUMS are provided, only those locations will be updated. + +=cut + +sub process_standardize { + my $job = shift; + my @others = qsearch('queue', { + 'status' => 'locked', + 'job' => $job->job, + 'jobnum' => {op=>'!=', value=>$job->jobnum}, + }); + return if @others; + my @locationnums = grep /^\d+$/, @_; + my $where = "AND locationnum IN(".join(',',@locationnums).")" + if scalar(@locationnums); + my @locations = qsearch({ + table => 'cust_location', + hashref => { addr_clean => '', disabled => '' }, + extra_sql => $where, + }); + my $n_todo = scalar(@locations); + my $n_done = 0; + + # special: log this + my $log; + eval "use Text::CSV"; + open $log, '>', "$FS::UID::cache_dir/process_standardize-" . + time2str('%Y%m%d',time) . + ".csv"; + my $csv = Text::CSV->new({binary => 1, eol => "\n"}); + + foreach my $cust_location (@locations) { + $job->update_statustext( int(100 * $n_done/$n_todo) . ",$n_done / $n_todo locations" ) if $job; + my $result = FS::GeocodeCache->standardize($cust_location); + if ( $result->{addr_clean} and !$result->{error} ) { + my @cols = ($cust_location->locationnum); + foreach (keys %$result) { + push @cols, $cust_location->get($_), $result->{$_}; + $cust_location->set($_, $result->{$_}); + } + # bypass immutable field restrictions + my $error = $cust_location->FS::Record::replace; + warn "location ".$cust_location->locationnum.": $error\n" if $error; + $csv->print($log, \@cols); + } + $n_done++; + dbh->commit; # so that we can resume if interrupted + } + close $log; +} + =head1 BUGS =head1 SEE ALSO