1 package FS::geocode_Mixin;
4 use vars qw( $DEBUG $me );
6 use Locale::Country ();
7 use Geo::Coder::Googlev3; #compile time for now, until others are supported
8 use FS::Record qw( qsearchs qsearch );
11 use FS::cust_location;
12 use FS::cust_tax_location;
16 $me = '[FS::geocode_Mixin]';
20 FS::geocode_Mixin - Mixin class for records that contain address and other
25 package FS::some_table;
26 use base ( FS::geocode_Mixin FS::Record );
30 FS::geocode_Mixin - This is a mixin class for records that contain address
31 and other location fields.
41 Returns a list of key/value pairs, with the following keys: address1, address2,
42 city, county, state, zip, country, geocode, location_type, location_number,
43 location_kind. The shipping address is used if present.
47 #geocode dependent on tax-ship_address config
51 my $prefix = $self->has_ship_address ? 'ship_' : '';
53 map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
54 $_ => $self->get($method);
56 qw( address1 address2 city county state zip country geocode
57 location_type location_number location_kind );
60 =item location_label [ OPTION => VALUE ... ]
62 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
70 used to separate the address elements (defaults to ', ')
74 a callback used for escaping the text of the address elements
84 my $separator = $opt{join_string} || ', ';
85 my $escape = $opt{escape_function} || sub{ shift };
86 my $ds = $opt{double_space} || ' ';
89 $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
90 my $prefix = $self->has_ship_address ? 'ship_' : '';
93 foreach (qw ( address1 address2 ) ) {
94 my $method = "$prefix$_";
95 $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
100 my $lt = $self->get($prefix.'location_type');
103 if ( 1 ) { #ikano, switch on via config
104 { no warnings 'void';
105 eval { 'use FS::part_export::ikano;' };
108 %location_type = FS::part_export::ikano->location_types;
110 %location_type = (); #?
113 $line .= ' '.&$escape( $location_type{$lt} || $lt );
116 $line .= ' '. &$escape($self->get($prefix.'location_number'))
117 if $self->get($prefix.'location_number');
120 foreach (qw ( city county state zip ) ) {
121 my $method = "$prefix$_";
122 if ( $self->$method ) {
123 $line .= ' (' if $method eq 'county';
124 $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
125 $line .= ' )' if $method eq 'county';
129 $line .= $separator. &$escape($self->country_full)
130 if $self->country ne $cydefault;
137 Returns the full country name.
143 $self->code2country($self->get('country'));
147 my( $self, $country ) = @_;
149 #a hash? not expecting an explosion of business from unrecognized countries..
150 return 'KKTC' if $country eq 'XC';
152 Locale::Country::code2country($country);
157 Look up the coordinates of the location using (currently) the Google Maps
158 API and set the 'latitude' and 'longitude' fields accordingly.
165 #my $module = FS::Conf->new->config('geocode_module') || 'Geo::Coder::Googlev3';
167 my $geocoder = Geo::Coder::Googlev3->new;
169 my $location = eval {
170 $geocoder->geocode( location =>
171 $self->get('address1'). ','.
172 ( $self->get('address2') ? $self->get('address2').',' : '' ).
173 $self->get('city'). ','.
174 $self->get('state'). ','.
179 warn "geocoding error: $@\n";
183 my $geo_loc = $location->{'geometry'}{'location'} or return;
184 if ( $geo_loc->{'lat'} && $geo_loc->{'lng'} ) {
185 $self->set('latitude', $geo_loc->{'lat'} );
186 $self->set('longitude', $geo_loc->{'lng'} );
187 $self->set('coord_auto', 'Y');
192 =item geocode DATA_VENDOR
194 Returns a value for the customer location as encoded by DATA_VENDOR.
195 Currently this only makes sense for "CCH" as DATA_VENDOR.
200 my ($self, $data_vendor) = (shift, shift); #always cch for now
202 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
203 return $geocode if $geocode;
205 if ( $self->isa('FS::cust_main') ) {
206 warn "WARNING: FS::cust_main->geocode deprecated";
209 my $m = FS::Conf->new->exists('tax-ship_address') ? 'ship_location'
211 my $location = $self->$m or return '';
212 return $location->geocode($data_vendor);
215 my($zip,$plus4) = split /-/, $self->get('zip')
216 if $self->country eq 'US';
220 #CCH specific location stuff
221 my $extra_sql = $plus4 ? "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'"
224 my @cust_tax_location =
226 'table' => 'cust_tax_location',
227 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
228 'extra_sql' => $extra_sql,
229 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
232 $geocode = $cust_tax_location[0]->geocode
233 if scalar(@cust_tax_location);
235 warn "WARNING: customer ". $self->custnum.
236 ": multiple locations for zip ". $self->get("zip").
237 "; using arbitrary geocode $geocode\n"
238 if scalar(@cust_tax_location) > 1;
243 =item process_district_update CLASS ID
245 Queueable function to update the tax district code using the selected method
246 (config 'tax_district_method'). CLASS is either 'FS::cust_main' or
247 'FS::cust_location'; ID is the key in one of those tables.
251 # this is run from the job queue so I'm not transactionizing it.
253 sub process_district_update {
259 eval "use FS::Misc::Geo qw(get_district); use FS::Conf; use $class;";
261 die "$class has no location data" if !$class->can('location_hash');
263 my $conf = FS::Conf->new;
264 my $method = $conf->config('tax_district_method')
265 or return; #nothing to do if null
266 my $self = $class->by_key($id) or die "object $id not found";
268 # dies on error, fine
269 my $tax_info = get_district({ $self->location_hash }, $method);
272 $self->set('district', $tax_info->{'district'} );
273 my $error = $self->replace;
274 die $error if $error;
276 my %hash = map { $_ => $tax_info->{$_} }
277 qw( district city county state country );
278 $hash{'source'} = $method; # apply the update only to taxes we maintain
280 my @old = qsearch('cust_main_county', \%hash);
282 # prune any duplicates rather than updating them
283 my %keep; # key => cust_main_county record
284 foreach my $cust_main_county (@old) {
285 my $key = join('.', $cust_main_county->city ,
286 $cust_main_county->district ,
287 $cust_main_county->taxclass
289 if ( exists $keep{$key} ) {
290 my $disable_this = $cust_main_county;
291 # prefer records that have a tax name
292 if ( $cust_main_county->taxname and not $keep{$key}->taxname ) {
293 $disable_this = $keep{$key};
294 $keep{$key} = $cust_main_county;
296 # disable by setting the rate to zero, and setting source to null
297 # so it doesn't get auto-updated in the future. don't actually
298 # delete it, that produces orphan records
299 warn "disabling tax rate #" .
300 $disable_this->taxnum .
301 " because it's a duplicate for $key\n"
303 # by setting its rate to zero, and never updating
305 $disable_this->set('tax' => 0);
306 $disable_this->set('source' => '');
307 $error = $disable_this->replace;
308 die $error if $error;
311 $keep{$key} ||= $cust_main_county;
314 foreach my $key (keys %keep) {
315 my $cust_main_county = $keep{$key};
316 warn "updating tax rate #".$cust_main_county->taxnum.
317 " for $key" if $DEBUG;
318 # update the tax rate only
319 $cust_main_county->set('tax', $tax_info->{'tax'});
320 $error ||= $cust_main_county->replace;
323 # make a new tax record, and mark it so we can find it later
324 $tax_info->{'source'} = $method;
325 my $new = new FS::cust_main_county $tax_info;
326 warn "creating tax rate for district ".$tax_info->{'district'} if $DEBUG;
327 $error = $new->insert;
329 die $error if $error;
341 L<FS::Record>, schema.html from the base documentation.