external taxes support package locations RT10093
[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::cust_pkg;
9 use FS::cust_location;
10 use FS::cust_tax_location;
11 use FS::part_pkg;
12
13 $DEBUG = 0;
14 $me = '[FS::geocode_Mixin]';
15
16 =head1 NAME
17
18 FS::geocode_Mixin - Mixin class for records that contain address and other
19 location fields.
20
21 =head1 SYNOPSIS
22
23   package FS::some_table;
24   use base ( FS::geocode_Mixin FS::Record );
25
26 =head1 DESCRIPTION
27
28 FS::geocode_Mixin - This is a mixin class for records that contain address
29 and other location fields.
30
31 =head1 METHODS
32
33 =over 4
34
35 =cut
36
37 =item location_hash
38
39 Returns a list of key/value pairs, with the following keys: address1, address2,
40 city, county, state, zip, country.  The shipping address is used if present.
41
42 =cut
43
44 #geocode dependent on tax-ship_address config
45
46 sub location_hash {
47   my $self = shift;
48   my $prefix = $self->has_ship_address ? 'ship_' : '';
49
50   map { my $method = ($_ eq 'geocode') ? $_ : $prefix.$_;
51         $_ => $self->get($method);
52       }
53       qw( address1 address2 city county state zip country geocode );
54 }
55
56 =item location_label [ OPTION => VALUE ... ]
57
58 Returns the label of the service location (see analog in L<FS::cust_location>) for this customer.
59
60 Options are
61
62 =over 4
63
64 =item join_string
65
66 used to separate the address elements (defaults to ', ')
67
68 =item escape_function
69
70 a callback used for escaping the text of the address elements
71
72 =back
73
74 =cut
75
76 sub location_label {
77   my $self = shift;
78   my %opt = @_;
79
80   my $separator = $opt{join_string} || ', ';
81   my $escape = $opt{escape_function} || sub{ shift };
82   my $ds = $opt{double_space} || '  ';
83   my $line = '';
84   my $cydefault = 
85     $opt{'countrydefault'} || FS::conf->new->config('countrydefault') || 'US';
86   my $prefix = $self->has_ship_address ? 'ship_' : '';
87
88   my $notfirst = 0;
89   foreach (qw ( address1 address2 ) ) {
90     my $method = "$prefix$_";
91     $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
92       if $self->$method;
93     $notfirst++;
94   }
95   $notfirst = 0;
96   foreach (qw ( city county state zip ) ) {
97     my $method = "$prefix$_";
98     if ( $self->$method ) {
99       $line .= ' (' if $method eq 'county';
100       $line .= ($notfirst ? ' ' : $separator). &$escape($self->$method);
101       $line .= ' )' if $method eq 'county';
102       $notfirst++;
103     }
104   }
105   $line .= $separator. &$escape(code2country($self->country))
106     if $self->country ne $cydefault;
107
108   $line;
109 }
110
111 =item geocode DATA_VENDOR
112
113 Returns a value for the customer location as encoded by DATA_VENDOR.
114 Currently this only makes sense for "CCH" as DATA_VENDOR.
115
116 =cut
117
118 sub geocode {
119   my ($self, $data_vendor) = (shift, shift);  #always cch for now
120
121   my $geocode = $self->get('geocode');  #XXX only one data_vendor for geocode
122   return $geocode if $geocode;
123
124   my $prefix =
125    ( FS::conf->new->exists('tax-ship_address') && $self->has_ship_address )
126    ? 'ship_'
127    : '';
128
129   my($zip,$plus4) = split /-/, $self->get("${prefix}zip")
130     if $self->country eq 'US';
131
132   $zip ||= '';
133   $plus4 ||= '';
134   #CCH specific location stuff
135   my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
136
137   my @cust_tax_location =
138     qsearch( {
139                'table'     => 'cust_tax_location', 
140                'hashref'   => { 'zip' => $zip, 'data_vendor' => $data_vendor },
141                'extra_sql' => $extra_sql,
142                'order_by'  => 'ORDER BY plus4hi',#overlapping with distinct ends
143              }
144            );
145   $geocode = $cust_tax_location[0]->geocode
146     if scalar(@cust_tax_location);
147
148   $geocode;
149 }
150
151 =back
152
153 =head1 BUGS
154
155 =head1 SEE ALSO
156
157 L<FS::Record>, schema.html from the base documentation.
158
159 =cut
160
161 1;
162