rest of customer churn report, #30132
[freeside.git] / FS / FS / cust_main / Status.pm
1 package FS::cust_main::Status;
2
3 use strict;
4 use vars qw( $conf ); # $module ); #$DEBUG $me );
5 use Tie::IxHash;
6 use FS::UID;
7 use FS::cust_pkg;
8
9 #$DEBUG = 0;
10 #$me = '[FS::cust_main::Status]';
11
12 install_callback FS::UID sub { 
13   $conf = new FS::Conf;
14   #$module = $conf->config('cust_main-status_module') || 'Classic';
15 };
16
17 =head1 NAME
18
19 FS::cust_main::Status - Status mixin for cust_main
20
21 =head1 SYNOPSIS
22
23 =head1 DESCRIPTION
24
25 These methods are available on FS::cust_main objects:
26
27 =head1 METHODS
28
29 =over 4
30
31 =item statuscolors
32
33 Returns an (ordered with Tie::IxHash) hash reference of possible status
34 names and colors.
35
36 =cut
37
38 sub statuscolors {
39   #my $self = shift; #i guess i'm a class method
40
41   my %statuscolors;
42
43   my $module = $conf->config('cust_main-status_module') || 'Classic';
44
45   if ( $module eq 'Classic' ) {
46     tie %statuscolors, 'Tie::IxHash',
47       'prospect'  => '7e0079', #'000000', #black?  naw, purple
48       'active'    => '00CC00', #green
49       'ordered'   => '009999', #teal? cyan?
50       'inactive'  => '0000CC', #blue
51       'suspended' => 'FF9900', #yellow
52       'cancelled' => 'FF0000', #red
53     ;
54   } elsif ( $module eq 'Recurring' ) {
55     tie %statuscolors, 'Tie::IxHash',
56       'prospect'  => '7e0079', #'000000', #black?  naw, purple
57       'active'    => '00CC00', #green
58       'ordered'   => '009999', #teal? cyan?
59       'suspended' => 'FF9900', #yellow
60       'cancelled' => 'FF0000', #red
61       'inactive'  => '0000CC', #blue
62     ;
63   } else {
64     die "unknown status module $module";
65   }
66
67   \%statuscolors;
68
69 }
70
71 =item cancelled_sql
72
73 =cut
74
75 sub cancelled_sql {
76   my $self = shift;
77
78   my $recurring_sql = FS::cust_pkg->recurring_sql;
79   my $cancelled_sql = FS::cust_pkg->cancelled_sql;
80   my $select_count_pkgs = $self->select_count_pkgs_sql;
81
82   my $sql = "
83         0 < ( $select_count_pkgs )
84     AND 0 = ( $select_count_pkgs AND $recurring_sql
85                   AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
86             )
87     AND 0 < ( $select_count_pkgs AND $cancelled_sql   )
88   ";
89
90   my $module = $conf->config('cust_main-status_module') || 'Classic';
91
92   if ( $module eq 'Classic' ) {
93     $sql .=
94       " AND 0 = (  $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " ) ";
95   #} elsif ( $module eq 'Recurring' ) {
96   #} else {
97   #  die "unknown status module $module";
98   }
99
100   $sql;
101
102 }
103
104 =back
105
106 =head1 CLASS METHODS
107
108 =over 4
109
110 =item churn_sql START, END
111
112 Returns an SQL statement for the customer churn status query.  The columns
113 returned are the custnum and the number of active, suspended, and cancelled
114 packages (excluding one-time packages) at the start date ("s_active",
115 "s_suspended", and "s_cancelled") and the end date ("e_active", etc.).
116
117 =cut
118
119 # not sure this belongs here...FS::cust_main::Packages?
120
121 sub churn_sql {
122   my $self = shift;
123   my ($speriod, $eperiod) = @_;
124
125   my $s_sql = FS::h_cust_pkg->status_as_of_sql($speriod);
126   my $e_sql = FS::h_cust_pkg->status_as_of_sql($eperiod);
127
128   my @select = (
129     'custnum',
130     'COALESCE(SUM(s.is_active::int),0)     as s_active',
131     'COALESCE(SUM(s.is_suspended::int),0)  as s_suspended',
132     'COALESCE(SUM(s.is_cancelled::int),0)  as s_cancelled',
133     'COALESCE(SUM(e.is_active::int),0)     as e_active',
134     'COALESCE(SUM(e.is_suspended::int),0)  as e_suspended',
135     'COALESCE(SUM(e.is_cancelled::int),0)  as e_cancelled',
136   );
137   my $from = "($s_sql) AS s FULL JOIN ($e_sql) AS e USING (custnum)";
138
139   return "SELECT ".join(',', @select)." FROM $from GROUP BY custnum";
140 }
141
142 =head1 BUGS
143
144 =head1 SEE ALSO
145
146 L<FS::cust_main>
147
148 =cut
149
150 1;
151