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"!);
12 my $XLS = IO::String->new($output);
13 my $workbook = $format->{class}->new($XLS)
14 or die "Error opening .xls file: $!";
16 my $worksheet = $workbook->add_worksheet('Summary');
38 foreach (keys %format) {
39 my %f = (%default, %{$format{$_}});
40 $format{$_} = $workbook->add_format(%f);
41 $format{"m_$_"} = $workbook->add_format(%f);
47 my $thisrow = shift @cells;
48 for my $cell (@$thisrow) {
50 # placeholder, so increment $c so that we write to the correct place
56 $f = 'header' if $row->{header} or $cell->{header};
57 $f = 'money' if $cell->{format} eq 'money';
58 if ( $cell->{rowspan} > 1 or $cell->{colspan} > 1 ) {
59 my $range = xl_range_formula(
61 $r, $r - 1 + ($cell->{rowspan} || 1),
62 $c, $c - 1 + ($cell->{colspan} || 1)
64 #warn "merging $range\n";
65 $worksheet->merge_range($range, $cell->{value}, $format{"m_$f"});
67 #warn "writing ".xl_rowcol_to_cell($r, $c)."\n";
68 $worksheet->write( $r, $c, $cell->{value}, $format{$f} );
76 http_header('Content-Length' => length($output));
80 <& /elements/header.html, $title &>
81 % my $myself = $cgi->self_url;
82 <P ALIGN="right" CLASS="noprint">
83 Download full reports<BR>
84 as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
85 % # as <A HREF="<% "$myself;_type=csv" %>">CSV file</A> # is this still needed?
87 <style type="text/css">
89 background-color: #f8f8f8;
90 border: 1px solid black;
96 .total * { background-color: #f5f6be; }
97 .shaded * { background-color: #c8c8c8; }
98 .totalshaded * { background-color: #bfc094; }
100 <table class="report" width="100%" cellspacing=0>
101 % foreach my $rowinfo (@rows) {
102 <tr<% $rowinfo->{class} ? ' class="'.$rowinfo->{class}.'"' : ''%>>
103 % my $thisrow = shift @cells;
104 % foreach my $cell (@$thisrow) {
105 % next if !ref($cell); # placeholders
106 % my $td = $cell->{header} ? 'th' : 'td';
108 % $style .= " rowspan=".$cell->{rowspan} if $cell->{rowspan} > 1;
109 % $style .= " colspan=".$cell->{colspan} if $cell->{colspan} > 1;
110 <<%$td%><%$style%>><% $cell->{value} |h %></<%$td%>>
116 <& /elements/footer.html &>
121 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
123 my ($agentnum,$sel_agent);
124 if ( $cgi->param('agentnum') eq 'all' ) {
127 elsif ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
129 $sel_agent = qsearchs('agent', { 'agentnum' => $agentnum } );
130 die "agentnum $agentnum not found!" unless $sel_agent;
132 my $title = $sel_agent ? $sel_agent->agent.' ' : '';
134 my ($refnum,$sel_part_referral);
135 if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
137 $sel_part_referral = qsearchs('part_referral', { 'refnum' => $refnum } );
138 die "refnum $refnum not found!" unless $sel_part_referral;
140 $title .= $sel_part_referral->referral.' '
141 if $sel_part_referral;
143 $title .= 'Customer Accounting Summary Report';
145 my @cust_classnums = grep /^\d+$/, $cgi->param('cust_classnum');
147 my @items = ('netsales', 'cashflow');
148 my @params = ( [], [] );
150 if ( $cgi->param('setuprecur') ) {
152 # instead of 'cashflow' (payments - refunds), use 'receipts'
153 # (applied payments), because it's divisible into setup and recur.
154 @items = ('netsales', 'receipts', 'netsales', 'receipts');
156 [ setuprecur => 'setup' ],
157 [ setuprecur => 'setup' ],
158 [ setuprecur => 'recur' ],
159 [ setuprecur => 'recur' ],
163 my @cross_params = ();
166 my $status = $cgi->param('status');
167 die "invalid status" unless $status =~ /^\w+|$/;
170 foreach (qw(agentnum refnum status)) {
171 if ( defined $cgi->param($_) ) {
172 $search_hash{$_} = $cgi->param($_);
175 $search_hash{'classnum'} = [ $cgi->param('cust_classnum') ]
176 if $cgi->param('cust_classnum');
178 my $query = FS::cust_main::Search->search(\%search_hash);
179 my @custs = qsearch($query);
181 foreach my $cust_main ( @custs ) {
182 # XXX should do this in the qsearch
183 next unless ($status eq '' || $status eq $cust_main->status);
184 next unless ($agentnum == 0 || $cust_main->agentnum eq $agentnum);
185 next unless ($refnum == 0 || $cust_main->refnum eq $refnum);
187 push @custnames, $cust_main->name;
189 push @cross_params, [ ('custnum' => $cust_main->custnum) ];
195 cross_params => \@cross_params,
196 agentnum => $agentnum,
199 for ( qw(start_month start_year end_month end_year) ) {
200 if ( $cgi->param($_) =~ /^(\d+)$/ ) {
205 warn Dumper(OPTIONS => \%opt) if $cgi->param('debug');
206 my $report = FS::Report::Table::Monthly->new(%opt);
207 my $data = $report->data;
208 warn Dumper(DATA => $data) if $cgi->param('debug') >= 2;
212 my @rows; # hashes of row info
213 my @cells; # arrayrefs of cell info
214 # We use Excel currency format, but not Excel dates, because
215 # these are whole months and there's no nice way to express that.
216 # This is the historical behavior for monthly reports.
221 { header => 1, rowspan => 2, colspan => ($setuprecur ? 2 : 1) },
222 ($setuprecur ? '' : ()),
224 { header => 1, colspan => 2, value => time2str('%b %Y', $_) },
226 } @{ $data->{speriod} }
228 my $ncols = scalar(@{ $data->{speriod} });
232 ($setuprecur ? '' : ()),
234 ( { header => 1, value => mt('Billed') },
235 { header => 1, value => mt('Paid') }
239 # use PDL; # ha ha, I just might.
241 foreach my $name (@custnames) { # correspond to cross_params
242 my $skip = 1; # skip the customer iff ALL of their values are zero
243 for my $subrow (0..($setuprecur ? 1 : 0)) { # the setup/recur axis
244 push @rows, { class => $subrow ? 'shaded' : '' };
246 if ( $subrow == 0 ) {
251 rowspan => ($setuprecur ? 2 : 1) };
258 { value => $subrow ? mt('recurring') : mt('setup'),
261 for my $col (0..$ncols-1) { # the month
262 for my $subcol (0..1) { # the billed/paid axis
263 my $item = $subrow * 2 + $subcol;
264 my $value = $data->{data}[$item][$col][$row];
265 $skip = 0 if abs($value) > 0.005;
266 push @thisrow, { value => sprintf('%0.2f', $value), format => 'money' };
267 $total[( ($ncols * $subrow) + $col ) * 2 + $subcol] += $value;
270 push @cells, \@thisrow;
273 # all values are zero--remove the rows we just added
283 for my $subrow (0..($setuprecur ? 1 : 0)) {
284 push @rows, { class => ($subrow ? 'totalshaded' : 'total') };
286 if ( $subrow == 0 ) {
288 { value => mt('Total'),
290 rowspan => ($setuprecur ? 2 : 1), };
296 { value => $subrow ? mt('recurring') : mt('setup'),
299 for my $col (0..($ncols * 2)-1) { # month and billed/paid axis
300 my $value = $total[($subrow * $ncols * 2) + $col];
301 push @thisrow, { value => sprintf('%0.2f', $value), format => 'money' };
303 push @cells, \@thisrow;
306 if ( $cgi->param('debug') >= 3 ) {
307 warn Dumper(\@rows, \@cells);
310 my $title = 'Customer Accounting Summary';