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';
34 if(what.form.enter_censustract) {
35 what.form.enter_censustract.disabled = true;
39 function location_clear(what) {
40 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
41 var ftype = what.form.<%$_%>.tagName;
42 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
44 if(what.form.enter_censustract) {
45 what.form.enter_censustract.value = '';
47 % if ( $opt{'alt_format'} ) {
48 changeSelect(what.form.location_kind, '');
49 changeSelect(what.form.location_type, '');
50 what.form.location_number.value = '';
54 function location_enable(what) {
55 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
56 what.form.<%$_%>.disabled = false;
57 var ftype = what.form.<%$_%>.tagName;
58 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
60 if(what.form.enter_censustract) {
61 what.form.enter_censustract.disabled = false;
63 % if ( $opt{'alt_format'} ) {
64 if ( what.form.location_type &&
65 what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
66 what.form.location_number.disabled = false;
67 what.form.location_number.style.backgroundColor = '#ffffff';
72 function locationnum_changed(what) {
73 var locationnum = what.options[what.selectedIndex].value;
74 if ( locationnum == -2 ) { //(not required)
75 location_disable(what);
78 if ( locationnum == -1 ) { //Add new location
81 changeSelect(what.form.country, <% $countrydefault |js_string %>);
83 country_changed( what.form.country,
84 fix_state_factory( <% $statedefault |js_string %>,
89 location_enable(what);
92 if ( locationnum == -3 ) { //service address location for qualificaitons
93 what.form.address1.value = <% $cust_location->address1 |js_string %>;
94 what.form.address2.value = <% $cust_location->address2 |js_string %>;
95 what.form.city.value = <% $cust_location->city |js_string %>;
96 what.form.zip.value = <% $cust_location->zip |js_string %>;
97 % if ( $opt{'alt_format'} ) {
98 what.form.location_number.value = <% $cust_location->location_number |js_string %>;
99 changeSelect(what.form.location_kind, <% $cust_location->location_kind |js_string %> );
100 changeSelect(what.form.location_type, <% $cust_location->location_type |js_string %> );
103 changeSelect(what.form.country, <% $cust_location->country | js_string %> );
105 country_changed( what.form.country,
106 fix_state_factory( <% $cust_location->state | js_string %>,
107 <% $cust_location->county | js_string %>
110 location_enable(what);
114 %# default service address is now just another location
115 get_location( locationnum, update_location );
118 if ( locationnum == 0 ) {
121 % #sleep/wait until dropdowns are updated?
122 location_disable(what);
127 % #sleep/wait until dropdowns are updated?
128 location_enable(what);
135 function fix_state_factory (state, county) {
136 function fix_state() {
137 var state_el = document.getElementById('state');
138 changeSelect(state_el, state);
139 state_changed(state_el, fix_county_factory(county) );
144 function fix_county_factory(county) {
145 function fix_county() {
146 var county_el = document.getElementById('county');
147 if ( county.length > 0 ) {
148 changeSelect(county_el, county );
150 county_el.selectedIndex = 0;
152 county_changed(county_el);
157 function changeSelect(what, value) {
158 for ( var i=0; i<what.length; i++) {
159 if ( what.options[i].value == value ) {
160 what.selectedIndex = i;
165 var location_fields = <% encode_json(\@location_fields) %>;
166 function update_location( string ) {
167 var hash = JSON.parse(string);
168 for(var i = 0; i < location_fields.length; i++) {
169 var f = location_fields[i];
170 if (hash[f] && document.getElementById(f)) {
171 document.getElementById(f).value = hash[f];
174 country_changed( document.getElementById('country'),
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' : '' %>
202 ><% $loc->line( cust_main => $cust_main,
203 countrydefault => $countrydefault,
209 <% $locationnum == -1 ? 'SELECTED' : '' %>
216 <% include('/elements/location.html',
217 'object' => $cust_location,
218 #'onchange' ? probably not
219 'disabled' => $disabled,
221 'no_bold' => $opt{'no_bold'},
222 'alt_format' => $opt{'alt_format'},
224 'enable_censustract' => 1,
227 <SCRIPT TYPE="text/javascript">
228 locationnum_changed(document.getElementById('locationnum'));
232 my $conf = new FS::Conf;
233 my $countrydefault = $conf->config('countrydefault') || 'US';
234 my $statedefault = $conf->config('statedefault')
235 || ($countrydefault eq 'US' ? 'CA' : '');
238 my $cgi = $opt{'cgi'};
239 my $cust_pkg = $opt{'cust_pkg'};
240 my $cust_main = $opt{'cust_main'};
241 my $prospect_main = $opt{'prospect_main'};
242 die "cust_main or prospect_main required" unless $cust_main or $prospect_main;
244 my $locationnum = '';
245 if ( $cgi->param('error') ) {
246 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
249 if ( length($opt{'curr_value'}) ) {
250 $locationnum = $opt{'curr_value'};
251 } elsif ($prospect_main) {
252 my @cust_location = $prospect_main->cust_location;
253 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
255 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
256 $locationnum = $1 || $cust_main->ship_locationnum;
260 #probably could use explicit controls
261 # (cust_main locations not editable for tax reasons)
262 my $editable = $cust_main ? 0 : 1; #could use explicit control
263 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
265 my @location_fields = FS::cust_main->location_fields;
266 if ( $opt{'alt_format'} ) {
267 push @location_fields, qw( location_type location_number location_kind );
270 my $cust_location; #the one that shows by default in the location edit space
271 if ( $locationnum && $locationnum > 0 ) {
272 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
273 or die "unknown locationnum";
275 $cust_location = new FS::cust_location;
276 if ( $locationnum == -1 || $locationnum == -3 ) {
277 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
278 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
279 my $pkg_location = $cust_pkg->cust_location;
280 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
281 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
282 } elsif ( $cust_main ) {
283 $cust_location = $cust_main->ship_location; #I think
287 $cust_location->coord_auto('Y');
289 my $location_sort = sub {
290 #enabled w/label_prefix _location # $a->locationname cmp $b->locationname
292 $a->country cmp $b->country
293 or lc($a->city) cmp lc($b->city)
294 or lc($a->address1) cmp lc($b->address1)
295 or lc($a->address2) cmp lc($b->address2)
299 push @cust_location, $cust_main->cust_location if $cust_main;
300 push @cust_location, $prospect_main->cust_location if $prospect_main;
301 push @cust_location, $cust_location
302 if !$cust_main && $cust_location && $cust_location->locationnum > 0
303 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
305 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
307 $cust_location = $cust_location[0]
308 if ( $prospect_main )
309 && !$opt{'is_optional'}
314 || ( $editable && $locationnum )
316 && !$opt{'is_optional'} && !@cust_location && $addnew
322 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
323 $cust_location->locationnum(-3);
324 $cust_location->alternize;
325 push @cust_location, $cust_location;
328 my $th = $opt{'no_bold'} ? 'TD' : 'TH';