1 <% include( 'elements/search.html',
2 'title' => 'Line items',
3 'name' => 'line items',
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total',
7 $unearned ? ( $money_char. '%.2f unearned revenue' ) : (),
13 ? ( 'Unearned', 'Owed', 'Payment date' )
16 ( $use_usage eq 'usage'
21 ? ( 'Charge start', 'Charge end' )
26 FS::UI::Web::cust_header(),
30 sub { $_[0]->pkgnum > 0
31 ? $_[0]->get('pkg') # possibly use override.pkg
32 : $_[0]->get('itemdesc') # but i think this correct
34 #strikethrough or "N/A ($amount)" or something these when
35 # they're not applicable to pkg_tax search
36 sub { my $cust_bill_pkg = shift;
39 $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
40 my $elapsed = $unearned - $cust_bill_pkg->sdate;
41 $elapsed = 0 if $elapsed < 0;
43 my $remaining = 1 - $elapsed/$period;
45 sprintf($money_char. '%.2f',
46 $remaining * $cust_bill_pkg->recur );
49 sprintf($money_char.'%.2f', $cust_bill_pkg->setup );
53 ? ( $owed_sub, $payment_date_sub, )
56 sub { my $row = shift;
58 if ( $use_usage eq 'recurring' ) {
59 $value = $row->recur - $row->usage;
60 } elsif ( $use_usage eq 'usage' ) {
65 sprintf($money_char.'%.2f', $value );
68 ? ( sub { time2str('%b %d %Y', shift->sdate ) },
69 sub { time2str('%b %d %Y', shift->edate ) },
74 sub { time2str('%b %d %Y', shift->_date ) },
75 \&FS::UI::Web::cust_fields,
78 'setup', #broken in $unearned case i guess
79 ( $unearned ? ('', '') : () ),
80 ( $use_usage eq 'recurring' ? 'recur - usage' :
81 $use_usage eq 'usage' ? 'usage'
84 ( $unearned ? ('sdate', 'edate') : () ),
92 ( $unearned ? ( '', '' ) : () ),
94 ( $unearned ? ( '', '' ) : () ),
97 ( map { $_ ne 'Cust. Status' ? $clink : '' }
98 FS::UI::Web::cust_header()
101 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
103 ( $unearned ? 'rc' : '' ).
105 ( $unearned ? 'cc' : '' ).
107 FS::UI::Web::cust_aligns(),
112 ( $unearned ? ( '', '' ) : () ),
114 ( $unearned ? ( '', '' ) : () ),
117 FS::UI::Web::cust_colors(),
123 ( $unearned ? ( '', '' ) : () ),
125 ( $unearned ? ( '', '' ) : () ),
128 FS::UI::Web::cust_styles(),
134 #LOTS of false laziness below w/cust_credit_bill_pkg.cgi
137 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
139 my $conf = new FS::Conf;
143 #here is the agent virtualization
145 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
147 my @where = ( $agentnums_sql );
149 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
150 push @where, "_date >= $beginning",
153 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
154 push @where, "cust_main.agentnum = $1";
158 # not specified: all classes
161 my $use_override = $cgi->param('use_override');
162 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
165 $comparison = "IS NULL";
167 $comparison = "= $1";
170 if ( $use_override ) {
172 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
173 override.classnum $comparison AND pkgpart_override IS NOT NULL
176 push @where, "part_pkg.classnum $comparison";
180 if ( $cgi->param('taxclass')
181 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
182 #(should we save a taxclass or a link to taxnum
183 # in cust_bill_pkg or something like
184 # cust_bill_pkg_tax_location?)
188 #override taxclass when use_override is specified? probably
189 #if ( $use_override ) {
192 # ' ( '. join(' OR ',
194 # ' ( part_pkg.taxclass = '. dbh->quote($_).
195 # ' AND pkgpart_override IS NULL '.
197 # ' override.taxclass = '. dbh->quote($_).
198 # ' AND pkgpart_override IS NOT NULL '.
201 # $cgi->param('taxclass')
209 map ' part_pkg.taxclass = '.dbh->quote($_),
210 $cgi->param('taxclass')
218 my @loc_param = qw( city county state country );
220 if ( $cgi->param('out') ) {
222 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
223 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
224 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
227 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
228 if $cgi->param('istax');
232 SELECT COUNT(*) FROM cust_main_county
233 WHERE cust_main_county.tax > 0
238 #not linked to by anything, but useful for debugging "out of taxable region"
239 if ( grep $cgi->param($_), @loc_param ) {
241 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
243 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
244 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
245 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
248 push @where, $loc_sql;
252 } elsif ( $cgi->param('country') ) {
254 my @counties = $cgi->param('county');
256 if ( scalar(@counties) > 1 ) {
258 #hacky, could be more efficient. care if it is ever used for more than the
259 # tax-report_groups filtering kludge
262 ' ( '. join(' OR ', map {
264 my %ph = ( 'county' => dbh->quote($_),
265 map { $_ => dbh->quote( $cgi->param($_) ) }
266 qw( city state country )
269 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
270 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
271 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
280 push @where, $locs_sql;
284 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
286 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
287 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
288 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
291 push @where, $loc_sql;
295 if ( $cgi->param('istax') ) {
296 if ( $cgi->param('taxname') ) {
297 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
298 #} elsif ( $cgi->param('taxnameNULL') {
300 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
302 } elsif ( $cgi->param('nottax') ) {
303 #what can we usefully do with "taxname" ???? look up a class???
305 #warn "neither nottax nor istax parameters specified";
308 if ( $cgi->param('taxclassNULL') ) {
310 my %hash = ( 'country' => scalar($cgi->param('country')) );
311 foreach (qw( state county )) {
312 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
314 my $cust_main_county = qsearchs('cust_main_county', \%hash);
315 die "unknown base region for empty taxclass" unless $cust_main_county;
317 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
318 push @where, $same_sql if $same_sql;
322 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
324 push @where, FS::tax_rate_location->location_sql(
325 map { $_ => (scalar($cgi->param($_)) || '') }
326 qw( city county state locationtaxid )
329 } elsif ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
333 push @where, "cust_bill_pkg.sdate < $unearned",
334 "cust_bill_pkg.edate > $unearned",
335 "cust_bill_pkg.recur != 0",
336 "part_pkg.freq != '0'",
337 "part_pkg.freq != '1'",
338 "part_pkg.freq NOT LIKE '%h'",
339 "part_pkg.freq NOT LIKE '%d'",
340 "part_pkg.freq NOT LIKE '%w'";
344 if ( $cgi->param('itemdesc') ) {
345 if ( $cgi->param('itemdesc') eq 'Tax' ) {
346 push @where, "(itemdesc='Tax' OR itemdesc is null)";
348 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
352 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
353 my ( $group_op, $group_value ) = ( $1, $2 );
354 if ( $group_op eq '=' ) {
355 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
356 push @where, 'itemdesc = '. dbh->quote($group_value);
357 } elsif ( $group_op eq '!=' ) {
358 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
360 die "guru meditation #00de: group_op $group_op\n";
365 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
366 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
368 if ( $cgi->param('cust_tax') ) {
369 #false laziness -ish w/report_tax.cgi
371 if ( $cgi->param('taxname') ) {
372 my $q_taxname = dbh->quote($cgi->param('taxname'));
375 OR EXISTS ( SELECT 1 FROM cust_main_exemption
376 WHERE cust_main_exemption.custnum = cust_main.custnum
377 AND cust_main_exemption.taxname = $q_taxname )
381 $cust_exempt = " tax = 'Y' ";
384 push @where, $cust_exempt;
387 my $use_usage = $cgi->param('use_usage');
390 if ( $cgi->param('pkg_tax') ) {
395 ( CASE WHEN part_pkg.setuptax = 'Y'
396 THEN cust_bill_pkg.setup
401 ( CASE WHEN part_pkg.recurtax = 'Y'
402 THEN cust_bill_pkg.recur
409 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
410 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
411 "( tax != 'Y' OR tax IS NULL )";
413 } elsif ( $cgi->param('taxable') ) {
415 my $setup_taxable = "(
416 CASE WHEN part_pkg.setuptax = 'Y'
418 ELSE cust_bill_pkg.setup
422 my $recur_taxable = "(
423 CASE WHEN part_pkg.recurtax = 'Y'
425 ELSE cust_bill_pkg.recur
430 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
431 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
435 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
438 #not tax-exempt package (setup or recur)
440 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
441 AND cust_bill_pkg.setup > 0 )
443 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
444 AND cust_bill_pkg.recur > 0 )
446 #not a tax_exempt customer
447 "( tax != 'Y' OR tax IS NULL )";
448 #not covered in full by a monthly tax exemption (texas tax)
449 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
454 $count_query = "SELECT COUNT(*), ";
456 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
459 if ( $use_usage eq 'recurring' ) {
460 $count_query .= "SUM(setup + recur - usage)";
461 } elsif ( $use_usage eq 'usage' ) {
462 $count_query .= "SUM(usage)";
463 } elsif ( $unearned ) {
464 $count_query .= "SUM(cust_bill_pkg.recur)";
465 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
466 $count_query .= "SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
467 } elsif ( $cgi->param('iscredit') eq 'rate') {
468 $count_query .= "SUM( cust_credit_bill_pkg.amount )";
470 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
475 #false laziness w/report_prepaid_income.cgi
477 my $float = 'REAL'; #'DOUBLE PRECISION';
479 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS $float)";
480 my $elapsed = "(CASE WHEN cust_bill_pkg.sdate > $unearned
482 ELSE ($unearned - cust_bill_pkg.sdate)
484 #my $elapsed = "CAST($unearned - cust_bill_pkg.sdate AS $float)";
486 my $remaining = "(1 - $elapsed/$period)";
488 $count_query .= ", SUM($remaining * cust_bill_pkg.recur)";
494 my $join_cust = ' JOIN cust_bill USING ( invnum )
495 LEFT JOIN cust_main USING ( custnum ) ';
499 if ( $cgi->param('nottax') ) {
501 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
502 LEFT JOIN part_pkg USING ( pkgpart )
503 LEFT JOIN part_pkg AS override
504 ON pkgpart_override = override.pkgpart ';
505 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
506 if $conf->exists('tax-pkg_address');
508 } elsif ( $cgi->param('istax') ) {
510 #false laziness w/report_tax.cgi $taxfromwhere
511 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
512 $cgi->param('iscredit') eq 'rate') {
515 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
516 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
518 } elsif ( $conf->exists('tax-pkg_address') ) {
520 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
521 LEFT JOIN cust_location USING ( locationnum ) ';
523 #quelle kludge, somewhat false laziness w/report_tax.cgi
524 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
527 if ( $cgi->param('iscredit') ) {
528 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
529 if ( $cgi->param('iscredit') eq 'rate' ) {
530 $join_pkg .= ', billpkgtaxratelocationnum )';
531 } elsif ( $conf->exists('tax-pkg_address') ) {
532 $join_pkg .= ', billpkgtaxlocationnum )';
533 push @where, "billpkgtaxratelocationnum IS NULL";
536 push @where, "billpkgtaxratelocationnum IS NULL";
543 warn "neiether nottax nor istax parameters specified";
545 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
546 LEFT JOIN part_pkg USING ( pkgpart ) ';
550 my $where = ' WHERE '. join(' AND ', @where);
554 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
555 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
556 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
557 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
560 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
563 my @select = ( 'cust_bill_pkg.*',
564 'cust_bill._date', );
566 push @select, 'part_pkg.pkg',
568 unless $cgi->param('istax');
570 push @select, 'cust_main.custnum',
571 FS::UI::Web::cust_sql_fields();
574 'table' => 'cust_bill_pkg',
575 'addl_from' => "$join_cust $join_pkg",
577 'select' => join(', ', @select ),
578 'extra_sql' => $where,
579 'order_by' => 'ORDER BY _date, billpkgnum',
582 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
583 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
585 my $conf = new FS::Conf;
586 my $money_char = $conf->config('money_char') || '$';
589 $money_char. shift->owed_recur; #_recur :/
592 my $payment_date_sub = sub {
593 #my $cust_bill_pkg = shift;
594 my @cust_pay = sort { $a->_date <=> $b->_date }
595 map $_->cust_bill_pay->cust_pay,
596 shift->cust_bill_pay_pkg('recur') #recur :/
598 time2str('%b %d %Y', $cust_pay[-1]->_date );