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 ) = @_;
26 #some false laziness w/search/rt_transaction.html
28 my $transactiontime = "
29 CASE transactions.type when 'Set'
30 THEN ( to_number(newvalue, '999999')
31 - to_number(oldvalue, '999999')
39 SELECT SUM($transactiontime) FROM acct_rt_transaction
40 LEFT JOIN Transactions
41 ON Transactions.Id = acct_rt_transaction.transaction_id
43 AND Transactions.Created >= ?
46 my $sth = dbh->prepare($sql) or die dbh->errstr;
47 $sth->execute( $svc_acct->svcnum,
48 time2str('%Y-%m-%d %X', time - $last*86400 )
52 my $seconds = $sth->fetchrow_arrayref->[0];
54 my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
56 $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
66 unless $FS::CurrentUser::CurrentUser->access_right('List services');
68 my $link = [ "${p}view/svc_acct.cgi?", 'svcnum' ];
71 if ( $svc_acct->custnum ) {
72 [ "${p}view/cust_main.cgi?", 'custnum' ];
80 my @header = ( '#', 'Service', 'Account', 'UID' );
81 my @fields = ( 'svcnum', 'svc', 'email', 'uid' );
82 my @links = ( $link, $link, $link, $link );
84 my @color = ( '', '', '', '' );
85 my @style = ( '', '', '', '' );
87 if ( $cgi->param('domain') ) {
89 qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
90 unless ( $svc_domain ) {
91 #it would be nice if this looked more like the other "not found"
92 #errors, but this will do for now.
93 errorpage("Domain ". $cgi->param('domain'). " not found at all");
95 push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
99 my $timepermonth = '';
101 my $orderby = 'ORDER BY svcnum';
102 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
104 push @extra_sql, 'pkgnum IS NULL'
105 if $cgi->param('magic') eq 'unlinked';
108 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
110 $sortby = "LOWER($sortby)"
111 if $sortby eq 'username';
112 push @extra_sql, "$sortby IS NOT NULL"
113 if $sortby eq 'uid' || $sortby eq 'seconds';
114 $orderby = "ORDER BY $sortby";
117 if ( $sortby eq 'seconds' ) {
118 #push @header, 'Time remaining';
119 push @header, 'Time';
120 push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
126 my $conf = new FS::Conf;
127 if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
128 push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
131 my $svc_acct = shift;
132 my $seconds = $svc_acct->seconds;
133 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
134 my $part_pkg = $cust_pkg->part_pkg;
135 #my $timepermonth = $part_pkg->option('seconds');
136 $timepermonth = $part_pkg->option('seconds');
137 $timepermonth = $timepermonth / $part_pkg->freq
138 if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
139 return format_time($seconds) unless $timepermonth;
140 #my $recur = $part_pkg->calc_recur($cust_pkg);
141 my $recur = $part_pkg->base_recur($cust_pkg);
142 my $balance = $cust_pkg->cust_main->balance;
143 my $months_unpaid = $balance / $recur;
144 my $time_unpaid = $months_unpaid * $timepermonth;
145 format_time($seconds-$time_unpaid).
146 sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
148 sub { timelast( shift, 30, $timepermonth ); },
149 sub { timelast( shift, 60, $timepermonth ); },
150 sub { timelast( shift, 90, $timepermonth ); },
152 push @links, '', '', '', '';
154 push @color, '', '', '', '';
155 push @style, '', '', '', '';
160 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
161 push @extra_sql, "popnum = $1";
162 $orderby = "ORDER BY LOWER(username)";
163 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
164 push @extra_sql, "svcpart = $1";
165 $orderby = "ORDER BY uid";
166 #$orderby = "ORDER BY svcnum";
168 $orderby = "ORDER BY uid";
173 foreach ( $cgi->param('username_type') ) {
174 $username_type{$_}++;
177 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
180 push @username_sql, "username ILIKE '$username'"
181 if $username_type{'Exact'}
182 || $username_type{'Fuzzy'};
184 push @username_sql, "username ILIKE '\%$username\%'"
185 if $username_type{'Substring'}
186 || $username_type{'All'};
188 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
189 &FS::svc_acct::check_and_rebuild_fuzzyfiles;
190 my $all_username = &FS::svc_acct::all_username;
193 if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
194 foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
199 #if ($username_type{'Sound-alike'}) {
202 push @username_sql, "username = '$_'"
203 foreach (keys %username);
207 push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
211 push @header, FS::UI::Web::cust_header();
212 push @fields, \&FS::UI::Web::cust_fields,
213 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
214 FS::UI::Web::cust_header();
215 $align .= FS::UI::Web::cust_aligns();
216 push @color, FS::UI::Web::cust_colors();
217 push @style, FS::UI::Web::cust_styles();
219 my $addl_from = ' LEFT JOIN cust_svc USING ( svcnum ) '.
220 ' LEFT JOIN part_svc USING ( svcpart ) '.
221 ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
222 ' LEFT JOIN cust_main USING ( custnum ) ';
224 #here is the agent virtualization
225 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
226 'null_right' => 'View/link unlinked services'
231 ? ' WHERE '. join(' AND ', @extra_sql )
234 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
235 #if ( keys %svc_acct ) {
236 # $count_query .= ' WHERE '.
237 # join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
243 'table' => 'svc_acct',
244 'hashref' => {}, # \%svc_acct,
245 'select' => join(', ',
249 FS::UI::Web::cust_sql_fields(),
251 'extra_sql' => "$extra_sql $orderby",
252 'addl_from' => $addl_from,