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', ],
10 ( $use_usage eq 'usage'
12 : emt('Recurring charge')
16 FS::UI::Web::cust_header(),
19 sub { $_[0]->pkgnum > 0
20 ? $_[0]->get('pkg') # possibly use override.pkg
21 : $_[0]->get('itemdesc') # but i think this correct
23 #strikethrough or "N/A ($amount)" or something these when
24 # they're not applicable to pkg_tax search
25 sub { my $cust_bill_pkg = shift;
26 sprintf($money_char.'%.2f', $cust_bill_pkg->setup );
28 sub { my $row = shift;
30 if ( $use_usage eq 'recurring' ) {
31 $value = $row->recur - $row->usage;
32 } elsif ( $use_usage eq 'usage' ) {
37 sprintf($money_char.'%.2f', $value );
40 sub { time2str('%b %d %Y', shift->_date ) },
41 \&FS::UI::Web::cust_fields,
46 ( $use_usage eq 'recurring'
62 ( map { $_ ne 'Cust. Status' ? $clink : '' }
63 FS::UI::Web::cust_header()
66 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
70 FS::UI::Web::cust_aligns(),
78 FS::UI::Web::cust_colors(),
87 FS::UI::Web::cust_styles(),
92 #LOTS of false laziness below w/cust_credit_bill_pkg.cgi
95 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
97 my $conf = new FS::Conf;
99 my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' );
100 my ($join_cust, $join_pkg ) = ('', '');
102 #here is the agent virtualization
104 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
106 my @where = ( $agentnums_sql );
108 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
110 if ( $cgi->param('status') =~ /^([a-z]+)$/ ) {
111 push @where, FS::cust_main->cust_status_sql . " = '$1'";
114 if ( $cgi->param('distribute') == 1 ) {
115 push @where, "sdate <= $ending",
116 "edate > $beginning",
120 push @where, "cust_bill._date >= $beginning",
121 "cust_bill._date <= $ending";
124 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
125 push @where, "cust_main.agentnum = $1";
128 if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
129 push @where, "cust_main.refnum = $1";
133 # not specified: all classes
136 my $use_override = $cgi->param('use_override');
137 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
140 $comparison = "IS NULL";
142 $comparison = "= $1";
145 if ( $use_override ) {
147 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
148 override.classnum $comparison AND pkgpart_override IS NOT NULL
151 push @where, "part_pkg.classnum $comparison";
155 if ( $cgi->param('taxclass')
156 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
157 #(should we save a taxclass or a link to taxnum
158 # in cust_bill_pkg or something like
159 # cust_bill_pkg_tax_location?)
163 #override taxclass when use_override is specified? probably
165 push @where, ' part_pkg.taxclass IN ( '.
166 join(', ', map dbh->quote($_), $cgi->param('taxclass') ).
171 my @loc_param = qw( district city county state country );
173 if ( $cgi->param('out') ) {
175 my ( $loc_sql, @param ) = FS::cust_location->in_county_sql( 'ornull' => 1 );
176 # while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
177 # $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
180 warn "\nLOC_SQL:\n$loc_sql\n";
183 SELECT COUNT(*) FROM cust_main_county
184 WHERE cust_main_county.tax > 0
189 #not linked to by anything, but useful for debugging "out of taxable region"
190 if ( grep $cgi->param($_), @loc_param ) {
192 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
194 my ( $loc_sql, @param ) = FS::cust_location->in_county_sql(param => 1);
195 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
196 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
199 warn "\nLOC_SQL:\n$loc_sql\n";
200 push @where, $loc_sql;
204 } elsif ( $cgi->param('country') ) { # and not $cgi->param('out')
206 my @counties = $cgi->param('county');
208 if ( scalar(@counties) > 1 ) {
210 #hacky, could be more efficient. care if it is ever used for more than the
211 # tax-report_groups filtering kludge
214 ' ( '. join(' OR ', map {
216 my %ph = ( 'county' => dbh->quote($_),
217 map { $_ => dbh->quote( $cgi->param($_) ) }
218 qw( district city state country )
221 my ( $loc_sql, @param ) = FS::cust_location->in_county_sql(param => 1);
222 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
223 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
232 warn "\nLOC_SQL:\n$locs_sql\n";
233 push @where, $locs_sql;
235 } else { #scalar(@counties) <= 1
237 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
240 my ( $loc_sql, @param ) = FS::cust_location->in_county_sql(param => 1);
241 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
242 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
245 warn "\nLOC_SQL:\n$loc_sql\n";
246 push @where, $loc_sql;
250 if ( $cgi->param('istax') ) {
251 if ( $cgi->param('taxname') ) {
252 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
253 #} elsif ( $cgi->param('taxnameNULL') {
255 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
257 } elsif ( $cgi->param('nottax') ) {
258 #what can we usefully do with "taxname" ???? look up a class???
260 #warn "neither nottax nor istax parameters specified";
263 if ( $cgi->param('taxclassNULL')
264 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
265 #(see comment above?)
268 my %hash = ( 'country' => scalar($cgi->param('country')) );
269 foreach (qw( state county )) {
270 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
272 my $cust_main_county = qsearchs('cust_main_county', \%hash);
273 die "unknown base region for empty taxclass" unless $cust_main_county;
275 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
276 $same_sql =~ s/taxclass/part_pkg.taxclass/g;
277 push @where, $same_sql if $same_sql;
281 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
282 # and not $cgi->param('out' or 'country')
284 push @where, FS::tax_rate_location->location_sql(
285 map { $_ => (scalar($cgi->param($_)) || '') }
286 qw( district city county state locationtaxid )
291 if ( $cgi->param('itemdesc') ) {
292 if ( $cgi->param('itemdesc') eq 'Tax' ) {
293 push @where, "(itemdesc='Tax' OR itemdesc is null)";
295 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
299 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
300 my ( $group_op, $group_value ) = ( $1, $2 );
301 if ( $group_op eq '=' ) {
302 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
303 push @where, 'itemdesc = '. dbh->quote($group_value);
304 } elsif ( $group_op eq '!=' ) {
305 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
307 die "guru meditation #00de: group_op $group_op\n";
312 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
313 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
315 if ( $cgi->param('cust_tax') ) {
316 #false laziness -ish w/report_tax.cgi
318 if ( $cgi->param('taxname') ) {
319 my $q_taxname = dbh->quote($cgi->param('taxname'));
322 OR EXISTS ( SELECT 1 FROM cust_main_exemption
323 WHERE cust_main_exemption.custnum = cust_main.custnum
324 AND cust_main_exemption.taxname = $q_taxname )
328 $cust_exempt = " tax = 'Y' ";
331 push @where, $cust_exempt;
334 my $use_usage = $cgi->param('use_usage');
337 if ( $cgi->param('pkg_tax') ) {
342 ( CASE WHEN part_pkg.setuptax = 'Y'
343 THEN cust_bill_pkg.setup
348 ( CASE WHEN part_pkg.recurtax = 'Y'
349 THEN cust_bill_pkg.recur
356 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
357 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
358 "( tax != 'Y' OR tax IS NULL )";
360 } elsif ( $cgi->param('taxable') ) {
362 my $setup_taxable = "(
363 CASE WHEN part_pkg.setuptax = 'Y'
365 ELSE cust_bill_pkg.setup
369 my $recur_taxable = "(
370 CASE WHEN part_pkg.recurtax = 'Y'
372 ELSE cust_bill_pkg.recur
377 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
378 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
382 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
385 #not tax-exempt package (setup or recur)
387 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
388 AND cust_bill_pkg.setup > 0 )
390 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
391 AND cust_bill_pkg.recur > 0 )
393 #not a tax_exempt customer
394 "( tax != 'Y' OR tax IS NULL )", # assume this was intended?
395 #not covered in full by a monthly tax exemption (texas tax)
396 "0 < ( $setup_taxable + $recur_taxable - $exempt )";
401 $count_query = "SELECT COUNT(*), ";
403 $count_query = "SELECT COUNT(DISTINCT billpkgnum), ";
406 if ( $use_usage eq 'recurring' ) {
407 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur - usage)";
408 } elsif ( $use_usage eq 'usage' ) {
409 $count_query .= "SUM(usage)";
410 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
411 $count_query .= "SUM( COALESCE(cust_bill_pkg_tax_rate_location.amount, cust_bill_pkg.setup + cust_bill_pkg.recur))";
412 } elsif ( $cgi->param('iscredit') eq 'rate') {
413 $count_query .= "SUM( cust_credit_bill_pkg.amount )";
415 $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
420 $join_cust = ' JOIN cust_bill USING ( invnum )
421 LEFT JOIN cust_main USING ( custnum ) ';
423 if ( $cgi->param('nottax') ) {
425 $join_pkg .= ' LEFT JOIN cust_pkg USING ( pkgnum )
426 LEFT JOIN part_pkg USING ( pkgpart )
427 LEFT JOIN part_pkg AS override
428 ON pkgpart_override = override.pkgpart
429 LEFT JOIN cust_location
430 ON cust_location.locationnum = '.
431 FS::cust_pkg->tax_locationnum_sql;
433 } elsif ( $cgi->param('istax') ) {
435 #false laziness w/report_tax.cgi $taxfromwhere
436 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
437 $cgi->param('iscredit') eq 'rate') {
439 # using tax_rate_location and friends
441 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
442 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
444 #} elsif ( $conf->exists('tax-pkg_address') ) {
447 # using cust_bill_pkg_tax_location to relate tax items to locations
448 # ...but for consolidated taxes we don't want to duplicate this
449 my $tax_item_location = '(SELECT DISTINCT billpkgnum, locationnum
450 FROM cust_bill_pkg_tax_location) AS tax_item_location';
452 $join_pkg .= " LEFT JOIN $tax_item_location USING ( billpkgnum )
453 LEFT JOIN cust_location
454 ON tax_item_location.locationnum =
455 cust_location.locationnum ";
457 #quelle kludge, somewhat false laziness w/report_tax.cgi
458 s/cust_pkg\.locationnum/tax_item_location.locationnum/g for @where;
461 if ( $cgi->param('iscredit') ) {
462 $join_pkg .= ' JOIN cust_credit_bill_pkg USING ( billpkgnum';
463 if ( $cgi->param('iscredit') eq 'rate' ) {
464 $join_pkg .= ', billpkgtaxratelocationnum )';
465 } elsif ( $conf->exists('tax-pkg_address') ) {
466 $join_pkg .= ', billpkgtaxlocationnum )';
467 push @where, "billpkgtaxratelocationnum IS NULL";
470 push @where, "billpkgtaxratelocationnum IS NULL";
477 warn "neither nottax nor istax parameters specified";
479 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
480 LEFT JOIN part_pkg USING ( pkgpart ) ';
484 my $where = ' WHERE '. join(' AND ', @where);
488 " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
489 ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
490 WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
491 ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
494 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
497 push @select, 'part_pkg.pkg',
499 unless $cgi->param('istax');
501 push @select, 'cust_main.custnum',
502 FS::UI::Web::cust_sql_fields();
505 'table' => 'cust_bill_pkg',
506 'addl_from' => "$join_cust $join_pkg",
508 'select' => join(",\n", @select ),
509 'extra_sql' => $where,
510 'order_by' => 'ORDER BY cust_bill._date, billpkgnum',
513 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
514 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
516 my $conf = new FS::Conf;
517 my $money_char = $conf->config('money_char') || '$';
520 $money_char . shift->get('owed') # owed_recur is not correct here
522 my $payment_date_sub = sub {
523 #my $cust_bill_pkg = shift;
524 my @cust_pay = sort { $a->_date <=> $b->_date }
525 map $_->cust_bill_pay->cust_pay,
526 shift->cust_bill_pay_pkg('recur') #recur :/
528 time2str('%b %d %Y', $cust_pay[-1]->_date );