1 <% include( 'elements/search.html',
2 'title' => 'Account Search Results',
5 'count_query' => $count_query,
18 #false laziness w/ClientAPI/MyAccount.pm
21 (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
25 my( $svc_acct, $last, $permonth ) = @_;
28 SELECT SUM(support) FROM acct_rt_transaction
29 LEFT JOIN Transactions
30 ON Transactions.Id = acct_rt_transaction.transaction_id
32 AND Transactions.Created >= ?
35 my $sth = dbh->prepare($sql) or die dbh->errstr;
36 $sth->execute( $svc_acct->svcnum,
37 time2str('%Y-%m-%d %X', time - $last*86400 )
41 my $seconds = $sth->fetchrow_arrayref->[0];
43 #my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
44 my $return = (($seconds < 0) ? '-' : '') . format_time($seconds);
46 $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
55 my $curuser = $FS::CurrentUser::CurrentUser;
57 die "access denied" unless $curuser->access_right('List services');
59 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
62 if ( $svc_acct->custnum ) {
63 [ "${p}view/cust_main.cgi?", 'custnum' ];
72 my @header = ( 'Service', 'Account' );
73 my @fields = ( 'svc', 'email' );
74 my @links = ( $link, $link );
76 my @color = ( '', '' );
77 my @style = ( '', '' );
80 my $conf = new FS::Conf;
82 if ( $conf->exists('report-showpasswords') #its a terrible idea
83 && $curuser->access_right('List service passwords') #but if you insist...
86 push @header, 'Password';
87 push @fields, 'get_cleartext_password';
94 push @header, 'Real Name';
95 push @fields, 'finger';
101 #hide the UID, its much less useful these days
102 if ( $cgi->param('show_uid') ) { #XXX add a checkbox
111 push @header, 'Last Login';
112 push @fields, 'last_login_text';
119 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
120 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
123 my $timepermonth = '';
125 my $orderby = 'ORDER BY svcnum';
126 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
128 $search_hash{'unlinked'} = 1
129 if $cgi->param('magic') eq 'unlinked';
132 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
134 $sortby = "LOWER($sortby)"
135 if $sortby eq 'username';
136 push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
137 if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
138 $orderby = "ORDER BY $sortby";
141 if ( $sortby eq 'seconds' ) {
143 #push @header, 'Time remaining';
144 push @header, 'Time';
145 push @fields, sub { my $svc_acct = shift;
146 $tot_time += $svc_acct->seconds;
147 format_time($svc_acct->seconds);
154 @footer = ( 'Total', '', '', '',
155 sub { format_time($tot_time) }, #time
158 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
159 my $tot_paid_time = 0;
160 my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
161 push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
164 my $svc_acct = shift;
165 my $seconds = $svc_acct->seconds;
166 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
167 my $part_pkg = $cust_pkg->part_pkg;
169 #my $timepermonth = $part_pkg->option('seconds');
170 $timepermonth = $part_pkg->option('seconds');
171 $timepermonth = $timepermonth / $part_pkg->freq
172 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
174 #my $recur = $part_pkg->calc_recur($cust_pkg);
175 my $recur = $part_pkg->base_recur($cust_pkg);
177 return format_time($seconds) unless $timepermonth && $recur;
179 my $balance = $cust_pkg->cust_main->balance;
180 my $periods_unpaid = $balance / $recur;
181 my $time_unpaid = $periods_unpaid * $timepermonth;
182 $time_unpaid *= $part_pkg->freq
183 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
184 $tot_paid_time += $seconds-$time_unpaid;
185 format_time($seconds-$time_unpaid).
186 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
188 sub { timelast( shift, 30, $timepermonth ); },
189 sub { timelast( shift, 60, $timepermonth ); },
190 sub { timelast( shift, 90, $timepermonth ); },
192 push @links, '', '', '', '';
194 push @color, '', '', '', '';
195 push @style, '', '', '', '';
197 sub { format_time($tot_paid_time) }, #paid time
198 '', #XXX sub { $tot{'30'} }, #30
199 '', #XXX sub { $tot{'60'} }, #60
200 '', #XXX sub { $tot{'90'} }, #90
204 push @footer, '', '';
208 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
210 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
212 $sortby = "LOWER($sortby)"
213 if $sortby eq 'username';
214 push @extra_sql, "last_login IS NULL";
215 $orderby = "ORDER BY $sortby";
218 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
222 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
224 foreach my $field (qw( last_login last_logout )) {
226 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
228 next if $beginning == 0 && $ending == 4294967295;
230 if ($cgi->param($field."_invert")) {
232 "(svc_acct.$field IS NULL OR ".
233 "svc_acct.$field < $beginning AND ".
234 "svc_acct.$field > $ending)";
237 "svc_acct.$field IS NOT NULL",
238 "svc_acct.$field >= $beginning",
239 "svc_acct.$field <= $ending";
242 $orderby ||= "ORDER BY svc_acct.$field" .
243 ($cgi->param($field."_invert") ? ' DESC' : '');
247 $orderby ||= "ORDER BY svcnum";
249 } elsif ( $cgi->param('popnum') ) {
250 $orderby = "ORDER BY LOWER(username)";
251 } elsif ( $cgi->param('svcpart') ) {
252 $orderby = "ORDER BY uid";
253 #$orderby = "ORDER BY svcnum";
255 $orderby = "ORDER BY uid";
260 foreach ( $cgi->param('username_type') ) {
261 $username_type{$_}++;
264 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
265 my $username = lc($1);
267 push @username_sql, "LOWER(username) LIKE '$username'"
268 if $username_type{'Exact'}
269 || $username_type{'Fuzzy'};
271 push @username_sql, "LOWER(username) LIKE '\%$username\%'"
272 if $username_type{'Substring'}
273 || $username_type{'All'};
275 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
276 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
277 my $all_username = &FS::svc_acct::all_username;
280 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
281 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
286 #if ($username_type{'Sound-alike'}) {
289 push @username_sql, "username = '$_'"
290 foreach (keys %username);
294 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
298 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
300 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
301 my @pkg_fields = split(',', $1);
302 foreach my $pkg_field ( @pkg_fields ) {
303 ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
304 push @header, $header;
306 #not the most efficient to do it every field, but this is of niche use. so far
307 push @fields, sub { my $svc_acct = shift;
308 my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
309 my $value = $cust_pkg->get($pkg_field);#closures help alot
310 $value ? time2str('%b %d %Y', $value ) : '';
320 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
321 push @fields, \&FS::UI::Web::cust_fields,
322 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
323 FS::UI::Web::cust_header($cgi->param('cust_fields'));
324 $align .= FS::UI::Web::cust_aligns();
325 push @color, FS::UI::Web::cust_colors();
326 push @style, FS::UI::Web::cust_styles();
328 $search_hash{'order_by'} = $orderby;
329 $search_hash{'where'} = \@extra_sql;
331 my $sql_query = FS::svc_acct->search(\%search_hash);
332 my $count_query = delete($sql_query->{'count_query'});