diff options
Diffstat (limited to 'httemplate/misc/xmlhttp-cust_main-address_standardize.html')
-rw-r--r-- | httemplate/misc/xmlhttp-cust_main-address_standardize.html | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/httemplate/misc/xmlhttp-cust_main-address_standardize.html b/httemplate/misc/xmlhttp-cust_main-address_standardize.html new file mode 100644 index 000000000..3b9e142f5 --- /dev/null +++ b/httemplate/misc/xmlhttp-cust_main-address_standardize.html @@ -0,0 +1,92 @@ +<% objToJson($return) %> +<%init> + +my $DEBUG = 0; + +my $conf = new FS::Conf; + +my $sub = $cgi->param('sub'); + +my $return = {}; + +if ( $sub eq 'address_standardize' ) { + + my %arg = $cgi->param('arg'); + $return = \%arg; + warn join('', map "$_: $arg{$_}\n", keys %arg ) + if $DEBUG; + + my $userid = $conf->config('usps_webtools-userid'); + my $password = $conf->config('usps_webtools-password'); + + if ( length($userid) && length($password) ) { + + my $verifier = Business::US::USPS::WebTools::AddressStandardization->new( { + UserID => $userid, #$ENV{USPS_WEBTOOLS_USERID}, + Password => $password, #$ENV{USPS_WEBTOOLS_PASSWORD}, + #Testing => 1, + } ); + + foreach my $pre ( '', 'ship_' ) { + + my($zip5, $zip4) = split('-',$arg{$pre.'zip'}); + + my %usps_args = ( + FirmName => $arg{$pre.'company'}, + Address2 => $arg{$pre.'address1'}, + Address1 => $arg{$pre.'address2'}, + City => $arg{$pre.'city'}, + State => $arg{$pre.'state'}, + Zip5 => $zip5, + Zip4 => $zip4, + ); + warn join('', map "$_: $usps_args{$_}\n", keys %usps_args ) + if $DEBUG; + + my $hash = $verifier->verify_address( %usps_args ); + + warn $verifier->response + if $DEBUG; + + unless ( $verifier->is_error ) { + + my $zip = $hash->{Zip5}; + $zip .= '-'. $hash->{Zip4} if $hash->{Zip4} =~ /\d/; + + $return = { + %$return, + "new_$pre".'company' => $hash->{FirmName}, + "new_$pre".'address1' => $hash->{Address2}, + "new_$pre".'address2' => $hash->{Address1}, + "new_$pre".'city' => $hash->{City}, + "new_$pre".'state' => $hash->{State}, + "new_$pre".'zip' => $zip, + }; + + my @fields = (qw( company address1 address2 city state zip )); #hmm + + my $changed = + scalar( grep { $return->{$pre.$_} ne $return->{"new_$pre$_"} } + @fields + ) + ? 1 : 0; + + $return->{$pre.'address_standardized'} = $changed; + + } else { + + $return->{$pre.'error'} = "USPS WebTools error: ". + $verifier->{error}{description}; + + + } + + } + + } + + $return; + +} + +</%init> |