qualification address handling changes, RT#7111
[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             'disable_empty' => 0, #1 to disable
15          )
16
17 </%doc>
18
19 <% include('/elements/xmlhttp.html',
20               'url'  => $p.'misc/location.cgi',
21               'subs' => [ 'get_location' ],
22            )
23 %>
24
25 <SCRIPT TYPE="text/javascript">
26
27   function location_disable(what) {
28 %   for (@location_fields, 'city_select') { 
29       what.form.<%$_%>.disabled = true;
30       var ftype = what.form.<%$_%>.tagName;
31       if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
32       else what.form.<%$_%>.value = '';
33       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
34 %   } 
35   }
36
37   function location_clear(what) {
38 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
39       var ftype = what.form.<%$_%>.tagName;
40       if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
41 %   }
42 %   if ( $opt{'alt_format'} ) {
43       changeSelect(what.form.location_kind, '');
44       changeSelect(what.form.location_type, '');
45       what.form.location_number.value = '';
46 %   }
47   }
48
49   function location_enable(what) {
50 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
51       what.form.<%$_%>.disabled = false;
52       var ftype = what.form.<%$_%>.tagName;
53       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
54 %   } 
55
56     if ( what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
57       what.form.location_number.disabled = false;
58       what.form.location_number.style.backgroundColor = '#ffffff';
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     if ( locationnum == 0 ) { //(default service address)
105 %     if ( $cust_main ) {
106       what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
107       what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
108       what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
109       what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
110
111       changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
112
113       country_changed( what.form.country,
114                        fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
115                                           <% $cust_main->get($prefix.'county') | js_string %>
116                                         )
117                      );
118 %     }
119
120     } else {
121       get_location( locationnum, update_location );
122     } 
123
124 %   if ( $editable ) {
125       if ( locationnum == 0 ) {
126 %   }
127
128 %       #sleep/wait until dropdowns are updated?
129         location_disable(what);
130
131 %   if ( $editable ) {
132       } else {
133
134 %       #sleep/wait until dropdowns are updated?
135         location_enable(what);
136
137       }
138 %   }
139
140   }
141
142   function fix_state_factory (state, county) {
143     function fix_state() {
144       var state_el = document.getElementById('state');
145       changeSelect(state_el, state);
146       state_changed(state_el, fix_county_factory(county) );
147     }
148     return fix_state;
149   }
150
151   function fix_county_factory(county) {
152     function fix_county() {
153       var county_el = document.getElementById('county');
154       if ( county.length > 0 ) {
155         changeSelect(county_el, county );
156       } else {
157         county_el.selectedIndex = 0;
158       }
159       county_changed(county_el);
160     }
161     return fix_county;
162   }
163
164   function changeSelect(what, value) {
165     for ( var i=0; i<what.length; i++) {
166       if ( what.options[i].value == value ) {
167         what.selectedIndex = i;
168       }
169     }
170   }
171
172   function update_location( string ) {
173     var hash = eval('('+string+')');
174     document.getElementById('address1').value = hash['address1'];
175     document.getElementById('city').value     = hash['city'];
176     document.getElementById('zip').value      = hash['zip'];
177
178 %   if ( $opt{'alt_format'} ) {
179       changeSelect( document.getElementById('location_kind'), hash['location_kind']);
180       changeSelect( document.getElementById('location_type'), hash['location_type']);
181       document.getElementById('location_number').value = hash['location_number'];
182 %   } else {
183       document.getElementById('address2').value = hash['address2'];
184 %   }
185
186     var country_el = document.getElementById('country');
187
188     changeSelect( country_el, hash['country'] );
189
190     country_changed( country_el,
191                      fix_state_factory( hash['state'],
192                                         hash['county']
193                                       )
194                    );
195   }
196
197 </SCRIPT>
198
199 <TR>
200   <<%$th%> ALIGN="right"><% $opt{'label'} || 'Service&nbsp;location' %></<%$th%>>
201   <TD COLSPAN=7>
202     <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
203 % if ( !$prospect_main && !$opt{'disable_empty'} ) {
204       <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
205 % }
206 % if ( $opt{'is_optional'} ) {
207     <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
208 % }
209 %
210 %     foreach my $loc ( @cust_location ) {
211         <OPTION VALUE="<% $loc->locationnum %>"
212                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
213         ><% $loc->line |h %>
214 %     }
215 %     if ( $addnew ) {
216         <OPTION VALUE="-1"
217                 <% $locationnum == -1 ? 'SELECTED' : '' %>
218         >Add new location
219 %     }
220     </SELECT>
221   </TD>
222 </TR>
223
224 <% include('/elements/location.html',
225              'object'       => $cust_location,
226              #'onchange' ?  probably not
227              'disabled'     => $disabled,
228              'no_asterisks' => 1,
229              'no_bold'      => $opt{'no_bold'},
230              'alt_format'   => $opt{'alt_format'},
231           )
232 %>
233
234 <%init>
235
236 my $conf = new FS::Conf;
237 my $countrydefault = $conf->config('countrydefault') || 'US';
238 my $statedefault = $conf->config('statedefault')
239                    || ($countrydefault eq 'US' ? 'CA' : '');
240
241 my %opt = @_;
242 my $cgi           = $opt{'cgi'};
243 my $cust_pkg      = $opt{'cust_pkg'};
244 my $cust_main     = $opt{'cust_main'};
245 my $prospect_main = $opt{'prospect_main'};
246
247 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
248
249 my $locationnum = '';
250 if ( $cgi->param('error') ) {
251   $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
252   $locationnum = $1;
253 } else {
254   if ( length($opt{'curr_value'}) ) {
255     $locationnum = $opt{'curr_value'};
256   } elsif ($prospect_main) {
257     my @cust_location = $prospect_main->cust_location;
258     $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
259   } else { #?
260     $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
261     $locationnum = $1;
262   }
263 }
264
265 #probably could use explicit controls
266 # (cust_main locations not editable for tax reasons)
267 my $editable = $cust_main ? 0 : 1; #could use explicit control
268 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
269
270 my @location_fields = qw( address1 address2 city county state zip country );
271 if ( $opt{'alt_format'} ) {
272     push @location_fields, qw( location_type location_number location_kind );
273 }
274
275 my $cust_location;
276 if ( $locationnum && $locationnum > 0 ) {
277   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
278     or die "unknown locationnum";
279 } else {
280   $cust_location = new FS::cust_location;
281   if ( $locationnum == -1 || $locationnum == -3 ) {
282     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
283   } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
284     my $pkg_location = $cust_pkg->cust_location;
285     $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
286     $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
287   } elsif ( $cust_main ) {
288     $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
289   }
290 }
291
292 my $location_sort = sub {
293         $a->country   cmp $b->country
294   or lc($a->city)     cmp lc($b->city)
295   or lc($a->address1) cmp lc($b->address1)
296   or lc($a->address2) cmp lc($b->address2)
297 };
298
299 my @cust_location = ();
300 push @cust_location, $cust_main->cust_location if $cust_main;
301 push @cust_location, $prospect_main->cust_location if $prospect_main;
302 push @cust_location, $cust_location
303   if !$cust_main && $cust_location && $cust_location->locationnum > 0
304   && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
305
306 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
307
308 $cust_location = $cust_location[0];
309   if ( $prospect_main || $opt{'disable_empty'} )
310   && !$opt{'is_optional'}
311   && @cust_location;
312
313 my $disabled =
314   ( $locationnum < 0
315     || ( $editable && $locationnum )
316     || ( ( $prospect_main || $opt{'disable_empty'} )
317          && !$opt{'is_optional'} && !@cust_location && $addnew
318        )
319   )
320     ? ''
321     : 'DISABLED';
322
323 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
324   $cust_location->locationnum(-3);
325   $cust_location->alternize;
326   push @cust_location, $cust_location;
327 }
328
329 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
330
331 </%init>