X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill_pkg.cgi;h=67f618cdbd6c00cb322ac1e337775a7ba7af5122;hb=bee4494248009b7b998391bc68c5d10a31220057;hp=440ab150cdb2a1deb7bbd565007dfea2ab3803e4;hpb=439d00a59c67a7d9d53b5d89c14ab332be16e38b;p=freeside.git diff --git a/httemplate/search/cust_bill_pkg.cgi b/httemplate/search/cust_bill_pkg.cgi index 440ab150c..67f618cdb 100644 --- a/httemplate/search/cust_bill_pkg.cgi +++ b/httemplate/search/cust_bill_pkg.cgi @@ -23,10 +23,7 @@ ? $_[0]->get('pkgpart') : '' }, - sub { $_[0]->pkgnum > 0 - ? $_[0]->get('pkg') - : $_[0]->get('itemdesc') - }, + 'itemdesc', # is part_pkg.pkg if applicable @post_desc, #strikethrough or "N/A ($amount)" or something these when # they're not applicable to pkg_tax search @@ -246,6 +243,9 @@ if ( $conf->exists('enable_taxclasses') ) { $post_desc_align .= 'l'; } +# used in several places +my $itemdesc = 'COALESCE(part_fee.itemdesc, part_pkg.pkg, cust_bill_pkg.itemdesc)'; + # valid in both the tax and non-tax cases my $join_cust = " LEFT JOIN cust_bill ON (cust_bill_pkg.invnum = cust_bill.invnum)". @@ -265,8 +265,7 @@ if ( $cgi->param('distribute') == 1 ) { push @where, "sdate <= $ending", "edate > $beginning", ; -} -else { +} else { push @where, "cust_bill._date >= $beginning", "cust_bill._date <= $ending"; } @@ -326,7 +325,7 @@ push @select, "COALESCE($part_pkg.taxclass, part_fee.taxclass) AS taxclass" # the non-tax case if ( $cgi->param('nottax') ) { - push @select, "part_fee.itemdesc"; + push @select, "($itemdesc) AS itemdesc"; push @where, '(cust_bill_pkg.pkgnum > 0 OR cust_bill_pkg.feepart IS NOT NULL)'; @@ -423,63 +422,88 @@ if ( $cgi->param('nottax') ) { # If we're showing 'out' (items that aren't region/class taxable), # then we need the set of all items minus the union of those. - my $exempt_sub; + + if ( $cgi->param('out') ) { + # separate from the rest, in that we're not going to join cust_main_county + # in the outer query + + my @exclude = ( 'cust_tax_exempt_pkg.billpkgnum', + 'cust_bill_pkg_tax_location.taxable_billpkgnum' + ); + foreach my $col (@exclude) { + my ($table) = split(/\./, $col); + my $this_where = 'WHERE ' . join(' AND ', + "$col = cust_bill_pkg.billpkgnum", + @tax_where + ); - if ( @exempt_where or @tax_where - or $cgi->param('taxable') or $cgi->param('out') ) - { - # process exemption restrictions, including @tax_where - my $exempt_sub = 'SELECT SUM(amount) as exempt_amount, billpkgnum - FROM cust_tax_exempt_pkg JOIN cust_main_county USING (taxnum)'; + push @where, + "NOT EXISTS(SELECT 1 FROM $table + JOIN cust_main_county USING (taxnum) + $this_where + )"; + } + + } else { + # everything that returns things joined to a tax definition - $exempt_sub .= ' WHERE '.join(' AND ', @tax_where, @exempt_where) - if (@tax_where or @exempt_where); + my $exempt_sub; + if ( @exempt_where or @tax_where or $cgi->param('taxable') ) { - $exempt_sub .= ' GROUP BY billpkgnum'; + # process exemption restrictions, including @tax_where + my $exempt_sub = 'SELECT SUM(amount) as exempt_amount, billpkgnum + FROM cust_tax_exempt_pkg JOIN cust_main_county USING (taxnum)'; - $join_pkg .= " LEFT JOIN ($exempt_sub) AS item_exempt - USING (billpkgnum)"; - - # process tax restrictions - unshift @tax_where, - 'cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum', - 'cust_main_county.tax > 0'; - } + $exempt_sub .= ' WHERE '.join(' AND ', @tax_where, @exempt_where) + if (@tax_where or @exempt_where); - my $tax_sub = "SELECT 1 - FROM cust_bill_pkg_tax_location - JOIN cust_bill_pkg AS tax_item USING (billpkgnum) - JOIN cust_main_county USING (taxnum) - WHERE ". join(' AND ', @tax_where); + $exempt_sub .= ' GROUP BY billpkgnum'; - # now do something with that - if ( @exempt_where ) { + $join_pkg .= " LEFT JOIN ($exempt_sub) AS item_exempt + USING (billpkgnum)"; + + # process tax restrictions + unshift @tax_where, + 'cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum', + 'cust_main_county.tax > 0'; + } - push @where, 'item_exempt.billpkgnum IS NOT NULL'; - push @select, 'item_exempt.exempt_amount'; - push @peritem, 'exempt_amount'; - push @peritem_desc, 'Exempt'; - push @total, 'SUM(exempt_amount)'; - push @total_desc, "$money_char%.2f tax-exempt"; + my $tax_sub = "SELECT 1 + FROM cust_bill_pkg_tax_location + JOIN cust_bill_pkg AS tax_item USING (billpkgnum) + JOIN cust_main_county USING (taxnum) + WHERE ". join(' AND ', @tax_where); - } elsif ( $cgi->param('taxable') ) { + # now do something with that + if ( @exempt_where ) { - my $taxable = 'cust_bill_pkg.setup + cust_bill_pkg.recur '. - '- COALESCE(item_exempt.exempt_amount, 0)'; + push @where, 'item_exempt.billpkgnum IS NOT NULL'; + push @select, 'item_exempt.exempt_amount'; + push @peritem, 'exempt_amount'; + push @peritem_desc, 'Exempt'; + push @total, 'SUM(exempt_amount)'; + push @total_desc, "$money_char%.2f tax-exempt"; - push @select, "($taxable) AS taxable_amount"; - push @where, "EXISTS($tax_sub)"; - push @peritem, 'taxable_amount'; - push @peritem_desc, 'Taxable'; - push @total, "SUM($taxable)"; - push @total_desc, "$money_char%.2f taxable"; + } elsif ( $cgi->param('taxable') ) { - } elsif ( @tax_where ) { + my $taxable = 'cust_bill_pkg.setup + cust_bill_pkg.recur '. + '- COALESCE(item_exempt.exempt_amount, 0)'; - # union of taxable + all exempt_ cases - push @where, "(EXISTS($tax_sub) OR item_exempt.billpkgnum IS NOT NULL)"; + push @select, "($taxable) AS taxable_amount"; + push @where, "EXISTS($tax_sub)"; + push @peritem, 'taxable_amount'; + push @peritem_desc, 'Taxable'; + push @total, "SUM($taxable)"; + push @total_desc, "$money_char%.2f taxable"; - } + } elsif ( @tax_where ) { + + # union of taxable + all exempt_ cases + push @where, "(EXISTS($tax_sub) OR item_exempt.billpkgnum IS NOT NULL)"; + + } + + } # handle all joins to cust_main_county # recur/usage separation if ( $cgi->param('usage') eq 'recurring' ) { @@ -509,32 +533,23 @@ if ( $cgi->param('nottax') ) { push @where, 'cust_bill_pkg.pkgnum = 0'; # tax location when using tax_rate_location - if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) { + if ( $cgi->param('vendortax') ) { $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '. ' LEFT JOIN tax_rate_location USING ( taxratelocationnum )'; - push @where, FS::tax_rate_location->location_sql( - map { $_ => (scalar($cgi->param($_)) || '') } - qw( district city county state locationtaxid ) - ); + foreach (qw( state county city locationtaxid)) { + if ( scalar($cgi->param($_)) ) { + my $place = dbh->quote( $cgi->param($_) ); + push @where, "tax_rate_location.$_ = $place"; + } + } $total[1] = 'SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur) )'; - } elsif ( $cgi->param('out') ) { - - $join_pkg .= ' - LEFT JOIN cust_bill_pkg_tax_location USING (billpkgnum) - '; - push @where, 'cust_bill_pkg_tax_location.billpkgnum IS NULL'; - - # each billpkgnum should appear only once - $total[0] = 'COUNT(*)'; - $total[1] = 'SUM(cust_bill_pkg.setup)'; - - } else { # not locationtaxid or 'out'--the normal case + } else { # the internal-tax case $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING (billpkgnum) @@ -559,35 +574,36 @@ if ( $cgi->param('nottax') ) { ' )' if @classnums; } - } - # taxclass - if ( $cgi->param('taxclassNULL') ) { - push @where, 'cust_main_county.taxclass IS NULL'; - } + # taxclass + if ( $cgi->param('taxclassNULL') ) { + push @where, 'cust_main_county.taxclass IS NULL'; + } - # taxname - if ( $cgi->param('taxnameNULL') ) { - push @where, 'cust_main_county.taxname IS NULL OR '. - 'cust_main_county.taxname = \'Tax\''; - } elsif ( $cgi->param('taxname') ) { - push @where, 'cust_main_county.taxname = '. - dbh->quote($cgi->param('taxname')); - } + # taxname + if ( $cgi->param('taxnameNULL') ) { + push @where, 'cust_main_county.taxname IS NULL OR '. + 'cust_main_county.taxname = \'Tax\''; + } elsif ( $cgi->param('taxname') ) { + push @where, 'cust_main_county.taxname = '. + dbh->quote($cgi->param('taxname')); + } - # specific taxnums - if ( $cgi->param('taxnum') =~ /^([\d,]+)$/) { - push @where, "cust_main_county.taxnum IN ($1)"; - } + # itemdesc, for breakdown from the vendor tax report + if ( $cgi->param('itemdesc') ) { + if ( $cgi->param('itemdesc') eq 'Tax' ) { + push @where, "($itemdesc = 'Tax' OR $itemdesc is null)"; + } else { + push @where, "$itemdesc = ". dbh->quote($cgi->param('itemdesc')); + } + } - # itemdesc, for some reason - if ( $cgi->param('itemdesc') ) { - if ( $cgi->param('itemdesc') eq 'Tax' ) { - push @where, "(itemdesc='Tax' OR itemdesc is null)"; - } else { - push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc')); + # specific taxnums + if ( $cgi->param('taxnum') =~ /^([\d,]+)$/) { + push @where, "cust_main_county.taxnum IN ($1)"; } - } + + } #end of "normal case" } # nottax / istax @@ -603,6 +619,12 @@ push @select, "($pay_sub) AS pay_amount"; # credit if ( $cgi->param('credit') ) { + my $credit_where; + + my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'credit'); + $credit_where = "WHERE cust_credit_bill._date >= $cr_begin " . + "AND cust_credit_bill._date <= $cr_end"; + my $credit_sub; if ( $cgi->param('istax') ) { @@ -616,6 +638,7 @@ if ( $cgi->param('credit') ) { JOIN cust_credit USING (crednum) LEFT JOIN reason USING (reasonnum) LEFT JOIN access_user USING (usernum) + $credit_where GROUP BY billpkgnum, billpkgtaxlocationnum, reason.reason, access_user.username"; @@ -646,6 +669,7 @@ if ( $cgi->param('credit') ) { JOIN cust_credit USING (crednum) LEFT JOIN reason USING (reasonnum) LEFT JOIN access_user USING (usernum) + $credit_where GROUP BY billpkgnum, reason.reason, access_user.username"; $join_pkg .= " LEFT JOIN ($credit_sub) AS item_credit USING (billpkgnum)"; }