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