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 var location_fields = <% encode_json(\@location_fields) %>;
157 function update_location( string ) {
158 var hash = JSON.parse(string);
159 for(var i = 0; i < location_fields.length; i++) {
160 var f = location_fields[i];
161 if (hash[f] && document.getElementById(f)) {
162 document.getElementById(f).value = hash[f];
165 country_changed( document.getElementById('country'),
166 fix_state_factory( hash['state'],
175 <<%$th%> ALIGN="right"><% $opt{'label'} || emt('Service location') %></<%$th%>>
177 <SELECT NAME = "locationnum"
179 onchange = "locationnum_changed(this);"
181 % if ( $cust_main ) {
182 <OPTION VALUE="<% $cust_main->ship_locationnum %>"><% $opt{'empty_label'} || '(default service address)' |h %>
184 % if ( $opt{'is_optional'} ) {
185 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
188 % foreach my $loc ( @cust_location ) {
189 % # don't show the ship_location redundantly
190 % next if $cust_main && $cust_main->ship_locationnum == $loc->locationnum;
191 <OPTION VALUE="<% $loc->locationnum %>"
192 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
197 <% $locationnum == -1 ? 'SELECTED' : '' %>
204 <% include('/elements/location.html',
205 'object' => $cust_location,
206 #'onchange' ? probably not
207 'disabled' => $disabled,
209 'no_bold' => $opt{'no_bold'},
210 'alt_format' => $opt{'alt_format'},
212 'enable_censustract' => 1,
215 <SCRIPT TYPE="text/javascript">
216 locationnum_changed(document.getElementById('locationnum'));
220 my $conf = new FS::Conf;
221 my $countrydefault = $conf->config('countrydefault') || 'US';
222 my $statedefault = $conf->config('statedefault')
223 || ($countrydefault eq 'US' ? 'CA' : '');
226 my $cgi = $opt{'cgi'};
227 my $cust_pkg = $opt{'cust_pkg'};
228 my $cust_main = $opt{'cust_main'};
229 my $prospect_main = $opt{'prospect_main'};
230 die "cust_main or prospect_main required" unless $cust_main or $prospect_main;
232 my $locationnum = '';
233 if ( $cgi->param('error') ) {
234 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
237 if ( length($opt{'curr_value'}) ) {
238 $locationnum = $opt{'curr_value'};
239 } elsif ($prospect_main) {
240 my @cust_location = $prospect_main->cust_location;
241 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
243 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
244 $locationnum = $1 || $cust_main->ship_locationnum;
248 #probably could use explicit controls
249 # (cust_main locations not editable for tax reasons)
250 my $editable = $cust_main ? 0 : 1; #could use explicit control
251 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
253 my @location_fields = FS::cust_main->location_fields;
254 if ( $opt{'alt_format'} ) {
255 push @location_fields, qw( location_type location_number location_kind );
258 my $cust_location; #the one that shows by default in the location edit space
259 if ( $locationnum && $locationnum > 0 ) {
260 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
261 or die "unknown locationnum";
263 $cust_location = new FS::cust_location;
264 if ( $locationnum == -1 || $locationnum == -3 ) {
265 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
266 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
267 my $pkg_location = $cust_pkg->cust_location;
268 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
269 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
270 } elsif ( $cust_main ) {
271 $cust_location = $cust_main->ship_location; #I think
275 $cust_location->coord_auto('Y');
277 my $location_sort = sub {
278 $a->country cmp $b->country
279 or lc($a->city) cmp lc($b->city)
280 or lc($a->address1) cmp lc($b->address1)
281 or lc($a->address2) cmp lc($b->address2)
285 push @cust_location, $cust_main->cust_location if $cust_main;
286 push @cust_location, $prospect_main->cust_location if $prospect_main;
287 push @cust_location, $cust_location
288 if !$cust_main && $cust_location && $cust_location->locationnum > 0
289 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
291 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
293 $cust_location = $cust_location[0]
294 if ( $prospect_main )
295 && !$opt{'is_optional'}
300 || ( $editable && $locationnum )
302 && !$opt{'is_optional'} && !@cust_location && $addnew
308 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
309 $cust_location->locationnum(-3);
310 $cust_location->alternize;
311 push @cust_location, $cust_location;
314 my $th = $opt{'no_bold'} ? 'TD' : 'TH';