fix location selection for prospects, #940
[freeside.git] / httemplate / elements / tr-select-cust_location.html
1 <%doc>
2
3 Example:
4
5   include('/elements/tr-select-cust_location.html',
6             'cgi'       => $cgi,
7
8             'cust_main'     => $cust_main,
9             #or
10             'prospect_main' => $prospect_main,
11
12             #optional
13             'empty_label'   => '(default service address)',
14          )
15
16 </%doc>
17
18 <% include('/elements/xmlhttp.html',
19               'url'  => $p.'misc/location.cgi',
20               'subs' => [ 'get_location' ],
21            )
22 %>
23
24 <SCRIPT TYPE="text/javascript">
25
26   function location_disable(what) {
27 %   for (@location_fields, 'city_select') { 
28       what.form.<%$_%>.disabled = true;
29       var ftype = what.form.<%$_%>.tagName;
30       if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
31       else what.form.<%$_%>.value = '';
32       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
33 %   } 
34   }
35
36   function location_clear(what) {
37 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
38       var ftype = what.form.<%$_%>.tagName;
39       if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
40 %   }
41 %   if ( $opt{'alt_format'} ) {
42       changeSelect(what.form.location_kind, '');
43       changeSelect(what.form.location_type, '');
44       what.form.location_number.value = '';
45 %   }
46   }
47
48   function location_enable(what) {
49 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
50       what.form.<%$_%>.disabled = false;
51       var ftype = what.form.<%$_%>.tagName;
52       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
53 %   } 
54 %   if ( $opt{'alt_format'} ) {
55     if ( what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
56       what.form.location_number.disabled = false;
57       what.form.location_number.style.backgroundColor = '#ffffff';
58     }
59 %   }
60   }
61
62   function locationnum_changed(what) {
63     var locationnum = what.options[what.selectedIndex].value;
64     if ( locationnum == -2 ) { //(not required)
65       location_disable(what);
66       return;
67     }
68     if ( locationnum == -1 ) { //Add new location
69       location_clear(what);
70
71       changeSelect(what.form.country, <% $countrydefault |js_string %>);
72
73       country_changed( what.form.country,
74                        fix_state_factory( <% $statedefault |js_string %>,
75                                           ''
76                                         )
77                      );
78
79       location_enable(what);
80       return;
81     }
82     if ( locationnum == -3 ) { //service address location for qualificaitons
83       what.form.address1.value = <% $cust_location->address1 |js_string %>;
84       what.form.address2.value = <% $cust_location->address2 |js_string %>;
85       what.form.city.value = <% $cust_location->city |js_string %>;
86       what.form.zip.value = <% $cust_location->zip |js_string %>;
87 %     if ( $opt{'alt_format'} ) {
88         what.form.location_number.value = <% $cust_location->location_number |js_string %>;
89         changeSelect(what.form.location_kind, <% $cust_location->location_kind |js_string %> );
90         changeSelect(what.form.location_type, <% $cust_location->location_type |js_string %> );
91 %     }
92
93       changeSelect(what.form.country, <% $cust_location->country | js_string %> );
94
95       country_changed( what.form.country,
96                        fix_state_factory( <% $cust_location->state | js_string %>,
97                                           <% $cust_location->county | js_string %>
98                                         )
99                      );
100       location_enable(what);
101       return;
102     }
103
104 %# default service address is now just another location
105     get_location( locationnum, update_location );
106
107 %   if ( $editable ) {
108       if ( locationnum == 0 ) {
109 %   }
110
111 %       #sleep/wait until dropdowns are updated?
112         location_disable(what);
113
114 %   if ( $editable ) {
115       } else {
116
117 %       #sleep/wait until dropdowns are updated?
118         location_enable(what);
119
120       }
121 %   }
122
123   }
124
125   function fix_state_factory (state, county) {
126     function fix_state() {
127       var state_el = document.getElementById('state');
128       changeSelect(state_el, state);
129       state_changed(state_el, fix_county_factory(county) );
130     }
131     return fix_state;
132   }
133
134   function fix_county_factory(county) {
135     function fix_county() {
136       var county_el = document.getElementById('county');
137       if ( county.length > 0 ) {
138         changeSelect(county_el, county );
139       } else {
140         county_el.selectedIndex = 0;
141       }
142       county_changed(county_el);
143     }
144     return fix_county;
145   }
146
147   function changeSelect(what, value) {
148     for ( var i=0; i<what.length; i++) {
149       if ( what.options[i].value == value ) {
150         what.selectedIndex = i;
151       }
152     }
153   }
154
155   function update_location( string ) {
156     var hash = eval('('+string+')');
157     document.getElementById('address1').value = hash['address1'];
158     document.getElementById('city').value     = hash['city'];
159     document.getElementById('zip').value      = hash['zip'];
160
161 %   if ( $opt{'alt_format'} ) {
162       changeSelect( document.getElementById('location_kind'), hash['location_kind']);
163       changeSelect( document.getElementById('location_type'), hash['location_type']);
164       document.getElementById('location_number').value = hash['location_number'];
165 %   } else {
166       document.getElementById('address2').value = hash['address2'];
167 %   }
168
169     var country_el = document.getElementById('country');
170
171     changeSelect( country_el, hash['country'] );
172
173     country_changed( country_el,
174                      fix_state_factory( hash['state'],
175                                         hash['county']
176                                       )
177                    );
178   }
179
180 </SCRIPT>
181
182 <TR>
183   <<%$th%> ALIGN="right"><% $opt{'label'} || emt('Service location') %></<%$th%>>
184   <TD COLSPAN=7>
185     <SELECT NAME     = "locationnum"
186             ID       = "locationnum"
187             onChange = "locationnum_changed(this);"
188     >
189 % if ( $cust_main ) {
190       <OPTION VALUE="<% $cust_main->ship_locationnum %>"><% $opt{'empty_label'} || '(default service address)' |h %>
191 % }
192 % if ( $opt{'is_optional'} ) {
193     <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
194 % }
195 %
196 %     foreach my $loc ( @cust_location ) {
197 %       # don't show the ship_location redundantly
198 %       next if $cust_main && $cust_main->ship_locationnum == $loc->locationnum;
199         <OPTION VALUE="<% $loc->locationnum %>"
200                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
201         ><% $loc->line |h %>
202 %     }
203 %     if ( $addnew ) {
204         <OPTION VALUE="-1"
205                 <% $locationnum == -1 ? 'SELECTED' : '' %>
206         >Add new location
207 %     }
208     </SELECT>
209   </TD>
210 </TR>
211
212 <% include('/elements/location.html',
213              'object'       => $cust_location,
214              #'onchange' ?  probably not
215              'disabled'     => $disabled,
216              'no_asterisks' => 1,
217              'no_bold'      => $opt{'no_bold'},
218              'alt_format'   => $opt{'alt_format'},
219           )
220 %>
221 <SCRIPT TYPE="text/javascript">
222   locationnum_changed(document.getElementById('locationnum'));
223 </SCRIPT>
224 <%init>
225
226 my $conf = new FS::Conf;
227 my $countrydefault = $conf->config('countrydefault') || 'US';
228 my $statedefault = $conf->config('statedefault')
229                    || ($countrydefault eq 'US' ? 'CA' : '');
230
231 my %opt = @_;
232 my $cgi           = $opt{'cgi'};
233 my $cust_pkg      = $opt{'cust_pkg'};
234 my $cust_main     = $opt{'cust_main'};
235 my $prospect_main = $opt{'prospect_main'};
236 die "cust_main or prospect_main required" unless $cust_main or $prospect_main;
237
238 my $locationnum = '';
239 if ( $cgi->param('error') ) {
240   $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
241   $locationnum = $1;
242 } else {
243   if ( length($opt{'curr_value'}) ) {
244     $locationnum = $opt{'curr_value'};
245   } elsif ($prospect_main) {
246     my @cust_location = $prospect_main->cust_location;
247     $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
248   } else { #$cust_main
249     $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
250     $locationnum = $1 || $cust_main->ship_locationnum;
251   }
252 }
253
254 #probably could use explicit controls
255 # (cust_main locations not editable for tax reasons)
256 my $editable = $cust_main ? 0 : 1; #could use explicit control
257 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
258
259 my @location_fields = qw( address1 address2 city county state zip country
260                           latitude longitude
261                         );
262 if ( $opt{'alt_format'} ) {
263     push @location_fields, qw( location_type location_number location_kind );
264 }
265
266 my $cust_location; #the one that shows by default in the location edit space
267 if ( $locationnum && $locationnum > 0 ) {
268   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
269     or die "unknown locationnum";
270 } else {
271   $cust_location = new FS::cust_location;
272   if ( $locationnum == -1 || $locationnum == -3 ) {
273     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
274   } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
275     my $pkg_location = $cust_pkg->cust_location;
276     $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
277     $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
278   } elsif ( $cust_main ) {
279     $cust_location = $cust_main->ship_location; #I think
280   }
281 }
282
283 my $location_sort = sub {
284         $a->country   cmp $b->country
285   or lc($a->city)     cmp lc($b->city)
286   or lc($a->address1) cmp lc($b->address1)
287   or lc($a->address2) cmp lc($b->address2)
288 };
289
290 my @cust_location;
291 push @cust_location, $cust_main->cust_location if $cust_main;
292 push @cust_location, $prospect_main->cust_location if $prospect_main;
293 push @cust_location, $cust_location
294   if !$cust_main && $cust_location && $cust_location->locationnum > 0
295   && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
296
297 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
298
299 $cust_location = $cust_location[0]
300   if ( $prospect_main )
301   && !$opt{'is_optional'}
302   && @cust_location;
303
304 my $disabled =
305   ( $locationnum < 0
306     || ( $editable && $locationnum )
307     || ( $prospect_main
308          && !$opt{'is_optional'} && !@cust_location && $addnew
309        )
310   )
311     ? ''
312     : 'DISABLED';
313
314 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
315   $cust_location->locationnum(-3);
316   $cust_location->alternize;
317   push @cust_location, $cust_location;
318 }
319
320 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
321
322 </%init>