1 package FS::geocode_Mixin;
4 use vars qw( $DEBUG $me );
7 use FS::Record qw( qsearchs qsearch );
10 use FS::cust_tax_location;
14 $me = '[FS::geocode_Mixin]';
18 FS::geocode_Mixin - Mixin class for records that contain address and other
23 package FS::some_table;
24 use base ( FS::geocode_Mixin FS::Record );
28 FS::geocode_Mixin - This is a mixin class for records that contain address
29 and other location fields.
39 Returns a list of key/value pairs, with the following keys: address1, address2,
40 city, county, state, zip, country. The shipping address is used if present.
44 #geocode dependent on tax-ship_address config
48 my $prefix = $self->has_ship_address ? 'ship_' : '';
50 map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
51 $_ => $self->get($method);
53 qw( address1 address2 city county state zip country geocode );
56 =item location_label [ OPTION => VALUE ... ]
58 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
66 used to separate the address elements (defaults to ', ')
70 a callback used for escaping the text of the address elements
80 my $separator = $opt{join_string} || ', ';
81 my $escape = $opt{escape_function} || sub{ shift };
82 my $ds = $opt{double_space} || ' ';
85 $opt{'countrydefault'} || FS::conf->new->config('countrydefault') || 'US';
86 my $prefix = $self->has_ship_address ? 'ship_' : '';
89 foreach (qw ( address1 address2 ) ) {
90 my $method = "$prefix$_";
91 $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
96 foreach (qw ( city county state zip ) ) {
97 my $method = "$prefix$_";
98 if ( $self->$method ) {
99 $line .= ' (' if $method eq 'county';
100 $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
101 $line .= ' )' if $method eq 'county';
105 $line .= $separator. &$escape(code2country($self->country))
106 if $self->country ne $cydefault;
111 =item geocode DATA_VENDOR
113 Returns a value for the customer location as encoded by DATA_VENDOR.
114 Currently this only makes sense for "CCH" as DATA_VENDOR.
119 my ($self, $data_vendor) = (shift, shift); #always cch for now
121 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
122 return $geocode if $geocode;
125 ( FS::conf->new->exists('tax-ship_address') && $self->has_ship_address )
129 my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
130 if $self->country eq 'US';
134 #CCH specific location stuff
135 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
137 my @cust_tax_location =
139 'table' => 'cust_tax_location',
140 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
141 'extra_sql' => $extra_sql,
142 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
145 $geocode = $cust_tax_location[0]->geocode
146 if scalar(@cust_tax_location);
157 L<FS::Record>, schema.html from the base documentation.