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');
62 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
65 if ( $svc_acct->custnum ) {
66 [ "${p}view/cust_main.cgi?", 'custnum' ];
75 my @header = ( 'Service', 'Account' );
76 my @fields = ( 'svc', 'email' );
77 my @links = ( $link, $link );
79 my @color = ( '', '' );
80 my @style = ( '', '' );
83 my $conf = new FS::Conf;
85 if ( $conf->exists('report-showpasswords') #its a terrible idea
86 && $curuser->access_right('List service passwords') #but if you insist...
89 push @header, emt('Password');
90 push @fields, 'get_cleartext_password';
97 push @header, emt('Real Name');
98 push @fields, 'finger';
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');
114 push @header, emt('Last Login');
115 push @fields, 'last_login_text';
122 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
123 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
126 for (qw( towernum sectornum )) {
127 $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
130 my $timepermonth = '';
132 my $orderby = 'ORDER BY svcnum';
133 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
135 $search_hash{'unlinked'} = 1
136 if $cgi->param('magic') eq 'unlinked';
139 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
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";
148 if ( $sortby eq 'seconds' ) {
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);
160 @footer = ( 'Total', '', '', '',
161 sub { format_time($tot_time) }, #time
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');
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;
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;
180 #my $recur = $part_pkg->calc_recur($cust_pkg);
181 my $recur = $part_pkg->base_recur($cust_pkg);
183 return format_time($seconds) unless $timepermonth && $recur;
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 );
194 sub { timelast( shift, 30, $timepermonth ); },
195 sub { timelast( shift, 60, $timepermonth ); },
196 sub { timelast( shift, 90, $timepermonth ); },
198 push @links, '', '', '', '';
200 push @color, '', '', '', '';
201 push @style, '', '', '', '';
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
210 push @footer, '', '';
214 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
216 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
218 $sortby = "LOWER($sortby)"
219 if $sortby eq 'username';
220 push @extra_sql, "last_login IS NULL";
221 $orderby = "ORDER BY $sortby";
224 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
228 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
230 foreach my $field (qw( last_login last_logout )) {
232 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
234 next if $beginning == 0 && $ending == 4294967295;
236 if ($cgi->param($field."_invert")) {
238 "(svc_acct.$field IS NULL OR ".
239 "svc_acct.$field < $beginning AND ".
240 "svc_acct.$field > $ending)";
243 "svc_acct.$field IS NOT NULL",
244 "svc_acct.$field >= $beginning",
245 "svc_acct.$field <= $ending";
248 $orderby ||= "ORDER BY svc_acct.$field" .
249 ($cgi->param($field."_invert") ? ' DESC' : '');
253 $orderby ||= "ORDER BY svcnum";
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";
261 $orderby = "ORDER BY uid";
266 foreach ( $cgi->param('username_type') ) {
267 $username_type{$_}++;
270 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
271 my $username = lc($1);
273 push @username_sql, "LOWER(username) LIKE '$username'"
274 if $username_type{'Exact'}
275 || $username_type{'Fuzzy'};
277 push @username_sql, "LOWER(username) LIKE '\%$username\%'"
278 if $username_type{'Substring'}
279 || $username_type{'All'};
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;
286 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
287 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
292 #if ($username_type{'Sound-alike'}) {
295 push @username_sql, "username = '$_'"
296 foreach (keys %username);
300 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
304 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
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;
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 ) : '';
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();
334 $search_hash{'order_by'} = $orderby;
335 $search_hash{'where'} = \@extra_sql;
337 my $sql_query = FS::svc_acct->search(\%search_hash);
338 my $count_query = delete($sql_query->{'count_query'});