X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=httemplate%2Felements%2Fstandardize_locations.js;h=15c5761a06caf087286656d7c52d20139e68080e;hb=a72a10f754f7465121d6137bb3dcee0a21ea6443;hp=e6a4aa6071a334693ae8ef8fc63d9263d760395e;hpb=fb4ab1073f0d15d660c6cdc4e07afebf68ef3924;p=freeside.git diff --git a/httemplate/elements/standardize_locations.js b/httemplate/elements/standardize_locations.js index e6a4aa607..15c5761a0 100644 --- a/httemplate/elements/standardize_locations.js +++ b/httemplate/elements/standardize_locations.js @@ -1,183 +1,198 @@ -function standardize_locations() { +function status_message(text, caption) { + text = '
' + text + '
'; + 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 = { onlyship: <% $onlyship ? 1 : 0 %> }; +% if ( !$onlyship ) { + returnobj['same'] = cf.elements['same'].checked; % } % if ( $withfirm ) { - 'ship_company', cf.elements['<% $ship_prefix %>company'].value, +% # not part of either address, really + returnobj['company'] = cf.elements['company'].value; +% } +% if ( $withcensus ) { +% # "entered" censustract always goes with the ship_ address if there is one + returnobj['ship_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 - ); +% 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; +} - address_standardize( address_info, update_address ); +function standardize_locations() { -} + var cf = document.<% $formname %>; + var address_info = form_address_info(); -var standardize_address; + var changed = false; // have any of the address fields been changed? -function update_address(arg) { +// clear coord_auto fields if the user has changed the coordinates +% for my $pre (@prefixes) { +% for my $field ($pre.'latitude', $pre.'longitude') { - var argsHash = eval('(' + arg + ')'); + if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) { + cf.elements['<% $pre %>coord_auto'].value = ''; + } - var changed = argsHash['address_standardized']; - var ship_changed = argsHash['ship_address_standardized']; - var error = argsHash['error']; - var ship_error = argsHash['ship_error']; - +% } #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; + } - //yay closures - standardize_address = function () { +% } #foreach $pre - var cf = document.<% $formname %>; - var state_el = cf.elements['<% $main_prefix %>state']; - var ship_state_el = cf.elements['<% $ship_prefix %>state']; + // standardize if the old address wasn't clean + if ( cf.elements['old_ship_addr_clean'].value == '' || + cf.elements['old_bill_addr_clean'].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']; + changed = true; + + } + // 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 ) { + 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 ) { + 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 +} + +var returned; -% if ( $conf->exists('cust_main-auto_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); - standardize_address(); + if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) { -% } else { + replace_address(); // with the contents of returned['new'] + + } else if ( returned['all_same'] ) { - // popup a confirmation popup + // 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(); - var confirm_change = - 'Entered billing address | ' + - 'Standardized billing address |
---|---|
' + argsHash['company'] + - ' | ' + argsHash['new_company'] + ' |
' + argsHash['address1'] + - ' | ' + argsHash['new_address1'] + ' |
' + argsHash['address2'] + - ' | ' + argsHash['new_address2'] + ' |
' + argsHash['city'] + ', ' + argsHash['state'] + ' ' + argsHash['zip'] + - ' | ' + argsHash['new_city'] + ', ' + argsHash['new_state'] + ' ' + argsHash['new_zip'] + ' |
Entered service address | ' + - 'Standardized service address |
' + argsHash['ship_company'] + - ' | ' + argsHash['new_ship_company'] + ' |
' + argsHash['ship_address1'] + - ' | ' + argsHash['new_ship_address1'] + ' |
' + argsHash['ship_address2'] + - ' | ' + argsHash['new_ship_address2'] + ' |
' + argsHash['ship_city'] + ', ' + argsHash['ship_state'] + ' ' + argsHash['ship_zip'] + - ' | ' + argsHash['new_ship_city'] + ', ' + argsHash['new_ship_state'] + ' ' + argsHash['new_ship_zip'] + ' |
' + - '' + - ' | ' + - '' + - ' |
' + - ' |