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_enable(what) {
37 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
38 what.form.<%$_%>.disabled = false;
39 var ftype = what.form.<%$_%>.tagName;
40 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
41 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
44 if ( what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
45 what.form.location_number.disabled = false;
46 what.form.location_number.style.backgroundColor = '#ffffff';
48 what.form.location_number.value = '';
51 function locationnum_changed(what) {
52 var locationnum = what.options[what.selectedIndex].value;
53 if ( locationnum == -2 ) {
54 location_disable(what);
57 if ( locationnum == -1 ) {
58 location_enable(what);
60 changeSelect(what.form.country, <% $countrydefault |js_string %>);
62 country_changed( what.form.country,
63 fix_state_factory( <% $statedefault |js_string %>,
70 if ( locationnum == 0 ) {
72 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
73 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
74 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
75 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
77 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
79 country_changed( what.form.country,
80 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
81 <% $cust_main->get($prefix.'county') | js_string %>
87 get_location( locationnum, update_location );
91 if ( locationnum == 0 ) {
94 % #sleep/wait until dropdowns are updated?
95 location_disable(what);
100 % #sleep/wait until dropdowns are updated?
101 location_enable(what);
108 function fix_state_factory (state, county) {
109 function fix_state() {
110 var state_el = document.getElementById('state');
111 changeSelect(state_el, state);
112 state_changed(state_el, fix_county_factory(county) );
117 function fix_county_factory(county) {
118 function fix_county() {
119 var county_el = document.getElementById('county');
120 if ( county.length > 0 ) {
121 changeSelect(county_el, county );
123 county_el.selectedIndex = 0;
125 county_changed(county_el);
130 function changeSelect(what, value) {
131 for ( var i=0; i<what.length; i++) {
132 if ( what.options[i].value == value ) {
133 what.selectedIndex = i;
138 function update_location( string ) {
139 var hash = eval('('+string+')');
140 document.getElementById('address1').value = hash['address1'];
141 document.getElementById('address2').value = hash['address2'];
142 document.getElementById('city').value = hash['city'];
143 document.getElementById('zip').value = hash['zip'];
145 var country_el = document.getElementById('country');
147 changeSelect( country_el, hash['country'] );
149 country_changed( country_el,
150 fix_state_factory( hash['state'],
159 <<%$th%> ALIGN="right"><% $opt{'label'} || 'Service location' %></<%$th%>>
161 <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
162 % if ( !$prospect_main ) {
163 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
165 % if ( $opt{'is_optional'} ) {
166 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
169 % foreach my $loc ( @cust_location ) {
170 <OPTION VALUE="<% $loc->locationnum %>"
171 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
176 <% $locationnum == -1 ? 'SELECTED' : '' %>
183 <% include('/elements/location.html',
184 'object' => $cust_location,
185 #'onchange' ? probably not
186 'disabled' => $disabled,
188 'no_bold' => $opt{'no_bold'},
189 'alt_format' => $opt{'alt_format'},
195 my $conf = new FS::Conf;
196 my $countrydefault = $conf->config('countrydefault') || 'US';
197 my $statedefault = $conf->config('statedefault')
198 || ($countrydefault eq 'US' ? 'CA' : '');
201 my $cgi = $opt{'cgi'};
202 my $cust_pkg = $opt{'cust_pkg'};
203 my $cust_main = $opt{'cust_main'};
204 my $prospect_main = $opt{'prospect_main'};
206 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
208 my $locationnum = '';
209 if ( $cgi->param('error') ) {
210 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
213 if ( length($opt{'curr_value'}) ) {
214 $locationnum = $opt{'curr_value'};
215 } elsif ($prospect_main) {
216 my @cust_location = $prospect_main->cust_location;
217 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
219 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
224 #probably could use explicit controls
225 # (cust_main locations not editable for tax reasons)
226 my $editable = $cust_main ? 0 : 1; #could use explicit control
227 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
229 my @location_fields = qw( address1 address2 city county state zip country );
230 if ( $opt{'alt_format'} ) {
231 push @location_fields, qw( location_type location_number location_kind );
235 if ( $locationnum && $locationnum > 0 ) {
236 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
237 or die "unknown locationnum";
239 $cust_location = new FS::cust_location;
240 if ( $locationnum == -1 ) {
241 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
242 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
243 my $pkg_location = $cust_pkg->cust_location;
244 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
245 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
246 } elsif ( $cust_main ) {
247 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
251 my $location_sort = sub {
252 $a->country cmp $b->country
253 or lc($a->city) cmp lc($b->city)
254 or lc($a->address1) cmp lc($b->address1)
255 or lc($a->address2) cmp lc($b->address2)
258 my @cust_location = ();
259 push @cust_location, $cust_main->cust_location if $cust_main;
260 push @cust_location, $prospect_main->cust_location if $prospect_main;
261 push @cust_location, $cust_location
262 if !$cust_main && $cust_location && $cust_location->locationnum > 0
263 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
265 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
269 || ( $editable && $locationnum )
270 || ( $prospect_main && !$opt{'is_optional'} && !@cust_location && $addnew )
275 my $th = $opt{'no_bold'} ? 'TD' : 'TH';