add package churn report/graph
[freeside.git] / httemplate / graph / elements / monthly.html
1 <%doc>
2
3 Example:
4
5   include('elements/monthly.html',
6     #required
7     'title'           => 'Page title',
8     'items'           => \@items,
9     'labels'          => \@labels,       # or \%labels (keys are items)
10
11     #required?
12     'colors'          => \@colors,       # or \%colors,
13
14     #recommended
15     'graph_labels'    => \@graph_labels, # or \%graph_labels,
16
17     #optional
18     'params'          => \@params, # opt,
19     'links'           => \@links,      # or \%link, #opt
20     'link_fromparam'  => 'param_from', #defaults to 'begin'
21     'link_toparam'    => 'param_to',   #defaults to 'end'
22
23     #optional, pulled from CGI params if not specified
24     'start_month'     => $smonth,
25     'start_year'      => $syear,
26     'end_month'       => $emonth,
27     'end_year'        => $eyear,
28
29     #optional
30     'agentnum'        => $agentnum,
31     'nototal'         => 1,
32     'graph_type'      => 'LinesPoints',
33     'remove_empty'    => 1,
34     'bottom_total'    => 1,
35     'sprintf'         => '%u', #sprintf format, overrides default %.2f
36     'disable_money'   => 1,
37   );
38
39 </%doc>
40 % if ( $cgi->param('_type') =~ /^(csv)$/ ) {
41 %
42 %   #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
43 %   http_header('Content-Type' => 'text/plain' );
44 %
45 %   my $csv = new Text::CSV_XS { 'always_quote' => 1,
46 %                                'eol'          => "\n", #"\015\012", #"\012"
47 %                              };
48 %
49 %   $csv->combine(map { my $m=$_; $m =~ s/^(\d+)\//$mon[$1-1] /; $m; }
50 %                     ('', @{$data->{label}}, $opt{'nototal'} ? () : 'Total')
51 %                );
52 %   
53 <% $csv->string %>
54 %
55 %   my @bottom_total = ();
56 %   foreach ( @{ $data->{'items'} } ) {
57 %
58 %     my $col = 0;
59 %     my $total = 0;
60 %     $csv->combine(
61 %       shift( @{ $data->{'item_labels'} } ),
62 %       map { $total += $_; $bottom_total[$col++] += $_; sprintf($sprintf, $_); }
63 %         ( @{ shift( @{$data->{data}} ) } ),
64 %       ( $opt{'nototal'} ? () : sprintf($sprintf, $total) ),
65 %     );
66 %     unless ( $opt{'nototal'} ) { 
67 %       $bottom_total[$col++] += $total; 
68 %     } 
69 %
70 <% $csv->string %>
71 %
72 %   }
73
74 %   if ( $opt{'bottom_total'} ) {
75 %     $csv->combine(
76 %       'Total',
77 %       map { sprintf($sprintf, $_) } @bottom_total,
78 %     );
79 %
80 <% $csv->string %>
81 %
82 %   } 
83 %   
84 % } elsif ( $cgi->param('_type') =~ /(\.xls)$/ ) {
85 %
86 %   #http_header('Content-Type' => 'application/excel' ); #eww
87 %   http_header('Content-Type' => 'application/vnd.ms-excel' );
88 %   #http_header('Content-Type' => 'application/msexcel' ); #alas
89 %
90 %   my $output = '';
91 %   my $XLS = new IO::Scalar \$output;
92 %   my $workbook = Spreadsheet::WriteExcel->new($XLS)
93 %     or die "Error opening .xls file: $!";
94 %
95 %   my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
96 %
97 %   my($r,$c) = (0,0);
98 %
99 %   foreach ('', @{$data->{label}}, ($opt{'nototal'} ? () : 'Total') ) {
100 %     my $header = $_;
101 %     $header =~ s/^(\d+)\//$mon[$1-1] /;
102 %     $worksheet->write($r, $c++, $header)
103 %   }
104 %
105 %   my @bottom_total = ();
106 %   foreach ( @{ $data->{'items'} } ) {
107 %     $r++;
108 %     $c = 0;
109 %     my $total = 0;
110 %     $worksheet->write( $r, $c++, shift( @{ $data->{'item_labels'} } ) );
111 %     foreach ( @{ shift( @{$data->{data}} ) } ) {
112 %       $total += $_;
113 %       $bottom_total[$c] += $_;
114 %       $worksheet->write($r, $c++,  sprintf($sprintf, $_) );
115 %     }
116 %     unless ( $opt{'nototal'} ) { 
117 %       $bottom_total[$c] += $total; 
118 %       $worksheet->write($r, $c++,  sprintf($sprintf, $total) );
119 %     } 
120 %   }
121
122 %   $c = 0;
123 %   if ( $opt{'bottom_total'} ) {
124 %     $r++;
125 %     $worksheet->write($r, $c++, 'Total');
126 %     $worksheet->write($r, $c++, sprintf($sprintf, $_)) foreach @bottom_total;
127 %   } 
128 %   
129 %   $workbook->close();# or die "Error creating .xls file: $!";
130 %
131 %   http_header('Content-Length' => length($output) );
132 %   
133 <% $output %>
134 % } elsif ( $cgi->param('_type') =~ /^(png)$/ ) {
135 %
136 %   #my $chart = Chart::LinesPoints->new(1024,480);
137 %   #my $chart = Chart::LinesPoints->new(768,480);
138 %
139 %   my $graph_type = 'LinesPoints';
140 %   if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain)$/ ) {
141 %     $graph_type = $1;
142 %   }
143 %   my $class = "Chart::$graph_type";
144 %
145 %   my $chart = $class->new(976,384);
146 %   
147 %   my $d = 0;
148 %   $chart->set(
149 %     #'min_val' => 0,
150 %     'legend' => 'bottom',
151 %     'colors' => { ( 
152 %                     map { my $color = $_;
153 %                           'dataset'.$d++ =>
154 %                             [ map hex($_), unpack 'a2a2a2', $color ]
155 %                         }
156 %                         #@{ $opt{'colors'} }
157 %                         @{ $data->{'colors'} }
158 %                   ),
159 %                   #'grey_background' => [ 211, 211, 211 ],
160 %                   'grey_background' => 'white',
161 %                   'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
162 %                 },
163 %     #'grey_background' => 'false',
164 %     'legend_labels' => $data->{'item_labels'},
165 %     'brush_size' => 4,
166 %     #'pt_size' => 12,
167 %   );
168 %
169 %   #my @data = map { $data->{$_} } ( 'label', @items );
170 %   my @data = @{ $data->{data} };
171 %   unshift @data, $data->{'label'};
172 %   
173 %   http_header('Content-Type' => 'image/png' );
174 %
175 %   $chart->_set_colors();
176 %   
177 <% $chart->scalar_png(\@data) %>
178 %
179 % } else {
180 %
181 <% include('/elements/header.html', $opt{'title'} ) %>
182 % $cgi->param('_type', 'png'); 
183
184 <IMG SRC="<% $cgi->self_url %>" WIDTH="976" HEIGHT="384">
185 <P ALIGN="right">
186
187 % unless ( $opt{'disable_download'} ) { 
188 %   $cgi->param('_type', "monthly.xls" ); 
189             Download full results
190             as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A>
191 %   $cgi->param('_type', 'csv'); 
192             as <A HREF="<% $cgi->self_url %>">CSV file</A></P>
193 %   $cgi->param('_type', "html" ); 
194 % } 
195 %
196 </P>
197 <% include('/elements/table.html', 'e8e8e8') %>
198
199 <TR>
200
201   <TD></TD>
202
203 % foreach my $column ( @{$data->{label}} ) {
204 %       #$column =~ s/^(\d+)\//$mon[$1-1]<BR>/e;
205 %       $column =~ s/^(\d+)\//$mon[$1-1]<BR>/;
206     <TH><% $column %></TH>
207 % } 
208
209 % unless ( $opt{'nototal'} ) { 
210     <TH>Total</TH>
211 % } 
212
213 </TR>
214
215 % my @bottom_total = ();
216 % foreach my $row ( @{ $data->{'items'} } ) {
217 %
218 %     #my $color = shift( @{ $opt{'colors'} } );
219 %     my $color = shift( @{ $data->{'colors'} } );
220 %     my $link = shift( @{ $data->{'links'} } );
221 %     my ( $begin, $end ) = ( $fromparam, $toparam );
222 %     if ( ref($link) ) {
223 %       my $ref = $link;
224 %       $link =  $ref->{link};
225 %       $begin = $ref->{fromparam};
226 %       $end =   $ref->{toparam};
227 %     }
228 %     $link = $link ? qq(<A HREF="$link) : '';
229 %     my $label = shift( @{ $data->{'item_labels'} } );
230
231       <TR>
232
233         <TH>
234           <FONT COLOR="#<% $color %>"><% $label %></FONT>
235         </TH>
236
237 %       #my $link = exists($opt{'links'}{$row})
238 %             #  ? qq(<A HREF="$opt{'links'}{$row})
239 %             #  : '';
240 %       my @speriod = @{$data->{speriod}};
241 %       my @eperiod = @{$data->{eperiod}};
242 %       my $total = 0;
243 %    
244 %       my $col = 0;
245 %       foreach my $column ( @{ shift( @{$data->{data}} ) } ) {
246
247           <TD ALIGN="right" BGCOLOR="#ffffff">
248             <% $link ? $link. "$begin=". shift(@speriod). ";$end=". shift(@eperiod). '">' : '' %><FONT COLOR="#<% $color %>"><% $money_char %><% sprintf($sprintf,, $column) %></FONT><% $link ? '</A>' : '' %>
249           </TD>
250 %
251 %         $total += $column;
252 %         $bottom_total[$col++] += $column;
253 %      
254 %       } 
255
256 %       unless ( $opt{'nototal'} ) { 
257             <TD ALIGN="right" BGCOLOR="#f5f6be">
258               <% $link ? $link. "$begin=". ${$data->{speriod}}[0]. ";$end=". ${$data->{eperiod}}[-1]. '">' : '' %><FONT COLOR="#<% $color %>"><% $money_char %><% sprintf($sprintf, $total) %></FONT><% $link ? '</A>' : '' %>
259             </TD>
260 %           $bottom_total[$col++] += $total; 
261 %       } 
262
263       </TR>
264
265 % } 
266
267 % if ( $opt{'bottom_total'} ) {
268 %     my @speriod = ( @{$data->{speriod}}, ${$data->{speriod}}[0] );
269 %     my @eperiod = ( @{$data->{eperiod}}, ${$data->{eperiod}}[-1] );
270
271   <TR>
272     <TH>Total</TH>
273
274 % foreach my $total ( @bottom_total ) { 
275
276       <TD ALIGN="right" BGCOLOR="#f5f6be">
277         <% $opt{'bottom_link'}
278               ? '<A HREF="'. $opt{'bottom_link'}.
279                 "$fromparam=". shift(@speriod).
280                 ";$toparam=". shift(@eperiod). '">'
281               : ''
282         %>$<% sprintf($sprintf, $total) %><% $opt{'bottom_link'} ? '</A>' : '' %>
283
284       </TD>
285
286 % } 
287
288   </TR>
289
290 % } 
291
292 </TABLE>
293
294 <% include('/elements/footer.html') %>
295 % } 
296 <%once>
297
298 </%once>
299 <%init>
300
301 my(%opt) = @_;
302
303 my $sprintf = $opt{'sprintf'} || '%.2f';
304 my $fromparam = $opt{'link_fromparam'} || 'begin';
305 my $toparam =   $opt{'link_toparam'}   || 'end';
306
307 my $conf = new FS::Conf;
308 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
309
310 my @items = @{ $opt{'items'} };
311
312 foreach my $other (qw( labels graph_labels colors links )) {
313 #foreach my $other (qw( labels graph_labels colors )) {
314   if ( ref($opt{$other}) eq 'HASH' ) {
315     $opt{$other} = [ map $opt{$other}{$_}, @items ];
316   }
317 }
318
319 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
320
321 #find first month
322 $opt{'start_month'} ||= $cgi->param('start_month'); # || $curmon+1;
323 $opt{'start_year'}  ||= $cgi->param('start_year'); # || 1899+$curyear;
324
325 #find last month
326 $opt{'end_month'} ||= $cgi->param('end_month'); # || $curmon+1;
327 $opt{'end_year'}  ||= $cgi->param('end_year'); # || 1900+$curyear;
328
329 my $report = new FS::Report::Table::Monthly (
330
331   #'items'       => $opt{'items'},
332   'items'        => \@items,
333   'params'       => $opt{'params'},
334   'item_labels'  => ( $cgi->param('_type') =~ /^(png)$/
335                         ? $opt{'graph_labels'}
336                         : $opt{'labels'}
337                     ),
338   'colors'       => $opt{'colors'},
339   'links'        => $opt{'links'},
340
341   'start_month'  => $opt{'start_month'},
342   'start_year'   => $opt{'start_year'},
343   'end_month'    => $opt{'end_month'},
344   'end_year'     => $opt{'end_year'},
345
346   'agentnum'     => $opt{'agentnum'},
347   'remove_empty' => $opt{'remove_empty'},
348 );
349 my $data = $report->data;
350
351 </%init>