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