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' ) : (),
13 emt('Owed'), # useful in 'paid' mode?
15 : ( emt('Setup charge') )
17 ( $use_usage eq 'usage'
19 : emt('Recurring charge')
22 ? ( emt('Charge start'), emt('Charge end') )
27 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 sprintf($money_char.'%.2f',
40 $cust_bill_pkg->unearned_revenue)
43 sprintf($money_char.'%.2f', $cust_bill_pkg->setup );
47 ? ( $owed_sub, $payment_date_sub, )
50 sub { my $row = shift;
52 if ( $use_usage eq 'recurring' or $unearned ) {
53 $value = $row->recur - $row->usage;
54 } elsif ( $use_usage eq 'usage' ) {
59 sprintf($money_char.'%.2f', $value );
62 ? ( sub { time2str('%b %d %Y', shift->sdate ) },
63 # shift edate back a day
65 # (to avoid skipping a day during DST)
66 sub { time2str('%b %d %Y', shift->edate - 82799 ) },
71 sub { time2str('%b %d %Y', shift->_date ) },
72 \&FS::UI::Web::cust_fields,
76 'setup', #broken in $unearned case i guess
77 ( $unearned ? ('', '') : () ),
78 ( $use_usage eq 'recurring' or $unearned
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(),
133 #LOTS of false laziness below w/cust_credit_bill_pkg.cgi
136 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
138 my $conf = new FS::Conf;
141 my $unearned_mode = '';
142 my $unearned_base = '';
143 my $unearned_sql = '';
145 my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' );
146 my ($join_cust, $join_pkg ) = ('', '');
148 #here is the agent virtualization
150 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
152 my @where = ( $agentnums_sql );
154 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
156 if ( $cgi->param('status') =~ /^([a-z]+)$/ ) {
157 push @where, FS::cust_main->cust_status_sql . " = '$1'";
160 if ( $cgi->param('distribute') == 1 ) {
161 push @where, "sdate <= $ending",
162 "edate > $beginning",
166 push @where, "cust_bill._date >= $beginning",
167 "cust_bill._date <= $ending";
170 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
171 push @where, "cust_main.agentnum = $1";
175 # not specified: all classes
178 my $use_override = $cgi->param('use_override');
179 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
182 $comparison = "IS NULL";
184 $comparison = "= $1";
187 if ( $use_override ) {
189 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
190 override.classnum $comparison AND pkgpart_override IS NOT NULL
193 push @where, "part_pkg.classnum $comparison";
197 if ( $cgi->param('taxclass')
198 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
199 #(should we save a taxclass or a link to taxnum
200 # in cust_bill_pkg or something like
201 # cust_bill_pkg_tax_location?)
205 #override taxclass when use_override is specified? probably
206 #if ( $use_override ) {
209 # ' ( '. join(' OR ',
211 # ' ( part_pkg.taxclass = '. dbh->quote($_).
212 # ' AND pkgpart_override IS NULL '.
214 # ' override.taxclass = '. dbh->quote($_).
215 # ' AND pkgpart_override IS NOT NULL '.
218 # $cgi->param('taxclass')
224 push @where, ' part_pkg.taxclass IN ( '.
225 join(', ', map dbh->quote($_), $cgi->param('taxclass') ).
232 my @loc_param = qw( district city county state country );
234 if ( $cgi->param('out') ) {
236 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
237 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
238 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
241 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
242 if $cgi->param('istax');
246 SELECT COUNT(*) FROM cust_main_county
247 WHERE cust_main_county.tax > 0
252 #not linked to by anything, but useful for debugging "out of taxable region"
253 if ( grep $cgi->param($_), @loc_param ) {
255 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
257 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
258 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
259 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
262 push @where, $loc_sql;
266 } elsif ( $cgi->param('country') ) {
268 my @counties = $cgi->param('county');
270 if ( scalar(@counties) > 1 ) {
272 #hacky, could be more efficient. care if it is ever used for more than the
273 # tax-report_groups filtering kludge
276 ' ( '. join(' OR ', map {
278 my %ph = ( 'county' => dbh->quote($_),
279 map { $_ => dbh->quote( $cgi->param($_) ) }
280 qw( district city state country )
283 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
284 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
285 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
294 push @where, $locs_sql;
298 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
300 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
301 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
302 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
305 push @where, $loc_sql;
309 if ( $cgi->param('istax') ) {
310 if ( $cgi->param('taxname') ) {
311 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
312 #} elsif ( $cgi->param('taxnameNULL') {
314 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
316 } elsif ( $cgi->param('nottax') ) {
317 #what can we usefully do with "taxname" ???? look up a class???
319 #warn "neither nottax nor istax parameters specified";
322 if ( $cgi->param('taxclassNULL')
323 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
324 #(see comment above?)
327 my %hash = ( 'country' => scalar($cgi->param('country')) );
328 foreach (qw( state county )) {
329 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
331 my $cust_main_county = qsearchs('cust_main_county', \%hash);
332 die "unknown base region for empty taxclass" unless $cust_main_county;
334 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
335 $same_sql =~ s/taxclass/part_pkg.taxclass/g;
336 push @where, $same_sql if $same_sql;
340 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
342 push @where, FS::tax_rate_location->location_sql(
343 map { $_ => (scalar($cgi->param($_)) || '') }
344 qw( district city county state locationtaxid )
349 # unearned revenue mode
350 if ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
353 $unearned_mode = $cgi->param('mode');
355 push @where, "cust_bill_pkg.sdate < $unearned",
356 "cust_bill_pkg.edate > $unearned",
357 "cust_bill_pkg.recur != 0",
358 "part_pkg.freq != '0'";
360 if ( !$cgi->param('include_monthly') ) {
362 "part_pkg.freq != '1'",
363 "part_pkg.freq NOT LIKE '%h'",
364 "part_pkg.freq NOT LIKE '%d'",
365 "part_pkg.freq NOT LIKE '%w'";
368 my $usage_sql = FS::cust_bill_pkg->usage_sql;
369 push @select, "($usage_sql) AS usage"; # we need this
370 my $owed_sql = FS::cust_bill_pkg->owed_sql($unearned, '',
371 setuprecur => 'recur', no_usage => 1);
372 push @select, "($owed_sql) AS owed"; # also this
374 $unearned_base = "(cust_bill_pkg.recur - $usage_sql)";
375 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS REAL)";
376 my $elapsed = "GREATEST( $unearned - cust_bill_pkg.sdate, 0 )";
377 my $remaining = "(1 - $elapsed/$period)";
379 $unearned_sql = "CAST(
380 GREATEST( ( $unearned_base * $remaining ) - $owed_sql, 0 )
383 push @select, "$unearned_sql AS unearned_revenue";
385 if ( $unearned_mode eq 'billed' ) {
386 # include only rows that have some unearned portion
387 push @where, "$unearned_base > 0";
389 elsif ( $unearned_mode eq 'paid' ) {
390 # include only those that have some _paid_ unearned portion
391 push @where, "$unearned_sql > 0";
396 if ( $cgi->param('itemdesc') ) {
397 if ( $cgi->param('itemdesc') eq 'Tax' ) {
398 push @where, "(itemdesc='Tax' OR itemdesc is null)";
400 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
404 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
405 my ( $group_op, $group_value ) = ( $1, $2 );
406 if ( $group_op eq '=' ) {
407 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
408 push @where, 'itemdesc = '. dbh->quote($group_value);
409 } elsif ( $group_op eq '!=' ) {
410 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
412 die "guru meditation #00de: group_op $group_op\n";
417 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
418 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
420 if ( $cgi->param('cust_tax') ) {
421 #false laziness -ish w/report_tax.cgi
423 if ( $cgi->param('taxname') ) {
424 my $q_taxname = dbh->quote($cgi->param('taxname'));
427 OR EXISTS ( SELECT 1 FROM cust_main_exemption
428 WHERE cust_main_exemption.custnum = cust_main.custnum
429 AND cust_main_exemption.taxname = $q_taxname )
433 $cust_exempt = " tax = 'Y' ";
436 push @where, $cust_exempt;
439 my $use_usage = $cgi->param('use_usage');
442 if ( $cgi->param('pkg_tax') ) {
447 ( CASE WHEN part_pkg.setuptax = 'Y'
448 THEN cust_bill_pkg.setup
453 ( CASE WHEN part_pkg.recurtax = 'Y'
454 THEN cust_bill_pkg.recur
461 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
462 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
463 "( tax != 'Y' OR tax IS NULL )";
465 } elsif ( $cgi->param('taxable') ) {
467 my $setup_taxable = "(
468 CASE WHEN part_pkg.setuptax = 'Y'
470 ELSE cust_bill_pkg.setup
474 my $recur_taxable = "(
475 CASE WHEN part_pkg.recurtax = 'Y'
477 ELSE cust_bill_pkg.recur
482 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
483 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
487 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
490 #not tax-exempt package (setup or recur)
492 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
493 AND cust_bill_pkg.setup > 0 )
495 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
496 AND cust_bill_pkg.recur > 0 )
498 #not a tax_exempt customer
499 "( tax != 'Y' OR tax IS NULL )";
500 #not covered in full by a monthly tax exemption (texas tax)
501 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
506 $count_query = "SELECT COUNT(*), ";
508 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
512 $count_query .= "SUM( $unearned_base ), SUM( $unearned_sql )";
513 } elsif ( $use_usage eq 'recurring' ) {
514 $count_query .= "SUM(setup + recur - usage)";
515 } elsif ( $use_usage eq 'usage' ) {
516 $count_query .= "SUM(usage)";
517 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
518 $count_query .= "SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
519 } elsif ( $cgi->param('iscredit') eq 'rate') {
520 $count_query .= "SUM( cust_credit_bill_pkg.amount )";
522 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
527 $join_cust = ' JOIN cust_bill USING ( invnum )
528 LEFT JOIN cust_main USING ( custnum ) ';
530 if ( $cgi->param('nottax') ) {
532 $join_pkg .= ' LEFT JOIN cust_pkg USING ( pkgnum )
533 LEFT JOIN part_pkg USING ( pkgpart )
534 LEFT JOIN part_pkg AS override
535 ON pkgpart_override = override.pkgpart ';
536 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
537 if $conf->exists('tax-pkg_address');
539 } elsif ( $cgi->param('istax') ) {
541 #false laziness w/report_tax.cgi $taxfromwhere
542 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
543 $cgi->param('iscredit') eq 'rate') {
546 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
547 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
549 } elsif ( $conf->exists('tax-pkg_address') ) {
551 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
552 LEFT JOIN cust_location USING ( locationnum ) ';
554 #quelle kludge, somewhat false laziness w/report_tax.cgi
555 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
558 if ( $cgi->param('iscredit') ) {
559 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
560 if ( $cgi->param('iscredit') eq 'rate' ) {
561 $join_pkg .= ', billpkgtaxratelocationnum )';
562 } elsif ( $conf->exists('tax-pkg_address') ) {
563 $join_pkg .= ', billpkgtaxlocationnum )';
564 push @where, "billpkgtaxratelocationnum IS NULL";
567 push @where, "billpkgtaxratelocationnum IS NULL";
574 warn "neiether nottax nor istax parameters specified";
576 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
577 LEFT JOIN part_pkg USING ( pkgpart ) ';
581 my $where = ' WHERE '. join(' AND ', @where);
585 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
586 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
587 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
588 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
591 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
594 push @select, 'part_pkg.pkg',
596 unless $cgi->param('istax');
598 push @select, 'cust_main.custnum',
599 FS::UI::Web::cust_sql_fields();
602 'table' => 'cust_bill_pkg',
603 'addl_from' => "$join_cust $join_pkg",
605 'select' => join(",\n", @select ),
606 'extra_sql' => $where,
607 'order_by' => 'ORDER BY cust_bill._date, billpkgnum',
610 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
611 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
613 my $conf = new FS::Conf;
614 my $money_char = $conf->config('money_char') || '$';
617 $money_char . shift->get('owed') # owed_recur is not correct here
619 my $payment_date_sub = sub {
620 #my $cust_bill_pkg = shift;
621 my @cust_pay = sort { $a->_date <=> $b->_date }
622 map $_->cust_bill_pay->cust_pay,
623 shift->cust_bill_pay_pkg('recur') #recur :/
625 time2str('%b %d %Y', $cust_pay[-1]->_date );