ecccd90f19dd59db8588b0ab3ba6f081b4170a83
[freeside.git] / httemplate / elements / standardize_locations.js
1 function status_message(text, caption) {
2   text = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">' + text + '</FONT></B></P>';
3   caption = caption || 'Please wait...';
4   overlib(text, WIDTH, 444, HEIGHT, 168, CAPTION, caption, STICKY, AUTOSTATUSCAP, CLOSECLICK, NOCLOSE, MIDX, 0, MIDY, 0);
5 }
6
7 function form_address_info() {
8   var cf = document.<% $formname %>;
9
10   var returnobj = { billship: <% $billship %> };
11 % if ( $billship ) {
12   returnobj['same'] = cf.elements['same'].checked;
13 % }
14 % for my $pre (@prefixes) {
15 %   # normal case
16 %   for my $field (qw(address1 address2 state zip country), ($conf->exists('cust_main-no_city_in_address') ? () : 'city')) {
17     returnobj['<% $pre %><% $field %>'] = cf.elements['<% $pre %><% $field %>'].value;
18 %   } #for $field
19 %   if ( $withcensus ) {
20     returnobj['<% $pre %>censustract'] = cf.elements['<% $pre %>enter_censustract'].value;
21 %   }
22 % } #foreach $pre
23
24   return returnobj;
25 }
26
27 function standardize_locations() {
28
29   var cf = document.<% $formname %>;
30   var address_info = form_address_info();
31
32   var changed = false; // have any of the address fields been changed?
33
34 // clear coord_auto fields if the user has changed the coordinates
35 % for my $pre (@prefixes) {
36 %   for my $field ($pre.'latitude', $pre.'longitude') {
37
38   if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
39     cf.elements['<% $pre %>coord_auto'].value = '';
40   }
41
42 %   } #foreach $field
43   // but if the coordinates have been set to null, turn coord_auto on 
44   // and standardize
45   if ( cf.elements['<% $pre %>latitude'].value == '' &&
46        cf.elements['<% $pre %>longitude'].value == '' ) {
47     cf.elements['<% $pre %>coord_auto'].value = 'Y';
48     changed = true;
49   }
50   // standardize if the old address wasn't clean
51   if ( cf.elements['<% $pre %>addr_clean'].value == '' ) {
52     changed = true;
53   }
54 % } #foreach $pre
55
56   // or if it was clean but has been changed
57   for (var key in address_info) {
58     var old_el = cf.elements['old_'+key];
59     if ( old_el && address_info[key] != old_el.value ) {
60       changed = true;
61       break;
62     }
63   }
64
65 % # If address hasn't been changed, auto-confirm the existing value of 
66 % # censustract so that we don't ask the user to confirm it again.
67
68   if ( !changed && <% $withcensus %> ) {
69 %   if ( $billship ) {
70     if ( address_info['same'] ) {
71       cf.elements['bill_censustract'].value =
72         address_info['bill_censustract'];
73     } else {
74       cf.elements['ship_censustract'].value =
75         address_info['ship_censustract'];
76     }
77 %   } else {
78       cf.elements['censustract'].value =
79         address_info['censustract'];
80 %   }
81   }
82
83 % if ( $conf->config('address_standardize_method') ) {
84   if ( changed ) {
85     status_message('Verifying address...');
86     address_standardize(JSON.stringify(address_info), confirm_standardize);
87   }
88   else {
89 %   foreach my $pre (@prefixes) {
90     cf.elements['<% $pre %>addr_clean'].value = 'Y';
91 %   }
92     post_standardization();
93   }
94
95 % } else {
96
97   post_standardization();
98
99 % } # if address_standardize_method
100 }
101
102 var returned;
103
104 function confirm_standardize(arg) {
105   // contains 'old', which was what we sent, and 'new', which is what came
106   // back, including any errors
107   returned = JSON.parse(arg);
108
109   if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
110
111     replace_address(); // with the contents of returned['new']
112   
113   } else if ( returned['all_same'] ) {
114
115     // then all entered address fields are correct
116     // but we still need to set the lat/long fields and addr_clean
117
118     if ( returned['all_clean'] ) {
119       status_message('Verified');
120     } else {
121       status_message('Unverified');
122     }
123     replace_address();
124
125   } else {
126
127     var querystring = encodeURIComponent( JSON.stringify(returned) );
128     // confirmation popup: knows to call replace_address(), 
129     // post_standardization(), or submit_abort() depending on the 
130     // user's choice.
131     OLpostAJAX(
132         '<%$p%>/misc/confirm-address_standardize.html', 
133         'q='+querystring,
134         function() {
135           overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY, 
136             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 
137             650, HEIGHT, 368, BGCOLOR, '#333399', CGCOLOR, '#333399', 
138             TEXTSIZE, 3 );
139         }, 0);
140
141   }
142 }
143
144 function replace_address() {
145
146   var newaddr = returned['new'];
147
148   var cf = document.<% $formname %>;
149   var crf = document.forms['confirm_replace_form'];
150 %  foreach my $pre (@prefixes) {
151   var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
152   var replace = true; // auto_standardize_address won't load the form, so just do it
153   if ( crf && crf['<% $pre %>replace'] ) {
154     replace = crf['<% $pre %>replace'].value == 'Y';
155   }
156   var error = newaddr['<% $pre %>error'];
157   if ( clean && replace ) {
158 %   foreach my $field (qw(address1 address2 state zip addr_clean ),($conf->exists('cust_main-no_city_in_address') ? () : 'city')) {
159     cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
160 %   } #foreach $field
161
162     if ( cf.elements['<% $pre %>coord_auto'].value ) {
163       cf.elements['<% $pre %>latitude'].value  = newaddr['<% $pre %>latitude'];
164       cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
165     }
166 %   if ( $withcensus ) {
167     var census_replace = true;
168     if ( crf && crf['census_replace'] ) {
169       census_replace = crf['census_replace'].value == 'Y';
170     }
171
172     if ( clean && census_replace && newaddr['<% $pre %>censustract'] ) {
173       cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
174     }
175 %   } #if $withcensus
176   } // if clean
177 % } #foreach $pre
178
179   post_standardization();
180
181 }
182
183 function confirm_manual_address() {
184 %# not much to do in this case, just confirm the censustract
185 % if ( $withcensus ) {
186   var cf = document.<% $formname %>;
187 %   foreach my $pre (@prefixes) {
188   cf.elements['<% $pre %>censustract'].value =
189     cf.elements['<% $pre %>enter_censustract'].value;
190 %   }
191 % } # $withcensus
192   post_standardization();
193 }
194
195 function post_standardization() {
196
197 % if ( $need_tax_location ) {
198
199   var cf = document.<% $formname %>;
200
201   var prefix = '<% $taxpre %>';
202   // fix edge case with cust_main
203   if ( cf.elements['same']
204     && cf.elements['same'].checked
205     && prefix == 'ship_' ) {
206
207     prefix = 'bill_';
208   }
209
210   if ( new String(cf.elements[prefix + 'zip'].value).length < 10 )
211   {
212
213     var country_el = cf.elements[prefix + 'country'];
214     var country = country_el.options[ country_el.selectedIndex ].value;
215     var geocode = cf.elements[prefix + 'geocode'].value;
216
217     if ( country == 'CA' || country == 'US' ) {
218
219       var state_el = cf.elements[prefix + 'state'];
220       var state = state_el.options[ state_el.selectedIndex ].value;
221
222       var url = "<% $p %>/misc/choose_tax_location.html?" +
223                   "city="     + cf.elements[prefix + 'city'].value +
224                   ";state="    + state + 
225                   ";zip="      + cf.elements[prefix + '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[prefix + 'geocode'].value = 'DEFAULT';
237       <% $post_geocode %>;
238
239     }
240
241   } else {
242
243     cf.elements[prefix + '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     var prefix = '<% $taxpre %>';
263     if ( cf.elements['same']
264       && cf.elements['same'].checked
265       && prefix == 'ship_' ) {
266       prefix = 'bill_';
267     }
268
269 %# this used to set the city/state/zip to the selected value; I think
270 %# that's wrong.
271     var argsHash = JSON.parse(what.value);
272     cf.elements[prefix + 'geocode'].value  = argsHash['geocode'];
273     <% $post_geocode %>;
274
275   }
276
277   // popup a chooser
278
279   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
280
281 }
282
283 function setselect(el, value) {
284
285   for ( var s = 0; s < el.options.length; s++ ) {
286      if ( el.options[s].value == value ) {
287        el.selectedIndex = s;
288      }
289   }
290
291 }
292
293 % if ($census_functions) { # do not use this in cust_main
294 function confirm_censustract() {
295 %   if ( FS::Conf->new->exists('cust_main-require_censustract') ) {
296   var form = document.<% $formname %>;
297   if ( form.elements['censustract'].value == '' ) {
298     var address_info = form_address_info();
299     address_info['latitude']  = form.elements['latitude'].value;
300     address_info['longitude'] = form.elements['longitude'].value;
301     OLpostAJAX(
302         '<%$p%>/misc/confirm-censustract.html',
303         'q=' + encodeURIComponent(JSON.stringify(address_info)),
304         function() {
305           if ( OLresponseAJAX ) {
306             overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
307               AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
308               576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
309               TEXTSIZE, 3 );
310           } else {
311             // no response
312             <% $post_censustract %>;
313           }
314         },
315         0);
316   } else {
317     <% $post_censustract %>;
318   }
319 %   } else { # skip this step
320   <% $post_censustract %>;
321 %   }
322 }
323
324 function set_censustract(tract, year) {
325   var form = document.<% $formname %>;
326   form.elements['censustract'].value = tract;
327   form.elements['censusyear'].value = year;
328   <% $post_censustract %>;
329 }
330
331 % } # $census_functions
332
333 <%init>
334
335 my %opt = @_;
336 my $conf = new FS::Conf;
337
338 my $withcensus = $opt{'with_census'} ? 1 : 0;
339
340 my @prefixes = '';
341 my $billship = $opt{'billship'} ? 1 : 0; # whether to have bill_ and ship_ prefixes
342 my $taxpre = '';
343 # probably should just geocode both addresses, since either one could
344 # be a package address in the future
345 if ($billship) {
346   @prefixes = qw(bill_ ship_);
347   $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : 'bill_';
348 }
349
350 my $formname =  $opt{form} || 'CustomerForm';
351 my $post_geocode = $opt{callback} || 'post_geocode();';
352 my $post_censustract;
353
354 my $census_functions = $opt{'with_census_functions'} ? 1 : 0;
355 if ( $census_functions ) {
356   $post_censustract = $post_geocode;
357   $post_geocode = 'confirm_censustract()';
358 }
359
360 my $tax_engine = FS::TaxEngine->new;
361 my $need_tax_location = $tax_engine->info->{manual_tax_location} ? 1 : 0;
362
363 </%init>