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 'graph_labels' => \@graph_labels, #defaults to row_labels
16 'links' => \@links, #optional
18 #these run parallel to the elements of each @item
19 'col_labels' => \@col_labels, #required
20 'axis_labels' => \@axis_labels, #defaults to col_labels
24 'graph_type' => 'LinesPoints',
26 'sprintf' => '%u', #sprintf format, overrides default %.2f
30 About @links: Each element must be an arrayref, corresponding to an element of
31 @items. Within the array, the first element is a URL prefix, and the rest
32 are suffixes corresponding to data elements. These will be joined without
33 any delimiter and linked from the elements in @data.
36 % if ( $cgi->param('_type') =~ /^(csv)$/ ) {
38 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
39 % #http_header('Content-Type' => 'text/plain' );
40 % http_header('Content-Type' => 'text/csv');
41 % http_header('Content-Disposition' => "attachment;filename=$filename.csv");
43 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
44 % 'eol' => "\n", #"\015\012", #"\012"
47 % $csv->combine('', @col_labels, $opt{'nototal'} ? () : 'Total');
51 % my @bottom_total = ();
52 % foreach ( @items ) {
57 % shift( @row_labels ),
58 % map { $total += $_; $bottom_total[$col++] += $_; sprintf($sprintf, $_); }
59 % ( @{ shift( @data ) } ),
60 % ( $opt{'nototal'} ? () : sprintf($sprintf, $total) ),
62 % unless ( $opt{'nototal'} ) {
63 % $bottom_total[$col++] += $total;
69 % if ( $opt{'bottom_total'} ) {
72 % map { sprintf($sprintf, $_) } @bottom_total,
79 % } elsif ( $cgi->param('_type') =~ /(xls)$/ ) {
81 % #http_header('Content-Type' => 'application/excel' ); #eww
82 % http_header('Content-Type' => 'application/vnd.ms-excel' );
83 % #http_header('Content-Type' => 'application/msexcel' ); #alas
84 % http_header('Content-Disposition' => "attachment;filename=$filename.xls");
87 % my $XLS = new IO::Scalar \$output;
88 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
89 % or die "Error opening .xls file: $!";
91 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
95 % foreach ('', @col_labels, ($opt{'nototal'} ? () : 'Total') ) {
97 % $worksheet->write($r, $c++, $header)
100 % my @bottom_total = ();
101 % foreach ( @items ) {
105 % $worksheet->write( $r, $c++, shift( @row_labels ) );
106 % foreach ( @{ shift( @data ) } ) {
108 % $bottom_total[$c-1] += $_;
109 % $worksheet->write($r, $c++, sprintf($sprintf, $_) );
111 % unless ( $opt{'nototal'} ) {
112 % $bottom_total[$c-1] += $total;
113 % $worksheet->write($r, $c++, sprintf($sprintf, $total) );
118 % if ( $opt{'bottom_total'} ) {
120 % $worksheet->write($r, $c++, 'Total');
121 % $worksheet->write($r, $c++, sprintf($sprintf, $_)) foreach @bottom_total;
124 % $workbook->close();# or die "Error creating .xls file: $!";
126 % http_header('Content-Length' => length($output) );
129 % } elsif ( $cgi->param('_type') eq 'png' ) {
131 % my $graph_type = 'LinesPoints';
132 % if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain|Bars)$/ ) {
135 % my $class = "Chart::$graph_type";
137 % my $chart = $class->new(976,384);
142 % 'legend' => 'bottom',
144 % map { my $color = $_;
146 % [ map hex($_), unpack 'a2a2a2', $color ]
148 % @{ $opt{'colors'} }
150 % 'grey_background' => 'white',
151 % 'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
153 % 'legend_labels' => $opt{'graph_labels'},
157 % http_header('Content-Type' => 'image/png' );
159 % $chart->_set_colors();
161 <% $chart->scalar_png([ $opt{'axis_labels'}, @data ]) %>
165 <% include('/elements/header.html', $opt{'title'} ) %>
166 % unless ( $opt{'graph_type'} eq 'none' ) {
167 % $cgi->param('_type', 'png');
169 <IMG SRC="<% $cgi->self_url %>" WIDTH="976" HEIGHT="384">
173 % unless ( $opt{'disable_download'} ) {
174 % $cgi->param('_type', "xls" );
175 Download full results<BR>
176 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
177 % $cgi->param('_type', 'csv');
178 as <A HREF="<% $cgi->self_url %>">CSV file</A></P>
179 % $cgi->param('_type', "html" );
183 <% include('/elements/table.html', 'f8f8f8') %>
189 % foreach my $column ( @col_labels ) {
190 % $column =~ s/ /\<BR\>/; # working on a smarter way to do this
191 <TH><% $column %></TH>
194 % unless ( $opt{'nototal'} ) {
200 % my @bottom_total = ();
201 % foreach my $row ( @items ) {
203 % my $color = shift( @{ $opt{'colors'} } );
204 % my @links = @{ shift( @{ $opt{'links'} } ) };
205 % # $opt{'links'} is an array parallel to items.
206 % # Each element of that is an array containing a prefix,
207 % # followed by suffixes matched to the cells of the table.
208 % my $link_prefix = shift @links;
209 % $link_prefix = $link_prefix ? qq(<A HREF="$link_prefix) : ''; #"
210 % my $label = shift( @row_labels );
215 <FONT COLOR="#<% $color %>"><% $label %></FONT>
220 % foreach my $column ( @{ shift( @data ) } ) {
222 <TD ALIGN="right" BGCOLOR="#ffffff">
223 <% $link_prefix ? $link_prefix . shift(@links) . '">' : '' %><FONT COLOR="#<% $color %>"><% $money_char %><% sprintf($sprintf,, $column) %></FONT><% $link_prefix ? '</A>' : '' %>
227 % $bottom_total[$col++] += $column;
231 % unless ( $opt{'nototal'} ) {
232 <TD ALIGN="right" BGCOLOR="#f5f6be">
233 <% $link_prefix ? $link_prefix. shift(@links) . '">' : '' %><FONT COLOR="#<% $color %>"><% $money_char %><% sprintf($sprintf, $total) %></FONT><% $link_prefix ? '</A>' : '' %>
235 % $bottom_total[$col++] += $total;
242 % if ( $opt{'bottom_total'} ) {
245 % my @bottom_links = $opt{'bottom_link'} ? @{ $opt{'bottom_link'} } : ();
246 % my $prefix = shift(@bottom_links);
247 % pop @bottom_links if $opt{'nototal'};
248 % foreach my $total ( @bottom_total ) {
250 <TD ALIGN="right" BGCOLOR="#f5f6be">
252 ? '<A HREF="'. $prefix .shift(@bottom_links). '">'
254 %><% $money_char %><% sprintf($sprintf, $total) %><% $prefix ? '</A>' : '' %>
266 <% include('/elements/footer.html') %>
275 my $sprintf = $opt{'sprintf'} || '%.2f';
277 my $conf = new FS::Conf;
278 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
280 my @items = @{ $opt{'items'} };
282 foreach my $other (qw( col_labels row_labels graph_labels axis_labels colors links )) {
283 if ( ref($opt{$other}) eq 'HASH' ) {
284 $opt{$other} = [ map $opt{$other}{$_}, @items ];
288 my @col_labels = @{$opt{'col_labels'}};
289 my @row_labels = @{$opt{'row_labels'}};
290 my @data = @{$opt{'data'}};
292 $opt{'axis_labels'} ||= $opt{'col_labels'};
293 $opt{'graph_labels'} ||= $opt{'row_labels'};
295 my $filename = $cgi->url(-relative => 1);
296 $filename =~ s/\.(cgi|html)$//;