4 use base qw( Exporter );
5 use vars qw( $DEBUG @EXPORT_OK );
8 use HTTP::Request::Common qw( GET POST );
16 @EXPORT_OK = qw( get_censustract get_district );
20 FS::Misc::Geo - routines to fetch geographic information
26 =item get_censustract LOCATION YEAR
28 Given a location hash (see L<FS::location_Mixin>) and a census map year,
29 returns a census tract code (consisting of state, county, and tract
30 codes) or an error message.
38 warn Dumper($location, $year) if $DEBUG;
40 my $url = 'http://www.ffiec.gov/Geocode/default.aspx';
45 my $ua = new LWP::UserAgent('cookie_jar' => HTTP::Cookies->new);
46 my $res = $ua->request( GET( $url ) );
51 if (!$res->is_success) {
53 $error = $res->message;
57 my $content = $res->content;
58 my $p = new HTML::TokeParser \$content;
61 while (my $token = $p->get_tag('input') ) {
62 if ($token->[1]->{name} eq '__VIEWSTATE') {
63 $viewstate = $token->[1]->{value};
65 if ($token->[1]->{name} eq '__EVENTVALIDATION') {
66 $eventvalidation = $token->[1]->{value};
68 last if $viewstate && $eventvalidation;
71 if (!$viewstate or !$eventvalidation ) {
73 $error = "either no __VIEWSTATE or __EVENTVALIDATION found";
77 my($zip5, $zip4) = split('-',$location->{zip});
81 __VIEWSTATE => $viewstate,
82 __EVENTVALIDATION => $eventvalidation,
83 __VIEWSTATEENCRYPTED => '',
85 txtAddress => $location->{address1},
86 txtCity => $location->{city},
87 ddlbState => $location->{state},
89 btnSearch => 'Search',
91 warn join("\n", @ffiec_args )
94 push @{ $ua->requests_redirectable }, 'POST';
95 $res = $ua->request( POST( $url, \@ffiec_args ) );
99 unless ($res->code eq '200') {
101 $error = $res->message;
105 my @id = qw( MSACode StateCode CountyCode TractCode );
106 $content = $res->content;
107 warn $res->content if $DEBUG > 1;
108 $p = new HTML::TokeParser \$content;
109 my $prefix = 'UcGeoResult11_lb';
111 sub { my $t=shift; scalar( grep { lc($t) eq lc("$prefix$_")} @id ) };
113 while (my $token = $p->get_tag('span') ) {
114 next unless ( $token->[1]->{id} && &$compare( $token->[1]->{id} ) );
115 $token->[1]->{id} =~ /^$prefix(\w+)$/;
116 $return->{lc($1)} = $p->get_trimmed_text("/span");
119 unless ( $return->{tractcode} ) {
120 warn "$error: $content ". Dumper($return) if $DEBUG;
121 $error = "No census tract found";
123 $return->{tractcode} .= ' '
124 unless $error || $JSON::VERSION >= 2; #broken JSON 1 workaround
126 } #unless ($res->code eq '200')
128 } #unless ($viewstate)
130 } #unless ($res->code eq '200')
132 return "FFIEC Geocoding error: $error" if $error;
134 $return->{'statecode'} . $return->{'countycode'} . $return->{'tractcode'};
137 sub get_district_methods {
139 'wa_sales' => 'Washington sales tax',
142 =item get_district LOCATION METHOD
144 For the location hash in LOCATION, using lookup method METHOD, fetch
145 tax district information. Currently the only available method is
146 'wa_sales' (the Washington Department of Revenue sales tax lookup).
148 Returns a hash reference containing the following fields:
153 - exempt_amount (currently zero)
154 - city, county, state, country (from
156 The intent is that you can assign this to an L<FS::cust_main_county>
157 object and insert it if there's not yet a tax rate defined for that
160 get_district will die on error.
168 my $location = shift;
169 my $method = shift or return '';
170 warn Dumper($location, $method) if $DEBUG;
175 my $location = shift;
177 return '' if $location->{state} ne 'WA';
179 my $return = { %$location };
180 $return->{'exempt_amount'} = 0.00;
182 my $url = 'http://webgis2.dor.wa.gov/TaxRateLookup_AGS/TaxReport.aspx';
183 my $ua = new LWP::UserAgent;
185 my $delim = '<|>'; # yes, <|>
186 my $year = (localtime)[5] + 1900;
187 my $month = (localtime)[4] + 1;
188 my @zip = split('-', $location->{zip});
191 'TaxType=S', #sales; 'P' = property
192 'Src=0', #does something complicated
194 'Addr='.uri_escape($location->{address1}),
195 'City='.uri_escape($location->{city}),
197 'Zip1='.($zip[1] || ''), #optional
204 my $query_string = join($delim, @args );
205 $url .= "?$query_string";
206 warn "\nrequest: $url\n\n" if $DEBUG;
208 my $res = $ua->request( GET( "$url?$query_string" ) );
213 if ($res->code ne '200') {
214 $error = $res->message;
217 my $content = $res->content;
218 my $p = new HTML::TokeParser \$content;
220 while ( my $t = $p->get_tag('script') ) {
221 my $u = $p->get_token; #either enclosed text or the </script> tag
222 if ( $u->[0] eq 'T' and $u->[1] =~ /tblSales/ ) {
227 if ( $js ) { #found it
228 # strip down to the quoted string, which contains escaped single quotes.
229 $js =~ s/.*\('tblSales'\);c.innerHTML='//s;
230 $js =~ s/(?<!\\)'.*//s; # (?<!\\) means "not preceded by a backslash"
231 warn "\n\n innerHTML:\n$js\n\n" if $DEBUG > 2;
233 $p = new HTML::TokeParser \$js;
234 TD: while ( my $td = $p->get_tag('td') ) {
235 while ( my $u = $p->get_token ) {
236 next TD if $u->[0] eq 'E' and $u->[1] eq 'td';
237 next if $u->[0] ne 'T'; # skip non-text
240 if ( lc($text) eq 'location code' ) {
241 $p->get_tag('td'); # skip to the next column
243 $u = $p->get_token until $u->[0] eq 'T'; # and then skip non-text
244 $return->{'district'} = $u->[1];
246 elsif ( lc($text) eq 'total tax rate' ) {
249 $u = $p->get_token until $u->[0] eq 'T';
250 $return->{'tax'} = $u->[1];
256 if ( $return->{'district'} =~ /^\d+$/ and $return->{'tax'} =~ /^.\d+$/ ) {
257 $return->{'tax'} *= 100; #percentage
258 warn Dumper($return) if $DEBUG;
262 $error = 'district code/tax rate not found';
266 $error = "failed to parse document";
269 die "WA tax district lookup error: $error";