fix editing of service address fields, from #21327
[freeside.git] / httemplate / elements / location.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/location.html',
6              'object'         => $cust_location
7              'prefix'         => $pre, # prefixed to form field names
8              'onchange'       => $javascript,
9              'geocode'        => $geocode, #passed through
10              'censustract'    => $censustract, #passed through
11              'no_asterisks'   => 0, #set true to disable the red asterisks next
12                                     #to required fields
13              'address1_label' => 'Address', #label for address
14              'enable_coords'  => 1, #show latitude/longitude fields
15              'enable_district' => 1, #show tax district field
16              'enable_censustract' => 1, #show censustract field
17              
18          )
19
20 </%doc>
21
22 % if ( $opt{'alt_format'} ) {
23
24 <TR>
25     <<%$th%> ALIGN="right">Location&nbsp;kind</<%$th%>>
26     <TD>
27     <% include('/elements/select.html',
28                  'cgi'        => $cgi,
29                  'field'      => 'location_kind',
30                  'id'         => 'location_kind',
31                  'disabled'   => $disabled,
32                  #'style'      => \@style,
33                  'options'    => \@location_kind_options,
34                  'labels'     => $location_kind_labels,
35                  'curr_value' => scalar($cgi->param('location_kind'))
36                                    || $object->get('location_kind'),
37               )
38     %>
39     </TD>
40   </TR>
41
42 % } 
43
44 <TR>
45   <<%$th%> STYLE="width:16ex" ALIGN="right"><%$r%><% $opt{'address1_label'} || emt('Address') %></<%$th%>>
46   <TD COLSPAN=7>
47     <INPUT TYPE     = "text"
48            NAME     = "<%$pre%>address1"
49            ID       = "<%$pre%>address1"
50            VALUE    = "<% $object->get('address1') |h %>"
51            SIZE     = 54
52            onChange = "<% $onchange %>"
53            <% $disabled %>
54            <% $style %>
55     >
56   </TD>
57 </TR>
58
59 % if ( ! $opt{'alt_format'} ) { #regular format
60
61 <TR>
62       <TD ALIGN="right"><FONT ID="<% $pre %>address2_required" color="#ff0000" <% $address2_label_style %>>*</FONT>&nbsp;<FONT ID="<% $pre %>address2_label" <% $address2_label_style %>><B>Unit&nbsp;#</B></FONT></TD>
63       <TD COLSPAN=7>
64         <INPUT TYPE     = "text"
65                NAME     = "<%$pre%>address2"
66                ID       = "<%$pre%>address2"
67                VALUE    = "<% $object->get('address2') |h %>"
68                SIZE     = 54
69                onChange = "<% $onchange %>"
70                <% $disabled %>
71                <% $style %>
72         >
73       </TD>
74 </TR>
75
76 % } else { # alternate format
77
78       <INPUT TYPE  = "hidden"
79              NAME  = "<%$pre%>address2"
80              VALUE = "<% $object->get('address2') |h %>"
81       >
82
83 <TR>
84     <<%$th%> ALIGN="right">Unit&nbsp;type&nbsp;and&nbsp;#</<%$th%>>
85     <TD COLSPAN=7>
86
87 %     my $location_type = scalar($cgi->param('location_type'))
88 %                           || $object->get('location_type');
89 %     #my $location_number = scalar($cgi->param('location_number'))
90 %     #                        || $object->get($pre.'location_number');
91 %
92 %   if ( $object->get($pre.'address2') && ! $location_type ) {
93 %   }
94 %
95 %     if ( 1 ) { #ikano, switch on via config
96 %       tie my %location_types, 'Tie::IxHash',
97 %         FS::part_export::ikano->location_types;
98         <% include('/elements/select.html',
99                      'cgi'        => $cgi,
100                      'field'      => 'location_type',
101                      'id'         => 'location_type',
102                      'disabled'   => $disabled,
103                      #'style'      => \@style,
104                      'options'    => [ keys %location_types ],
105                      'labels'     => \%location_types,
106                      'curr_value' => $location_type,
107                      'onchange'   => 'location_type_changed',
108                   )
109         %>
110         <SCRIPT TYPE="text/javascript">
111           function location_type_changed (what) {
112             if ( what.options[what.selectedIndex].value == '' ) {
113               what.form.location_number.disabled = true;
114               what.form.location_number.style.backgroundColor = '#dddddd';
115             } else {
116               what.form.location_number.disabled = false;
117               what.form.location_number.style.backgroundColor = '#ffffff';
118             }
119           }
120         </SCRIPT>
121 %     } else {
122         <INPUT TYPE  = "text" 
123                NAME  = "location_type" 
124                ID    = "location_type"
125                VALUE = "<% $location_type |h %>"
126                SIZE  = "10"
127                <% $disabled %>
128                <% $style %>
129         >
130 %     }
131
132     <INPUT TYPE="text" 
133                NAME  = "location_number"
134                ID    = "location_number"
135                VALUE = "<% scalar($cgi->param('location_number')) || $object->get('location_number') |h %>"
136                SIZE  = "5"
137                <% $disabled || ($location_type ? '' : 'DISABLED') %>
138                <% $style %>
139         >
140
141 %    #XXX i don't work so well when the dropdown is changed :/  i probably need to be triggered by "default service address"
142 %    $alt_err =~ s/(ship_)?address2/'<B>'.encode_entities($object->get($1.'address2')).'<\/B>'/e;
143      <% $alt_err %>
144
145     </TD>
146
147 </TR>
148
149 % } 
150
151
152 <TR>
153   <<%$th%> ALIGN="right"><%$r%><% mt('City') |h %></<%$th%>>
154   <TD WIDTH="1"><% include('/elements/city.html', %select_hash, 'text_style' => \@style ) %></TD>
155   <<%$th%> ALIGN="right" WIDTH="1" ID="<%$pre%>countylabel" <%$county_style%>><%$r%>County</<%$th%>>
156   <TD WIDTH="1"><% include('/elements/select-county.html', %select_hash ) %></TD>
157   <<%$th%> ALIGN="right" WIDTH="1"><%$r%><% mt('State') |h %></<%$th%>>
158   <TD WIDTH="1">
159     <% include('/elements/select-state.html', %select_hash ) %>
160   </TD>
161   <<%$th%> ALIGN="right" WIDTH="1"><%$r%><% mt('Zip') |h %></<%$th%>>
162   <TD>
163     <INPUT TYPE     = "text"
164            NAME     = "<%$pre%>zip"
165            ID       = "<%$pre%>zip"
166            VALUE    = "<% $object->get('zip') |h %>"
167            SIZE     = 11
168            onChange = "<% $onchange %>"
169            <% $disabled %>
170            <% $style %>
171     >
172   </TD>
173 </TR>
174
175 <TR>
176   <<%$th%> ALIGN="right"><%$r%><% mt('Country') |h %></<%$th%>>
177   <TD COLSPAN=6><% include('/elements/select-country.html', %select_hash ) %></TD>
178 </TR>
179
180 % if ( $opt{enable_coords} ) {
181 <TR>
182   <TD ALIGN="right"><% mt('Latitude') |h %></TD>
183   <TD COLSPAN=7>
184     <INPUT TYPE  = "text"
185            NAME  = "<%$pre%>latitude"
186            ID    = "<%$pre%>latitude"
187            VALUE = "<% $object->get('latitude') |h %>"
188            <% $disabled %>
189            <% $style %>
190     >
191     <% mt('Longitude') |h %>
192     <INPUT TYPE  = "text"
193            NAME  = "<%$pre%>longitude"
194            ID    = "<%$pre%>longitude"
195            VALUE = "<% $object->get('longitude') |h %>"
196            <% $disabled %>
197            <% $style %>
198     >
199   </TD>
200 </TR>
201 % } else {
202 %   foreach (qw(latitude longitude)) {
203 <INPUT TYPE="hidden" NAME="<% $_ %>" ID="<% $_ %>" VALUE="<% $object->get($_) |h%>">
204 %   }
205 % }
206 <INPUT TYPE="hidden" NAME="<%$pre%>coord_auto" VALUE="<% $object->coord_auto %>">
207
208 <INPUT TYPE="hidden" NAME="<%$pre%>geocode" VALUE="<% $object->geocode %>">
209 <INPUT TYPE="hidden" NAME="<%$pre%>censustract" VALUE="<% $object->censustract %>">
210 <INPUT TYPE="hidden" NAME="<%$pre%>censusyear" VALUE="<% $object->censusyear %>">
211 % if ( $opt{enable_censustract} ) {
212 <TR>
213   <TD ALIGN="right">Census&nbsp;tract</TD>
214   <TD COLSPAN=8>
215     <INPUT TYPE="text" SIZE=15
216            NAME="enter_censustract" 
217            VALUE="<% $object->censustract |h %>">
218     <% '(automatic)' %>
219   </TD>
220 </TR>
221 % }
222 % if ( $conf->config('tax_district_method') ) {
223 %   if ( $opt{enable_district} ) {
224   <TR>
225     <TD ALIGN="right">Tax&nbsp;district</TD>
226     <TD COLSPAN=8>
227       <INPUT TYPE="text" SIZE=15
228              NAME="<%$pre%>district" 
229              ID="<%$pre%>district"
230              VALUE="<% $object->district |h %>">
231     <% '(automatic)' %>
232     </TD>
233   </TR>
234 %   } else {
235     <INPUT TYPE="hidden" ID="<%$pre%>" NAME="<%$pre%>district" VALUE="<% $object->district %>">
236 %   }
237 % }
238
239 %# For address standardization:
240 %# keep a clean copy of the address so we know if we need
241 %# to re-standardize
242 % foreach (qw(address1 city state country zip latitude
243 %             longitude censustract district addr_clean) ) {
244 <INPUT TYPE="hidden" NAME="old_<%$pre.$_%>" ID="old_<%$pre.$_%>" VALUE="<% $object->get($_) |h%>">
245 % }
246 %# Placeholders
247 <INPUT TYPE="hidden" NAME="<%$pre%>cachenum" VALUE="">
248 <INPUT TYPE="hidden" NAME="<%$pre%>addr_clean" VALUE="">
249 <%init>
250
251 my %opt = @_;
252
253 my $pre      = $opt{'prefix'};
254 my $object   = $opt{'object'};
255 my $onchange = $opt{'onchange'};
256 my $disabled = $opt{'disabled'};
257
258 my $conf = new FS::Conf;
259
260 my $r = $opt{'no_asterisks'} ? '' : qq!<font color="#ff0000">*</font>&nbsp;!;
261
262 my $countrydefault = $conf->config('countrydefault') || 'US';
263 my $statedefault = $conf->config('statedefault') 
264                    || ($countrydefault eq 'US' ? 'CA' : '');
265 $object ||= FS::cust_location->new({
266   'country' => $countrydefault,
267   'state'   => $statedefault,
268 });
269
270 my $alt_err = ($opt{'alt_format'} && !$disabled) ? $object->alternize : '';
271
272 my @style = ();
273 push @style, 'background-color: #dddddd' if $disabled;
274
275 my @address2_label_style = ();
276 push @address2_label_style, 'visibility:hidden'
277   if $disabled
278   || ! $conf->exists('cust_main-require_address2')
279   || ( !$pre && !$opt{'same_checked'} );
280
281 my @counties = counties( $object->get('state'),
282                          $object->get('country'),
283                        );
284 my @county_style = ();
285 push @county_style, 'display:none' # 'visibility:hidden'
286   unless scalar(@counties) > 1;
287
288 my $style =
289   scalar(@style)
290     ? 'STYLE="'. join(';', @style). '"'
291     : '';
292 my $address2_label_style =
293   scalar(@address2_label_style)
294     ? 'STYLE="'. join(';', @address2_label_style). '"'
295     : '';
296 my $county_style = 
297   scalar(@county_style)
298     ? 'STYLE="'. join(';', @county_style). '"'
299     : '';
300
301 my %select_hash = (
302   'city'     => $object->get('city'),
303   'county'   => $object->get('county'),
304   'state'    => $object->get('state'),
305   'country'  => $object->get('country'),
306   'prefix'   => $pre,
307   'onchange' => $onchange,
308   'disabled' => $disabled,
309   #'style'    => \@style,
310 );
311
312 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
313
314 my @location_kind_options = ( '', 'R', 'B' );
315 my $location_kind_labels = { '' => '', 'R' => 'Residential', 'B' => 'Business' };
316
317 </%init>