default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / misc / xmlhttp-wa_state-find_district_for_address.html
1 <%doc>
2
3 Expects POST keys:
4 * address1
5 * address2
6 * city
7 * state
8 * zip
9 * country
10
11 Also accepts all the above keys with the prefixes bill_ and ship_
12
13 Returns json with a key for each given address, e.g., whose value is
14 the tax district number. e.g.
15 {
16   address: {
17     district: 1234
18     tax: 7.9
19     exempt_amount: 0
20     city: MOXEE CITY
21     state: WA
22   },
23   bill: {
24     error: district not found
25     district:
26   },
27 }
28
29 </%doc>
30 <% encode_json($return) %>
31 <%init>
32 use Data::Dumper;
33 use FS::Misc::Geo;
34
35 http_header('Content-Type' => 'application/json');
36
37 my $DEBUG = 0;
38 my %param = ( $cgi->Vars );
39 my $return = {};
40
41 warn '$param: '.Dumper( \%param )."\n"
42   if $DEBUG;
43
44 my %address;
45 for my $prefix ( '', 'bill', 'ship' ) {
46   my $addr_key = $prefix || 'address';
47   $address{$addr_key} = {};
48   $address{$addr_key}->{$_} = $param{ $prefix ? "${prefix}_${_}" : $_ }
49     for qw/ address1 address2 city state zip country /;
50   delete $address{$addr_key}
51     unless $address{$addr_key}->{address1}
52         && $address{$addr_key}->{city};
53 }
54 warn Dumper( \%address )
55   if $DEBUG;
56
57 for my $k ( keys %address ) {
58   next unless lc $address{$k}->{state} eq 'wa';
59   my $response = FS::Misc::Geo::wa_sales( $address{$k} );
60   warn Dumper( $response )
61     if $DEBUG;
62
63   if ( ref $response ) {
64     $return->{$k} = $response;
65   } else {
66     $return->{$k} = { error => 'Lookup Failed' };
67   }
68 }
69
70 unless ( keys %$return ) {
71   $return->{error} = 'No WA addresses passed for lookup - nothing to do';
72 }
73
74 warn '$return: '.Dumper( $return )."\n"
75   if $DEBUG;
76 </%init>