qualification address handling changes, RT#7111
[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   if ( $self->get($prefix.'location_type') ) {
100     my %location_type;
101     if ( 1 ) { #ikano, switch on via config
102       { no warnings 'void';
103         eval { 'use FS::part_export::ikano;' };
104         die $@ if $@;
105       }
106       %location_type = FS::part_export::ikano->location_types;
107     } else {
108       %location_type = (); #?
109     }
110
111     $line .= ' '.&$escape( $location_type{ $self->get($prefix.'location_type') }
112                                        ||  $self->get($prefix.'location_type')
113                          );
114   }
115
116   $line .= ' '. &$escape($self->get($prefix.'location_number'))
117     if $self->get($prefix.'location_number');
118
119   $notfirst = 0;
120   foreach (qw ( city county state zip ) ) {
121     my $method = "$prefix$_";
122     if ( $self->$method ) {
123       $line .= ' (' if $method eq 'county';
124       $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
125       $line .= ' )' if $method eq 'county';
126       $notfirst++;
127     }
128   }
129   $line .= $separator. &$escape(code2country($self->country))
130     if $self->country ne $cydefault;
131
132   $line;
133 }
134
135 =item geocode DATA_VENDOR
136
137 Returns a value for the customer location as encoded by DATA_VENDOR.
138 Currently this only makes sense for "CCH" as DATA_VENDOR.
139
140 =cut
141
142 sub geocode {
143   my ($self, $data_vendor) = (shift, shift);  #always cch for now
144
145   my $geocode = $self->get('geocode');  #XXX only one data_vendor for geocode
146   return $geocode if $geocode;
147
148   my $prefix =
149    ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
150    ? 'ship_'
151    : '';
152
153   my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
154     if $self->country eq 'US';
155
156   $zip ||= '';
157   $plus4 ||= '';
158   #CCH specific location stuff
159   my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
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   $geocode;
173 }
174
175 =item alternize
176
177 Attempts to parse data for location_type and location_number from address1
178 and address2.
179
180 =cut
181
182 sub alternize {
183   my $self = shift;
184   my $prefix = $self->has_ship_address ? 'ship_' : '';
185
186   return '' if $self->get($prefix.'location_type')
187             || $self->get($prefix.'location_number');
188
189   my %parse;
190   if ( 1 ) { #ikano, switch on via config
191     { no warnings 'void';
192       eval { 'use FS::part_export::ikano;' };
193       die $@ if $@;
194     }
195     %parse = FS::part_export::ikano->location_types_parse;
196   } else {
197     %parse = (); #?
198   }
199
200   foreach my $from ('address1', 'address2') {
201     foreach my $parse ( keys %parse ) {
202       my $value = $self->get($prefix.$from);
203       if ( $value =~ s/(^|\W+)$parse\W+(\w+)\W*$//i ) {
204         $self->set($prefix.'location_type', $parse{$parse});
205         $self->set($prefix.'location_number', $2);
206         $self->set($prefix.$from, $value);
207         return '';
208       }
209     }
210   }
211
212   #nothing matched, no changes
213   $self->get($prefix.'address2')
214     ? "Can't parse unit type and number from ${prefix}address2"
215     : '';
216 }
217
218 =back
219
220 =head1 BUGS
221
222 =head1 SEE ALSO
223
224 L<FS::Record>, schema.html from the base documentation.
225
226 =cut
227
228 1;
229