1 <% include("/elements/header.html", "$agentname Tax Report - ".
3 ? time2str('%h %o %Y ', $beginning )
7 ( $ending == 4294967295
9 : time2str('%h %o %Y', $ending )
14 <% include('/elements/table-grid.html') %>
17 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
18 <TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=9>Sales</TH>
19 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
20 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Rate</TH>
21 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2></TH>
22 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax owed</TH>
23 % unless ( $cgi->param('show_taxclasses') ) {
24 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax invoiced</TH>
25 <TH CLASS="grid" BGCOLOR="#cccccc" ROWSPAN=2>Tax credited</TH>
30 <TH CLASS="grid" BGCOLOR="#cccccc">Total</TH>
31 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
32 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt customer)</FONT></TH>
33 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
34 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(tax-exempt package)</FONT></TH>
35 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
36 <TH CLASS="grid" BGCOLOR="#cccccc">Non-taxable<BR><FONT SIZE=-1>(monthly exemption)</FONT></TH>
37 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
38 <TH CLASS="grid" BGCOLOR="#cccccc">Taxable</TH>
41 % my $bgcolor1 = '#eeeeee';
42 % my $bgcolor2 = '#ffffff';
45 % foreach my $region ( @regions ) {
48 % if ( $region->{'label'} eq $out ) {
51 % $link = ';'. $region->{'url_param'}
52 % if $region->{'url_param'};
55 % if ( $bgcolor eq $bgcolor1 ) {
56 % $bgcolor = $bgcolor2;
58 % $bgcolor = $bgcolor1;
62 % my $hicolor = $bgcolor;
63 % unless ( $cgi->param('show_taxclasses') ) {
64 % my $diff = abs( sprintf( '%.2f', $region->{'owed'} )
65 % - sprintf( '%.2f', $region->{'tax'} )
67 % if ( $diff > 0.02 ) {
68 % # $hicolor = $hicolor eq '#eeeeee' ? '#eeee66' : '#ffff99';
69 % #} elsif ( $diff ) {
70 % $hicolor = $hicolor eq '#eeeeee' ? '#eeee99' : '#ffffcc';
75 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
76 % my $tdh = qq(TD CLASS="grid" BGCOLOR="$hicolor");
77 % my $bigmath = '<FONT FACE="sans-serif" SIZE="+1"><B>';
78 % my $bme = '</B></FONT>';
81 <<%$td%>><% $region->{'label'} %></TD>
82 <<%$td%> ALIGN="right">
83 <A HREF="<% $baselink. $link %>;nottax=1"
84 ><% &$money_sprintf( $region->{'total'} ) %></A>
86 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
87 <<%$td%> ALIGN="right">
88 <A HREF="<% $baselink. $link %>;nottax=1;cust_tax=Y"
89 ><% &$money_sprintf( $region->{'exempt_cust'} ) %></A>
91 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
92 <<%$td%> ALIGN="right">
93 <A HREF="<% $baselink. $link %>;nottax=1;pkg_tax=Y"
94 ><% &$money_sprintf( $region->{'exempt_pkg'} ) %></A>
96 <<%$td%>><FONT SIZE="+1"><B> - </B></FONT></TD>
97 <<%$td%> ALIGN="right">
98 <A HREF="<% $exemptlink. $link %>"
99 ><% &$money_sprintf( $region->{'exempt_monthly'} ) %></A>
101 <<%$td%>><FONT SIZE="+1"><B> = </B></FONT></TD>
102 <<%$td%> ALIGN="right">
103 <A HREF="<% $baselink. $link %>;nottax=1;taxable=1"
104 ><% &$money_sprintf( $region->{'taxable'} ) %></A>
106 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath X $bme" %></TD>
107 <<%$td%> ALIGN="right"><% $region->{'rate'} %></TD>
108 <<%$td%>><% $region->{'label'} eq 'Total' ? '' : "$bigmath = $bme" %></TD>
109 <<%$tdh%> ALIGN="right">
110 <% &$money_sprintf( $region->{'owed'} ) %>
113 % unless ( $cgi->param('show_taxclasses') ) {
114 % my $invlink = $region->{'url_param_inv'}
115 % ? ';'. $region->{'url_param_inv'}
118 <<%$tdh%> ALIGN="right">
119 <A HREF="<% $baselink. $invlink %>;istax=1"
120 ><% &$money_sprintf( $region->{'tax'} ) %></A>
122 <<%$tdh%> ALIGN="right">
123 <A HREF="<% $baselink. $invlink %>;istax=1;iscredit=1"
124 ><% &$money_sprintf( $region->{'credit'} ) %></A>
133 % if ( $cgi->param('show_taxclasses') ) {
136 <% include('/elements/table-grid.html') %>
138 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
139 <TH CLASS="grid" BGCOLOR="#cccccc">Tax invoiced</TH>
140 <TH CLASS="grid" BGCOLOR="#cccccc">Tax credited</TH>
143 % #some false laziness w/above
144 % $bgcolor1 = '#eeeeee';
145 % $bgcolor2 = '#ffffff';
147 % foreach my $region ( @base_regions ) {
150 % if ( $region->{'label'} eq $out ) {
153 % $link = ';'. $region->{'url_param'}
154 % if $region->{'url_param'};
157 % if ( $bgcolor eq $bgcolor1 ) {
158 % $bgcolor = $bgcolor2;
160 % $bgcolor = $bgcolor1;
162 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
165 <<%$td%>><% $region->{'label'} %></TD>
166 <<%$td%> ALIGN="right">
167 <A HREF="<% $baselink. $link %>;istax=1"
168 ><% &$money_sprintf( $region->{'tax'} ) %></A>
174 % if ( $bgcolor eq $bgcolor1 ) {
175 % $bgcolor = $bgcolor2;
177 % $bgcolor = $bgcolor1;
179 % my $td = qq(TD CLASS="grid" BGCOLOR="$bgcolor");
183 <<%$td%> ALIGN="right">
184 <A HREF="<% $baselink %>;istax=1"
185 ><% &$money_sprintf( $tot_tax ) %></A>
193 <% include('/elements/footer.html') %>
198 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
200 my $conf = new FS::Conf;
202 my $user = getotaker;
204 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
206 my $join_cust = ' JOIN cust_bill USING ( invnum )
207 LEFT JOIN cust_main USING ( custnum ) ';
208 my $join_cust_pkg = $join_cust.
209 ' LEFT JOIN cust_pkg USING ( pkgnum )
210 LEFT JOIN part_pkg USING ( pkgpart ) ';
211 $join_cust_pkg .= ' LEFT JOIN cust_location USING ( locationnum )'
212 if $conf->exists('tax-pkg_address');
214 my $from_join_cust_pkg = " FROM cust_bill_pkg $join_cust_pkg ";
216 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
218 my( $location_sql, @base_param ) = FS::cust_pkg->location_sql;
219 $where .= " AND $location_sql ";
222 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
223 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
224 die "agent not found" unless $agent;
225 $agentname = $agent->agent;
226 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
231 my $prefix = @_ ? shift : '';
233 ( $table.${prefix}county = cust_main_county.county
234 OR cust_main_county.county = ''
235 OR cust_main_county.county IS NULL )
236 AND ( $table.${prefix}state = cust_main_county.state
237 OR cust_main_county.state = ''
238 OR cust_main_county.state IS NULL )
239 AND ( $table.${prefix}country = cust_main_county.country )
244 if ( $conf->exists('tax-ship_address') ) {
247 ( cust_main_county.country = cust_main.country
248 OR cust_main_county.country = cust_main.ship_country
254 ( ( ship_last IS NULL OR ship_last = '' )
255 AND ". gotcust('cust_main'). "
258 ( ship_last IS NOT NULL AND ship_last != ''
259 AND ". gotcust('cust_main', 'ship_'). "
266 $gotcust = gotcust('cust_main');
269 if ( $conf->exists('tax-pkg_address') ) {
271 ( cust_pkg.locationnum IS NULL AND $gotcust)
272 OR ( cust_pkg.locationnum IS NOT NULL AND ". gotcust('cust_location'). " )";
274 "WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
275 LEFT JOIN cust_main USING ( custnum )
276 LEFT JOIN cust_location USING ( locationnum )
283 "WHERE 0 < ( SELECT COUNT(*) FROM cust_main WHERE $gotcust LIMIT 1 )";
286 my $out = 'Out of taxable region(s)';
289 foreach my $r ( qsearch({ 'table' => 'cust_main_county',
290 'extra_sql' => $gotcust,
294 #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
296 my $label = getlabel($r);
297 $regions{$label}->{'label'} = $label;
299 $regions{$label}->{$_} = $r->$_() for (qw( county state country )); #taxname?
301 $regions{$label}->{'url_param'} =
302 join(';', map "$_=".uri_escape($r->$_()),
303 qw( county state country taxname )
306 my @param = @base_param;
307 my $mywhere = $where;
309 if ( $r->taxclass ) {
311 $mywhere .= " AND taxclass = ? ";
312 push @param, 'taxclass';
313 $regions{$label}->{'url_param'} .= ';taxclass='. uri_escape($r->taxclass);
314 #no, always# if $cgi->param('show_taxclasses');
316 $regions{$label}->{'taxclass'} = $r->taxclass;
320 $regions{$label}->{'url_param'} .= ';taxclassNULL=1'
321 if $cgi->param('show_taxclasses');
323 my $same_sql = $r->sql_taxclass_sameregion;
324 $mywhere .= " AND $same_sql" if $same_sql;
328 my $fromwhere = "$from_join_cust_pkg $mywhere"; # AND payby != 'COMP' ";
330 # my $label = getlabel($r);
331 # $regions{$label}->{'label'} = $label;
333 my $nottax = 'pkgnum != 0';
335 ## calculate total for this region
338 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax";
339 my $t = scalar_sql($r, \@param, $t_sql);
340 $regions{$label}->{'total'} += $t;
342 #if ( $label eq $out ) # && $t ) {
343 # warn "adding $t for ".
344 # join('/', map $r->$_, qw( taxclass county state country ) ). "\n";
345 # #warn $t_sql if $r->state eq 'FL';
348 ## calculate customer-exemption for this region
352 # my($taxable, $x_cust) = (0, 0);
353 # foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
354 # qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
355 # $taxable += scalar_sql($r, \@param,
356 # "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
359 # $x_cust += scalar_sql($r, \@param,
360 # "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
364 #false laziness -ish w/report_tax.cgi
367 my $q_taxname = dbh->quote($r->taxname);
370 OR EXISTS ( SELECT 1 FROM cust_main_exemption
371 WHERE cust_main_exemption.custnum = cust_main.custnum
372 AND cust_main_exemption.taxname = $q_taxname
377 $cust_exempt = " tax = 'Y' ";
380 my $x_cust = scalar_sql($r, \@param,
381 "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
382 $fromwhere AND $nottax AND $cust_exempt "
385 $regions{$label}->{'exempt_cust'} += $x_cust;
387 ## calculate package-exemption for this region
389 my $x_pkg = scalar_sql($r, \@param,
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
406 ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
407 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
409 AND ( tax != 'Y' OR tax IS NULL )
412 $regions{$label}->{'exempt_pkg'} += $x_pkg;
414 ## calculate monthly exemption (texas tax) for this region
416 # count up all the cust_tax_exempt_pkg records associated with
417 # the actual line items.
419 my $x_monthly = scalar_sql($r, \@param,
421 FROM cust_tax_exempt_pkg
422 JOIN cust_bill_pkg USING ( billpkgnum )
426 $regions{$label}->{'exempt_monthly'} += $x_monthly;
428 my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
429 $regions{$label}->{'taxable'} += $taxable;
431 $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
433 if ( defined($regions{$label}->{'rate'})
434 && $regions{$label}->{'rate'} != $r->tax.'%' ) {
435 $regions{$label}->{'rate'} = 'variable';
437 $regions{$label}->{'rate'} = $r->tax.'%';
442 my $distinct = "country, state, county,
443 CASE WHEN taxname IS NULL THEN '' ELSE taxname END AS taxname";
444 my $taxclass_distinct =
445 #a little bit unsure of this part... test?
446 #ah, it looks like it winds up being irrelevant as ->{'tax'}
447 # from $regions is not displayed when show_taxclasses is on
448 ( $cgi->param('show_taxclasses')
449 ? " CASE WHEN taxclass IS NULL THEN '' ELSE taxclass END "
455 'select' => "DISTINCT $distinct, $taxclass_distinct",
456 'table' => 'cust_main_county',
458 'extra_sql' => $gotcust,
461 my $taxfromwhere = " FROM cust_bill_pkg $join_cust ";
462 my $taxwhere = $where;
463 if ( $conf->exists('tax-pkg_address') ) {
465 $taxfromwhere .= 'LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
466 LEFT JOIN cust_location USING ( locationnum ) ';
469 $taxwhere =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
472 my $creditfromwhere = $taxfromwhere.
473 " JOIN cust_credit_bill_pkg USING (billpkgnum";
474 $creditfromwhere .= " ,billpkgtaxlocationnum"
475 if $conf->exists('tax-pkg_address');
476 $creditfromwhere .= ")";
478 $taxfromwhere .= " $taxwhere "; #AND payby != 'COMP' ";
479 $creditfromwhere .= " $taxwhere AND billpkgtaxratelocationnum IS NULL"; #AND payby != 'COMP' ";
481 my @taxparam = @base_param;
484 #should i be a cust_main_county method or something
485 #need to pass in $taxfromwhere & @taxparam???
486 my $_taxamount_sub = sub {
489 #match itemdesc if necessary!
492 ? 'AND itemdesc = '. dbh->quote($r->taxname)
493 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
495 my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) ".
496 " $taxfromwhere AND cust_bill_pkg.pkgnum = 0 $named_tax";
498 scalar_sql($r, \@taxparam, $sql );
501 my $_creditamount_sub = sub {
504 #match itemdesc if necessary!
507 ? 'AND itemdesc = '. dbh->quote($r->taxname)
508 : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
510 my $sql = "SELECT SUM(cust_credit_bill_pkg.amount) ".
511 " $creditfromwhere AND cust_bill_pkg.pkgnum = 0 $named_tax";
513 scalar_sql($r, \@taxparam, $sql );
516 #tax-report_groups filtering
517 my($group_op, $group_value) = ( '', '' );
518 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ ) {
519 ( $group_op, $group_value ) = ( $1, $2 );
521 my $group_test = sub {
523 return 1 unless $group_op; #in case we get called inadvertantly
524 if ( $label eq $out ) { #don't display "out of taxable region" in this case
526 } elsif ( $group_op eq '=' ) {
527 $label =~ /^$group_value/;
528 } elsif ( $group_op eq '!=' ) {
529 $label !~ /^$group_value/;
531 die "guru meditation #00de: group_op $group_op\n";
537 #foreach my $label ( keys %regions ) {
538 foreach my $r ( qsearch(\%qsearch) ) {
540 #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
542 my $label = getlabel($r);
544 next unless &{$group_test}($label);
547 #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
548 #my @param = @base_param;
550 my $x = &{$_taxamount_sub}($r);
552 $regions{$label}->{'tax'} += $x;
553 $tot_tax += $x unless $cgi->param('show_taxclasses');
555 ## calculate credit for this region
557 $x = &{$_creditamount_sub}($r);
559 $regions{$label}->{'credit'} += $x;
560 $tot_credit += $x unless $cgi->param('show_taxclasses');
564 my %base_regions = ();
565 if ( $cgi->param('show_taxclasses') ) {
567 $qsearch{'select'} = "DISTINCT $distinct";
568 foreach my $r ( qsearch(\%qsearch) ) {
570 my $x = &{$_taxamount_sub}($r);
572 my $base_label = getlabel($r, 'no_taxclass'=>1 );
573 $base_regions{$base_label}->{'label'} = $base_label;
575 $base_regions{$base_label}->{'url_param'} =
576 join(';', map "$_=". uri_escape($r->$_()),
577 qw( county state country taxname )
580 $base_regions{$base_label}->{'tax'} += $x;
583 ## calculate credit for this region
585 $x = &{$_creditamount_sub}($r);
587 $base_regions{$base_label}->{'credit'} += $x;
594 my @regions = keys %regions;
596 #tax-report_groups filtering
597 @regions = grep &{$group_test}($_), @regions
601 my( $total, $tot_taxable, $tot_owed ) = ( 0, 0, 0 );
602 my( $exempt_cust, $exempt_pkg, $exempt_monthly, $tot_credit ) = ( 0, 0, 0, 0 );
608 $total += $regions{$_}->{'total'};
609 $tot_taxable += $regions{$_}->{'taxable'};
610 $tot_owed += $regions{$_}->{'owed'};
611 $exempt_cust += $regions{$_}->{'exempt_cust'};
612 $exempt_pkg += $regions{$_}->{'exempt_pkg'};
613 $exempt_monthly += $regions{$_}->{'exempt_monthly'};
614 $tot_credit += $regions{$_}->{'credit'};
615 $taxclasses{$regions{$_}->{'taxclass'}} = 1
616 if $regions{$_}->{'taxclass'};
617 $county{$regions{$_}->{'county'}} = 1;
618 $state{$regions{$_}->{'state'}} = 1;
619 $country{$regions{$_}->{'country'}} = 1;
622 my $total_url_param = '';
623 my $total_url_param_invoiced = '';
626 my @country = keys %country;
627 warn "WARNING: multiple countries on this grouped report; total links broken"
628 if scalar(@country) > 1;
629 my $country = $country[0];
631 my @state = keys %state;
632 warn "WARNING: multiple countries on this grouped report; total links broken"
633 if scalar(@state) > 1;
634 my $state = $state[0];
636 $total_url_param_invoiced =
638 'report_group='.uri_escape("$group_op $group_value").';'.
639 join(';', map 'taxclass='.uri_escape($_), keys %taxclasses );
640 $total_url_param .= ';'.
641 "country=$country;state=".uri_escape($state).';'.
642 join(';', map 'county='.uri_escape($_), keys %county ) ;
649 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
653 map $base_regions{$_},
654 sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
660 'url_param' => $total_url_param,
661 'url_param_inv' => $total_url_param_invoiced,
663 'exempt_cust' => $exempt_cust,
664 'exempt_pkg' => $exempt_pkg,
665 'exempt_monthly' => $exempt_monthly,
666 'taxable' => $tot_taxable,
670 'credit' => $tot_credit,
675 my $money_char = $conf->config('money_char') || '$';
676 my $money_sprintf = sub {
677 $money_char. sprintf('%.2f', shift );
687 && ! scalar( qsearch('cust_main_county', { 'state' => $r->state,
688 'county' => $r->county,
689 'country' => $r->country,
690 'tax' => { op=>'>', value=>0 },
696 #kludge to avoid "will not stay shared" warning
697 my $out = 'Out of taxable region(s)';
699 # } elsif ( $r->taxname && count_taxname($r->taxname) == 1 ) {
700 # $label = $r->taxname;
701 ## $regions{$label}->{'taxname'} = $label;
702 ## push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
704 $label = $r->country;
705 $label = $r->state.", $label" if $r->state;
706 $label = $r->county." county, $label" if $r->county;
707 $label = "$label (". $r->taxclass. ")"
709 && $cgi->param('show_taxclasses')
710 && ! $opt{'no_taxclass'};
711 $label = $r->taxname. " ($label)" if $r->taxname;
716 #my %count_taxname = (); #cache
718 # my $taxname = shift;
719 # return $count_taxname{$taxname} if exists $count_taxname{$taxname};
720 # my $sql = 'SELECT COUNT(*) FROM cust_main_county WHERE taxname = ?';
721 # my $sth = dbh->prepare($sql) or die dbh->errstr;
722 # $sth->execute( $taxname )
723 # or die "Unexpected error executing statement $sql: ". $sth->errstr;
724 # $count_taxname{$taxname} = $sth->fetchrow_arrayref->[0];
727 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
728 #to FS::Report or FS::Record or who the fuck knows where)
730 my( $r, $param, $sql ) = @_;
732 my $sth = dbh->prepare($sql) or die dbh->errstr;
733 $sth->execute( map $r->$_(), @$param )
734 or die "Unexpected error executing statement $sql: ". $sth->errstr;
735 $sth->fetchrow_arrayref->[0] || 0;
738 my $dateagentlink = "begin=$beginning;end=$ending";
739 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
740 if length($agentname);
741 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
742 my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";