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