5 <& /elements/tr-select-cust_location.html,
8 'cust_main' => $cust_main,
10 'prospect_main' => $prospect_main,
13 'empty_label' => '(default service address)',
18 <& /elements/xmlhttp.html,
19 'url' => $p.'misc/location.cgi',
20 'subs' => [ 'get_location' ],
23 <SCRIPT TYPE="text/javascript">
25 function location_disable(what) {
26 % for (@location_fields, 'city_select') {
27 what.form.<%$_%>.disabled = true;
28 var ftype = what.form.<%$_%>.tagName;
29 if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
30 else what.form.<%$_%>.value = '';
31 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
33 if(what.form.enter_censustract) {
34 what.form.enter_censustract.disabled = true;
38 function location_clear(what) {
39 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
40 var ftype = what.form.<%$_%>.tagName;
41 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
43 if(what.form.enter_censustract) {
44 what.form.enter_censustract.value = '';
46 % if ( $opt{'alt_format'} ) {
47 changeSelect(what.form.location_kind, '');
48 changeSelect(what.form.location_type, '');
49 what.form.location_number.value = '';
53 function location_enable(what) {
54 % for (grep { $_ ne 'location_number' } @location_fields, 'city_select') {
55 what.form.<%$_%>.disabled = false;
56 var ftype = what.form.<%$_%>.tagName;
57 if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
59 if(what.form.enter_censustract) {
60 what.form.enter_censustract.disabled = false;
62 % if ( $opt{'alt_format'} ) {
63 if ( what.form.location_type &&
64 what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
65 what.form.location_number.disabled = false;
66 what.form.location_number.style.backgroundColor = '#ffffff';
71 function locationnum_changed(what) {
72 var locationnum = what.options[what.selectedIndex].value;
73 if ( locationnum == -2 ) { //(not required)
74 location_disable(what);
77 if ( locationnum == -1 ) { //Add new location
80 changeSelect(what.form.country, <% $countrydefault |js_string %>);
82 country_changed( what.form.country,
83 fix_state_factory( <% $statedefault |js_string %>,
88 location_enable(what);
91 if ( locationnum == -3 ) { //service address location for qualificaitons
92 what.form.address1.value = <% $cust_location->address1 |js_string %>;
93 what.form.address2.value = <% $cust_location->address2 |js_string %>;
94 what.form.city.value = <% $cust_location->city |js_string %>;
95 what.form.zip.value = <% $cust_location->zip |js_string %>;
96 % if ( $opt{'alt_format'} ) {
97 what.form.location_number.value = <% $cust_location->location_number |js_string %>;
98 changeSelect(what.form.location_kind, <% $cust_location->location_kind |js_string %> );
99 changeSelect(what.form.location_type, <% $cust_location->location_type |js_string %> );
102 changeSelect(what.form.country, <% $cust_location->country | js_string %> );
104 country_changed( what.form.country,
105 fix_state_factory( <% $cust_location->state | js_string %>,
106 <% $cust_location->county | js_string %>
109 location_enable(what);
113 %# default service address is now just another location
114 get_location( locationnum, update_location );
117 if ( locationnum == 0 ) {
120 % #sleep/wait until dropdowns are updated?
121 location_disable(what);
126 % #sleep/wait until dropdowns are updated?
127 location_enable(what);
134 function fix_state_factory (state, county) {
135 function fix_state() {
136 var state_el = document.getElementById('state');
137 changeSelect(state_el, state);
138 state_changed(state_el, fix_county_factory(county) );
143 function fix_county_factory(county) {
144 function fix_county() {
145 var county_el = document.getElementById('county');
146 if ( county.length > 0 ) {
147 changeSelect(county_el, county );
149 county_el.selectedIndex = 0;
151 county_changed(county_el);
156 function changeSelect(what, value) {
157 for ( var i=0; i<what.length; i++) {
158 if ( what.options[i].value == value ) {
159 what.selectedIndex = i;
164 var location_fields = <% encode_json(\@location_fields) %>;
165 function update_location( string ) {
166 var hash = JSON.parse(string);
167 for(var i = 0; i < location_fields.length; i++) {
168 var f = location_fields[i];
169 if (hash[f] && document.getElementById(f)) {
170 document.getElementById(f).value = hash[f];
173 country_changed( document.getElementById('country'),
174 fix_state_factory( hash['state'],
183 <<%$th%> ALIGN="right"><% $opt{'label'} || emt('Service location') %></<%$th%>>
186 <& /elements/select-cust_location.html,
188 'curr_value' => $locationnum,
189 'cust_location' => \@cust_location,
190 'onchange' => 'locationnum_changed(this);',
196 <& /elements/location.html,
197 'object' => $cust_location,
198 #'onchange' ? probably not
199 'disabled' => $disabled,
201 'no_bold' => $opt{'no_bold'},
202 'alt_format' => $opt{'alt_format'},
203 'enable_coords' => 1,
204 'enable_censustract' => 1,
205 'enable_district' => $conf->exists('tax_district_method') ? 1 : 0,
208 <SCRIPT TYPE="text/javascript">
209 % if ( $prospect_main ) { # && ! $opt{is_optional} ) {
211 changeSelect(document.getElementById('country'), <% $cust_location->country || $countrydefault |js_string %>);
213 country_changed( document.getElementById('country'),
214 fix_state_factory( <% $cust_location->state || $statedefault |js_string %>,
218 % } elsif ( $locationnum != -1 ) {
219 locationnum_changed(document.getElementById('locationnum'));
225 my $conf = new FS::Conf;
226 my $countrydefault = $conf->config('countrydefault') || 'US';
227 my $statedefault = $conf->config('statedefault')
228 || ($countrydefault eq 'US' ? 'CA' : '');
231 my $cgi = $opt{'cgi'};
232 my $cust_pkg = $opt{'cust_pkg'};
233 my $cust_main = $opt{'cust_main'};
234 my $prospect_main = $opt{'prospect_main'};
235 die "cust_main or prospect_main required" unless $cust_main or $prospect_main;
237 my $locationnum = '';
238 if ( $cgi->param('error') ) {
239 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
242 if ( length($opt{'curr_value'}) ) {
243 $locationnum = $opt{'curr_value'};
244 } elsif ($prospect_main) {
245 my @cust_location = $prospect_main->cust_location;
246 $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
248 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
249 $locationnum = $1 || $cust_main->ship_locationnum;
253 #probably could use explicit controls
254 # (cust_main locations not editable for tax reasons)
255 my $editable = $cust_main ? 0 : 1; #could use explicit control
256 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
258 my @location_fields = FS::cust_main->location_fields;
259 if ( $opt{'alt_format'} ) {
260 push @location_fields, qw( location_type location_number location_kind );
263 my $cust_location; #the one that shows by default in the location edit space
264 if ( $locationnum && $locationnum > 0 ) {
265 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
266 or die "unknown locationnum";
268 $cust_location = new FS::cust_location;
269 if ( $locationnum == -1 || $locationnum == -3 ) {
270 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
271 } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
272 my $pkg_location = $cust_pkg->cust_location;
273 $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
274 $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
275 } elsif ( $cust_main ) {
276 $cust_location = $cust_main->ship_location; #I think
280 $cust_location->coord_auto('Y');
282 my $location_sort = sub {
283 #enabled w/label_prefix _location # $a->locationname cmp $b->locationname
285 $a->country cmp $b->country
286 or lc($a->state) cmp lc($b->state)
287 or lc($a->city) cmp lc($b->city)
288 or lc($a->county) cmp lc($b->county)
289 or lc($a->address1) cmp lc($b->address1)
290 or lc($a->address2) cmp lc($b->address2)
294 push @cust_location, $cust_main->cust_location if $cust_main;
295 push @cust_location, $prospect_main->cust_location if $prospect_main;
296 push @cust_location, $cust_location
297 if !$cust_main && $cust_location && $cust_location->locationnum > 0
298 && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
300 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
302 $cust_location = $cust_location[0]
304 && !$opt{'is_optional'}
309 || ( $editable && $locationnum )
311 && !$opt{'is_optional'} && !@cust_location && $addnew
317 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
318 $cust_location->locationnum(-3);
319 $cust_location->alternize;
320 push @cust_location, $cust_location;
323 my $th = $opt{'no_bold'} ? 'TD' : 'TH';