show regular time in months like paid time
[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 $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
63 my $link_cust = sub {
64   my $svc_acct = shift;
65   if ( $svc_acct->custnum ) {
66     [ "${p}view/cust_main.cgi?", 'custnum' ];
67   } else {
68     '';
69   }
70 };
71
72 my %search_hash = ();
73 my @extra_sql = ();
74
75 my @header = ( 'Service', 'Account' );
76 my @fields = ( 'svc', 'email' );
77 my @links = ( $link, $link );
78 my $align = 'll';
79 my @color = ( '', '' );
80 my @style = ( '', '' );
81 my @footer = ();
82
83 my $conf = new FS::Conf;
84
85 if ( $conf->exists('report-showpasswords') #its a terrible idea
86      && $curuser->access_right('List service passwords') #but if you insist...
87    )
88 {
89   push @header, emt('Password');
90   push @fields, 'get_cleartext_password';
91   push @links, $link;
92   $align .= 'l';
93   push @color, '';
94   push @style, '';
95 }
96
97 push @header, emt('Real Name');
98 push @fields, 'finger';
99 push @links, $link;
100 $align .= 'l';
101 push @color, '';
102 push @style, '';
103
104 #hide the UID, its much less useful these days
105 if ( $cgi->param('show_uid') ) { #XXX add a checkbox
106   push @header, emt('UID');
107   push @fields, 'uid';
108   push @links, $link;
109   $align .= 'l';
110   push @color, '';
111   push @style, '';
112 }
113
114 push @header, emt('Last Login');
115 push @fields, 'last_login_text';
116 push @links, $link;
117 $align .= 'r';
118 push @color, '';
119 push @style, '';
120
121
122 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
123   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
124 }
125
126 for (qw( towernum sectornum )) {
127   $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
128 }
129
130 my $timepermonth = '';
131
132 my $orderby = 'ORDER BY svcnum';
133 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
134
135   $search_hash{'unlinked'} = 1
136     if $cgi->param('magic') eq 'unlinked';
137
138   my $sortby = '';
139   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
140     $sortby = $1;
141     $sortby = "LOWER($sortby)"
142       if $sortby eq 'username';
143     push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
144       if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
145     $orderby = "ORDER BY $sortby";
146   }
147
148   if ( $sortby eq 'seconds' ) {
149     my $tot_time = 0;
150     push @header, emt('Time');
151
152     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
153       push @fields, sub { my $svc_acct = shift;
154                           my $seconds = $svc_acct->seconds;
155                           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
156                           my $part_pkg = $cust_pkg->part_pkg;
157
158                           $tot_time += $svc_acct->seconds;
159
160                           $timepermonth = $part_pkg->option('seconds');
161                           $timepermonth = $timepermonth / $part_pkg->freq
162                             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
163                           my $recur = $part_pkg->base_recur($cust_pkg);
164
165                           return format_time($seconds)
166                             unless $timepermonth && $recur;
167
168                           format_time($seconds).
169                             sprintf(' (%.2fx monthly)', $seconds / $timepermonth );
170
171                         };
172     } else {
173       push @fields, sub { my $svc_acct = shift;
174                           $tot_time += $svc_acct->seconds;
175                           format_time($svc_acct->seconds);
176                         };
177     }
178
179     push @links, '';
180     $align .= 'r';
181     push @color, '';
182     push @style, '';
183
184     @footer = ( 'Total', '', '', '',
185                 sub { format_time($tot_time) }, #time
186               );
187
188     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
189       my $tot_paid_time = 0;
190       my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
191       push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
192       push @fields,
193         sub {
194           my $svc_acct = shift;
195           my $seconds = $svc_acct->seconds;
196           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
197           my $part_pkg = $cust_pkg->part_pkg;
198
199           #my $timepermonth = $part_pkg->option('seconds');
200           $timepermonth = $part_pkg->option('seconds');
201           $timepermonth = $timepermonth / $part_pkg->freq
202             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
203
204           #my $recur = $part_pkg->calc_recur($cust_pkg);
205           my $recur = $part_pkg->base_recur($cust_pkg);
206
207           return format_time($seconds) unless $timepermonth && $recur;
208
209           my $balance = $cust_pkg->cust_main->balance;
210           my $periods_unpaid = $balance / $recur;
211           my $time_unpaid = $periods_unpaid * $timepermonth;
212           $time_unpaid *= $part_pkg->freq
213             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
214           $tot_paid_time += $seconds-$time_unpaid;
215           format_time($seconds-$time_unpaid).
216             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
217         },
218         sub { timelast( shift, 30, $timepermonth ); },
219         sub { timelast( shift, 60, $timepermonth ); },
220         sub { timelast( shift, 90, $timepermonth ); },
221       ;
222       push @links, '', '', '', '';
223       $align .= 'rrrr';
224       push @color, '', '', '', '';
225       push @style, '', '', '', '';
226       push @footer, 
227         sub { format_time($tot_paid_time) }, #paid time
228         '', #XXX sub { $tot{'30'} }, #30
229         '', #XXX sub { $tot{'60'} }, #60
230         '', #XXX sub { $tot{'90'} }, #90
231       ;
232     }
233
234     push @footer, '', '';
235
236   }
237
238 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
239
240   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
241     my $sortby = $1;
242     $sortby = "LOWER($sortby)"
243       if $sortby eq 'username';
244     push @extra_sql, "last_login IS NULL";
245     $orderby = "ORDER BY $sortby";
246   }
247
248 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
249
250   $orderby = "";
251
252   $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
253
254   foreach my $field (qw( last_login last_logout )) {
255
256     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
257
258     next if $beginning == 0 && $ending == 4294967295;
259
260     if ($cgi->param($field."_invert")) {
261       push @extra_sql,
262         "(svc_acct.$field IS NULL OR ".
263         "svc_acct.$field < $beginning AND ".
264         "svc_acct.$field > $ending)";
265     } else {
266       push @extra_sql,
267         "svc_acct.$field IS NOT NULL",
268         "svc_acct.$field >= $beginning",
269         "svc_acct.$field <= $ending";
270     }
271   
272     $orderby ||= "ORDER BY svc_acct.$field" .
273       ($cgi->param($field."_invert") ? ' DESC' : '');
274
275   }
276
277   $orderby ||= "ORDER BY svcnum";
278
279 } elsif ( $cgi->param('popnum') ) {
280   $orderby = "ORDER BY LOWER(username)";
281 } elsif ( $cgi->param('svcpart') ) {
282   $orderby = "ORDER BY uid";
283   #$orderby = "ORDER BY svcnum";
284 } else {
285   $orderby = "ORDER BY uid";
286
287   my @username_sql;
288
289   my %username_type;
290   foreach ( $cgi->param('username_type') ) {
291     $username_type{$_}++;
292   }
293
294   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
295   my $username = lc($1);
296
297   push @username_sql, "LOWER(username) LIKE '$username'"
298     if $username_type{'Exact'}
299     || $username_type{'Fuzzy'};
300
301   push @username_sql, "LOWER(username) LIKE '\%$username\%'"
302     if $username_type{'Substring'}
303     || $username_type{'All'};
304
305   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
306     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
307     my $all_username = &FS::svc_acct::all_username;
308
309     my %username;
310     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
311       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
312         $username{$_}++; 
313       }
314     }
315
316     #if ($username_type{'Sound-alike'}) {
317     #}
318
319     push @username_sql, "username = '$_'"
320       foreach (keys %username);
321
322   }
323
324   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
325
326 }
327
328 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
329
330 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
331 my @pkg_fields = split(',', $1);
332 foreach my $pkg_field ( @pkg_fields ) {
333   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
334   push @header, $header;
335
336   #not the most efficient to do it every field, but this is of niche use. so far
337   push @fields, sub { my $svc_acct = shift;
338                       my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
339                       my $value = $cust_pkg->get($pkg_field);#closures help alot
340                       $value ? time2str('%b %d %Y', $value ) : '';
341                     };
342
343   push @links, '';
344   $align .= 'c';
345   push @color, '';
346   push @style, '';
347   
348 }
349
350 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
351 push @fields, \&FS::UI::Web::cust_fields,
352 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
353                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
354 $align .= FS::UI::Web::cust_aligns();
355 push @color, FS::UI::Web::cust_colors();
356 push @style, FS::UI::Web::cust_styles();
357
358 $search_hash{'order_by'} = $orderby;
359 $search_hash{'where'} = \@extra_sql;
360
361 my $sql_query = FS::svc_acct->search(\%search_hash);
362 my $count_query = delete($sql_query->{'count_query'});
363
364 </%init>