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"></TH>
18 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
19 <TH CLASS="grid" BGCOLOR="#cccccc">Tax collected</TH>
20 <TH CLASS="grid" BGCOLOR="#cccccc"> </TH>
21 <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
22 <TH CLASS="grid" BGCOLOR="#cccccc">Tax credited</TH>
24 % my $bgcolor1 = '#eeeeee';
25 % my $bgcolor2 = '#ffffff';
28 % foreach my $tax ( @taxes ) {
30 % if ( $bgcolor eq $bgcolor1 ) {
31 % $bgcolor = $bgcolor2;
33 % $bgcolor = $bgcolor1;
37 % if ( $tax->{'label'} ne 'Total' ) {
38 % $link = ';'. $tax->{'url_param'};
43 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $tax->{'label'} %></TD>
44 <% $tax->{base} ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
45 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
46 <A HREF="<% $baselink. $link %>;istax=1"><% $money_char %><% sprintf('%.2f', $tax->{'tax'} ) %></A>
48 <% !($tax->{base}) ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
49 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"></TD>
50 <% $tax->{base} ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
51 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ALIGN="right">
52 <A HREF="<% $baselink. $link %>;istax=1;iscredit=rate"><% $money_char %><% sprintf('%.2f', $tax->{'credit'} ) %></A>
54 <% !($tax->{base}) ? qq!<TD CLASS="grid" BGCOLOR="$bgcolor"></TD>! : '' %>
65 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
67 my $conf = new FS::Conf;
68 my $money_char = $conf->config('money_char') || '$';
70 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
73 JOIN cust_bill USING ( invnum )
74 LEFT JOIN cust_main USING ( custnum )
77 my $join_loc = "LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum )";
78 my $join_tax_loc = "LEFT JOIN tax_rate_location USING ( taxratelocationnum )";
80 my $addl_from = " $join_cust $join_loc $join_tax_loc ";
82 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
85 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
86 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
87 die "agent not found" unless $agent;
88 $agentname = $agent->agent;
89 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
92 # my ( $location_sql, @location_param ) = FS::cust_pkg->location_sql;
93 # $where .= " AND $location_sql";
94 #my @taxparam = ( 'itemdesc', @location_param );
95 # now something along the lines of geocode matching ?
96 #$where .= FS::cust_pkg->_location_sql_where('cust_tax_location');;
97 my @taxparam = ( 'itemdesc', 'tax_rate_location.state', 'tax_rate_location.county', 'tax_rate_location.city', 'cust_bill_pkg_tax_rate_location.locationtaxid' );
99 my $select = 'DISTINCT itemdesc,locationtaxid,tax_rate_location.state,tax_rate_location.county,tax_rate_location.city';
105 foreach my $t (qsearch({ table => 'cust_bill_pkg',
107 hashref => { pkgpart => 0 },
108 addl_from => $addl_from,
113 my @params = map { my $f = $_; $f =~ s/.*\.//; $f } @taxparam;
114 my $label = join('~', map { $t->$_ } @params);
115 $label = 'Tax'. $label if $label =~ /^~/;
116 unless ( exists( $taxes{$label} ) ) {
117 my ($baselabel, @trash) = split /~/, $label;
119 $taxes{$label}->{'label'} = join(', ', split(/~/, $label) );
120 $taxes{$label}->{'url_param'} =
121 join(';', map { "$_=". uri_escape($t->$_) } @params);
123 my $taxwhere = "FROM cust_bill_pkg $addl_from $where AND payby != 'COMP' ".
124 "AND ". join( ' AND ', map { "( $_ = ? OR ? = '' AND $_ IS NULL)" } @taxparam );
126 my $sql = "SELECT SUM(amount) $taxwhere AND cust_bill_pkg.pkgnum = 0";
128 my $x = scalar_sql($t, [ map { $_, $_ } @params ], $sql );
130 $taxes{$label}->{'tax'} += $x;
132 my $creditfrom = " JOIN cust_credit_bill_pkg USING (billpkgnum,billpkgtaxratelocationnum) ";
133 my $creditwhere = "FROM cust_bill_pkg $addl_from $creditfrom $where ".
134 "AND payby != 'COMP' ".
135 "AND ". join( ' AND ', map { "( $_ = ? OR ? = '' AND $_ IS NULL)" } @taxparam );
137 $sql = "SELECT SUM(cust_credit_bill_pkg.amount) ".
138 " $creditwhere AND cust_bill_pkg.pkgnum = 0";
140 my $y = scalar_sql($t, [ map { $_, $_ } @params ], $sql );
142 $taxes{$label}->{'credit'} += $y;
144 unless ( exists( $taxes{$baselabel} ) ) {
146 $basetaxes{$baselabel}->{'label'} = $baselabel;
147 $basetaxes{$baselabel}->{'url_param'} = "itemdesc=$baselabel";
148 $basetaxes{$baselabel}->{'base'} = 1;
152 $basetaxes{$baselabel}->{'tax'} += $x;
153 $basetaxes{$baselabel}->{'credit'} += $y;
157 # calculate customer-exemption for this tax
158 # calculate package-exemption for this tax
159 # calculate monthly exemption (texas tax) for this tax
160 # count up all the cust_tax_exempt_pkg records associated with
161 # the actual line items.
168 foreach my $tax ( sort { $a cmp $b } keys %taxes ) {
169 my ($base, @trash) = split '~', $tax;
170 my $basetax = delete( $basetaxes{$base} );
172 if ( $basetax->{tax} == $taxes{$tax}->{tax} ) {
173 $taxes{$tax}->{base} = 1;
175 push @taxes, $basetax;
178 push @taxes, $taxes{$tax};
191 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
192 #to FS::Report or FS::Record or who the fuck knows where)
194 my( $r, $param, $sql ) = @_;
195 my $sth = dbh->prepare($sql) or die dbh->errstr;
196 $sth->execute( map $r->$_(), @$param )
197 or die "Unexpected error executing statement $sql: ". $sth->errstr;
198 $sth->fetchrow_arrayref->[0] || 0;
201 my $dateagentlink = "begin=$beginning;end=$ending";
202 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
203 if length($agentname);
204 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";