1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
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 returnobj = { billship: <% $billship %> };
% if ( $billship ) {
returnobj['same'] = cf.elements['same'].checked;
% }
% 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;
% }
% }
% 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;
}
function standardize_locations() {
var cf = document.<% $formname %>;
var address_info = form_address_info();
var changed = false; // have any of the address fields been changed?
// clear coord_auto fields if the user has changed the coordinates
% for my $pre (@prefixes) {
% for my $field ($pre.'latitude', $pre.'longitude') {
if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
cf.elements['<% $pre %>coord_auto'].value = '';
}
% } #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 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 address_standardize_method
}
var returned;
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);
if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
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();
} 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);
}
}
function replace_address() {
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
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() {
% if ( $conf->exists('enable_taxproducts') ) {
var cf = document.<% $formname %>;
var prefix = '<% $taxpre %>';
// fix edge case with cust_main
if ( cf.elements['same']
&& cf.elements['same'].checked
&& prefix == 'ship_' ) {
prefix = 'bill_';
}
if ( new String(cf.elements[prefix + 'zip'].value).length < 10 )
{
var country_el = cf.elements[prefix + 'country'];
var country = country_el.options[ country_el.selectedIndex ].value;
var geocode = cf.elements[prefix + 'geocode'].value;
if ( country == 'CA' || country == 'US' ) {
var state_el = cf.elements[prefix + 'state'];
var state = state_el.options[ state_el.selectedIndex ].value;
var url = "<% $p %>/misc/choose_tax_location.html" +
"?data_vendor=cch-zip" +
";city=" + cf.elements[prefix + 'city'].value +
";state=" + state +
";zip=" + cf.elements[prefix + 'zip'].value +
";country=" + country +
";geocode=" + geocode +
";formname=" + '<% $formname %>' +
";";
// popup a chooser
OLgetAJAX( url, update_geocode, 300 );
} else {
cf.elements[prefix + 'geocode'].value = 'DEFAULT';
<% $post_geocode %>;
}
} else {
cf.elements[prefix + 'geocode'].value = '';
<% $post_geocode %>;
}
% } else {
<% $post_geocode %>;
% }
}
function update_geocode() {
//yay closures
set_geocode = function (what) {
var cf = document.<% $formname %>;
var prefix = '<% $taxpre %>';
if ( cf.elements['same']
&& cf.elements['same'].checked
&& prefix == 'ship_' ) {
prefix = 'bill_';
}
//alert(what.options[what.selectedIndex].value);
var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
cf.elements[prefix + 'city'].value = argsHash['city'];
setselect(cf.elements[prefix + 'state'], argsHash['state']);
cf.elements[prefix + 'zip'].value = argsHash['zip'];
cf.elements[prefix + 'geocode'].value = argsHash['geocode'];
<% $post_geocode %>;
}
// popup a chooser
overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
}
function setselect(el, value) {
for ( var s = 0; s < el.options.length; s++ ) {
if ( el.options[s].value == value ) {
el.selectedIndex = s;
}
}
}
<%init>
my %opt = @_;
my $conf = new FS::Conf;
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 $post_geocode = $opt{callback} || 'post_geocode();';
</%init>
|