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' ) : (),
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 );
52 sub { my $row = shift;
54 if ( $use_usage eq 'recurring' ) {
55 $value = $row->recur - $row->usage;
56 } elsif ( $use_usage eq 'usage' ) {
61 sprintf($money_char.'%.2f', $value );
64 ? ( sub { time2str('%b %d %Y', shift->sdate ) },
65 sub { time2str('%b %d %Y', shift->edate ) },
70 sub { time2str('%b %d %Y', shift->_date ) },
71 \&FS::UI::Web::cust_fields,
78 ( $unearned ? ( '', '' ) : () ),
81 ( map { $_ ne 'Cust. Status' ? $clink : '' }
82 FS::UI::Web::cust_header()
85 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
87 ( $unearned ? 'cc' : '' ).
89 FS::UI::Web::cust_aligns(),
95 ( $unearned ? ( '', '' ) : () ),
98 FS::UI::Web::cust_colors(),
105 ( $unearned ? ( '', '' ) : () ),
108 FS::UI::Web::cust_styles(),
114 #LOTS of false laziness below w/cust_credit_bill_pkg.cgi
117 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
119 my $conf = new FS::Conf;
123 #here is the agent virtualization
125 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
127 my @where = ( $agentnums_sql );
129 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
130 push @where, "_date >= $beginning",
133 push @where , " payby != 'COMP' "
134 unless $cgi->param('include_comp_cust');
136 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
137 push @where, "cust_main.agentnum = $1";
141 # not specified: all classes
144 my $use_override = $cgi->param('use_override');
145 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
148 $comparison = "IS NULL";
150 $comparison = "= $1";
153 if ( $use_override ) {
155 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
156 override.classnum $comparison AND pkgpart_override IS NOT NULL
159 push @where, "part_pkg.classnum $comparison";
163 if ( $cgi->param('taxclass')
164 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
165 #(should we save a taxclass or a link to taxnum
166 # in cust_bill_pkg or something like
167 # cust_bill_pkg_tax_location?)
171 #override taxclass when use_override is specified? probably
172 #if ( $use_override ) {
175 # ' ( '. join(' OR ',
177 # ' ( part_pkg.taxclass = '. dbh->quote($_).
178 # ' AND pkgpart_override IS NULL '.
180 # ' override.taxclass = '. dbh->quote($_).
181 # ' AND pkgpart_override IS NOT NULL '.
184 # $cgi->param('taxclass')
192 map ' part_pkg.taxclass = '.dbh->quote($_),
193 $cgi->param('taxclass')
201 my @loc_param = qw( city county state country );
203 if ( $cgi->param('out') ) {
205 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
206 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
207 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
210 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
211 if $cgi->param('istax');
215 SELECT COUNT(*) FROM cust_main_county
216 WHERE cust_main_county.tax > 0
221 #not linked to by anything, but useful for debugging "out of taxable region"
222 if ( grep $cgi->param($_), @loc_param ) {
224 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
226 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
227 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
228 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
231 push @where, $loc_sql;
235 } elsif ( $cgi->param('country') ) {
237 my @counties = $cgi->param('county');
239 if ( scalar(@counties) > 1 ) {
241 #hacky, could be more efficient. care if it is ever used for more than the
242 # tax-report_groups filtering kludge
245 ' ( '. join(' OR ', map {
247 my %ph = ( 'county' => dbh->quote($_),
248 map { $_ => dbh->quote( $cgi->param($_) ) }
249 qw( city state country )
252 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
253 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
254 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
263 push @where, $locs_sql;
267 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
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;
274 push @where, $loc_sql;
278 if ( $cgi->param('istax') ) {
279 if ( $cgi->param('taxname') ) {
280 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
281 #} elsif ( $cgi->param('taxnameNULL') {
283 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
285 } elsif ( $cgi->param('nottax') ) {
286 #what can we usefully do with "taxname" ???? look up a class???
288 #warn "neither nottax nor istax parameters specified";
291 if ( $cgi->param('taxclassNULL') ) {
293 my %hash = ( 'country' => scalar($cgi->param('country')) );
294 foreach (qw( state county )) {
295 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
297 my $cust_main_county = qsearchs('cust_main_county', \%hash);
298 die "unknown base region for empty taxclass" unless $cust_main_county;
300 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
301 push @where, $same_sql if $same_sql;
305 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
307 # this should really be shoved out to FS::cust_pkg->location_sql or something
308 # along with the code in report_newtax.cgi
311 'county' => 'tax_rate_location.county',
312 'state' => 'tax_rate_location.state',
313 'city' => 'tax_rate_location.city',
314 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
317 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
318 qw( city county state locationtaxid );
321 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
325 } elsif ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
329 push @where, "cust_bill_pkg.sdate < $unearned",
330 "cust_bill_pkg.edate > $unearned",
331 "cust_bill_pkg.recur != 0",
332 "part_pkg.freq != '0'",
333 "part_pkg.freq != '1'",
334 "part_pkg.freq NOT LIKE '%h'",
335 "part_pkg.freq NOT LIKE '%d'",
336 "part_pkg.freq NOT LIKE '%w'";
340 if ( $cgi->param('itemdesc') ) {
341 if ( $cgi->param('itemdesc') eq 'Tax' ) {
342 push @where, "(itemdesc='Tax' OR itemdesc is null)";
344 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
348 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
349 my ( $group_op, $group_value ) = ( $1, $2 );
350 if ( $group_op eq '=' ) {
351 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
352 push @where, 'itemdesc = '. dbh->quote($group_value);
353 } elsif ( $group_op eq '!=' ) {
354 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
356 die "guru meditation #00de: group_op $group_op\n";
361 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
362 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
364 if ( $cgi->param('cust_tax') ) {
365 #false laziness -ish w/report_tax.cgi
367 if ( $cgi->param('taxname') ) {
368 my $q_taxname = dbh->quote($cgi->param('taxname'));
371 OR EXISTS ( SELECT 1 FROM cust_main_exemption
372 WHERE cust_main_exemption.custnum = cust_main.custnum
373 AND cust_main_exemption.taxname = $q_taxname )
377 $cust_exempt = " tax = 'Y' ";
380 push @where, $cust_exempt;
383 my $use_usage = $cgi->param('use_usage');
386 if ( $cgi->param('pkg_tax') ) {
391 ( CASE WHEN part_pkg.setuptax = 'Y'
392 THEN cust_bill_pkg.setup
397 ( CASE WHEN part_pkg.recurtax = 'Y'
398 THEN cust_bill_pkg.recur
405 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
406 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
407 "( tax != 'Y' OR tax IS NULL )";
409 } elsif ( $cgi->param('taxable') ) {
411 my $setup_taxable = "(
412 CASE WHEN part_pkg.setuptax = 'Y'
414 ELSE cust_bill_pkg.setup
418 my $recur_taxable = "(
419 CASE WHEN part_pkg.recurtax = 'Y'
421 ELSE cust_bill_pkg.recur
426 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
427 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
431 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
434 #not tax-exempt package (setup or recur)
436 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
437 AND cust_bill_pkg.setup > 0 )
439 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
440 AND cust_bill_pkg.recur > 0 )
442 #not a tax_exempt customer
443 "( tax != 'Y' OR tax IS NULL )";
444 #not covered in full by a monthly tax exemption (texas tax)
445 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
449 $count_query = "SELECT COUNT(*), ";
451 if ( $use_usage eq 'recurring' ) {
452 $count_query .= "SUM(setup + recur - usage)";
453 } elsif ( $use_usage eq 'usage' ) {
454 $count_query .= "SUM(usage)";
455 } elsif ( $unearned ) {
456 $count_query .= "SUM(cust_bill_pkg.recur)";
458 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
463 #false laziness w/report_prepaid_income.cgi
465 my $float = 'REAL'; #'DOUBLE PRECISION';
467 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS $float)";
468 my $elapsed = "(CASE WHEN cust_bill_pkg.sdate > $unearned
470 ELSE ($unearned - cust_bill_pkg.sdate)
472 #my $elapsed = "CAST($unearned - cust_bill_pkg.sdate AS $float)";
474 my $remaining = "(1 - $elapsed/$period)";
476 $count_query .= ", SUM($remaining * cust_bill_pkg.recur)";
482 my $join_cust = ' JOIN cust_bill USING ( invnum )
483 LEFT JOIN cust_main USING ( custnum ) ';
487 if ( $cgi->param('nottax') ) {
489 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
490 LEFT JOIN part_pkg USING ( pkgpart )
491 LEFT JOIN part_pkg AS override
492 ON pkgpart_override = override.pkgpart ';
493 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
494 if $conf->exists('tax-pkg_address');
496 } elsif ( $cgi->param('istax') ) {
498 #false laziness w/report_tax.cgi $taxfromwhere
499 if ( $conf->exists('tax-pkg_address') ) {
500 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
501 LEFT JOIN cust_location USING ( locationnum ) ';
503 #quelle kludge, somewhat false laziness w/report_tax.cgi
504 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
505 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
506 $cgi->param('iscredit') eq 'rate') {
508 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
509 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
512 if ( $cgi->param('iscredit') ) {
513 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
514 if ( $conf->exists('tax-pkg_address') ) {
515 $join_pkg .= ', billpkgtaxlocationnum )';
516 push @where, "billpkgtaxratelocationnum IS NULL";
517 } elsif ( $cgi->param('iscredit') eq 'rate' ) {
518 $join_pkg .= ', billpkgtaxratelocationnum )';
521 push @where, "billpkgtaxratelocationnum IS NULL";
528 warn "neiether nottax nor istax parameters specified";
530 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
531 LEFT JOIN part_pkg USING ( pkgpart ) ';
535 my $where = ' WHERE '. join(' AND ', @where);
539 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
540 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
541 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
542 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
545 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
548 my @select = ( 'cust_bill_pkg.*',
549 'cust_bill._date', );
551 push @select, 'part_pkg.pkg',
553 unless $cgi->param('istax');
555 push @select, 'cust_main.custnum',
556 FS::UI::Web::cust_sql_fields();
559 'table' => 'cust_bill_pkg',
560 'addl_from' => "$join_cust $join_pkg",
562 'select' => join(', ', @select ),
563 'extra_sql' => $where,
564 'order_by' => 'ORDER BY _date, billpkgnum',
567 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
568 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
570 my $conf = new FS::Conf;
571 my $money_char = $conf->config('money_char') || '$';