e2abf5625fb348c37a8ba0db27771f51bdf9bd58
[freeside.git] / httemplate / search / svc_acct.cgi
1 <% include( 'elements/search.html',
2                  'title'       => 'Account Search Results',
3                  'name'        => 'accounts',
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'redirect'    => $link,
7                  'header'      => \@header,
8                  'fields'      => \@fields,
9                  'links'       => \@links,
10                  'align'       => $align,
11                  'color'       => \@color,
12                  'style'       => \@style,
13              )
14 %>
15 <%once>
16
17 #false laziness w/ClientAPI/MyAccount.pm
18 sub format_time { 
19   my $support = shift;
20   (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
21 }
22
23 sub timelast {
24   my( $svc_acct, $last, $permonth ) = @_;
25
26   my $sql = "
27     SELECT SUM(support) FROM acct_rt_transaction
28       LEFT JOIN Transactions
29         ON Transactions.Id = acct_rt_transaction.transaction_id
30     WHERE svcnum = ? 
31       AND Transactions.Created >= ?
32   ";
33
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 ) 
37                )
38     or die $sth->errstr;
39
40   my $seconds = $sth->fetchrow_arrayref->[0];
41
42   #my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
43   my $return = (($seconds < 0) ? '-' : '') . format_time($seconds);
44
45   $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
46
47   $return;
48
49 }
50
51 </%once>
52 <%init>
53
54 die "access denied"
55   unless $FS::CurrentUser::CurrentUser->access_right('List services');
56
57 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
58 my $link_cust = sub {
59   my $svc_acct = shift;
60   if ( $svc_acct->custnum ) {
61     [ "${p}view/cust_main.cgi?", 'custnum' ];
62   } else {
63     '';
64   }
65 };
66
67 my %search_hash = ();
68 my @extra_sql = ();
69
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 );
73 my $align = 'rlllr';
74 my @color = ( '', '', '', '', '' );
75 my @style = ( '', '', '', '', '' );
76
77 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
78   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
79 }
80
81 my $timepermonth = '';
82
83 my $orderby = 'ORDER BY svcnum';
84 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
85
86   $search_hash{'unlinked'} = 1
87     if $cgi->param('magic') eq 'unlinked';
88
89   my $sortby = '';
90   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
91     $sortby = $1;
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";
97   }
98
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) };
103     push @links, '';
104     $align .= 'r';
105     push @color, '';
106     push @style, '';
107
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';
111       push @fields,
112         sub {
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;
117
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;
122
123           #my $recur = $part_pkg->calc_recur($cust_pkg);
124           my $recur = $part_pkg->base_recur($cust_pkg);
125
126           return format_time($seconds) unless $timepermonth && $recur;
127
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 );
135         },
136         sub { timelast( shift, 30, $timepermonth ); },
137         sub { timelast( shift, 60, $timepermonth ); },
138         sub { timelast( shift, 90, $timepermonth ); },
139       ;
140       push @links, '', '', '', '';
141       $align .= 'rrrr';
142       push @color, '', '', '', '';
143       push @style, '', '', '', '';
144     }
145
146   }
147
148 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
149
150   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
151     my $sortby = $1;
152     $sortby = "LOWER($sortby)"
153       if $sortby eq 'username';
154     push @extra_sql, "last_login IS NULL";
155     $orderby = "ORDER BY $sortby";
156   }
157
158 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
159
160   $orderby = "";
161
162   $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
163
164   foreach my $field (qw( last_login last_logout )) {
165
166     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
167
168     next if $beginning == 0 && $ending == 4294967295;
169
170     if ($cgi->param($field."_invert")) {
171       push @extra_sql,
172         "(svc_acct.$field IS NULL OR ".
173         "svc_acct.$field < $beginning AND ".
174         "svc_acct.$field > $ending)";
175     } else {
176       push @extra_sql,
177         "svc_acct.$field IS NOT NULL",
178         "svc_acct.$field >= $beginning",
179         "svc_acct.$field <= $ending";
180     }
181   
182     $orderby ||= "ORDER BY svc_acct.$field" .
183       ($cgi->param($field."_invert") ? ' DESC' : '');
184
185   }
186
187   $orderby ||= "ORDER BY svcnum";
188
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";
194 } else {
195   $orderby = "ORDER BY uid";
196
197   my @username_sql;
198
199   my %username_type;
200   foreach ( $cgi->param('username_type') ) {
201     $username_type{$_}++;
202   }
203
204   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
205   my $username = $1;
206
207   push @username_sql, "username ILIKE '$username'"
208     if $username_type{'Exact'}
209     || $username_type{'Fuzzy'};
210
211   push @username_sql, "username ILIKE '\%$username\%'"
212     if $username_type{'Substring'}
213     || $username_type{'All'};
214
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;
218
219     my %username;
220     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
221       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
222         $username{$_}++; 
223       }
224     }
225
226     #if ($username_type{'Sound-alike'}) {
227     #}
228
229     push @username_sql, "username = '$_'"
230       foreach (keys %username);
231
232   }
233
234   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
235
236 }
237
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();
245
246 $search_hash{'order_by'} = $orderby;
247 $search_hash{'where'} = \@extra_sql;
248
249 my $sql_query = FS::svc_acct->search(\%search_hash);
250 my $count_query = delete($sql_query->{'count_query'});
251
252 </%init>