eWay self-signup fixes
[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.  The shipping address is used if present.
42
43 =cut
44
45 #geocode dependent on tax-ship_address config
46
47 sub location_hash {
48   my $self = shift;
49   my $prefix = $self->has_ship_address ? 'ship_' : '';
50
51   map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
52         $_ => $self->get($method);
53       }
54       qw( address1 address2 city county state zip country geocode 
55         location_type location_number location_kind );
56 }
57
58 =item location_label [ OPTION => VALUE ... ]
59
60 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
61
62 Options are
63
64 =over 4
65
66 =item join_string
67
68 used to separate the address elements (defaults to ', ')
69
70 =item escape_function
71
72 a callback used for escaping the text of the address elements
73
74 =back
75
76 =cut
77
78 sub location_label {
79   my $self = shift;
80   my %opt = @_;
81
82   my $separator = $opt{join_string} || ', ';
83   my $escape = $opt{escape_function} || sub{ shift };
84   my $ds = $opt{double_space} || '  ';
85   my $line = '';
86   my $cydefault = 
87     $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
88   my $prefix = $self->has_ship_address ? 'ship_' : '';
89
90   my $notfirst = 0;
91   foreach (qw ( address1 address2 location_type location_number ) ) {
92     my $method = "$prefix$_";
93     $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
94       if $self->$method;
95     $notfirst++;
96   }
97   $notfirst = 0;
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';
104       $notfirst++;
105     }
106   }
107   $line .= $separator. &$escape(code2country($self->country))
108     if $self->country ne $cydefault;
109
110   $line;
111 }
112
113 =item geocode DATA_VENDOR
114
115 Returns a value for the customer location as encoded by DATA_VENDOR.
116 Currently this only makes sense for "CCH" as DATA_VENDOR.
117
118 =cut
119
120 sub geocode {
121   my ($self, $data_vendor) = (shift, shift);  #always cch for now
122
123   my $geocode = $self->get('geocode');  #XXX only one data_vendor for geocode
124   return $geocode if $geocode;
125
126   my $prefix =
127    ( FS::Conf->new->exists('tax-ship_address') && $self->has_ship_address )
128    ? 'ship_'
129    : '';
130
131   my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
132     if $self->country eq 'US';
133
134   $zip ||= '';
135   $plus4 ||= '';
136   #CCH specific location stuff
137   my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
138
139   my @cust_tax_location =
140     qsearch( {
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
145              }
146            );
147   $geocode = $cust_tax_location[0]->geocode
148     if scalar(@cust_tax_location);
149
150   $geocode;
151 }
152
153 =back
154
155 =head1 BUGS
156
157 =head1 SEE ALSO
158
159 L<FS::Record>, schema.html from the base documentation.
160
161 =cut
162
163 1;
164