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 locationnum_changed(what) {
27 var locationnum = what.options[what.selectedIndex].value;
28 if ( locationnum == -2 ) {
29 % for (@location_fields, 'city_select') {
30 what.form.<%$_%>.disabled = true;
31 var ftype = what.form.<%$_%>.tagName;
32 if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
33 else what.form.<%$_%>.value = '';
34 what.form.<%$_%>.style.backgroundColor = '#dddddd';
39 if ( locationnum == -1 ) {
41 % for (@location_fields, 'city_select') {
42 what.form.<%$_%>.disabled = false;
43 what.form.<%$_%>.style.backgroundColor = '#ffffff';
44 var ftype = what.form.<%$_%>.tagName;
45 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
48 changeSelect(what.form.country, <% $countrydefault |js_string %>);
50 country_changed( what.form.country,
51 fix_state_factory( <% $statedefault |js_string %>,
58 if ( locationnum == 0 ) {
60 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
61 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
62 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
63 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
65 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
67 country_changed( what.form.country,
68 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
69 <% $cust_main->get($prefix.'county') | js_string %>
75 get_location( locationnum, update_location );
79 if ( locationnum == 0 ) {
82 %#sleep/wait until dropdowns are updated?
83 % for (@location_fields, 'city_select') {
84 what.form.<%$_%>.disabled = true;
85 what.form.<%$_%>.style.backgroundColor = '#dddddd';
91 %#sleep/wait until dropdowns are updated?
92 % for (@location_fields, 'city_select') {
93 what.form.<%$_%>.disabled = false;
94 what.form.<%$_%>.style.backgroundColor = '#ffffff';
103 function fix_state_factory (state, county) {
104 function fix_state() {
105 var state_el = document.getElementById('state');
106 changeSelect(state_el, state);
107 state_changed(state_el, fix_county_factory(county) );
112 function fix_county_factory(county) {
113 function fix_county() {
114 var county_el = document.getElementById('county');
115 if ( county.length > 0 ) {
116 changeSelect(county_el, county );
118 county_el.selectedIndex = 0;
120 county_changed(county_el);
125 function changeSelect(what, value) {
126 for ( var i=0; i<what.length; i++) {
127 if ( what.options[i].value == value ) {
128 what.selectedIndex = i;
133 function update_location( string ) {
134 var hash = eval('('+string+')');
135 document.getElementById('address1').value = hash['address1'];
136 document.getElementById('address2').value = hash['address2'];
137 document.getElementById('city').value = hash['city'];
138 document.getElementById('zip').value = hash['zip'];
140 var country_el = document.getElementById('country');
142 changeSelect( country_el, hash['country'] );
144 country_changed( country_el,
145 fix_state_factory( hash['state'],
154 <<%$th%> ALIGN="right"><% $opt{'label'} || 'Service location' %></<%$th%>>
156 <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
157 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
158 % if ( $opt{'is_optional'} ) {
159 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
161 % my @locations = $cust_main ? $cust_main->cust_location : ();
162 % push @locations, $cust_location
163 % if !$cust_main && $cust_location && $cust_location->locationnum>0;
164 % foreach my $loc ( sort $location_sort @locations ) {
165 % next if $loc->disabled;
166 <OPTION VALUE="<% $loc->locationnum %>"
167 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
172 <% $locationnum == -1 ? 'SELECTED' : '' %>
179 <% include('/elements/location.html',
180 'object' => $cust_location,
181 #'onchange' ? probably not
182 'disabled' => $disabled,
184 'no_bold' => $opt{'no_bold'},
185 'alt_format' => $opt{'alt_format'},
191 my $conf = new FS::Conf;
192 my $countrydefault = $conf->config('countrydefault') || 'US';
193 my $statedefault = $conf->config('statedefault')
194 || ($countrydefault eq 'US' ? 'CA' : '');
197 my $cgi = $opt{'cgi'};
198 my $cust_pkg = $opt{'cust_pkg'};
199 my $cust_main = $opt{'cust_main'};
200 my $prospect_main = $opt{'prospect_main'};
202 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
205 if ( length($opt{'curr_value'}) ) {
206 $locationnum = $opt{'curr_value'};
208 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
212 #probably could use explicit controls
213 # (cust_main locations not editable for tax reasons)
214 my $editable = $cust_main ? 0 : 1; #could use explicit control
215 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
217 my @location_fields = qw( address1 address2 city county state zip country );
218 if ( $opt{'alt_format'} ) {
219 push @location_fields, qw( location_type location_number location_kind );
223 if ( $locationnum && $locationnum > 0 ) {
224 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
225 or die "unknown locationnum";
227 $cust_location = new FS::cust_location;
228 if ( $locationnum == -1 ) {
229 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
230 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
231 my $pkg_location = $cust_pkg->cust_location;
232 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
233 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
234 } elsif ( $cust_main ) {
235 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
239 my $location_sort = sub {
240 $a->country cmp $b->country
241 or lc($a->city) cmp lc($b->city)
242 or lc($a->address1) cmp lc($b->address1)
243 or lc($a->address2) cmp lc($b->address2)
246 my $disabled = ( $locationnum < 0 || ($editable && $locationnum) )
250 my $th = $opt{'no_bold'} ? 'TD' : 'TH';