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 my $lt = $self->get($prefix.'location_type');
102 if ( 1 ) { #ikano, switch on via config
103 { no warnings 'void';
104 eval { 'use FS::part_export::ikano;' };
107 %location_type = FS::part_export::ikano->location_types;
109 %location_type = (); #?
112 $line .= ' '.&$escape( $location_type{$lt} || $lt );
115 $line .= ' '. &$escape($self->get($prefix.'location_number'))
116 if $self->get($prefix.'location_number');
119 foreach (qw ( city county state zip ) ) {
120 my $method = "$prefix$_";
121 if ( $self->$method ) {
122 $line .= ' (' if $method eq 'county';
123 $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
124 $line .= ' )' if $method eq 'county';
128 $line .= $separator. &$escape(code2country($self->country))
129 if $self->country ne $cydefault;
134 =item geocode DATA_VENDOR
136 Returns a value for the customer location as encoded by DATA_VENDOR.
137 Currently this only makes sense for "CCH" as DATA_VENDOR.
142 my ($self, $data_vendor) = (shift, shift); #always cch for now
144 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
145 return $geocode if $geocode;
148 ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
152 my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
153 if $self->country eq 'US';
157 #CCH specific location stuff
158 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
160 my @cust_tax_location =
162 'table' => 'cust_tax_location',
163 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
164 'extra_sql' => $extra_sql,
165 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
168 $geocode = $cust_tax_location[0]->geocode
169 if scalar(@cust_tax_location);
180 L<FS::Record>, schema.html from the base documentation.