4b988f166676b7f8f3a125fdfab6b63eb23798a8
[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     'daily'           => 1, # omit for monthly granularity
23     'no_graph'        => \@no_graph, # items to leave off the graph (subtotals)
24
25     #optional, pulled from CGI params if not specified
26     'start_month'     => $smonth,
27     'start_year'      => $syear,
28     'end_month'       => $emonth,
29     'end_year'        => $eyear,
30
31
32     #optional, pulled from CGI params if not specified, 
33     #only if 'daily' option is given
34     'start_day'       => $sday,
35     'end_day'         => $eday,
36
37     #optional
38     'agentnum'        => $agentnum,
39     'refnum'          => $refnum,
40     'cust_classnum'   => \@classnums,
41     'nototal'         => 1,
42     'graph_type'      => 'LinesPoints',
43     'remove_empty'    => 1,
44     'bottom_total'    => 1,
45     'sprintf'         => '%u', #sprintf format, overrides default %.2f
46     'disable_money'   => 1,
47   );
48
49 </%doc>
50 <% include('report.html',
51             'items'         => $data->{'items'},
52             'data'          => $data->{'data'},
53             'row_labels'    => $data->{'item_labels'},
54             'graph_labels'  => \@graph_labels,
55             'col_labels'    => $col_labels,
56             'axis_labels'   => $data->{label},
57             'colors'        => \@colors,
58             'links'         => \@links,
59             'no_graph'      => \@no_graph,
60             'bottom_link'   => \@bottom_link,
61             'transpose'     => $opt{'daily'},
62             map { $_, $opt{$_} } (qw(title
63                                     nototal
64                                     graph_type
65                                     bottom_total
66                                     sprintf
67                                     disable_money
68                                     chart_options)),
69           ) %>
70 <%init>
71
72 my(%opt) = @_;
73 $opt{'debug'} ||= $cgi->param('debug');
74
75 my $conf = new FS::Conf;
76 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
77
78 my $fromparam = $opt{'link_fromparam'} || 'begin';
79 my $toparam   = $opt{'link_toparam'} || 'end';
80
81 my @items = @{ $opt{'items'} };
82
83 foreach my $other (qw( labels graph_labels colors links )) {
84   if ( ref($opt{$other}) eq 'HASH' ) {
85     $opt{$other} = [ map $opt{$other}{$_}, @items ];
86   }
87 }
88
89 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
90
91 #find first month
92 $opt{'start_month'} ||= $cgi->param('start_month'); # || $curmon+1;
93 $opt{'start_year'}  ||= $cgi->param('start_year'); # || 1899+$curyear;
94
95 #find last month
96 $opt{'end_month'} ||= $cgi->param('end_month'); # || $curmon+1;
97 $opt{'end_year'}  ||= $cgi->param('end_year'); # || 1900+$curyear;
98
99 $opt{'projection'} ||= $cgi->param('projection') ? 1 : 0;
100
101 if ( $opt{'daily'} ) { # daily granularity
102     $opt{'start_day'} ||= $cgi->param('start_day');
103     $opt{'end_day'} ||= $cgi->param('end_day');
104 }
105
106 my %reportopts = (
107       'items'        => \@items,
108       'params'       => $opt{'params'},
109       'item_labels'  => ( $cgi->param('_type') =~ /^(png)$/ 
110                             ? $opt{'graph_labels'}
111                             : $opt{'labels'}
112                         ),
113       'colors'       => $opt{'colors'},
114       'links'        => $opt{'links'},
115
116       'start_day'    => $opt{'start_day'},
117       'start_month'  => $opt{'start_month'},
118       'start_year'   => $opt{'start_year'},
119       'end_day'      => $opt{'end_day'},
120       'end_month'    => $opt{'end_month'},
121       'end_year'     => $opt{'end_year'},
122       'projection'   => $opt{'projection'},
123       'agentnum'     => $opt{'agentnum'},
124       'refnum'       => $opt{'refnum'},
125       'cust_classnum'=> $opt{'cust_classnum'},
126       'remove_empty' => $opt{'remove_empty'},
127       'doublemonths' => $opt{'doublemonths'},
128       'normalize'    => $opt{'normalize'},
129 );
130
131 warn Dumper({ 'REPORTOPTS' => \%reportopts }) if $opt{'debug'};
132
133 my $report;
134 $report = new FS::Report::Table::Daily(%reportopts) if $opt{'daily'};
135 $report = new FS::Report::Table::Monthly(%reportopts) unless $opt{'daily'};
136 my $data = $report->data;
137
138 warn Dumper({'DATA' => $data}) if $opt{'debug'};
139
140 if ( $data->{'error'} ) {
141   die $data->{'error'}; # could be smarter
142 }
143
144 my $col_labels = [ map { my $m = $_; $m =~ s/^(\d+)\//$mon[$1-1] / ; $m }
145                              @{$data->{label}} ];
146 $col_labels = $data->{label} if $opt{'daily'};
147
148 my @colors;
149 my @graph_labels;
150 my @no_graph;
151 #if ( $opt{'remove_empty'} ) { # no, always do this
152   # then filter out per-item things for collapsed rows
153 foreach my $i (@{ $data->{'indices'} }) {
154   push @colors,       $opt{'colors'}[$i];
155   push @graph_labels, $opt{'graph_labels'}[$i];
156   push @no_graph,     $opt{'no_graph'}[$i];
157 }
158
159 my @links;
160 foreach my $link (@{ $data->{'links'} }) {
161   my @speriod = @{$data->{'speriod'}};
162   my @eperiod = @{$data->{'eperiod'}};
163   my ($begin, $end) = ($fromparam, $toparam);
164
165   my @new = ( $link );
166   if(ref($link)) {
167     $begin = $link->{'fromparam'};
168     $end   = $link->{'toparam'};
169     @new = ( $link->{'link'} );
170   }
171   while(@speriod) {
172     push @new, "$begin=". shift(@speriod).";$end=".shift(@eperiod);
173   }
174   if(! $opt{'nototal'}) {
175     push @new, "$begin=". $data->{'speriod'}[0] . ";$end=". $data->{'eperiod'}[-1];
176   }
177   push @links, \@new;
178 }
179
180 my @bottom_link;
181 if($opt{'bottom_link'}) {
182   my @speriod = (@{$data->{'speriod'}}, $data->{'speriod'}[0]);
183   my @eperiod = (@{$data->{'eperiod'}}, $data->{'eperiod'}[-1]);
184   
185   push @bottom_link, $opt{'bottom_link'};
186   while(@speriod) {
187     push @bottom_link, "$fromparam=". shift(@speriod). ";$toparam=". shift(@eperiod);
188   }
189 }
190
191 </%init>