Merge branch 'patch-18' of https://github.com/gjones2/Freeside
[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     push @fields, sub { my $svc_acct = shift;
152                         $tot_time += $svc_acct->seconds;
153                         format_time($svc_acct->seconds);
154                       };
155     push @links, '';
156     $align .= 'r';
157     push @color, '';
158     push @style, '';
159
160     @footer = ( 'Total', '', '', '',
161                 sub { format_time($tot_time) }, #time
162               );
163
164     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
165       my $tot_paid_time = 0;
166       my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
167       push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
168       push @fields,
169         sub {
170           my $svc_acct = shift;
171           my $seconds = $svc_acct->seconds;
172           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
173           my $part_pkg = $cust_pkg->part_pkg;
174
175           #my $timepermonth = $part_pkg->option('seconds');
176           $timepermonth = $part_pkg->option('seconds');
177           $timepermonth = $timepermonth / $part_pkg->freq
178             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
179
180           #my $recur = $part_pkg->calc_recur($cust_pkg);
181           my $recur = $part_pkg->base_recur($cust_pkg);
182
183           return format_time($seconds) unless $timepermonth && $recur;
184
185           my $balance = $cust_pkg->cust_main->balance;
186           my $periods_unpaid = $balance / $recur;
187           my $time_unpaid = $periods_unpaid * $timepermonth;
188           $time_unpaid *= $part_pkg->freq
189             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
190           $tot_paid_time += $seconds-$time_unpaid;
191           format_time($seconds-$time_unpaid).
192             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
193         },
194         sub { timelast( shift, 30, $timepermonth ); },
195         sub { timelast( shift, 60, $timepermonth ); },
196         sub { timelast( shift, 90, $timepermonth ); },
197       ;
198       push @links, '', '', '', '';
199       $align .= 'rrrr';
200       push @color, '', '', '', '';
201       push @style, '', '', '', '';
202       push @footer, 
203         sub { format_time($tot_paid_time) }, #paid time
204         '', #XXX sub { $tot{'30'} }, #30
205         '', #XXX sub { $tot{'60'} }, #60
206         '', #XXX sub { $tot{'90'} }, #90
207       ;
208     }
209
210     push @footer, '', '';
211
212   }
213
214 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
215
216   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
217     my $sortby = $1;
218     $sortby = "LOWER($sortby)"
219       if $sortby eq 'username';
220     push @extra_sql, "last_login IS NULL";
221     $orderby = "ORDER BY $sortby";
222   }
223
224 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
225
226   $orderby = "";
227
228   $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
229
230   foreach my $field (qw( last_login last_logout )) {
231
232     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
233
234     next if $beginning == 0 && $ending == 4294967295;
235
236     if ($cgi->param($field."_invert")) {
237       push @extra_sql,
238         "(svc_acct.$field IS NULL OR ".
239         "svc_acct.$field < $beginning AND ".
240         "svc_acct.$field > $ending)";
241     } else {
242       push @extra_sql,
243         "svc_acct.$field IS NOT NULL",
244         "svc_acct.$field >= $beginning",
245         "svc_acct.$field <= $ending";
246     }
247   
248     $orderby ||= "ORDER BY svc_acct.$field" .
249       ($cgi->param($field."_invert") ? ' DESC' : '');
250
251   }
252
253   $orderby ||= "ORDER BY svcnum";
254
255 } elsif ( $cgi->param('popnum') ) {
256   $orderby = "ORDER BY LOWER(username)";
257 } elsif ( $cgi->param('svcpart') ) {
258   $orderby = "ORDER BY uid";
259   #$orderby = "ORDER BY svcnum";
260 } else {
261   $orderby = "ORDER BY uid";
262
263   my @username_sql;
264
265   my %username_type;
266   foreach ( $cgi->param('username_type') ) {
267     $username_type{$_}++;
268   }
269
270   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
271   my $username = lc($1);
272
273   push @username_sql, "LOWER(username) LIKE '$username'"
274     if $username_type{'Exact'}
275     || $username_type{'Fuzzy'};
276
277   push @username_sql, "LOWER(username) LIKE '\%$username\%'"
278     if $username_type{'Substring'}
279     || $username_type{'All'};
280
281   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
282     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
283     my $all_username = &FS::svc_acct::all_username;
284
285     my %username;
286     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
287       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
288         $username{$_}++; 
289       }
290     }
291
292     #if ($username_type{'Sound-alike'}) {
293     #}
294
295     push @username_sql, "username = '$_'"
296       foreach (keys %username);
297
298   }
299
300   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
301
302 }
303
304 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
305
306 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
307 my @pkg_fields = split(',', $1);
308 foreach my $pkg_field ( @pkg_fields ) {
309   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
310   push @header, $header;
311
312   #not the most efficient to do it every field, but this is of niche use. so far
313   push @fields, sub { my $svc_acct = shift;
314                       my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
315                       my $value = $cust_pkg->get($pkg_field);#closures help alot
316                       $value ? time2str('%b %d %Y', $value ) : '';
317                     };
318
319   push @links, '';
320   $align .= 'c';
321   push @color, '';
322   push @style, '';
323   
324 }
325
326 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
327 push @fields, \&FS::UI::Web::cust_fields,
328 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
329                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
330 $align .= FS::UI::Web::cust_aligns();
331 push @color, FS::UI::Web::cust_colors();
332 push @style, FS::UI::Web::cust_styles();
333
334 $search_hash{'order_by'} = $orderby;
335 $search_hash{'where'} = \@extra_sql;
336
337 my $sql_query = FS::svc_acct->search(\%search_hash);
338 my $count_query = delete($sql_query->{'count_query'});
339
340 </%init>