per-agent subtotals on sales report, #18566
[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     'nototal'         => 1,
41     'graph_type'      => 'LinesPoints',
42     'remove_empty'    => 1,
43     'bottom_total'    => 1,
44     'sprintf'         => '%u', #sprintf format, overrides default %.2f
45     'disable_money'   => 1,
46   );
47
48 </%doc>
49 <% include('report.html',
50             'items'         => $data->{'items'},
51             'data'          => $data->{'data'},
52             'row_labels'    => $data->{'item_labels'},
53             'graph_labels'  => \@graph_labels,
54             'col_labels'    => $col_labels,
55             'axis_labels'   => $data->{label},
56             'colors'        => \@colors,
57             'links'         => \@links,
58             'no_graph'      => \@no_graph,
59             'bottom_link'   => \@bottom_link,
60             'transpose'     => $opt{'daily'},
61             map { $_, $opt{$_} } (qw(title
62                                     nototal
63                                     graph_type
64                                     bottom_total
65                                     sprintf
66                                     disable_money
67                                     chart_options)),
68           ) %>
69 <%init>
70
71 my(%opt) = @_;
72 $opt{'debug'} ||= $cgi->param('debug');
73
74 my $conf = new FS::Conf;
75 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
76
77 my $fromparam = $opt{'link_fromparam'} || 'begin';
78 my $toparam   = $opt{'link_toparam'} || 'end';
79
80 my @items = @{ $opt{'items'} };
81
82 foreach my $other (qw( labels graph_labels colors links )) {
83   if ( ref($opt{$other}) eq 'HASH' ) {
84     $opt{$other} = [ map $opt{$other}{$_}, @items ];
85   }
86 }
87
88 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
89
90 #find first month
91 $opt{'start_month'} ||= $cgi->param('start_month'); # || $curmon+1;
92 $opt{'start_year'}  ||= $cgi->param('start_year'); # || 1899+$curyear;
93
94 #find last month
95 $opt{'end_month'} ||= $cgi->param('end_month'); # || $curmon+1;
96 $opt{'end_year'}  ||= $cgi->param('end_year'); # || 1900+$curyear;
97
98 $opt{'projection'} ||= $cgi->param('projection') ? 1 : 0;
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       'projection'   => $opt{'projection'},
122       'agentnum'     => $opt{'agentnum'},
123       'refnum'       => $opt{'refnum'},
124       'remove_empty' => $opt{'remove_empty'},
125       'doublemonths' => $opt{'doublemonths'},
126 );
127
128 warn Dumper({ 'REPORTOPTS' => \%reportopts }) if $opt{'debug'};
129
130 my $report;
131 $report = new FS::Report::Table::Daily(%reportopts) if $opt{'daily'};
132 $report = new FS::Report::Table::Monthly(%reportopts) unless $opt{'daily'};
133 my $data = $report->data;
134
135 warn Dumper({'DATA' => $data}) if $opt{'debug'};
136
137 if ( $data->{'error'} ) {
138   die $data->{'error'}; # could be smarter
139 }
140
141 my $col_labels = [ map { my $m = $_; $m =~ s/^(\d+)\//$mon[$1-1] / ; $m }
142                              @{$data->{label}} ];
143 $col_labels = $data->{label} if $opt{'daily'};
144
145 my @colors;
146 my @graph_labels;
147 my @no_graph;
148 if ( $opt{'remove_empty'} ) {
149   # then filter out per-item things for collapsed rows
150   foreach my $i (@{ $data->{'indices'} }) {
151     push @colors,       $opt{'colors'}[$i];
152     push @graph_labels, $opt{'graph_labels'}[$i];
153     push @no_graph,     $opt{'no_graph'}[$i];
154   }
155 } else {
156   @colors       = @{ $opt{'colors'} };
157   @graph_labels = @{ $opt{'graph_labels'} };
158   @no_graph     = @{ $opt{'no_graph'} || [] };
159 }
160
161 my @links;
162 foreach my $link (@{ $data->{'links'} }) {
163   my @speriod = @{$data->{'speriod'}};
164   my @eperiod = @{$data->{'eperiod'}};
165   my ($begin, $end) = ($fromparam, $toparam);
166
167   my @new = ( $link );
168   if(ref($link)) {
169     $begin = $link->{'fromparam'};
170     $end   = $link->{'toparam'};
171     @new = ( $link->{'link'} );
172   }
173   while(@speriod) {
174     push @new, "$begin=". shift(@speriod).";$end=".shift(@eperiod);
175   }
176   if(! $opt{'nototal'}) {
177     push @new, "$begin=". $data->{'speriod'}[0] . ";$end=". $data->{'eperiod'}[-1];
178   }
179   push @links, \@new;
180 }
181
182 my @bottom_link;
183 if($opt{'bottom_link'}) {
184   my @speriod = (@{$data->{'speriod'}}, $data->{'speriod'}[0]);
185   my @eperiod = (@{$data->{'eperiod'}}, $data->{'eperiod'}[-1]);
186   
187   push @bottom_link, $opt{'bottom_link'};
188   while(@speriod) {
189     push @bottom_link, "$fromparam=". shift(@speriod). ";$toparam=". shift(@eperiod);
190   }
191 }
192
193 </%init>