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 = ( 'svcnum', 'svc', 'email' );
74 my @links = ( $link, $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 #maybe hide the UID if a flag isn't passed... its much less useful these days
95 push @header, 'Real Name', 'UID', 'Last Login';
96 push @fields, 'finger', 'uid', 'last_login_text';
97 push @links, $link, $link, $link;
99 push @color, '', '', '';
100 push @style, '', '', '';
102 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
103 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
106 my $timepermonth = '';
108 my $orderby = 'ORDER BY svcnum';
109 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
111 $search_hash{'unlinked'} = 1
112 if $cgi->param('magic') eq 'unlinked';
115 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
117 $sortby = "LOWER($sortby)"
118 if $sortby eq 'username';
119 push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
120 if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
121 $orderby = "ORDER BY $sortby";
124 if ( $sortby eq 'seconds' ) {
126 #push @header, 'Time remaining';
127 push @header, 'Time';
128 push @fields, sub { my $svc_acct = shift;
129 $tot_time += $svc_acct->seconds;
130 format_time($svc_acct->seconds);
137 @footer = ( '', 'Total', '', '', '',
138 sub { format_time($tot_time) }, #time
141 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
142 my $tot_paid_time = 0;
143 my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
144 push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
147 my $svc_acct = shift;
148 my $seconds = $svc_acct->seconds;
149 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
150 my $part_pkg = $cust_pkg->part_pkg;
152 #my $timepermonth = $part_pkg->option('seconds');
153 $timepermonth = $part_pkg->option('seconds');
154 $timepermonth = $timepermonth / $part_pkg->freq
155 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
157 #my $recur = $part_pkg->calc_recur($cust_pkg);
158 my $recur = $part_pkg->base_recur($cust_pkg);
160 return format_time($seconds) unless $timepermonth && $recur;
162 my $balance = $cust_pkg->cust_main->balance;
163 my $periods_unpaid = $balance / $recur;
164 my $time_unpaid = $periods_unpaid * $timepermonth;
165 $time_unpaid *= $part_pkg->freq
166 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
167 $tot_paid_time += $seconds-$time_unpaid;
168 format_time($seconds-$time_unpaid).
169 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
171 sub { timelast( shift, 30, $timepermonth ); },
172 sub { timelast( shift, 60, $timepermonth ); },
173 sub { timelast( shift, 90, $timepermonth ); },
175 push @links, '', '', '', '';
177 push @color, '', '', '', '';
178 push @style, '', '', '', '';
180 sub { format_time($tot_paid_time) }, #paid time
181 '', #XXX sub { $tot{'30'} }, #30
182 '', #XXX sub { $tot{'60'} }, #60
183 '', #XXX sub { $tot{'90'} }, #90
189 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
191 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
193 $sortby = "LOWER($sortby)"
194 if $sortby eq 'username';
195 push @extra_sql, "last_login IS NULL";
196 $orderby = "ORDER BY $sortby";
199 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
203 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
205 foreach my $field (qw( last_login last_logout )) {
207 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
209 next if $beginning == 0 && $ending == 4294967295;
211 if ($cgi->param($field."_invert")) {
213 "(svc_acct.$field IS NULL OR ".
214 "svc_acct.$field < $beginning AND ".
215 "svc_acct.$field > $ending)";
218 "svc_acct.$field IS NOT NULL",
219 "svc_acct.$field >= $beginning",
220 "svc_acct.$field <= $ending";
223 $orderby ||= "ORDER BY svc_acct.$field" .
224 ($cgi->param($field."_invert") ? ' DESC' : '');
228 $orderby ||= "ORDER BY svcnum";
230 } elsif ( $cgi->param('popnum') ) {
231 $orderby = "ORDER BY LOWER(username)";
232 } elsif ( $cgi->param('svcpart') ) {
233 $orderby = "ORDER BY uid";
234 #$orderby = "ORDER BY svcnum";
236 $orderby = "ORDER BY uid";
241 foreach ( $cgi->param('username_type') ) {
242 $username_type{$_}++;
245 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
248 push @username_sql, "username ILIKE '$username'"
249 if $username_type{'Exact'}
250 || $username_type{'Fuzzy'};
252 push @username_sql, "username ILIKE '\%$username\%'"
253 if $username_type{'Substring'}
254 || $username_type{'All'};
256 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
257 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
258 my $all_username = &FS::svc_acct::all_username;
261 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
262 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
267 #if ($username_type{'Sound-alike'}) {
270 push @username_sql, "username = '$_'"
271 foreach (keys %username);
275 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
279 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
281 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
282 my @pkg_fields = split(',', $1);
283 foreach my $pkg_field ( @pkg_fields ) {
284 ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
285 push @header, $header;
287 #not the most efficient to do it every field, but this is of niche use. so far
288 push @fields, sub { my $svc_acct = shift;
289 my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
290 my $value = $cust_pkg->get($pkg_field);#closures help alot
291 $value ? time2str('%b %d %Y', $value ) : '';
301 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
302 push @fields, \&FS::UI::Web::cust_fields,
303 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
304 FS::UI::Web::cust_header($cgi->param('cust_fields'));
305 $align .= FS::UI::Web::cust_aligns();
306 push @color, FS::UI::Web::cust_colors();
307 push @style, FS::UI::Web::cust_styles();
309 $search_hash{'order_by'} = $orderby;
310 $search_hash{'where'} = \@extra_sql;
312 my $sql_query = FS::svc_acct->search(\%search_hash);
313 my $count_query = delete($sql_query->{'count_query'});