f7746165a4483d6550d19e5e28a4f65ec353544f
[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 %   #false laziness w/  search/elements/search-xls
81 %   my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
82 %   $filename .= $format->{extension};
83 %   
84 %   http_header('Content-Type' => $format->{mime_type} );
85 %   http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
86 %
87 %   my $output = '';
88 %   my $XLS = new IO::Scalar \$output;
89 %   my $workbook = $format->{class}->new($XLS)
90 %     or die "Error opening .xls file: $!";
91 %
92 %   my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
93 %
94 %   my($r,$c) = (0,0);
95 %
96 %   foreach ('', @col_labels, ($opt{'nototal'} ? () : 'Total') ) {
97 %     my $header = $_;
98 %     $worksheet->write($r, $c++, $header)
99 %   }
100 %
101 %   my @bottom_total = ();
102 %   foreach ( @items ) {
103 %     $r++;
104 %     $c = 0;
105 %     my $total = 0;
106 %     $worksheet->write( $r, $c++, shift( @row_labels ) );
107 %     foreach ( @{ shift( @data ) } ) {
108 %       $total += $_;
109 %       $bottom_total[$c-1] += $_;
110 %       $worksheet->write($r, $c++,  sprintf($sprintf, $_) );
111 %     }
112 %     unless ( $opt{'nototal'} ) { 
113 %       $bottom_total[$c-1] += $total; 
114 %       $worksheet->write($r, $c++,  sprintf($sprintf, $total) );
115 %     } 
116 %   }
117
118 %   $c = 0;
119 %   if ( $opt{'bottom_total'} ) {
120 %     $r++;
121 %     $worksheet->write($r, $c++, 'Total');
122 %     $worksheet->write($r, $c++, sprintf($sprintf, $_)) foreach @bottom_total;
123 %   } 
124 %   
125 %   $workbook->close();# or die "Error creating .xls file: $!";
126 %
127 %   http_header('Content-Length' => length($output) );
128 %   
129 <% $output %>
130 % } elsif ( $cgi->param('_type') eq 'png' ) {
131 %
132 %   my $graph_type = 'LinesPoints';
133 %   if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain|Bars)$/ ) {
134 %     $graph_type = $1;
135 %   }
136 %   my $class = "Chart::$graph_type";
137 %
138 %   my $chart = $class->new(976,384);
139 % # the chart area itself is 900 pixels wide, and the date labels are ~60 each.
140 % # staggered, we can fit about 28 of them.
141 % # they're about 12 pixels high, so vertically, we can fit about 60 (allowing
142 % # space for them to be readable).
143 % # after that we have to start skipping labels. also remove the dots, since 
144 % # they're just a blob at that point.
145 %   my $num_labels = scalar(@{ $opt{axis_labels} });
146 %   my %chart_opt = %{ $opt{chart_options} || {} };
147 %   if ( $num_labels > 28 ) {
148 %     $chart_opt{x_ticks} = 'vertical';
149 %     if ( $num_labels > 60 ) {
150 %       $chart_opt{skip_x_ticks} = int($num_labels / 60) + 1;
151 %       $chart_opt{pt_size} = 1;
152 %     }
153 %   }
154 %   my $d = 0;
155 %   $chart->set(
156 %     #'min_val' => 0,
157 %     'legend' => 'bottom',
158 %     'colors' => { ( 
159 %                     map { my $color = $_;
160 %                           'dataset'.$d++ =>
161 %                             [ map hex($_), unpack 'a2a2a2', $color ]
162 %                         }
163 %                         @{ $opt{'colors'} }
164 %                   ),
165 %                   'grey_background' => 'white',
166 %                   'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
167 %                 },
168 %     'legend_labels' => $opt{'graph_labels'},
169 %     'brush_size' => 4,
170 %     %chart_opt,
171 %   );
172 %
173 %   http_header('Content-Type' => 'image/png' );
174 %   http_header('Cache-Control' => 'no-cache' );
175 %
176 %   $chart->_set_colors();
177 %   
178 <% $chart->scalar_png([ $opt{'axis_labels'}, @data ]) %>
179 %
180 % } else {
181 % # image and download links should use the cached data
182 % # just directly reference this component
183 % my $myself = $p.'graph/elements/report.html?session='.$session;
184 %
185 <% include('/elements/header.html', $opt{'title'} ) %>
186 % unless ( $opt{'graph_type'} eq 'none' ) {
187
188 <IMG SRC="<% "$myself;_type=png" %>" WIDTH="976" HEIGHT="384"
189  STYLE="page-break-after:always;">
190 % }
191 <P ALIGN="right" CLASS="noprint">
192
193 % unless ( $opt{'disable_download'} ) { 
194             Download full results<BR>
195             as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
196             as <A HREF="<% "$myself;_type=csv" %>">CSV file</A></P>
197 % } 
198 %
199 </P>
200 %# indexed by item, then by entry (the element indices of @{$data[$i]}).
201 % my @cell = ();
202 % my @styles;
203 % my $num_entries = scalar(@col_labels);
204 % my $num_items = scalar(@items);
205 % $cell[0] = ['']; #top left corner
206 % foreach my $column ( @col_labels ) {
207 %   $column =~ s/ /\<BR\>/;
208 %   push @{$cell[0]}, $column;
209 % }
210 % if ( ! $opt{'nototal'} ) {
211 %   $num_entries++;
212 %   push @{$cell[0]}, emt('Total');
213 % }
214
215 % # i for item, e for entry
216 % my $i = 1;
217 % foreach my $row ( @items ) {
218 % #make a style
219 %   my $color = shift @{ $opt{'colors'} };
220 %   push @styles, ".i$i { text-align: right; color: #$color; }";
221 % #create the data row
222 %   my $links = shift @{$opt{'links'}} || [''];
223 %   my $link_prefix = shift @$links;
224 %   $link_prefix = '<A CLASS="cell" HREF="'.$link_prefix if $link_prefix;
225 %   my $label = shift @row_labels;
226 %   $cell[$i] = [ $label ];
227 %
228 %   my $data_row = $data[$i-1];
229 %#   my $data_row = shift @data;
230 %   if ( ! $opt{'nototal'} ) {
231 %     push @$data_row, sum(@$data_row);
232 %   }
233 %   foreach ( @$data_row ) {
234 %     my $entry = $_;
235 %     $entry = $money_char . sprintf($sprintf, $entry);
236 %     $entry = $link_prefix . shift(@$links) . "\">$entry</A>" if $link_prefix;
237 %     push @{$cell[$i]}, $entry;
238 %   }
239 %   $i++;
240 % }
241 % if ( $opt{'bottom_total'} ) {
242 %   # it's an extra item
243 %   $num_items++;
244 %   push @styles, ".i$i { text-align: right; background-color: #f5f6be; }";
245 %   my $links = $opt{'bottom_link'} || [];
246 %   my $link_prefix = shift @$links;
247 %   $link_prefix = '<A CLASS="cell" HREF="'.$link_prefix if $link_prefix;
248 %   $cell[$i] = [ emt('Total') ];
249 %   for (my $e = 0; $e < $num_entries + 1; $e++) {
250 %     my $entry = sum(map { $_->[$e] } @data);
251 %     $entry = $money_char . sprintf($sprintf, $entry);
252 %     $entry = $link_prefix . shift(@$links) . "\">$entry</A>" if $link_prefix;
253 %     push @{$cell[$i]}, $entry;
254 %   }
255 % }
256
257 <STYLE type="text/css">
258 a.cell {
259   color: inherit !important;
260 }
261 td.cell {
262   border-color: #000;
263 }
264 <% join("\n", @styles) %>
265 %# item labels
266 .e0 {
267   text-align: center;
268   font-weight: bold;
269 }
270 %# totals
271 % if ( ! $opt{'nototal'} ) {
272 .e<% $num_entries %> {
273   text-align: right;
274   background-color: #f5f6be;
275 }
276 % }
277 %# date labels
278 .i0 {
279   text-align: center;
280   font-weight: bold;
281 }
282 </STYLE>
283
284 <% include('/elements/table.html', 'f8f8f8') %>
285 % if ( $opt{'transpose'} ) {
286 %   for ( my $e = 0; $e < $num_entries + 1; $e++ ) {
287   <TR>
288 %     for ( my $i = 0; $i < $num_items + 1; $i++ ) {
289     <TD CLASS="<%"cell i$i e$e"%>"><% $cell[$i][$e] %></TD>
290 %     }
291   </TR>
292 %   }
293 %
294 % } else { #!transpose
295 %
296 %   for (my $i = 0; $i < $num_items + 1; $i++) {
297   <TR>
298 %     for (my $e = 0; $e < $num_entries + 1; $e++) {
299     <TD CLASS="<%"cell i$i e$e"%>"><% $cell[$i][$e] %></TD>
300 %     }
301   </TR>
302 %   }
303 </TABLE>
304 % }
305
306 <% include('/elements/footer.html') %>
307 % } 
308 <%init>
309
310 my(%opt) = @_;
311 my $session;
312 # load from cache if possible, to avoid recalculating
313 if ( $cgi->param('session') =~ /^(\d+)$/ ) {
314   $session = $1;
315   %opt = %{ $m->cache->get($session) };
316 }
317 else {
318   $session = sprintf("%010d%06d", time, int(rand(1000000)));
319   $m->cache->set($session, \%opt, '1h');
320 }
321
322 my $sprintf = $opt{'sprintf'} || '%.2f';
323
324 my $conf = new FS::Conf;
325 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
326
327 my @items = @{ $opt{'items'} };
328
329 foreach my $other (qw( col_labels row_labels graph_labels axis_labels colors links )) {
330   if ( ref($opt{$other}) eq 'HASH' ) {
331     $opt{$other} = [ map $opt{$other}{$_}, @items ];
332   }
333 }
334
335 my @col_labels = @{$opt{'col_labels'}};
336 my @row_labels = @{$opt{'row_labels'}};
337 my @data       = @{$opt{'data'}};
338
339 $opt{'axis_labels'}  ||= $opt{'col_labels'};
340 $opt{'graph_labels'} ||= $opt{'row_labels'};
341
342 my $filename = $cgi->url(-relative => 1);
343 $filename =~ s/\.(cgi|html)$//;
344
345 </%init>