1 <& elements/search.html,
2 'title' => emt('Account Search Results'),
3 'name' => emt('accounts'),
5 'count_query' => $count_query,
17 #false laziness w/ClientAPI/MyAccount.pm
20 (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
24 my( $svc_acct, $last, $permonth ) = @_;
27 SELECT SUM(support) FROM acct_rt_transaction
28 LEFT JOIN Transactions
29 ON Transactions.Id = acct_rt_transaction.transaction_id
31 AND Transactions.Created >= ?
34 my $sth = dbh->prepare($sql) or die dbh->errstr;
35 $sth->execute( $svc_acct->svcnum,
36 time2str('%Y-%m-%d %X', time - $last*86400 )
40 my $seconds = $sth->fetchrow_arrayref->[0];
42 #my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
43 my $return = (($seconds < 0) ? '-' : '') . format_time($seconds);
45 $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
54 my $curuser = $FS::CurrentUser::CurrentUser;
56 die "access denied" unless $curuser->access_right('List services');
58 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
61 if ( $svc_acct->custnum ) {
62 [ "${p}view/cust_main.cgi?", 'custnum' ];
71 my @header = ( 'Service', 'Account' );
72 my @fields = ( 'svc', 'email' );
73 my @links = ( $link, $link );
75 my @color = ( '', '' );
76 my @style = ( '', '' );
79 my $conf = new FS::Conf;
81 if ( $conf->exists('report-showpasswords') #its a terrible idea
82 && $curuser->access_right('List service passwords') #but if you insist...
85 push @header, emt('Password');
86 push @fields, 'get_cleartext_password';
93 push @header, emt('Real Name');
94 push @fields, 'finger';
100 #hide the UID, its much less useful these days
101 if ( $cgi->param('show_uid') ) { #XXX add a checkbox
102 push @header, emt('UID');
110 push @header, emt('Last Login');
111 push @fields, 'last_login_text';
118 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
119 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
122 my $timepermonth = '';
124 my $orderby = 'ORDER BY svcnum';
125 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
127 $search_hash{'unlinked'} = 1
128 if $cgi->param('magic') eq 'unlinked';
131 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
133 $sortby = "LOWER($sortby)"
134 if $sortby eq 'username';
135 push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
136 if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
137 $orderby = "ORDER BY $sortby";
140 if ( $sortby eq 'seconds' ) {
142 push @header, emt('Time');
143 push @fields, sub { my $svc_acct = shift;
144 $tot_time += $svc_acct->seconds;
145 format_time($svc_acct->seconds);
152 @footer = ( 'Total', '', '', '',
153 sub { format_time($tot_time) }, #time
156 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
157 my $tot_paid_time = 0;
158 my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
159 push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
162 my $svc_acct = shift;
163 my $seconds = $svc_acct->seconds;
164 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
165 my $part_pkg = $cust_pkg->part_pkg;
167 #my $timepermonth = $part_pkg->option('seconds');
168 $timepermonth = $part_pkg->option('seconds');
169 $timepermonth = $timepermonth / $part_pkg->freq
170 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
172 #my $recur = $part_pkg->calc_recur($cust_pkg);
173 my $recur = $part_pkg->base_recur($cust_pkg);
175 return format_time($seconds) unless $timepermonth && $recur;
177 my $balance = $cust_pkg->cust_main->balance;
178 my $periods_unpaid = $balance / $recur;
179 my $time_unpaid = $periods_unpaid * $timepermonth;
180 $time_unpaid *= $part_pkg->freq
181 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
182 $tot_paid_time += $seconds-$time_unpaid;
183 format_time($seconds-$time_unpaid).
184 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
186 sub { timelast( shift, 30, $timepermonth ); },
187 sub { timelast( shift, 60, $timepermonth ); },
188 sub { timelast( shift, 90, $timepermonth ); },
190 push @links, '', '', '', '';
192 push @color, '', '', '', '';
193 push @style, '', '', '', '';
195 sub { format_time($tot_paid_time) }, #paid time
196 '', #XXX sub { $tot{'30'} }, #30
197 '', #XXX sub { $tot{'60'} }, #60
198 '', #XXX sub { $tot{'90'} }, #90
202 push @footer, '', '';
206 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
208 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
210 $sortby = "LOWER($sortby)"
211 if $sortby eq 'username';
212 push @extra_sql, "last_login IS NULL";
213 $orderby = "ORDER BY $sortby";
216 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
220 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
222 foreach my $field (qw( last_login last_logout )) {
224 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
226 next if $beginning == 0 && $ending == 4294967295;
228 if ($cgi->param($field."_invert")) {
230 "(svc_acct.$field IS NULL OR ".
231 "svc_acct.$field < $beginning AND ".
232 "svc_acct.$field > $ending)";
235 "svc_acct.$field IS NOT NULL",
236 "svc_acct.$field >= $beginning",
237 "svc_acct.$field <= $ending";
240 $orderby ||= "ORDER BY svc_acct.$field" .
241 ($cgi->param($field."_invert") ? ' DESC' : '');
245 $orderby ||= "ORDER BY svcnum";
247 } elsif ( $cgi->param('popnum') ) {
248 $orderby = "ORDER BY LOWER(username)";
249 } elsif ( $cgi->param('svcpart') ) {
250 $orderby = "ORDER BY uid";
251 #$orderby = "ORDER BY svcnum";
253 $orderby = "ORDER BY uid";
258 foreach ( $cgi->param('username_type') ) {
259 $username_type{$_}++;
262 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
263 my $username = lc($1);
265 push @username_sql, "LOWER(username) LIKE '$username'"
266 if $username_type{'Exact'}
267 || $username_type{'Fuzzy'};
269 push @username_sql, "LOWER(username) LIKE '\%$username\%'"
270 if $username_type{'Substring'}
271 || $username_type{'All'};
273 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
274 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
275 my $all_username = &FS::svc_acct::all_username;
278 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
279 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
284 #if ($username_type{'Sound-alike'}) {
287 push @username_sql, "username = '$_'"
288 foreach (keys %username);
292 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
296 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
298 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
299 my @pkg_fields = split(',', $1);
300 foreach my $pkg_field ( @pkg_fields ) {
301 ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
302 push @header, $header;
304 #not the most efficient to do it every field, but this is of niche use. so far
305 push @fields, sub { my $svc_acct = shift;
306 my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
307 my $value = $cust_pkg->get($pkg_field);#closures help alot
308 $value ? time2str('%b %d %Y', $value ) : '';
318 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
319 push @fields, \&FS::UI::Web::cust_fields,
320 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
321 FS::UI::Web::cust_header($cgi->param('cust_fields'));
322 $align .= FS::UI::Web::cust_aligns();
323 push @color, FS::UI::Web::cust_colors();
324 push @style, FS::UI::Web::cust_styles();
326 $search_hash{'order_by'} = $orderby;
327 $search_hash{'where'} = \@extra_sql;
329 my $sql_query = FS::svc_acct->search(\%search_hash);
330 my $count_query = delete($sql_query->{'count_query'});