various customer location-related fixes, #13763
[freeside.git] / httemplate / elements / standardize_locations.js
1 function form_address_info() {
2   var cf = document.<% $formname %>;
3   var state_el      = cf.elements['<% $main_prefix %>state'];
4   var ship_state_el = cf.elements['<% $ship_prefix %>state'];
5   return {
6 % if ( $onlyship ) {
7     'onlyship': 1,
8 % } else {
9 %   if ( $withfirm ) {
10     'company',  cf.elements['company'].value,
11 %   }
12     'address1': cf.elements['<% $main_prefix %>address1'].value,
13     'address2': cf.elements['<% $main_prefix %>address2'].value,
14     'city':     cf.elements['<% $main_prefix %>city'].value,
15     'state':    state_el.options[ state_el.selectedIndex ].value,
16     'zip':      cf.elements['<% $main_prefix %>zip'].value,
17     'country':  cf.elements['<% $main_prefix %>country'].value,
18 % }
19 % if ( $withcensus ) {
20     'ship_censustract': cf.elements['enter_censustract'].value,
21 % }
22     'ship_address1': cf.elements['<% $ship_prefix %>address1'].value,
23     'ship_address2': cf.elements['<% $ship_prefix %>address2'].value,
24     'ship_city':     cf.elements['<% $ship_prefix %>city'].value,
25     'ship_state':    ship_state_el.options[ ship_state_el.selectedIndex ].value,
26     'ship_zip':      cf.elements['<% $ship_prefix %>zip'].value,
27     'ship_country':  cf.elements['<% $ship_prefix %>country'].value,
28 % if ( !$onlyship ) {
29     'same':     cf.elements['same'].checked
30 % }
31   };
32 }
33
34 function standardize_locations() {
35
36   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>';
37   overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
38   var cf = document.<% $formname %>;
39   var address_info = form_address_info();
40
41   var changed = false; // have any of the address fields been changed?
42
43 // clear coord_auto fields if the user has changed the coordinates
44 % for my $pre ($ship_prefix, $onlyship ? () : $main_prefix) {
45 %   for my $field ($pre.'latitude', $pre.'longitude') {
46
47   if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
48     cf.elements['<% $pre %>coord_auto'].value = '';
49   }
50
51 %   }
52   // but if the coordinates have been set to null, turn coord_auto on 
53   // and standardize
54   if ( cf.elements['<% $pre %>latitude'].value == '' &&
55        cf.elements['<% $pre %>longitude'].value == '' ) {
56     cf.elements['<% $pre %>coord_auto'].value = 'Y';
57     changed = true;
58   }
59
60 % }
61
62   // standardize if the old address wasn't clean
63   if ( cf.elements['old_<% $ship_prefix %>addr_clean'].value == '' ||
64       ( <% !$onlyship || 0 %> && 
65         cf.elements['old_<% $main_prefix %>addr_clean'].value == '' ) ) {
66
67     changed = true;
68
69   }
70   // or if it was clean but has been changed
71   for (var key in address_info) {
72     var old_el = cf.elements['old_'+key];
73     if ( old_el && address_info[key] != old_el.value ) {
74       changed = true;
75       break;
76     }
77   }
78
79 % # If address hasn't been changed, auto-confirm the existing value of 
80 % # censustract so that we don't ask the user to confirm it again.
81
82   if ( !changed ) {
83     cf.elements['<% $main_prefix %>censustract'].value =
84       address_info['ship_censustract'];
85   }
86
87 % if ( $conf->config('address_standardize_method') ) {
88   if ( changed ) {
89     address_standardize(JSON.stringify(address_info), confirm_standardize);
90   }
91   else {
92     cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
93 %   if ( !$onlyship ) {
94     cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
95 %   }
96     post_standardization();
97   }
98
99 % } else {
100
101   post_standardization();
102
103 % } # if address_standardize_method
104 }
105
106 var returned;
107
108 function confirm_standardize(arg) {
109   // contains 'old', which was what we sent, and 'new', which is what came
110   // back, including any errors
111   returned = JSON.parse(arg);
112
113   if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
114
115     replace_address(); // with the contents of returned['new']
116   
117   }
118   else {
119
120     var querystring = encodeURIComponent( JSON.stringify(returned) );
121     // confirmation popup: knows to call replace_address(), 
122     // post_standardization(), or submit_abort() depending on the 
123     // user's choice.
124     OLpostAJAX(
125         '<%$p%>/misc/confirm-address_standardize.html', 
126         'q='+querystring,
127         function() {
128           overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY, 
129             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 
130             576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', 
131             TEXTSIZE, 3 );
132         }, 0);
133
134   }
135 }
136
137 function replace_address() {
138
139   var newaddr = returned['new'];
140
141   var clean = newaddr['addr_clean'] == 'Y';
142   var ship_clean = newaddr['ship_addr_clean'] == 'Y';
143   var error = newaddr['error'];
144   var ship_error = newaddr['ship_error'];
145
146   var cf = document.<% $formname %>;
147   var state_el      = cf.elements['<% $main_prefix %>state'];
148   var ship_state_el = cf.elements['<% $ship_prefix %>state'];
149
150 % if ( !$onlyship ) {
151   if ( clean ) {
152 %   if ( $withfirm ) {
153         cf.elements['<% $main_prefix %>company'].value  = newaddr['company'];
154 %   }
155         cf.elements['<% $main_prefix %>address1'].value = newaddr['address1'];
156         cf.elements['<% $main_prefix %>address2'].value = newaddr['address2'];
157         cf.elements['<% $main_prefix %>city'].value     = newaddr['city'];
158         setselect(cf.elements['<% $main_prefix %>state'], newaddr['state']);
159         cf.elements['<% $main_prefix %>zip'].value      = newaddr['zip'];
160         cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
161
162         if ( cf.elements['<% $main_prefix %>coord_auto'].value ) {
163           cf.elements['<% $main_prefix %>latitude'].value = newaddr['latitude'];
164           cf.elements['<% $main_prefix %>longitude'].value = newaddr['longitude'];
165         }
166   }
167 % }
168
169   if ( ship_clean ) {
170 % if ( $withfirm ) {
171       cf.elements['<% $ship_prefix %>company'].value  = newaddr['ship_company'];
172 % }
173       cf.elements['<% $ship_prefix %>address1'].value = newaddr['ship_address1'];
174       cf.elements['<% $ship_prefix %>address2'].value = newaddr['ship_address2'];
175       cf.elements['<% $ship_prefix %>city'].value     = newaddr['ship_city'];
176       setselect(cf.elements['<% $ship_prefix %>state'], newaddr['ship_state']);
177       cf.elements['<% $ship_prefix %>zip'].value      = newaddr['ship_zip'];
178       cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
179       if ( cf.elements['<% $ship_prefix %>coord_auto'].value ) {
180         cf.elements['<% $ship_prefix %>latitude'].value = newaddr['latitude'];
181         cf.elements['<% $ship_prefix %>longitude'].value = newaddr['longitude'];
182       }
183   }
184 % if ( $withcensus ) {
185 % # then set the censustract if address_standardize provided one.
186   if ( ship_clean && newaddr['ship_censustract'] ) {
187       cf.elements['<% $main_prefix %>censustract'].value = newaddr['ship_censustract'];
188   }
189 % }
190
191   post_standardization();
192
193 }
194
195 function confirm_manual_address() {
196 %# not much to do in this case, just confirm the censustract
197 % if ( $withcensus ) {
198   var cf = document.<% $formname %>;
199   cf.elements['<% $main_prefix %>censustract'].value =
200   cf.elements['enter_censustract'].value;
201 % }
202   post_standardization();
203 }
204
205 function post_standardization() {
206
207 % if ( $conf->exists('enable_taxproducts') ) {
208
209   if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
210   {
211
212     var country_el = cf.elements['<% $taxpre %>country'];
213     var country = country_el.options[ country_el.selectedIndex ].value;
214     var geocode = cf.elements['geocode'].value;
215
216     if ( country == 'CA' || country == 'US' ) {
217
218       var state_el = cf.elements['<% $taxpre %>state'];
219       var state = state_el.options[ state_el.selectedIndex ].value;
220
221       var url = "<% $p %>/misc/choose_tax_location.html" +
222                   "?data_vendor=cch-zip" + 
223                   ";city="     + cf.elements['<% $taxpre %>city'].value +
224                   ";state="    + state + 
225                   ";zip="      + cf.elements['<% $taxpre %>zip'].value +
226                   ";country="  + country +
227                   ";geocode="  + geocode +
228                   ";formname=" + '<% $formname %>' +
229                   ";";
230
231       // popup a chooser
232       OLgetAJAX( url, update_geocode, 300 );
233
234     } else {
235
236       cf.elements['geocode'].value = 'DEFAULT';
237       <% $post_geocode %>;
238
239     }
240
241   } else {
242
243     cf.elements['geocode'].value = '';
244     <% $post_geocode %>;
245
246   }
247
248 % } else {
249
250   <% $post_geocode %>;
251
252 % }
253
254 }
255
256 function update_geocode() {
257
258   //yay closures
259   set_geocode = function (what) {
260
261     var cf = document.<% $formname %>;
262
263     //alert(what.options[what.selectedIndex].value);
264     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
265     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
266     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
267     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
268     cf.elements['geocode'].value  = argsHash['geocode'];
269     <% $post_geocode %>;
270
271   }
272
273   // popup a chooser
274
275   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
276
277 }
278
279 function setselect(el, value) {
280
281   for ( var s = 0; s < el.options.length; s++ ) {
282      if ( el.options[s].value == value ) {
283        el.selectedIndex = s;
284      }
285   }
286
287 }
288 <%init>
289
290 my %opt = @_;
291 my $conf = new FS::Conf;
292
293 my $withfirm = 1;
294 my $withcensus = 1;
295
296 my $formname =  $opt{form} || 'CustomerForm';
297 my $onlyship =  $opt{onlyship} || '';
298 my $main_prefix =  $opt{main_prefix} || '';
299 my $ship_prefix =  $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
300 my $taxpre = $main_prefix;
301 $taxpre = $ship_prefix if ( $conf->exists('tax-ship_address') || $onlyship );
302 my $post_geocode = $opt{callback} || 'post_geocode();';
303 $withfirm = 0 if $opt{no_company};
304 $withcensus = 0 if $opt{no_census};
305
306 </%init>