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');
264 if ( $country && $state && $county &&
265 $cgi->param('city') =~
266 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
270 if ( $city eq '__NONE__' ) {
271 $title = "No city, $title";
273 $title = "$city city, $title";
276 $cgi->delete('city');
278 $title = " for $title" if $title;
281 if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) {
283 $title .= " for $taxclass tax class";
285 $cgi->delete('taxclass');
287 if ( $country || $taxclass ) {
288 push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi';
291 $cgi->param('dummy', 1);
294 "window.location = '". $cgi->self_url.
295 ";country=' + encodeURIComponent( document.getElementById('country').options[document.getElementById('country').selectedIndex].value ) + ".
296 "';state=' + encodeURIComponent( document.getElementById('state').options[document.getElementById('state').selectedIndex].value ) +".
297 "';county=' + encodeURIComponent( document.getElementById('county').options[document.getElementById('county').selectedIndex].value )";
299 $filter_change .= " +';city=' + encodeURIComponent( document.getElementById('city').options[document.getElementById('city').selectedIndex].value )"
300 if $conf->exists('enable_taxclasses');
302 $filter_change .= ";";
304 #restore this so pagination works
305 $cgi->param('country', $country) if $country;
306 $cgi->param('state', $state ) if $state;
307 $cgi->param('county', $county ) if $county;
308 $cgi->param('city', $city ) if $city;
309 $cgi->param('taxclass', $county ) if $taxclass;
312 '<BR>( show country: '.
313 include('/elements/select-country.html',
314 'country' => $country,
315 'onchange' => $filter_change,
316 'empty_label' => '(all)',
317 'disable_empty' => 0,
318 'disable_stateupdate' => 1,
321 my %states_hash = $country ? states_hash($country) : ();
322 if ( scalar(keys(%states_hash)) > 1 ) {
325 include('/elements/select-state.html',
326 'country' => $country,
328 'onchange' => $filter_change,
329 'empty_label' => '(all)',
330 'disable_empty' => 0,
331 'disable_countyupdate' => 1,
335 '<SELECT NAME="state" ID="state" STYLE="display:none">'.
336 ' <OPTION VALUE="" SELECTED>'.
340 my @counties = ( $country && $state ) ? counties($state, $country) : ();
341 if ( scalar(@counties) > 1 ) {
344 include('/elements/select-county.html',
345 'country' => $country,
348 'onchange' => $filter_change,
349 'empty_label' => '(all)',
350 'empty_data_label' => '(none)',
351 'empty_data_value' => '__NONE__',
352 'disable_empty' => 0,
353 'disable_cityupdate' => 1,
357 '<SELECT NAME="county" ID="county" STYLE="display:none">'.
358 ' <OPTION VALUE="" SELECTED>'.
362 if ( $conf->exists('enable_taxclasses') ) {
363 my @cities = ( $country && $state && $county ) ? cities($county, $state, $country) : ();
364 if ( scalar(@cities) > 1 ) {
367 include('/elements/select-city.html',
368 'country' => $country,
372 'onchange' => $filter_change,
373 'empty_label' => '(all)',
374 'empty_data_label' => '(none)',
375 'empty_data_value' => '__NONE__',
376 'disable_empty' => 0,
377 'disable_cityupdate' => 1,
381 '<SELECT NAME="city" ID="city" STYLE="display:none">'.
382 ' <OPTION VALUE="" SELECTED>'.
387 $html_posttotal .= ' )';
389 my $bulk_popup_link =
390 include( '/elements/popup_link_onclick.html',
391 'action' => "${p}edit/bulk-cust_main_county.html?taxnum=MAGIC_taxnum_MAGIC",
392 'actionlabel' => 'Bulk add new tax',
395 #default# 'width' => 540,
396 #default# 'color' => '#333399',
399 my $html_foot = <<END;
400 <SCRIPT TYPE="text/javascript">
402 function setAll(setTo) {
403 theForm = document.taxesForm;
404 for (i=0,n=theForm.elements.length;i<n;i++) {
405 if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
406 theForm.elements[i].checked = setTo;
411 function toggleAll() {
412 theForm = document.taxesForm;
413 for (i=0,n=theForm.elements.length;i<n;i++) {
414 if (theForm.elements[i].name.indexOf("cust_main_county") != -1) {
415 if ( theForm.elements[i].checked == true ) {
416 theForm.elements[i].checked = false;
418 theForm.elements[i].checked = true;
424 function bulkPopup(action) {
425 var bulk_popup_link = "$bulk_popup_link";
427 theForm = document.taxesForm;
428 for (i=0,n=theForm.elements.length;i<n;i++) {
429 if ( theForm.elements[i].name.indexOf("cust_main_county") != -1
430 && theForm.elements[i].checked == true
432 var name = theForm.elements[i].name;
433 var taxnum = name.replace(/cust_main_county/, '');
434 if ( bulkstring != '' ) {
435 bulkstring = bulkstring + ',';
437 bulkstring = bulkstring + taxnum;
441 bulkstring = bulkstring + ';action=' + action;
442 if ( bulk_popup_link.length > 1920 ) { // IE 2083 URL limit
443 alert('Too many selections'); // should do some session thing...
446 bulk_popup_link = bulk_popup_link.replace(/MAGIC_taxnum_MAGIC/, bulkstring);
447 eval(bulk_popup_link);
453 <A HREF="javascript:setAll(true)">select all</A> |
454 <A HREF="javascript:setAll(false)">unselect all</A> |
455 <A HREF="javascript:toggleAll()">toggle all</A>
457 <A HREF="javascript:void(0);" onClick="bulkPopup('add');">Add new tax to selected</A>
459 <A HREF="javascript:void(0);" onClick="bulkPopup('edit');">Bulk edit selected</A>
461 <A HREF="javascript:void(0);" onClick="bulkPopup('edit_rate_only');">Bulk edit rate only selected</A>
465 my $count_query = 'SELECT COUNT(*) FROM cust_main_county';
467 $hashref->{'country'} = $country;
468 $count_query .= ' WHERE country = '. dbh->quote($country);
471 $hashref->{'state'} = $state;
472 $count_query .= ' AND state = '. dbh->quote($state);
475 if ( $county eq '__NONE__' ) {
476 $hashref->{'county'} = '';
477 $count_query .= " AND ( county = '' OR county IS NULL ) ";
479 $hashref->{'county'} = $county;
480 $count_query .= ' AND county = '. dbh->quote($county);
484 if ( $city eq '__NONE__' ) {
485 $hashref->{'city'} = '';
486 $count_query .= " AND ( city = '' OR city IS NULL ) ";
488 $hashref->{'city'} = $city;
489 $count_query .= ' AND city = '. dbh->quote($city);
493 $hashref->{'taxclass'} = $taxclass;
494 $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
495 ' taxclass = '. dbh->quote($taxclass);
501 my @header = ( 'Country', 'State/Province', 'County', 'City', '' );
502 # last column is 'district', but usually unused
503 my @header2 = ( '', '', '', '', '' );
504 my @links = ( '', '', '', '', '' );
505 my @link_onclicks = ( '', '', '', '', '' );
508 my %seen_country = ();
510 my %seen_county = ();
513 sub { my $country = shift->country;
514 return '' if $seen_country{$country}++;
515 FS::geocode_Mixin->code2country($country). " ($country)";
519 sub { my $label = $seen_state{$_[0]->country}->{$_[0]->state}++
520 ? '' : state_label($_[0]->state, $_[0]->country);
522 my $countylinks = ( $_[0]->county && $label )
523 ? ' '. add_link(
524 desc => 'Add more counties',
526 label=> 'add more counties',
532 label=> 'remove all counties',
541 : ' '. expand_link( desc => 'Add States',
543 label => 'add states',
548 $label.$countylinks.$addlink;
553 $seen_county{$_[0]->country}->{$_[0]->state}->{$_[0]->county}++
554 ? '' : $_[0]->county;
558 $citylinks = $_[0]->city
559 ? ' '. add_link(
560 desc => 'Add more cities',
562 label=> 'add more cities',
568 label=> 'remove all cities',
572 : ' '. remove_link( col => 'county',
573 label=> 'remove county',
582 expand_link( desc => 'Add Counties',
584 label => 'add counties',
594 if ( $r->taxclass #but if it has a taxclass, can't remove
595 or $r->district ) { # or a district
599 remove_link( col => 'city',
600 label=> 'remove city',
607 expand_link( desc => 'Add Cities',
609 label => 'add cities',
618 if ( $r->district ) {
619 $r->district . ' '.
620 remove_link( col => 'district',
621 label=> 'remove district',
626 # manually editing districts is not exactly intended
633 sub { shift->state ? '000000' : '999999' },
634 sub { shift->county ? '000000' : '999999' },
635 sub { shift->city ? '000000' : '999999' },
638 if ( $conf->exists('enable_taxclasses') ) {
639 push @header, qq!Tax class (<A HREF="${p}edit/part_pkg_taxclass.html">add new</A>)!;
640 push @header2, '(per-package classification)';
643 if ( $r->taxclass ) {
646 my $sql = 'SELECT COUNT(*) FROM cust_main_county
647 WHERE country = ? AND state = ? AND county = ?
648 AND city = ? AND taxclass IS NOT NULL';
649 if ( FS::Record->scalar_sql($sql, map $r->$_,
650 qw( country state county city) ) ) {
654 separate_taxclasses_link($r, 'Separate Taxclasses').
655 'separate taxclasses</A></FONT>';
659 push @color, sub { shift->taxclass ? '000000' : '999999' };
661 push @link_onclicks, '';
674 '(printed on invoices)',
682 my $cust_main_county = shift;
685 if ( $cb_oldrow->district ne $cust_main_county->district
686 || $cb_oldrow->city ne $cust_main_county->city
687 || $cb_oldrow->county ne $cust_main_county->county
688 || $cb_oldrow->state ne $cust_main_county->state
689 || $cb_oldrow->country ne $cust_main_county->country
690 || $cb_oldrow->taxclass ne $cust_main_county->taxclass )
700 $cb_oldrow = $cust_main_county;
703 my $taxnum = $cust_main_county->taxnum;
704 qq!<INPUT NAME="cust_main_county$taxnum" TYPE="checkbox" VALUE="1">!;
712 sub { shift->taxname || 'Tax' },
713 sub { shift->tax. '% <FONT SIZE="-1">(edit)</FONT>' },
719 sub { shift->taxname ? '000000' : '666666' },
720 sub { shift->tax ? '000000' : '666666' },
726 my @cell_style = map $cell_style_sub, (1..scalar(@header));
728 push @links, '', '', $edit_link, '';
729 push @link_onclicks, '', '', $edit_onclick, '';