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 % if ( !$prospect_main ) {
158 <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
160 % if ( $opt{'is_optional'} ) {
161 <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
163 % my @locations = $cust_main ? $cust_main->cust_location : ();
164 % @locations = qsearch('cust_location',
165 % { 'prospectnum' => $prospect_main->prospectnum } )
167 % push @locations, $cust_location
168 % if !$cust_main && $cust_location && $cust_location->locationnum>0;
169 % foreach my $loc ( sort $location_sort @locations ) {
170 % next if $loc->disabled;
171 <OPTION VALUE="<% $loc->locationnum %>"
172 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
177 <% $locationnum == -1 ? 'SELECTED' : '' %>
184 <% include('/elements/location.html',
185 'object' => $cust_location,
186 #'onchange' ? probably not
187 'disabled' => $disabled,
189 'no_bold' => $opt{'no_bold'},
190 'alt_format' => $opt{'alt_format'},
196 my $conf = new FS::Conf;
197 my $countrydefault = $conf->config('countrydefault') || 'US';
198 my $statedefault = $conf->config('statedefault')
199 || ($countrydefault eq 'US' ? 'CA' : '');
202 my $cgi = $opt{'cgi'};
203 my $cust_pkg = $opt{'cust_pkg'};
204 my $cust_main = $opt{'cust_main'};
205 my $prospect_main = $opt{'prospect_main'};
207 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
210 if ( length($opt{'curr_value'}) ) {
211 $locationnum = $opt{'curr_value'};
213 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
217 #probably could use explicit controls
218 # (cust_main locations not editable for tax reasons)
219 my $editable = $cust_main ? 0 : 1; #could use explicit control
220 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
222 my @location_fields = qw( address1 address2 city county state zip country );
223 if ( $opt{'alt_format'} ) {
224 push @location_fields, qw( location_type location_number location_kind );
228 if ( $locationnum && $locationnum > 0 ) {
229 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
230 or die "unknown locationnum";
232 $cust_location = new FS::cust_location;
233 if ( $locationnum == -1 ) {
234 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
235 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
236 my $pkg_location = $cust_pkg->cust_location;
237 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
238 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
239 } elsif ( $cust_main ) {
240 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
244 my $location_sort = sub {
245 $a->country cmp $b->country
246 or lc($a->city) cmp lc($b->city)
247 or lc($a->address1) cmp lc($b->address1)
248 or lc($a->address2) cmp lc($b->address2)
251 my $disabled = ( $locationnum < 0 || ($editable && $locationnum) )
255 my $th = $opt{'no_bold'} ? 'TD' : 'TH';