c1b9f865b7cde9f3b4d31c145cf8a5c519c4dd26
[freeside.git] / FS / FS / geocode_Mixin.pm
1 package FS::geocode_Mixin;
2
3 use strict;
4 use vars qw( $DEBUG $me );
5 use Carp;
6 use Locale::Country;
7 use FS::Record qw( qsearchs qsearch );
8 use FS::Conf;
9 use FS::cust_pkg;
10 use FS::cust_location;
11 use FS::cust_tax_location;
12 use FS::part_pkg;
13
14 $DEBUG = 0;
15 $me = '[FS::geocode_Mixin]';
16
17 =head1 NAME
18
19 FS::geocode_Mixin - Mixin class for records that contain address and other
20 location fields.
21
22 =head1 SYNOPSIS
23
24   package FS::some_table;
25   use base ( FS::geocode_Mixin FS::Record );
26
27 =head1 DESCRIPTION
28
29 FS::geocode_Mixin - This is a mixin class for records that contain address
30 and other location fields.
31
32 =head1 METHODS
33
34 =over 4
35
36 =cut
37
38 =item location_hash
39
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.
43
44 =cut
45
46 #geocode dependent on tax-ship_address config
47
48 sub location_hash {
49   my $self = shift;
50   my $prefix = $self->has_ship_address ? 'ship_' : '';
51
52   map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
53         $_ => $self->get($method);
54       }
55       qw( address1 address2 city county state zip country geocode 
56         location_type location_number location_kind );
57 }
58
59 =item location_label [ OPTION => VALUE ... ]
60
61 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
62
63 Options are
64
65 =over 4
66
67 =item join_string
68
69 used to separate the address elements (defaults to ', ')
70
71 =item escape_function
72
73 a callback used for escaping the text of the address elements
74
75 =back
76
77 =cut
78
79 sub location_label {
80   my $self = shift;
81   my %opt = @_;
82
83   my $separator = $opt{join_string} || ', ';
84   my $escape = $opt{escape_function} || sub{ shift };
85   my $ds = $opt{double_space} || '  ';
86   my $line = '';
87   my $cydefault = 
88     $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
89   my $prefix = $self->has_ship_address ? 'ship_' : '';
90
91   my $notfirst = 0;
92   foreach (qw ( address1 address2 ) ) {
93     my $method = "$prefix$_";
94     $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
95       if $self->$method;
96     $notfirst++;
97   }
98
99   my $lt = $self->get($prefix.'location_type');
100   if ( $lt ) {
101     my %location_type;
102     if ( 1 ) { #ikano, switch on via config
103       { no warnings 'void';
104         eval { 'use FS::part_export::ikano;' };
105         die $@ if $@;
106       }
107       %location_type = FS::part_export::ikano->location_types;
108     } else {
109       %location_type = (); #?
110     }
111
112     $line .= ' '.&$escape( $location_type{$lt} || $lt );
113   }
114
115   $line .= ' '. &$escape($self->get($prefix.'location_number'))
116     if $self->get($prefix.'location_number');
117
118   $notfirst = 0;
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';
125       $notfirst++;
126     }
127   }
128   $line .= $separator. &$escape(code2country($self->country))
129     if $self->country ne $cydefault;
130
131   $line;
132 }
133
134 =item geocode DATA_VENDOR
135
136 Returns a value for the customer location as encoded by DATA_VENDOR.
137 Currently this only makes sense for "CCH" as DATA_VENDOR.
138
139 =cut
140
141 sub geocode {
142   my ($self, $data_vendor) = (shift, shift);  #always cch for now
143
144   my $geocode = $self->get('geocode');  #XXX only one data_vendor for geocode
145   return $geocode if $geocode;
146
147   my $prefix =
148    ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
149    ? 'ship_'
150    : '';
151
152   my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
153     if $self->country eq 'US';
154
155   $zip ||= '';
156   $plus4 ||= '';
157   #CCH specific location stuff
158   my $extra_sql = $plus4 ? "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'"
159                          : '';
160
161   my @cust_tax_location =
162     qsearch( {
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
167              }
168            );
169   $geocode = $cust_tax_location[0]->geocode
170     if scalar(@cust_tax_location);
171
172   warn "WARNING: customer ". $self->custnum.
173        ": multiple locations for zip ". $self->get("${prefix}zip").
174        "; using arbitrary geocode $geocode\n"
175     if scalar(@cust_tax_location) > 1;
176
177   $geocode;
178 }
179
180 =back
181
182 =head1 BUGS
183
184 =head1 SEE ALSO
185
186 L<FS::Record>, schema.html from the base documentation.
187
188 =cut
189
190 1;
191