Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / elements / standardize_locations.js
index e6a4aa6..d7c3670 100644 (file)
-function standardize_locations() {
+function status_message(text, caption) {
+  text = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">' + text + '</FONT></B></P>';
+  caption = caption || 'Please wait...';
+  overlib(text, WIDTH, 444, HEIGHT, 168, CAPTION, caption, STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
+}
 
+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'];
-
-  var address_info = new Array(
-% if ( $onlyship ) {
-    'onlyship', 1,
-% } else {
-%   if ( $withfirm ) {
-    'company',  cf.elements['<% $main_prefix %>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,
+  var returnobj = { billship: <% $billship %> };
+% if ( $billship ) {
+  returnobj['same'] = cf.elements['same'].checked;
 % }
-% if ( $withfirm ) {
-    'ship_company',  cf.elements['<% $ship_prefix %>company'].value,
+% if ( $withcensus ) {
+% # "entered" censustract always goes with the ship_ address if there is one
+%   if ( $billship ) {
+    returnobj['ship_censustract'] = cf.elements['enter_censustract'].value;
+%   } else { # there's only a package address, so it's just "censustract"
+    returnobj['censustract'] = cf.elements['enter_censustract'].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
-  );
-
-  address_standardize( address_info, update_address );
-
+% 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;
 }
 
-var standardize_address;
+function standardize_locations() {
 
-function update_address(arg) {
+  var cf = document.<% $formname %>;
+  var address_info = form_address_info();
 
-  var argsHash = eval('(' + arg + ')');
+  var changed = false; // have any of the address fields been changed?
 
-  var changed  = argsHash['address_standardized'];
-  var ship_changed = argsHash['ship_address_standardized'];
-  var error = argsHash['error'];
-  var ship_error = argsHash['ship_error'];
-  
+// clear coord_auto fields if the user has changed the coordinates
+% for my $pre (@prefixes) {
+%   for my $field ($pre.'latitude', $pre.'longitude') {
 
-  //yay closures
-  standardize_address = function () {
-
-    var cf = document.<% $formname %>;
-    var state_el      = cf.elements['<% $main_prefix %>state'];
-    var ship_state_el = cf.elements['<% $ship_prefix %>state'];
+  if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
+    cf.elements['<% $pre %>coord_auto'].value = '';
+  }
 
-% if ( !$onlyship ) {
-    if ( changed ) {
-%   if ( $withfirm ) {
-      cf.elements['<% $main_prefix %>company'].value  = argsHash['new_company'];
-%   }
-      cf.elements['<% $main_prefix %>address1'].value = argsHash['new_address1'];
-      cf.elements['<% $main_prefix %>address2'].value = argsHash['new_address2'];
-      cf.elements['<% $main_prefix %>city'].value     = argsHash['new_city'];
-      setselect(cf.elements['<% $main_prefix %>state'], argsHash['new_state']);
-      cf.elements['<% $main_prefix %>zip'].value      = argsHash['new_zip'];
+%   } #foreach $field
+  // but if the coordinates have been set to null, turn coord_auto on 
+  // and standardize
+  if ( cf.elements['<% $pre %>latitude'].value == '' &&
+       cf.elements['<% $pre %>longitude'].value == '' ) {
+    cf.elements['<% $pre %>coord_auto'].value = 'Y';
+    changed = true;
+  }
+  // standardize if the old address wasn't clean
+  if ( cf.elements['<% $pre %>addr_clean'].value == '' ) {
+    changed = true;
+  }
+% } #foreach $pre
+
+  // or if it was clean but has been changed
+  for (var key in address_info) {
+    var old_el = cf.elements['old_'+key];
+    if ( old_el && address_info[key] != old_el.value ) {
+      changed = true;
+      break;
     }
-% }
+  }
 
-    if ( ship_changed ) {
-% if ( $withfirm ) {
-      cf.elements['<% $ship_prefix %>company'].value  = argsHash['new_ship_company'];
-% }
-      cf.elements['<% $ship_prefix %>address1'].value = argsHash['new_ship_address1'];
-      cf.elements['<% $ship_prefix %>address2'].value = argsHash['new_ship_address2'];
-      cf.elements['<% $ship_prefix %>city'].value     = argsHash['new_ship_city'];
-      setselect(cf.elements['<% $ship_prefix %>state'], argsHash['new_ship_state']);
-      cf.elements['<% $ship_prefix %>zip'].value      = argsHash['new_ship_zip'];
+% # If address hasn't been changed, auto-confirm the existing value of 
+% # censustract so that we don't ask the user to confirm it again.
+
+  if ( !changed && <% $withcensus %> ) {
+%   if ( $billship ) {
+    if ( address_info['same'] ) {
+      cf.elements['bill_censustract'].value =
+        address_info['bill_censustract'];
+    } else {
+      cf.elements['ship_censustract'].value =
+        address_info['ship_censustract'];
     }
+%   } else {
+      cf.elements['censustract'].value =
+        address_info['censustract'];
+%   }
+  }
 
+% if ( $conf->config('address_standardize_method') ) {
+  if ( changed ) {
+    status_message('Verifying address...');
+    address_standardize(JSON.stringify(address_info), confirm_standardize);
+  }
+  else {
+%   foreach my $pre (@prefixes) {
+    cf.elements['<% $pre %>addr_clean'].value = 'Y';
+%   }
     post_standardization();
-
   }
 
+% } else {
 
+  post_standardization();
 
-  if ( changed || ship_changed ) {
+% } # if address_standardize_method
+}
 
-%   if ( $conf->exists('cust_main-auto_standardize_address') ) {
+var returned;
 
-    standardize_address();
+function confirm_standardize(arg) {
+  // contains 'old', which was what we sent, and 'new', which is what came
+  // back, including any errors
+  returned = JSON.parse(arg);
 
-%   } else {
+  if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
 
-    // popup a confirmation popup
-
-    var confirm_change =
-      '<CENTER><BR><B>Confirm address standardization</B><BR><BR>' +
-      '<TABLE>';
-    
-    if ( changed ) {
-
-      confirm_change = confirm_change + 
-        '<TR><TH>Entered billing address</TH>' +
-          '<TH>Standardized billing address</TH></TR>';
-        // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
-      
-      if ( argsHash['company'] || argsHash['new_company'] ) {
-        confirm_change = confirm_change +
-        '<TR><TD>' + argsHash['company'] +
-          '</TD><TD>' + argsHash['new_company'] + '</TD></TR>';
-      }
-      
-      confirm_change = confirm_change +
-        '<TR><TD>' + argsHash['address1'] +
-          '</TD><TD>' + argsHash['new_address1'] + '</TD></TR>' +
-        '<TR><TD>' + argsHash['address2'] +
-          '</TD><TD>' + argsHash['new_address2'] + '</TD></TR>' +
-        '<TR><TD>' + argsHash['city'] + ', ' + argsHash['state'] + '  ' + argsHash['zip'] +
-          '</TD><TD>' + argsHash['new_city'] + ', ' + argsHash['new_state'] + '  ' + argsHash['new_zip'] + '</TD></TR>' +
-          '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
+    replace_address(); // with the contents of returned['new']
+  
+  } else if ( returned['all_same'] ) {
 
-    }
+    // then all entered address fields are correct
+    // but we still need to set the lat/long fields and addr_clean
+    status_message('Verified');
+    replace_address();
 
-    if ( ship_changed ) {
-
-      confirm_change = confirm_change + 
-        '<TR><TH>Entered service address</TH>' +
-          '<TH>Standardized service address</TH></TR>';
-        // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
-      
-      if ( argsHash['ship_company'] || argsHash['new_ship_company'] ) {
-        confirm_change = confirm_change +
-        '<TR><TD>' + argsHash['ship_company'] +
-          '</TD><TD>' + argsHash['new_ship_company'] + '</TD></TR>';
-      }
-      
-      confirm_change = confirm_change +
-        '<TR><TD>' + argsHash['ship_address1'] +
-          '</TD><TD>' + argsHash['new_ship_address1'] + '</TD></TR>' +
-        '<TR><TD>' + argsHash['ship_address2'] +
-          '</TD><TD>' + argsHash['new_ship_address2'] + '</TD></TR>' +
-        '<TR><TD>' + argsHash['ship_city'] + ', ' + argsHash['ship_state'] + '  ' + argsHash['ship_zip'] +
-          '</TD><TD>' + argsHash['new_ship_city'] + ', ' + argsHash['new_ship_state'] + '  ' + argsHash['new_ship_zip'] + '</TD></TR>' +
-        '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
+  } else {
 
-    }
+    var querystring = encodeURIComponent( JSON.stringify(returned) );
+    // confirmation popup: knows to call replace_address(), 
+    // post_standardization(), or submit_abort() depending on the 
+    // user's choice.
+    OLpostAJAX(
+        '<%$p%>/misc/confirm-address_standardize.html', 
+        'q='+querystring,
+        function() {
+          overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY, 
+            AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 
+            576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', 
+            TEXTSIZE, 3 );
+        }, 0);
 
-    var addresses = 'address';
-    var height = 268;
-    if ( changed && ship_changed ) {
-      addresses = 'addresses';
-      height = 396; // #what
-    }
+  }
+}
 
-    confirm_change = confirm_change +
-      '<TR><TD>' +
-        '<BUTTON TYPE="button" onClick="post_standardization();"><IMG SRC="<%$p%>images/error.png" ALT=""> Use entered ' + addresses + '</BUTTON>' + 
-      '</TD><TD>' +
-        '<BUTTON TYPE="button" onClick="standardize_address();"><IMG SRC="<%$p%>images/tick.png" ALT=""> Use standardized ' + addresses + '</BUTTON>' + 
-      '</TD></TR>' +
-      '<TR><TD COLSPAN=2 ALIGN="center">' +
-        '<BUTTON TYPE="button" onClick="document.<% $formname %>.submitButton.disabled=false; parent.cClick();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
-        
-      '</TABLE></CENTER>';
+function replace_address() {
 
-    overlib( confirm_change, CAPTION, 'Confirm address standardization', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, height, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
+  var newaddr = returned['new'];
 
-%   }
+  var cf = document.<% $formname %>;
+%  foreach my $pre (@prefixes) {
+  var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
+  var error = newaddr['<% $pre %>error'];
+  if ( clean ) {
+%   foreach my $field (qw(address1 address2 city state zip addr_clean censustract)) {
+    cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
+%   } #foreach $field
+
+    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
 
-  } else {
+  post_standardization();
 
-    post_standardization();
+}
 
+function confirm_manual_address() {
+%# not much to do in this case, just confirm the censustract
+% if ( $withcensus ) {
+  var cf = document.<% $formname %>;
+%   if ( $billship ) {
+  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;
   }
-
-
+%   } else {
+  cf.elements['censustract'].value = cf.elements['enter_censustract'].value;
+%   }
+% }
+  post_standardization();
 }
 
 function post_standardization() {
 
-  var cf = document.<% $formname %>;
-
 % if ( $conf->exists('enable_taxproducts') ) {
 
+  var cf = document.<% $formname %>;
+
   if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
   {
 
     var country_el = cf.elements['<% $taxpre %>country'];
     var country = country_el.options[ country_el.selectedIndex ].value;
-    var geocode = cf.elements['geocode'].value;
+    var geocode = cf.elements['bill_geocode'].value;
 
     if ( country == 'CA' || country == 'US' ) {
 
@@ -207,14 +229,14 @@ function post_standardization() {
 
     } else {
 
-      cf.elements['geocode'].value = 'DEFAULT';
+      cf.elements['bill_geocode'].value = 'DEFAULT';
       <% $post_geocode %>;
 
     }
 
   } else {
 
-    cf.elements['geocode'].value = '';
+    cf.elements['bill_geocode'].value = '';
     <% $post_geocode %>;
 
   }
@@ -239,14 +261,14 @@ function update_geocode() {
     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
-    cf.elements['geocode'].value  = argsHash['geocode'];
+    cf.elements['bill_geocode'].value  = argsHash['geocode'];
     <% $post_geocode %>;
 
   }
 
   // popup a chooser
 
-  overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
+  overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
 
 }
 
@@ -264,15 +286,19 @@ function setselect(el, value) {
 my %opt = @_;
 my $conf = new FS::Conf;
 
-my $withfirm = 1;
+my $withcensus = $opt{'with_census'} ? 1 : 0;
+
+my @prefixes = '';
+my $billship = $opt{'billship'} ? 1 : 0; # whether to have bill_ and ship_ prefixes
+my $taxpre = '';
+# probably should just geocode both addresses, since either one could
+# be a package address in the future
+if ($billship) {
+  @prefixes = qw(bill_ ship_);
+  $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : 'bill_';
+}
 
 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 $post_geocode = $opt{callback} || 'post_geocode();';
-$withfirm = 0 if $opt{no_company};
 
 </%init>