projected sales report, #15393
[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-1] += $_;
109 %       $worksheet->write($r, $c++,  sprintf($sprintf, $_) );
110 %     }
111 %     unless ( $opt{'nototal'} ) { 
112 %       $bottom_total[$c-1] += $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 %   http_header('Cache-Control' => 'no-cache' );
159 %
160 %   $chart->_set_colors();
161 %   
162 <% $chart->scalar_png([ $opt{'axis_labels'}, @data ]) %>
163 %
164 % } else {
165 % # image and download links should use the cached data
166 % # just directly reference this component
167 % my $myself = $p.'graph/elements/report.html?session='.$session;
168 %
169 <% include('/elements/header.html', $opt{'title'} ) %>
170 % unless ( $opt{'graph_type'} eq 'none' ) {
171
172 <IMG SRC="<% "$myself;_type=png" %>" WIDTH="976" HEIGHT="384">
173 % }
174 <P ALIGN="right">
175
176 % unless ( $opt{'disable_download'} ) { 
177             Download full results<BR>
178             as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
179             as <A HREF="<% "$myself;_type=csv" %>">CSV file</A></P>
180 % } 
181 %
182 </P>
183 <% include('/elements/table.html', 'f8f8f8') %>
184
185 <TR>
186
187   <TD></TD>
188
189 % foreach my $column ( @col_labels ) {
190 %       $column =~ s/ /\<BR\>/; # working on a smarter way to do this
191     <TH><% $column %></TH>
192 % } 
193
194 % unless ( $opt{'nototal'} ) { 
195     <TH>Total</TH>
196 % } 
197
198 </TR>
199
200 % my @bottom_total = ();
201 % foreach my $row ( @items ) {
202 %
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 );
211
212       <TR>
213
214         <TH>
215           <FONT COLOR="#<% $color %>"><% $label %></FONT>
216         </TH>
217
218 %       my $total = 0;
219 %       my $col = 0;
220 %       foreach my $column ( @{ shift( @data ) } ) {
221
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>' : '' %>
224           </TD>
225 %
226 %         $total += $column;
227 %         $bottom_total[$col++] += $column;
228 %      
229 %       } 
230
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>' : '' %>
234             </TD>
235 %           $bottom_total[$col++] += $total; 
236 %       } 
237
238       </TR>
239
240 % } 
241
242 % if ( $opt{'bottom_total'} ) {
243   <TR>
244     <TH>Total</TH>
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 ) { 
249
250       <TD ALIGN="right" BGCOLOR="#f5f6be">
251         <% $prefix
252               ? '<A HREF="'. $prefix .shift(@bottom_links). '">'
253               : ''
254         %><% $money_char %><% sprintf($sprintf, $total) %><% $prefix ? '</A>' : '' %>
255
256       </TD>
257
258 % } 
259
260   </TR>
261
262 % } 
263
264 </TABLE>
265
266 <% include('/elements/footer.html') %>
267 % } 
268 <%once>
269
270 </%once>
271 <%init>
272
273 my(%opt) = @_;
274 my $session;
275 # load from cache if possible, to avoid recalculating
276 if ( $cgi->param('session') =~ /^(\d+)$/ ) {
277   $session = $1;
278   %opt = %{ $m->cache->get($session) };
279 }
280 else {
281   $session = sprintf("%10d%6d", time, int(rand(1000000)));
282   $m->cache->set($session, \%opt, '1h');
283 }
284
285 my $sprintf = $opt{'sprintf'} || '%.2f';
286
287 my $conf = new FS::Conf;
288 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
289
290 my @items = @{ $opt{'items'} };
291
292 foreach my $other (qw( col_labels row_labels graph_labels axis_labels colors links )) {
293   if ( ref($opt{$other}) eq 'HASH' ) {
294     $opt{$other} = [ map $opt{$other}{$_}, @items ];
295   }
296 }
297
298 my @col_labels = @{$opt{'col_labels'}};
299 my @row_labels = @{$opt{'row_labels'}};
300 my @data       = @{$opt{'data'}};
301
302 $opt{'axis_labels'}  ||= $opt{'col_labels'};
303 $opt{'graph_labels'} ||= $opt{'row_labels'};
304
305 my $filename = $cgi->url(-relative => 1);
306 $filename =~ s/\.(cgi|html)$//;
307
308 </%init>