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');
152 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
153 push @fields, sub { my $svc_acct = shift;
154 my $seconds = $svc_acct->seconds;
155 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
156 my $part_pkg = $cust_pkg->part_pkg;
158 $tot_time += $svc_acct->seconds;
160 $timepermonth = $part_pkg->option('seconds');
161 $timepermonth = $timepermonth / $part_pkg->freq
162 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
163 my $recur = $part_pkg->base_recur($cust_pkg);
165 return format_time($seconds)
166 unless $timepermonth && $recur;
168 format_time($seconds).
169 sprintf(' (%.2fx monthly)', $seconds / $timepermonth );
173 push @fields, sub { my $svc_acct = shift;
174 $tot_time += $svc_acct->seconds;
175 format_time($svc_acct->seconds);
184 @footer = ( 'Total', '', '', '',
185 sub { format_time($tot_time) }, #time
188 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
189 my $tot_paid_time = 0;
190 my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
191 push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
194 my $svc_acct = shift;
195 my $seconds = $svc_acct->seconds;
196 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
197 my $part_pkg = $cust_pkg->part_pkg;
199 #my $timepermonth = $part_pkg->option('seconds');
200 $timepermonth = $part_pkg->option('seconds');
201 $timepermonth = $timepermonth / $part_pkg->freq
202 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
204 #my $recur = $part_pkg->calc_recur($cust_pkg);
205 my $recur = $part_pkg->base_recur($cust_pkg);
207 return format_time($seconds) unless $timepermonth && $recur;
209 my $balance = $cust_pkg->cust_main->balance;
210 my $periods_unpaid = $balance / $recur;
211 my $time_unpaid = $periods_unpaid * $timepermonth;
212 $time_unpaid *= $part_pkg->freq
213 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
214 $tot_paid_time += $seconds-$time_unpaid;
215 format_time($seconds-$time_unpaid).
216 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
218 sub { timelast( shift, 30, $timepermonth ); },
219 sub { timelast( shift, 60, $timepermonth ); },
220 sub { timelast( shift, 90, $timepermonth ); },
222 push @links, '', '', '', '';
224 push @color, '', '', '', '';
225 push @style, '', '', '', '';
227 sub { format_time($tot_paid_time) }, #paid time
228 '', #XXX sub { $tot{'30'} }, #30
229 '', #XXX sub { $tot{'60'} }, #60
230 '', #XXX sub { $tot{'90'} }, #90
234 push @footer, '', '';
238 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
240 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
242 $sortby = "LOWER($sortby)"
243 if $sortby eq 'username';
244 push @extra_sql, "last_login IS NULL";
245 $orderby = "ORDER BY $sortby";
248 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
252 $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
254 foreach my $field (qw( last_login last_logout )) {
256 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
258 next if $beginning == 0 && $ending == 4294967295;
260 if ($cgi->param($field."_invert")) {
262 "(svc_acct.$field IS NULL OR ".
263 "svc_acct.$field < $beginning AND ".
264 "svc_acct.$field > $ending)";
267 "svc_acct.$field IS NOT NULL",
268 "svc_acct.$field >= $beginning",
269 "svc_acct.$field <= $ending";
272 $orderby ||= "ORDER BY svc_acct.$field" .
273 ($cgi->param($field."_invert") ? ' DESC' : '');
277 $orderby ||= "ORDER BY svcnum";
279 } elsif ( $cgi->param('popnum') ) {
280 $orderby = "ORDER BY LOWER(username)";
281 } elsif ( $cgi->param('svcpart') ) {
282 $orderby = "ORDER BY uid";
283 #$orderby = "ORDER BY svcnum";
285 $orderby = "ORDER BY uid";
290 foreach ( $cgi->param('username_type') ) {
291 $username_type{$_}++;
294 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
295 my $username = lc($1);
297 push @username_sql, "LOWER(username) LIKE '$username'"
298 if $username_type{'Exact'}
299 || $username_type{'Fuzzy'};
301 push @username_sql, "LOWER(username) LIKE '\%$username\%'"
302 if $username_type{'Substring'}
303 || $username_type{'All'};
305 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
306 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
307 my $all_username = &FS::svc_acct::all_username;
310 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
311 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
316 #if ($username_type{'Sound-alike'}) {
319 push @username_sql, "username = '$_'"
320 foreach (keys %username);
324 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
328 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
330 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
331 my @pkg_fields = split(',', $1);
332 foreach my $pkg_field ( @pkg_fields ) {
333 ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
334 push @header, $header;
336 #not the most efficient to do it every field, but this is of niche use. so far
337 push @fields, sub { my $svc_acct = shift;
338 my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
339 my $value = $cust_pkg->get($pkg_field);#closures help alot
340 $value ? time2str('%b %d %Y', $value ) : '';
350 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
351 push @fields, \&FS::UI::Web::cust_fields,
352 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
353 FS::UI::Web::cust_header($cgi->param('cust_fields'));
354 $align .= FS::UI::Web::cust_aligns();
355 push @color, FS::UI::Web::cust_colors();
356 push @style, FS::UI::Web::cust_styles();
358 $search_hash{'order_by'} = $orderby;
359 $search_hash{'where'} = \@extra_sql;
361 my $sql_query = FS::svc_acct->search(\%search_hash);
362 my $count_query = delete($sql_query->{'count_query'});