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