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