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