quiet warnings about CGI::param in list context
[freeside.git] / httemplate / search / svc_acct.cgi
1 <& elements/svc_Common.html,
2                  'title'       => emt('Account Search Results'),
3                  'name'        => emt('accounts'),
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'redirect'    => $link,
7                  'header'      => \@header,
8                  'fields'      => \@fields,
9                  'links'       => \@links,
10                  'align'       => $align,
11                  'color'       => \@color,
12                  'style'       => \@style,
13                  'footer'      => \@footer,
14                  'html_init'   => include('/elements/email-link.html',
15                                     'search_hash' => \%search_hash,
16                                     'table'       => 'svc_acct'
17                                   ),
18 &>
19 <%once>
20
21 #false laziness w/ClientAPI/MyAccount.pm
22 sub format_time { 
23   my $support = shift;
24   (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
25 }
26
27 sub timelast {
28   my( $svc_acct, $last, $permonth ) = @_;
29
30   my $sql = "
31     SELECT SUM(support) FROM acct_rt_transaction
32       LEFT JOIN Transactions
33         ON Transactions.Id = acct_rt_transaction.transaction_id
34     WHERE svcnum = ? 
35       AND Transactions.Created >= ?
36   ";
37
38   my $sth = dbh->prepare($sql) or die dbh->errstr;
39   $sth->execute( $svc_acct->svcnum,
40                  time2str('%Y-%m-%d %X', time - $last*86400 ) 
41                )
42     or die $sth->errstr;
43
44   my $seconds = $sth->fetchrow_arrayref->[0];
45
46   #my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
47   my $return = (($seconds < 0) ? '-' : '') . format_time($seconds);
48
49   $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
50
51   $return;
52
53 }
54
55 </%once>
56 <%init>
57
58 my $curuser =  $FS::CurrentUser::CurrentUser;
59
60 die "access denied" unless $curuser->access_right('List services');
61
62 my %cust_pkg_cache;
63
64 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
65 my $link_cust = sub {
66   my $svc_acct = shift;
67   if ( $svc_acct->custnum ) {
68     [ "${p}view/cust_main.cgi?", 'custnum' ];
69   } else {
70     '';
71   }
72 };
73
74 my %search_hash = ();
75 my @extra_sql = ();
76
77 my @header = ( 'Service', 'Account' );
78 my @fields = ( 'svc', 'email' );
79 my @links = ( $link, $link );
80 my $align = 'll';
81 my @color = ( '', '' );
82 my @style = ( '', '' );
83 my @footer = ();
84
85 my $conf = new FS::Conf;
86
87 if ( $conf->exists('report-showpasswords') #its a terrible idea
88      && $curuser->access_right('List service passwords') #but if you insist...
89    )
90 {
91   push @header, emt('Password');
92   push @fields, 'get_cleartext_password';
93   push @links, $link;
94   $align .= 'l';
95   push @color, '';
96   push @style, '';
97 }
98
99 push @header, emt('Real Name');
100 push @fields, 'finger';
101 push @links, $link;
102 $align .= 'l';
103 push @color, '';
104 push @style, '';
105
106 #hide the UID, its much less useful these days
107 if ( $cgi->param('show_uid') ) { #XXX add a checkbox
108   push @header, emt('UID');
109   push @fields, 'uid';
110   push @links, $link;
111   $align .= 'l';
112   push @color, '';
113   push @style, '';
114 }
115
116 push @header, emt('Last Login');
117 push @fields, 'last_login_text';
118 push @links, $link;
119 $align .= 'r';
120 push @color, '';
121 push @style, '';
122
123
124 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
125   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
126 }
127
128 for (qw( towernum sectornum )) {
129   $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
130 }
131
132 my $timepermonth = '';
133
134 my $orderby = 'ORDER BY svcnum';
135 my $addl_from = '';
136 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
137
138   $search_hash{'unlinked'} = 1
139     if $cgi->param('magic') eq 'unlinked';
140
141   my $sortby = '';
142   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
143     $sortby = $1;
144     $sortby = "LOWER($sortby)"
145       if $sortby eq 'username';
146     push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
147       if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
148     $orderby = "ORDER BY $sortby";
149   }
150
151   if ( $sortby eq 'seconds' ) {
152     my $tot_time = 0;
153     push @header, emt('Time');
154
155     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
156       push @fields, sub { my $svc_acct = shift;
157                           my $seconds = $svc_acct->seconds;
158                           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
159                           my $part_pkg = $cust_pkg->part_pkg;
160
161                           $tot_time += $svc_acct->seconds;
162
163                           $timepermonth = $part_pkg->option('seconds');
164                           $timepermonth = $timepermonth / $part_pkg->freq
165                             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
166                           my $recur = $part_pkg->base_recur($cust_pkg);
167
168                           return format_time($seconds)
169                             unless $timepermonth && $recur;
170
171                           format_time($seconds).
172                             sprintf(' (%.2fx monthly)', $seconds / $timepermonth );
173
174                         };
175     } else {
176       push @fields, sub { my $svc_acct = shift;
177                           $tot_time += $svc_acct->seconds;
178                           format_time($svc_acct->seconds);
179                         };
180     }
181
182     push @links, '';
183     $align .= 'r';
184     push @color, '';
185     push @style, '';
186
187     @footer = ( 'Total', '', '', '',
188                 sub { format_time($tot_time) }, #time
189               );
190
191     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
192       my $tot_paid_time = 0;
193       my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
194       push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
195       push @fields,
196         sub {
197           my $svc_acct = shift;
198           my $seconds = $svc_acct->seconds;
199           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
200           my $part_pkg = $cust_pkg->part_pkg;
201
202           #my $timepermonth = $part_pkg->option('seconds');
203           $timepermonth = $part_pkg->option('seconds');
204           $timepermonth = $timepermonth / $part_pkg->freq
205             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
206
207           #my $recur = $part_pkg->calc_recur($cust_pkg);
208           my $recur = $part_pkg->base_recur($cust_pkg);
209
210           return format_time($seconds) unless $timepermonth && $recur;
211
212           my $balance = $cust_pkg->cust_main->balance;
213           my $periods_unpaid = $balance / $recur;
214           my $time_unpaid = $periods_unpaid * $timepermonth;
215           $time_unpaid *= $part_pkg->freq
216             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
217           $tot_paid_time += $seconds-$time_unpaid;
218           format_time($seconds-$time_unpaid).
219             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
220         },
221         sub { timelast( shift, 30, $timepermonth ); },
222         sub { timelast( shift, 60, $timepermonth ); },
223         sub { timelast( shift, 90, $timepermonth ); },
224       ;
225       push @links, '', '', '', '';
226       $align .= 'rrrr';
227       push @color, '', '', '', '';
228       push @style, '', '', '', '';
229       push @footer, 
230         sub { format_time($tot_paid_time) }, #paid time
231         '', #XXX sub { $tot{'30'} }, #30
232         '', #XXX sub { $tot{'60'} }, #60
233         '', #XXX sub { $tot{'90'} }, #90
234       ;
235     }
236
237     push @footer, '', '';
238
239   }
240
241 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
242
243   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
244     my $sortby = $1;
245     $sortby = "LOWER($sortby)"
246       if $sortby eq 'username';
247     push @extra_sql, "last_login IS NULL";
248     $orderby = "ORDER BY $sortby";
249   }
250
251 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
252
253   $orderby = "";
254
255   $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
256
257   $search_hash{'svcpart'} = [ $cgi->param('svcpart') ];
258
259   foreach my $field (qw( last_login last_logout )) {
260
261     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
262
263     next if $beginning == 0 && $ending == 4294967295;
264
265     if ($cgi->param($field."_invert")) {
266       push @extra_sql,
267         "(svc_acct.$field IS NULL OR ".
268         "svc_acct.$field < $beginning AND ".
269         "svc_acct.$field > $ending)";
270     } else {
271       push @extra_sql,
272         "svc_acct.$field IS NOT NULL",
273         "svc_acct.$field >= $beginning",
274         "svc_acct.$field <= $ending";
275     }
276   
277     $orderby ||= "ORDER BY svc_acct.$field" .
278       ($cgi->param($field."_invert") ? ' DESC' : '');
279
280   }
281
282   $orderby ||= "ORDER BY svcnum";
283
284 } elsif ( $cgi->param('popnum') ) {
285   $orderby = "ORDER BY LOWER(username)";
286 } elsif ( $cgi->param('svcpart') ) {
287   $orderby = "ORDER BY uid";
288   #$orderby = "ORDER BY svcnum";
289   if ( defined($cgi->param('cancelled')) ) {
290     $search_hash{'cancelled'} = $cgi->param('cancelled') ? 1 : 0;
291   }
292 } else {
293   $orderby = "ORDER BY uid";
294
295   my @username_sql;
296
297   my %username_type;
298   foreach ( $cgi->param('username_type') ) {
299     $username_type{$_}++;
300   }
301
302   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
303   my $username = lc($1);
304
305   push @username_sql, "LOWER(username) LIKE '$username'"
306     if $username_type{'Exact'}
307     || $username_type{'Fuzzy'};
308
309   push @username_sql, "LOWER(username) LIKE '\%$username\%'"
310     if $username_type{'Substring'}
311     || $username_type{'All'};
312
313   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
314     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
315     my $all_username = &FS::svc_acct::all_username;
316
317     my %username;
318     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
319       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
320         $username{$_}++; 
321       }
322     }
323
324     #if ($username_type{'Sound-alike'}) {
325     #}
326
327     push @username_sql, "username = '$_'"
328       foreach (keys %username);
329
330   }
331
332   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
333
334 }
335
336 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
337
338 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
339 my @pkg_fields = split(',', $1);
340 foreach my $pkg_field ( @pkg_fields ) {
341   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
342   push @header, $header;
343
344   #not the most efficient to do it every field, but this is of niche use. so far
345   push @fields, sub { my $svc_acct = shift;
346                       my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
347                       my $value = $cust_pkg->get($pkg_field);#closures help alot
348                       $value ? time2str('%b %d %Y', $value ) : '';
349                     };
350
351   push @links, '';
352   $align .= 'c';
353   push @color, '';
354   push @style, '';
355   
356 }
357
358 push @header, emt('Pkg. Status');
359 push @fields, sub {
360   $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
361   return '' unless $cust_pkg_cache{$_[0]->svcnum};
362   $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status;
363 };
364 push @links, '';
365 $align .= 'r';
366 push @color, sub {
367   $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
368   return '' unless $cust_pkg_cache{$_[0]->svcnum};
369   my $c = FS::cust_pkg::statuscolors;
370   $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
371 };
372 push @style, 'b';
373
374 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
375 push @fields, \&FS::UI::Web::cust_fields,
376 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
377                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
378 $align .= FS::UI::Web::cust_aligns();
379 push @color, FS::UI::Web::cust_colors();
380 push @style, FS::UI::Web::cust_styles();
381
382 $search_hash{'order_by'} = $orderby;
383 $search_hash{'where'} = \@extra_sql;
384 $search_hash{'addl_from'} = $addl_from;
385
386 my $sql_query = FS::svc_acct->search(\%search_hash);
387 my $count_query = delete($sql_query->{'count_query'});
388
389 </%init>