user-defined site ID / location codes per location, RT#30856, RT#27545
[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 % if ( $label_prefix eq '_location' ) {
45
46     <TR>
47       <TD ALIGN="right" ><% $opt{'locationname_label'} || emt('Location ID') %></TD>
48       <TD COLSPAN=7>
49         <INPUT TYPE     = "text"
50                NAME     = "<%$pre%>locationname"
51                ID       = "<%$pre%>locationname"
52                VALUE    = "<% $object->get('locationname') |h %>"
53                SIZE     = 24
54                onChange = "<% $onchange %>"
55                <% $disabled %>
56                <% $style %>
57         >
58       </TD>
59     </TR>
60
61 % } else {
62
63     <INPUT TYPE     = "hidden"
64            NAME     = "<%$pre%>locationname"
65            ID       = "<%$pre%>locationname"
66            VALUE    = "<% $object->get('locationname') |h %>"
67     >
68
69 % }
70
71 <TR>
72   <<%$th%> STYLE="width:16ex" ALIGN="right"><%$r%><% $opt{'address1_label'} || emt('Address') %></<%$th%>>
73   <TD COLSPAN=7>
74     <INPUT TYPE     = "text"
75            NAME     = "<%$pre%>address1"
76            ID       = "<%$pre%>address1"
77            VALUE    = "<% $object->get('address1') |h %>"
78            SIZE     = 54
79            onChange = "<% $onchange %>"
80            <% $disabled %>
81            <% $style %>
82     >
83   </TD>
84 </TR>
85
86 % if ( ! $opt{'alt_format'} ) { #regular format
87
88 <TR>
89       <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>
90       <TD COLSPAN=7>
91         <INPUT TYPE     = "text"
92                NAME     = "<%$pre%>address2"
93                ID       = "<%$pre%>address2"
94                VALUE    = "<% $object->get('address2') |h %>"
95                SIZE     = 54
96                onChange = "<% $onchange %>"
97                <% $disabled %>
98                <% $style %>
99         >
100       </TD>
101 </TR>
102
103 % } else { # alternate format
104
105       <INPUT TYPE  = "hidden"
106              NAME  = "<%$pre%>address2"
107              VALUE = "<% $object->get('address2') |h %>"
108       >
109
110 <TR>
111     <<%$th%> ALIGN="right">Unit&nbsp;type&nbsp;and&nbsp;#</<%$th%>>
112     <TD COLSPAN=7>
113
114 %     my $location_type = scalar($cgi->param('location_type'))
115 %                           || $object->get('location_type');
116 %     #my $location_number = scalar($cgi->param('location_number'))
117 %     #                        || $object->get($pre.'location_number');
118 %
119 %   if ( $object->get($pre.'address2') && ! $location_type ) {
120 %   }
121 %
122 %     if ( 1 ) { #ikano, switch on via config
123 %       tie my %location_types, 'Tie::IxHash',
124 %         FS::part_export::ikano->location_types;
125         <% include('/elements/select.html',
126                      'cgi'        => $cgi,
127                      'field'      => 'location_type',
128                      'id'         => 'location_type',
129                      'disabled'   => $disabled,
130                      #'style'      => \@style,
131                      'options'    => [ keys %location_types ],
132                      'labels'     => \%location_types,
133                      'curr_value' => $location_type,
134                      'onchange'   => 'location_type_changed',
135                   )
136         %>
137         <SCRIPT TYPE="text/javascript">
138           function location_type_changed (what) {
139             if ( what.options[what.selectedIndex].value == '' ) {
140               what.form.location_number.disabled = true;
141               what.form.location_number.style.backgroundColor = '#dddddd';
142             } else {
143               what.form.location_number.disabled = false;
144               what.form.location_number.style.backgroundColor = '#ffffff';
145             }
146           }
147         </SCRIPT>
148 %     } else {
149         <INPUT TYPE  = "text" 
150                NAME  = "location_type" 
151                ID    = "location_type"
152                VALUE = "<% $location_type |h %>"
153                SIZE  = "10"
154                <% $disabled %>
155                <% $style %>
156         >
157 %     }
158
159     <INPUT TYPE="text" 
160                NAME  = "location_number"
161                ID    = "location_number"
162                VALUE = "<% scalar($cgi->param('location_number')) || $object->get('location_number') |h %>"
163                SIZE  = "5"
164                <% $disabled || ($location_type ? '' : 'DISABLED') %>
165                <% $style %>
166         >
167
168 %    #XXX i don't work so well when the dropdown is changed :/  i probably need to be triggered by "default service address"
169 %    $alt_err =~ s/(ship_)?address2/'<B>'.encode_entities($object->get($1.'address2')).'<\/B>'/e;
170      <% $alt_err %>
171
172     </TD>
173
174 </TR>
175
176 % } 
177
178
179 <TR>
180   <<%$th%> ALIGN="right"><%$r%><% mt('City') |h %></<%$th%>>
181   <TD WIDTH="1"><% include('/elements/city.html', %select_hash, 'text_style' => \@style ) %></TD>
182   <<%$th%> ALIGN="right" WIDTH="1" ID="<%$pre%>countylabel" <%$county_style%>><%$r%>County</<%$th%>>
183   <TD WIDTH="1"><% include('/elements/select-county.html', %select_hash ) %></TD>
184   <<%$th%> ALIGN="right" WIDTH="1"><%$r%><% mt('State') |h %></<%$th%>>
185   <TD WIDTH="1">
186     <% include('/elements/select-state.html', %select_hash ) %>
187   </TD>
188   <<%$th%> ALIGN="right" WIDTH="1"><%$r%><% mt('Zip') |h %></<%$th%>>
189   <TD>
190     <INPUT TYPE     = "text"
191            NAME     = "<%$pre%>zip"
192            ID       = "<%$pre%>zip"
193            VALUE    = "<% $object->get('zip') |h %>"
194            SIZE     = 11
195            onChange = "<% $onchange %>"
196            <% $disabled %>
197            <% $style %>
198     >
199   </TD>
200 </TR>
201
202 <TR>
203   <<%$th%> ALIGN="right"><%$r%><% mt('Country') |h %></<%$th%>>
204   <TD COLSPAN=6><% include('/elements/select-country.html', %select_hash ) %></TD>
205 </TR>
206
207 % if ( $opt{enable_coords} ) {
208 <TR>
209   <TD ALIGN="right"><% mt('Latitude') |h %></TD>
210   <TD COLSPAN=7>
211     <INPUT TYPE  = "text"
212            NAME  = "<%$pre%>latitude"
213            ID    = "<%$pre%>latitude"
214            VALUE = "<% $object->get('latitude') |h %>"
215            <% $disabled %>
216            <% $style %>
217     >
218     <% mt('Longitude') |h %>
219     <INPUT TYPE  = "text"
220            NAME  = "<%$pre%>longitude"
221            ID    = "<%$pre%>longitude"
222            VALUE = "<% $object->get('longitude') |h %>"
223            <% $disabled %>
224            <% $style %>
225     >
226   </TD>
227 </TR>
228 % } else {
229 %   foreach (qw(latitude longitude)) {
230 <INPUT TYPE="hidden" NAME="<% $_ %>" ID="<% $_ %>" VALUE="<% $object->get($_) |h%>">
231 %   }
232 % }
233 <INPUT TYPE="hidden" NAME="<%$pre%>coord_auto" VALUE="<% $object->coord_auto %>">
234
235 <INPUT TYPE="hidden" NAME="<%$pre%>geocode" VALUE="<% $object->geocode %>">
236 <INPUT TYPE="hidden" NAME="<%$pre%>censustract" VALUE="<% $object->censustract %>">
237 <INPUT TYPE="hidden" NAME="<%$pre%>censusyear" VALUE="<% $object->censusyear %>">
238 % if ( $opt{enable_censustract} ) {
239 <TR>
240   <TD ALIGN="right">Census&nbsp;tract</TD>
241   <TD COLSPAN=8>
242     <INPUT TYPE="text" SIZE=15
243            ID="<% $pre %>enter_censustract" 
244            NAME="<% $pre %>enter_censustract" 
245            VALUE="<% $object->censustract |h %>">
246     <% '(automatic)' %>
247   </TD>
248 </TR>
249 % }
250 % if ( $opt{enable_district} and $conf->config('tax_district_method') ) {
251   <TR>
252     <TD ALIGN="right">Tax&nbsp;district</TD>
253     <TD COLSPAN=8>
254       <INPUT TYPE="text" SIZE=15
255              NAME="<%$pre%>district" 
256              ID="<%$pre%>district"
257              VALUE="<% $object->district |h %>">
258     <% '(automatic)' %>
259     </TD>
260   </TR>
261 % } else {
262     <INPUT TYPE="hidden" ID="<%$pre%>" NAME="<%$pre%>district" VALUE="<% $object->district %>">
263 % }
264
265 %# For address standardization:
266 %# keep a clean copy of the address so we know if we need
267 %# to re-standardize
268 % foreach (qw(address1 city state country zip latitude
269 %             longitude censustract district addr_clean) ) {
270 <INPUT TYPE="hidden" NAME="old_<%$pre.$_%>" ID="old_<%$pre.$_%>" VALUE="<% $object->get($_) |h%>">
271 % }
272 %# Placeholders
273 <INPUT TYPE="hidden" NAME="<%$pre%>cachenum" VALUE="">
274 <INPUT TYPE="hidden" NAME="<%$pre%>addr_clean" VALUE="">
275
276 <SCRIPT TYPE="text/javascript">
277 <&| /elements/onload.js &>
278   var clear_coords_ids = [
279     '<%$pre%>latitude',
280     '<%$pre%>longitude',
281     '<%$pre%>enter_censustract',
282     '<%$pre%>censustract',
283     '<%$pre%>district'
284   ];
285   function clear_coords() {
286     for (var i=0; i < clear_coords_ids.length; i++) {
287       var el = document.getElementById(clear_coords_ids[i]);
288       if ( el ) {
289         el.value = '';
290       }
291     }
292   }
293   var clear_coords_on_change = [
294     '<%$pre%>address1',
295     '<%$pre%>address2',
296     '<%$pre%>city',
297     '<%$pre%>state',
298     '<%$pre%>zip',
299     '<%$pre%>country'
300   ];
301   for (var i=0; i < clear_coords_on_change.length; i++) {
302     var el = document.getElementById(clear_coords_on_change[i]);
303     if ( el.addEventListener ) {
304       el.addEventListener('change', clear_coords);
305     } else if ( el.attachEvent ) {
306       el.attachEvent('onchange', clear_coords);
307     }
308   }
309 </&>
310 </SCRIPT>
311
312 <%init>
313
314 my %opt = @_;
315
316 my $pre      = $opt{'prefix'};
317 my $object   = $opt{'object'};
318 my $onchange = $opt{'onchange'};
319 my $disabled = $opt{'disabled'};
320
321 my $r = $opt{'no_asterisks'} ? '' : qq!<font color="#ff0000">*</font>&nbsp;!;
322
323 my $conf = new FS::Conf;
324 my $countrydefault = $conf->config('countrydefault') || 'US';
325 my $statedefault   = $conf->config('statedefault') 
326                        || ($countrydefault eq 'US' ? 'CA' : '');
327 my $label_prefix   = $conf->config('cust_location-label_prefix');
328
329 $object ||= FS::cust_location->new({
330   'country' => $countrydefault,
331   'state'   => $statedefault,
332 });
333
334 my $alt_err = ($opt{'alt_format'} && !$disabled) ? $object->alternize : '';
335
336 my @style = ();
337 push @style, 'background-color: #dddddd' if $disabled;
338
339 my @address2_label_style = ();
340 push @address2_label_style, 'visibility:hidden'
341   if $disabled
342   || ! $conf->exists('cust_main-require_address2')
343   || ( !$pre && !$opt{'same_checked'} );
344
345 my @counties = counties( $object->get('state'),
346                          $object->get('country'),
347                        );
348 my @county_style = ();
349 push @county_style, 'display:none' # 'visibility:hidden'
350   unless scalar(@counties) > 1;
351
352 my $style =
353   scalar(@style)
354     ? 'STYLE="'. join(';', @style). '"'
355     : '';
356 my $address2_label_style =
357   scalar(@address2_label_style)
358     ? 'STYLE="'. join(';', @address2_label_style). '"'
359     : '';
360 my $county_style = 
361   scalar(@county_style)
362     ? 'STYLE="'. join(';', @county_style). '"'
363     : '';
364
365 my %select_hash = (
366   'city'     => $object->get('city'),
367   'county'   => $object->get('county'),
368   'state'    => $object->get('state'),
369   'country'  => $object->get('country'),
370   'prefix'   => $pre,
371   'onchange' => $onchange,
372   'disabled' => $disabled,
373   #'style'    => \@style,
374 );
375
376 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
377
378 my @location_kind_options = ( '', 'R', 'B' );
379 my $location_kind_labels = { '' => '', 'R' => 'Residential', 'B' => 'Business' };
380
381 </%init>