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"
150 onChange = "locationnum_changed(this);"
152 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
153 % my @locations = $cust_main ? $cust_main->cust_location : ();
154 % push @locations, $cust_location
155 % if !$cust_main && $cust_location && $cust_location->locationnum>0;
156 % foreach my $loc ( sort $location_sort @locations ) {
157 % next if $loc->disabled;
158 <OPTION VALUE="<% $loc->locationnum %>"
159 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
164 <% $locationnum == -1 ? 'SELECTED' : '' %>
171 <% include('/elements/location.html',
172 'object' => $cust_location,
173 #'onchange' ? probably not
174 'disabled' => $disabled,
176 'no_bold' => $opt{'no_bold'},
182 my @location_fields = qw( address1 address2 city county state zip country );
187 my $conf = new FS::Conf;
188 my $countrydefault = $conf->config('countrydefault') || 'US';
189 my $statedefault = $conf->config('statedefault')
190 || ($countrydefault eq 'US' ? 'CA' : '');
193 my $cgi = $opt{'cgi'};
194 my $cust_pkg = $opt{'cust_pkg'};
195 my $cust_main = $opt{'cust_main'};
196 my $prospect_main = $opt{'prospect_main'};
198 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
201 if ( length($opt{'curr_value'}) ) {
202 $locationnum = $opt{'curr_value'};
204 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
208 #probably could use explicit controls
209 # (cust_main locations not editable for tax reasons)
210 my $editable = $cust_main ? 0 : 1; #could use explicit control
211 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
214 if ( $locationnum && $locationnum != -1 ) {
215 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
216 or die "unknown locationnum";
218 $cust_location = new FS::cust_location;
219 if ( $locationnum == -1 ) {
220 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
221 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
222 my $pkg_location = $cust_pkg->cust_location;
223 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
224 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
225 } elsif ( $cust_main ) {
226 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
230 my $location_sort = sub {
231 $a->country cmp $b->country
232 or lc($a->city) cmp lc($b->city)
233 or lc($a->address1) cmp lc($b->address1)
234 or lc($a->address2) cmp lc($b->address2)
237 my $disabled = ( $locationnum == -1 || ($editable && $locationnum) )
241 my $th = $opt{'no_bold'} ? 'TD' : 'TH';