summaryrefslogtreecommitdiff
path: root/httemplate/misc/xmlhttp-wa_state-find_district_for_address.html
blob: e59789bd1b571c2a853ecf1884f2a80fbc8e0647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<%doc>

Expects POST keys:
* address1
* address2
* city
* state
* zip
* country

Also accepts all the above keys with the prefixes bill_ and ship_

Returns json with a key for each given address, e.g., whose value is
the tax district number. e.g.
{
  address: {
    district: 1234
    tax: 7.9
    exempt_amount: 0
    city: MOXEE CITY
    state: WA
  },
  bill: {
    error: district not found
    district:
  },
}

</%doc>
<% encode_json($return) %>
<%init>
use Data::Dumper;
use FS::Misc::Geo;

http_header('Content-Type' => 'application/json');

my $DEBUG = 0;
my %param = ( $cgi->Vars );
my $return = {};

warn '$param: '.Dumper( \%param )."\n"
  if $DEBUG;

my %address;
for my $prefix ( '', 'bill', 'ship' ) {
  my $addr_key = $prefix || 'address';
  $address{$addr_key} = {};
  $address{$addr_key}->{$_} = $param{ $prefix ? "${prefix}_${_}" : $_ }
    for qw/ address1 address2 city state zip country /;
  delete $address{$addr_key}
    unless $address{$addr_key}->{address1}
        && $address{$addr_key}->{city};
}
warn Dumper( \%address )
  if $DEBUG;

for my $k ( keys %address ) {
  next unless lc $address{$k}->{state} eq 'wa';
  my $response = FS::Misc::Geo::wa_sales( $address{$k} );
  warn Dumper( $response )
    if $DEBUG;

  if ( ref $response ) {
    $return->{$k} = $response;
  } else {
    $return->{$k} = { error => 'Lookup Failed' };
  }
}

unless ( keys %$return ) {
  $return->{error} = 'No WA addresses passed for lookup - nothing to do';
}

warn '$return: '.Dumper( $return )."\n"
  if $DEBUG;
</%init>