1 <& elements/search.html,
2 'title' => emt('Line items'),
3 'name' => emt('line items'),
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total',
7 $unearned ? ( $money_char. '%.2f unearned revenue' ) : (),
16 emt('Owed'), # useful in 'paid' mode?
18 : ( emt('Setup charge') )
20 ( $use_usage eq 'usage'
22 : emt('Recurring charge')
25 ? ( emt('Charge start'), emt('Charge end') )
32 FS::UI::Web::cust_header(),
36 sub { $_[0]->pkgnum > 0
37 # possibly use override.pkg but i think this correct
38 ? $_[0]->get('pkgpart')
41 sub { $_[0]->pkgnum > 0
42 # possibly use override.pkg but i think this correct
44 : $_[0]->get('itemdesc')
47 #strikethrough or "N/A ($amount)" or something these when
48 # they're not applicable to pkg_tax search
49 sub { my $cust_bill_pkg = shift;
52 sprintf($money_char.'%.2f',
53 $cust_bill_pkg->unearned_revenue)
56 sprintf($money_char.'%.2f', $cust_bill_pkg->setup );
60 ? ( $owed_sub, $payment_date_sub, )
63 sub { my $row = shift;
65 if ( $use_usage eq 'recurring' or $unearned ) {
66 $value = $row->recur - $row->usage;
67 } elsif ( $use_usage eq 'usage' ) {
72 sprintf($money_char.'%.2f', $value );
75 ? ( sub { time2str('%b %d %Y', shift->sdate ) },
76 # shift edate back a day
78 # (to avoid skipping a day during DST)
79 sub { time2str('%b %d %Y', shift->edate - 82799 ) },
84 sub { time2str('%b %d %Y', shift->_date ) },
85 sub { sprintf($money_char.'%.2f', shift->get('pay_amount')) },
86 sub { sprintf($money_char.'%.2f', shift->get('credit_amount')) },
87 \&FS::UI::Web::cust_fields,
94 'setup', #broken in $unearned case i guess
95 ( $unearned ? ('', '') : () ),
96 ( $use_usage eq 'recurring' or $unearned
102 ( $unearned ? ('sdate', 'edate') : () ),
114 ( $unearned ? ( '', '' ) : () ),
116 ( $unearned ? ( '', '' ) : () ),
121 ( map { $_ ne 'Cust. Status' ? $clink : '' }
122 FS::UI::Web::cust_header()
125 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
126 'align' => $pkgnum_align.
130 ( $unearned ? 'rc' : '' ).
132 ( $unearned ? 'cc' : '' ).
134 FS::UI::Web::cust_aligns(),
141 ( $unearned ? ( '', '' ) : () ),
143 ( $unearned ? ( '', '' ) : () ),
148 FS::UI::Web::cust_colors(),
156 ( $unearned ? ( '', '' ) : () ),
158 ( $unearned ? ( '', '' ) : () ),
163 FS::UI::Web::cust_styles(),
168 #LOTS of false laziness below w/cust_credit_bill_pkg.cgi
170 my $curuser = $FS::CurrentUser::CurrentUser;
172 die "access denied" unless $curuser->access_right('Financial reports');
174 my $conf = new FS::Conf;
177 my $unearned_mode = '';
178 my $unearned_base = '';
179 my $unearned_sql = '';
181 my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' );
183 my @pkgnum_header = ();
186 my $pkgnum_align = '';
187 if ( $curuser->option('show_pkgnum') ) {
188 push @select, 'cust_bill_pkg.pkgnum';
189 push @pkgnum_header, 'Pkg Num';
190 push @pkgnum, sub { $_[0]->pkgnum > 0 ? $_[0]->pkgnum : '' };
191 push @pkgnum_null, '';
192 $pkgnum_align .= 'r';
195 my @post_desc_header = ();
197 my @post_desc_null = ();
198 my $post_desc_align = '';
199 if ( $conf->exists('enable_taxclasses') ) {
200 push @post_desc_header, 'Tax class';
201 push @post_desc, 'taxclass';
202 push @post_desc_null, '';
203 $post_desc_align .= 'l';
204 push @select, 'part_pkg.taxclass'; # or should this use override?
207 #here is the agent virtualization
209 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
211 my @where = ( $agentnums_sql );
213 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
215 if ( $cgi->param('status') =~ /^([a-z]+)$/ ) {
216 push @where, FS::cust_main->cust_status_sql . " = '$1'";
219 if ( $cgi->param('distribute') == 1 ) {
220 push @where, "sdate <= $ending",
221 "edate > $beginning",
225 push @where, "cust_bill._date >= $beginning",
226 "cust_bill._date <= $ending";
229 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
230 push @where, "cust_main.agentnum = $1";
233 if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
234 push @where, "cust_main.refnum = $1";
238 if ( $cgi->param('cust_classnum') ) {
239 my @classnums = grep /^\d+$/, $cgi->param('cust_classnum');
240 push @where, 'cust_main.classnum IN('.join(',',@classnums).')'
245 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
246 push @where, "cust_main.custnum = $1";
250 # not specified: all classes
253 my $use_override = $cgi->param('use_override');
254 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
257 $comparison = "IS NULL";
259 $comparison = "= $1";
262 if ( $use_override ) {
264 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
265 override.classnum $comparison AND pkgpart_override IS NOT NULL
268 push @where, "part_pkg.classnum $comparison";
272 if ( $cgi->param('taxclass')
273 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
274 #(should we save a taxclass or a link to taxnum
275 # in cust_bill_pkg or something like
276 # cust_bill_pkg_tax_location?)
280 #override taxclass when use_override is specified? probably
281 #if ( $use_override ) {
284 # ' ( '. join(' OR ',
286 # ' ( part_pkg.taxclass = '. dbh->quote($_).
287 # ' AND pkgpart_override IS NULL '.
289 # ' override.taxclass = '. dbh->quote($_).
290 # ' AND pkgpart_override IS NOT NULL '.
293 # $cgi->param('taxclass')
299 push @where, ' part_pkg.taxclass IN ( '.
300 join(', ', map dbh->quote($_), $cgi->param('taxclass') ).
307 my @loc_param = qw( district city county state country );
309 if ( $cgi->param('out') ) {
311 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
312 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
313 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
316 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
317 if $cgi->param('istax');
321 SELECT COUNT(*) FROM cust_main_county
322 WHERE cust_main_county.tax > 0
327 #not linked to by anything, but useful for debugging "out of taxable region"
328 if ( grep $cgi->param($_), @loc_param ) {
330 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
332 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
333 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
334 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
337 push @where, $loc_sql;
341 } elsif ( $cgi->param('country') ) {
343 my @counties = $cgi->param('county');
345 if ( scalar(@counties) > 1 ) {
347 #hacky, could be more efficient. care if it is ever used for more than the
348 # tax-report_groups filtering kludge
351 ' ( '. join(' OR ', map {
353 my %ph = ( 'county' => dbh->quote($_),
354 map { $_ => dbh->quote( $cgi->param($_) ) }
355 qw( district city state country )
358 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
359 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
360 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
369 push @where, $locs_sql;
373 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
375 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
376 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
377 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
380 push @where, $loc_sql;
384 if ( $cgi->param('istax') ) {
385 if ( $cgi->param('taxname') ) {
386 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
387 #} elsif ( $cgi->param('taxnameNULL') {
389 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
391 } elsif ( $cgi->param('nottax') ) {
392 #what can we usefully do with "taxname" ???? look up a class???
394 #warn "neither nottax nor istax parameters specified";
397 if ( $cgi->param('taxclassNULL')
398 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
399 #(see comment above?)
402 my %hash = ( 'country' => scalar($cgi->param('country')) );
403 foreach (qw( state county )) {
404 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
406 my $cust_main_county = qsearchs('cust_main_county', \%hash);
407 die "unknown base region for empty taxclass" unless $cust_main_county;
409 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
410 $same_sql =~ s/taxclass/part_pkg.taxclass/g;
411 push @where, $same_sql if $same_sql;
415 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
417 push @where, FS::tax_rate_location->location_sql(
418 map { $_ => (scalar($cgi->param($_)) || '') }
419 qw( district city county state locationtaxid )
424 # unearned revenue mode
425 if ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
428 $unearned_mode = $cgi->param('mode');
430 push @where, "cust_bill_pkg.sdate < $unearned",
431 "cust_bill_pkg.edate > $unearned",
432 "cust_bill_pkg.recur != 0",
433 "part_pkg.freq != '0'";
435 if ( !$cgi->param('include_monthly') ) {
437 "part_pkg.freq != '1'",
438 "part_pkg.freq NOT LIKE '%h'",
439 "part_pkg.freq NOT LIKE '%d'",
440 "part_pkg.freq NOT LIKE '%w'";
443 my $usage_sql = FS::cust_bill_pkg->usage_sql;
444 push @select, "($usage_sql) AS usage"; # we need this
445 my $paid_sql = 'GREATEST(' .
446 FS::cust_bill_pkg->paid_sql($unearned, '', setuprecur => 'recur') .
449 push @select, "$paid_sql AS paid_no_usage"; # need this either way
451 if ( $unearned_mode eq 'paid' ) {
452 # then use the amount paid, minus usage charges
453 $unearned_base = $paid_sql;
456 # use the amount billed, minus usage charges and credits
457 $unearned_base = "GREATEST( cust_bill_pkg.recur - ".
458 FS::cust_bill_pkg->credited_sql($unearned, '', setuprecur => 'recur') .
460 # include only rows that have some non-usage, non-credited portion
462 # whatever we're using as the base, only show rows where it's positive
463 push @where, "$unearned_base > 0";
465 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS REAL)";
466 my $elapsed = "GREATEST( $unearned - cust_bill_pkg.sdate, 0 )";
467 my $remaining = "(1 - $elapsed/$period)";
469 $unearned_sql = "CAST( $unearned_base * $remaining AS DECIMAL(10,2) )";
470 push @select, "$unearned_sql AS unearned_revenue";
472 # last payment/credit date
473 my %t = (pay => 'cust_bill_pay', credit => 'cust_credit_bill');
474 foreach my $x (qw(pay credit)) {
476 my $link = $table.'_pkg';
477 my $pkey = dbdef->table($table)->primary_key;
478 my $last_date_sql = "SELECT MAX(_date)
479 FROM $table JOIN $link USING ($pkey)
480 WHERE $link.billpkgnum = cust_bill_pkg.billpkgnum
481 AND $table._date <= $unearned";
482 push @select, "($last_date_sql) AS last_$x";
487 if ( $cgi->param('itemdesc') ) {
488 if ( $cgi->param('itemdesc') eq 'Tax' ) {
489 push @where, "(itemdesc='Tax' OR itemdesc is null)";
491 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
495 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
496 my ( $group_op, $group_value ) = ( $1, $2 );
497 if ( $group_op eq '=' ) {
498 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
499 push @where, 'itemdesc = '. dbh->quote($group_value);
500 } elsif ( $group_op eq '!=' ) {
501 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
503 die "guru meditation #00de: group_op $group_op\n";
508 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
509 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
511 if ( $cgi->param('cust_tax') ) {
512 #false laziness -ish w/report_tax.cgi
514 if ( $cgi->param('taxname') ) {
515 my $q_taxname = dbh->quote($cgi->param('taxname'));
518 OR EXISTS ( SELECT 1 FROM cust_main_exemption
519 WHERE cust_main_exemption.custnum = cust_main.custnum
520 AND cust_main_exemption.taxname = $q_taxname )
524 $cust_exempt = " tax = 'Y' ";
527 push @where, $cust_exempt;
530 my $use_usage = $cgi->param('use_usage');
533 if ( $cgi->param('pkg_tax') ) {
538 ( CASE WHEN part_pkg.setuptax = 'Y'
539 THEN cust_bill_pkg.setup
544 ( CASE WHEN part_pkg.recurtax = 'Y'
545 THEN cust_bill_pkg.recur
552 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
553 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
554 "( tax != 'Y' OR tax IS NULL )";
556 } elsif ( $cgi->param('taxable') ) {
558 my $setup_taxable = "(
559 CASE WHEN part_pkg.setuptax = 'Y'
561 ELSE cust_bill_pkg.setup
565 my $recur_taxable = "(
566 CASE WHEN part_pkg.recurtax = 'Y'
568 ELSE cust_bill_pkg.recur
573 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
574 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
578 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
581 #not tax-exempt package (setup or recur)
583 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
584 AND cust_bill_pkg.setup > 0 )
586 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
587 AND cust_bill_pkg.recur > 0 )
589 #not a tax_exempt customer
590 "( tax != 'Y' OR tax IS NULL )";
591 #not covered in full by a monthly tax exemption (texas tax)
592 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
597 $count_query = "SELECT COUNT(*), ";
599 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
603 $count_query .= "SUM( $unearned_base ), SUM( $unearned_sql )";
604 } elsif ( $use_usage eq 'recurring' ) {
605 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur - usage)";
606 } elsif ( $use_usage eq 'usage' ) {
607 $count_query .= "SUM(usage)";
608 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
609 $count_query .= "SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
610 } elsif ( $cgi->param('iscredit') eq 'rate') {
611 $count_query .= "SUM( cust_credit_bill_pkg.amount )";
613 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
618 my $join_cust = ' JOIN cust_bill USING ( invnum )
619 LEFT JOIN cust_main USING ( custnum ) ';
621 # we want the package and its definition if available
623 ' LEFT JOIN cust_pkg USING (pkgnum)
624 LEFT JOIN part_pkg USING (pkgpart)';
626 my $part_pkg = 'part_pkg';
627 if ( $cgi->param('use_override') ) { #"Separate sub-packages from parents"
628 # still need the real part_pkg for tax applicability,
630 $join_pkg .= " LEFT JOIN part_pkg AS override ON (
631 COALESCE(cust_bill_pkg.pkgpart_override, cust_pkg.pkgpart, 0) = override.pkgpart
633 $part_pkg = 'override';
636 if ( $cgi->param('nottax') ) {
638 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
639 if $conf->exists('tax-pkg_address');
641 } elsif ( $cgi->param('istax') ) {
643 #false laziness w/report_tax.cgi $taxfromwhere
644 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
645 $cgi->param('iscredit') eq 'rate') {
648 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
649 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
651 } elsif ( $conf->exists('tax-pkg_address') ) {
654 LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
655 LEFT JOIN cust_location ON cust_bill_pkg_tax_location.locationnum
656 = cust_location.locationnum
659 #quelle kludge, somewhat false laziness w/report_tax.cgi
660 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
663 if ( $cgi->param('iscredit') ) {
664 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
665 if ( $cgi->param('iscredit') eq 'rate' ) {
666 $join_pkg .= ', billpkgtaxratelocationnum )';
667 } elsif ( $conf->exists('tax-pkg_address') ) {
668 $join_pkg .= ', billpkgtaxlocationnum )';
669 push @where, "billpkgtaxratelocationnum IS NULL";
672 push @where, "billpkgtaxratelocationnum IS NULL";
680 my $pay_sub = "SELECT SUM(cust_bill_pay_pkg.amount) AS pay_amount,
682 FROM cust_bill_pay_pkg
683 GROUP BY billpkgnum";
684 $join_pkg .= " LEFT JOIN ($pay_sub) AS item_pay USING (billpkgnum)";
685 push @select, 'item_pay.pay_amount';
688 my $credit_sub = "SELECT SUM(cust_credit_bill_pkg.amount) AS credit_amount,
690 FROM cust_credit_bill_pkg
691 GROUP BY billpkgnum";
692 $join_pkg .= " LEFT JOIN ($credit_sub) AS item_credit USING (billpkgnum)";
693 push @select, 'item_credit.credit_amount';
695 my $where = ' WHERE '. join(' AND ', @where);
699 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
700 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
701 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
702 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
705 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
708 push @select, 'part_pkg.pkgpart',
712 push @select, 'cust_main.custnum',
713 FS::UI::Web::cust_sql_fields();
716 'table' => 'cust_bill_pkg',
717 'addl_from' => "$join_cust $join_pkg",
719 'select' => join(",\n", @select ),
720 'extra_sql' => $where,
721 'order_by' => 'ORDER BY cust_bill._date, billpkgnum',
724 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
725 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
726 my $pay_link = ''; #[, 'billpkgnum', ];
727 my $credit_link = [ "${p}search/cust_credit_bill_pkg.html?billpkgnum=", 'billpkgnum', ];
729 my $conf = new FS::Conf;
730 my $money_char = $conf->config('money_char') || '$';
733 $money_char . shift->get('owed') # owed_recur is not correct here
735 my $payment_date_sub = sub {
736 #my $cust_bill_pkg = shift;
737 my @cust_pay = sort { $a->_date <=> $b->_date }
738 map $_->cust_bill_pay->cust_pay,
739 shift->cust_bill_pay_pkg('recur') #recur :/
741 time2str('%b %d %Y', $cust_pay[-1]->_date );
744 warn "\n\nQUERY:\n".Dumper($query)."\n\nCOUNT_QUERY:\n$count_query\n\n"
745 if $cgi->param('debug');