send commission reports by email, #33101
[freeside.git] / httemplate / search / cust_main_churn.html
1 <& elements/search.html,
2   'title'       => $title,
3   'name'        => 'customers',
4   'query'       => $query,
5   'count_query' => $count_query,
6   'header'      => [ emt('#'),
7                      FS::UI::Web::cust_header(
8                        $cgi->param('cust_fields')
9                      ),
10                      { label => "Packages on $start_date", colspan => 3 },
11                      '',
12                      '',
13                      { label => "Packages on $end_date", colspan => 3 },
14                      '',
15                      '',
16                    ],
17   'header2'     => [ '',
18                      map({ '' } (FS::UI::Web::cust_header())),
19                      'Active',
20                      'Suspended',
21                      'Cancelled',
22                      'Active',
23                      'Suspended',
24                      'Cancelled',
25                    ],
26   'fields'      => [
27     'custnum',
28     \&FS::UI::Web::cust_fields,
29     's_active',
30     's_suspended',
31     's_cancelled',
32     'e_active',
33     'e_suspended',
34     'e_cancelled',
35   ],
36   'color' => [
37     '',
38     FS::UI::Web::cust_colors(),
39     # package colors here
40     '00CC00',
41     'FF9900',
42     'FF0000',
43
44     '00CC00',
45     'FF9900',
46     'FF0000',
47   ],
48   'style' => [ '',
49                FS::UI::Web::cust_styles(),
50                '' ],
51   'align' => 'r'. FS::UI::Web::cust_aligns(). 'rrrrrr',
52   'links' => [
53     '',
54     ( map { $_ ne 'Cust. Status' ? $clink : '' }
55           FS::UI::Web::cust_header(
56                                     $cgi->param('cust_fields')
57                                   )
58     ),
59     '',
60   ],
61 &>
62 <%init>
63
64 my $curuser = $FS::CurrentUser::CurrentUser;
65
66 die "access denied"
67   unless $curuser->access_right('List customers');
68
69 my($speriod, $eperiod) = FS::UI::Web::parse_beginning_ending($cgi);
70 my $start_date = time2str('%b %o, %Y', $speriod);
71 my $end_date = time2str('%b %o, %Y', $eperiod);
72
73 my $agentnum;
74 if ($cgi->param('agentnum') =~ /^(\d+)$/) {
75   $agentnum = $1;
76 }
77
78 # can't use this directly as it doesn't have any cust_main fields.
79 my $churn = FS::cust_main::Status->churn_sql($speriod, $eperiod);
80
81 my $query = {
82   'table'     => 'cust_main',
83   'select'    => 'cust_main.*, churn.*',
84   'addl_from' => " JOIN ($churn) AS churn USING (custnum) ",
85 };
86 my $count_query = "SELECT COUNT(*) FROM cust_main JOIN ($churn) AS churn USING (custnum)";
87 my @where;
88
89 my $status = $cgi->param('status');
90 my $title;
91 if ( $status eq 'active' ) {
92   $title = "Customers active on $start_date";
93   push @where, "s_active > 0";
94 } elsif ( $status eq 'started' ) {
95   $title = "Customers starting service, $start_date - $end_date";
96   push @where, "s_active = 0 and e_active > 0";
97 } elsif ( $status eq 'suspended' ) {
98   $title = "Customers suspended, $start_date - $end_date";
99   push @where, "s_active > 0 and e_active = 0 and e_suspended > 0";
100 } elsif ( $status eq 'resumed' ) {
101   $title = "Customers resuming service, $start_date - $end_date";
102   push @where, "s_active = 0 and s_suspended > 0 and e_active > 0";
103 } elsif ( $status eq 'cancelled' ) {
104   $title = "Customers cancelled, $start_date - $end_date";
105   push @where, "s_active > 0 and e_active = 0 and e_suspended = 0";
106 }
107
108 if ($agentnum) {
109   push @where, "agentnum = $agentnum";
110 }
111
112 if ( @where ) {
113   my $where = " WHERE ". join(' AND ', @where);
114   $query->{extra_sql} = $where;
115   $count_query .= $where;
116 }
117
118 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
119
120 </%init>