From: Mark Wells Date: Tue, 27 Oct 2015 06:44:19 +0000 (-0700) Subject: show "out of taxable region" credits on tax report, #37088 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=3a2b8908272ca64add0522d1e6536fa011b3ef9b show "out of taxable region" credits on tax report, #37088 --- diff --git a/FS/FS/Report/Tax.pm b/FS/FS/Report/Tax.pm index 93fc64435..f1f6be38e 100644 --- a/FS/FS/Report/Tax.pm +++ b/FS/FS/Report/Tax.pm @@ -475,11 +475,7 @@ sub report_internal { if ( $agentnum ) { $outside_where .= " AND cust_main.agentnum = $agentnum"; } - my $sql_outside = "SELECT SUM(cust_bill_pkg.setup + cust_bill_pkg.recur) - FROM cust_bill_pkg - $join_cust_pkg - $outside_where - AND $nottax + $outside_where .= " AND NOT EXISTS( SELECT 1 FROM cust_tax_exempt_pkg JOIN cust_main_county USING (taxnum) @@ -492,10 +488,37 @@ sub report_internal { JOIN cust_main_county USING (taxnum) WHERE cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum AND COALESCE(cust_main_county.taxname,'Tax') = $taxname + )"; + my $sql_outside = "SELECT SUM(cust_bill_pkg.setup + cust_bill_pkg.recur) + FROM cust_bill_pkg + $join_cust_pkg + $outside_where + AND $nottax + "; + warn "\nOUT_SALES:\n$sql_outside\n" if $DEBUG; + my $out_sales = FS::Record->scalar_sql($sql_outside); + + # and out-of-region credit applications, also (excluding those applied + # to out-of-region sales _or taxes_) + if ( $opt{credit_date} eq 'cust_credit_bill' ) { + $outside_where =~ s/cust_bill._date/cust_credit_bill._date/g; + } + + $sql_outside = "SELECT SUM(cust_credit_bill_pkg.amount) + FROM cust_credit_bill_pkg + JOIN cust_bill_pkg USING (billpkgnum) + $join_cust_pkg + JOIN cust_credit_bill USING (creditbillnum) + $outside_where + AND NOT EXISTS( + SELECT 1 FROM cust_bill_pkg_tax_location + JOIN cust_main_county USING (taxnum) + WHERE cust_bill_pkg_tax_location.billpkgnum = cust_bill_pkg.billpkgnum + AND COALESCE(cust_main_county.taxname,'Tax') = $taxname ) "; - warn "\nOUTSIDE:\n$sql_outside\n" if $DEBUG; - my $total_outside = FS::Record->scalar_sql($sql_outside); + warn "\nOUT_CREDIT:\n$sql_outside\n" if $DEBUG; + my $out_credit = FS::Record->scalar_sql($sql_outside); my %taxrates; foreach my $tax ( @@ -509,11 +532,12 @@ sub report_internal { # return the data bless { - 'opt' => \%opt, - 'data' => \%data, - 'total' => \%total, - 'taxrates' => \%taxrates, - 'outside' => $total_outside, + 'opt' => \%opt, + 'data' => \%data, + 'total' => \%total, + 'taxrates' => \%taxrates, + 'out_sales' => $out_sales, + 'out_credit' => $out_credit, }, $class; } diff --git a/httemplate/search/cust_credit_bill_pkg.html b/httemplate/search/cust_credit_bill_pkg.html index 5facd4ab3..b9bbc4dbb 100644 --- a/httemplate/search/cust_credit_bill_pkg.html +++ b/httemplate/search/cust_credit_bill_pkg.html @@ -210,37 +210,34 @@ if ( $cgi->param('taxclass') my @loc_param = qw( district city county state country ); if ( $cgi->param('out') ) { - # probably don't need this part - my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 ); - while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution - $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e; + my $has_taxname = ''; + if ( $cgi->param('taxname') ) { + $has_taxname = " AND COALESCE(cust_main_county.taxname, 'Tax') = " + . dbh->quote( $cgi->param('taxname') ); } - $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g - if $cgi->param('istax'); - - push @where, " - 0 = ( - SELECT COUNT(*) FROM cust_main_county - WHERE cust_main_county.tax > 0 - AND $loc_sql - ) - "; - - #not linked to by anything, but useful for debugging "out of taxable region" - if ( grep $cgi->param($_), @loc_param ) { - - my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param; - - my ( $loc_sql, @param ) = FS::cust_pkg->location_sql; - while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution - $loc_sql =~ s/\?/$ph{shift(@param)}/e; - } - - push @where, $loc_sql; - - } + # use the exact criteria from the tax report + push @where, + "NOT EXISTS( + SELECT 1 FROM cust_tax_exempt_pkg + JOIN cust_main_county USING (taxnum) + WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum + AND cust_tax_exempt_pkg.creditbillpkgnum IS NULL + $has_taxname + ) + AND NOT EXISTS( + SELECT 1 FROM cust_bill_pkg_tax_location + JOIN cust_main_county USING (taxnum) + WHERE cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum + $has_taxname + ) + AND NOT EXISTS( + SELECT 1 FROM cust_bill_pkg_tax_location + JOIN cust_main_county USING (taxnum) + WHERE cust_bill_pkg_tax_location.billpkgnum = cust_bill_pkg.billpkgnum + $has_taxname + )"; } elsif ( $cgi->param('country') ) { diff --git a/httemplate/search/report_tax-xls.cgi b/httemplate/search/report_tax-xls.cgi index 773b403f1..9b02457b0 100755 --- a/httemplate/search/report_tax-xls.cgi +++ b/httemplate/search/report_tax-xls.cgi @@ -221,11 +221,11 @@ foreach my $row (@rows) { } # at the end of everything -if ( $report->{outside} > 0 ) { +if ( $report->{out_sales} > 0 ) { my $f = $format[0]; $ws->set_row($y, 30); # height $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside}); - $ws->write($y, 1, $report->{outside}, $f->{currency_outside}); + $ws->write($y, 1, $report->{out_sales}, $f->{currency_outside}); $y++; } @@ -292,6 +292,15 @@ foreach my $row (@rows) { $prev_row = $row; } +if ( $report->{out_credit} > 0 ) { + my $f = $format[0]; + $ws->set_row($y, 30); # height + $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside}); + $ws->write($y, 1, $report->{out_credit}, $f->{currency_outside}); + $y++; +} + + for my $x (0..4) { $ws->set_column($x, $x, $widths[$x]); } diff --git a/httemplate/search/report_tax.cgi b/httemplate/search/report_tax.cgi index 9e625c80f..bbb3bc199 100644 --- a/httemplate/search/report_tax.cgi +++ b/httemplate/search/report_tax.cgi @@ -167,7 +167,7 @@ TD.rowhead { font-weight: bold; text-align: left; padding: 0px 3px } % } # foreach my $row % # at the end of everything -% if ( $report->{outside} > 0 ) { +% if ( $report->{out_sales} > 0 ) { @@ -175,7 +175,7 @@ TD.rowhead { font-weight: bold; text-align: left; padding: 0px 3px } - <% $money_sprintf->( $report->{outside } ) %> + <% $money_sprintf->( $report->{out_sales } ) %> @@ -253,7 +253,28 @@ TD.rowhead { font-weight: bold; text-align: left; padding: 0px 3px } % $rownum++; % $prev_row = $row; % } # foreach my $row -% # no "out of taxable region" for credits (yet) +% # "out of taxable region" for credits (there is a need for it) +% if ( $report->{out_credit} > 0 ) { +% my $creditlink = "cust_credit_bill_pkg.html?out=1;$dateagentlink"; +% if ( $params{'credit_date'} eq 'cust_credit_bill' ) { +% $creditlink =~ s/begin/credit_begin/; +% $creditlink =~ s/end/credit_end/; +% } +% $creditlink .= ";taxname=" . encode_entities($params{'taxname'}); + + + + <% emt('Out of taxable region') %> + + + + <% $money_sprintf->( $report->{out_credit } ) %> + + + + + +% }