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 # allow anything in here; FS::Report::Tax will treat it as unsafe
28 if ( length($cgi->param('taxname')) ) {
29 $params{taxname} = $cgi->param('taxname');
31 die "taxname required";
35 my $report = FS::Report::Tax->report_internal(%params);
36 my @rows = $report->table; # array of hashrefs
38 my %pkgclass_name = map { $_->classnum, $_->classname } qsearch('pkg_class');
39 $pkgclass_name{''} = 'Unclassified';
41 my $override = (scalar(@rows) >= 65536 ? 'XLSX' : '');
42 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
43 my $filename = 'report_tax'.$format->{extension};
45 http_header('Content-Type' => $format->{mime_type});
46 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
49 my $XLS = new IO::Scalar \$data;
50 my $workbook = $format->{class}->new($XLS)
51 or die "Error opening .xls file: $!";
53 # hardcoded formats, this could be handled better
54 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
89 num_format=> 8, # ($#,##0.00_);[Red]($#,##0.00)
95 num_format=> 10, # 0.00%
111 currency_outside => {
117 num_format=> 8, # ($#,##0.00_);[Red]($#,##0.00)
126 30, (13) x 6, 3, 7.5, 3, 11, 11, 3, 11, 3, 11
129 my @format = ( {}, {}, {} ); # white row, gray row, yellow (totals) row
130 foreach (keys(%formatdef)) {
131 my %f = (%default, %{$formatdef{$_}});
132 $format[0]->{$_} = $workbook->add_format(%f);
133 $format[1]->{$_} = $workbook->add_format(bg_color => $light_gray, %f);
134 $format[2]->{$_} = $workbook->add_format(bg_color => 'yellow',
138 my $ws = $workbook->add_worksheet('Sales and Tax');
141 $ws->merge_range(0, 0, 0, 14, $report->title, $format[0]->{title});
147 my $colhead = $format[0]->{colhead};
149 $ws->merge_range($y, 1, $y, 6, 'Sales', $colhead);
150 $ws->merge_range($y, 7, $y+1, 9, 'Rate', $colhead);
151 $ws->merge_range($y, 10, $y, 16, 'Tax', $colhead);
154 $colhead = $format[0]->{colhead_small};
155 $ws->write($y, 1, [ 'Total',
160 'Taxable' ], $colhead);
161 $ws->write($y, 10, 'Estimated', $colhead);
162 $ws->write($y, 11, 'Invoiced', $colhead);
163 $ws->write($y, 13, 'Credited', $colhead);
164 $ws->write($y, 15, 'Net due', $colhead);
165 $ws->write($y, 16, 'Collected',$colhead);
170 my $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
172 foreach my $row (@rows) {
174 if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
176 if ( $params{breakdown}->{pkgclass} ) {
177 $ws->merge_range($y, 0, $y, 15,
178 $pkgclass_name{$row->{pkgclass}},
179 $format[0]->{sectionhead}
185 my $f = $format[$rownum % 2];
186 if ( $row->{total} ) {
189 $ws->write($y, $x, $row->{label}, $f->{rowhead});
191 foreach (qw(sales exempt_cust exempt_pkg exempt_monthly sales_credited taxable)) {
192 $ws->write($y, $x, $row->{$_} || 0, $f->{currency});
195 $ws->write_string($y, $x, " \N{U+00D7} ", $f->{bigmath}); # MULTIPLICATION SIGN
197 my $rate = $row->{rate};
198 $rate = $rate / 100 if $rate =~ /^[\d\.]+$/;
199 $ws->write($y, $x, $rate, $f->{number});
201 $ws->write_string($y, $x, " = ", $f->{bigmath});
203 my $estimated = $row->{estimated} || 0;
204 $estimated = '' if $rate eq 'variable';
205 $ws->write($y, $x, $estimated, $f->{currency});
207 $ws->write($y, $x, $row->{tax} || 0, $f->{currency});
209 $ws->write_string($y, $x, " \N{U+2212} ", $f->{bigmath}); # MINUS SIGN
211 $ws->write($y, $x, $row->{tax_credited} || 0, $f->{currency});
213 $ws->write_string($y, $x, " = ", $f->{bigmath});
215 $ws->write($y, $x, $row->{tax} - $row->{tax_credited}, $f->{currency});
217 $ws->write($y, $x, $row->{tax_paid} || 0, $f->{currency});
224 # at the end of everything
225 if ( $report->{out_sales} > 0 ) {
227 $ws->set_row($y, 30); # height
228 $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside});
229 $ws->write($y, 1, $report->{out_sales}, $f->{currency_outside});
234 for my $x (0..scalar(@widths)-1) {
235 $ws->set_column($x, $x, $widths[$x]);
238 # do the same for the credit worksheet
239 $ws = $workbook->add_worksheet('Credits');
241 my $title = $report->title;
242 $title =~ s/Tax Report/Credits/;
244 $ws->merge_range(0, 0, 0, 14, $title, $format[0]->{title});
245 $ws->set_row(0, 30); # height
250 $colhead = $format[0]->{colhead};
252 $ws->merge_range($y, 1, $y+1, 1, 'Total', $colhead);
253 $ws->merge_range($y, 2, $y, 4, 'Applied to', $colhead);
256 $colhead = $format[0]->{colhead_small};
257 $ws->write($y, 2, [ 'Taxable sales',
265 $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
267 foreach my $row (@rows) {
269 if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
271 if ( $params{breakdown}->{pkgclass} ) {
272 $ws->merge_range($y, 0, $y, 4,
273 $pkgclass_name{$row->{pkgclass}},
274 $format[0]->{sectionhead}
280 my $f = $format[$rownum % 2];
281 if ( $row->{total} ) {
284 $ws->write($y, $x, $row->{label}, $f->{rowhead});
286 foreach (qw(credits sales_credited exempt_credited tax_credited)) {
287 $ws->write($y, $x, $row->{$_} || 0, $f->{currency});
296 if ( $report->{out_credit} > 0 ) {
298 $ws->set_row($y, 30); # height
299 $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside});
300 $ws->write($y, 1, $report->{out_credit}, $f->{currency_outside});
306 $ws->set_column($x, $x, $widths[$x]);
312 http_header('Content-Length' => length($data));