1 <& elements/svc_Common.html,
2 'title' => emt('Account Search Results'),
3 'name' => emt('accounts'),
5 'count_query' => $count_query,
14 'html_init' => include('/elements/email-link.html',
15 'search_hash' => \%search_hash,
21 #false laziness w/ClientAPI/MyAccount.pm
24 (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
28 my( $svc_acct, $last, $permonth ) = @_;
31 SELECT SUM(support) FROM acct_rt_transaction
32 LEFT JOIN Transactions
33 ON Transactions.Id = acct_rt_transaction.transaction_id
35 AND Transactions.Created >= ?
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 )
44 my $seconds = $sth->fetchrow_arrayref->[0];
46 #my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
47 my $return = (($seconds < 0) ? '-' : '') . format_time($seconds);
49 $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
58 my $curuser = $FS::CurrentUser::CurrentUser;
60 die "access denied" unless $curuser->access_right('List services');
64 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
67 if ( $svc_acct->custnum ) {
68 [ "${p}view/cust_main.cgi?", 'custnum' ];
77 my @header = ( 'Service', 'Account' );
78 my @fields = ( 'svc', 'email' );
79 my @links = ( $link, $link );
81 my @color = ( '', '' );
82 my @style = ( '', '' );
85 my $conf = new FS::Conf;
87 if ( $conf->exists('report-showpasswords') #its a terrible idea
88 && $curuser->access_right('List service passwords') #but if you insist...
91 push @header, emt('Password');
92 push @fields, 'get_cleartext_password';
99 push @header, emt('Real Name');
100 push @fields, 'finger';
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');
116 push @header, emt('Last Login');
117 push @fields, 'last_login_text';
124 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
125 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
128 for (qw( towernum sectornum )) {
129 $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
132 my $timepermonth = '';
134 my $orderby = 'ORDER BY svcnum';
136 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
138 $search_hash{'unlinked'} = 1
139 if $cgi->param('magic') eq 'unlinked';
142 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
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";
151 if ( $sortby eq 'seconds' ) {
153 push @header, emt('Time');
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;
161 $tot_time += $svc_acct->seconds;
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);
168 return format_time($seconds)
169 unless $timepermonth && $recur;
171 format_time($seconds).
172 sprintf(' (%.2fx monthly)', $seconds / $timepermonth );
176 push @fields, sub { my $svc_acct = shift;
177 $tot_time += $svc_acct->seconds;
178 format_time($svc_acct->seconds);
187 @footer = ( 'Total', '', '', '',
188 sub { format_time($tot_time) }, #time
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');
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;
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;
207 #my $recur = $part_pkg->calc_recur($cust_pkg);
208 my $recur = $part_pkg->base_recur($cust_pkg);
210 return format_time($seconds) unless $timepermonth && $recur;
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 );
221 sub { timelast( shift, 30, $timepermonth ); },
222 sub { timelast( shift, 60, $timepermonth ); },
223 sub { timelast( shift, 90, $timepermonth ); },
225 push @links, '', '', '', '';
227 push @color, '', '', '', '';
228 push @style, '', '', '', '';
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
237 push @footer, '', '';
241 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
243 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
245 $sortby = "LOWER($sortby)"
246 if $sortby eq 'username';
247 push @extra_sql, "last_login IS NULL";
248 $orderby = "ORDER BY $sortby";
251 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
255 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
257 foreach my $field (qw( last_login last_logout )) {
259 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
261 next if $beginning == 0 && $ending == 4294967295;
263 if ($cgi->param($field."_invert")) {
265 "(svc_acct.$field IS NULL OR ".
266 "svc_acct.$field < $beginning AND ".
267 "svc_acct.$field > $ending)";
270 "svc_acct.$field IS NOT NULL",
271 "svc_acct.$field >= $beginning",
272 "svc_acct.$field <= $ending";
275 $orderby ||= "ORDER BY svc_acct.$field" .
276 ($cgi->param($field."_invert") ? ' DESC' : '');
280 $orderby ||= "ORDER BY svcnum";
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;
291 $orderby = "ORDER BY uid";
296 foreach ( $cgi->param('username_type') ) {
297 $username_type{$_}++;
300 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
301 my $username = lc($1);
303 push @username_sql, "LOWER(username) LIKE '$username'"
304 if $username_type{'Exact'}
305 || $username_type{'Fuzzy'};
307 push @username_sql, "LOWER(username) LIKE '\%$username\%'"
308 if $username_type{'Substring'}
309 || $username_type{'All'};
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;
316 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
317 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
322 #if ($username_type{'Sound-alike'}) {
325 push @username_sql, "username = '$_'"
326 foreach (keys %username);
330 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
334 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
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;
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 ) : '';
356 push @header, emt('Pkg. Status');
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;
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 };
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();
380 $search_hash{'order_by'} = $orderby;
381 $search_hash{'where'} = \@extra_sql;
382 $search_hash{'addl_from'} = $addl_from;
384 my $sql_query = FS::svc_acct->search(\%search_hash);
385 my $count_query = delete($sql_query->{'count_query'});