'fields' => [
@pkgnum,
sub { $_[0]->pkgnum > 0
- # possibly use override.pkg but i think this correct
? $_[0]->get('pkgpart')
: ''
},
- sub { $_[0]->pkgnum > 0
- # possibly use override.pkg but i think this correct
- ? $_[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
@peritem,
'invnum',
'_date',
- #'pay_amount',
- #'credit_amount',
+ '', #'pay_amount',
+ '', #'credit_amount',
+ FS::UI::Web::cust_sort_fields(),
],
'links' => [
@pkgnum_null,
- classnum: Filter on package class.
+- report_optionnum: Filter on package report class. Can be a single report
+ class number or a comma-separated list (where 0 is "no report class"), or the
+ word "multiple".
+
- use_override: Apply "classnum" and "taxclass" filtering based on the
override (bundle) pkgpart, rather than always using the true pkgpart.
-- nottax: Limit to items that are not taxes (pkgnum > 0).
+- nottax: Limit to items that are not taxes (pkgnum > 0 or feepart > 0).
-- istax: Limit to items that are taxes (pkgnum == 0).
+- istax: Limit to items that are taxes (pkgnum == 0 and feepart = null).
- taxnum: Limit to items whose tax definition matches this taxnum.
With "nottax" that means items that are subject to that tax;
push @post_desc, 'taxclass';
push @post_desc_null, '';
$post_desc_align .= 'l';
- push @select, 'part_pkg.taxclass'; # or should this use override?
}
+# 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 USING (invnum)".
+ " LEFT JOIN cust_bill ON (cust_bill_pkg.invnum = cust_bill.invnum)".
# use cust_pkg.locationnum if it exists
FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
push @where, "sdate <= $ending",
"edate > $beginning",
;
-}
-else {
+} else {
push @where, "cust_bill._date >= $beginning",
"cust_bill._date <= $ending";
}
push @where, "cust_main.agentnum = $1";
}
+# salesnum--see below
# refnum
if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
push @where, "cust_main.refnum = $1";
}
-# cust_classnum
-if ( $cgi->param('cust_classnum') ) {
- my @classnums = grep /^\d+$/, $cgi->param('cust_classnum');
- push @where, 'cust_main.classnum IN('.join(',',@classnums).')'
+# cust_classnum (false laziness w/ elements/cust_main_dayranges.html, elements/cust_pay_or_refund.html, prepaid_income.html, cust_bill_pay.html, cust_bill_pkg_referral.html, unearned_detail.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
+if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
+ my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
+ push @where, 'COALESCE( cust_main.classnum, 0) IN ( '.
+ join(',', map { $_ || '0' } @classnums ).
+ ' )'
if @classnums;
}
+
# custnum
if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
push @where, "cust_main.custnum = $1";
# we want the package and its definition if available
my $join_pkg =
' LEFT JOIN cust_pkg USING (pkgnum)
- LEFT JOIN part_pkg USING (pkgpart)';
+ LEFT JOIN part_pkg USING (pkgpart)
+ LEFT JOIN part_fee USING (feepart)';
my $part_pkg = 'part_pkg';
-if ( $cgi->param('use_override') ) { #"Separate sub-packages from parents"
+# "Separate sub-packages from parents"
+my $use_override = $cgi->param('use_override') ? 1 : 0;
+if ( $use_override ) {
# still need the real part_pkg for tax applicability,
# so alias this one
$join_pkg .= " LEFT JOIN part_pkg AS override ON (
)";
$part_pkg = 'override';
}
-push @select, 'part_pkg.pkgpart', 'part_pkg.pkg'; # or should this use override?
+push @select, "$part_pkg.pkgpart", "$part_pkg.pkg";
+push @select, "COALESCE($part_pkg.taxclass, part_fee.taxclass) AS taxclass"
+ if $conf->exists('enable_taxclasses');
# the non-tax case
if ( $cgi->param('nottax') ) {
- push @where, 'cust_bill_pkg.pkgnum > 0';
+ push @select, "($itemdesc) AS itemdesc";
+
+ push @where,
+ '(cust_bill_pkg.pkgnum > 0 OR cust_bill_pkg.feepart IS NOT NULL)';
my @tax_where; # will go into a subquery
my @exempt_where; # will also go into a subquery
# not specified: all classes
# 0: empty class
# N: classnum
- if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
- push @where, "COALESCE($part_pkg.classnum, 0) = $1";
+ 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;
+ }
+
+ if ( grep { $_ eq 'report_optionnum' } $cgi->param ) {
+ my $num = join(',', grep /^[\d,]+$/, $cgi->param('report_optionnum'));
+ my $not_num = join(',', grep /^[\d,]+$/, $cgi->param('not_report_optionnum'));
+ my $all = $cgi->param('all_report_options') ? 1 : 0;
+ push @where, # code reuse FTW
+ FS::Report::Table->with_report_option(
+ report_optionnum => $num,
+ not_report_optionnum => $not_num,
+ use_override => $use_override,
+ all_report_options => $all,
+ );
}
# taxclass
# effective taxclass, not the real one
push @tax_where, 'cust_main_county.taxclass IS NULL'
} elsif ( $cgi->param('taxclass') ) {
- push @tax_where, "$part_pkg.taxclass IN (" .
+ push @tax_where, "COALESCE(part_fee.taxclass, $part_pkg.taxclass) IN (" .
join(', ', map {dbh->quote($_)} $cgi->param('taxclass') ).
')';
}
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,
}
}
- # itemdesc, for some reason
+ # 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)";
+ push @where, "($itemdesc = 'Tax' OR $itemdesc is null)";
} else {
- push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
+ push @where, "$itemdesc = ". dbh->quote($cgi->param('itemdesc'));
}
}
# 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') ) {
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";
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)";
}
push @select, 'cust_main.custnum', FS::UI::Web::cust_sql_fields();
+#salesnum
+if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) {
+
+ my $salesnum = $1;
+ my $sales = FS::sales->by_key($salesnum)
+ or die "salesnum $salesnum not found";
+
+ my $subsearch = $sales->cust_bill_pkg_search('', '',
+ 'cust_main_sales' => ($cgi->param('cust_main_sales') ? 1 : 0),
+ 'paid' => ($cgi->param('paid') ? 1 : 0),
+ 'classnum' => scalar($cgi->param('classnum'))
+ );
+ $join_pkg .= " JOIN sales_pkg_class ON ( COALESCE(sales_pkg_class.classnum, 0) = COALESCE( part_fee.classnum, part_pkg.classnum, 0) )";
+
+ my $extra_sql = $subsearch->{extra_sql};
+ $extra_sql =~ s/^WHERE//;
+ push @where, $extra_sql;
+
+ $cgi->param('classnum', 0) unless $cgi->param('classnum');
+}
+
+
my $where = join(' AND ', @where);
$where &&= "WHERE $where";