sort by referral count, RT#75757
[freeside.git] / httemplate / search / cust_pkg_churn.html
1 <& elements/search.html,
2                   'title'       => $title,
3                   'name'        => 'packages',
4                   'query'       => $sql_query,
5                   'count_query' => $count_query,
6                   'header'      => [ emt('#'),
7                                      emt('Quantity'),
8                                      emt('Package'),
9                                      emt('Class'),
10                                      emt('Sales Person'),
11                                      emt('Ordered by'),
12                                      emt('Setup Fee'),
13                                      emt('Base Recur'),
14                                      emt('Freq.'),
15                                      emt('Setup'),
16                                      emt('Last bill'),
17                                      emt('Next bill'),
18                                      emt('Susp.'),
19                                      emt('Changed'),
20                                      emt('Cancel'),
21                                      @reason_header,
22                                      FS::UI::Web::cust_header(
23                                        $cgi->param('cust_fields')
24                                      ),
25                                      #emt('Services'), # even harder
26                                    ],
27                   'fields'      => [
28                     'pkgnum',
29                     'quantity',
30                     'pkg',
31                     'classname',
32                     'salesperson',
33                     'otaker',
34                     sub { sprintf( $money_char.'%.2f',
35                                    shift->part_pkg->option('setup_fee'),
36                                  );
37                         },
38                     sub { my $c = shift;
39                           sprintf( $money_char.'%.2f',
40                                    $c->part_pkg->base_recur($c)
41                                  );
42                         },
43                     sub { FS::part_pkg::freq_pretty(shift); },
44
45                     ( map { time_or_blank($_) }
46                       qw( setup last_bill bill susp change_date cancel ) ),
47
48                     @reason_fields,
49                     \&FS::UI::Web::cust_fields,
50                   ],
51                   'sort_fields' => [
52                     'cust_pkg.pkgnum',
53                     ('') x 5, # can use as-is
54                     ('') x 3, # can't use at all
55                     # use the plain SQL column names
56                     qw( setup last_bill bill susp change_date cancel ),
57                     @reason_blank,
58                     # cust_fields can take care of themselves
59                   ],
60                   'color' => [
61                     ('') x 15,
62                     @reason_blank,
63                     FS::UI::Web::cust_colors(),
64                   ],
65                   'style' => [ ('') x 15,
66                                @reason_blank,
67                                FS::UI::Web::cust_styles() ],
68                   'size'  => [ '', '', '', '', '-1' ],
69                   'align' => 'rrlcccrrlrrrrrr'.$reason_align. FS::UI::Web::cust_aligns(). 'r',
70                   'links' => [
71                     $link,
72                     $link,
73                     $link,
74                     ('') x 12,
75                     @reason_blank,
76                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
77                           FS::UI::Web::cust_header(
78                                                     $cgi->param('cust_fields')
79                                                   )
80                     ),
81                   ],
82 &>
83 <%once>
84 my %title = (
85   'active' => 'Active packages as of ',
86   'setup'  => 'Packages started between ',
87   'cancel' => 'Packages canceled between ',
88   'susp'   => 'Packages suspended between ',
89   'unsusp' => 'Packages unsuspended between ',
90 );
91 </%once>
92 <%init>
93
94 my $curuser = $FS::CurrentUser::CurrentUser;
95
96 die "access denied"
97   unless $curuser->access_right('List packages');
98
99 my $conf = new FS::Conf;
100 my $money_char = $conf->config('money_char') || '$';
101
102 my %search_hash = ();
103
104 # pass a very limited set of parameters through
105 #scalars
106 for (qw( agentnum zip )) 
107 {
108   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
109 }
110
111 #arrays / comma-separated lists
112 for my $param (qw( pkgpart classnum refnum towernum )) {
113   my @values = map { split(',') } $cgi->param($param);
114   $search_hash{$param} = \@values if scalar(@values);
115 }
116
117 ###
118 # do not pass dates to FS::cust_pkg->search; use the special churn_fromwhere
119 # logic.
120 ###
121
122 my $pkg_query = FS::cust_pkg->search(\%search_hash);
123 #warn Dumper $pkg_query;
124
125 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
126 my $status = $cgi->param('status');
127
128 my $title = emt($title{$status}) .
129             time2str('%b %o %Y', $beginning);
130 if ($status ne 'active') {
131   $title .= emt(' to ') . time2str('%b %o %Y', $ending);
132 }
133
134 my ($from, @where) = FS::h_cust_pkg->churn_fromwhere_sql($status, $beginning, $ending);
135
136 push @where, "freq != '0'";
137
138 # split off the primary table name
139 $from =~ s/^(\w+)(.*)$/$2/s;
140 my $table = $1;
141
142 # merge with $pkg_query
143 $from .= ' ' . $pkg_query->{addl_from};
144
145 my $extra_sql;
146 if ($pkg_query->{extra_sql}) {
147   $extra_sql = $pkg_query->{extra_sql} . ' AND ';
148 } else {
149   $extra_sql = 'WHERE ';
150 }
151 $extra_sql .= join(' AND ', @where);
152
153 my $sql_query = {
154   'select'    => $pkg_query->{select},
155   'table'     => $table,
156   'addl_from' => $from,
157   'extra_sql' => $extra_sql,
158 };
159 warn (Dumper $sql_query) if $cgi->param('debug');
160
161 my $count_query = "SELECT COUNT(*) FROM $table $from $extra_sql";
162
163 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
164              ? ''
165              : ';show=packages';
166
167 my $link = sub {
168   my $self = shift;
169   my $frag = 'cust_pkg'. $self->pkgnum; #hack for IE ignoring real #fragment
170   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
171                            "$show;fragment=$frag#cust_pkg",
172     'pkgnum'
173   ];
174 };
175
176 my $clink = sub {
177   my $cust_pkg = shift;
178   $cust_pkg->cust_main_custnum
179     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
180     : '';
181 };
182
183 sub time_or_blank {
184    my $column = shift;
185    return sub {
186      my $record = shift;
187      my $value = $record->get($column); #mmm closures
188      $value ? time2str('%b %d %Y', $value ) : '';
189    };
190 }
191
192 my (@reason_header,@reason_fields,@reason_blank);
193 my $reason_align = '';
194 if ($status eq 'cancel') {
195   push @reason_header, emt('Cancel Reason');
196   push @reason_fields, sub {
197     my $c = shift;
198     my $cust_pkg_reason = $c->last_cust_pkg_reason('cancel');
199     $cust_pkg_reason ? $cust_pkg_reason->reason->reason : '';
200   };
201   push @reason_blank, '';
202   $reason_align = 'l';
203 }
204 </%init>