fix table titles for new bg color
[freeside.git] / httemplate / graph / elements / report.html
1 <%doc>
2
3 Example:
4
5   include('elements/report.html',
6     #required
7     'title'           => 'Page title',
8     'items'           => \@items,
9     'data'            => [ \@item1 \@item2 ... ],
10
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
15
16     'links'           => \@links,         #optional
17
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
21
22     #optional
23     'nototal'         => 1,
24     'graph_type'      => 'LinesPoints',
25     'bottom_total'    => 1,
26     'sprintf'         => '%u', #sprintf format, overrides default %.2f
27     'disable_money'   => 1,
28   );
29
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.
34
35 </%doc>
36 % if ( $cgi->param('_type') =~ /^(csv)$/ ) {
37 %
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");
42 %
43 %   my $csv = new Text::CSV_XS { 'always_quote' => 1,
44 %                                'eol'          => "\n", #"\015\012", #"\012"
45 %                              };
46 %
47 %   $csv->combine('', @col_labels, $opt{'nototal'} ? () : 'Total');
48 %   
49 <% $csv->string %>
50 %
51 %   my @bottom_total = ();
52 %   foreach ( @items ) {
53 %
54 %     my $col = 0;
55 %     my $total = 0;
56 %     $csv->combine(
57 %       shift( @row_labels ),
58 %       map { $total += $_; $bottom_total[$col++] += $_; sprintf($sprintf, $_); }
59 %         ( @{ shift( @data ) } ),
60 %       ( $opt{'nototal'} ? () : sprintf($sprintf, $total) ),
61 %     );
62 %     unless ( $opt{'nototal'} ) { 
63 %       $bottom_total[$col++] += $total; 
64 %     } 
65 <% $csv->string %>
66 %
67 %   }
68
69 %   if ( $opt{'bottom_total'} ) {
70 %     $csv->combine(
71 %       'Total',
72 %       map { sprintf($sprintf, $_) } @bottom_total,
73 %     );
74 %
75 <% $csv->string %>
76 %
77 %   } 
78 %   
79 % } elsif ( $cgi->param('_type') =~ /(xls)$/ ) {
80 %
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");
85 %
86 %   my $output = '';
87 %   my $XLS = new IO::Scalar \$output;
88 %   my $workbook = Spreadsheet::WriteExcel->new($XLS)
89 %     or die "Error opening .xls file: $!";
90 %
91 %   my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
92 %
93 %   my($r,$c) = (0,0);
94 %
95 %   foreach ('', @col_labels, ($opt{'nototal'} ? () : 'Total') ) {
96 %     my $header = $_;
97 %     $worksheet->write($r, $c++, $header)
98 %   }
99 %
100 %   my @bottom_total = ();
101 %   foreach ( @items ) {
102 %     $r++;
103 %     $c = 0;
104 %     my $total = 0;
105 %     $worksheet->write( $r, $c++, shift( @row_labels ) );
106 %     foreach ( @{ shift( @data ) } ) {
107 %       $total += $_;
108 %       $bottom_total[$c] += $_;
109 %       $worksheet->write($r, $c++,  sprintf($sprintf, $_) );
110 %     }
111 %     unless ( $opt{'nototal'} ) { 
112 %       $bottom_total[$c] += $total; 
113 %       $worksheet->write($r, $c++,  sprintf($sprintf, $total) );
114 %     } 
115 %   }
116
117 %   $c = 0;
118 %   if ( $opt{'bottom_total'} ) {
119 %     $r++;
120 %     $worksheet->write($r, $c++, 'Total');
121 %     $worksheet->write($r, $c++, sprintf($sprintf, $_)) foreach @bottom_total;
122 %   } 
123 %   
124 %   $workbook->close();# or die "Error creating .xls file: $!";
125 %
126 %   http_header('Content-Length' => length($output) );
127 %   
128 <% $output %>
129 % } elsif ( $cgi->param('_type') eq 'png' ) {
130 %
131 %   my $graph_type = 'LinesPoints';
132 %   if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain|Bars)$/ ) {
133 %     $graph_type = $1;
134 %   }
135 %   my $class = "Chart::$graph_type";
136 %
137 %   my $chart = $class->new(976,384);
138 %   
139 %   my $d = 0;
140 %   $chart->set(
141 %     #'min_val' => 0,
142 %     'legend' => 'bottom',
143 %     'colors' => { ( 
144 %                     map { my $color = $_;
145 %                           'dataset'.$d++ =>
146 %                             [ map hex($_), unpack 'a2a2a2', $color ]
147 %                         }
148 %                         @{ $opt{'colors'} }
149 %                   ),
150 %                   'grey_background' => 'white',
151 %                   'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
152 %                 },
153 %     'legend_labels' => $opt{'graph_labels'},
154 %     'brush_size' => 4,
155 %   );
156 %
157 %   http_header('Content-Type' => 'image/png' );
158 %
159 %   $chart->_set_colors();
160 %   
161 <% $chart->scalar_png([ $opt{'axis_labels'}, @data ]) %>
162 %
163 % } else {
164 %
165 <% include('/elements/header.html', $opt{'title'} ) %>
166 % $cgi->param('_type', 'png'); 
167
168 <IMG SRC="<% $cgi->self_url %>" WIDTH="976" HEIGHT="384">
169 <P ALIGN="right">
170
171 % unless ( $opt{'disable_download'} ) { 
172 %   $cgi->param('_type', "xls" ); 
173             Download full results<BR>
174             as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
175 %   $cgi->param('_type', 'csv'); 
176             as <A HREF="<% $cgi->self_url %>">CSV file</A></P>
177 %   $cgi->param('_type', "html" ); 
178 % } 
179 %
180 </P>
181 <% include('/elements/table.html', 'f8f8f8') %>
182
183 <TR>
184
185   <TD></TD>
186
187 % foreach my $column ( @col_labels ) {
188 %       $column =~ s/ /\<BR\>/; # working on a smarter way to do this
189     <TH><% $column %></TH>
190 % } 
191
192 % unless ( $opt{'nototal'} ) { 
193     <TH>Total</TH>
194 % } 
195
196 </TR>
197
198 % my @bottom_total = ();
199 % foreach my $row ( @items ) {
200 %
201 %     my $color = shift( @{ $opt{'colors'} } );
202 %     my @links = @{ shift( @{ $opt{'links'} } ) };
203 % #   $opt{'links'} is an array parallel to items.
204 % #   Each element of that is an array containing a prefix,
205 % #   followed by suffixes matched to the cells of the table.
206 %     my $link_prefix = shift @links;
207 %     $link_prefix = $link_prefix ? qq(<A HREF="$link_prefix) : '';    #"
208 %     my $label = shift( @row_labels );
209
210       <TR>
211
212         <TH>
213           <FONT COLOR="#<% $color %>"><% $label %></FONT>
214         </TH>
215
216 %       my $total = 0;
217 %       my $col = 0;
218 %       foreach my $column ( @{ shift( @data ) } ) {
219
220           <TD ALIGN="right" BGCOLOR="#ffffff">
221             <% $link_prefix ? $link_prefix . shift(@links) . '">' : '' %><FONT COLOR="#<% $color %>"><% $money_char %><% sprintf($sprintf,, $column) %></FONT><% $link_prefix ? '</A>' : '' %>
222           </TD>
223 %
224 %         $total += $column;
225 %         $bottom_total[$col++] += $column;
226 %      
227 %       } 
228
229 %       unless ( $opt{'nototal'} ) { 
230             <TD ALIGN="right" BGCOLOR="#f5f6be">
231               <% $link_prefix ? $link_prefix. shift(@links) . '">' : '' %><FONT COLOR="#<% $color %>"><% $money_char %><% sprintf($sprintf, $total) %></FONT><% $link_prefix ? '</A>' : '' %>
232             </TD>
233 %           $bottom_total[$col++] += $total; 
234 %       } 
235
236       </TR>
237
238 % } 
239
240 % if ( $opt{'bottom_total'} ) {
241   <TR>
242     <TH>Total</TH>
243 %   my @bottom_links = $opt{'bottom_link'} ? @{ $opt{'bottom_link'} } : ();
244 %   my $prefix = shift(@bottom_links);
245 %   pop @bottom_links if $opt{'nototal'};
246 %   foreach my $total ( @bottom_total ) { 
247
248       <TD ALIGN="right" BGCOLOR="#f5f6be">
249         <% $prefix
250               ? '<A HREF="'. $prefix .shift(@bottom_links). '">'
251               : ''
252         %><% $money_char %><% sprintf($sprintf, $total) %><% $prefix ? '</A>' : '' %>
253
254       </TD>
255
256 % } 
257
258   </TR>
259
260 % } 
261
262 </TABLE>
263
264 <% include('/elements/footer.html') %>
265 % } 
266 <%once>
267
268 </%once>
269 <%init>
270
271 my(%opt) = @_;
272
273 my $sprintf = $opt{'sprintf'} || '%.2f';
274
275 my $conf = new FS::Conf;
276 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
277
278 my @items = @{ $opt{'items'} };
279
280 foreach my $other (qw( col_labels row_labels graph_labels axis_labels colors links )) {
281   if ( ref($opt{$other}) eq 'HASH' ) {
282     $opt{$other} = [ map $opt{$other}{$_}, @items ];
283   }
284 }
285
286 my @col_labels = @{$opt{'col_labels'}};
287 my @row_labels = @{$opt{'row_labels'}};
288 my @data       = @{$opt{'data'}};
289
290 $opt{'axis_labels'}  ||= $opt{'col_labels'};
291 $opt{'graph_labels'} ||= $opt{'row_labels'};
292
293 my $filename = $cgi->url(-relative => 1);
294 $filename =~ s/\.(cgi|html)$//;
295
296 </%init>