X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill_pkg.cgi;h=f4febdbf2fa415fa8dbba3b1823fb992c7823de9;hb=dde41fbab51d693dda80977a724ee722059d92f7;hp=6cda34a7fa72a250ce5e978537f935bb2405a3f4;hpb=16055ddbd7cf41399df4a24a337584225c34c91b;p=freeside.git diff --git a/httemplate/search/cust_bill_pkg.cgi b/httemplate/search/cust_bill_pkg.cgi index 6cda34a7f..f4febdbf2 100644 --- a/httemplate/search/cust_bill_pkg.cgi +++ b/httemplate/search/cust_bill_pkg.cgi @@ -241,8 +241,7 @@ if ( $cgi->param('distribute') == 1 ) { push @where, "sdate <= $ending", "edate > $beginning", ; -} -else { +} else { push @where, "cust_bill._date >= $beginning", "cust_bill._date <= $ending"; } @@ -357,11 +356,8 @@ if ( $cgi->param('nottax') ) { # we don't handle exempt_monthly here if ( $cgi->param('taxname') ) { # specific taxname - push @tax_where, 'cust_main_county.taxname = '. + push @tax_where, 'COALESCE(cust_main_county.taxname, \'Tax\') = '. dbh->quote($cgi->param('taxname')); - } elsif ( $cgi->param('taxnameNULL') ) { - push @tax_where, 'cust_main_county.taxname IS NULL OR '. - 'cust_main_county.taxname = \'Tax\''; } # country:state:county:city:district (may be repeated) @@ -389,12 +385,8 @@ if ( $cgi->param('nottax') ) { } # specific taxnums - if ( $cgi->param('taxnum') ) { - my $taxnum_in = join(',', - grep /^\d+$/, $cgi->param('taxnum') - ); - push @tax_where, "cust_main_county.taxnum IN ($taxnum_in)" - if $taxnum_in; + if ( $cgi->param('taxnum') =~ /^([0-9,]+)$/ ) { + push @tax_where, "cust_main_county.taxnum IN ($1)"; } # If we're showing exempt items, we need to find those with @@ -406,75 +398,97 @@ 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; + # Always exclude cust_tax_exempt_pkg records with non-NULL creditbillpkgnum. - 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)'; + if ( $cgi->param('out') ) { + # separate from the rest, in that we're not going to join cust_main_county + # in the outer query - $exempt_sub .= ' WHERE '.join(' AND ', @tax_where, @exempt_where) - if (@tax_where or @exempt_where); + 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 + ); - $exempt_sub .= ' GROUP BY billpkgnum'; + 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 - $join_pkg .= " LEFT JOIN ($exempt_sub) AS item_exempt - USING (billpkgnum)"; - } - - if ( @tax_where or $cgi->param('taxable') or $cgi->param('out') ) { - # process tax restrictions - unshift @tax_where, - 'cust_main_county.tax > 0'; - - my $tax_sub = "SELECT invnum, cust_bill_pkg_tax_location.pkgnum - 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). - " GROUP BY invnum, cust_bill_pkg_tax_location.pkgnum"; - - $join_pkg .= " LEFT JOIN ($tax_sub) AS item_tax - ON (item_tax.invnum = cust_bill_pkg.invnum AND - item_tax.pkgnum = cust_bill_pkg.pkgnum)"; - } + my $exempt_sub; + if ( @exempt_where or @tax_where or $cgi->param('taxable') ) { - # now do something with that - if ( @exempt_where ) { + push @exempt_where, "cust_tax_exempt_pkg.creditbillpkgnum IS NULL"; - 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"; + # 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)'; - } elsif ( $cgi->param('taxable') ) { + $exempt_sub .= ' WHERE '.join(' AND ', @tax_where, @exempt_where); - my $taxable = 'cust_bill_pkg.setup + cust_bill_pkg.recur '. - '- COALESCE(item_exempt.exempt_amount, 0)'; + $exempt_sub .= ' GROUP BY billpkgnum'; - push @where, 'item_tax.invnum IS NOT NULL'; - push @select, "($taxable) AS taxable_amount"; - push @peritem, 'taxable_amount'; - push @peritem_desc, 'Taxable'; - push @total, "SUM($taxable)"; - push @total_desc, "$money_char%.2f taxable"; + $join_pkg .= " LEFT JOIN ($exempt_sub) AS item_exempt + USING (billpkgnum)"; + } + + if ( @tax_where or $cgi->param('taxable') ) { + # process tax restrictions + unshift @tax_where, + 'cust_main_county.tax > 0'; + + my $tax_sub = "SELECT cust_bill_pkg_tax_location.taxable_billpkgnum + 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). + " GROUP BY taxable_billpkgnum"; + + $join_pkg .= " LEFT JOIN ($tax_sub) AS item_tax + ON (item_tax.taxable_billpkgnum = cust_bill_pkg.billpkgnum) + "; + } - } elsif ( $cgi->param('out') ) { - - push @where, 'item_tax.invnum IS NULL', - 'item_exempt.billpkgnum IS NULL'; + # now do something with that + if ( $cgi->param('taxable') ) { + # taxable query: needs sale amount - exempt amount - } 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, - '(item_tax.invnum IS NOT NULL OR item_exempt.billpkgnum IS NOT NULL)'; + push @where, 'item_tax.taxable_billpkgnum IS NOT NULL'; + push @select, "($taxable) AS taxable_amount"; + push @peritem, 'taxable_amount'; + push @peritem_desc, 'Taxable'; + push @total, "SUM($taxable)"; + push @total_desc, "$money_char%.2f taxable"; - } + } elsif ( $cgi->param('exempt_cust') or $cgi->param('exempt_pkg') ) { + + 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"; + + } elsif ( @tax_where ) { + # union of taxable + all exempt_ cases + push @where, + '(item_tax.taxable_billpkgnum IS NOT NULL OR item_exempt.billpkgnum IS NOT NULL)'; + + } + + } # handle all joins to cust_main_county # recur/usage separation if ( $cgi->param('usage') eq 'recurring' ) { @@ -520,18 +534,7 @@ if ( $cgi->param('nottax') ) { 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) @@ -541,30 +544,26 @@ if ( $cgi->param('nottax') ) { # don't double-count the components of consolidated taxes $total[0] = 'COUNT(DISTINCT cust_bill_pkg.billpkgnum)'; $total[1] = 'SUM(cust_bill_pkg_tax_location.amount)'; - } - # 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') ) { - my $taxnum_in = join(',', - grep /^\d+$/, $cgi->param('taxnum') - ); - push @where, "cust_main_county.taxnum IN ($taxnum_in)" - if $taxnum_in; - } + # specific taxnums + if ( $cgi->param('taxnum') =~ /^([0-9,]+)$/ ) { + push @where, "cust_main_county.taxnum IN ($1)"; + } + } # the normal case # report group (itemdesc) if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ ) { @@ -588,6 +587,18 @@ if ( $cgi->param('nottax') ) { } } + # classnum (of underlying package) + # not specified: all classes + # 0: empty class + # N: classnum + if ( grep { $_ eq 'classnum' } $cgi->param ) { + my @classnums = grep /^\d+$/, $cgi->param('classnum'); + push @where, "COALESCE(part_fee.classnum, $part_pkg.classnum, 0) IN ( ". + join(',', @classnums ). + ' )' + if @classnums; + } + } # nottax / istax @@ -602,6 +613,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') ) { @@ -615,6 +632,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"; @@ -645,6 +663,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)"; }