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_enable(what) {
38 %   for (grep { $_ ne 'location_number' } @location_fields, 'city_select') { 
39       what.form.<%$_%>.disabled = false;
40       var ftype = what.form.<%$_%>.tagName;
41       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
42       if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
43 %   } 
44
45     if ( what.form.location_type.options[what.form.location_type.selectedIndex].value ) {
46       what.form.location_number.disabled = false;
47       what.form.location_number.style.backgroundColor = '#ffffff';
48     }
49     what.form.location_number.value = '';
50   }
51
52   function locationnum_changed(what) {
53     var locationnum = what.options[what.selectedIndex].value;
54     if ( locationnum == -2 ) {
55       location_disable(what);
56       return;
57     }
58     if ( locationnum == -1 ) {
59       location_enable(what);
60
61       changeSelect(what.form.country, <% $countrydefault |js_string %>);
62
63       country_changed( what.form.country,
64                        fix_state_factory( <% $statedefault |js_string %>,
65                                           ''
66                                         )
67                      );
68       return;
69     }
70
71     if ( locationnum == 0 ) {
72 %     if ( $cust_main ) {
73       what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
74       what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
75       what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
76       what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
77
78       changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
79
80       country_changed( what.form.country,
81                        fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
82                                           <% $cust_main->get($prefix.'county') | js_string %>
83                                         )
84                      );
85 %     }
86
87     } else {
88       get_location( locationnum, update_location );
89     } 
90
91 %   if ( $editable ) {
92       if ( locationnum == 0 ) {
93 %   }
94
95 %       #sleep/wait until dropdowns are updated?
96         location_disable(what);
97
98 %   if ( $editable ) {
99       } else {
100
101 %       #sleep/wait until dropdowns are updated?
102         location_enable(what);
103
104       }
105 %   }
106
107   }
108
109   function fix_state_factory (state, county) {
110     function fix_state() {
111       var state_el = document.getElementById('state');
112       changeSelect(state_el, state);
113       state_changed(state_el, fix_county_factory(county) );
114     }
115     return fix_state;
116   }
117
118   function fix_county_factory(county) {
119     function fix_county() {
120       var county_el = document.getElementById('county');
121       if ( county.length > 0 ) {
122         changeSelect(county_el, county );
123       } else {
124         county_el.selectedIndex = 0;
125       }
126       county_changed(county_el);
127     }
128     return fix_county;
129   }
130
131   function changeSelect(what, value) {
132     for ( var i=0; i<what.length; i++) {
133       if ( what.options[i].value == value ) {
134         what.selectedIndex = i;
135       }
136     }
137   }
138
139   function update_location( string ) {
140     var hash = eval('('+string+')');
141     document.getElementById('address1').value = hash['address1'];
142     document.getElementById('address2').value = hash['address2'];
143     document.getElementById('city').value     = hash['city'];
144     document.getElementById('zip').value      = hash['zip'];
145
146     var country_el = document.getElementById('country');
147
148     changeSelect( country_el, hash['country'] );
149
150     country_changed( country_el,
151                      fix_state_factory( hash['state'],
152                                         hash['county']
153                                       )
154                    );
155   }
156
157 </SCRIPT>
158
159 <TR>
160   <<%$th%> ALIGN="right"><% $opt{'label'} || 'Service&nbsp;location' %></<%$th%>>
161   <TD COLSPAN=7>
162     <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
163 % if ( !$prospect_main && !$opt{'disable_empty'} ) {
164       <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
165 % }
166 % if ( $opt{'is_optional'} ) {
167     <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
168 % }
169 %
170 %     foreach my $loc ( @cust_location ) {
171         <OPTION VALUE="<% $loc->locationnum %>"
172                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
173         ><% $loc->line |h %>
174 %     }
175 %     if ( $addnew ) {
176         <OPTION VALUE="-1"
177                 <% $locationnum == -1 ? 'SELECTED' : '' %>
178         >Add new location
179 %     }
180     </SELECT>
181   </TD>
182 </TR>
183
184 <% include('/elements/location.html',
185              'object'       => $cust_location,
186              #'onchange' ?  probably not
187              'disabled'     => $disabled,
188              'no_asterisks' => 1,
189              'no_bold'      => $opt{'no_bold'},
190              'alt_format'   => $opt{'alt_format'},
191           )
192 %>
193
194 <%init>
195
196 my $conf = new FS::Conf;
197 my $countrydefault = $conf->config('countrydefault') || 'US';
198 my $statedefault = $conf->config('statedefault')
199                    || ($countrydefault eq 'US' ? 'CA' : '');
200
201 my %opt = @_;
202 my $cgi           = $opt{'cgi'};
203 my $cust_pkg      = $opt{'cust_pkg'};
204 my $cust_main     = $opt{'cust_main'};
205 my $prospect_main = $opt{'prospect_main'};
206
207 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
208
209 my $locationnum = '';
210 if ( $cgi->param('error') ) {
211   $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
212   $locationnum = $1;
213 } else {
214   if ( length($opt{'curr_value'}) ) {
215     $locationnum = $opt{'curr_value'};
216   } elsif ($prospect_main) {
217     my @cust_location = $prospect_main->cust_location;
218     $locationnum = $cust_location[0]->locationnum if scalar(@cust_location)==1;
219   } else { #?
220     $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
221     $locationnum = $1;
222   }
223 }
224
225 #probably could use explicit controls
226 # (cust_main locations not editable for tax reasons)
227 my $editable = $cust_main ? 0 : 1; #could use explicit control
228 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
229
230 my @location_fields = qw( address1 address2 city county state zip country );
231 if ( $opt{'alt_format'} ) {
232     push @location_fields, qw( location_type location_number location_kind );
233 }
234
235 my $cust_location;
236 if ( $locationnum && $locationnum > 0 ) {
237   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
238     or die "unknown locationnum";
239 } else {
240   $cust_location = new FS::cust_location;
241   if ( $locationnum == -1 ) {
242     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
243   } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
244     my $pkg_location = $cust_pkg->cust_location;
245     $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
246     $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
247   } elsif ( $cust_main ) {
248     $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
249   }
250 }
251
252 my $location_sort = sub {
253         $a->country   cmp $b->country
254   or lc($a->city)     cmp lc($b->city)
255   or lc($a->address1) cmp lc($b->address1)
256   or lc($a->address2) cmp lc($b->address2)
257 };
258
259 my @cust_location = ();
260 push @cust_location, $cust_main->cust_location if $cust_main;
261 push @cust_location, $prospect_main->cust_location if $prospect_main;
262 push @cust_location, $cust_location
263   if !$cust_main && $cust_location && $cust_location->locationnum > 0
264   && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
265
266 @cust_location = sort $location_sort grep !$_->disabled, @cust_location;
267
268 my $disabled =
269   ( $locationnum < 0
270     || ( $editable && $locationnum )
271     || ( ( $prospect_main || $opt{'disable_empty'} )
272          && !$opt{'is_optional'} && !@cust_location && $addnew
273        )
274   )
275     ? ''
276     : 'DISABLED';
277
278 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
279
280 </%init>