add more options to advanced package reporting
[freeside.git] / httemplate / search / cust_pkg.cgi
1 <% include( 'elements/search.html',
2                   'title'       => 'Package Search Results', 
3                   'name'        => 'packages',
4                   'query'       => $sql_query,
5                   'count_query' => $count_query,
6                   #'redirect'    => $link,
7                   'header'      => [ '#',
8                                      'Package',
9                                      'Class',
10                                      'Status',
11                                      'Freq.',
12                                      'Setup',
13                                      'Last bill',
14                                      'Next bill',
15                                      'Susp.',
16                                      'Expire',
17                                      'Cancel',
18                                      FS::UI::Web::cust_header(
19                                        $cgi->param('cust_fields')
20                                      ),
21                                      'Services',
22                                    ],
23                   'fields'      => [
24                     'pkgnum',
25                     sub { #my $part_pkg = $part_pkg{shift->pkgpart};
26                           #$part_pkg->pkg; # ' - '. $part_pkg->comment;
27                           $_[0]->pkg; # ' - '. $_[0]->comment;
28                         },
29                     'classname',
30                     sub { ucfirst(shift->status); },
31                     sub { #shift->part_pkg->freq_pretty;
32
33                           #my $part_pkg = $part_pkg{shift->pkgpart};
34                           #$part_pkg->freq_pretty;
35
36                           FS::part_pkg::freq_pretty(shift);
37                         },
38
39                     #sub { time2str('%b %d %Y', shift->setup); },
40                     #sub { time2str('%b %d %Y', shift->last_bill); },
41                     #sub { time2str('%b %d %Y', shift->bill); },
42                     #sub { time2str('%b %d %Y', shift->susp); },
43                     #sub { time2str('%b %d %Y', shift->expire); },
44                     #sub { time2str('%b %d %Y', shift->get('cancel')); },
45                     ( map { time_or_blank($_) }
46                           qw( setup last_bill bill susp expire cancel ) ),
47
48                     \&FS::UI::Web::cust_fields,
49                     #sub { '<table border=0 cellspacing=0 cellpadding=0 STYLE="border:none">'.
50                     #      join('', map { '<tr><td align="right" style="border:none">'. $_->[0].
51                     #                     ':</td><td style="border:none">'. $_->[1]. '</td></tr>' }
52                     #                   shift->labels
53                     #          ).
54                     #      '</table>';
55                     #    },
56                     sub {
57                           [ map {
58                                   [ 
59                                     { 'data' => $_->[0]. ':',
60                                       'align'=> 'right',
61                                     },
62                                     { 'data' => $_->[1],
63                                       'align'=> 'left',
64                                       'link' => $p. 'view/' .
65                                                 $_->[2]. '.cgi?'. $_->[3],
66                                     },
67                                   ];
68                                 } shift->labels
69                           ];
70                         },
71                   ],
72                   'color' => [
73                     '',
74                     '',
75                     '',
76                     sub { shift->statuscolor; },
77                     '',
78                     '',
79                     '',
80                     '',
81                     '',
82                     '',
83                     '',
84                     FS::UI::Web::cust_colors(),
85                     '',
86                   ],
87                   'style' => [ '', '', '', 'b', '', '', '', '', '', '', '',
88                                FS::UI::Web::cust_styles() ],
89                   'size'  => [ '', '', '', '-1', ],
90                   'align' => 'rllclrrrrrr'. FS::UI::Web::cust_aligns(). 'r',
91                   'links' => [
92                     $link,
93                     $link,
94                     '',
95                     '',
96                     '',
97                     '',
98                     '',
99                     '',
100                     '',
101                     '',
102                     '',
103                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
104                           FS::UI::Web::cust_header(
105                                                     $cgi->param('cust_fields')
106                                                   )
107                     ),
108                     '',
109                   ],
110               )
111 %>
112 <%init>
113
114 die "access denied"
115   unless $FS::CurrentUser::CurrentUser->access_right('List packages');
116
117 # my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});
118
119 my($query) = $cgi->keywords;
120
121 my @where = ();
122
123 ##
124 # parse agent
125 ##
126
127 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
128   push @where,
129     "agentnum = $1";
130 }
131
132 ##
133 # parse status
134 ##
135
136 if (    $cgi->param('magic')  eq 'active'
137      || $cgi->param('status') eq 'active' ) {
138
139   push @where, FS::cust_pkg->active_sql();
140
141 } elsif (    $cgi->param('magic')  eq 'inactive'
142           || $cgi->param('status') eq 'inactive' ) {
143
144   push @where, FS::cust_pkg->inactive_sql();
145
146
147 } elsif (    $cgi->param('magic')  eq 'suspended'
148           || $cgi->param('status') eq 'suspended'  ) {
149
150   push @where, FS::cust_pkg->suspended_sql();
151
152 } elsif (    $cgi->param('magic')  =~ /^cancell?ed$/
153           || $cgi->param('status') =~ /^cancell?ed$/ ) {
154
155   push @where, FS::cust_pkg->cancelled_sql();
156
157 } elsif ( $cgi->param('status') =~ /^(one-time charge|inactive)$/ ) {
158
159   push @where, FS::cust_pkg->inactive_sql();
160
161 }
162
163 ###
164 # parse package class
165 ###
166
167 #false lazinessish w/graph/cust_bill_pkg.cgi
168 my $classnum = 0;
169 my @pkg_class = ();
170 if ( exists($cgi->Vars->{'classnum'})
171      && $cgi->param('classnum') =~ /^(\d*)$/
172    )
173 {
174   $classnum = $1;
175   if ( $classnum ) { #a specific class
176     push @where, "classnum = $classnum";
177
178     #@pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) );
179     #die "classnum $classnum not found!" unless $pkg_class[0];
180     #$title .= $pkg_class[0]->classname.' ';
181
182   } elsif ( $classnum eq '' ) { #the empty class
183
184     push @where, "classnum IS NULL";
185     #$title .= 'Empty class ';
186     #@pkg_class = ( '(empty class)' );
187   } elsif ( $classnum eq '0' ) {
188     #@pkg_class = qsearch('pkg_class', {} ); # { 'disabled' => '' } );
189     #push @pkg_class, '(empty class)';
190   } else {
191     die "illegal classnum";
192   }
193 }
194 #eslaf
195
196 ###
197 # parse part_pkg
198 ###
199
200 my $pkgpart = join (' OR pkgpart=',
201                     grep {$_} map { /^(\d+)$/; } ($cgi->param('pkgpart')));
202 push @where,  '(pkgpart=' . $pkgpart . ')' if $pkgpart;
203
204 ###
205 # parse dates
206 ###
207
208 my $orderby = '';
209
210 #false laziness w/report_cust_pkg.html
211 my %disable = (
212   'all'             => {},
213   'one-time charge' => { 'last_bill'=>1, 'bill'=>1, 'susp'=>1, 'expire'=>1, 'cancel'=>1, },
214   'active'          => { 'susp'=>1, 'cancel'=>1 },
215   'suspended'       => { 'cancel' => 1 },
216   'cancelled'       => {},
217   ''                => {},
218 );
219
220 foreach my $field (qw( setup last_bill bill susp expire cancel )) {
221
222   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
223
224   next if $beginning == 0 && $ending == 4294967295
225        or $disable{$cgi->param('status')}->{$field};
226
227   push @where,
228     "$field IS NOT NULL",
229     "$field >= $beginning",
230     "$field <= $ending";
231
232   $orderby ||= "ORDER BY $field";
233
234 }
235
236 $orderby ||= 'ORDER BY bill';
237
238 ###
239 # parse magic, legacy, etc.
240 ###
241
242 if ( $cgi->param('magic') &&
243      $cgi->param('magic') =~ /^(active|inactive|suspended|cancell?ed)$/
244 ) {
245
246   $orderby = 'ORDER BY pkgnum';
247
248   if ( $cgi->param('pkgpart') =~ /^(\d+)$/ ) {
249     push @where, "pkgpart = $1";
250   }
251
252 } elsif ( $query eq 'pkgnum' ) {
253
254   $orderby = 'ORDER BY pkgnum';
255
256 } elsif ( $query eq 'APKG_pkgnum' ) {
257
258   $orderby = 'ORDER BY pkgnum';
259
260   push @where, '0 < (
261     SELECT count(*) FROM pkg_svc
262      WHERE pkg_svc.pkgpart =  cust_pkg.pkgpart
263        AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
264                                  WHERE cust_svc.pkgnum  = cust_pkg.pkgnum
265                                    AND cust_svc.svcpart = pkg_svc.svcpart
266                               )
267   )';
268   
269 }
270
271 ##
272 # setup queries, links, subs, etc. for the search
273 ##
274
275 # here is the agent virtualization
276 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
277
278 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
279
280 my $addl_from = 'LEFT JOIN cust_main USING ( custnum  ) '.
281                 'LEFT JOIN part_pkg  USING ( pkgpart  ) '.
282                 'LEFT JOIN pkg_class USING ( classnum ) ';
283
284 my $count_query = "SELECT COUNT(*) FROM cust_pkg $addl_from $extra_sql";
285
286 my $sql_query = {
287   'table'     => 'cust_pkg',
288   'hashref'   => {},
289   'select'    => join(', ',
290                             'cust_pkg.*',
291                             ( map "part_pkg.$_", qw( pkg freq ) ),
292                             'pkg_class.classname',
293                             'cust_main.custnum as cust_main_custnum',
294                             FS::UI::Web::cust_sql_fields(
295                               $cgi->param('cust_fields')
296                             ),
297                  ),
298   'extra_sql' => "$extra_sql $orderby",
299   'addl_from' => $addl_from,
300 };
301
302 my $link = sub {
303   [ "${p}view/cust_main.cgi?".shift->custnum.'#cust_pkg', 'pkgnum' ];
304 };
305
306 my $clink = sub {
307   my $cust_pkg = shift;
308   $cust_pkg->cust_main_custnum
309     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
310     : '';
311 };
312
313 #if ( scalar(@cust_pkg) == 1 ) {
314 #  print $cgi->redirect("${p}view/cust_main.cgi?". $cust_pkg[0]->custnum.
315 #                       "#cust_pkg". $cust_pkg[0]->pkgnum );
316
317 #    my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
318 #    my $rowspan = scalar(@cust_svc) || 1;
319
320 #    my $n2 = '';
321 #    foreach my $cust_svc ( @cust_svc ) {
322 #      my($label, $value, $svcdb) = $cust_svc->label;
323 #      my $svcnum = $cust_svc->svcnum;
324 #      my $sview = $p. "view";
325 #      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
326 #            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
327 #      $n2="</TR><TR>";
328 #    }
329
330 sub time_or_blank {
331    my $column = shift;
332    return sub {
333      my $record = shift;
334      my $value = $record->get($column); #mmm closures
335      $value ? time2str('%b %d %Y', $value ) : '';
336    };
337 }
338
339 </%init>