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,
10 'table' => 'cust_main_county',
11 'hashref' => $hashref,
13 'ORDER BY country, state, county, city, '.
14 'district, taxclass, '.
15 "COALESCE(taxname, '')",
17 'count_query' => $count_query,
19 'header2' => \@header2,
23 'cell_style' => \@cell_style,
25 'link_onclicks' => \@link_onclicks,
30 my $conf = new FS::Conf;
31 my $money_char = $conf->config('money_char') || '$';
33 my $exempt_sub = sub {
34 my $cust_main_county = shift;
38 sprintf("$money_char%.2f per month", $cust_main_county->exempt_amount )
39 if $cust_main_county->exempt_amount > 0;
41 push @exempt, 'Setup fee'
42 if $cust_main_county->setuptax =~ /^Y$/i;
44 push @exempt, 'Recurring fee'
45 if $cust_main_county->recurtax =~ /^Y$/i;
47 [ map [ {'data'=>$_} ], @exempt ];
52 my $cell_style_sub = sub {
54 if ( $cs_oldrow ne $row ) {
56 if ( $cs_oldrow->country ne $row->country ) {
57 $cell_style = 'border-top:2px solid #000000';
58 } elsif ( $cs_oldrow->state ne $row->state ) {
59 $cell_style = 'border-top:1px solid #888888';
60 } elsif ( $cs_oldrow->county ne $row->county ) {
61 $cell_style = 'border-top:1px solid #cccccc';
71 #my $edit_link = [ "${p}edit/cust_main_county.html", 'taxnum' ];
72 my $edit_link = [ 'javascript:void(0);', sub { ''; } ];
74 my $edit_onclick = sub {
76 my $taxnum = $row->taxnum;
77 include( '/elements/popup_link_onclick.html',
78 'action' => "${p}edit/cust_main_county.html?$taxnum",
79 'actionlabel' => 'Edit tax rate',
81 #default# 'width' => 540,
82 #default# 'color' => '#333399',
90 if ( $ex_oldrow eq $param{'row'} ) {
93 $ex_oldrow = $param{'row'};
96 my $taxnum = $param{'row'}->taxnum;
97 my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum";
100 include( '/elements/popup_link.html',
101 'label' => $param{'label'},
103 'actionlabel' => $param{'desc'},
105 #default# 'width' => 540,
106 #default# 'color' => '#333399',
114 #if ( $ex_oldrow eq $param{'row'} ) {
117 # $ex_oldrow = $param{'row'};
120 my %below = ( 'county' => 'city',
123 my $what = $below{ $param{'col' } };
125 my $taxnum = $param{'row'}->taxnum;
126 my $url = "${p}edit/cust_main_county-add.cgi?taxnum=$taxnum;what=$what";
129 include( '/elements/popup_link.html',
130 'label' => $param{'label'},
132 'actionlabel' => $param{'desc'},
134 #default# 'width' => 540,
135 #default# 'color' => '#333399',
143 my $row = $param{'row'};
144 my $col = $param{'col'};
146 # if $col eq 'state' and $row->city
148 # 'table' => 'cust_main_county',
150 # 'country' => $row->country,
151 # 'state' => $row->state,
152 # 'city' => { op=>'!=', value=>'' },
154 # 'order_by' => 'LIMIT 1',
157 my %below = ( 'county' => 'city',
161 #XXX can still show the link when you have some counties broken down into
162 #cities and others not :/
164 my $taxnum = $param{'row'}->taxnum;
165 my $url = "${p}edit/process/cust_main_county-collapse.cgi?taxnum=$taxnum;".
166 'country='. uri_escape($cgi->param('country')). ';'.
167 'state='. uri_escape($cgi->param('state')). ';'.
168 'county='. uri_escape($cgi->param('county'));
169 $url = "javascript:collapse_areyousure('$url', '$below{$col}', '$col')";
171 qq(<FONT SIZE="-1"><A HREF="$url">$param{'label'}</A></FONT>);
177 my $row = $param{'row'};
178 my $col = $param{'col'};
180 my $taxnum = $param{'row'}->taxnum;
181 my $url = "${p}edit/process/cust_main_county-remove.cgi?taxnum=$taxnum;".
182 'country='. uri_escape($cgi->param('country')). ';'.
183 'state='. uri_escape($cgi->param('state')). ';'.
184 'county='. uri_escape($cgi->param('county'));
185 $url = "javascript:remove_areyousure('$url', '$col')";
187 qq(<FONT SIZE="-1"><A HREF="$url">$param{'label'}</A></FONT>);
191 sub separate_taxclasses_link {
193 my $taxnum = $row->taxnum;
194 my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum";
196 qq!<FONT SIZE="-1"><A HREF="$url">!;
199 #un-separate taxclasses too
205 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
207 #my $conf = new FS::Conf;
208 #my $money_char = $conf->config('money_char') || '$';
209 my $enable_taxclasses = $conf->exists('enable_taxclasses');
213 my $html_init = <<END;
215 function collapse_areyousure(href,col,above) {
216 if (confirm('Are you sure you want to remove all ' + col + ' tax rates for this ' + above + '?') == true)
217 window.location.href = href;
219 function remove_areyousure(href,col) {
220 if (confirm('Are you sure you want to remove this ' + col + '?') == true)
221 window.location.href = href;
226 $html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass."
227 if $enable_taxclasses;
228 $html_init .= '<BR><BR>';
230 $html_init .= include('/elements/init_overlib.html');
235 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
239 $cgi->delete('country');
242 if ( $country && $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) {
244 $title = "$state, $title";
246 $cgi->delete('state');
249 if ( $country && $state &&
250 $cgi->param('county') =~
251 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
255 if ( $county eq '__NONE__' ) {
256 $title = "No county, $title";
258 $title = "$county county, $title";
261 $cgi->delete('county');
263 $title = " for $title" if $title;
266 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
268 $title .= " for $taxclass tax class";
270 $cgi->delete('taxclass');
272 if ( $country || $taxclass ) {
273 push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
276 $cgi->param('dummy', 1);
279 "window.location = '". $cgi->self_url.
280 ";country=' + encodeURIComponent( document.getElementById('country').options[document.getElementById('country').selectedIndex].value ) + ".
281 "';state=' + encodeURIComponent( document.getElementById('state').options[document.getElementById('state').selectedIndex].value ) +".
282 "';county=' + encodeURIComponent( document.getElementById('county').options[document.getElementById('county').selectedIndex].value );";
284 #restore this so pagination works
285 $cgi->param('country', $country) if $country;
286 $cgi->param('state', $state ) if $state;
287 $cgi->param('county', $county ) if $county;
288 $cgi->param('taxclass', $county ) if $taxclass;
291 '<BR>( show country: '.
292 include('/elements/select-country.html',
293 'country' => $country,
294 'onchange' => $filter_change,
295 'empty_label' => '(all)',
296 'disable_empty' => 0,
297 'disable_stateupdate' => 1,
300 my %states_hash = $country ? states_hash($country) : ();
301 if ( scalar(keys(%states_hash)) > 1 ) {
304 include('/elements/select-state.html',
305 'country' => $country,
307 'onchange' => $filter_change,
308 'empty_label' => '(all)',
309 'disable_empty' => 0,
310 'disable_countyupdate' => 1,
314 '<SELECT NAME="state" ID="state" STYLE="display:none">'.
315 ' <OPTION VALUE="" SELECTED>'.
319 my @counties = ( $country && $state ) ? counties($state, $country) : ();
320 if ( scalar(@counties) > 1 ) {
323 include('/elements/select-county.html',
324 'country' => $country,
327 'onchange' => $filter_change,
328 'empty_label' => '(all)',
329 'empty_data_label' => '(none)',
330 'empty_data_value' => '__NONE__',
331 'disable_empty' => 0,
332 'disable_cityupdate' => 1,
336 '<SELECT NAME="county" ID="county" STYLE="display:none">'.
337 ' <OPTION VALUE="" SELECTED>'.
341 $html_posttotal .= ' )';
343 my $bulk_popup_link =
344 include( '/elements/popup_link_onclick.html',
345 'action' => "${p}edit/bulk-cust_main_county.html?taxnum=MAGIC_taxnum_MAGIC",
346 'actionlabel' => 'Bulk add new tax',
349 #default# 'width' => 540,
350 #default# 'color' => '#333399',
353 my $html_foot = <<END;
354 <SCRIPT TYPE="text/javascript">
356 function setAll(setTo) {
357 theForm = document.taxesForm;
358 for (i=0,n=theForm.elements.length;i<n;i++) {
359 if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
360 theForm.elements[i].checked = setTo;
365 function toggleAll() {
366 theForm = document.taxesForm;
367 for (i=0,n=theForm.elements.length;i<n;i++) {
368 if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
369 if ( theForm.elements[i].checked == true ) {
370 theForm.elements[i].checked = false;
372 theForm.elements[i].checked = true;
378 function bulkPopup(action) {
379 var bulk_popup_link = "$bulk_popup_link";
381 theForm = document.taxesForm;
382 for (i=0,n=theForm.elements.length;i<n;i++) {
383 if ( theForm.elements[i].name.indexOf("cust_main_county") != -1
384 && theForm.elements[i].checked == true
386 var name = theForm.elements[i].name;
387 var taxnum = name.replace(/cust_main_county/, '');
388 if ( bulkstring != '' ) {
389 bulkstring = bulkstring + ',';
391 bulkstring = bulkstring + taxnum;
395 bulkstring = bulkstring + ';action=' + action;
396 if ( bulk_popup_link.length > 1920 ) { // IE 2083 URL limit
397 alert('Too many selections'); // should do some session thing...
400 bulk_popup_link = bulk_popup_link.replace(/MAGIC_taxnum_MAGIC/, bulkstring);
401 eval(bulk_popup_link);
407 <A HREF="javascript:setAll(true)">select all</A> |
408 <A HREF="javascript:setAll(false)">unselect all</A> |
409 <A HREF="javascript:toggleAll()">toggle all</A>
411 <A HREF="javascript:void(0);" onClick="bulkPopup('add');">Add new tax to selected</A>
413 <A HREF="javascript:void(0);" onClick="bulkPopup('edit');">Bulk edit selected</A>
418 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
420 $hashref->{'country'} = $country;
421 $count_query .= ' WHERE country = '. dbh->quote($country);
424 $hashref->{'state'} = $state;
425 $count_query .= ' AND state = '. dbh->quote($state);
428 if ( $county eq '__NONE__' ) {
429 $hashref->{'county'} = '';
430 $count_query .= " AND ( county = '' OR county IS NULL ) ";
432 $hashref->{'county'} = $county;
433 $count_query .= ' AND county = '. dbh->quote($county);
437 $hashref->{'taxclass'} = $taxclass;
438 $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
439 ' taxclass = '. dbh->quote($taxclass);
445 my @header = ( 'Country', 'State/Province', 'County', 'City', '' );
446 # last column is 'district', but usually unused
447 my @header2 = ( '', '', '', '', '' );
448 my @links = ( '', '', '', '', '' );
449 my @link_onclicks = ( '', '', '', '', '' );
452 my %seen_country = ();
454 my %seen_county = ();
457 sub { my $country = shift->country;
458 return '' if $seen_country{$country}++;
459 code2country($country). " ($country)";
463 sub { my $label = $seen_state{$_[0]->country}->{$_[0]->state}++
464 ? '' : state_label($_[0]->state, $_[0]->country);
466 my $countylinks = ( $_[0]->county && $label )
467 ? ' '. add_link(
468 desc => 'Add more counties',
470 label=> 'add more counties',
476 label=> 'remove all counties',
485 : ' '. expand_link( desc => 'Add States',
487 label => 'add states',
492 $label.$countylinks.$addlink;
497 $seen_county{$_[0]->country}->{$_[0]->state}->{$_[0]->county}++
498 ? '' : $_[0]->county;
502 $citylinks = $_[0]->city
503 ? ' '. add_link(
504 desc => 'Add more cities',
506 label=> 'add more cities',
512 label=> 'remove all cities',
516 : ' '. remove_link( col => 'county',
517 label=> 'remove county',
526 expand_link( desc => 'Add Counties',
528 label => 'add counties',
538 if ( $r->taxclass #but if it has a taxclass, can't remove
539 or $r->district ) { # or a district
543 remove_link( col => 'city',
544 label=> 'remove city',
551 expand_link( desc => 'Add Cities',
553 label => 'add cities',
562 if ( $r->district ) {
563 $r->district . ' '.
564 remove_link( col => 'district',
565 label=> 'remove district',
570 # manually editing districts is not exactly intended
577 sub { shift->state ? '000000' : '999999' },
578 sub { shift->county ? '000000' : '999999' },
579 sub { shift->city ? '000000' : '999999' },
582 if ( $conf->exists('enable_taxclasses') ) {
583 push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
584 push @header2, '(per-package classification)';
587 if ( $r->taxclass ) {
590 my $sql = 'SELECT COUNT(*) FROM cust_main_county
591 WHERE country = ? AND state = ? AND county = ?
592 AND city = ? AND taxclass IS NOT NULL';
593 if ( FS::Record->scalar_sql($sql, map $r->$_,
594 qw( country state county city) ) ) {
598 separate_taxclasses_link($r, 'Separate Taxclasses').
599 'separate taxclasses</A></FONT>';
603 push @color, sub { shift->taxclass ? '000000' : '999999' };
605 push @link_onclicks, '';
618 '(printed on invoices)',
626 my $cust_main_county = shift;
629 if ( $cb_oldrow->district ne $cust_main_county->district
630 || $cb_oldrow->city ne $cust_main_county->city
631 || $cb_oldrow->county ne $cust_main_county->county
632 || $cb_oldrow->state ne $cust_main_county->state
633 || $cb_oldrow->country ne $cust_main_county->country
634 || $cb_oldrow->taxclass ne $cust_main_county->taxclass )
644 $cb_oldrow = $cust_main_county;
647 my $taxnum = $cust_main_county->taxnum;
648 qq!<INPUT NAME="cust_main_county$taxnum" TYPE="checkbox" VALUE="1">!;
656 sub { shift->taxname || 'Tax' },
657 sub { shift->tax. '% <FONT SIZE="-1">(edit)</FONT>' },
663 sub { shift->taxname ? '000000' : '666666' },
664 sub { shift->tax ? '000000' : '666666' },
670 my @cell_style = map $cell_style_sub, (1..scalar(@header));
672 push @links, '', '', $edit_link, '';
673 push @link_onclicks, '', '', $edit_onclick, '';