3b9f73aa143acc598710ce2bf965611828ecaaae
[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 $report = new FS::Report::Table::Monthly (
33
34     #'items'       => $opt{'items'},
35     'items'        => \@items,
36     'params'       => $opt{'params'},
37     'item_labels'  => ( $cgi->param('_type') =~ /^(png)$/
38                           ? $opt{'graph_labels'}
39                           : $opt{'labels'}
40                       ),
41     'colors'       => $opt{'colors'},
42     'links'        => $opt{'links'},
43
44     'start_month'  => $opt{'start_month'},
45     'start_year'   => $opt{'start_year'},
46     'end_month'    => $opt{'end_month'},
47     'end_year'     => $opt{'end_year'},
48
49     'agentnum'     => $opt{'agentnum'},
50     'remove_empty' => $opt{'remove_empty'},
51   );
52   my $data = $report->data;
53
54   if ( $cgi->param('_type') =~ /^(png)$/ ) {
55
56     #my $chart = Chart::LinesPoints->new(1024,480);
57     #my $chart = Chart::LinesPoints->new(768,480);
58
59     my $graph_type = 'LinesPoints';
60     if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain)$/ ) {
61       $graph_type = $1;
62     }
63     my $class = "Chart::$graph_type";
64
65     my $chart = $class->new(976,384);
66     
67     my $d = 0;
68     $chart->set(
69       #'min_val' => 0,
70       'legend' => 'bottom',
71       'colors' => { ( 
72                       map { my $color = $_;
73                             'dataset'.$d++ =>
74                               [ map hex($_), unpack 'a2a2a2', $color ]
75                           }
76                           #@{ $opt{'colors'} }
77                           @{ $data->{'colors'} }
78                     ),
79                     #'grey_background' => [ 211, 211, 211 ],
80                     'grey_background' => 'white',
81                     'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
82                   },
83       #'grey_background' => 'false',
84       'legend_labels' => $data->{'item_labels'},
85       'brush_size' => 4,
86       #'pt_size' => 12,
87     );
88
89     #my @data = map { $data->{$_} } ( 'label', @items );
90     my @data = @{ $data->{data} };
91     unshift @data, $data->{'label'};
92     
93     http_header('Content-Type' => 'image/png' );
94
95     $chart->_set_colors();
96
97     %><%= $chart->scalar_png(\@data) %><%
98
99   } else {
100
101     my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
102
103 %><%= include('/elements/header.html', $opt{'title'} ) %>
104
105 <% $cgi->param('_type', 'png'); %>
106 <IMG SRC="<%= $cgi->self_url %>" WIDTH="976" HEIGHT="384">
107 <BR>
108
109 <%= table('e8e8e8') %>
110
111 <TR>
112
113   <TD></TD>
114
115   <% foreach my $column ( @{$data->{label}} ) {
116        #$column =~ s/^(\d+)\//$mon[$1-1]<BR>/e;
117        $column =~ s/^(\d+)\//$mon[$1-1]<BR>/;
118   %>
119     <TH><%= $column %></TH>
120   <% } %>
121
122   <% unless ( $opt{'nototal'} ) { %>
123     <TH>Total</TH>
124   <% } %>
125
126 </TR>
127
128 <% my @bottom_total = ();
129    foreach my $row ( @{ $data->{'items'} } ) {
130
131      #my $color = shift( @{ $opt{'colors'} } );
132      my $color = shift( @{ $data->{'colors'} } );
133      my $link = shift( @{ $data->{'links'} } );
134      $link = $link ? qq(<A HREF="$link) : '';
135 %>
136
137   <TR>
138
139     <TH><FONT COLOR="#<%= $color %>"><%= shift( @{ $data->{'item_labels'} } ) %></FONT></TH>
140
141     <% #my $link = exists($opt{'links'}{$row})
142        #  ? qq(<A HREF="$opt{'links'}{$row})
143        #  : '';
144        my @speriod = @{$data->{speriod}};
145        my @eperiod = @{$data->{eperiod}};
146        my $total = 0;
147     %>
148     <% my $col = 0;
149        foreach my $column ( @{ shift( @{$data->{data}} ) } ) { # ( @{$data->{$row}} ) {
150     %>
151
152       <TD ALIGN="right" BGCOLOR="#ffffff">
153         <%= $link ? $link. 'begin='. shift(@speriod). ';end='. shift(@eperiod). '">' : '' %><FONT COLOR="#<%= $color %>">$<%= sprintf("%.2f", $column) %></FONT><%= $link ? '</A>' : '' %>
154       </TD>
155       <%
156          $total += $column;
157          $bottom_total[$col++] += $column;
158       %>
159
160     <% } %>
161
162     <% unless ( $opt{'nototal'} ) { %>
163
164       <TD ALIGN="right" BGCOLOR="#f5f6be">
165         <%= $link ? $link. 'begin='. ${$data->{speriod}}[0]. ';end='. ${$data->{eperiod}}[-1]. '">' : '' %><FONT COLOR="#<%= $color %>">$<%= sprintf("%.2f", $total) %></FONT><%= $link ? '</A>' : '' %>
166       </TD>
167
168       <% $bottom_total[$col++] += $total; %>
169
170     <% } %>
171
172   </TR>
173
174 <% } %>
175
176 <% if ( $opt{'bottom_total'} ) {
177      my @speriod = ( @{$data->{speriod}}, ${$data->{speriod}}[0] );
178      my @eperiod = ( @{$data->{eperiod}}, ${$data->{eperiod}}[-1] );
179 %>
180
181   <TR>
182     <TH>Total</TH>
183
184     <% foreach my $total ( @bottom_total ) { %>
185
186       <TD ALIGN="right" BGCOLOR="#f5f6be">
187         <%= $opt{'bottom_link'}
188               ? '<A HREF="'. $opt{'bottom_link'}.
189                 'begin='. shift(@speriod).
190                 ';end='. shift(@eperiod). '">'
191               : ''
192         %>$<%= sprintf("%.2f", $total) %><%= $opt{'bottom_link'} ? '</A>' : '' %>
193
194       </TD>
195
196     <% } %>
197
198   </TR>
199
200 <% } %>
201
202 </TABLE>
203
204 <%= include('/elements/footer.html') %>
205
206 <% } %>