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,
81 ( $unearned ? ( '', '' ) : () ),
83 ( $unearned ? ( '', '' ) : () ),
86 ( map { $_ ne 'Cust. Status' ? $clink : '' }
87 FS::UI::Web::cust_header()
90 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
92 ( $unearned ? 'rc' : '' ).
94 ( $unearned ? 'cc' : '' ).
96 FS::UI::Web::cust_aligns(),
101 ( $unearned ? ( '', '' ) : () ),
103 ( $unearned ? ( '', '' ) : () ),
106 FS::UI::Web::cust_colors(),
112 ( $unearned ? ( '', '' ) : () ),
114 ( $unearned ? ( '', '' ) : () ),
117 FS::UI::Web::cust_styles(),
124 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
126 my $conf = new FS::Conf;
130 #here is the agent virtualization
132 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
134 my @where = ( $agentnums_sql );
136 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
137 push @where, "_date >= $beginning",
140 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
141 push @where, "cust_main.agentnum = $1";
145 # not specified: all classes
148 my $use_override = $cgi->param('use_override');
149 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
152 $comparison = "IS NULL";
154 $comparison = "= $1";
157 if ( $use_override ) {
159 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
160 override.classnum $comparison AND pkgpart_override IS NOT NULL
163 push @where, "part_pkg.classnum $comparison";
167 if ( $cgi->param('taxclass')
168 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
169 #(should we save a taxclass or a link to taxnum
170 # in cust_bill_pkg or something like
171 # cust_bill_pkg_tax_location?)
175 #override taxclass when use_override is specified? probably
176 #if ( $use_override ) {
179 # ' ( '. join(' OR ',
181 # ' ( part_pkg.taxclass = '. dbh->quote($_).
182 # ' AND pkgpart_override IS NULL '.
184 # ' override.taxclass = '. dbh->quote($_).
185 # ' AND pkgpart_override IS NOT NULL '.
188 # $cgi->param('taxclass')
196 map ' part_pkg.taxclass = '.dbh->quote($_),
197 $cgi->param('taxclass')
205 if ( $cgi->param('out') ) {
207 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
208 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
209 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
212 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
213 if $cgi->param('istax');
217 SELECT COUNT(*) FROM cust_main_county
218 WHERE cust_main_county.tax > 0
223 #not linked to by anything, but useful for debugging "out of taxable region"
224 if ( grep $cgi->param($_), qw( county state country ) ) {
226 my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
227 qw( county state country );
229 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
230 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
231 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
234 push @where, $loc_sql;
238 } elsif ( $cgi->param('country') ) {
240 my @counties = $cgi->param('county');
242 if ( scalar(@counties) > 1 ) {
244 #hacky, could be more efficient. care if it is ever used for more than the
245 # tax-report_groups filtering kludge
248 ' ( '. join(' OR ', map {
250 my %ph = ( 'county' => dbh->quote($_),
251 map { $_ => dbh->quote( $cgi->param($_) ) }
255 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
256 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
257 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
266 push @where, $locs_sql;
270 my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
271 qw( county state country );
273 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
274 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
275 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
278 push @where, $loc_sql;
282 if ( $cgi->param('istax') ) {
283 if ( $cgi->param('taxname') ) {
284 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
285 #} elsif ( $cgi->param('taxnameNULL') {
287 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
289 } elsif ( $cgi->param('nottax') ) {
290 #what can we usefully do with "taxname" ???? look up a class???
292 #warn "neither nottax nor istax parameters specified";
295 if ( $cgi->param('taxclassNULL') ) {
297 my %hash = ( 'country' => scalar($cgi->param('country')) );
298 foreach (qw( state county )) {
299 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
301 my $cust_main_county = qsearchs('cust_main_county', \%hash);
302 die "unknown base region for empty taxclass" unless $cust_main_county;
304 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
305 push @where, $same_sql if $same_sql;
309 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
311 # this should really be shoved out to FS::cust_pkg->location_sql or something
312 # along with the code in report_newtax.cgi
315 'county' => 'tax_rate_location.county',
316 'state' => 'tax_rate_location.state',
317 'city' => 'tax_rate_location.city',
318 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
321 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
322 qw( county state city locationtaxid );
325 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
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 )",
453 $count_query = "SELECT COUNT(*), ";
455 if ( $use_usage eq 'recurring' ) {
456 $count_query .= "SUM(setup + recur - usage)";
457 } elsif ( $use_usage eq 'usage' ) {
458 $count_query .= "SUM(usage)";
459 } elsif ( $unearned ) {
460 $count_query .= "SUM(cust_bill_pkg.recur)";
462 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
467 #false laziness w/report_prepaid_income.cgi
469 my $float = 'REAL'; #'DOUBLE PRECISION';
471 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS $float)";
472 my $elapsed = "(CASE WHEN cust_bill_pkg.sdate > $unearned
474 ELSE ($unearned - cust_bill_pkg.sdate)
476 #my $elapsed = "CAST($unearned - cust_bill_pkg.sdate AS $float)";
478 my $remaining = "(1 - $elapsed/$period)";
480 $count_query .= ", SUM($remaining * cust_bill_pkg.recur)";
486 my $where = ' WHERE '. join(' AND ', @where);
488 my $join_cust = ' JOIN cust_bill USING ( invnum )
489 LEFT JOIN cust_main USING ( custnum ) ';
493 if ( $cgi->param('nottax') ) {
495 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
496 LEFT JOIN part_pkg USING ( pkgpart )
497 LEFT JOIN part_pkg AS override
498 ON pkgpart_override = override.pkgpart ';
499 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
500 if $conf->exists('tax-pkg_address');
502 } elsif ( $cgi->param('istax') ) {
504 #false laziness w/report_tax.cgi $taxfromwhere
505 if ( $conf->exists('tax-pkg_address') ) {
506 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
507 LEFT JOIN cust_location USING ( locationnum ) ';
509 #quelle kludge, false laziness w/report_tax.cgi
510 $where =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
511 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
513 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
514 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
520 warn "neiether nottax nor istax parameters specified";
522 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
523 LEFT JOIN part_pkg USING ( pkgpart ) ';
529 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
530 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
531 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
532 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
535 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
538 my @select = ( 'cust_bill_pkg.*',
539 'cust_bill._date', );
541 push @select, 'part_pkg.pkg',
543 unless $cgi->param('istax');
545 push @select, 'cust_main.custnum',
546 FS::UI::Web::cust_sql_fields();
549 'table' => 'cust_bill_pkg',
550 'addl_from' => "$join_cust $join_pkg",
552 'select' => join(', ', @select ),
553 'extra_sql' => $where,
554 'order_by' => 'ORDER BY _date, billpkgnum',
557 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
558 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
560 my $conf = new FS::Conf;
561 my $money_char = $conf->config('money_char') || '$';
564 $money_char. shift->owed_recur; #_recur :/
567 my $payment_date_sub = sub {
568 #my $cust_bill_pkg = shift;
569 my @cust_pay = sort { $a->_date <=> $b->_date }
570 map $_->cust_bill_pay->cust_pay,
571 shift->cust_bill_pay_pkg('recur') #recur :/
573 time2str('%b %d %Y', $cust_pay[-1]->_date );