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
55 location_type location_number location_kind );
58 =item location_label [ OPTION => VALUE ... ]
60 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
68 used to separate the address elements (defaults to ', ')
72 a callback used for escaping the text of the address elements
82 my $separator = $opt{join_string} || ', ';
83 my $escape = $opt{escape_function} || sub{ shift };
84 my $ds = $opt{double_space} || ' ';
87 $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
88 my $prefix = $self->has_ship_address ? 'ship_' : '';
91 foreach (qw ( address1 address2 location_type location_number ) ) {
92 my $method = "$prefix$_";
93 $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
98 foreach (qw ( city county state zip ) ) {
99 my $method = "$prefix$_";
100 if ( $self->$method ) {
101 $line .= ' (' if $method eq 'county';
102 $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
103 $line .= ' )' if $method eq 'county';
107 $line .= $separator. &$escape(code2country($self->country))
108 if $self->country ne $cydefault;
113 =item geocode DATA_VENDOR
115 Returns a value for the customer location as encoded by DATA_VENDOR.
116 Currently this only makes sense for "CCH" as DATA_VENDOR.
121 my ($self, $data_vendor) = (shift, shift); #always cch for now
123 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
124 return $geocode if $geocode;
127 ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
131 my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
132 if $self->country eq 'US';
136 #CCH specific location stuff
137 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
139 my @cust_tax_location =
141 'table' => 'cust_tax_location',
142 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
143 'extra_sql' => $extra_sql,
144 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
147 $geocode = $cust_tax_location[0]->geocode
148 if scalar(@cust_tax_location);
159 L<FS::Record>, schema.html from the base documentation.