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' ) : (),
12 ? ( emt('Unearned'), emt('Owed'), emt('Payment date') )
13 : ( emt('Setup charge') )
15 ( $use_usage eq 'usage'
17 : emt('Recurring charge')
20 ? ( emt('Charge start'), emt('Charge end') )
25 FS::UI::Web::cust_header(),
28 sub { $_[0]->pkgnum > 0
29 ? $_[0]->get('pkg') # possibly use override.pkg
30 : $_[0]->get('itemdesc') # but i think this correct
32 #strikethrough or "N/A ($amount)" or something these when
33 # they're not applicable to pkg_tax search
34 sub { my $cust_bill_pkg = shift;
37 $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
38 my $elapsed = $unearned - $cust_bill_pkg->sdate;
39 $elapsed = 0 if $elapsed < 0;
41 my $remaining = 1 - $elapsed/$period;
43 sprintf($money_char. '%.2f',
44 $remaining * $cust_bill_pkg->recur );
47 sprintf($money_char.'%.2f', $cust_bill_pkg->setup );
51 ? ( $owed_sub, $payment_date_sub, )
54 sub { my $row = shift;
56 if ( $use_usage eq 'recurring' ) {
57 $value = $row->recur - $row->usage;
58 } elsif ( $use_usage eq 'usage' ) {
63 sprintf($money_char.'%.2f', $value );
66 ? ( sub { time2str('%b %d %Y', shift->sdate ) },
67 sub { time2str('%b %d %Y', shift->edate ) },
72 sub { time2str('%b %d %Y', shift->_date ) },
73 \&FS::UI::Web::cust_fields,
77 'setup', #broken in $unearned case i guess
78 ( $unearned ? ('', '') : () ),
79 ( $use_usage eq 'recurring' ? 'recur - usage' :
80 $use_usage eq 'usage' ? 'usage'
83 ( $unearned ? ('sdate', 'edate') : () ),
91 ( $unearned ? ( '', '' ) : () ),
93 ( $unearned ? ( '', '' ) : () ),
96 ( map { $_ ne 'Cust. Status' ? $clink : '' }
97 FS::UI::Web::cust_header()
100 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
102 ( $unearned ? 'rc' : '' ).
104 ( $unearned ? 'cc' : '' ).
106 FS::UI::Web::cust_aligns(),
111 ( $unearned ? ( '', '' ) : () ),
113 ( $unearned ? ( '', '' ) : () ),
116 FS::UI::Web::cust_colors(),
122 ( $unearned ? ( '', '' ) : () ),
124 ( $unearned ? ( '', '' ) : () ),
127 FS::UI::Web::cust_styles(),
132 #LOTS of false laziness below w/cust_credit_bill_pkg.cgi
135 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
137 my $conf = new FS::Conf;
141 #here is the agent virtualization
143 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
145 my @where = ( $agentnums_sql );
147 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
148 push @where, "_date >= $beginning",
151 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
152 push @where, "cust_main.agentnum = $1";
156 # not specified: all classes
159 my $use_override = $cgi->param('use_override');
160 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
163 $comparison = "IS NULL";
165 $comparison = "= $1";
168 if ( $use_override ) {
170 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
171 override.classnum $comparison AND pkgpart_override IS NOT NULL
174 push @where, "part_pkg.classnum $comparison";
178 if ( $cgi->param('taxclass')
179 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
180 #(should we save a taxclass or a link to taxnum
181 # in cust_bill_pkg or something like
182 # cust_bill_pkg_tax_location?)
186 #override taxclass when use_override is specified? probably
187 #if ( $use_override ) {
190 # ' ( '. join(' OR ',
192 # ' ( part_pkg.taxclass = '. dbh->quote($_).
193 # ' AND pkgpart_override IS NULL '.
195 # ' override.taxclass = '. dbh->quote($_).
196 # ' AND pkgpart_override IS NOT NULL '.
199 # $cgi->param('taxclass')
205 push @where, ' part_pkg.taxclass IN ( '.
206 join(', ', map dbh->quote($_), $cgi->param('taxclass') ).
213 my @loc_param = qw( city county state country );
215 if ( $cgi->param('out') ) {
217 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
218 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
219 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
222 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
223 if $cgi->param('istax');
227 SELECT COUNT(*) FROM cust_main_county
228 WHERE cust_main_county.tax > 0
233 #not linked to by anything, but useful for debugging "out of taxable region"
234 if ( grep $cgi->param($_), @loc_param ) {
236 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
238 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
239 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
240 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
243 push @where, $loc_sql;
247 } elsif ( $cgi->param('country') ) {
249 my @counties = $cgi->param('county');
251 if ( scalar(@counties) > 1 ) {
253 #hacky, could be more efficient. care if it is ever used for more than the
254 # tax-report_groups filtering kludge
257 ' ( '. join(' OR ', map {
259 my %ph = ( 'county' => dbh->quote($_),
260 map { $_ => dbh->quote( $cgi->param($_) ) }
261 qw( city state country )
264 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
265 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
266 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
275 push @where, $locs_sql;
279 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
281 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
282 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
283 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
286 push @where, $loc_sql;
290 if ( $cgi->param('istax') ) {
291 if ( $cgi->param('taxname') ) {
292 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
293 #} elsif ( $cgi->param('taxnameNULL') {
295 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
297 } elsif ( $cgi->param('nottax') ) {
298 #what can we usefully do with "taxname" ???? look up a class???
300 #warn "neither nottax nor istax parameters specified";
303 if ( $cgi->param('taxclassNULL') ) {
305 my %hash = ( 'country' => scalar($cgi->param('country')) );
306 foreach (qw( state county )) {
307 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
309 my $cust_main_county = qsearchs('cust_main_county', \%hash);
310 die "unknown base region for empty taxclass" unless $cust_main_county;
312 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
313 $same_sql =~ s/taxclass/part_pkg.taxclass/g;
314 push @where, $same_sql if $same_sql;
318 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
320 push @where, FS::tax_rate_location->location_sql(
321 map { $_ => (scalar($cgi->param($_)) || '') }
322 qw( city county state locationtaxid )
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 )",
450 $count_query = "SELECT COUNT(*), ";
452 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
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)";
461 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
462 $count_query .= "SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
463 } elsif ( $cgi->param('iscredit') eq 'rate') {
464 $count_query .= "SUM( cust_credit_bill_pkg.amount )";
466 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
471 #false laziness w/report_prepaid_income.cgi
473 my $float = 'REAL'; #'DOUBLE PRECISION';
475 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS $float)";
476 my $elapsed = "(CASE WHEN cust_bill_pkg.sdate > $unearned
478 ELSE ($unearned - cust_bill_pkg.sdate)
480 #my $elapsed = "CAST($unearned - cust_bill_pkg.sdate AS $float)";
482 my $remaining = "(1 - $elapsed/$period)";
484 $count_query .= ", SUM($remaining * cust_bill_pkg.recur)";
490 my $join_cust = ' JOIN cust_bill USING ( invnum )
491 LEFT JOIN cust_main USING ( custnum ) ';
495 if ( $cgi->param('nottax') ) {
497 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
498 LEFT JOIN part_pkg USING ( pkgpart )
499 LEFT JOIN part_pkg AS override
500 ON pkgpart_override = override.pkgpart ';
501 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
502 if $conf->exists('tax-pkg_address');
504 } elsif ( $cgi->param('istax') ) {
506 #false laziness w/report_tax.cgi $taxfromwhere
507 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
508 $cgi->param('iscredit') eq 'rate') {
511 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
512 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
514 } elsif ( $conf->exists('tax-pkg_address') ) {
516 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
517 LEFT JOIN cust_location USING ( locationnum ) ';
519 #quelle kludge, somewhat false laziness w/report_tax.cgi
520 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
523 if ( $cgi->param('iscredit') ) {
524 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
525 if ( $cgi->param('iscredit') eq 'rate' ) {
526 $join_pkg .= ', billpkgtaxratelocationnum )';
527 } elsif ( $conf->exists('tax-pkg_address') ) {
528 $join_pkg .= ', billpkgtaxlocationnum )';
529 push @where, "billpkgtaxratelocationnum IS NULL";
532 push @where, "billpkgtaxratelocationnum IS NULL";
539 warn "neiether nottax nor istax parameters specified";
541 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
542 LEFT JOIN part_pkg USING ( pkgpart ) ';
546 my $where = ' WHERE '. join(' AND ', @where);
550 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
551 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
552 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
553 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
556 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
559 my @select = ( 'cust_bill_pkg.*',
560 'cust_bill._date', );
562 push @select, 'part_pkg.pkg',
564 unless $cgi->param('istax');
566 push @select, 'cust_main.custnum',
567 FS::UI::Web::cust_sql_fields();
570 'table' => 'cust_bill_pkg',
571 'addl_from' => "$join_cust $join_pkg",
573 'select' => join(', ', @select ),
574 'extra_sql' => $where,
575 'order_by' => 'ORDER BY _date, billpkgnum',
578 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
579 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
581 my $conf = new FS::Conf;
582 my $money_char = $conf->config('money_char') || '$';
585 $money_char. shift->owed_recur; #_recur :/
588 my $payment_date_sub = sub {
589 #my $cust_bill_pkg = shift;
590 my @cust_pay = sort { $a->_date <=> $b->_date }
591 map $_->cust_bill_pay->cust_pay,
592 shift->cust_bill_pay_pkg('recur') #recur :/
594 time2str('%b %d %Y', $cust_pay[-1]->_date );