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 == -1 ) {
30 % for (@location_fields, 'city_select') {
31 what.form.<%$_%>.disabled = false;
32 what.form.<%$_%>.style.backgroundColor = '#ffffff';
35 what.form.address1.value = '';
36 what.form.address2.value = '';
37 what.form.city.value = '';
38 what.form.zip.value = '';
40 changeSelect(what.form.country, <% $countrydefault |js_string %>);
42 country_changed( what.form.country,
43 fix_state_factory( <% $statedefault |js_string %>,
50 if ( locationnum == 0 ) {
52 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
53 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
54 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
55 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
57 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
59 country_changed( what.form.country,
60 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
61 <% $cust_main->get($prefix.'county') | js_string %>
67 get_location( locationnum, update_location );
71 if ( locationnum == 0 ) {
74 %#sleep/wait until dropdowns are updated?
75 % for (@location_fields, 'city_select') {
76 what.form.<%$_%>.disabled = true;
77 what.form.<%$_%>.style.backgroundColor = '#dddddd';
83 %#sleep/wait until dropdowns are updated?
84 % for (@location_fields, 'city_select') {
85 what.form.<%$_%>.disabled = false;
86 what.form.<%$_%>.style.backgroundColor = '#ffffff';
95 function fix_state_factory (state, county) {
96 function fix_state() {
97 var state_el = document.getElementById('state');
98 changeSelect(state_el, state);
99 state_changed(state_el, fix_county_factory(county) );
104 function fix_county_factory(county) {
105 function fix_county() {
106 var county_el = document.getElementById('county');
107 if ( county.length > 0 ) {
108 changeSelect(county_el, county );
110 county_el.selectedIndex = 0;
112 county_changed(county_el);
117 function changeSelect(what, value) {
118 for ( var i=0; i<what.length; i++) {
119 if ( what.options[i].value == value ) {
120 what.selectedIndex = i;
125 function update_location( string ) {
126 var hash = eval('('+string+')');
127 document.getElementById('address1').value = hash['address1'];
128 document.getElementById('address2').value = hash['address2'];
129 document.getElementById('city').value = hash['city'];
130 document.getElementById('zip').value = hash['zip'];
132 var country_el = document.getElementById('country');
134 changeSelect( country_el, hash['country'] );
136 country_changed( country_el,
137 fix_state_factory( hash['state'],
146 <TH ALIGN="right">Service location</TH>
148 <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
149 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
150 % my @locations = $cust_main ? $cust_main->cust_location : ();
151 % push @locations, $cust_location
152 % if !$cust_main && $cust_location && $cust_location->locationnum>0;
153 % foreach my $loc ( @locations ) {
154 <OPTION VALUE="<% $loc->locationnum %>"
155 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
160 <% $locationnum == -1 ? 'SELECTED' : '' %>
167 <% include('/elements/location.html',
168 'object' => $cust_location,
169 #'onchange' ? probably not
170 'disabled' => $disabled,
177 my @location_fields = qw( address1 address2 city county state zip country );
182 my $conf = new FS::Conf;
183 my $countrydefault = $conf->config('countrydefault') || 'US';
184 my $statedefault = $conf->config('statedefault')
185 || ($countrydefault eq 'US' ? 'CA' : '');
188 my $cgi = $opt{'cgi'};
189 my $cust_main = $opt{'cust_main'};
190 my $prospect_main = $opt{'prospect_main'};
192 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
195 if ( length($opt{'curr_value'}) ) {
196 $locationnum = $opt{'curr_value'};
198 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
202 #probably could use explicit controls
203 # (cust_main locations not editable for tax reasons)
204 my $editable = $cust_main ? 0 : 1; #could use explicit control
205 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
208 if ( $locationnum && $locationnum != -1 ) {
209 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
210 or die "unknown locationnum";
212 $cust_location = new FS::cust_location;
213 if ( $locationnum == -1 ) {
214 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
215 } elsif ( $cust_main ) {
216 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
220 my $disabled = ( $locationnum == -1 || ($editable && $locationnum) )