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);
149 if ( $cgi->param('distribute') == 1 ) {
150 push @where, "sdate <= $ending",
151 "edate > $beginning",
155 push @where, "_date >= $beginning",
159 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
160 push @where, "cust_main.agentnum = $1";
164 # not specified: all classes
167 my $use_override = $cgi->param('use_override');
168 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
171 $comparison = "IS NULL";
173 $comparison = "= $1";
176 if ( $use_override ) {
178 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
179 override.classnum $comparison AND pkgpart_override IS NOT NULL
182 push @where, "part_pkg.classnum $comparison";
186 if ( $cgi->param('taxclass')
187 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
188 #(should we save a taxclass or a link to taxnum
189 # in cust_bill_pkg or something like
190 # cust_bill_pkg_tax_location?)
194 #override taxclass when use_override is specified? probably
195 #if ( $use_override ) {
198 # ' ( '. join(' OR ',
200 # ' ( part_pkg.taxclass = '. dbh->quote($_).
201 # ' AND pkgpart_override IS NULL '.
203 # ' override.taxclass = '. dbh->quote($_).
204 # ' AND pkgpart_override IS NOT NULL '.
207 # $cgi->param('taxclass')
213 push @where, ' part_pkg.taxclass IN ( '.
214 join(', ', map dbh->quote($_), $cgi->param('taxclass') ).
221 my @loc_param = qw( city county state country );
223 if ( $cgi->param('out') ) {
225 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
226 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
227 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
230 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
231 if $cgi->param('istax');
235 SELECT COUNT(*) FROM cust_main_county
236 WHERE cust_main_county.tax > 0
241 #not linked to by anything, but useful for debugging "out of taxable region"
242 if ( grep $cgi->param($_), @loc_param ) {
244 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
246 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
247 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
248 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
251 push @where, $loc_sql;
255 } elsif ( $cgi->param('country') ) {
257 my @counties = $cgi->param('county');
259 if ( scalar(@counties) > 1 ) {
261 #hacky, could be more efficient. care if it is ever used for more than the
262 # tax-report_groups filtering kludge
265 ' ( '. join(' OR ', map {
267 my %ph = ( 'county' => dbh->quote($_),
268 map { $_ => dbh->quote( $cgi->param($_) ) }
269 qw( city state country )
272 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
273 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
274 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
283 push @where, $locs_sql;
287 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
289 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
290 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
291 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
294 push @where, $loc_sql;
298 if ( $cgi->param('istax') ) {
299 if ( $cgi->param('taxname') ) {
300 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
301 #} elsif ( $cgi->param('taxnameNULL') {
303 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
305 } elsif ( $cgi->param('nottax') ) {
306 #what can we usefully do with "taxname" ???? look up a class???
308 #warn "neither nottax nor istax parameters specified";
311 if ( $cgi->param('taxclassNULL')
312 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
313 #(see comment above?)
316 my %hash = ( 'country' => scalar($cgi->param('country')) );
317 foreach (qw( state county )) {
318 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
320 my $cust_main_county = qsearchs('cust_main_county', \%hash);
321 die "unknown base region for empty taxclass" unless $cust_main_county;
323 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
324 $same_sql =~ s/taxclass/part_pkg.taxclass/g;
325 push @where, $same_sql if $same_sql;
329 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
331 push @where, FS::tax_rate_location->location_sql(
332 map { $_ => (scalar($cgi->param($_)) || '') }
333 qw( city county state locationtaxid )
336 } elsif ( $cgi->param('unearned_now') =~ /^(\d+)$/ ) {
340 push @where, "cust_bill_pkg.sdate < $unearned",
341 "cust_bill_pkg.edate > $unearned",
342 "cust_bill_pkg.recur != 0",
343 "part_pkg.freq != '0'",
344 "part_pkg.freq != '1'",
345 "part_pkg.freq NOT LIKE '%h'",
346 "part_pkg.freq NOT LIKE '%d'",
347 "part_pkg.freq NOT LIKE '%w'";
351 if ( $cgi->param('itemdesc') ) {
352 if ( $cgi->param('itemdesc') eq 'Tax' ) {
353 push @where, "(itemdesc='Tax' OR itemdesc is null)";
355 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
359 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
360 my ( $group_op, $group_value ) = ( $1, $2 );
361 if ( $group_op eq '=' ) {
362 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
363 push @where, 'itemdesc = '. dbh->quote($group_value);
364 } elsif ( $group_op eq '!=' ) {
365 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
367 die "guru meditation #00de: group_op $group_op\n";
372 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
373 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
375 if ( $cgi->param('cust_tax') ) {
376 #false laziness -ish w/report_tax.cgi
378 if ( $cgi->param('taxname') ) {
379 my $q_taxname = dbh->quote($cgi->param('taxname'));
382 OR EXISTS ( SELECT 1 FROM cust_main_exemption
383 WHERE cust_main_exemption.custnum = cust_main.custnum
384 AND cust_main_exemption.taxname = $q_taxname )
388 $cust_exempt = " tax = 'Y' ";
391 push @where, $cust_exempt;
394 my $use_usage = $cgi->param('use_usage');
397 if ( $cgi->param('pkg_tax') ) {
402 ( CASE WHEN part_pkg.setuptax = 'Y'
403 THEN cust_bill_pkg.setup
408 ( CASE WHEN part_pkg.recurtax = 'Y'
409 THEN cust_bill_pkg.recur
416 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
417 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
418 "( tax != 'Y' OR tax IS NULL )";
420 } elsif ( $cgi->param('taxable') ) {
422 my $setup_taxable = "(
423 CASE WHEN part_pkg.setuptax = 'Y'
425 ELSE cust_bill_pkg.setup
429 my $recur_taxable = "(
430 CASE WHEN part_pkg.recurtax = 'Y'
432 ELSE cust_bill_pkg.recur
437 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
438 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
442 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
445 #not tax-exempt package (setup or recur)
447 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
448 AND cust_bill_pkg.setup > 0 )
450 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
451 AND cust_bill_pkg.recur > 0 )
453 #not a tax_exempt customer
454 "( tax != 'Y' OR tax IS NULL )";
455 #not covered in full by a monthly tax exemption (texas tax)
456 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
461 $count_query = "SELECT COUNT(*), ";
463 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
466 if ( $use_usage eq 'recurring' ) {
467 $count_query .= "SUM(setup + recur - usage)";
468 } elsif ( $use_usage eq 'usage' ) {
469 $count_query .= "SUM(usage)";
470 } elsif ( $unearned ) {
471 $count_query .= "SUM(cust_bill_pkg.recur)";
472 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
473 $count_query .= "SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
474 } elsif ( $cgi->param('iscredit') eq 'rate') {
475 $count_query .= "SUM( cust_credit_bill_pkg.amount )";
477 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
482 #false laziness w/report_prepaid_income.cgi
484 my $float = 'REAL'; #'DOUBLE PRECISION';
486 my $period = "CAST(cust_bill_pkg.edate - cust_bill_pkg.sdate AS $float)";
487 my $elapsed = "(CASE WHEN cust_bill_pkg.sdate > $unearned
489 ELSE ($unearned - cust_bill_pkg.sdate)
491 #my $elapsed = "CAST($unearned - cust_bill_pkg.sdate AS $float)";
493 my $remaining = "(1 - $elapsed/$period)";
495 $count_query .= ", SUM($remaining * cust_bill_pkg.recur)";
501 my $join_cust = ' JOIN cust_bill USING ( invnum )
502 LEFT JOIN cust_main USING ( custnum ) ';
506 if ( $cgi->param('nottax') ) {
508 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
509 LEFT JOIN part_pkg USING ( pkgpart )
510 LEFT JOIN part_pkg AS override
511 ON pkgpart_override = override.pkgpart ';
512 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
513 if $conf->exists('tax-pkg_address');
515 } elsif ( $cgi->param('istax') ) {
517 #false laziness w/report_tax.cgi $taxfromwhere
518 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
519 $cgi->param('iscredit') eq 'rate') {
522 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
523 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
525 } elsif ( $conf->exists('tax-pkg_address') ) {
527 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
528 LEFT JOIN cust_location USING ( locationnum ) ';
530 #quelle kludge, somewhat false laziness w/report_tax.cgi
531 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
534 if ( $cgi->param('iscredit') ) {
535 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
536 if ( $cgi->param('iscredit') eq 'rate' ) {
537 $join_pkg .= ', billpkgtaxratelocationnum )';
538 } elsif ( $conf->exists('tax-pkg_address') ) {
539 $join_pkg .= ', billpkgtaxlocationnum )';
540 push @where, "billpkgtaxratelocationnum IS NULL";
543 push @where, "billpkgtaxratelocationnum IS NULL";
550 warn "neiether nottax nor istax parameters specified";
552 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
553 LEFT JOIN part_pkg USING ( pkgpart ) ';
557 my $where = ' WHERE '. join(' AND ', @where);
561 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
562 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
563 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
564 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
567 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
570 my @select = ( 'cust_bill_pkg.*',
571 'cust_bill._date', );
573 push @select, 'part_pkg.pkg',
575 unless $cgi->param('istax');
577 push @select, 'cust_main.custnum',
578 FS::UI::Web::cust_sql_fields();
581 'table' => 'cust_bill_pkg',
582 'addl_from' => "$join_cust $join_pkg",
584 'select' => join(', ', @select ),
585 'extra_sql' => $where,
586 'order_by' => 'ORDER BY _date, billpkgnum',
589 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
590 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
592 my $conf = new FS::Conf;
593 my $money_char = $conf->config('money_char') || '$';
596 $money_char. shift->owed_recur; #_recur :/
599 my $payment_date_sub = sub {
600 #my $cust_bill_pkg = shift;
601 my @cust_pay = sort { $a->_date <=> $b->_date }
602 map $_->cust_bill_pay->cust_pay,
603 shift->cust_bill_pay_pkg('recur') #recur :/
605 time2str('%b %d %Y', $cust_pay[-1]->_date );