tax liability report adjustments, #25935, #26589, #27698
[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   rowhead_outside => {
103     size      => 11,
104     align     => 'left',
105     valign    => 'vcenter',
106     bg_color  => 'gray',
107     bold      => 1,
108     italic    => 1,
109   },
110   currency_outside => {
111     size      => 11,
112     align     => 'right',
113     valign    => 'vcenter',
114     bg_color  => 'gray',
115     italic    => 1,
116     num_format=> 8, # ($#,##0.00_);[Red]($#,##0.00)
117   },
118
119 );
120 my %default = (
121   font      => 'Calibri',
122   border    => 1,
123 );
124 my @widths = ( #ick
125   30, (13) x 5, 3, 7.5, 3, 11, 11, 3, 11, 3, 11
126 );
127
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',
134                                            italic   => 1,
135                                            %f);
136 }
137 my $ws = $workbook->add_worksheet('taxreport');
138
139 # main title
140 $ws->merge_range(0, 0, 0, 14, $report->title, $format[0]->{title});
141 # excel position
142 my $x = 0;
143 my $y = 2;
144
145 my $colhead = $format[0]->{colhead};
146 # print header
147 $ws->merge_range($y, 1, $y, 5, 'Sales', $colhead);
148 $ws->merge_range($y, 6, $y+1, 8, 'Rate', $colhead);
149 $ws->merge_range($y, 9, $y, 14, 'Tax', $colhead);
150
151 $y++;
152 $colhead = $format[0]->{colhead_small};
153 $ws->write($y, 1, [ 'Total', 'Exempt customer', 'Exempt package', 'Monthly exemption',
154                     'Taxable' ], $colhead);
155 $ws->write($y, 9, 'Estimated', $colhead);
156 $ws->write($y, 10, 'Invoiced', $colhead);
157 $ws->write($y, 12, 'Credited', $colhead);
158 $ws->write($y, 14, 'Net due',  $colhead);
159 $y++;
160
161 # print data
162 my $rownum = 0;
163 my $prev_row = { pkgclass => 'DUMMY PKGCLASS' };
164
165 foreach my $row (@rows) {
166   $x = 0;
167   if ( $row->{pkgclass} ne $prev_row->{pkgclass} ) {
168     $rownum = 0;
169     if ( $params{breakdown}->{pkgclass} ) {
170       $ws->merge_range($y, 0, $y, 14,
171         $pkgclass_name{$row->{pkgclass}},
172         $format[0]->{sectionhead}
173       );
174       $y++;
175     }
176   }
177   # pick a format set
178   my $f = $format[$rownum % 2];
179   if ( $row->{total} ) {
180     $f = $format[2];
181   }
182   $ws->write($y, $x, $row->{label}, $f->{rowhead});
183   $x++;
184   foreach (qw(sales exempt_cust exempt_pkg exempt_monthly taxable)) {
185     $ws->write($y, $x, $row->{$_} || 0, $f->{currency});
186     $x++;
187   }
188   $ws->write_string($y, $x, " \N{U+00D7} ", $f->{bigmath}); # MULTIPLICATION SIGN
189   $x++;
190   my $rate = $row->{rate};
191   $rate = $rate / 100 if $rate =~ /^[\d\.]+$/;
192   $ws->write($y, $x, $rate, $f->{number});
193   $x++;
194   $ws->write_string($y, $x, " = ", $f->{bigmath});
195   $x++;
196   my $estimated = $row->{estimated} || 0;
197   $estimated = '' if $rate eq 'variable';
198   $ws->write($y, $x, $estimated, $f->{currency});
199   $x++;
200   $ws->write($y, $x, $row->{tax} || 0, $f->{currency});
201   $x++;
202   $ws->write_string($y, $x, " \N{U+2212} ", $f->{bigmath}); # MINUS SIGN
203   $x++;
204   $ws->write($y, $x, $row->{credit} || 0, $f->{currency});
205   $x++;
206   $ws->write_string($y, $x, " = ", $f->{bigmath});
207   $x++;
208   $ws->write($y, $x, $row->{tax} - $row->{credit}, $f->{currency});
209
210   $rownum++;
211   $y++;
212   $prev_row = $row;
213 }
214
215 # at the end of everything
216 if ( $report->{outside} > 0 ) {
217   my $f = $format[0];
218   $ws->set_row($y, 30); # height
219   $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside});
220   $ws->write($y, 1, $report->{outside}, $f->{currency_outside});
221   $y++;
222 }
223
224 # ewwwww...
225 for my $x (0..scalar(@widths)-1) {
226   $ws->set_column($x, $x, $widths[$x]);
227 }
228
229 $workbook->close;
230
231 http_header('Content-Length' => length($data));
232 $m->print($data);
233 </%init>