user confirmation for FFIEC censustract lookups
[freeside.git] / httemplate / elements / tr-select-cust_location.html
1 <%doc>
2
3 Example:
4
5   include('/elements/tr-select-cust_location.html',
6             'cgi'       => $cgi,
7
8             'cust_main'     => $cust_main,
9             #or
10             'prospect_main' => $prospect_main,
11
12             #optional
13             'empty_label'   => '(default service address)',
14             'disable_empty' => 0, #1 to disable
15          )
16
17 </%doc>
18
19 <% include('/elements/xmlhttp.html',
20               'url'  => $p.'misc/location.cgi',
21               'subs' => [ 'get_location' ],
22            )
23 %>
24
25 <SCRIPT TYPE="text/javascript">
26
27   function location_disable(what) {
28 %   for (@location_fields, 'city_select') { 
29       what.form.<%$_%>.disabled = true;
30       var ftype = what.form.<%$_%>.tagName;
31       if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
32       else what.form.<%$_%>.value = '';
33       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
34 %   } 
35   }
36
37   function location_clear(what) {
38 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
39       var ftype = what.form.<%$_%>.tagName;
40       if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
41 %   }
42 %   if ( $opt{'alt_format'} ) {
43       changeSelect(what.form.location_kind, '');
44       changeSelect(what.form.location_type, '');
45       what.form.location_number.value = '';
46 %   }
47   }
48
49   function location_enable(what) {
50 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
51       what.form.<%$_%>.disabled = false;
52       var ftype = what.form.<%$_%>.tagName;
53       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
54 %   } 
55
56     if ( what.form.location_type &&
57          what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
58       what.form.location_number.disabled = false;
59       what.form.location_number.style.backgroundColor = '#ffffff';
60     }
61   }
62
63   function locationnum_changed(what) {
64     var locationnum = what.options[what.selectedIndex].value;
65     if ( locationnum == -2 ) { //(not required)
66       location_disable(what);
67       return;
68     }
69     if ( locationnum == -1 ) { //Add new location
70       location_clear(what);
71
72       changeSelect(what.form.country, <% $countrydefault |js_string %>);
73
74       country_changed( what.form.country,
75                        fix_state_factory( <% $statedefault |js_string %>,
76                                           ''
77                                         )
78                      );
79
80       location_enable(what);
81       return;
82     }
83     if ( locationnum == -3 ) { //service address location for qualificaitons
84       what.form.address1.value = <% $cust_location->address1 |js_string %>;
85       what.form.address2.value = <% $cust_location->address2 |js_string %>;
86       what.form.city.value = <% $cust_location->city |js_string %>;
87       what.form.zip.value = <% $cust_location->zip |js_string %>;
88 %     if ( $opt{'alt_format'} ) {
89         what.form.location_number.value = <% $cust_location->location_number |js_string %>;
90         changeSelect(what.form.location_kind, <% $cust_location->location_kind |js_string %> );
91         changeSelect(what.form.location_type, <% $cust_location->location_type |js_string %> );
92 %     }
93
94       changeSelect(what.form.country, <% $cust_location->country | js_string %> );
95
96       country_changed( what.form.country,
97                        fix_state_factory( <% $cust_location->state | js_string %>,
98                                           <% $cust_location->county | js_string %>
99                                         )
100                      );
101       location_enable(what);
102       return;
103     }
104
105     if ( locationnum == 0 ) { //(default service address)
106 %     if ( $cust_main ) {
107       what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
108       what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
109       what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
110       what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
111
112       changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
113
114       country_changed( what.form.country,
115                        fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
116                                           <% $cust_main->get($prefix.'county') | js_string %>
117                                         )
118                      );
119 %     }
120
121     } else {
122       get_location( locationnum, update_location );
123     } 
124
125 %   if ( $editable ) {
126       if ( locationnum == 0 ) {
127 %   }
128
129 %       #sleep/wait until dropdowns are updated?
130         location_disable(what);
131
132 %   if ( $editable ) {
133       } else {
134
135 %       #sleep/wait until dropdowns are updated?
136         location_enable(what);
137
138       }
139 %   }
140
141   }
142
143   function fix_state_factory (state, county) {
144     function fix_state() {
145       var state_el = document.getElementById('state');
146       changeSelect(state_el, state);
147       state_changed(state_el, fix_county_factory(county) );
148     }
149     return fix_state;
150   }
151
152   function fix_county_factory(county) {
153     function fix_county() {
154       var county_el = document.getElementById('county');
155       if ( county.length > 0 ) {
156         changeSelect(county_el, county );
157       } else {
158         county_el.selectedIndex = 0;
159       }
160       county_changed(county_el);
161     }
162     return fix_county;
163   }
164
165   function changeSelect(what, value) {
166     for ( var i=0; i<what.length; i++) {
167       if ( what.options[i].value == value ) {
168         what.selectedIndex = i;
169       }
170     }
171   }
172
173   function update_location( string ) {
174     var hash = eval('('+string+')');
175     document.getElementById('address1').value = hash['address1'];
176     document.getElementById('city').value     = hash['city'];
177     document.getElementById('zip').value      = hash['zip'];
178
179 %   if ( $opt{'alt_format'} ) {
180       changeSelect( document.getElementById('location_kind'), hash['location_kind']);
181       changeSelect( document.getElementById('location_type'), hash['location_type']);
182       document.getElementById('location_number').value = hash['location_number'];
183 %   } else {
184       document.getElementById('address2').value = hash['address2'];
185 %   }
186
187     var country_el = document.getElementById('country');
188
189     changeSelect( country_el, hash['country'] );
190
191     country_changed( country_el,
192                      fix_state_factory( hash['state'],
193                                         hash['county']
194                                       )
195                    );
196   }
197
198 </SCRIPT>
199
200 <TR>
201   <<%$th%> ALIGN="right"><% $opt{'label'} || emt('Service location') %></<%$th%>>
202   <TD COLSPAN=7>
203     <SELECT NAME     = "locationnum"
204             ID       = "locationnum"
205             onChange = "locationnum_changed(this);"
206     >
207 % if ( !$prospect_main && !$opt{'disable_empty'} ) {
208       <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
209 % }
210 % if ( $opt{'is_optional'} ) {
211     <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
212 % }
213 %
214 %     foreach my $loc ( @cust_location ) {
215         <OPTION VALUE="<% $loc->locationnum %>"
216                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
217         ><% $loc->line |h %>
218 %     }
219 %     if ( $addnew ) {
220         <OPTION VALUE="-1"
221                 <% $locationnum == -1 ? 'SELECTED' : '' %>
222         >Add new location
223 %     }
224     </SELECT>
225   </TD>
226 </TR>
227
228 <% include('/elements/location.html',
229              'object'       => $cust_location,
230              #'onchange' ?  probably not
231              'disabled'     => $disabled,
232              'no_asterisks' => 1,
233              'no_bold'      => $opt{'no_bold'},
234              'alt_format'   => $opt{'alt_format'},
235           )
236 %>
237
238 <%init>
239
240 my $conf = new FS::Conf;
241 my $countrydefault = $conf->config('countrydefault') || 'US';
242 my $statedefault = $conf->config('statedefault')
243                    || ($countrydefault eq 'US' ? 'CA' : '');
244
245 my %opt = @_;
246 my $cgi           = $opt{'cgi'};
247 my $cust_pkg      = $opt{'cust_pkg'};
248 my $cust_main     = $opt{'cust_main'};
249 my $prospect_main = $opt{'prospect_main'};
250
251 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
252
253 my $locationnum = '';
254 if ( $cgi->param('error') ) {
255   $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
256   $locationnum = $1;
257 } else {
258   if ( length($opt{'curr_value'}) ) {
259     $locationnum = $opt{'curr_value'};
260   } elsif ($prospect_main) {
261     my @cust_location = $prospect_main->cust_location;
262     $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
263   } else { #?
264     $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
265     $locationnum = $1;
266   }
267 }
268
269 #probably could use explicit controls
270 # (cust_main locations not editable for tax reasons)
271 my $editable = $cust_main ? 0 : 1; #could use explicit control
272 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
273
274 my @location_fields = qw( address1 address2 city county state zip country
275                           latitude longitude
276                         );
277 if ( $opt{'alt_format'} ) {
278     push @location_fields, qw( location_type location_number location_kind );
279 }
280
281 my $cust_location;
282 if ( $locationnum && $locationnum > 0 ) {
283   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
284     or die "unknown locationnum";
285 } else {
286   $cust_location = new FS::cust_location;
287   if ( $locationnum == -1 || $locationnum == -3 ) {
288     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
289   } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
290     my $pkg_location = $cust_pkg->cust_location;
291     $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
292     $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
293   } elsif ( $cust_main ) {
294     $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
295   }
296 }
297
298 $cust_location->coord_auto('Y');
299
300 my $location_sort = sub {
301         $a->country   cmp $b->country
302   or lc($a->city)     cmp lc($b->city)
303   or lc($a->address1) cmp lc($b->address1)
304   or lc($a->address2) cmp lc($b->address2)
305 };
306
307 my @cust_location = ();
308 push @cust_location, $cust_main->cust_location if $cust_main;
309 push @cust_location, $prospect_main->cust_location if $prospect_main;
310 push @cust_location, $cust_location
311   if !$cust_main && $cust_location && $cust_location->locationnum > 0
312   && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
313
314 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
315
316 $cust_location = $cust_location[0]
317   if ( $prospect_main || $opt{'disable_empty'} )
318   && !$opt{'is_optional'}
319   && @cust_location;
320
321 my $disabled =
322   ( $locationnum < 0
323     || ( $editable && $locationnum )
324     || ( ( $prospect_main || $opt{'disable_empty'} )
325          && !$opt{'is_optional'} && !@cust_location && $addnew
326        )
327   )
328     ? ''
329     : 'DISABLED';
330
331 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
332   $cust_location->locationnum(-3);
333   $cust_location->alternize;
334   push @cust_location, $cust_location;
335 }
336
337 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
338
339 </%init>