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. The shipping address is used if present.
45 #geocode dependent on tax-ship_address config
49 my $prefix = $self->has_ship_address ? 'ship_' : '';
51 map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
52 $_ => $self->get($method);
54 qw( address1 address2 city county state zip country geocode );
57 =item location_label [ OPTION => VALUE ... ]
59 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
67 used to separate the address elements (defaults to ', ')
71 a callback used for escaping the text of the address elements
81 my $separator = $opt{join_string} || ', ';
82 my $escape = $opt{escape_function} || sub{ shift };
83 my $ds = $opt{double_space} || ' ';
86 $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
87 my $prefix = $self->has_ship_address ? 'ship_' : '';
90 foreach (qw ( address1 address2 ) ) {
91 my $method = "$prefix$_";
92 $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
97 foreach (qw ( city county state zip ) ) {
98 my $method = "$prefix$_";
99 if ( $self->$method ) {
100 $line .= ' (' if $method eq 'county';
101 $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
102 $line .= ' )' if $method eq 'county';
106 $line .= $separator. &$escape(code2country($self->country))
107 if $self->country ne $cydefault;
112 =item geocode DATA_VENDOR
114 Returns a value for the customer location as encoded by DATA_VENDOR.
115 Currently this only makes sense for "CCH" as DATA_VENDOR.
120 my ($self, $data_vendor) = (shift, shift); #always cch for now
122 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
123 return $geocode if $geocode;
126 ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
130 my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
131 if $self->country eq 'US';
135 #CCH specific location stuff
136 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
138 my @cust_tax_location =
140 'table' => 'cust_tax_location',
141 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
142 'extra_sql' => $extra_sql,
143 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
146 $geocode = $cust_tax_location[0]->geocode
147 if scalar(@cust_tax_location);
158 L<FS::Record>, schema.html from the base documentation.