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;
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 = ( 'svcnum', 'svc', 'email' );
73 my @links = ( $link, $link, $link );
75 my @color = ( '', '', '' );
76 my @style = ( '', '', '' );
78 my $conf = new FS::Conf;
80 if ( $conf->exists('report-showpasswords') #its a terrible idea
81 && $curuser->access_right('List service passwords') #but if you insist...
84 push @header, 'Password';
85 push @fields, 'get_cleartext_password';
92 #maybe hide the UID if a flag isn't passed... its much less useful these days
93 push @header, 'Real Name', 'UID', 'Last Login';
94 push @fields, 'finger', 'uid', 'last_login_text';
95 push @links, $link, $link, $link;
97 push @color, '', '', '';
98 push @style, '', '', '';
100 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
101 $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
104 my $timepermonth = '';
106 my $orderby = 'ORDER BY svcnum';
107 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
109 $search_hash{'unlinked'} = 1
110 if $cgi->param('magic') eq 'unlinked';
113 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
115 $sortby = "LOWER($sortby)"
116 if $sortby eq 'username';
117 push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
118 if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
119 $orderby = "ORDER BY $sortby";
122 if ( $sortby eq 'seconds' ) {
123 #push @header, 'Time remaining';
124 push @header, 'Time';
125 push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
131 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
132 push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
135 my $svc_acct = shift;
136 my $seconds = $svc_acct->seconds;
137 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
138 my $part_pkg = $cust_pkg->part_pkg;
140 #my $timepermonth = $part_pkg->option('seconds');
141 $timepermonth = $part_pkg->option('seconds');
142 $timepermonth = $timepermonth / $part_pkg->freq
143 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
145 #my $recur = $part_pkg->calc_recur($cust_pkg);
146 my $recur = $part_pkg->base_recur($cust_pkg);
148 return format_time($seconds) unless $timepermonth && $recur;
150 my $balance = $cust_pkg->cust_main->balance;
151 my $periods_unpaid = $balance / $recur;
152 my $time_unpaid = $periods_unpaid * $timepermonth;
153 $time_unpaid *= $part_pkg->freq
154 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
155 format_time($seconds-$time_unpaid).
156 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
158 sub { timelast( shift, 30, $timepermonth ); },
159 sub { timelast( shift, 60, $timepermonth ); },
160 sub { timelast( shift, 90, $timepermonth ); },
162 push @links, '', '', '', '';
164 push @color, '', '', '', '';
165 push @style, '', '', '', '';
170 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
172 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
174 $sortby = "LOWER($sortby)"
175 if $sortby eq 'username';
176 push @extra_sql, "last_login IS NULL";
177 $orderby = "ORDER BY $sortby";
180 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
184 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
186 foreach my $field (qw( last_login last_logout )) {
188 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
190 next if $beginning == 0 && $ending == 4294967295;
192 if ($cgi->param($field."_invert")) {
194 "(svc_acct.$field IS NULL OR ".
195 "svc_acct.$field < $beginning AND ".
196 "svc_acct.$field > $ending)";
199 "svc_acct.$field IS NOT NULL",
200 "svc_acct.$field >= $beginning",
201 "svc_acct.$field <= $ending";
204 $orderby ||= "ORDER BY svc_acct.$field" .
205 ($cgi->param($field."_invert") ? ' DESC' : '');
209 $orderby ||= "ORDER BY svcnum";
211 } elsif ( $cgi->param('popnum') ) {
212 $orderby = "ORDER BY LOWER(username)";
213 } elsif ( $cgi->param('svcpart') ) {
214 $orderby = "ORDER BY uid";
215 #$orderby = "ORDER BY svcnum";
217 $orderby = "ORDER BY uid";
222 foreach ( $cgi->param('username_type') ) {
223 $username_type{$_}++;
226 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
229 push @username_sql, "username ILIKE '$username'"
230 if $username_type{'Exact'}
231 || $username_type{'Fuzzy'};
233 push @username_sql, "username ILIKE '\%$username\%'"
234 if $username_type{'Substring'}
235 || $username_type{'All'};
237 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
238 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
239 my $all_username = &FS::svc_acct::all_username;
242 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
243 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
248 #if ($username_type{'Sound-alike'}) {
251 push @username_sql, "username = '$_'"
252 foreach (keys %username);
256 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
260 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
262 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
263 my @pkg_fields = split(',', $1);
264 foreach my $pkg_field ( @pkg_fields ) {
265 ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
266 push @header, $header;
268 #not the most efficient to do it every field, but this is of niche use. so far
269 push @fields, sub { my $svc_acct = shift;
270 my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
271 my $value = $cust_pkg->get($pkg_field);#closures help alot
272 $value ? time2str('%b %d %Y', $value ) : '';
282 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
283 push @fields, \&FS::UI::Web::cust_fields,
284 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
285 FS::UI::Web::cust_header($cgi->param('cust_fields'));
286 $align .= FS::UI::Web::cust_aligns();
287 push @color, FS::UI::Web::cust_colors();
288 push @style, FS::UI::Web::cust_styles();
290 $search_hash{'order_by'} = $orderby;
291 $search_hash{'where'} = \@extra_sql;
293 my $sql_query = FS::svc_acct->search(\%search_hash);
294 my $count_query = delete($sql_query->{'count_query'});