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