5 include('/elements/tr-select-cust_location.html',
8 'cust_main' => $cust_main,
10 'prospect_main' => $prospect_main,
13 'empty_label' => '(default service address)',
18 <% include('/elements/xmlhttp.html',
19 'url' => $p.'misc/location.cgi',
20 'subs' => [ 'get_location' ],
24 <SCRIPT TYPE="text/javascript">
26 function location_disable(what) {
27 % for (@location_fields, 'city_select') {
28 what.form.<%$_%>.disabled = true;
29 var ftype = what.form.<%$_%>.tagName;
30 if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
31 else what.form.<%$_%>.value = '';
32 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
36 function location_clear(what) {
37 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
38 var ftype = what.form.<%$_%>.tagName;
39 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
41 % if ( $opt{'alt_format'} ) {
42 changeSelect(what.form.location_kind, '');
43 changeSelect(what.form.location_type, '');
44 what.form.location_number.value = '';
48 function location_enable(what) {
49 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
50 what.form.<%$_%>.disabled = false;
51 var ftype = what.form.<%$_%>.tagName;
52 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
54 % if ( $opt{'alt_format'} ) {
55 if ( what.form.location_type &&
56 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';
63 function locationnum_changed(what) {
64 var locationnum = what.options[what.selectedIndex].value;
65 if ( locationnum == -2 ) { //(not required)
66 location_disable(what);
69 if ( locationnum == -1 ) { //Add new location
72 changeSelect(what.form.country, <% $countrydefault |js_string %>);
74 country_changed( what.form.country,
75 fix_state_factory( <% $statedefault |js_string %>,
80 location_enable(what);
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 %> );
94 changeSelect(what.form.country, <% $cust_location->country | js_string %> );
96 country_changed( what.form.country,
97 fix_state_factory( <% $cust_location->state | js_string %>,
98 <% $cust_location->county | js_string %>
101 location_enable(what);
105 %# default service address is now just another location
106 get_location( locationnum, update_location );
109 if ( locationnum == 0 ) {
112 % #sleep/wait until dropdowns are updated?
113 location_disable(what);
118 % #sleep/wait until dropdowns are updated?
119 location_enable(what);
126 function fix_state_factory (state, county) {
127 function fix_state() {
128 var state_el = document.getElementById('state');
129 changeSelect(state_el, state);
130 state_changed(state_el, fix_county_factory(county) );
135 function fix_county_factory(county) {
136 function fix_county() {
137 var county_el = document.getElementById('county');
138 if ( county.length > 0 ) {
139 changeSelect(county_el, county );
141 county_el.selectedIndex = 0;
143 county_changed(county_el);
148 function changeSelect(what, value) {
149 for ( var i=0; i<what.length; i++) {
150 if ( what.options[i].value == value ) {
151 what.selectedIndex = i;
156 function update_location( string ) {
157 var hash = eval('('+string+')');
158 document.getElementById('address1').value = hash['address1'];
159 document.getElementById('city').value = hash['city'];
160 document.getElementById('zip').value = hash['zip'];
162 % if ( $opt{'alt_format'} ) {
163 changeSelect( document.getElementById('location_kind'), hash['location_kind']);
164 changeSelect( document.getElementById('location_type'), hash['location_type']);
165 document.getElementById('location_number').value = hash['location_number'];
167 document.getElementById('address2').value = hash['address2'];
170 var country_el = document.getElementById('country');
172 changeSelect( country_el, hash['country'] );
174 country_changed( country_el,
175 fix_state_factory( hash['state'],
184 <<%$th%> ALIGN="right"><% $opt{'label'} || emt('Service location') %></<%$th%>>
186 <SELECT NAME = "locationnum"
188 onChange = "locationnum_changed(this);"
190 % if ( $cust_main ) {
191 <OPTION VALUE="<% $cust_main->ship_locationnum %>"><% $opt{'empty_label'} || '(default service address)' |h %>
193 % if ( $opt{'is_optional'} ) {
194 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
197 % foreach my $loc ( @cust_location ) {
198 % # don't show the ship_location redundantly
199 % next if $cust_main && $cust_main->ship_locationnum == $loc->locationnum;
200 <OPTION VALUE="<% $loc->locationnum %>"
201 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
206 <% $locationnum == -1 ? 'SELECTED' : '' %>
213 <% include('/elements/location.html',
214 'object' => $cust_location,
215 #'onchange' ? probably not
216 'disabled' => $disabled,
218 'no_bold' => $opt{'no_bold'},
219 'alt_format' => $opt{'alt_format'},
223 <SCRIPT TYPE="text/javascript">
224 locationnum_changed(document.getElementById('locationnum'));
228 my $conf = new FS::Conf;
229 my $countrydefault = $conf->config('countrydefault') || 'US';
230 my $statedefault = $conf->config('statedefault')
231 || ($countrydefault eq 'US' ? 'CA' : '');
234 my $cgi = $opt{'cgi'};
235 my $cust_pkg = $opt{'cust_pkg'};
236 my $cust_main = $opt{'cust_main'};
237 my $prospect_main = $opt{'prospect_main'};
238 die "cust_main or prospect_main required" unless $cust_main or $prospect_main;
240 my $locationnum = '';
241 if ( $cgi->param('error') ) {
242 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
245 if ( length($opt{'curr_value'}) ) {
246 $locationnum = $opt{'curr_value'};
247 } elsif ($prospect_main) {
248 my @cust_location = $prospect_main->cust_location;
249 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
251 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
252 $locationnum = $1 || $cust_main->ship_locationnum;
256 #probably could use explicit controls
257 # (cust_main locations not editable for tax reasons)
258 my $editable = $cust_main ? 0 : 1; #could use explicit control
259 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
261 my @location_fields = qw( address1 address2 city county state zip country
264 if ( $opt{'alt_format'} ) {
265 push @location_fields, qw( location_type location_number location_kind );
268 my $cust_location; #the one that shows by default in the location edit space
269 if ( $locationnum && $locationnum > 0 ) {
270 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
271 or die "unknown locationnum";
273 $cust_location = new FS::cust_location;
274 if ( $locationnum == -1 || $locationnum == -3 ) {
275 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
276 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
277 my $pkg_location = $cust_pkg->cust_location;
278 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
279 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
280 } elsif ( $cust_main ) {
281 $cust_location = $cust_main->ship_location; #I think
285 $cust_location->coord_auto('Y');
287 my $location_sort = sub {
288 $a->country cmp $b->country
289 or lc($a->city) cmp lc($b->city)
290 or lc($a->address1) cmp lc($b->address1)
291 or lc($a->address2) cmp lc($b->address2)
295 push @cust_location, $cust_main->cust_location if $cust_main;
296 push @cust_location, $prospect_main->cust_location if $prospect_main;
297 push @cust_location, $cust_location
298 if !$cust_main && $cust_location && $cust_location->locationnum > 0
299 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
301 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
303 $cust_location = $cust_location[0]
304 if ( $prospect_main )
305 && !$opt{'is_optional'}
310 || ( $editable && $locationnum )
312 && !$opt{'is_optional'} && !@cust_location && $addnew
318 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
319 $cust_location->locationnum(-3);
320 $cust_location->alternize;
321 push @cust_location, $cust_location;
324 my $th = $opt{'no_bold'} ? 'TD' : 'TH';