tax report improvements, #23449, #25935
[freeside.git] / httemplate / search / report_tax-xls.cgi
1 <%init>
2
3 die "access denied"
4   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
5
6 my $DEBUG = $cgi->param('debug') || 0;
7
8 my $conf = new FS::Conf;
9
10 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
11
12 my %params = (
13   beginning => $beginning,
14   ending    => $ending,
15 );
16 $params{country} = $cgi->param('country');
17 $params{debug}   = $DEBUG;
18 $params{breakdown} = { map { $_ => 1 } $cgi->param('breakdown') };
19
20 my $agentname;
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;
25 }
26
27 if ( $cgi->param('taxname') =~ /^([\w ]+)$/ ) {
28   $params{taxname} = $1;
29 } else {
30   die "taxname required";
31 }
32
33 # generate the report
34 my $report = FS::Report::Tax->report_internal(%params);
35 my @rows = $report->table; # array of hashrefs
36
37 my %pkgclass_name = map { $_->classnum, $_->classname } qsearch('pkg_class');
38 $pkgclass_name{''} = 'Unclassified';
39
40 my $override = (scalar(@rows) >= 65536 ? 'XLSX' : '');
41 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
42 my $filename = 'report_tax'.$format->{extension};
43
44 http_header('Content-Type' => $format->{mime_type});
45 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
46
47 my $data = '';
48 my $XLS = new IO::Scalar \$data;
49 my $workbook = $format->{class}->new($XLS)
50   or die "Error opening .xls file: $!";
51
52 # hardcoded formats, this could be handled better
53 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
54 my %formatdef = (
55   title => {
56     size      => 24,
57     align     => 'center',
58     bg_color  => 'silver',
59   },
60   sectionhead => {
61     size      => 11,
62     bold      => 1,
63     bg_color  => 'silver',
64   },
65   colhead => {
66     size      => 11,
67     bold      => 1,
68     align     => 'center',
69     valign    => 'vcenter',
70     text_wrap => 1,
71   },
72   colhead_small => {
73     size      => 8,
74     bold      => 1,
75     align     => 'center',
76     valign    => 'vcenter',
77     text_wrap => 1,
78   },
79   rowhead => {
80     size      => 11,
81     valign    => 'bottom',
82     text_wrap => 1,
83   },
84   currency => {
85     size      => 11,
86     align     => 'right',
87     valign    => 'bottom',
88     num_format=> 8, # ($#,##0.00_);[Red]($#,##0.00)
89   },
90   number  => {
91     size      => 11,
92     align     => 'right',
93     valign    => 'bottom',
94     num_format=> 10, # 0.00%
95   },
96   bigmath => {
97     size      => 12,
98     align     => 'center',
99     valign    => 'vcenter',
100     bold      => 1,
101   },
102 );
103 my %default = (
104   font      => 'Calibri',
105   border    => 1,
106 );
107 my @widths = ( #ick
108   18, (13) x 5, 3, 7.5, 3, 11, 11, 3, 11, 3, 11
109 );
110
111 my @format = ( {}, {}, {} ); # white row, gray row, yellow (totals) row
112 foreach (keys(%formatdef)) {
113   my %f = (%default, %{$formatdef{$_}});
114   $format[0]->{$_} = $workbook->add_format(%f);
115   $format[1]->{$_} = $workbook->add_format(bg_color => $light_gray, %f);
116   $format[2]->{$_} = $workbook->add_format(bg_color => 'yellow',
117                                            italic   => 1,
118                                            %f);
119 }
120 my $ws = $workbook->add_worksheet('taxreport');
121
122 # main title
123 $ws->merge_range(0, 0, 0, 14, $report->title, $format[0]->{title});
124 # excel position
125 my $x = 0;
126 my $y = 2;
127
128 my $colhead = $format[0]->{colhead};
129 # print header
130 $ws->merge_range($y, 1, $y, 5, 'Sales', $colhead);
131 $ws->merge_range($y, 6, $y+1, 8, 'Rate', $colhead);
132 $ws->merge_range($y, 9, $y, 14, 'Tax', $colhead);
133
134 $y++;
135 $colhead = $format[0]->{colhead_small};
136 $ws->write($y, 1, [ 'Total', 'Exempt customer', 'Exempt package', 'Monthly exemption',
137                     'Taxable' ], $colhead);
138 $ws->write($y, 9, 'Estimated', $colhead);
139 $ws->write($y, 10, 'Invoiced', $colhead);
140 $ws->write($y, 12, 'Credited', $colhead);
141 $ws->write($y, 14, 'Net due',  $colhead);
142 $y++;
143
144 # print data
145 my $rownum = 0;
146 my $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
147
148 foreach my $row (@rows) {
149   $x = 0;
150   if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
151     $rownum = 0;
152     if ( $params{breakdown}->{pkgclass} ) {
153       $ws->merge_range($y, 0, $y, 14,
154         $pkgclass_name{$row->{pkgclass}},
155         $format[0]->{sectionhead}
156       );
157       $y++;
158     }
159   }
160   # pick a format set
161   my $f = $format[$rownum % 2];
162   if ( $row->{total} ) {
163     $f = $format[2];
164   }
165   $ws->write($y, $x, $row->{label}, $f->{rowhead});
166   $x++;
167   foreach (qw(sales exempt_cust exempt_pkg exempt_monthly taxable)) {
168     $ws->write($y, $x, $row->{$_} || 0, $f->{currency});
169     $x++;
170   }
171   $ws->write_string($y, $x, " \N{U+00D7} ", $f->{bigmath}); # MULTIPLICATION SIGN
172   $x++;
173   my $rate = $row->{rate};
174   $rate = $rate / 100 if $rate =~ /^[\d\.]+$/;
175   $ws->write($y, $x, $rate, $f->{number});
176   $x++;
177   $ws->write_string($y, $x, " = ", $f->{bigmath});
178   $x++;
179   my $estimated = $row->{estimated} || 0;
180   $estimated = '' if $rate eq 'variable';
181   $ws->write($y, $x, $estimated, $f->{currency});
182   $x++;
183   $ws->write($y, $x, $row->{tax} || 0, $f->{currency});
184   $x++;
185   $ws->write_string($y, $x, " \N{U+2212} ", $f->{bigmath}); # MINUS SIGN
186   $x++;
187   $ws->write($y, $x, $row->{credit} || 0, $f->{currency});
188   $x++;
189   $ws->write_string($y, $x, " = ", $f->{bigmath});
190   $x++;
191   $ws->write($y, $x, $row->{tax} - $row->{credit}, $f->{currency});
192
193   $rownum++;
194   $y++;
195   $prev_row = $row;
196 }
197
198 # ewwwww...
199 for my $x (0..scalar(@widths)-1) {
200   $ws->set_column($x, $x, $widths[$x]);
201 }
202
203 $workbook->close;
204
205 http_header('Content-Length' => length($data));
206 $m->print($data);
207 </%init>