RT# 78019 - Added total revenue line to Package churn report
[freeside.git] / httemplate / graph / cust_pkg.html
1 <& elements/monthly.html,
2   'title'         => $agentname. 'Package Churn',
3   'items'         => \@items,
4   'labels'        => \@labels,
5   'graph_labels'  => \@labels,
6   'colors'        => \@colors,
7   'links'         => \@links,
8   'params'        => \@params,
9   'agentnum'      => $agentnum,
10   'sprintf'       => ( $normalize ? '%0.1f%%' : '%u'),
11   'sprintf_fields' => $sprintf_fields,
12   'normalize'     => ( $normalize ? 0 : undef ),
13   'disable_money' => 1,
14   'remove_empty'  => (scalar(@group_keys) > 1 ? 1 : 0),
15   'nototal'       => 1,
16   'no_graph'      => [ 1, 0, 0, 0, 0, 1 ], # don't graph 'active, total_revenue'
17 &>
18 <%init>
19
20 #XXX use a different ACL for package churn?
21 my $curuser = $FS::CurrentUser::CurrentUser;
22 die "access denied"
23   unless $curuser->access_right('Financial reports');
24
25 #false laziness w/money_time.cgi, cust_bill_pkg.cgi
26
27 #XXX or virtual
28 my( $agentnum, $agent ) = ('', '');
29 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
30   $agentnum = $1;
31   $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
32   die "agentnum $agentnum not found!" unless $agent;
33 }
34
35 my $agentname = $agent ? $agent->agent.' ' : '';
36
37 my @base_items = qw( active_pkg setup_pkg susp_pkg unsusp_pkg cancel_pkg total_revenue_pkg );
38
39 my %base_labels = (
40   'active_pkg' => 'Active packages',
41   'setup_pkg'  => 'New orders',
42   'susp_pkg'   => 'Suspensions',
43   'unsusp_pkg' => 'Unsuspensions',
44   'cancel_pkg' => 'Cancellations',
45   'total_revenue_pkg' => 'Total Revenue'
46 );
47
48 my %base_colors = (
49   'active_pkg'  => '000000', #black
50   'setup_pkg'   => '00cc00', #green
51   'susp_pkg'    => 'ff9900', #yellow
52   'unsusp_pkg'  => '44ff44', #light green
53   'cancel_pkg'  => 'cc0000', #red 
54   'total_revenue_pkg'  => '0000ff', #blue
55 );
56
57 my $sprintf_fields = {
58   'total_revenue_pkg' => '%.2f', #format to 2 decimal places
59 };
60
61 my %base_links;
62 foreach my $status (qw(active setup cancel susp unsusp)) {
63   $base_links{$status.'_pkg'} =
64     "${p}search/cust_pkg_churn.html?agentnum=$agentnum;status=$status;";
65 }
66
67 my %filter_params = (
68   # not agentnum, that's elsewhere
69   'refnum'      => [ $cgi->param('refnum') ],
70   'classnum'    => [ $cgi->param('classnum') ],
71   'towernum'    => [ $cgi->param('towernum') ],
72 );
73 if ( $cgi->param('zip') =~ /^(\w+)/ ) {
74   $filter_params{zip} = $1;
75 }
76 foreach my $link (values %base_links) {
77   foreach my $key (keys(%filter_params)) {
78     my $value = $filter_params{$key};
79     if (ref($value)) {
80       $value = join(',', @$value);
81     }
82     $link .= "$key=$value;" if length($value);
83   }
84 }
85
86
87 # In order to keep this from being the same trainwreck as cust_bill_pkg.cgi,
88 # we allow ONE breakdown axis, besides the setup/susp/cancel inherent in 
89 # the report.
90
91 my $breakdown = $cgi->param('breakdown_by');
92 my ($name_col, $table);
93 if ($breakdown eq 'classnum') {
94   $table = 'pkg_class';
95   $name_col = 'classname';
96 } elsif ($breakdown eq 'refnum') {
97   $table = 'part_referral';
98   $name_col = 'referral';
99 } elsif ($breakdown eq 'towernum') {
100   $table = 'tower';
101   $name_col = 'towername';
102 } elsif ($breakdown) {
103   die "unknown breakdown column '$breakdown'\n";
104 }
105
106 my @group_keys;
107 my @group_labels;
108 if ( $table ) {
109   my @groups;
110   if ( $cgi->param($breakdown) ) {
111     foreach my $key ($cgi->param($breakdown)) {
112       next if $key =~ /\D/;
113       push @groups, qsearch( $table, { $breakdown => $key });
114     }
115   } else {
116     @groups = qsearch( $table );
117   }
118   foreach (@groups) {
119     push @group_keys, $_->get($breakdown);
120     push @group_labels, $_->get($name_col);
121   }
122 }
123
124 my (@items, @labels, @colors, @links, @params);
125 if (scalar(@group_keys) > 1) {
126   my $hue = 180;
127   foreach my $key (@group_keys) {
128     # this gives a decent level of contrast as long as there aren't too many
129     # result sets
130     my $scheme = Color::Scheme->new
131       ->scheme('triade')
132       ->from_hue($hue)
133       ->distance(0.5);
134     my $label = shift @group_labels;
135     my $i = 0; # item index
136     foreach (@base_items) {
137       # append the item
138       push @items, $_;
139       # and its parameters
140       push @params, [
141         %filter_params,
142         $breakdown => $key
143       ];
144       # and a label prefixed with the group label
145       push @labels, "$label - $base_labels{$_}";
146       # and colors (?!)
147       push @colors, $scheme->colorset->[$i]->[1];
148       # and links...
149       my $this_link = $base_links{$_};
150       $this_link .= "$breakdown=$key;";
151       push @links, $this_link;
152       $i++;
153     } #foreach (@base_items
154     $hue += 35;
155   } # foreach @group_keys
156 } else {
157   @items = @base_items;
158   @labels = @base_labels{@base_items};
159   @colors = @base_colors{@base_items};
160   @links = @base_links{@base_items};
161   @params = map { [ %filter_params ] } @base_items;
162 }
163
164 my $normalize = $cgi->param('normalize');
165
166 </%init>