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 invoiced</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="<% $creditlink. $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 #shit, all sorts of false laxiness w/tax_rate::geneate_liability_report
69 my $conf = new FS::Conf;
70 my $money_char = $conf->config('money_char') || '$';
72 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
75 JOIN cust_bill USING ( invnum )
76 LEFT JOIN cust_main USING ( custnum )
79 my $join_loc = "LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum )";
80 my $join_tax_loc = "LEFT JOIN tax_rate_location USING ( taxratelocationnum )";
82 my $addl_from = " $join_cust $join_loc $join_tax_loc ";
84 my $where = "WHERE _date >= $beginning AND _date <= $ending ";
87 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
88 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
89 die "agent not found" unless $agent;
90 $agentname = $agent->agent;
91 $where .= ' AND cust_main.agentnum = '. $agent->agentnum;
94 #my @taxparam = ( 'itemdesc', 'tax_rate_location.state', 'tax_rate_location.county', 'tax_rate_location.city', 'cust_bill_pkg_tax_rate_location.locationtaxid' );
95 my @taxparams = qw( city county state locationtaxid );
96 my @params = ('itemdesc', @taxparams);
98 my $select = 'DISTINCT itemdesc,locationtaxid,tax_rate_location.state,tax_rate_location.county,tax_rate_location.city';
104 foreach my $t (qsearch({ table => 'cust_bill_pkg',
106 hashref => { pkgpart => 0 },
107 addl_from => $addl_from,
112 #my @params = map { my $f = $_; $f =~ s/.*\.//; $f } @taxparam;
113 my $label = join('~', map { $t->$_ } @params);
114 $label = 'Tax'. $label if $label =~ /^~/;
115 unless ( exists( $taxes{$label} ) ) {
116 my ($baselabel, @trash) = split /~/, $label;
118 $taxes{$label}->{'label'} = join(', ', split(/~/, $label) );
119 $taxes{$label}->{'url_param'} =
120 join(';', map { "$_=". uri_escape($t->$_) } @params);
122 my $payby_itemdesc_loc =
124 "AND ( itemdesc = ? OR ? = '' AND itemdesc IS NULL ) ".
125 "AND ". FS::tax_rate_location->location_sql( map { $_ => $t->$_ }
130 "FROM cust_bill_pkg $addl_from $where AND $payby_itemdesc_loc";
132 my $sql = "SELECT SUM(amount) $taxwhere AND cust_bill_pkg.pkgnum = 0";
134 my $x = scalar_sql($t, [ 'itemdesc', 'itemdesc' ], $sql );
136 $taxes{$label}->{'tax'} += $x;
139 "JOIN cust_credit_bill_pkg USING (billpkgnum,billpkgtaxratelocationnum)";
141 "FROM cust_bill_pkg $addl_from $creditfrom $where AND $payby_itemdesc_loc";
143 $sql = "SELECT SUM(cust_credit_bill_pkg.amount) ".
144 " $creditwhere AND cust_bill_pkg.pkgnum = 0";
146 my $y = scalar_sql($t, [ 'itemdesc', 'itemdesc' ], $sql );
148 $taxes{$label}->{'credit'} += $y;
150 unless ( exists( $taxes{$baselabel} ) ) {
152 $basetaxes{$baselabel}->{'label'} = $baselabel;
153 $basetaxes{$baselabel}->{'url_param'} = "itemdesc=$baselabel";
154 $basetaxes{$baselabel}->{'base'} = 1;
158 $basetaxes{$baselabel}->{'tax'} += $x;
159 $basetaxes{$baselabel}->{'credit'} += $y;
163 # calculate customer-exemption for this tax
164 # calculate package-exemption for this tax
165 # calculate monthly exemption (texas tax) for this tax
166 # count up all the cust_tax_exempt_pkg records associated with
167 # the actual line items.
174 foreach my $tax ( sort { $a cmp $b } keys %taxes ) {
175 my ($base, @trash) = split '~', $tax;
176 my $basetax = delete( $basetaxes{$base} );
178 if ( $basetax->{tax} == $taxes{$tax}->{tax} ) {
179 $taxes{$tax}->{base} = 1;
181 push @taxes, $basetax;
184 push @taxes, $taxes{$tax};
197 #false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
198 #to FS::Report or FS::Record or who the fuck knows where)
200 my( $r, $param, $sql ) = @_;
201 my $sth = dbh->prepare($sql) or die dbh->errstr;
202 $sth->execute( map $r->$_(), @$param )
203 or die "Unexpected error executing statement $sql: ". $sth->errstr;
204 $sth->fetchrow_arrayref->[0] || 0;
207 my $dateagentlink = "begin=$beginning;end=$ending";
208 $dateagentlink .= ';agentnum='. $cgi->param('agentnum')
209 if length($agentname);
210 my $baselink = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
211 my $creditlink = $p. "search/cust_credit_bill_pkg.html?$dateagentlink";