5 include('/elements/tr-select-cust_location.html',
8 'cust_main' => $cust_main,
10 'prospect_main' => $prospect_main,
13 'empty_label' => '(default service address)',
14 'disable_empty' => 0, #1 to disable
19 <% include('/elements/xmlhttp.html',
20 'url' => $p.'misc/location.cgi',
21 'subs' => [ 'get_location' ],
25 <SCRIPT TYPE="text/javascript">
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';
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 = '';
42 % if ( $opt{'alt_format'} ) {
43 changeSelect(what.form.location_kind, '');
44 changeSelect(what.form.location_type, '');
45 what.form.location_number.value = '';
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';
56 if ( what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
57 what.form.location_number.disabled = false;
58 what.form.location_number.style.backgroundColor = '#ffffff';
62 function locationnum_changed(what) {
63 var locationnum = what.options[what.selectedIndex].value;
64 if ( locationnum == -2 ) { //(not required)
65 location_disable(what);
68 if ( locationnum == -1 ) { //Add new location
71 changeSelect(what.form.country, <% $countrydefault |js_string %>);
73 country_changed( what.form.country,
74 fix_state_factory( <% $statedefault |js_string %>,
79 location_enable(what);
82 if ( locationnum == -3 ) { //service address location for qualificaitons
83 what.form.address1.value = <% $cust_location->address1 |js_string %>;
84 what.form.address2.value = <% $cust_location->address2 |js_string %>;
85 what.form.city.value = <% $cust_location->city |js_string %>;
86 what.form.zip.value = <% $cust_location->zip |js_string %>;
87 % if ( $opt{'alt_format'} ) {
88 what.form.location_number.value = <% $cust_location->location_number |js_string %>;
89 changeSelect(what.form.location_kind, <% $cust_location->location_kind |js_string %> );
90 changeSelect(what.form.location_type, <% $cust_location->location_type |js_string %> );
93 changeSelect(what.form.country, <% $cust_location->country | js_string %> );
95 country_changed( what.form.country,
96 fix_state_factory( <% $cust_location->state | js_string %>,
97 <% $cust_location->county | js_string %>
100 location_enable(what);
104 if ( locationnum == 0 ) { //(default service address)
105 % if ( $cust_main ) {
106 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
107 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
108 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
109 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
111 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
113 country_changed( what.form.country,
114 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
115 <% $cust_main->get($prefix.'county') | js_string %>
121 get_location( locationnum, update_location );
125 if ( locationnum == 0 ) {
128 % #sleep/wait until dropdowns are updated?
129 location_disable(what);
134 % #sleep/wait until dropdowns are updated?
135 location_enable(what);
142 function fix_state_factory (state, county) {
143 function fix_state() {
144 var state_el = document.getElementById('state');
145 changeSelect(state_el, state);
146 state_changed(state_el, fix_county_factory(county) );
151 function fix_county_factory(county) {
152 function fix_county() {
153 var county_el = document.getElementById('county');
154 if ( county.length > 0 ) {
155 changeSelect(county_el, county );
157 county_el.selectedIndex = 0;
159 county_changed(county_el);
164 function changeSelect(what, value) {
165 for ( var i=0; i<what.length; i++) {
166 if ( what.options[i].value == value ) {
167 what.selectedIndex = i;
172 function update_location( string ) {
173 var hash = eval('('+string+')');
174 document.getElementById('address1').value = hash['address1'];
175 document.getElementById('city').value = hash['city'];
176 document.getElementById('zip').value = hash['zip'];
178 % if ( $opt{'alt_format'} ) {
179 changeSelect( document.getElementById('location_kind'), hash['location_kind']);
180 changeSelect( document.getElementById('location_type'), hash['location_type']);
181 document.getElementById('location_number').value = hash['location_number'];
183 document.getElementById('address2').value = hash['address2'];
186 var country_el = document.getElementById('country');
188 changeSelect( country_el, hash['country'] );
190 country_changed( country_el,
191 fix_state_factory( hash['state'],
200 <<%$th%> ALIGN="right"><% $opt{'label'} || emt('Service location') %></<%$th%>>
202 <SELECT NAME = "locationnum"
204 onChange = "locationnum_changed(this);"
206 % if ( !$prospect_main && !$opt{'disable_empty'} ) {
207 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
209 % if ( $opt{'is_optional'} ) {
210 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
213 % foreach my $loc ( @cust_location ) {
214 <OPTION VALUE="<% $loc->locationnum %>"
215 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
220 <% $locationnum == -1 ? 'SELECTED' : '' %>
227 <% include('/elements/location.html',
228 'object' => $cust_location,
229 #'onchange' ? probably not
230 'disabled' => $disabled,
232 'no_bold' => $opt{'no_bold'},
233 'alt_format' => $opt{'alt_format'},
239 my $conf = new FS::Conf;
240 my $countrydefault = $conf->config('countrydefault') || 'US';
241 my $statedefault = $conf->config('statedefault')
242 || ($countrydefault eq 'US' ? 'CA' : '');
245 my $cgi = $opt{'cgi'};
246 my $cust_pkg = $opt{'cust_pkg'};
247 my $cust_main = $opt{'cust_main'};
248 my $prospect_main = $opt{'prospect_main'};
250 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
252 my $locationnum = '';
253 if ( $cgi->param('error') ) {
254 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
257 if ( length($opt{'curr_value'}) ) {
258 $locationnum = $opt{'curr_value'};
259 } elsif ($prospect_main) {
260 my @cust_location = $prospect_main->cust_location;
261 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
263 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
268 #probably could use explicit controls
269 # (cust_main locations not editable for tax reasons)
270 my $editable = $cust_main ? 0 : 1; #could use explicit control
271 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
273 my @location_fields = qw( address1 address2 city county state zip country );
274 if ( $opt{'alt_format'} ) {
275 push @location_fields, qw( location_type location_number location_kind );
279 if ( $locationnum && $locationnum > 0 ) {
280 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
281 or die "unknown locationnum";
283 $cust_location = new FS::cust_location;
284 if ( $locationnum == -1 || $locationnum == -3 ) {
285 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
286 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
287 my $pkg_location = $cust_pkg->cust_location;
288 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
289 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
290 } elsif ( $cust_main ) {
291 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
295 my $location_sort = sub {
296 $a->country cmp $b->country
297 or lc($a->city) cmp lc($b->city)
298 or lc($a->address1) cmp lc($b->address1)
299 or lc($a->address2) cmp lc($b->address2)
302 my @cust_location = ();
303 push @cust_location, $cust_main->cust_location if $cust_main;
304 push @cust_location, $prospect_main->cust_location if $prospect_main;
305 push @cust_location, $cust_location
306 if !$cust_main && $cust_location && $cust_location->locationnum > 0
307 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
309 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
311 $cust_location = $cust_location[0]
312 if ( $prospect_main || $opt{'disable_empty'} )
313 && !$opt{'is_optional'}
318 || ( $editable && $locationnum )
319 || ( ( $prospect_main || $opt{'disable_empty'} )
320 && !$opt{'is_optional'} && !@cust_location && $addnew
326 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
327 $cust_location->locationnum(-3);
328 $cust_location->alternize;
329 push @cust_location, $cust_location;
332 my $th = $opt{'no_bold'} ? 'TD' : 'TH';