deal 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 && $cgi->param('county') =~ /^([\w \-\'\[\]]+)$/ ) {
152   $county = $1;
153   if ( $county eq '__NONE__' ) {
154     $title = "No county, $title";
155   } else {
156     $title = "$county county, $title";
157   }
158 }
159 $cgi->delete('county');
160
161 $title = " for $title" if $title;
162
163 my $taxclass = '';
164 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
165   $taxclass = $1;
166   $title .= " for $taxclass tax class";
167 }
168 $cgi->delete('taxclass');
169
170 if ( $country || $taxclass ) {
171   push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
172 }
173
174 $cgi->param('dummy', 1);
175
176 my $filter_change =
177   "window.location = '". $cgi->self_url.
178   ";country=' + encodeURIComponent( document.getElementById('country').options[document.getElementById('country').selectedIndex].value ) + ".
179   "';state='   + encodeURIComponent( document.getElementById('state').options[document.getElementById('state').selectedIndex].value ) +".
180   "';county='  + encodeURIComponent( document.getElementById('county').options[document.getElementById('county').selectedIndex].value );";
181
182 #restore this so pagination works
183 $cgi->param('country',  $country) if $country;
184 $cgi->param('state',    $state  ) if $state;
185 $cgi->param('county',   $county ) if $county;
186 $cgi->param('taxclass', $county ) if $taxclass;
187
188 my $html_posttotal =
189   '<BR>( show country: '.
190   include('/elements/select-country.html',
191             'country'             => $country,
192             'onchange'            => $filter_change,
193             'empty_label'         => '(all)',
194             'disable_empty'       => 0,
195             'disable_stateupdate' => 1,
196          );
197
198 my %states_hash = $country ? states_hash($country) : ();
199 if ( scalar(keys(%states_hash)) > 1 ) {
200   $html_posttotal .=
201     ' show state: '.
202     include('/elements/select-state.html',
203               'country'              => $country,
204               'state'                => $state,
205               'onchange'             => $filter_change,
206               'empty_label'          => '(all)',
207               'disable_empty'        => 0,
208               'disable_countyupdate' => 1,
209            );
210 } else {
211   $html_posttotal .=
212     '<SELECT NAME="state" ID="state" STYLE="display:none">'.
213     '  <OPTION VALUE="" SELECTED>'.
214     '</SELECT>';
215 }
216
217 my @counties = ( $country && $state ) ? counties($state, $country) : ();
218 if ( scalar(@counties) > 1 ) {
219   $html_posttotal .=
220     ' show county: '.
221     include('/elements/select-county.html',
222               'country'              => $country,
223               'state'                => $state,
224               'county'               => $county,
225               'onchange'             => $filter_change,
226               'empty_label'          => '(all)',
227               'empty_data_label'     => '(none)',
228               'empty_data_value'     => '__NONE__',
229               'disable_empty'        => 0,
230               'disable_countyupdate' => 1,
231            );
232 } else {
233   $html_posttotal .=
234     '<SELECT NAME="county" ID="county" STYLE="display:none">'.
235     '  <OPTION VALUE="" SELECTED>'.
236     '</SELECT>';
237 }
238
239 $html_posttotal .= ' )';
240
241 my $bulk_popup_link = 
242   include( '/elements/popup_link_onclick.html',
243              'action'      => "${p}edit/bulk-cust_main_county.html?MAGIC_taxnum_MAGIC",
244              'actionlabel' => 'Bulk add new tax',
245              'nofalse'     => 1,
246              'height'      => 420,
247              #default# 'width'  => 540,
248              #default# 'color' => '#333399',
249          );
250
251 my $html_foot = <<END;
252 <SCRIPT TYPE="text/javascript">
253
254   function setAll(setTo) {
255     theForm = document.taxesForm;
256     for (i=0,n=theForm.elements.length;i<n;i++) {
257       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
258         theForm.elements[i].checked = setTo;
259       }
260     }
261   }
262
263   function toggleAll() {
264     theForm = document.taxesForm;
265     for (i=0,n=theForm.elements.length;i<n;i++) {
266       if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
267         if ( theForm.elements[i].checked == true ) {
268           theForm.elements[i].checked = false;
269         } else {
270           theForm.elements[i].checked = true;
271         }
272       }
273     }
274   }
275
276   function bulkPopup() {
277     var bulk_popup_link = "$bulk_popup_link";
278     var bulkstring = '';
279     theForm = document.taxesForm;
280     for (i=0,n=theForm.elements.length;i<n;i++) {
281       if (    theForm.elements[i].name.indexOf("cust_main_county") != -1
282            && theForm.elements[i].checked == true
283          ) {
284         var name = theForm.elements[i].name;
285         var taxnum = name.replace(/cust_main_county/, '');
286         if ( bulkstring != '' ) {
287           bulkstring = bulkstring + ',';
288         }
289         bulkstring = bulkstring + taxnum;
290        
291       }
292     }
293     if ( bulk_popup_link.length > 1920 ) { // IE 2083 URL limit
294       alert('Too many selections'); // should do some session thing...
295       return false;
296     }
297     bulk_popup_link = bulk_popup_link.replace(/MAGIC_taxnum_MAGIC/, bulkstring);
298     eval(bulk_popup_link);
299   }
300
301 </SCRIPT>
302
303 <BR>
304 <A HREF="javascript:setAll(true)">select all</A> |
305 <A HREF="javascript:setAll(false)">unselect all</A> |
306 <A HREF="javascript:toggleAll()">toggle all</A>
307 <BR><BR>
308 <A HREF="javascript:void(0);" onClick="bulkPopup();">Add new tax to selected</A>
309
310 END
311
312 my $hashref = {};
313 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
314 if ( $country ) {
315   $hashref->{'country'} = $country;
316   $count_query .= ' WHERE country = '. dbh->quote($country);
317 }
318 if ( $state ) {
319   $hashref->{'state'} = $state;
320   $count_query .= ' AND state   = '. dbh->quote($state);
321 }
322 if ( $county ) {
323   if ( $county eq '__NONE__' ) {
324     $hashref->{'county'} = '';
325     $count_query .= " AND ( county = '' OR county IS NULL ) ";
326   } else {
327     $hashref->{'county'} = $county;
328     $count_query .= ' AND county  = '. dbh->quote($county);
329   }
330 }
331 if ( $taxclass ) {
332   $hashref->{'taxclass'} = $taxclass;
333   $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
334                   ' taxclass  = '. dbh->quote($taxclass);
335 }
336
337
338 $cell_style = '';
339
340 my @header        = ( 'Country', 'State/Province', 'County',);
341 my @header2       = ( '', '', '', );
342 my @links         = ( '', '', '', );
343 my @link_onclicks = ( '', '', '', );
344 my $align = 'lll';
345
346 my @fields = (
347   sub { my $country = shift->country;
348         code2country($country). " ($country)";
349       },
350   sub { state_label($_[0]->state, $_[0]->country).
351         ( $_[0]->state
352             ? ''
353             : '&nbsp'. expand_link( desc  => 'Add States',
354                                     row   => $_[0],
355                                     label => 'add&nbsp;states',
356                                   )
357         )
358       },
359   sub { $_[0]->county || '(all)&nbsp'.
360                          expand_link( desc  => 'Add Counties',
361                                       row   => $_[0],
362                                       label => 'add&nbsp;counties',
363                                     )
364       },
365 );
366
367 my @color = (
368   '000000',
369   sub { shift->state  ? '000000' : '999999' },
370   sub { shift->county ? '000000' : '999999' },
371 );
372
373 if ( $conf->exists('enable_taxclasses') ) {
374   push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
375   push @header2, '(per-package classification)';
376   push @fields, sub { $_[0]->taxclass || '(all)&nbsp'.
377                        separate_taxclasses_link($_[0], 'Separate Taxclasses').
378                        'separate&nbsp;taxclasses</A></FONT>'
379                     };
380   push @color, sub { shift->taxclass ? '000000' : '999999' };
381   push @links, '';
382   push @link_onclicks, '';
383   $align .= 'l';
384 }
385
386 push @header,
387               '', #checkbox column
388               'Tax name',
389               'Rate', #'Tax',
390               'Exemptions',
391               ;
392
393 push @header2,
394                '',
395                '(printed on invoices)',
396                '',
397                '',
398                ;
399
400 my $newregion = 1;
401 my $cb_oldrow = '';
402 my $cb_sub = sub {
403   my $cust_main_county = shift;
404
405   if ( $cb_oldrow ) {
406     if (    $cb_oldrow->country  ne $cust_main_county->country 
407          || $cb_oldrow->state    ne $cust_main_county->state  
408          || $cb_oldrow->county   ne $cust_main_county->county  
409          || $cb_oldrow->taxclass ne $cust_main_county->taxclass )
410     {
411       $newregion = 1;
412     } else {
413       $newregion = 0;
414     }  
415     
416   } else {
417     $newregion = 1;
418   }
419   $cb_oldrow = $cust_main_county;
420
421   if ( $newregion ) {
422     my $taxnum = $cust_main_county->taxnum;
423     qq!<INPUT NAME="cust_main_county$taxnum" TYPE="checkbox" VALUE="1">!;
424   } else {
425     '';
426   }
427 };
428
429 push @fields, 
430   $cb_sub,
431   sub { shift->taxname || 'Tax' },
432   sub { shift->tax. '%&nbsp;<FONT SIZE="-1">(edit)</FONT>' },
433   $exempt_sub,
434 ;
435
436 push @color,
437   '000000',
438   sub { shift->taxname ? '000000' : '666666' },
439   sub { shift->tax     ? '000000' : '666666' },
440   '000000',
441 ;
442
443 $align .= 'clrl';
444
445 my @cell_style = map $cell_style_sub, (1..scalar(@header));
446
447 push @links,         '', '', $edit_link,    '';
448 push @link_onclicks, '', '', $edit_onclick, '';
449
450 </%init>