1 % if ( $cgi->param('_type') =~ /(xls)$/ ) {
3 # egregious false laziness w/ search/report_tax-xls.cgi
4 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
5 my $filename = $cgi->url(-relative => 1);
6 $filename =~ s/\.html$//;
7 $filename .= $format->{extension};
8 http_header('Content-Type' => $format->{mime_type});
9 http_header('Content-Disposition' => qq!attachment;filename="$filename"!);
13 my $XLS = IO::String->new($output);;
14 my $workbook = $format->{class}->new($XLS)
15 or die "Error opening .xls file: $!";
17 my $worksheet = $workbook->add_worksheet('Summary');
39 foreach (keys %format) {
40 my %f = (%default, %{$format{$_}});
41 $format{$_} = $workbook->add_format(%f);
42 $format{"m_$_"} = $workbook->add_format(%f);
48 my $thisrow = shift @cells;
49 for my $cell (@$thisrow) {
51 # placeholder, so increment $c so that we write to the correct place
57 $f = 'header' if $row->{header} or $cell->{header};
58 $f = 'money' if $cell->{format} eq 'money';
59 if ( $cell->{rowspan} > 1 or $cell->{colspan} > 1 ) {
60 my $range = xl_range_formula(
62 $r, $r - 1 + ($cell->{rowspan} || 1),
63 $c, $c - 1 + ($cell->{colspan} || 1)
65 #warn "merging $range\n";
66 $worksheet->merge_range($range, $cell->{value}, $format{"m_$f"});
68 #warn "writing ".xl_rowcol_to_cell($r, $c)."\n";
69 $worksheet->write( $r, $c, $cell->{value}, $format{$f} );
77 http_header('Content-Length' => length($output));
81 <& /elements/header.html, $title &>
82 % my $myself = $cgi->self_url;
83 <P ALIGN="right" CLASS="noprint">
84 Download full reports<BR>
85 as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
86 % # as <A HREF="<% "$myself;_type=csv" %>">CSV file</A> # is this still needed?
88 <style type="text/css">
90 background-color: #f8f8f8;
91 border: 1px solid black;
97 .total * { background-color: #f5f6be; }
98 .shaded * { background-color: #c8c8c8; }
99 .totalshaded * { background-color: #bfc094; }
101 <table class="report" width="100%" cellspacing=0>
102 % foreach my $rowinfo (@rows) {
103 <tr<% $rowinfo->{class} ? ' class="'.$rowinfo->{class}.'"' : ''%>>
104 % my $thisrow = shift @cells;
105 % foreach my $cell (@$thisrow) {
106 % next if !ref($cell); # placeholders
107 % my $td = $cell->{header} ? 'th' : 'td';
109 % $style .= " rowspan=".$cell->{rowspan} if $cell->{rowspan} > 1;
110 % $style .= " colspan=".$cell->{colspan} if $cell->{colspan} > 1;
111 <<%$td%><%$style%>><% $cell->{value} %></<%$td%>>
117 <& /elements/footer.html &>
122 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
124 my ($agentnum,$sel_agent);
125 if ( $cgi->param('agentnum') eq 'all' ) {
128 elsif ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
130 $sel_agent = qsearchs('agent', { 'agentnum' => $agentnum } );
131 die "agentnum $agentnum not found!" unless $sel_agent;
133 my $title = $sel_agent ? $sel_agent->agent.' ' : '';
135 my ($refnum,$sel_part_referral);
136 if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
138 $sel_part_referral = qsearchs('part_referral', { 'refnum' => $refnum } );
139 die "refnum $refnum not found!" unless $sel_part_referral;
141 $title .= $sel_part_referral->referral.' '
142 if $sel_part_referral;
144 $title .= 'Customer Accounting Summary Report';
147 @custs = qsearch('cust_main', {} );
149 my @items = ('netsales', 'cashflow');
150 my @params = ( [], [] );
152 if ( $cgi->param('setuprecur') ) {
154 # instead of 'cashflow' (payments - refunds), use 'receipts'
155 # (applied payments), because it's divisible into setup and recur.
156 @items = ('netsales', 'receipts', 'netsales', 'receipts');
158 [ setuprecur => 'setup' ],
159 [ setuprecur => 'setup' ],
160 [ setuprecur => 'recur' ],
161 [ setuprecur => 'recur' ],
165 my @cross_params = ();
168 my $status = $cgi->param('status');
169 die "invalid status" unless $status =~ /^\w+|$/;
171 foreach my $cust_main ( @custs ) {
172 # XXX should do this in the qsearch
173 next unless ($status eq '' || $status eq $cust_main->status);
174 next unless ($agentnum == 0 || $cust_main->agentnum eq $agentnum);
175 next unless ($refnum == 0 || $cust_main->refnum eq $refnum);
177 push @custnames, $cust_main->name;
179 push @cross_params, [ ('custnum' => $cust_main->custnum) ];
185 cross_params => \@cross_params,
186 agentnum => $agentnum,
189 for ( qw(start_month start_year end_month end_year) ) {
190 if ( $cgi->param($_) =~ /^(\d+)$/ ) {
195 warn Dumper(OPTIONS => \%opt) if $cgi->param('debug');
196 my $report = FS::Report::Table::Monthly->new(%opt);
197 my $data = $report->data;
198 warn Dumper(DATA => $data) if $cgi->param('debug') >= 2;
202 my @rows; # hashes of row info
203 my @cells; # arrayrefs of cell info
204 # We use Excel currency format, but not Excel dates, because
205 # these are whole months and there's no nice way to express that.
206 # This is the historical behavior for monthly reports.
211 { header => 1, rowspan => 2, colspan => ($setuprecur ? 2 : 1) },
212 ($setuprecur ? '' : ()),
214 { header => 1, colspan => 2, value => time2str('%b %Y', $_) },
216 } @{ $data->{speriod} }
218 my $ncols = scalar(@{ $data->{speriod} });
222 ($setuprecur ? '' : ()),
224 ( { header => 1, value => mt('Billed') },
225 { header => 1, value => mt('Paid') }
229 # use PDL; # ha ha, I just might.
231 foreach my $name (@custnames) { # correspond to cross_params
232 my $skip = 1; # skip the customer iff ALL of their values are zero
233 for my $subrow (0..($setuprecur ? 1 : 0)) { # the setup/recur axis
234 push @rows, { class => $subrow ? 'shaded' : '' };
236 if ( $subrow == 0 ) {
241 rowspan => ($setuprecur ? 2 : 1) };
248 { value => $subrow ? mt('recurring') : mt('setup'),
251 for my $col (0..$ncols-1) { # the month
252 for my $subcol (0..1) { # the billed/paid axis
253 my $item = $subrow * 2 + $subcol;
254 my $value = $data->{data}[$item][$col][$row];
255 $skip = 0 if abs($value) > 0.005;
256 push @thisrow, { value => sprintf('%0.2f', $value), format => 'money' };
257 $total[( ($ncols * $subrow) + $col ) * 2 + $subcol] += $value;
260 push @cells, \@thisrow;
263 # all values are zero--remove the rows we just added
273 for my $subrow (0..($setuprecur ? 1 : 0)) {
274 push @rows, { class => ($subrow ? 'totalshaded' : 'total') };
276 if ( $subrow == 0 ) {
278 { value => mt('Total'),
280 rowspan => ($setuprecur ? 2 : 1), };
286 { value => $subrow ? mt('recurring') : mt('setup'),
289 for my $col (0..($ncols * 2)-1) { # month and billed/paid axis
290 my $value = $total[($subrow * $ncols * 2) + $col];
291 push @thisrow, { value => sprintf('%0.2f', $value), format => 'money' };
293 push @cells, \@thisrow;
296 if ( $cgi->param('debug') >= 3 ) {
297 warn Dumper(\@rows, \@cells);
300 my $title = 'Customer Accounting Summary';