v4 style
[freeside.git] / httemplate / elements / tr-select-cust_location.html
1 <%doc>
2
3 Example:
4
5   <& /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 <& /elements/xmlhttp.html,
19      'url'  => $p.'misc/location.cgi',
20      'subs' => [ 'get_location' ],
21 &>
22
23 <SCRIPT TYPE="text/javascript">
24
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';
32 %   } 
33     if(what.form.enter_censustract) {
34       what.form.enter_censustract.disabled = true;
35     }
36   }
37
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 = '';
42 %   }
43     if(what.form.enter_censustract) {
44       what.form.enter_censustract.value = '';
45     }
46 %   if ( $opt{'alt_format'} ) {
47       changeSelect(what.form.location_kind, '');
48       changeSelect(what.form.location_type, '');
49       what.form.location_number.value = '';
50 %   }
51   }
52
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';
58 %   } 
59     if(what.form.enter_censustract) {
60       what.form.enter_censustract.disabled = false;
61     }
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';
67       }
68 %   }
69   }
70
71   function locationnum_changed(what) {
72     var locationnum = what.options[what.selectedIndex].value;
73     if ( locationnum == -2 ) { //(not required)
74       location_disable(what);
75       return;
76     }
77     if ( locationnum == -1 ) { //Add new location
78       location_clear(what);
79
80       changeSelect(what.form.country, <% $countrydefault |js_string %>);
81
82       country_changed( what.form.country,
83                        fix_state_factory( <% $statedefault |js_string %>,
84                                           ''
85                                         )
86                      );
87
88       location_enable(what);
89       return;
90     }
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 %> );
100 %     }
101
102       changeSelect(what.form.country, <% $cust_location->country | js_string %> );
103
104       country_changed( what.form.country,
105                        fix_state_factory( <% $cust_location->state | js_string %>,
106                                           <% $cust_location->county | js_string %>
107                                         )
108                      );
109       location_enable(what);
110       return;
111     }
112
113 %# default service address is now just another location
114     get_location( locationnum, update_location );
115
116 %   if ( $editable ) {
117       if ( locationnum == 0 ) {
118 %   }
119
120 %       #sleep/wait until dropdowns are updated?
121         location_disable(what);
122
123 %   if ( $editable ) {
124       } else {
125
126 %       #sleep/wait until dropdowns are updated?
127         location_enable(what);
128
129       }
130 %   }
131
132   }
133
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) );
139     }
140     return fix_state;
141   }
142
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 );
148       } else {
149         county_el.selectedIndex = 0;
150       }
151       county_changed(county_el);
152     }
153     return fix_county;
154   }
155
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;
160       }
161     }
162   }
163
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];
171       }
172     }
173     country_changed( document.getElementById('country'),
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
186     <& /elements/select-cust_location.html,
187          %opt,
188          'curr_value'    => $locationnum,
189          'cust_location' => \@cust_location,
190          'onchange'      => 'locationnum_changed(this);',
191     &>
192
193   </TD>
194 </TR>
195
196 <& /elements/location.html,
197      'object'             => $cust_location,
198      #'onchange' ?  probably not
199      'disabled'           => $disabled,
200      'no_asterisks'       => 1,
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,
206 &>
207
208 <SCRIPT TYPE="text/javascript">
209 % if ( $prospect_main ) { # && ! $opt{is_optional} ) {
210
211     changeSelect(document.getElementById('country'), <% $cust_location->country || $countrydefault |js_string %>);
212
213     country_changed( document.getElementById('country'),
214                      fix_state_factory( <% $cust_location->state || $statedefault |js_string %>,
215                                         ''
216                                       )
217                    );
218 % } elsif ( $locationnum != -1 ) {
219     locationnum_changed(document.getElementById('locationnum'));
220 % }
221 </SCRIPT>
222
223 <%init>
224
225 my $conf = new FS::Conf;
226 my $countrydefault = $conf->config('countrydefault') || 'US';
227 my $statedefault = $conf->config('statedefault')
228                    || ($countrydefault eq 'US' ? 'CA' : '');
229
230 my %opt = @_;
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;
236
237 my $locationnum = '';
238 if ( $cgi->param('error') ) {
239   $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
240   $locationnum = $1;
241 } else {
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;
247   } else { #$cust_main
248     $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
249     $locationnum = $1 || $cust_main->ship_locationnum;
250   }
251 }
252
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 );
257
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 );
261 }
262
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";
267 } else {
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
277   }
278 }
279
280 $cust_location->coord_auto('Y');
281
282 my $location_sort = sub {
283   #enabled w/label_prefix _location #    $a->locationname cmp $b->locationname
284                                     # or 
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)
291 };
292
293 my @cust_location;
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;
299
300 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
301
302 $cust_location = $cust_location[0]
303   if $prospect_main
304   && !$opt{'is_optional'}
305   && @cust_location;
306
307 my $disabled =
308   ( $locationnum < 0
309     || ( $editable && $locationnum )
310     || ( $prospect_main
311          && !$opt{'is_optional'} && !@cust_location && $addnew
312        )
313   )
314     ? ''
315     : 'DISABLED';
316
317 if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
318   $cust_location->locationnum(-3);
319   $cust_location->alternize;
320   push @cust_location, $cust_location;
321 }
322
323 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
324
325 </%init>