summaryrefslogtreecommitdiff
path: root/httemplate/graph/cust_pkg.html
blob: 6aea10492f335bfa2691e0c982481030ce3be8e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<& elements/monthly.html,
  'title'         => $agentname. 'Package Churn',
  'items'         => \@items,
  'labels'        => \@labels,
  'graph_labels'  => \@labels,
  'colors'        => \@colors,
  'links'         => \@links,
  'params'        => \@params,
  'agentnum'      => $agentnum,
  'sprintf'       => ( $normalize ? '%0.1f%%' : '%u'),
  'sprintf_fields' => $sprintf_fields,
  'normalize'     => ( $normalize ? 0 : undef ),
  'disable_money' => 1,
  'remove_empty'  => (scalar(@group_keys) > 1 ? 1 : 0),
  'nototal'       => 1,
  'no_graph'      => [ 1, 0, 0, 0, 0, 1, 1 ], # don't graph 'active, total_revenue, total_revenue_diff'
&>
<%init>

#XXX use a different ACL for package churn?
my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied"
  unless $curuser->access_right('Financial reports');

#false laziness w/money_time.cgi, cust_bill_pkg.cgi

#XXX or virtual
my( $agentnum, $agent ) = ('', '');
if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
  $agentnum = $1;
  $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
  die "agentnum $agentnum not found!" unless $agent;
}

my $agentname = $agent ? $agent->agent.' ' : '';

my @base_items = qw( active_pkg setup_pkg susp_pkg unsusp_pkg cancel_pkg total_revenue_pkg total_revenue_diff );

my %base_labels = (
  'active_pkg' => 'Active packages',
  'setup_pkg'  => 'New orders',
  'susp_pkg'   => 'Suspensions',
  'unsusp_pkg' => 'Unsuspensions',
  'cancel_pkg' => 'Cancellations',
  'total_revenue_pkg' => 'Total Revenue',
  'total_revenue_diff' => 'Revenue Difference',
);

my %base_colors = (
  'active_pkg'  => '000000', #black
  'setup_pkg'   => '00cc00', #green
  'susp_pkg'    => 'ff9900', #yellow
  'unsusp_pkg'  => '44ff44', #light green
  'cancel_pkg'  => 'cc0000', #red 
  'total_revenue_pkg'  => '0000ff', #blue
  'total_revenue_diff'  => '0000ff', #blue
);

my $sprintf_fields = {
  'total_revenue_pkg' => '%.2f', #format to 2 decimal places
  'total_revenue_diff' => '%.2f', #format to 2 decimal places
};

my %base_links;
foreach my $status (qw(active setup cancel susp unsusp)) {
  $base_links{$status.'_pkg'} =
    "${p}search/cust_pkg_churn.html?agentnum=$agentnum;status=$status;";
}

my %filter_params = (
  # not agentnum, that's elsewhere
  'refnum'      => [ $cgi->param('refnum') ],
  'classnum'    => [ $cgi->param('classnum') ],
  'towernum'    => [ $cgi->param('towernum') ],
);
if ( $cgi->param('zip') =~ /^(\w+)/ ) {
  $filter_params{zip} = $1;
}
foreach my $link (values %base_links) {
  foreach my $key (keys(%filter_params)) {
    my $value = $filter_params{$key};
    if (ref($value)) {
      $value = join(',', @$value);
    }
    $link .= "$key=$value;" if length($value);
  }
}


# In order to keep this from being the same trainwreck as cust_bill_pkg.cgi,
# we allow ONE breakdown axis, besides the setup/susp/cancel inherent in 
# the report.

my $breakdown = $cgi->param('breakdown_by');
my ($name_col, $table);
if ($breakdown eq 'classnum') {
  $table = 'pkg_class';
  $name_col = 'classname';
} elsif ($breakdown eq 'refnum') {
  $table = 'part_referral';
  $name_col = 'referral';
} elsif ($breakdown eq 'towernum') {
  $table = 'tower';
  $name_col = 'towername';
} elsif ($breakdown) {
  die "unknown breakdown column '$breakdown'\n";
}

my @group_keys;
my @group_labels;
if ( $table ) {
  my @groups;
  if ( $cgi->param($breakdown) ) {
    foreach my $key ($cgi->param($breakdown)) {
      next if $key =~ /\D/;
      push @groups, qsearch( $table, { $breakdown => $key });
    }
  } else {
    @groups = qsearch( $table );
  }
  foreach (@groups) {
    push @group_keys, $_->get($breakdown);
    push @group_labels, $_->get($name_col);
  }
}

my (@items, @labels, @colors, @links, @params);
if (scalar(@group_keys) > 1) {
  my $hue = 180;
  foreach my $key (@group_keys) {
    # this gives a decent level of contrast as long as there aren't too many
    # result sets
    my $scheme = Color::Scheme->new
      ->scheme('triade')
      ->from_hue($hue)
      ->distance(0.5);
    my $label = shift @group_labels;
    my $i = 0; # item index
    foreach (@base_items) {
      # append the item
      push @items, $_;
      # and its parameters
      push @params, [
        %filter_params,
        $breakdown => $key
      ];
      # and a label prefixed with the group label
      push @labels, "$label - $base_labels{$_}";
      # and colors (?!)
      push @colors, $scheme->colorset->[$i]->[1];
      # and links...
      my $this_link = $base_links{$_};
      $this_link .= "$breakdown=$key;";
      push @links, $this_link;
      $i++;
    } #foreach (@base_items
    $hue += 35;
  } # foreach @group_keys
} else {
  @items = @base_items;
  @labels = @base_labels{@base_items};
  @colors = @base_colors{@base_items};
  @links = @base_links{@base_items};
  @params = map { [ %filter_params ] } @base_items;
}

my $normalize = $cgi->param('normalize');

</%init>