cust_location editing features, RT#10766
[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       <OPTION VALUE=""><% $opt{'empty_label'} || '(default service address)' |h %>
158 % if ( $opt{'is_optional'} ) {
159     <OPTION VALUE="-2" <% $locationnum == -2 ? 'SELECTED' : ''%>><% $opt{'optional_label'} || '(not required)' |h %>
160 % }
161 %     my @locations = $cust_main ? $cust_main->cust_location : ();
162 %     push @locations, $cust_location
163 %       if !$cust_main && $cust_location && $cust_location->locationnum>0;
164 %     foreach my $loc ( sort $location_sort @locations ) {
165 %       next if $loc->disabled;
166         <OPTION VALUE="<% $loc->locationnum %>"
167                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
168         ><% $loc->line |h %>
169 %     }
170 %     if ( $addnew ) {
171         <OPTION VALUE="-1"
172                 <% $locationnum == -1 ? 'SELECTED' : '' %>
173         >Add new location
174 %     }
175     </SELECT>
176   </TD>
177 </TR>
178
179 <% include('/elements/location.html',
180              'object'       => $cust_location,
181              #'onchange' ?  probably not
182              'disabled'     => $disabled,
183              'no_asterisks' => 1,
184              'no_bold'      => $opt{'no_bold'},
185              'alt_format'   => $opt{'alt_format'},
186           )
187 %>
188
189 <%init>
190
191 my $conf = new FS::Conf;
192 my $countrydefault = $conf->config('countrydefault') || 'US';
193 my $statedefault = $conf->config('statedefault')
194                    || ($countrydefault eq 'US' ? 'CA' : '');
195
196 my %opt = @_;
197 my $cgi           = $opt{'cgi'};
198 my $cust_pkg     = $opt{'cust_pkg'};
199 my $cust_main     = $opt{'cust_main'};
200 my $prospect_main = $opt{'prospect_main'};
201
202 my $prefix = ($cust_main && length($cust_main->ship_last)) ? 'ship_' : '';
203
204 my $locationnum;
205 if ( length($opt{'curr_value'}) ) {
206   $locationnum = $opt{'curr_value'};
207 } else {
208   $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
209   $locationnum = $1;
210 }
211
212 #probably could use explicit controls
213 # (cust_main locations not editable for tax reasons)
214 my $editable = $cust_main ? 0 : 1; #could use explicit control
215 my $addnew = $cust_main ? 1 : ( $locationnum>0 ? 0 : 1 );
216
217 my @location_fields = qw( address1 address2 city county state zip country );
218 if ( $opt{'alt_format'} ) {
219     push @location_fields, qw( location_type location_number location_kind );
220 }
221
222 my $cust_location;
223 if ( $locationnum && $locationnum > 0 ) {
224   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
225     or die "unknown locationnum";
226 } else {
227   $cust_location = new FS::cust_location;
228   if ( $locationnum == -1 ) {
229     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
230   } elsif ( $cust_pkg && $cust_pkg->locationnum ) {
231     my $pkg_location = $cust_pkg->cust_location;
232     $cust_location->$_( $pkg_location->$_ ) foreach @location_fields;
233     $opt{'empty_label'} ||= 'package address: '.$pkg_location->line;
234   } elsif ( $cust_main ) {
235     $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;
236   }
237 }
238
239 my $location_sort = sub {
240         $a->country   cmp $b->country
241   or lc($a->city)     cmp lc($b->city)
242   or lc($a->address1) cmp lc($b->address1)
243   or lc($a->address2) cmp lc($b->address2)
244 };
245
246 my $disabled = ( $locationnum < 0 || ($editable && $locationnum) )
247                  ? ''
248                  : 'DISABLED';
249
250 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
251
252 </%init>