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