5 include('/elements/tr-select-cust_location.html',
7 'cust_main' => $cust_main,
12 <% include('/elements/xmlhttp.html',
13 'url' => $p.'misc/location.cgi',
14 'subs' => [ 'get_location' ],
18 <SCRIPT TYPE="text/javascript">
20 function locationnum_changed(what) {
21 var locationnum = what.options[what.selectedIndex].value;
22 if ( locationnum == -1 ) {
24 % for (@location_fields) {
25 what.form.<%$_%>.disabled = false;
26 what.form.<%$_%>.style.backgroundColor = '#ffffff';
29 what.form.address1.value = '';
30 what.form.address2.value = '';
31 what.form.city.value = '';
32 what.form.zip.value = '';
34 changeSelect(what.form.country, <% $countrydefault |js_string %>);
36 country_changed( what.form.country,
37 fix_state_factory( <% $statedefault |js_string %>,
44 if ( locationnum == 0 ) {
45 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
46 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
47 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
48 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
50 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
52 country_changed( what.form.country,
53 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
54 <% $cust_main->get($prefix.'county') | js_string %>
59 get_location( locationnum, update_location );
62 %#sleep/wait until dropdowns are updated?
63 % for (@location_fields) {
64 what.form.<%$_%>.disabled = true;
65 what.form.<%$_%>.style.backgroundColor = '#dddddd';
71 function fix_state_factory (state, county) {
72 function fix_state() {
73 var state_el = document.getElementById('state');
74 changeSelect(state_el, state);
75 state_changed(state_el, fix_county_factory(county) );
80 function fix_county_factory(county) {
81 function fix_county() {
82 var county_el = document.getElementById('county');
83 if ( county.length > 0 ) {
84 changeSelect(county_el, county );
86 county_el.selectedIndex = 0;
92 function changeSelect(what, value) {
93 for ( var i=0; i<what.length; i++) {
94 if ( what.options[i].value == value ) {
95 what.selectedIndex = i;
100 function update_location( string ) {
101 var hash = eval('('+string+')');
102 document.getElementById('address1').value = hash['address1'];
103 document.getElementById('address2').value = hash['address2'];
104 document.getElementById('city').value = hash['city'];
105 document.getElementById('zip').value = hash['zip'];
107 var country_el = document.getElementById('country');
109 changeSelect( country_el, hash['country'] );
111 country_changed( country_el,
112 fix_state_factory( hash['state'],
121 <TH ALIGN="right">Service location</TH>
123 <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
124 <OPTION VALUE="">(default service address)
125 % foreach my $loc ( $cust_main->cust_location ) {
126 <OPTION VALUE="<% $loc->locationnum %>"
127 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
131 <% $locationnum == -1 ? 'SELECTED' : '' %>
137 <% include('/elements/location.html',
138 'object' => $cust_location,
139 #'onchange' ? probably not
140 'disabled' => ( $locationnum == -1 ? '' : 'DISABLED' ),
147 my @location_fields = qw( address1 address2 city county state zip country );
152 my $conf = new FS::Conf;
153 my $countrydefault = $conf->config('countrydefault') || 'US';
154 my $statedefault = $conf->config('statedefault')
155 || ($countrydefault eq 'US' ? 'CA' : '');
158 my $cgi = $opt{'cgi'};
159 my $cust_main = $opt{'cust_main'};
161 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
163 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
164 my $locationnum = $1;
166 if ( $locationnum && $locationnum != -1 ) {
167 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
168 or die "unknown locationnum";
170 $cust_location = new FS::cust_location;
171 if ( $locationnum == -1 ) {
172 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
174 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;