1 <% include( 'elements/search.html',
2 'title' => 'Account Search Results',
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;
55 unless $FS::CurrentUser::CurrentUser->access_right('List services');
57 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
60 if ( $svc_acct->custnum ) {
61 [ "${p}view/cust_main.cgi?", 'custnum' ];
70 my @header = ( '#', 'Service', 'Account', 'UID', 'Last Login' );
71 my @fields = ( 'svcnum', 'svc', 'email', 'uid', 'last_login_text' );
72 my @links = ( $link, $link, $link, $link, $link );
74 my @color = ( '', '', '', '', '' );
75 my @style = ( '', '', '', '', '' );
77 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
78 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
81 my $timepermonth = '';
83 my $orderby = 'ORDER BY svcnum';
84 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
86 $search_hash{'unlinked'} = 1
87 if $cgi->param('magic') eq 'unlinked';
90 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
92 $sortby = "LOWER($sortby)"
93 if $sortby eq 'username';
94 push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
95 if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
96 $orderby = "ORDER BY $sortby";
99 if ( $sortby eq 'seconds' ) {
100 #push @header, 'Time remaining';
101 push @header, 'Time';
102 push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
108 my $conf = new FS::Conf;
109 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
110 push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
113 my $svc_acct = shift;
114 my $seconds = $svc_acct->seconds;
115 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
116 my $part_pkg = $cust_pkg->part_pkg;
118 #my $timepermonth = $part_pkg->option('seconds');
119 $timepermonth = $part_pkg->option('seconds');
120 $timepermonth = $timepermonth / $part_pkg->freq
121 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
123 #my $recur = $part_pkg->calc_recur($cust_pkg);
124 my $recur = $part_pkg->base_recur($cust_pkg);
126 return format_time($seconds) unless $timepermonth && $recur;
128 my $balance = $cust_pkg->cust_main->balance;
129 my $periods_unpaid = $balance / $recur;
130 my $time_unpaid = $periods_unpaid * $timepermonth;
131 $time_unpaid *= $part_pkg->freq
132 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
133 format_time($seconds-$time_unpaid).
134 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
136 sub { timelast( shift, 30, $timepermonth ); },
137 sub { timelast( shift, 60, $timepermonth ); },
138 sub { timelast( shift, 90, $timepermonth ); },
140 push @links, '', '', '', '';
142 push @color, '', '', '', '';
143 push @style, '', '', '', '';
148 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
150 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
152 $sortby = "LOWER($sortby)"
153 if $sortby eq 'username';
154 push @extra_sql, "last_login IS NULL";
155 $orderby = "ORDER BY $sortby";
158 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
162 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
164 foreach my $field (qw( last_login last_logout )) {
166 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
168 next if $beginning == 0 && $ending == 4294967295;
170 if ($cgi->param($field."_invert")) {
172 "(svc_acct.$field IS NULL OR ".
173 "svc_acct.$field < $beginning AND ".
174 "svc_acct.$field > $ending)";
177 "svc_acct.$field IS NOT NULL",
178 "svc_acct.$field >= $beginning",
179 "svc_acct.$field <= $ending";
182 $orderby ||= "ORDER BY svc_acct.$field" .
183 ($cgi->param($field."_invert") ? ' DESC' : '');
187 $orderby ||= "ORDER BY svcnum";
189 } elsif ( $cgi->param('popnum') ) {
190 $orderby = "ORDER BY LOWER(username)";
191 } elsif ( $cgi->param('svcpart') ) {
192 $orderby = "ORDER BY uid";
193 #$orderby = "ORDER BY svcnum";
195 $orderby = "ORDER BY uid";
200 foreach ( $cgi->param('username_type') ) {
201 $username_type{$_}++;
204 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
207 push @username_sql, "username ILIKE '$username'"
208 if $username_type{'Exact'}
209 || $username_type{'Fuzzy'};
211 push @username_sql, "username ILIKE '\%$username\%'"
212 if $username_type{'Substring'}
213 || $username_type{'All'};
215 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
216 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
217 my $all_username = &FS::svc_acct::all_username;
220 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
221 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
226 #if ($username_type{'Sound-alike'}) {
229 push @username_sql, "username = '$_'"
230 foreach (keys %username);
234 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
238 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
239 push @fields, \&FS::UI::Web::cust_fields,
240 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
241 FS::UI::Web::cust_header($cgi->param('cust_fields'));
242 $align .= FS::UI::Web::cust_aligns();
243 push @color, FS::UI::Web::cust_colors();
244 push @style, FS::UI::Web::cust_styles();
246 $search_hash{'order_by'} = $orderby;
247 $search_hash{'where'} = \@extra_sql;
249 my $sql_query = FS::svc_acct->search(\%search_hash);
250 my $count_query = delete($sql_query->{'count_query'});