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"><% $opt{'label'} || '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 ( sort $location_sort @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,
172 'no_bold' => $opt{'no_bold'},
178 my @location_fields = qw( address1 address2 city county state zip country );
183 my $conf = new FS::Conf;
184 my $countrydefault = $conf->config('countrydefault') || 'US';
185 my $statedefault = $conf->config('statedefault')
186 || ($countrydefault eq 'US' ? 'CA' : '');
189 my $cgi = $opt{'cgi'};
190 my $cust_pkg = $opt{'cust_pkg'};
191 my $cust_main = $opt{'cust_main'};
192 my $prospect_main = $opt{'prospect_main'};
194 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
197 if ( length($opt{'curr_value'}) ) {
198 $locationnum = $opt{'curr_value'};
200 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
204 #probably could use explicit controls
205 # (cust_main locations not editable for tax reasons)
206 my $editable = $cust_main ? 0 : 1; #could use explicit control
207 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
210 if ( $locationnum && $locationnum != -1 ) {
211 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
212 or die "unknown locationnum";
214 $cust_location = new FS::cust_location;
215 if ( $locationnum == -1 ) {
216 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
217 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
218 my $pkg_location = $cust_pkg->cust_location;
219 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
220 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
221 } elsif ( $cust_main ) {
222 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
226 my $location_sort = sub {
227 $a->country cmp $b->country
228 or $a->city cmp $b->city
229 or $a->address1 cmp $b->address1
230 or $a->address2 cmp $b->address2
233 my $disabled = ( $locationnum == -1 || ($editable && $locationnum) )
237 my $th = $opt{'no_bold'} ? 'TD' : 'TH';