1 package FS::geocode_Mixin;
4 use vars qw( $DEBUG $me );
7 use FS::Record qw( qsearchs qsearch );
10 use FS::cust_location;
11 use FS::cust_tax_location;
15 $me = '[FS::geocode_Mixin]';
19 FS::geocode_Mixin - Mixin class for records that contain address and other
24 package FS::some_table;
25 use base ( FS::geocode_Mixin FS::Record );
29 FS::geocode_Mixin - This is a mixin class for records that contain address
30 and other location fields.
40 Returns a list of key/value pairs, with the following keys: address1, address2,
41 city, county, state, zip, country, geocode, location_type, location_number,
42 location_kind. The shipping address is used if present.
46 #geocode dependent on tax-ship_address config
50 my $prefix = $self->has_ship_address ? 'ship_' : '';
52 map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
53 $_ => $self->get($method);
55 qw( address1 address2 city county state zip country geocode
56 location_type location_number location_kind );
59 =item location_label [ OPTION => VALUE ... ]
61 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
69 used to separate the address elements (defaults to ', ')
73 a callback used for escaping the text of the address elements
83 my $separator = $opt{join_string} || ', ';
84 my $escape = $opt{escape_function} || sub{ shift };
85 my $ds = $opt{double_space} || ' ';
88 $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
89 my $prefix = $self->has_ship_address ? 'ship_' : '';
92 foreach (qw ( address1 address2 ) ) {
93 my $method = "$prefix$_";
94 $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
99 if ( $self->get($prefix.'location_type') ) {
101 if ( 1 ) { #ikano, switch on via config
102 { no warnings 'void';
103 eval { 'use FS::part_export::ikano;' };
106 %location_type = FS::part_export::ikano->location_types;
108 %location_type = (); #?
111 $line .= ' '.&$escape( $location_type{ $self->get($prefix.'location_type') }
112 || $self->get($prefix.'location_type')
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;
135 =item geocode DATA_VENDOR
137 Returns a value for the customer location as encoded by DATA_VENDOR.
138 Currently this only makes sense for "CCH" as DATA_VENDOR.
143 my ($self, $data_vendor) = (shift, shift); #always cch for now
145 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
146 return $geocode if $geocode;
149 ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
153 my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
154 if $self->country eq 'US';
158 #CCH specific location stuff
159 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
161 my @cust_tax_location =
163 'table' => 'cust_tax_location',
164 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
165 'extra_sql' => $extra_sql,
166 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
169 $geocode = $cust_tax_location[0]->geocode
170 if scalar(@cust_tax_location);
177 Attempts to parse data for location_type and location_number from address1
184 my $prefix = $self->has_ship_address ? 'ship_' : '';
186 return '' if $self->get($prefix.'location_type')
187 || $self->get($prefix.'location_number');
190 if ( 1 ) { #ikano, switch on via config
191 { no warnings 'void';
192 eval { 'use FS::part_export::ikano;' };
195 %parse = FS::part_export::ikano->location_types_parse;
200 foreach my $from ('address1', 'address2') {
201 foreach my $parse ( keys %parse ) {
202 my $value = $self->get($prefix.$from);
203 if ( $value =~ s/(^|\W+)$parse\W+(\w+)\W*$//i ) {
204 $self->set($prefix.'location_type', $parse{$parse});
205 $self->set($prefix.'location_number', $2);
206 $self->set($prefix.$from, $value);
212 #nothing matched, no changes
213 $self->get($prefix.'address2')
214 ? "Can't parse unit type and number from ${prefix}address2"
224 L<FS::Record>, schema.html from the base documentation.