finish dealing with counties with spaces, etc., RT#4496
[freeside.git] / httemplate / browse / cust_main_county.cgi
1 <% include( 'elements/browse.html',
2      'title'          => "Tax Rates $title",
3      'name_singular'  => 'tax rate',
4      'menubar'        => \@menubar,
5      'html_init'      => $html_init,
6      'html_posttotal' => $html_posttotal,
7      'html_form'      => '<FORM NAME="taxesForm">',
8      'html_foot'      => $html_foot,
9      'query'          => {
10                            'table'    => 'cust_main_county',
11                            'hashref'  => $hashref,
12                            'order_by' =>
13                              'ORDER BY country, state, county, taxclass',
14                          },
15      'count_query'    => $count_query,
16      'header'         => \@header,
17      'header2'        => \@header2,
18      'fields'         => \@fields,
19      'align'          => $align,
20      'color'          => \@color,
21      'cell_style'     => \@cell_style,
22      'links'          => \@links,
23      'link_onclicks'  => \@link_onclicks,
24   )
25 %>
26 %
27 % #         <FONT SIZE=-1><A HREF="<% $p %>edit/process/cust_main_county-collapse.cgi?<% $hashref->{taxnum} %>">collapse state</A></FONT>
28 % # % } 
29 %
30 <%once>
31
32 my $conf = new FS::Conf;
33 my $money_char = $conf->config('money_char') || '$';
34
35 my $exempt_sub = sub {
36   my $cust_main_county = shift;
37
38   my @exempt = ();
39   push @exempt,
40        sprintf("$money_char%.2f&nbsp;per&nbsp;month", $cust_main_county->exempt_amount )
41     if $cust_main_county->exempt_amount > 0;
42
43   push @exempt, 'Setup&nbsp;fee'
44     if $cust_main_county->setuptax =~ /^Y$/i;
45
46   push @exempt, 'Recurring&nbsp;fee'
47     if $cust_main_county->recurtax =~ /^Y$/i;
48
49   [ map [ {'data'=>$_} ], @exempt ];
50 };
51
52 my $oldrow;
53 my $cell_style;
54 my $cell_style_sub = sub {
55   my $row = shift;
56   if ( $oldrow ne $row ) {
57     if ( $oldrow ) {
58       if ( $oldrow->country ne $row->country ) {
59         $cell_style = 'border-top:1px solid #000000';
60       } elsif ( $oldrow->state ne $row->state ) {
61         $cell_style = 'border-top:1px solid #cccccc'; #default?
62       } elsif ( $oldrow->state eq $row->state ) {
63         #$cell_style = 'border-top:dashed 1px dark gray';
64         $cell_style = 'border-top:1px dashed #cccccc';
65       }
66     }
67     $oldrow = $row;
68   }
69   return $cell_style;
70 };
71
72 #my $edit_link = [ "${p}edit/cust_main_county.html", 'taxnum' ];
73 my $edit_link = [ 'javascript:void(0);', sub { ''; } ];
74
75 my $edit_onclick = sub {
76   my $row = shift;
77   my $taxnum = $row->taxnum;
78   include( '/elements/popup_link_onclick.html',
79              'action'      => "${p}edit/cust_main_county.html?$taxnum",
80              'actionlabel' => 'Edit tax rate',
81              'height'      => 420,
82              #default# 'width'  => 540,
83              #default# 'color' => '#333399',
84          );
85 };
86
87 sub expand_link {
88   my %param = @_;
89
90   my $taxnum = $param{'row'}->taxnum;
91   my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum";
92
93   '<FONT SIZE="-1">'.
94     include( '/elements/popup_link.html',
95                'label'       => $param{'label'},
96                'action'      => $url,
97                'actionlabel' => $param{'desc'},
98                'height'      => 420,
99                #default# 'width'  => 540,
100                #default# 'color' => '#333399',
101            ).
102   '</FONT>';
103 }
104
105 sub separate_taxclasses_link {
106   my( $row ) = @_;
107   my $taxnum = $row->taxnum;
108   my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum";
109
110   qq!<FONT SIZE="-1"><A HREF="$url">!;
111 }
112
113 </%once>
114 <%init>
115
116 die "access denied"
117   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
118
119 #my $conf = new FS::Conf;
120 #my $money_char = $conf->config('money_char') || '$';
121 my $enable_taxclasses = $conf->exists('enable_taxclasses');
122
123 my @menubar;
124
125 my $html_init =
126   "Click on <u>add states</u> to specify a country's tax rates by state or province.
127    <BR>Click on <u>add counties</u> to specify a state's tax rates by county.";
128 $html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass."
129   if $enable_taxclasses;
130 $html_init .= '<BR><BR>';
131
132 $html_init .= include('/elements/init_overlib.html');
133
134 my $title = '';
135
136 my $country = '';
137 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
138   $country = $1;
139   $title = $country;
140 }
141 $cgi->delete('country');
142
143 my $state = '';
144 if ( $country && $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) {
145   $state = $1;
146   $title = "$state, $title";
147 }
148 $cgi->delete('state');
149
150 my $county = '';
151 if ( $country && $state &&
152      $cgi->param('county') =~
153        /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
154    )
155 {
156   $county = $1;
157   if ( $county eq '__NONE__' ) {
158     $title = "No county, $title";
159   } else {
160     $title = "$county county, $title";
161   }
162 }
163 $cgi->delete('county');
164
165 $title = " for $title" if $title;
166
167 my $taxclass = '';
168 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
169   $taxclass = $1;
170   $title .= " for $taxclass tax class";
171 }
172 $cgi->delete('taxclass');
173
174 if ( $country || $taxclass ) {
175   push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
176 }
177
178 $cgi->param('dummy', 1);
179
180 my $filter_change =
181   "window.location = '". $cgi->self_url.
182   ";country=' + encodeURIComponent( document.getElementById('country').options[document.getElementById('country').selectedIndex].value ) + ".
183   "';state='   + encodeURIComponent( document.getElementById('state').options[document.getElementById('state').selectedIndex].value ) +".
184   "';county='  + encodeURIComponent( document.getElementById('county').options[document.getElementById('county').selectedIndex].value );";
185
186 #restore this so pagination works
187 $cgi->param('country',  $country) if $country;
188 $cgi->param('state',    $state  ) if $state;
189 $cgi->param('county',   $county ) if $county;
190 $cgi->param('taxclass', $county ) if $taxclass;
191
192 my $html_posttotal =
193   '<BR>( show country: '.
194   include('/elements/select-country.html',
195             'country'             => $country,
196             'onchange'            => $filter_change,
197             'empty_label'         => '(all)',
198             'disable_empty'       => 0,
199             'disable_stateupdate' => 1,
200          );
201
202 my %states_hash = $country ? states_hash($country) : ();
203 if ( scalar(keys(%states_hash)) > 1 ) {
204   $html_posttotal .=
205     ' show state: '.
206     include('/elements/select-state.html',
207               'country'              => $country,
208               'state'                => $state,
209               'onchange'             => $filter_change,
210               'empty_label'          => '(all)',
211               'disable_empty'        => 0,
212               'disable_countyupdate' => 1,
213            );
214 } else {
215   $html_posttotal .=
216     '<SELECT NAME="state" ID="state" STYLE="display:none">'.
217     '  <OPTION VALUE="" SELECTED>'.
218     '</SELECT>';
219 }
220
221 my @counties = ( $country && $state ) ? counties($state, $country) : ();
222 if ( scalar(@counties) > 1 ) {
223   $html_posttotal .=
224     ' show county: '.
225     include('/elements/select-county.html',
226               'country'              => $country,
227               'state'                => $state,
228               'county'               => $county,
229               'onchange'             => $filter_change,
230               'empty_label'          => '(all)',
231               'empty_data_label'     => '(none)',
232               'empty_data_value'     => '__NONE__',
233               'disable_empty'        => 0,
234               'disable_countyupdate' => 1,
235            );
236 } else {
237   $html_posttotal .=
238     '<SELECT NAME="county" ID="county" STYLE="display:none">'.
239     '  <OPTION VALUE="" SELECTED>'.
240     '</SELECT>';
241 }
242
243 $html_posttotal .= ' )';
244
245 my $bulk_popup_link = 
246   include( '/elements/popup_link_onclick.html',
247              'action'      => "${p}edit/bulk-cust_main_county.html?MAGIC_taxnum_MAGIC",
248              'actionlabel' => 'Bulk add new tax',
249              'nofalse'     => 1,
250              'height'      => 420,
251              #default# 'width'  => 540,
252              #default# 'color' => '#333399',
253          );
254
255 my $html_foot = <<END;
256 <SCRIPT TYPE="text/javascript">
257
258   function setAll(setTo) {
259     theForm = document.taxesForm;
260     for (i=0,n=theForm.elements.length;i<n;i++) {
261       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
262         theForm.elements[i].checked = setTo;
263       }
264     }
265   }
266
267   function toggleAll() {
268     theForm = document.taxesForm;
269     for (i=0,n=theForm.elements.length;i<n;i++) {
270       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
271         if ( theForm.elements[i].checked == true ) {
272           theForm.elements[i].checked = false;
273         } else {
274           theForm.elements[i].checked = true;
275         }
276       }
277     }
278   }
279
280   function bulkPopup() {
281     var bulk_popup_link = "$bulk_popup_link";
282     var bulkstring = '';
283     theForm = document.taxesForm;
284     for (i=0,n=theForm.elements.length;i<n;i++) {
285       if (    theForm.elements[i].name.indexOf("cust_main_county") != -1
286            && theForm.elements[i].checked == true
287          ) {
288         var name = theForm.elements[i].name;
289         var taxnum = name.replace(/cust_main_county/, '');
290         if ( bulkstring != '' ) {
291           bulkstring = bulkstring + ',';
292         }
293         bulkstring = bulkstring + taxnum;
294        
295       }
296     }
297     if ( bulk_popup_link.length > 1920 ) { // IE 2083 URL limit
298       alert('Too many selections'); // should do some session thing...
299       return false;
300     }
301     bulk_popup_link = bulk_popup_link.replace(/MAGIC_taxnum_MAGIC/, bulkstring);
302     eval(bulk_popup_link);
303   }
304
305 </SCRIPT>
306
307 <BR>
308 <A HREF="javascript:setAll(true)">select all</A> |
309 <A HREF="javascript:setAll(false)">unselect all</A> |
310 <A HREF="javascript:toggleAll()">toggle all</A>
311 <BR><BR>
312 <A HREF="javascript:void(0);" onClick="bulkPopup();">Add new tax to selected</A>
313
314 END
315
316 my $hashref = {};
317 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
318 if ( $country ) {
319   $hashref->{'country'} = $country;
320   $count_query .= ' WHERE country = '. dbh->quote($country);
321 }
322 if ( $state ) {
323   $hashref->{'state'} = $state;
324   $count_query .= ' AND state   = '. dbh->quote($state);
325 }
326 if ( $county ) {
327   if ( $county eq '__NONE__' ) {
328     $hashref->{'county'} = '';
329     $count_query .= " AND ( county = '' OR county IS NULL ) ";
330   } else {
331     $hashref->{'county'} = $county;
332     $count_query .= ' AND county  = '. dbh->quote($county);
333   }
334 }
335 if ( $taxclass ) {
336   $hashref->{'taxclass'} = $taxclass;
337   $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
338                   ' taxclass  = '. dbh->quote($taxclass);
339 }
340
341
342 $cell_style = '';
343
344 my @header        = ( 'Country', 'State/Province', 'County',);
345 my @header2       = ( '', '', '', );
346 my @links         = ( '', '', '', );
347 my @link_onclicks = ( '', '', '', );
348 my $align = 'lll';
349
350 my @fields = (
351   sub { my $country = shift->country;
352         code2country($country). " ($country)";
353       },
354   sub { state_label($_[0]->state, $_[0]->country).
355         ( $_[0]->state
356             ? ''
357             : '&nbsp'. expand_link( desc  => 'Add States',
358                                     row   => $_[0],
359                                     label => 'add&nbsp;states',
360                                   )
361         )
362       },
363   sub { $_[0]->county || '(all)&nbsp'.
364                          expand_link( desc  => 'Add Counties',
365                                       row   => $_[0],
366                                       label => 'add&nbsp;counties',
367                                     )
368       },
369 );
370
371 my @color = (
372   '000000',
373   sub { shift->state  ? '000000' : '999999' },
374   sub { shift->county ? '000000' : '999999' },
375 );
376
377 if ( $conf->exists('enable_taxclasses') ) {
378   push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
379   push @header2, '(per-package classification)';
380   push @fields, sub { $_[0]->taxclass || '(all)&nbsp'.
381                        separate_taxclasses_link($_[0], 'Separate Taxclasses').
382                        'separate&nbsp;taxclasses</A></FONT>'
383                     };
384   push @color, sub { shift->taxclass ? '000000' : '999999' };
385   push @links, '';
386   push @link_onclicks, '';
387   $align .= 'l';
388 }
389
390 push @header,
391               '', #checkbox column
392               'Tax name',
393               'Rate', #'Tax',
394               'Exemptions',
395               ;
396
397 push @header2,
398                '',
399                '(printed on invoices)',
400                '',
401                '',
402                ;
403
404 my $newregion = 1;
405 my $cb_oldrow = '';
406 my $cb_sub = sub {
407   my $cust_main_county = shift;
408
409   if ( $cb_oldrow ) {
410     if (    $cb_oldrow->country  ne $cust_main_county->country 
411          || $cb_oldrow->state    ne $cust_main_county->state  
412          || $cb_oldrow->county   ne $cust_main_county->county  
413          || $cb_oldrow->taxclass ne $cust_main_county->taxclass )
414     {
415       $newregion = 1;
416     } else {
417       $newregion = 0;
418     }  
419     
420   } else {
421     $newregion = 1;
422   }
423   $cb_oldrow = $cust_main_county;
424
425   if ( $newregion ) {
426     my $taxnum = $cust_main_county->taxnum;
427     qq!<INPUT NAME="cust_main_county$taxnum" TYPE="checkbox" VALUE="1">!;
428   } else {
429     '';
430   }
431 };
432
433 push @fields, 
434   $cb_sub,
435   sub { shift->taxname || 'Tax' },
436   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
437   $exempt_sub,
438 ;
439
440 push @color,
441   '000000',
442   sub { shift->taxname ? '000000' : '666666' },
443   sub { shift->tax     ? '000000' : '666666' },
444   '000000',
445 ;
446
447 $align .= 'clrl';
448
449 my @cell_style = map $cell_style_sub, (1..scalar(@header));
450
451 push @links,         '', '', $edit_link,    '';
452 push @link_onclicks, '', '', $edit_onclick, '';
453
454 </%init>