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