1 package FS::geocode_Mixin;
4 use vars qw( $DEBUG $me );
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(code2country($self->country))
130 if $self->country ne $cydefault;
141 my $pre = scalar(@_) ? shift : '';
143 #my $module = FS::Conf->new->config('geocode_module') || 'Geo::Coder::Googlev3';
145 my $geocoder = Geo::Coder::Googlev3->new;
147 my $location = eval {
148 $geocoder->geocode( location =>
149 $self->get($pre.'address1'). ','.
150 ( $self->get($pre.'address2') ? $self->get($pre.'address2').',' : '' ).
151 $self->get($pre.'city'). ','.
152 $self->get($pre.'state'). ','.
153 code2country($self->get($pre.'country'))
157 warn "geocoding error: $@\n";
161 my $geo_loc = $location->{'geometry'}{'location'} or return;
162 if ( $geo_loc->{'lat'} && $geo_loc->{'lng'} ) {
163 $self->set($pre.'latitude', $geo_loc->{'lat'} );
164 $self->set($pre.'longitude', $geo_loc->{'lng'} );
165 $self->set($pre.'coord_auto', 'Y');
170 =item geocode DATA_VENDOR
172 Returns a value for the customer location as encoded by DATA_VENDOR.
173 Currently this only makes sense for "CCH" as DATA_VENDOR.
178 my ($self, $data_vendor) = (shift, shift); #always cch for now
180 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
181 return $geocode if $geocode;
184 ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
188 my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
189 if $self->country eq 'US';
193 #CCH specific location stuff
194 my $extra_sql = $plus4 ? "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'"
197 my @cust_tax_location =
199 'table' => 'cust_tax_location',
200 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
201 'extra_sql' => $extra_sql,
202 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
205 $geocode = $cust_tax_location[0]->geocode
206 if scalar(@cust_tax_location);
208 warn "WARNING: customer ". $self->custnum.
209 ": multiple locations for zip ". $self->get("${prefix}zip").
210 "; using arbitrary geocode $geocode\n"
211 if scalar(@cust_tax_location) > 1;
222 L<FS::Record>, schema.html from the base documentation.