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));
44 $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
54 unless $FS::CurrentUser::CurrentUser->access_right('List services');
56 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
59 if ( $svc_acct->custnum ) {
60 [ "${p}view/cust_main.cgi?", 'custnum' ];
68 my @header = ( '#', 'Service', 'Account', 'UID', 'Last Login' );
69 my @fields = ( 'svcnum', 'svc', 'email', 'uid', 'last_login_text' );
70 my @links = ( $link, $link, $link, $link, $link );
72 my @color = ( '', '', '', '', '' );
73 my @style = ( '', '', '', '', '' );
75 if ( $cgi->param('domain') ) {
77 qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
78 unless ( $svc_domain ) {
79 #it would be nice if this looked more like the other "not found"
80 #errors, but this will do for now.
81 errorpage("Domain ". $cgi->param('domain'). " not found at all");
83 push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
87 my $timepermonth = '';
89 my $orderby = 'ORDER BY svcnum';
90 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
92 push @extra_sql, 'pkgnum IS NULL'
93 if $cgi->param('magic') eq 'unlinked';
96 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
98 $sortby = "LOWER($sortby)"
99 if $sortby eq 'username';
100 push @extra_sql, "$sortby IS NOT NULL"
101 if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
102 $orderby = "ORDER BY $sortby";
105 if ( $sortby eq 'seconds' ) {
106 #push @header, 'Time remaining';
107 push @header, 'Time';
108 push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
114 my $conf = new FS::Conf;
115 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
116 push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
119 my $svc_acct = shift;
120 my $seconds = $svc_acct->seconds;
121 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
122 my $part_pkg = $cust_pkg->part_pkg;
123 #my $timepermonth = $part_pkg->option('seconds');
124 $timepermonth = $part_pkg->option('seconds');
125 $timepermonth = $timepermonth / $part_pkg->freq
126 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
127 return format_time($seconds) unless $timepermonth;
128 #my $recur = $part_pkg->calc_recur($cust_pkg);
129 my $recur = $part_pkg->base_recur($cust_pkg);
130 my $balance = $cust_pkg->cust_main->balance;
131 my $months_unpaid = $balance / $recur;
132 my $time_unpaid = $months_unpaid * $timepermonth;
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$/ ) {
161 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
162 push @extra_sql, "agentnum = $1";
165 my $pkgpart = join (' OR cust_pkg.pkgpart=',
166 grep {$_} map { /^(\d+)$/; } ($cgi->param('pkgpart')));
167 push @extra_sql, '(cust_pkg.pkgpart=' . $pkgpart . ')' if $pkgpart;
169 foreach my $field (qw( last_login last_logout )) {
171 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
173 next if $beginning == 0 && $ending == 4294967295;
175 if ($cgi->param($field."_invert")) {
177 "(svc_acct.$field IS NULL OR ".
178 "svc_acct.$field < $beginning AND ".
179 "svc_acct.$field > $ending)";
182 "svc_acct.$field IS NOT NULL",
183 "svc_acct.$field >= $beginning",
184 "svc_acct.$field <= $ending";
187 $orderby ||= "ORDER BY svc_acct.$field" .
188 ($cgi->param($field."_invert") ? ' DESC' : '');
192 $orderby ||= "ORDER BY svcnum";
194 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
195 push @extra_sql, "popnum = $1";
196 $orderby = "ORDER BY LOWER(username)";
197 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
198 push @extra_sql, "svcpart = $1";
199 $orderby = "ORDER BY uid";
200 #$orderby = "ORDER BY svcnum";
202 $orderby = "ORDER BY uid";
207 foreach ( $cgi->param('username_type') ) {
208 $username_type{$_}++;
211 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
214 push @username_sql, "username ILIKE '$username'"
215 if $username_type{'Exact'}
216 || $username_type{'Fuzzy'};
218 push @username_sql, "username ILIKE '\%$username\%'"
219 if $username_type{'Substring'}
220 || $username_type{'All'};
222 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
223 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
224 my $all_username = &FS::svc_acct::all_username;
227 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
228 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
233 #if ($username_type{'Sound-alike'}) {
236 push @username_sql, "username = '$_'"
237 foreach (keys %username);
241 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
245 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
246 push @fields, \&FS::UI::Web::cust_fields,
247 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
248 FS::UI::Web::cust_header($cgi->param('cust_fields'));
249 $align .= FS::UI::Web::cust_aligns();
250 push @color, FS::UI::Web::cust_colors();
251 push @style, FS::UI::Web::cust_styles();
253 my $addl_from = ' LEFT JOIN cust_svc USING ( svcnum ) '.
254 ' LEFT JOIN part_svc USING ( svcpart ) '.
255 ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
256 ' LEFT JOIN cust_main USING ( custnum ) ';
258 #here is the agent virtualization
259 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
260 'null_right' => 'View/link unlinked services'
265 ? ' WHERE '. join(' AND ', @extra_sql )
268 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
269 #if ( keys %svc_acct ) {
270 # $count_query .= ' WHERE '.
271 # join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
277 'table' => 'svc_acct',
278 'hashref' => {}, # \%svc_acct,
279 'select' => join(', ',
283 FS::UI::Web::cust_sql_fields(),
285 'extra_sql' => "$extra_sql $orderby",
286 'addl_from' => $addl_from,