5 include('elements/report.html',
7 'title' => 'Page title',
9 'data' => [ \@item1 \@item2 ... ],
11 #these run parallel to items, and can be given as hashes
12 'row_labels' => \@row_labels, #required
13 'colors' => \@colors, #required
14 'bgcolors' => \@bgcolors, #optional
15 'graph_labels' => \@graph_labels, #defaults to row_labels
17 'links' => \@links, #optional
18 'no_graph' => \@no_graph, #optional
20 #these run parallel to the elements of each @item
21 'col_labels' => \@col_labels, #required
22 'axis_labels' => \@axis_labels, #defaults to col_labels
26 'graph_type' => 'LinesPoints', #can be 'none' for no graph
28 'sprintf' => '%u', #sprintf format, overrides default %.2f
32 About @links: Each element must be an arrayref, corresponding to an element of
33 @items. Within the array, the first element is a URL prefix, and the rest
34 are suffixes corresponding to data elements. These will be joined without
35 any delimiter and linked from the elements in @data.
38 % if ( $cgi->param('_type') =~ /^(csv)$/ ) {
40 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
41 % #http_header('Content-Type' => 'text/plain' );
42 % http_header('Content-Type' => 'text/csv');
43 % http_header('Content-Disposition' => "attachment;filename=$filename.csv");
45 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
46 % 'eol' => "\n", #"\015\012", #"\012"
49 % $csv->combine('', @col_labels, $opt{'nototal'} ? () : 'Total');
53 % my @bottom_total = ();
55 % foreach ( @items ) {
58 % my @row = map { sprintf($sprintf, $_) } @{ shift(@data) };
59 % my $total = sum(@row);
60 % push @row, sprintf($sprintf, $total) unless $opt{'nototal'};
61 % unless ($opt{'no_graph'}[$row]) {
63 % $bottom_total[$col++] += $_;
66 % $csv->combine(shift(@row_labels), @row);
71 % if ( $opt{'bottom_total'} ) {
74 % map { sprintf($sprintf, $_) } @bottom_total,
81 % } elsif ( $cgi->param('_type') =~ /(xls)$/ ) {
82 % #false laziness w/ search/elements/search-xls
83 % my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
84 % $filename .= $format->{extension};
86 % http_header('Content-Type' => $format->{mime_type} );
87 % http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
90 % my $XLS = new IO::Scalar \$output;
91 % my $workbook = $format->{class}->new($XLS)
92 % or die "Error opening .xls file: $!";
94 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
96 % my($row,$col) = (0,0);
98 % foreach ('', @col_labels, ($opt{'nototal'} ? () : 'Total') ) {
100 % $worksheet->write($row, $col++, $header)
103 % my @bottom_total = ();
104 % foreach ( @items ) {
108 % $worksheet->write( $row, $col++, shift( @row_labels ) );
109 % foreach ( @{ shift( @data ) } ) {
111 % $bottom_total[$col-1] += $_ unless $opt{no_graph}[$row];
112 % $worksheet->write_number($row, $col++, sprintf($sprintf, $_) );
114 % if ( !$opt{'nototal'} ) {
115 % $bottom_total[$col-1] += $total unless $opt{no_graph}[$row];
116 % $worksheet->write_number($row, $col++, sprintf($sprintf, $total) );
121 % if ( $opt{'bottom_total'} ) {
123 % $worksheet->write($row, $col++, 'Total');
124 % $worksheet->write_number($row, $col++, sprintf($sprintf, $_)) foreach @bottom_total;
127 % $workbook->close();# or die "Error creating .xls file: $!";
129 % http_header('Content-Length' => length($output) );
130 % $m->print($output);
132 % } elsif ( $cgi->param('_type') eq 'png' ) {
133 % # delete any items that shouldn't be on the graph
134 % if ( my $no_graph = $opt{'no_graph'} ) {
136 % while (@$no_graph) {
137 % if ( shift @$no_graph ) {
138 % splice @data, $i, 1;
139 % splice @{$opt{'graph_labels'}}, $i, 1;
140 % splice @{$opt{'colors'}}, $i, 1;
141 % $i--; # because everything is shifted down
146 % my $graph_type = 'LinesPoints';
147 % if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain|Bars)$/ ) {
150 % my $class = "Chart::$graph_type";
152 % my $chart = $class->new(976,384);
153 % # the chart area itself is 900 pixels wide, and the date labels are ~60 each.
154 % # staggered, we can fit about 28 of them.
155 % # they're about 12 pixels high, so vertically, we can fit about 60 (allowing
156 % # space for them to be readable).
157 % # after that we have to start skipping labels. also remove the dots, since
158 % # they're just a blob at that point.
159 % my $num_labels = scalar(@{ $opt{axis_labels} });
160 % my %chart_opt = %{ $opt{chart_options} || {} };
161 % if ( $num_labels > 28 ) {
162 % $chart_opt{x_ticks} = 'vertical';
163 % if ( $num_labels > 60 ) {
164 % $chart_opt{skip_x_ticks} = int($num_labels / 60) + 1;
165 % $chart_opt{pt_size} = 1;
171 % 'legend' => 'bottom',
173 % map { my $color = $_;
175 % [ map hex($_), unpack 'a2a2a2', $color ]
177 % @{ $opt{'colors'} }
179 % 'grey_background' => 'white',
180 % 'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
182 % 'legend_labels' => $opt{'graph_labels'},
187 % http_header('Content-Type' => 'image/png' );
188 % http_header('Cache-Control' => 'no-cache' );
190 % $chart->_set_colors();
192 <% $chart->scalar_png([ $opt{'axis_labels'}, @data ]) %>
195 % # image and download links should use the cached data
196 % # just directly reference this component
197 % my $myself = $p.'graph/elements/report.html?session='.$session;
199 <% include('/elements/header.html', $opt{'title'} ) %>
200 % unless ( $opt{'graph_type'} eq 'none' ) {
202 <IMG SRC="<% "$myself;_type=png" %>" WIDTH="976" HEIGHT="384"
203 STYLE="page-break-after:always;">
205 <P ALIGN="right" CLASS="noprint">
207 % unless ( $opt{'disable_download'} ) {
208 Download full results<BR>
209 as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
210 as <A HREF="<% "$myself;_type=csv" %>">CSV file</A></P>
214 %# indexed by item, then by entry (the element indices of @{$data[$i]}).
217 % my $num_entries = scalar(@col_labels);
218 % my $num_items = scalar(@items);
219 % $cell[0] = ['']; #top left corner
220 % foreach my $column ( @col_labels ) {
221 % $column =~ s/ /\<BR\>/;
222 % push @{$cell[0]}, $column;
224 % if ( ! $opt{'nototal'} ) {
226 % push @{$cell[0]}, emt('Total');
229 % # i for item, e for entry
231 % my @bottom_total = map {0} @col_labels;
232 % foreach my $row ( @items ) {
234 % my $color = shift @{ $opt{'colors'} };
235 % my $bgcolor = $opt{'bgcolors'} ? (shift @{ $opt{'bgcolors'} }) : 'ffffff';
236 % push @styles, ".i$i { text-align: right; color: #$color; background: #$bgcolor; }";
237 % #create the data row
238 % my $links = shift @{$opt{'links'}} || [''];
239 % my $link_prefix = shift @$links;
240 % $link_prefix = '<A CLASS="cell" HREF="'.$link_prefix if $link_prefix;
241 % my $label = shift @row_labels;
242 % $cell[$i] = [ $label ];
244 % my $data_row = $data[$i-1];
245 %# my $data_row = shift @data;
246 % if ( ! $opt{'nototal'} ) {
247 % push @$data_row, sum(@$data_row);
250 % foreach ( @$data_row ) {
252 % $entry = $money_char . sprintf($sprintf, $entry);
253 % $entry = $link_prefix . shift(@$links) . "\">$entry</A>" if $link_prefix;
254 % push @{$cell[$i]}, $entry;
255 % $bottom_total[$e++] += $_ unless $opt{no_graph}[$i-1];
259 % if ( $opt{'bottom_total'} ) {
260 % # it's an extra item
262 % push @styles, ".i$i { text-align: right; background-color: #f5f6be; }";
263 % my $links = $opt{'bottom_link'} || [];
264 % my $link_prefix = shift @$links;
265 % $link_prefix = '<A CLASS="cell" HREF="'.$link_prefix if $link_prefix;
266 % $cell[$i] = [ emt('Total') ];
267 % for (my $e = 0; $e < $num_entries + 1; $e++) {
268 % my $entry = $bottom_total[$e];
269 % $entry = $money_char . sprintf($sprintf, $entry);
270 % $entry = $link_prefix . shift(@$links) . "\">$entry</A>" if $link_prefix;
271 % push @{$cell[$i]}, $entry;
275 <STYLE type="text/css">
277 color: inherit !important;
282 <% join("\n", @styles) %>
289 % if ( ! $opt{'nototal'} ) {
290 .e<% $num_entries %> {
292 background-color: #f5f6be;
302 <% include('/elements/table.html', 'f8f8f8') %>
303 % if ( $opt{'transpose'} ) {
304 % for ( my $e = 0; $e < $num_entries + 1; $e++ ) {
306 % for ( my $i = 0; $i < $num_items + 1; $i++ ) {
307 <TD CLASS="<%"cell i$i e$e"%>"><% $cell[$i][$e] %></TD>
312 % } else { #!transpose
314 % for (my $i = 0; $i < $num_items + 1; $i++) {
316 % for (my $e = 0; $e < $num_entries + 1; $e++) {
317 <TD CLASS="<%"cell i$i e$e"%>"><% $cell[$i][$e] %></TD>
324 <% include('/elements/footer.html') %>
330 # load from cache if possible, to avoid recalculating
331 if ( $cgi->param('session') =~ /^(\d+)$/ ) {
333 %opt = %{ $m->cache->get($session) };
336 $session = sprintf("%010d", random_id(10));
337 $m->cache->set($session, \%opt, '1h');
340 my $sprintf = $opt{'sprintf'} || '%.2f';
342 my $conf = new FS::Conf;
343 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
345 my @items = @{ $opt{'items'} };
347 foreach my $other (qw( col_labels row_labels graph_labels axis_labels colors links )) {
348 if ( ref($opt{$other}) eq 'HASH' ) {
349 $opt{$other} = [ map $opt{$other}{$_}, @items ];
353 my @col_labels = @{$opt{'col_labels'}};
354 my @row_labels = @{$opt{'row_labels'}};
355 my @data = @{$opt{'data'}};
357 $opt{'axis_labels'} ||= $opt{'col_labels'};
358 $opt{'graph_labels'} ||= $opt{'row_labels'};
360 my $filename = $cgi->url(-relative => 1);
361 $filename =~ s/\.(cgi|html)$//;