RT# 73422 Fix report Customer Contacts
[freeside.git] / httemplate / misc / xmlhttp-address_standardize.html
1 <% encode_json($return) %>\
2 <%init>
3
4 local $SIG{__DIE__}; #disable Mason error trap
5
6 my $DEBUG = 0;
7
8 my $conf = new FS::Conf;
9
10 my $sub = $cgi->param('sub');
11
12 warn $cgi->param('arg') if $DEBUG;
13
14 my %old = %{ decode_json($cgi->param('arg')) }
15   or die "bad argument '".$cgi->param('arg')."'";
16
17 my %new;
18
19 my @prefixes = ('');
20 if ( $old{same} ) {
21   @prefixes = ('bill_');
22 } elsif ( $old{billship} ) {
23   @prefixes = ('bill_', 'ship_');
24 }
25 my $all_same = 1;
26 my $all_clean = 1;
27 foreach my $pre ( @prefixes ) {
28
29   my $location = {
30     map { $_ => $old{$pre.$_} }
31       qw( company address1 address2 city state zip country )
32   };
33
34   my $cache = eval { FS::GeocodeCache->standardize($location) };
35   $cache->set_coord;
36   # don't do set_censustract here, though censustract may be set by now
37
38   foreach ( keys(%$cache) ) {
39     $new{$pre.$_} = $cache->get($_);
40   }
41
42   foreach ( qw(address1 address2 city state zip country) ) {
43     $all_same = 0 if ( $new{$pre.$_} ne $old{$pre.$_} );
44     last if !$all_same;
45   }
46
47   $all_same = 0 if ( length( $old{$pre.'censustract'} ) > 0 &&
48                      length( $new{$pre.'censustract'} ) > 0 &&
49                      $old{$pre.'censustract'} ne $new{$pre.'censustract'} );
50
51   $all_same = 0 if $new{$pre.'error'};
52
53   $all_clean = 0 if !$new{$pre.'addr_clean'};
54 }
55
56 my $return = { old => \%old,
57                new => \%new,
58                all_same => $all_same,
59                all_clean => $all_clean };
60 warn "result:\n".encode_json($return) if $DEBUG;
61 </%init>