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