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 # this should really be shoved out to FS::cust_pkg->location_sql or something
325 # along with the code in report_newtax.cgi
328 'county' => 'tax_rate_location.county',
329 'state' => 'tax_rate_location.state',
330 'city' => 'tax_rate_location.city',
331 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
334 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
335 qw( city county state locationtaxid );
338 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
342 } elsif ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
346 push @where, "cust_bill_pkg.sdate < $unearned",
347 "cust_bill_pkg.edate > $unearned",
348 "cust_bill_pkg.recur != 0",
349 "part_pkg.freq != '0'",
350 "part_pkg.freq != '1'",
351 "part_pkg.freq NOT LIKE '%h'",
352 "part_pkg.freq NOT LIKE '%d'",
353 "part_pkg.freq NOT LIKE '%w'";
357 if ( $cgi->param('itemdesc') ) {
358 if ( $cgi->param('itemdesc') eq 'Tax' ) {
359 push @where, "(itemdesc='Tax' OR itemdesc is null)";
361 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
365 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
366 my ( $group_op, $group_value ) = ( $1, $2 );
367 if ( $group_op eq '=' ) {
368 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
369 push @where, 'itemdesc = '. dbh->quote($group_value);
370 } elsif ( $group_op eq '!=' ) {
371 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
373 die "guru meditation #00de: group_op $group_op\n";
378 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
379 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
381 if ( $cgi->param('cust_tax') ) {
382 #false laziness -ish w/report_tax.cgi
384 if ( $cgi->param('taxname') ) {
385 my $q_taxname = dbh->quote($cgi->param('taxname'));
388 OR EXISTS ( SELECT 1 FROM cust_main_exemption
389 WHERE cust_main_exemption.custnum = cust_main.custnum
390 AND cust_main_exemption.taxname = $q_taxname )
394 $cust_exempt = " tax = 'Y' ";
397 push @where, $cust_exempt;
400 my $use_usage = $cgi->param('use_usage');
403 if ( $cgi->param('pkg_tax') ) {
408 ( CASE WHEN part_pkg.setuptax = 'Y'
409 THEN cust_bill_pkg.setup
414 ( CASE WHEN part_pkg.recurtax = 'Y'
415 THEN cust_bill_pkg.recur
422 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
423 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
424 "( tax != 'Y' OR tax IS NULL )";
426 } elsif ( $cgi->param('taxable') ) {
428 my $setup_taxable = "(
429 CASE WHEN part_pkg.setuptax = 'Y'
431 ELSE cust_bill_pkg.setup
435 my $recur_taxable = "(
436 CASE WHEN part_pkg.recurtax = 'Y'
438 ELSE cust_bill_pkg.recur
443 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
444 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
448 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
451 #not tax-exempt package (setup or recur)
453 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
454 AND cust_bill_pkg.setup > 0 )
456 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
457 AND cust_bill_pkg.recur > 0 )
459 #not a tax_exempt customer
460 "( tax != 'Y' OR tax IS NULL )";
461 #not covered in full by a monthly tax exemption (texas tax)
462 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
466 #$count_query = "SELECT COUNT(*), ";
467 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
469 if ( $use_usage eq 'recurring' ) {
470 $count_query .= "SUM(setup + recur - usage)";
471 } elsif ( $use_usage eq 'usage' ) {
472 $count_query .= "SUM(usage)";
473 } elsif ( $unearned ) {
474 $count_query .= "SUM(cust_bill_pkg.recur)";
476 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
477 $cgi->param('iscredit') eq 'rate') {
478 $count_query .= "SUM( COALESCE(amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
480 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
486 #false laziness w/report_prepaid_income.cgi
488 my $float = 'REAL'; #'DOUBLE PRECISION';
490 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS $float)";
491 my $elapsed = "(CASE WHEN cust_bill_pkg.sdate > $unearned
493 ELSE ($unearned - cust_bill_pkg.sdate)
495 #my $elapsed = "CAST($unearned - cust_bill_pkg.sdate AS $float)";
497 my $remaining = "(1 - $elapsed/$period)";
499 $count_query .= ", SUM($remaining * cust_bill_pkg.recur)";
505 my $join_cust = ' JOIN cust_bill USING ( invnum )
506 LEFT JOIN cust_main USING ( custnum ) ';
510 if ( $cgi->param('nottax') ) {
512 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
513 LEFT JOIN part_pkg USING ( pkgpart )
514 LEFT JOIN part_pkg AS override
515 ON pkgpart_override = override.pkgpart ';
516 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
517 if $conf->exists('tax-pkg_address');
519 } elsif ( $cgi->param('istax') ) {
521 #false laziness w/report_tax.cgi $taxfromwhere
522 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
523 $cgi->param('iscredit') eq 'rate') {
526 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
527 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
529 } elsif ( $conf->exists('tax-pkg_address') ) {
531 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
532 LEFT JOIN cust_location USING ( locationnum ) ';
534 #quelle kludge, somewhat false laziness w/report_tax.cgi
535 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
538 if ( $cgi->param('iscredit') ) {
539 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
540 if ( $cgi->param('iscredit') eq 'rate' ) {
541 $join_pkg .= ', billpkgtaxratelocationnum )';
542 } elsif ( $conf->exists('tax-pkg_address') ) {
543 $join_pkg .= ', billpkgtaxlocationnum )';
544 push @where, "billpkgtaxratelocationnum IS NULL";
547 push @where, "billpkgtaxratelocationnum IS NULL";
554 warn "neiether nottax nor istax parameters specified";
556 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
557 LEFT JOIN part_pkg USING ( pkgpart ) ';
561 my $where = ' WHERE '. join(' AND ', @where);
565 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
566 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
567 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
568 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
571 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
574 my @select = ( 'cust_bill_pkg.*',
575 'cust_bill._date', );
577 push @select, 'part_pkg.pkg',
579 unless $cgi->param('istax');
581 push @select, 'cust_main.custnum',
582 FS::UI::Web::cust_sql_fields();
585 'table' => 'cust_bill_pkg',
586 'addl_from' => "$join_cust $join_pkg",
588 'select' => join(', ', @select ),
589 'extra_sql' => $where,
590 'order_by' => 'ORDER BY _date, billpkgnum',
593 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
594 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
596 my $conf = new FS::Conf;
597 my $money_char = $conf->config('money_char') || '$';
600 $money_char. shift->owed_recur; #_recur :/
603 my $payment_date_sub = sub {
604 #my $cust_bill_pkg = shift;
605 my @cust_pay = sort { $a->_date <=> $b->_date }
606 map $_->cust_bill_pay->cust_pay,
607 shift->cust_bill_pay_pkg('recur') #recur :/
609 time2str('%b %d %Y', $cust_pay[-1]->_date );