X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill_pkg.cgi;h=d86641de392a9c451722e18e52cc16875af130e4;hb=ac01b85bf09d058d3cda097dffbf4e8fce5f4305;hp=34c89e53271fc120ec78de2cabd737e3591a3951;hpb=96783bdc58be6e4f2fc56d516a9ceba57af00ba8;p=freeside.git diff --git a/httemplate/search/cust_bill_pkg.cgi b/httemplate/search/cust_bill_pkg.cgi index 34c89e532..d86641de3 100644 --- a/httemplate/search/cust_bill_pkg.cgi +++ b/httemplate/search/cust_bill_pkg.cgi @@ -5,9 +5,12 @@ 'count_query' => $count_query, 'count_addl' => \@total_desc, 'header' => [ + @pkgnum_header, + emt('Pkg Def'), emt('Description'), @post_desc_header, @peritem_desc, + @currency_desc, emt('Invoice'), emt('Date'), emt('Paid'), @@ -15,14 +18,22 @@ FS::UI::Web::cust_header(), ], 'fields' => [ + @pkgnum, sub { $_[0]->pkgnum > 0 - ? $_[0]->get('pkg') # possibly use override.pkg - : $_[0]->get('itemdesc') # but i think this correct + # 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') }, @post_desc, #strikethrough or "N/A ($amount)" or something these when # they're not applicable to pkg_tax search @peritem_sub, + @currency_sub, 'invnum', sub { time2str('%b %d %Y', shift->_date ) }, sub { sprintf($money_char.'%.2f', shift->get('pay_amount')) }, @@ -30,19 +41,24 @@ \&FS::UI::Web::cust_fields, ], 'sort_fields' => [ + @pkgnum_null, + '', '', @post_desc_null, @peritem, + @currency, 'invnum', '_date', #'pay_amount', #'credit_amount', ], 'links' => [ - #'', + @pkgnum_null, + '', '', @post_desc_null, @peritem_null, + @currency_null, $ilink, $ilink, $pay_link, @@ -52,16 +68,20 @@ ), ], #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(), - 'align' => 'l'. + 'align' => $pkgnum_align. + 'rl'. $post_desc_align. $peritem_align. + $currency_align. 'rcrr'. FS::UI::Web::cust_aligns(), 'color' => [ - #'', + @pkgnum_null, + '', '', @post_desc_null, @peritem_null, + @currency_null, '', '', '', @@ -69,10 +89,12 @@ FS::UI::Web::cust_colors(), ], 'style' => [ - #'', + @pkgnum_null, + '', '', @post_desc_null, @peritem_null, + @currency_null, '', '', '', @@ -105,8 +127,14 @@ Filtering parameters: - refnum: Filter on customer reference source. +- cust_classnum: Filter on customer class. + - 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. @@ -116,8 +144,8 @@ Filtering parameters: - taxnum: Limit to items whose tax definition matches this taxnum. With "nottax" that means items that are subject to that tax; - with "istax" it's the tax charges themselves. Can be specified - more than once to include multiple taxes. + with "istax" it's the tax charges themselves. Can be a comma-separated + list to include multiple taxes. - country, state, county, city: Limit to items whose tax location matches these fields. If "nottax" it's the tax location of the package; @@ -165,19 +193,49 @@ Filtering parameters: <%init> -die "access denied" - unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); +my $curuser = $FS::CurrentUser::CurrentUser; + +die "access denied" unless $curuser->access_right('Financial reports'); my $conf = new FS::Conf; my $money_char = $conf->config('money_char') || '$'; my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' ); my @total = ( 'COUNT(*)', 'SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)'); -my @total_desc = ( '%d line items', $money_char.'%.2f total' ); # sprintf strings +my @total_desc = ( $money_char.'%.2f total' ); # sprintf strings my @peritem = ( 'setup', 'recur' ); my @peritem_desc = ( 'Setup charge', 'Recurring charge' ); +my @currency_desc = (); +my @currency_sub = (); +my @currency = (); +if ( $conf->config('currencies') ) { + @currency_desc = ( 'Setup billed', 'Recurring billed' ); + @currency_sub = ( + map { + my $what = $_; + sub { my $currency = $_[0]->get($what.'_billed_currency') or return ''; + $currency. ' '. currency_symbol($currency, SYM_HTML). + $_[0]->get($what.'_billed_amount'); + }; + } qw( setup recur ) + ); + @currency = ( 'setup_billed_amount', 'recur_billed_amount' ); #for sorting +} + +my @pkgnum_header = (); +my @pkgnum = (); +my @pkgnum_null; +my $pkgnum_align = ''; +if ( $curuser->option('show_pkgnum') ) { + push @select, 'cust_bill_pkg.pkgnum'; + push @pkgnum_header, 'Pkg Num'; + push @pkgnum, sub { $_[0]->pkgnum > 0 ? $_[0]->pkgnum : '' }; + push @pkgnum_null, ''; + $pkgnum_align .= 'r'; +} + my @post_desc_header = (); my @post_desc = (); my @post_desc_null = (); @@ -192,9 +250,9 @@ if ( $conf->exists('enable_taxclasses') ) { # valid in both the tax and non-tax cases my $join_cust = - " LEFT JOIN cust_bill USING (invnum) - LEFT JOIN cust_main USING (custnum) - "; + " 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'); #agent virtualization my $agentnums_sql = @@ -225,11 +283,22 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) { push @where, "cust_main.agentnum = $1"; } +# salesnum--see below # refnum if ( $cgi->param('refnum') =~ /^(\d+)$/ ) { push @where, "cust_main.refnum = $1"; } +# 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"; @@ -241,15 +310,15 @@ my $join_pkg = LEFT JOIN part_pkg USING (pkgpart)'; my $part_pkg = 'part_pkg'; -if ( $cgi->param('use_override') ) { +if ( $cgi->param('use_override') ) { #"Separate sub-packages from parents" # still need the real part_pkg for tax applicability, # so alias this one $join_pkg .= " LEFT JOIN part_pkg AS override ON ( - COALESCE(cust_bill_pkg.pkgpart_override, cust_pkg.pkgpart, 0) = part_pkg.pkgpart + COALESCE(cust_bill_pkg.pkgpart_override, cust_pkg.pkgpart, 0) = override.pkgpart )"; $part_pkg = 'override'; } -push @select, 'part_pkg.pkg'; # or should this use override? +push @select, 'part_pkg.pkgpart', 'part_pkg.pkg'; # or should this use override? # the non-tax case if ( $cgi->param('nottax') ) { @@ -263,8 +332,23 @@ if ( $cgi->param('nottax') ) { # 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_pkg.classnum, 0) IN ( ". + join(',', @classnums ). + ' )' + if @classnums; + } + + if ( grep { $_ eq 'report_optionnum' } $cgi->param ) { + my @nums = grep /^\w+$/, $cgi->param('report_optionnum'); + my $num = join(',', @nums); + push @where, # code reuse FTW + FS::Report::Table->with_report_option( $num, $cgi->param('use_override')); + } + + if ( $cgi->param('report_optionnum') =~ /^(\w+)$/ ) { + ; } # taxclass @@ -289,11 +373,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) @@ -321,12 +402,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') =~ /^([\d,]+)$/) { + push @tax_where, "cust_main_county.taxnum IN ($1)"; } # If we're showing exempt items, we need to find those with @@ -356,22 +433,16 @@ if ( $cgi->param('nottax') ) { USING (billpkgnum)"; } - if ( @tax_where or $cgi->param('taxable') or $cgi->param('out') ) { - # process tax restrictions - unshift @tax_where, - 'cust_main_county.tax > 0'; + # process tax restrictions + unshift @tax_where, + 'cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum', + 'cust_main_county.tax > 0'; - my $tax_sub = "SELECT invnum, cust_bill_pkg_tax_location.pkgnum + 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). - " 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)"; - } + WHERE ". join(' AND ', @tax_where); # now do something with that if ( @exempt_where ) { @@ -388,23 +459,17 @@ if ( $cgi->param('nottax') ) { my $taxable = 'cust_bill_pkg.setup + cust_bill_pkg.recur '. '- COALESCE(item_exempt.exempt_amount, 0)'; - push @where, 'item_tax.invnum 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 ( $cgi->param('out') ) { - - push @where, 'item_tax.invnum IS NULL', - 'item_exempt.billpkgnum IS NULL'; - } elsif ( @tax_where ) { # union of taxable + all exempt_ cases - push @where, - '(item_tax.invnum IS NOT NULL OR item_exempt.billpkgnum IS NOT NULL)'; + push @where, "(EXISTS($tax_sub) OR item_exempt.billpkgnum IS NOT NULL)"; } @@ -415,7 +480,7 @@ if ( $cgi->param('nottax') ) { push @select, "($recur_no_usage) AS recur_no_usage"; $peritem[1] = 'recur_no_usage'; $total[1] = "SUM(cust_bill_pkg.setup + $recur_no_usage)"; - $total_desc[1] .= ' (excluding usage)'; + $total_desc[0] .= ' (excluding usage)'; } elsif ( $cgi->param('usage') eq 'usage' ) { @@ -425,7 +490,7 @@ if ( $cgi->param('nottax') ) { $peritem[1] = '_usage'; $peritem_desc[1] = 'Usage charge'; $total[1] = "SUM($usage)"; - $total_desc[1] .= ' usage charges'; + $total_desc[0] .= ' usage charges'; } } elsif ( $cgi->param('istax') ) { @@ -452,7 +517,7 @@ if ( $cgi->param('nottax') ) { } elsif ( $cgi->param('out') ) { - $join_pkg = ' + $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING (billpkgnum) '; push @where, 'cust_bill_pkg_tax_location.billpkgnum IS NULL'; @@ -463,7 +528,7 @@ if ( $cgi->param('nottax') ) { } else { # not locationtaxid or 'out'--the normal case - $join_pkg = ' + $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING (billpkgnum) JOIN cust_main_county USING (taxnum) '; @@ -471,6 +536,21 @@ 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)'; + + # package classnum + if ( grep { $_ eq 'classnum' } $cgi->param ) { + my @classnums = grep /^\d*$/, $cgi->param('classnum'); + $join_pkg .= ' + JOIN cust_pkg AS taxed_pkg + ON (cust_bill_pkg_tax_location.pkgnum = taxed_pkg.pkgnum) + JOIN part_pkg AS taxed_part_pkg + ON (taxed_pkg.pkgpart = taxed_part_pkg.pkgpart) + '; + push @where, "COALESCE(taxed_part_pkg.classnum, 0) IN ( ". + join(',', @classnums ). + ' )' + if @classnums; + } } # taxclass @@ -488,25 +568,8 @@ if ( $cgi->param('nottax') ) { } # 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; - } - - # report group (itemdesc) - if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ ) { - my ( $group_op, $group_value ) = ( $1, $2 ); - if ( $group_op eq '=' ) { - #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%'); - push @where, 'itemdesc = '. dbh->quote($group_value); - } elsif ( $group_op eq '!=' ) { - push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )'; - } else { - die "guru meditation #00de: group_op $group_op\n"; - } + if ( $cgi->param('taxnum') =~ /^([\d,]+)$/) { + push @where, "cust_main_county.taxnum IN ($1)"; } # itemdesc, for some reason @@ -522,12 +585,11 @@ if ( $cgi->param('nottax') ) { #total payments -my $pay_sub = "SELECT SUM(cust_bill_pay_pkg.amount) AS pay_amount, - billpkgnum - FROM cust_bill_pay_pkg - GROUP BY billpkgnum"; -$join_pkg .= " LEFT JOIN ($pay_sub) AS item_pay USING (billpkgnum)"; -push @select, 'item_pay.pay_amount'; +my $pay_sub = "SELECT SUM(cust_bill_pay_pkg.amount) + FROM cust_bill_pay_pkg + WHERE cust_bill_pkg.billpkgnum = cust_bill_pay_pkg.billpkgnum + "; +push @select, "($pay_sub) AS pay_amount"; # credit @@ -593,24 +655,45 @@ if ( $cgi->param('credit') ) { #still want a credit total column - my $credit_sub = "SELECT SUM(cust_credit_bill_pkg.amount) AS credit_amount, - billpkgnum - FROM cust_credit_bill_pkg - GROUP BY billpkgnum"; - $join_pkg .= " LEFT JOIN ($credit_sub) AS item_credit USING (billpkgnum)"; - - push @select, 'item_credit.credit_amount'; + my $credit_sub = " + SELECT SUM(cust_credit_bill_pkg.amount) + FROM cust_credit_bill_pkg + WHERE cust_bill_pkg.billpkgnum = cust_credit_bill_pkg.billpkgnum + "; + push @select, "($credit_sub) AS credit_amount"; } 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_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"; my $query = { 'table' => 'cust_bill_pkg', - 'addl_from' => "$join_cust $join_pkg", + 'addl_from' => "$join_pkg $join_cust", 'hashref' => {}, 'select' => join(",\n", @select ), 'extra_sql' => $where, @@ -619,11 +702,9 @@ my $query = { my $count_query = 'SELECT ' . join(',', @total) . - " FROM cust_bill_pkg $join_cust $join_pkg + " FROM cust_bill_pkg $join_pkg $join_cust $where"; -shift @total_desc; #the first one is implicit - @peritem_desc = map {emt($_)} @peritem_desc; my @peritem_sub = map { my $field = $_; @@ -636,6 +717,10 @@ my @peritem_sub = map { my @peritem_null = map { '' } @peritem; # placeholders my $peritem_align = 'r' x scalar(@peritem); +@currency_desc = map {emt($_)} @currency_desc; +my @currency_null = map { '' } @currency; # placeholders +my $currency_align = 'r' x scalar(@currency); + my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ]; my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ]; @@ -644,4 +729,5 @@ my $credit_link = [ "${p}search/cust_credit_bill_pkg.html?billpkgnum=", 'billpkg warn "\n\nQUERY:\n".Dumper($query)."\n\nCOUNT_QUERY:\n$count_query\n\n" if $cgi->param('debug'); +