X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Freport_tax.cgi;h=8cf6bd7588162fc78aa96a9b5a163684f3d2285d;hb=ba50ec34aa882a14256550052a2b061d5c6fcb81;hp=ac76fad6e7ad50c2add0f8cb6a34a41cda96cb97;hpb=6c2f4c44fc083bde9dd055bd4db51e65fa377379;p=freeside.git diff --git a/httemplate/search/report_tax.cgi b/httemplate/search/report_tax.cgi index ac76fad6e..8cf6bd758 100755 --- a/httemplate/search/report_tax.cgi +++ b/httemplate/search/report_tax.cgi @@ -4,22 +4,217 @@ my $user = getotaker; $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/; -my $beginning = $1; +my $pbeginning = $1; +my $beginning = $1 ? str2time($1) : 0; $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/; -my $ending = $1; +my $pending = $1; +my $ending = ( $1 ? str2time($1) : 4294880896 ) + 86399; -print header('Tax Report Results'); +my $from_join_cust = " + FROM cust_bill_pkg + JOIN cust_bill USING ( invnum ) + JOIN cust_main USING ( custnum ) +"; +my $join_pkg = " + JOIN cust_pkg USING ( pkgnum ) + JOIN part_pkg USING ( pkgpart ) +"; +my $where = " + WHERE _date >= $beginning AND _date <= $ending + AND ( county = ? OR ? = '' ) + AND ( state = ? OR ? = '' ) + AND ( country = ? ) + AND payby != 'COMP' +"; +my @base_param = qw( county county state state country ); -open (REPORT, "freeside-tax-report -v -s $beginning -f $ending $user |"); +my $gotcust = " + WHERE 0 < ( SELECT COUNT(*) FROM cust_main + WHERE ( cust_main.county = cust_main_county.county + OR cust_main_county.county = '' + OR cust_main_county.county IS NULL ) + AND ( cust_main.state = cust_main_county.state + OR cust_main_county.state = '' + OR cust_main_county.state IS NULL ) + AND ( cust_main.country = cust_main_county.country ) + LIMIT 1 + ) +"; + +my($total, $exempt, $taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 ); +my $out = 'Out of taxable region(s)'; +my %regions; +foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) { + #warn $r->county. ' '. $r->state. ' '. $r->country. "\n"; + + my $label = getlabel($r); + $regions{$label}->{'label'} = $label; + + my $fromwhere = $from_join_cust. $join_pkg. $where; + my @param = @base_param; + + if ( $r->taxclass ) { + $fromwhere .= " AND ( taxclass = ? ) "; + push @param, 'taxclass'; + } + + my $nottax = 'pkgnum != 0'; + + my $a = scalar_sql($r, \@param, + "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax" + ); + $total += $a; + $regions{$label}->{'total'} += $a; + + foreach my $e ( grep { $r->get($_.'tax') =~ /^Y/i } + qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) { + my $x = scalar_sql($r, \@param, + "SELECT SUM($e) $fromwhere AND $nottax" + ); + $exempt += $x; + $regions{$label}->{'exempt'} += $x; + } + + foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i } + qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) { + my $t = scalar_sql($r, \@param, + "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )" + ); + $taxable += $t; + $regions{$label}->{'taxable'} += $t; + $owed += $t * ($r->tax/100); + $regions{$label}->{'owed'} += $t * ($r->tax/100); + + my $x = scalar_sql($r, \@param, + "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'" + ); + $exempt += $x; + $regions{$label}->{'exempt'} += $x; + } + + if ( defined($regions{$label}->{'rate'}) + && $regions{$label}->{'rate'} != $r->tax.'%' ) { + $regions{$label}->{'rate'} = 'variable'; + } else { + $regions{$label}->{'rate'} = $r->tax.'%'; + } -print '
'; -while('; -print ') { - print $_; } -print '