de2b2e9d495f5fa29c7456ee04f7359a9e306eca
[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
23     #optional, pulled from CGI params if not specified
24     'start_month'     => $smonth,
25     'start_year'      => $syear,
26     'end_month'       => $emonth,
27     'end_year'        => $eyear,
28
29     #optional
30     'agentnum'        => $agentnum,
31     'nototal'         => 1,
32     'graph_type'      => 'LinesPoints',
33     'remove_empty'    => 1,
34     'bottom_total'    => 1,
35     'sprintf'         => '%u', #sprintf format, overrides default %.2f
36     'disable_money'   => 1,
37   );
38
39 </%doc>
40 <% include('report.html',
41             'items'         => $data->{'items'},
42             'data'          => $data->{'data'},
43             'row_labels'    => $data->{'item_labels'},
44             'graph_labels'  => $opt{'graph_labels'} || $data->{'item_labels'},
45             'col_labels'    => [ map { my $m = $_; $m =~ s/^(\d+)\//$mon[$1-1] / ; $m }
46                                  @{$data->{label}} ],
47             'axis_labels'   => $data->{label},
48             'colors'        => $data->{colors},
49             'links'         => \@links,
50             'bottom_link'   => \@bottom_link,
51             map { $_, $opt{$_} } (qw(title 
52                                     nototal 
53                                     graph_type 
54                                     bottom_total 
55                                     sprintf 
56                                     disable_money)),
57           ) %>
58 <%init>
59
60 my(%opt) = @_;
61
62 my $conf = new FS::Conf;
63 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
64
65 my $fromparam = $opt{'link_fromparam'} || 'begin';
66 my $toparam   = $opt{'link_toparam'} || 'end';
67
68 my @items = @{ $opt{'items'} };
69
70 foreach my $other (qw( labels graph_labels colors links )) {
71   if ( ref($opt{$other}) eq 'HASH' ) {
72     $opt{$other} = [ map $opt{$other}{$_}, @items ];
73   }
74 }
75
76 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
77
78 #find first month
79 $opt{'start_month'} ||= $cgi->param('start_month'); # || $curmon+1;
80 $opt{'start_year'}  ||= $cgi->param('start_year'); # || 1899+$curyear;
81
82 #find last month
83 $opt{'end_month'} ||= $cgi->param('end_month'); # || $curmon+1;
84 $opt{'end_year'}  ||= $cgi->param('end_year'); # || 1900+$curyear;
85
86 my $report = new FS::Report::Table::Monthly (
87
88   'items'        => \@items,
89   'params'       => $opt{'params'},
90   'item_labels'  => ( $cgi->param('_type') =~ /^(png)$/
91                         ? $opt{'graph_labels'}
92                         : $opt{'labels'}
93                     ),
94   'colors'       => $opt{'colors'},
95   'links'        => $opt{'links'},
96
97   'start_month'  => $opt{'start_month'},
98   'start_year'   => $opt{'start_year'},
99   'end_month'    => $opt{'end_month'},
100   'end_year'     => $opt{'end_year'},
101
102   'agentnum'     => $opt{'agentnum'},
103   'remove_empty' => $opt{'remove_empty'},
104   'doublemonths' => $opt{'doublemonths'},
105 );
106 my $data = $report->data;
107
108 my @links;
109 foreach my $link (@{ $data->{'links'} }) {
110   my @speriod = @{$data->{'speriod'}};
111   my @eperiod = @{$data->{'eperiod'}};
112   my ($begin, $end) = ($fromparam, $toparam);
113
114   my @new = ( $link );
115   if(ref($link)) {
116     $begin = $link->{'fromparam'};
117     $end   = $link->{'toparam'};
118     @new = ( $link->{'link'} );
119   }
120   while(@speriod) {
121     push @new, "$begin=". shift(@speriod).";$end=".shift(@eperiod);
122   }
123   if(! $opt{'nototal'}) {
124     push @new, "$begin=". $data->{'speriod'}[0] . ";$end=". $data->{'eperiod'}[-1];
125   }
126   push @links, \@new;
127 }
128
129 my @bottom_link;
130 if($opt{'bottom_link'}) {
131   my @speriod = (@{$data->{'speriod'}}, $data->{'speriod'}[0]);
132   my @eperiod = (@{$data->{'eperiod'}}, $data->{'eperiod'}[-1]);
133   
134   push @bottom_link, $opt{'bottom_link'};
135   while(@speriod) {
136     push @bottom_link, "$fromparam=". shift(@speriod). ";$toparam=". shift(@eperiod);
137   }
138 }
139
140 </%init>