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