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