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