invoice_sections_with_taxes per-agent, RT#79636
[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   'sort_fields' => [
37     'custnum',
38     FS::UI::Web::cust_sql_fields,
39     's_active',
40     's_suspended',
41     's_cancelled',
42     'e_active',
43     'e_suspended',
44     'e_cancelled',
45   ],
46   'color' => [
47     '',
48     FS::UI::Web::cust_colors(),
49     # package colors here
50     '00CC00',
51     'FF9900',
52     'FF0000',
53
54     '00CC00',
55     'FF9900',
56     'FF0000',
57   ],
58   'style' => [ '',
59                FS::UI::Web::cust_styles(),
60                '' ],
61   'align' => 'r'. FS::UI::Web::cust_aligns(). 'rrrrrr',
62   'links' => [
63     '',
64     ( map { $_ ne 'Cust. Status' ? $clink : '' }
65           FS::UI::Web::cust_header(
66                                     $cgi->param('cust_fields')
67                                   )
68     ),
69     '',
70   ],
71 &>
72 <%init>
73
74 my $curuser = $FS::CurrentUser::CurrentUser;
75
76 die "access denied"
77   unless $curuser->access_right('List customers');
78
79 my($speriod, $eperiod) = FS::UI::Web::parse_beginning_ending($cgi);
80 my $start_date = time2str('%b %o, %Y', $speriod);
81 my $end_date = time2str('%b %o, %Y', $eperiod);
82
83 my $agentnum;
84 if ($cgi->param('agentnum') =~ /^(\d+)$/) {
85   $agentnum = $1;
86 }
87
88 # can't use this directly as it doesn't have any cust_main fields.
89 my $churn = FS::cust_main::Status->churn_sql($speriod, $eperiod);
90 my $select = join(',',
91   'churn.*',
92   'cust_main.*',
93   FS::UI::Web::cust_sql_fields()
94 );
95
96 my $addl_from = " JOIN ($churn) AS churn USING (custnum) ".
97                 FS::UI::Web::join_cust_main('cust_main.custnum') ;
98 my $query = {
99   'table'     => 'cust_main',
100   'select'    => $select,
101   'addl_from' => $addl_from,
102 };
103 my $count_query = "SELECT COUNT(*) FROM cust_main $addl_from";
104 my @where;
105
106 my $status = $cgi->param('status');
107 my $title;
108 if ( $status eq 'active' ) {
109   $title = "Customers active on $start_date";
110   push @where, "s_active > 0";
111 } elsif ( $status eq 'started' ) {
112   $title = "Customers starting service, $start_date - $end_date";
113   push @where, "s_active = 0 and e_active > 0";
114 } elsif ( $status eq 'suspended' ) {
115   $title = "Customers suspended, $start_date - $end_date";
116   push @where, "s_active > 0 and e_active = 0 and e_suspended > 0";
117 } elsif ( $status eq 'resumed' ) {
118   $title = "Customers resuming service, $start_date - $end_date";
119   push @where, "s_active = 0 and s_suspended > 0 and e_active > 0";
120 } elsif ( $status eq 'cancelled' ) {
121   $title = "Customers cancelled, $start_date - $end_date";
122   push @where, "s_active > 0 and e_active = 0 and e_suspended = 0";
123 }
124
125 if ($agentnum) {
126   push @where, "agentnum = $agentnum";
127 }
128
129 if ( @where ) {
130   my $where = " WHERE ". join(' AND ', @where);
131   $query->{extra_sql} = $where;
132   $count_query .= $where;
133 }
134
135 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
136
137 </%init>