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