1 <% include( 'elements/search.html',
2 'title' => 'Tax credits', #well, actually application of
3 'name' => 'tax credits', # credit to line item
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total', ],
23 FS::UI::Web::cust_header(),
27 sub { sprintf($money_char.'%.2f', shift->amount ) },
29 sub { time2str('%b %d %Y', shift->get('cust_credit_date') ) },
31 sub { shift->cust_credit_bill->cust_credit->reason },
33 sub { $_[0]->pkgnum > 0
34 ? $_[0]->get('pkg') # possibly use override.pkg
35 : $_[0]->get('itemdesc') # but i think this correct
38 sub { time2str('%b %d %Y', shift->_date ) },
39 \&FS::UI::Web::cust_fields,
49 ( map { $_ ne 'Cust. Status' ? $clink : '' }
50 FS::UI::Web::cust_header()
53 'align' => 'rrlllrr'.FS::UI::Web::cust_aligns(),
62 FS::UI::Web::cust_colors(),
72 FS::UI::Web::cust_styles(),
78 #LOTS of false laziness below w/cust_bill_pkg.cgi
81 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
83 my $conf = new FS::Conf;
85 #here is the agent virtualization
87 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
89 my @where = ( $agentnums_sql );
91 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
92 push @where, "cust_bill._date >= $beginning",
93 "cust_bill._date <= $ending";
95 push @where , " payby != 'COMP' "
96 unless $cgi->param('include_comp_cust');
98 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
99 push @where, "cust_main.agentnum = $1";
103 # not specified: all classes
106 my $use_override = $cgi->param('use_override');
107 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
110 $comparison = "IS NULL";
112 $comparison = "= $1";
115 if ( $use_override ) {
117 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
118 override.classnum $comparison AND pkgpart_override IS NOT NULL
121 push @where, "part_pkg.classnum $comparison";
125 if ( $cgi->param('taxclass')
126 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
127 #(should we save a taxclass or a link to taxnum
128 # in cust_bill_pkg or something like
129 # cust_bill_pkg_tax_location?)
133 #override taxclass when use_override is specified? probably
134 #if ( $use_override ) {
137 # ' ( '. join(' OR ',
139 # ' ( part_pkg.taxclass = '. dbh->quote($_).
140 # ' AND pkgpart_override IS NULL '.
142 # ' override.taxclass = '. dbh->quote($_).
143 # ' AND pkgpart_override IS NOT NULL '.
146 # $cgi->param('taxclass')
154 map ' part_pkg.taxclass = '.dbh->quote($_),
155 $cgi->param('taxclass')
163 my @loc_param = qw( city county state country );
165 if ( $cgi->param('out') ) {
167 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
168 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
169 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
172 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
173 if $cgi->param('istax');
177 SELECT COUNT(*) FROM cust_main_county
178 WHERE cust_main_county.tax > 0
183 #not linked to by anything, but useful for debugging "out of taxable region"
184 if ( grep $cgi->param($_), @loc_param ) {
186 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
188 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
189 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
190 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
193 push @where, $loc_sql;
197 } elsif ( $cgi->param('country') ) {
199 my @counties = $cgi->param('county');
201 if ( scalar(@counties) > 1 ) {
203 #hacky, could be more efficient. care if it is ever used for more than the
204 # tax-report_groups filtering kludge
207 ' ( '. join(' OR ', map {
209 my %ph = ( 'county' => dbh->quote($_),
210 map { $_ => dbh->quote( $cgi->param($_) ) }
211 qw( city state country )
214 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
215 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
216 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
225 push @where, $locs_sql;
229 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
231 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
232 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
233 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
236 push @where, $loc_sql;
241 if ( $cgi->param('istax') ) {
242 $title = 'Tax credits';
243 $name = 'tax credits';
244 if ( $cgi->param('taxname') ) {
245 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
246 #} elsif ( $cgi->param('taxnameNULL') {
248 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
250 } elsif ( $cgi->param('nottax') ) {
251 $title = 'Credit applications to line items';
252 $name = 'applications';
253 #what can we usefully do with "taxname" ???? look up a class???
255 $title = 'Credit applications to line items';
256 $name = 'applications';
257 #warn "neither nottax nor istax parameters specified";
260 if ( $cgi->param('taxclassNULL') ) {
262 my %hash = ( 'country' => scalar($cgi->param('country')) );
263 foreach (qw( state county )) {
264 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
266 my $cust_main_county = qsearchs('cust_main_county', \%hash);
267 die "unknown base region for empty taxclass" unless $cust_main_county;
269 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
270 push @where, $same_sql if $same_sql;
274 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
276 # this should really be shoved out to FS::cust_pkg->location_sql or something
277 # along with the code in report_newtax.cgi
280 'county' => 'tax_rate_location.county',
281 'state' => 'tax_rate_location.state',
282 'city' => 'tax_rate_location.city',
283 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
286 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
287 qw( city county state locationtaxid );
290 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
296 if ( $cgi->param('itemdesc') ) {
297 if ( $cgi->param('itemdesc') eq 'Tax' ) {
298 push @where, "(itemdesc='Tax' OR itemdesc is null)";
300 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
304 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
305 my ( $group_op, $group_value ) = ( $1, $2 );
306 if ( $group_op eq '=' ) {
307 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
308 push @where, 'itemdesc = '. dbh->quote($group_value);
309 } elsif ( $group_op eq '!=' ) {
310 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
312 die "guru meditation #00de: group_op $group_op\n";
317 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
318 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
320 if ( $cgi->param('cust_tax') ) {
321 #false laziness -ish w/report_tax.cgi
323 if ( $cgi->param('taxname') ) {
324 my $q_taxname = dbh->quote($cgi->param('taxname'));
327 OR EXISTS ( SELECT 1 FROM cust_main_exemption
328 WHERE cust_main_exemption.custnum = cust_main.custnum
329 AND cust_main_exemption.taxname = $q_taxname )
333 $cust_exempt = " tax = 'Y' ";
336 push @where, $cust_exempt;
339 my $use_usage = $cgi->param('use_usage');
342 if ( $cgi->param('pkg_tax') ) { #does this mean anything here?
347 ( CASE WHEN part_pkg.setuptax = 'Y'
348 THEN cust_bill_pkg.setup
353 ( CASE WHEN part_pkg.recurtax = 'Y'
354 THEN cust_bill_pkg.recur
361 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
362 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
363 "( tax != 'Y' OR tax IS NULL )";
365 } elsif ( $cgi->param('taxable') ) { #again, meaningful?
367 my $setup_taxable = "(
368 CASE WHEN part_pkg.setuptax = 'Y'
370 ELSE cust_bill_pkg.setup
374 my $recur_taxable = "(
375 CASE WHEN part_pkg.recurtax = 'Y'
377 ELSE cust_bill_pkg.recur
382 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
383 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
387 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
390 #not tax-exempt package (setup or recur)
392 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
393 AND cust_bill_pkg.setup > 0 )
395 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
396 AND cust_bill_pkg.recur > 0 )
398 #not a tax_exempt customer
399 "( tax != 'Y' OR tax IS NULL )";
400 #not covered in full by a monthly tax exemption (texas tax)
401 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
405 $count_query = "SELECT COUNT(*), ";
407 if ( $use_usage eq 'recurring' ) { #mean anything?
408 $count_query .= "SUM(setup + recur - usage)";
409 } elsif ( $use_usage eq 'usage' ) { #mean anything?
410 $count_query .= "SUM(usage)";
412 $count_query .= "SUM(cust_credit_bill_pkg.amount)";
418 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )
419 LEFT JOIN cust_main ON ( cust_bill.custnum = cust_main.custnum ) ';
424 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum ';
426 if ( $cgi->param('nottax') ) {
428 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
429 LEFT JOIN part_pkg USING ( pkgpart )
430 LEFT JOIN part_pkg AS override
431 ON pkgpart_override = override.pkgpart ';
432 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
433 if $conf->exists('tax-pkg_address');
435 } elsif ( $cgi->param('istax') ) {
437 #false laziness w/report_tax.cgi $taxfromwhere
438 if ( $conf->exists('tax-pkg_address') ) {
439 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
440 LEFT JOIN cust_location USING ( locationnum ) ';
442 #quelle kludge, somewhat false laziness w/report_tax.cgi
443 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
444 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
445 $cgi->param('iscredit') eq 'rate') {
447 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
448 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
451 if ( $conf->exists('tax-pkg_address') ) {
452 $join_cust_bill_pkg .= ', billpkgtaxlocationnum )';
453 push @where, "billpkgtaxratelocationnum IS NULL";
454 #} elsif ( $cgi->param('iscredit') eq 'rate' ) {
455 # $join_pkg .= ', billpkgtaxratelocationnum )';
457 $join_cust_bill_pkg .= ' )';
458 push @where, "billpkgtaxratelocationnum IS NULL";
464 warn "neiether nottax nor istax parameters specified";
466 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
467 LEFT JOIN part_pkg USING ( pkgpart ) ';
471 my $where = ' WHERE '. join(' AND ', @where);
473 my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
474 LEFT JOIN cust_credit USING ( crednum ) ';
478 # " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur,
479 # ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail
480 # WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum
481 # ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where
484 $count_query .= " FROM cust_credit_bill_pkg
492 my @select = ( 'cust_credit_bill_pkg.*',
494 'cust_credit.otaker',
495 'cust_credit._date AS cust_credit_date',
498 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
499 push @select, 'cust_main.custnum',
500 FS::UI::Web::cust_sql_fields();
503 'table' => 'cust_credit_bill_pkg',
504 'addl_from' => "$join_pkg
509 'select' => join(', ', @select ),
510 'extra_sql' => $where,
511 'order_by' => 'ORDER BY creditbillpkgnum', #cust_bill. or cust_credit._date?
514 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
515 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
517 my $conf = new FS::Conf;
518 my $money_char = $conf->config('money_char') || '$';