4 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
6 my $DEBUG = $cgi->param('debug') || 0;
8 my $conf = new FS::Conf;
10 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
13 beginning => $beginning,
16 $params{country} = $cgi->param('country');
17 $params{debug} = $DEBUG;
18 $params{breakdown} = { map { $_ => 1 } $cgi->param('breakdown') };
21 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
22 my $agent = FS::agent->by_key($1) or die "unknown agentnum $1";
23 $params{agentnum} = $1;
24 $agentname = $agent->agentname;
27 if ( $cgi->param('taxname') =~ /^([\w ]+)$/ ) {
28 $params{taxname} = $1;
30 die "taxname required";
34 my $report = FS::Report::Tax->report_internal(%params);
35 my @rows = $report->table; # array of hashrefs
37 my %pkgclass_name = map { $_->classnum, $_->classname } qsearch('pkg_class');
38 $pkgclass_name{''} = 'Unclassified';
40 my $override = (scalar(@rows) >= 65536 ? 'XLSX' : '');
41 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
42 my $filename = 'report_tax'.$format->{extension};
44 http_header('Content-Type' => $format->{mime_type});
45 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
48 my $XLS = new IO::Scalar \$data;
49 my $workbook = $format->{class}->new($XLS)
50 or die "Error opening .xls file: $!";
52 # hardcoded formats, this could be handled better
53 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
88 num_format=> 8, # ($#,##0.00_);[Red]($#,##0.00)
94 num_format=> 10, # 0.00%
110 currency_outside => {
116 num_format=> 8, # ($#,##0.00_);[Red]($#,##0.00)
125 30, (13) x 6, 3, 7.5, 3, 11, 11, 3, 11, 3, 11
128 my @format = ( {}, {}, {} ); # white row, gray row, yellow (totals) row
129 foreach (keys(%formatdef)) {
130 my %f = (%default, %{$formatdef{$_}});
131 $format[0]->{$_} = $workbook->add_format(%f);
132 $format[1]->{$_} = $workbook->add_format(bg_color => $light_gray, %f);
133 $format[2]->{$_} = $workbook->add_format(bg_color => 'yellow',
137 my $ws = $workbook->add_worksheet('Sales and Tax');
140 $ws->merge_range(0, 0, 0, 14, $report->title, $format[0]->{title});
146 my $colhead = $format[0]->{colhead};
148 $ws->merge_range($y, 1, $y, 6, 'Sales', $colhead);
149 $ws->merge_range($y, 7, $y+1, 9, 'Rate', $colhead);
150 $ws->merge_range($y, 10, $y, 16, 'Tax', $colhead);
153 $colhead = $format[0]->{colhead_small};
154 $ws->write($y, 1, [ 'Total',
159 'Taxable' ], $colhead);
160 $ws->write($y, 10, 'Estimated', $colhead);
161 $ws->write($y, 11, 'Invoiced', $colhead);
162 $ws->write($y, 13, 'Credited', $colhead);
163 $ws->write($y, 15, 'Net due', $colhead);
164 $ws->write($y, 16, 'Collected',$colhead);
169 my $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
171 foreach my $row (@rows) {
173 if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
175 if ( $params{breakdown}->{pkgclass} ) {
176 $ws->merge_range($y, 0, $y, 15,
177 $pkgclass_name{$row->{pkgclass}},
178 $format[0]->{sectionhead}
184 my $f = $format[$rownum % 2];
185 if ( $row->{total} ) {
188 $ws->write($y, $x, $row->{label}, $f->{rowhead});
190 foreach (qw(sales exempt_cust exempt_pkg exempt_monthly sales_credited taxable)) {
191 $ws->write($y, $x, $row->{$_} || 0, $f->{currency});
194 $ws->write_string($y, $x, " \N{U+00D7} ", $f->{bigmath}); # MULTIPLICATION SIGN
196 my $rate = $row->{rate};
197 $rate = $rate / 100 if $rate =~ /^[\d\.]+$/;
198 $ws->write($y, $x, $rate, $f->{number});
200 $ws->write_string($y, $x, " = ", $f->{bigmath});
202 my $estimated = $row->{estimated} || 0;
203 $estimated = '' if $rate eq 'variable';
204 $ws->write($y, $x, $estimated, $f->{currency});
206 $ws->write($y, $x, $row->{tax} || 0, $f->{currency});
208 $ws->write_string($y, $x, " \N{U+2212} ", $f->{bigmath}); # MINUS SIGN
210 $ws->write($y, $x, $row->{credit} || 0, $f->{currency});
212 $ws->write_string($y, $x, " = ", $f->{bigmath});
214 $ws->write($y, $x, $row->{tax} - $row->{credit}, $f->{currency});
216 $ws->write($y, $x, $row->{tax_paid} || 0, $f->{currency});
223 # at the end of everything
224 if ( $report->{outside} > 0 ) {
226 $ws->set_row($y, 30); # height
227 $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside});
228 $ws->write($y, 1, $report->{outside}, $f->{currency_outside});
233 for my $x (0..scalar(@widths)-1) {
234 $ws->set_column($x, $x, $widths[$x]);
237 # do the same for the credit worksheet
238 $ws = $workbook->add_worksheet('Credits');
240 my $title = $report->title;
241 $title =~ s/Tax Report/Credits/;
243 $ws->merge_range(0, 0, 0, 14, $title, $format[0]->{title});
244 $ws->set_row(0, 30); # height
249 $colhead = $format[0]->{colhead};
251 $ws->merge_range($y, 1, $y+1, 1, 'Total', $colhead);
252 $ws->merge_range($y, 2, $y, 4, 'Applied to', $colhead);
255 $colhead = $format[0]->{colhead_small};
256 $ws->write($y, 2, [ 'Taxable sales',
264 $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
266 foreach my $row (@rows) {
268 if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
270 if ( $params{breakdown}->{pkgclass} ) {
271 $ws->merge_range($y, 0, $y, 4,
272 $pkgclass_name{$row->{pkgclass}},
273 $format[0]->{sectionhead}
279 my $f = $format[$rownum % 2];
280 if ( $row->{total} ) {
283 $ws->write($y, $x, $row->{label}, $f->{rowhead});
285 foreach (qw(credits sales_credited exempt_credited tax_credited)) {
286 $ws->write($y, $x, $row->{$_} || 0, $f->{currency});
296 $ws->set_column($x, $x, $widths[$x]);
302 http_header('Content-Length' => length($data));