address standardization/census tract fixes
authorMark Wells <mark@freeside.biz>
Tue, 13 Nov 2012 23:32:31 +0000 (15:32 -0800)
committerMark Wells <mark@freeside.biz>
Tue, 13 Nov 2012 23:32:31 +0000 (15:32 -0800)
FS/FS/Upgrade.pm
FS/FS/cust_main/Location.pm
httemplate/edit/cust_main/bottomfixup.js
httemplate/elements/standardize_locations.js
httemplate/misc/confirm-address_standardize.html
httemplate/misc/confirm-censustract.html
httemplate/misc/xmlhttp-address_standardize.html

index 3f76f51..45cba82 100644 (file)
@@ -84,6 +84,13 @@ sub upgrade_config {
     }
   }
 
+  # if there's a USPS tools login, assume that's the standardization method
+  # you want to use
+  if ( length($conf->config('usps_webtools-userid')) > 0 and
+       !$conf->exists('address_standardize_method') ) {
+    $conf->set('address_standardize_method', 'usps');
+  }
+
 }
 
 sub upgrade_overlimit_groups {
index 8e30bb6..ba3513b 100644 (file)
@@ -18,7 +18,8 @@ BEGIN {
     no strict 'refs';
     @location_fields = 
       qw( address1 address2 city county state zip country district
-        latitude longitude coord_auto censustract censusyear geocode );
+        latitude longitude coord_auto censustract censusyear geocode
+        addr_clean );
 
     foreach my $f (@location_fields) {
       *{"FS::cust_main::Location::$f"} = sub {
index 6fc7989..1cfa52d 100644 (file)
@@ -7,7 +7,8 @@ my $company_longitude = $conf->config('company_longitude');
 
 my @fixups = ('copy_payby_fields', 'standardize_locations');
 
-push @fixups, 'confirm_censustract';
+push @fixups, 'confirm_censustract'
+    if $conf->exists('cust_main-require_censustract');
 
 push @fixups, 'check_unique'
     if $conf->exists('cust_main-check_unique') and !$opt{'custnum'};
@@ -67,12 +68,11 @@ function copy_payby_fields() {
   submit_continue();
 }
 
-<% include( '/elements/standardize_locations.js',
-            'callback' => 'submit_continue();',
-            'main_prefix' => 'bill_',
-            'no_company' => 1,
-          )
-%>
+<& /elements/standardize_locations.js,
+  'callback' => 'submit_continue();',
+  'main_prefix' => 'bill_',
+  'no_company' => 1,
+&>
 
 function copyelement(from, to) {
   if ( from == undefined ) {
@@ -96,14 +96,15 @@ function copyelement(from, to) {
   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
 }
 
-% # the value in 'ship_censustract' is the confirmed censustract; if it's set,
+% # the value in pre+'censustract' is the confirmed censustract; if it's set,
 % # do nothing here
 function confirm_censustract() {
   var cf = document.CustomerForm;
-  if ( cf.elements['ship_censustract'].value == '' ) {
+  var pre = cf.elements['same'].checked ? 'bill_' : 'ship_';
+  if ( cf.elements[pre+'censustract'].value == '' ) {
     var address_info = form_address_info();
-    address_info['ship_latitude']  = cf.elements['ship_latitude'].value;
-    address_info['ship_longitude'] = cf.elements['ship_longitude'].value;
+    address_info[pre+'latitude']  = cf.elements[pre+'latitude'].value;
+    address_info[pre+'longitude'] = cf.elements[pre+'longitude'].value;
     OLpostAJAX(
         '<%$p%>/misc/confirm-censustract.html',
         'q=' + encodeURIComponent(JSON.stringify(address_info)),
@@ -120,8 +121,12 @@ function confirm_censustract() {
 %# called from confirm-censustract.html
 function set_censustract(tract, year) {
   var cf = document.CustomerForm;
-  cf.elements['ship_censustract'].value = tract;
-  cf.elements['ship_censusyear'].value = year;
+  var pre = 'ship_';
+  if ( cf.elements['same'].checked ) {
+    pre = 'bill_';
+  }
+  cf.elements[pre + 'censustract'].value = tract;
+  cf.elements[pre + 'censusyear'].value = year;
   submit_continue();
 }
 
index ff45b18..d9c1df7 100644 (file)
@@ -1,54 +1,51 @@
 function form_address_info() {
   var cf = document.<% $formname %>;
-  var state_el      = cf.elements['<% $main_prefix %>state'];
-  var ship_state_el = cf.elements['<% $ship_prefix %>state'];
-  return {
-% if ( $onlyship ) {
-    'onlyship': 1,
-% } else {
-%   if ( $withfirm ) {
-    'company',  cf.elements['company'].value,
-%   }
-    'address1': cf.elements['<% $main_prefix %>address1'].value,
-    'address2': cf.elements['<% $main_prefix %>address2'].value,
-    'city':     cf.elements['<% $main_prefix %>city'].value,
-    'state':    state_el.options[ state_el.selectedIndex ].value,
-    'zip':      cf.elements['<% $main_prefix %>zip'].value,
-    'country':  cf.elements['<% $main_prefix %>country'].value,
+
+  var returnobj = { onlyship: <% $onlyship ? 1 : 0 %> };
+% if ( !$onlyship ) {
+  returnobj['same'] = cf.elements['same'].checked;
 % }
-% if ( $withcensus ) {
-    'ship_censustract': cf.elements['enter_censustract'].value,
+% if ( $withfirm ) {
+% # not part of either address, really
+  returnobj['company'] = cf.elements['company'].value;
 % }
-    'ship_address1': cf.elements['<% $ship_prefix %>address1'].value,
-    'ship_address2': cf.elements['<% $ship_prefix %>address2'].value,
-    'ship_city':     cf.elements['<% $ship_prefix %>city'].value,
-    'ship_state':    ship_state_el.options[ ship_state_el.selectedIndex ].value,
-    'ship_zip':      cf.elements['<% $ship_prefix %>zip'].value,
-    'ship_country':  cf.elements['<% $ship_prefix %>country'].value,
-% if ( !$onlyship ) {
-    'same':     cf.elements['same'].checked
+% if ( $withcensus ) {
+% # "entered" censustract always goes with the ship_ address if there is one
+  returnobj['ship_censustract'] = cf.elements['enter_censustract'].value;
 % }
-  };
+% for my $pre (@prefixes) {
+  if ( <% $pre eq 'ship_' ? 1 : 0 %> && returnobj['same'] ) {
+%   # special case: don't include any ship_ fields, and move the entered
+%   # censustract over to bill_.
+    returnobj['bill_censustract'] = returnobj['ship_censustract'];
+    delete returnobj['ship_censustract'];
+  } else {
+%   # normal case
+%   for my $field (qw(address1 address2 city state zip country)) {
+    returnobj['<% $pre %><% $field %>'] = cf.elements['<% $pre %><% $field %>'].value;
+%   } #for $field
+  } // if returnobj['same']
+% } #foreach $pre
+
+  return returnobj;
 }
 
 function standardize_locations() {
 
-  var startup_msg = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">Verifying address...</FONT></B></P>';
-  overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
   var cf = document.<% $formname %>;
   var address_info = form_address_info();
 
   var changed = false; // have any of the address fields been changed?
 
 // clear coord_auto fields if the user has changed the coordinates
-% for my $pre ($ship_prefix, $onlyship ? () : $main_prefix) {
+% for my $pre (@prefixes) {
 %   for my $field ($pre.'latitude', $pre.'longitude') {
 
   if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
     cf.elements['<% $pre %>coord_auto'].value = '';
   }
 
-%   }
+%   } #foreach $field
   // but if the coordinates have been set to null, turn coord_auto on 
   // and standardize
   if ( cf.elements['<% $pre %>latitude'].value == '' &&
@@ -57,12 +54,11 @@ function standardize_locations() {
     changed = true;
   }
 
-% }
+% } #foreach $pre
 
   // standardize if the old address wasn't clean
-  if ( cf.elements['old_<% $ship_prefix %>addr_clean'].value == '' ||
-      ( <% !$onlyship || 0 %> && 
-        cf.elements['old_<% $main_prefix %>addr_clean'].value == '' ) ) {
+  if ( cf.elements['old_ship_addr_clean'].value == '' ||
+       cf.elements['old_bill_addr_clean'].value == '' ) {
 
     changed = true;
 
@@ -80,18 +76,24 @@ function standardize_locations() {
 % # censustract so that we don't ask the user to confirm it again.
 
   if ( !changed ) {
-    cf.elements['<% $main_prefix %>censustract'].value =
-      address_info['ship_censustract'];
+    if ( address_info['same'] ) {
+      cf.elements['bill_censustract'].value =
+        address_info['bill_censustract'];
+    } else {
+      cf.elements['ship_censustract'].value =
+        address_info['ship_censustract'];
+    }
   }
 
 % if ( $conf->config('address_standardize_method') ) {
   if ( changed ) {
+    var startup_msg = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">Verifying address...</FONT></B></P>';
+    overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
     address_standardize(JSON.stringify(address_info), confirm_standardize);
   }
   else {
-    cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
-%   if ( !$onlyship ) {
-    cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
+%   foreach my $pre (@prefixes) {
+    cf.elements['<% $pre %>addr_clean'].value = 'Y';
 %   }
     post_standardization();
   }
@@ -138,55 +140,26 @@ function replace_address() {
 
   var newaddr = returned['new'];
 
-  var clean = newaddr['addr_clean'] == 'Y';
-  var ship_clean = newaddr['ship_addr_clean'] == 'Y';
-  var error = newaddr['error'];
-  var ship_error = newaddr['ship_error'];
-
   var cf = document.<% $formname %>;
-  var state_el      = cf.elements['<% $main_prefix %>state'];
-  var ship_state_el = cf.elements['<% $ship_prefix %>state'];
-
-% if ( !$onlyship ) {
+%  foreach my $pre (@prefixes) {
+  var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
+  var error = newaddr['<% $pre %>error'];
   if ( clean ) {
-%   if ( $withfirm ) {
-        cf.elements['<% $main_prefix %>company'].value  = newaddr['company'];
-%   }
-        cf.elements['<% $main_prefix %>address1'].value = newaddr['address1'];
-        cf.elements['<% $main_prefix %>address2'].value = newaddr['address2'];
-        cf.elements['<% $main_prefix %>city'].value     = newaddr['city'];
-        setselect(cf.elements['<% $main_prefix %>state'], newaddr['state']);
-        cf.elements['<% $main_prefix %>zip'].value      = newaddr['zip'];
-        cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
-
-        if ( cf.elements['<% $main_prefix %>coord_auto'].value ) {
-          cf.elements['<% $main_prefix %>latitude'].value = newaddr['latitude'];
-          cf.elements['<% $main_prefix %>longitude'].value = newaddr['longitude'];
-        }
-  }
-% }
+%   foreach my $field (qw(address1 address2 city state zip addr_clean censustract)) {
+    cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
+%   } #foreach $field
 
-  if ( ship_clean ) {
-% if ( $withfirm ) {
-      cf.elements['<% $ship_prefix %>company'].value  = newaddr['ship_company'];
-% }
-      cf.elements['<% $ship_prefix %>address1'].value = newaddr['ship_address1'];
-      cf.elements['<% $ship_prefix %>address2'].value = newaddr['ship_address2'];
-      cf.elements['<% $ship_prefix %>city'].value     = newaddr['ship_city'];
-      setselect(cf.elements['<% $ship_prefix %>state'], newaddr['ship_state']);
-      cf.elements['<% $ship_prefix %>zip'].value      = newaddr['ship_zip'];
-      cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
-      if ( cf.elements['<% $ship_prefix %>coord_auto'].value ) {
-        cf.elements['<% $ship_prefix %>latitude'].value = newaddr['latitude'];
-        cf.elements['<% $ship_prefix %>longitude'].value = newaddr['longitude'];
-      }
-  }
-% if ( $withcensus ) {
-% # then set the censustract if address_standardize provided one.
-  if ( ship_clean && newaddr['ship_censustract'] ) {
-      cf.elements['<% $main_prefix %>censustract'].value = newaddr['ship_censustract'];
-  }
-% }
+    if ( cf.elements['<% $pre %>coord_auto'].value ) {
+      cf.elements['<% $pre %>latitude'].value  = newaddr['<% $pre %>latitude'];
+      cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
+    }
+%   if ( $withcensus ) {
+    if ( clean && newaddr['<% $pre %>censustract'] ) {
+      cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
+    }
+%   } #if $withcensus
+  } // if clean
+% } #foreach $pre
 
   post_standardization();
 
@@ -196,8 +169,13 @@ function confirm_manual_address() {
 %# not much to do in this case, just confirm the censustract
 % if ( $withcensus ) {
   var cf = document.<% $formname %>;
-  cf.elements['<% $main_prefix %>censustract'].value =
-  cf.elements['enter_censustract'].value;
+  if ( cf.elements['same'] && cf.elements['same'].checked ) {
+    cf.elements['bill_censustract'].value =
+      cf.elements['enter_censustract'].value;
+  } else {
+    cf.elements['ship_censustract'].value =
+      cf.elements['enter_censustract'].value;
+  }
 % }
   post_standardization();
 }
@@ -295,12 +273,16 @@ my $withcensus = 1;
 
 my $formname =  $opt{form} || 'CustomerForm';
 my $onlyship =  $opt{onlyship} || '';
-my $main_prefix =  $opt{main_prefix} || '';
-my $ship_prefix =  $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
-my $taxpre = $main_prefix;
-$taxpre = $ship_prefix if ( $conf->exists('tax-ship_address') || $onlyship );
+#my $main_prefix =  $opt{main_prefix} || '';
+#my $ship_prefix =  $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
+# The prefixes are now 'ship_' and 'bill_'.
+my $taxpre = 'bill_';
+$taxpre = 'ship_' if ( $conf->exists('tax-ship_address') || $onlyship );
 my $post_geocode = $opt{callback} || 'post_geocode();';
 $withfirm = 0 if $opt{no_company};
 $withcensus = 0 if $opt{no_census};
 
+my @prefixes = ('ship_');
+unshift @prefixes, 'bill_' unless $onlyship;
+
 </%init>
index a6f4b06..57201ea 100644 (file)
@@ -2,7 +2,7 @@
 th { line-height: 150% }
 </STYLE>
 <CENTER><BR><B>
-% if ( $new{error} or $new{ship_error} ) {
+% if ( $new{bill_error} or $new{ship_error} ) {
 Address standardization error
 % }
 % else {
@@ -15,11 +15,11 @@ Confirm address standardization
 % if ( $old{onlyship} ) {
 %   @prefixes = ('ship_');
 % } elsif ( $old{same} ) {
-%   @prefixes = ('');
+%   @prefixes = ('bill_');
 % } else {
-%   @prefixes = ('', 'ship_');
+%   @prefixes = ('bill_', 'ship_');
 % }
-% for my $pre ('', 'ship_') {
+% for my $pre (@prefixes) {
 %   my $name = $pre eq 'ship_' ? 'service' : 'billing';
 %   if ( $new{$pre.'addr_clean'} ) {
   <TR>
@@ -70,24 +70,27 @@ Confirm address standardization
 % } # for $pre
 
 %# only do this part if address standardization provided a censustract
-% if ( $new{'ship_censustract'} ) {
+% my $pre = $old{same} ? 'bill_' : 'ship_';
+% my $censustract = $new{$pre.'censustract'};
+% my $census_error = $new{$pre.'census_error'};
+% if ( $censustract ) {
   <TR>
     <TH>Entered census tract</TH>
     <TH>Calculated census tract</TH>
   </TR>
   <TR>
-    <TD><% $old{'ship_censustract'} %></TD>
+    <TD><% $old{$pre.'censustract'} %></TD>
     <TD>
-%     if ( $new{'ship_census_error'} ) {
-      <FONT COLOR="#ff0000"><% $new{'ship_census_error'} %></FONT>
+%     if ( $census_error ) {
+      <FONT COLOR="#ff0000"><% $census_error %></FONT>
 %     } else {
-      <% $new{'ship_censustract'} %>
+      <% $censustract %>
 %     }
     </TD>
   </TR>
 % } #if censustract
 
-% if ( $new{error} or $new{ship_error} ) {
+% if ( $new{bill_error} or $new{ship_error} ) {
   <TR>
     <TD ALIGN="center">
     <BUTTON TYPE="button" STYLE="width:205px" onclick="confirm_manual_address();">
index ae0ae3a..6a11617 100644 (file)
@@ -61,12 +61,13 @@ warn $cgi->param('q') if $DEBUG;
 my $q = decode_json($cgi->param('q'))
   or die "bad argument '".$cgi->param('q')."'";
 
+my $pre = $q->{'same'} ? 'bill_' : 'ship_';
 my %location = (
-  map { $_ => $q->{'ship_'.$_} }
+  map { $_ => $q->{$pre.$_} }
     qw( company address1 address2 city state zip country latitude longitude )
 );
 
-my $old_tract = $q->{'ship_censustract'};
+my $old_tract = $q->{$pre.'censustract'};
 my $cache = eval { FS::GeocodeCache->new(%location) };
 $cache->set_censustract;
 my $year = FS::Conf->new->config('census_year');
index e28c06f..1620642 100644 (file)
@@ -20,9 +20,9 @@ my @prefixes;
 if ($old{onlyship}) {
   @prefixes = ('ship_');
 } elsif ( $old{same} ) {
-  @prefixes = ('');
+  @prefixes = ('bill_');
 } else {
-  @prefixes = ('', 'ship_');
+  @prefixes = ('bill_', 'ship_');
 }
 foreach my $pre ( @prefixes ) {