multi-select for agent on RADIUS usage report, RT#73050
[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   foreach my $field (qw( last_login last_logout )) {
258
259     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
260
261     next if $beginning == 0 && $ending == 4294967295;
262
263     if ($cgi->param($field."_invert")) {
264       push @extra_sql,
265         "(svc_acct.$field IS NULL OR ".
266         "svc_acct.$field < $beginning AND ".
267         "svc_acct.$field > $ending)";
268     } else {
269       push @extra_sql,
270         "svc_acct.$field IS NOT NULL",
271         "svc_acct.$field >= $beginning",
272         "svc_acct.$field <= $ending";
273     }
274   
275     $orderby ||= "ORDER BY svc_acct.$field" .
276       ($cgi->param($field."_invert") ? ' DESC' : '');
277
278   }
279
280   $orderby ||= "ORDER BY svcnum";
281
282 } elsif ( $cgi->param('popnum') ) {
283   $orderby = "ORDER BY LOWER(username)";
284 } elsif ( $cgi->param('svcpart') ) {
285   $orderby = "ORDER BY uid";
286   #$orderby = "ORDER BY svcnum";
287   if ( defined($cgi->param('cancelled')) ) {
288     $search_hash{'cancelled'} = $cgi->param('cancelled') ? 1 : 0;
289   }
290 } else {
291   $orderby = "ORDER BY uid";
292
293   my @username_sql;
294
295   my %username_type;
296   foreach ( $cgi->param('username_type') ) {
297     $username_type{$_}++;
298   }
299
300   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
301   my $username = lc($1);
302
303   push @username_sql, "LOWER(username) LIKE '$username'"
304     if $username_type{'Exact'}
305     || $username_type{'Fuzzy'};
306
307   push @username_sql, "LOWER(username) LIKE '\%$username\%'"
308     if $username_type{'Substring'}
309     || $username_type{'All'};
310
311   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
312     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
313     my $all_username = &FS::svc_acct::all_username;
314
315     my %username;
316     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
317       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
318         $username{$_}++; 
319       }
320     }
321
322     #if ($username_type{'Sound-alike'}) {
323     #}
324
325     push @username_sql, "username = '$_'"
326       foreach (keys %username);
327
328   }
329
330   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
331
332 }
333
334 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
335
336 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
337 my @pkg_fields = split(',', $1);
338 foreach my $pkg_field ( @pkg_fields ) {
339   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
340   push @header, $header;
341
342   #not the most efficient to do it every field, but this is of niche use. so far
343   push @fields, sub { my $svc_acct = shift;
344                       my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
345                       my $value = $cust_pkg->get($pkg_field);#closures help alot
346                       $value ? time2str('%b %d %Y', $value ) : '';
347                     };
348
349   push @links, '';
350   $align .= 'c';
351   push @color, '';
352   push @style, '';
353   
354 }
355
356 push @header, emt('Pkg. Status');
357 push @fields, sub {
358   $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
359   return '' unless $cust_pkg_cache{$_[0]->svcnum};
360   $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status;
361 };
362 push @links, '';
363 $align .= 'r';
364 push @color, sub {
365   $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
366   return '' unless $cust_pkg_cache{$_[0]->svcnum};
367   my $c = FS::cust_pkg::statuscolors;
368   $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
369 };
370 push @style, 'b';
371
372 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
373 push @fields, \&FS::UI::Web::cust_fields,
374 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
375                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
376 $align .= FS::UI::Web::cust_aligns();
377 push @color, FS::UI::Web::cust_colors();
378 push @style, FS::UI::Web::cust_styles();
379
380 $search_hash{'order_by'} = $orderby;
381 $search_hash{'where'} = \@extra_sql;
382 $search_hash{'addl_from'} = $addl_from;
383
384 my $sql_query = FS::svc_acct->search(\%search_hash);
385 my $count_query = delete($sql_query->{'count_query'});
386
387 </%init>