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_enable(what) {
38 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
39 what.form.<%$_%>.disabled = false;
40 var ftype = what.form.<%$_%>.tagName;
41 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
42 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
45 if ( what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
46 what.form.location_number.disabled = false;
47 what.form.location_number.style.backgroundColor = '#ffffff';
49 what.form.location_number.value = '';
52 function locationnum_changed(what) {
53 var locationnum = what.options[what.selectedIndex].value;
54 if ( locationnum == -2 ) {
55 location_disable(what);
58 if ( locationnum == -1 ) {
59 location_enable(what);
61 changeSelect(what.form.country, <% $countrydefault |js_string %>);
63 country_changed( what.form.country,
64 fix_state_factory( <% $statedefault |js_string %>,
71 if ( locationnum == 0 ) {
73 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
74 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
75 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
76 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
78 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
80 country_changed( what.form.country,
81 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
82 <% $cust_main->get($prefix.'county') | js_string %>
88 get_location( locationnum, update_location );
92 if ( locationnum == 0 ) {
95 % #sleep/wait until dropdowns are updated?
96 location_disable(what);
101 % #sleep/wait until dropdowns are updated?
102 location_enable(what);
109 function fix_state_factory (state, county) {
110 function fix_state() {
111 var state_el = document.getElementById('state');
112 changeSelect(state_el, state);
113 state_changed(state_el, fix_county_factory(county) );
118 function fix_county_factory(county) {
119 function fix_county() {
120 var county_el = document.getElementById('county');
121 if ( county.length > 0 ) {
122 changeSelect(county_el, county );
124 county_el.selectedIndex = 0;
126 county_changed(county_el);
131 function changeSelect(what, value) {
132 for ( var i=0; i<what.length; i++) {
133 if ( what.options[i].value == value ) {
134 what.selectedIndex = i;
139 function update_location( string ) {
140 var hash = eval('('+string+')');
141 document.getElementById('address1').value = hash['address1'];
142 document.getElementById('address2').value = hash['address2'];
143 document.getElementById('city').value = hash['city'];
144 document.getElementById('zip').value = hash['zip'];
146 var country_el = document.getElementById('country');
148 changeSelect( country_el, hash['country'] );
150 country_changed( country_el,
151 fix_state_factory( hash['state'],
160 <<%$th%> ALIGN="right"><% $opt{'label'} || 'Service location' %></<%$th%>>
162 <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
163 % if ( !$prospect_main && !$opt{'disable_empty'} ) {
164 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
166 % if ( $opt{'is_optional'} ) {
167 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
170 % foreach my $loc ( @cust_location ) {
171 <OPTION VALUE="<% $loc->locationnum %>"
172 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
177 <% $locationnum == -1 ? 'SELECTED' : '' %>
184 <% include('/elements/location.html',
185 'object' => $cust_location,
186 #'onchange' ? probably not
187 'disabled' => $disabled,
189 'no_bold' => $opt{'no_bold'},
190 'alt_format' => $opt{'alt_format'},
196 my $conf = new FS::Conf;
197 my $countrydefault = $conf->config('countrydefault') || 'US';
198 my $statedefault = $conf->config('statedefault')
199 || ($countrydefault eq 'US' ? 'CA' : '');
202 my $cgi = $opt{'cgi'};
203 my $cust_pkg = $opt{'cust_pkg'};
204 my $cust_main = $opt{'cust_main'};
205 my $prospect_main = $opt{'prospect_main'};
207 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
209 my $locationnum = '';
210 if ( $cgi->param('error') ) {
211 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
214 if ( length($opt{'curr_value'}) ) {
215 $locationnum = $opt{'curr_value'};
216 } elsif ($prospect_main) {
217 my @cust_location = $prospect_main->cust_location;
218 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
220 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
225 #probably could use explicit controls
226 # (cust_main locations not editable for tax reasons)
227 my $editable = $cust_main ? 0 : 1; #could use explicit control
228 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
230 my @location_fields = qw( address1 address2 city county state zip country );
231 if ( $opt{'alt_format'} ) {
232 push @location_fields, qw( location_type location_number location_kind );
236 if ( $locationnum && $locationnum > 0 ) {
237 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
238 or die "unknown locationnum";
240 $cust_location = new FS::cust_location;
241 if ( $locationnum == -1 ) {
242 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
243 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
244 my $pkg_location = $cust_pkg->cust_location;
245 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
246 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
247 } elsif ( $cust_main ) {
248 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
252 my $location_sort = sub {
253 $a->country cmp $b->country
254 or lc($a->city) cmp lc($b->city)
255 or lc($a->address1) cmp lc($b->address1)
256 or lc($a->address2) cmp lc($b->address2)
259 my @cust_location = ();
260 push @cust_location, $cust_main->cust_location if $cust_main;
261 push @cust_location, $prospect_main->cust_location if $prospect_main;
262 push @cust_location, $cust_location
263 if !$cust_main && $cust_location && $cust_location->locationnum > 0
264 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
266 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
270 || ( $editable && $locationnum )
271 || ( ( $prospect_main || $opt{'disable_empty'} )
272 && !$opt{'is_optional'} && !@cust_location && $addnew
278 my $th = $opt{'no_bold'} ? 'TD' : 'TH';