add domain selection to advanced account report (side effect on RT#4623)
[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
44   $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;
45
46   $return;
47
48 }
49
50 </%once>
51 <%init>
52
53 die "access denied"
54   unless $FS::CurrentUser::CurrentUser->access_right('List services');
55
56 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
57 my $link_cust = sub {
58   my $svc_acct = shift;
59   if ( $svc_acct->custnum ) {
60     [ "${p}view/cust_main.cgi?", 'custnum' ];
61   } else {
62     '';
63   }
64 };
65
66 my @extra_sql = ();
67
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 );
71 my $align = 'rlllr';
72 my @color = ( '', '', '', '', '' );
73 my @style = ( '', '', '', '', '' );
74
75 if ( $cgi->param('domain') ) { 
76   my $svc_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");
82   } else {
83     push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
84   }
85 }
86 if ( $cgi->param('domsvc') =~ /^(\d+)$/ ) { 
87   push @extra_sql, "domsvc = $1";
88 }
89
90 my $timepermonth = '';
91
92 my $orderby = 'ORDER BY svcnum';
93 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
94
95   push @extra_sql, 'pkgnum IS NULL'
96     if $cgi->param('magic') eq 'unlinked';
97
98   my $sortby = '';
99   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
100     $sortby = $1;
101     $sortby = "LOWER($sortby)"
102       if $sortby eq 'username';
103     push @extra_sql, "$sortby IS NOT NULL"
104       if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
105     $orderby = "ORDER BY $sortby";
106   }
107
108   if ( $sortby eq 'seconds' ) {
109     #push @header, 'Time remaining';
110     push @header, 'Time';
111     push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
112     push @links, '';
113     $align .= 'r';
114     push @color, '';
115     push @style, '';
116
117     my $conf = new FS::Conf;
118     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
119       push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
120       push @fields,
121         sub {
122           my $svc_acct = shift;
123           my $seconds = $svc_acct->seconds;
124           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
125           my $part_pkg = $cust_pkg->part_pkg;
126
127           #my $timepermonth = $part_pkg->option('seconds');
128           $timepermonth = $part_pkg->option('seconds');
129           $timepermonth = $timepermonth / $part_pkg->freq
130             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
131
132           #my $recur = $part_pkg->calc_recur($cust_pkg);
133           my $recur = $part_pkg->base_recur($cust_pkg);
134
135           return format_time($seconds) unless $timepermonth && $recur;
136
137           my $balance = $cust_pkg->cust_main->balance;
138           my $months_unpaid = $balance / $recur;
139           my $time_unpaid = $months_unpaid * $timepermonth;
140           format_time($seconds-$time_unpaid).
141             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
142         },
143         sub { timelast( shift, 30, $timepermonth ); },
144         sub { timelast( shift, 60, $timepermonth ); },
145         sub { timelast( shift, 90, $timepermonth ); },
146       ;
147       push @links, '', '', '', '';
148       $align .= 'rrrr';
149       push @color, '', '', '', '';
150       push @style, '', '', '', '';
151     }
152
153   }
154
155 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
156
157   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
158     my $sortby = $1;
159     $sortby = "LOWER($sortby)"
160       if $sortby eq 'username';
161     push @extra_sql, "last_login IS NULL";
162     $orderby = "ORDER BY $sortby";
163   }
164
165 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
166   $orderby = "";
167
168   if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
169     push @extra_sql, "agentnum = $1";
170   }
171
172   my $pkgpart = join (' OR cust_pkg.pkgpart=',
173                       grep {$_} map { /^(\d+)$/; } ($cgi->param('pkgpart')));
174   push @extra_sql,  '(cust_pkg.pkgpart=' . $pkgpart . ')' if $pkgpart;
175                       
176   foreach my $field (qw( last_login last_logout )) {
177
178     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
179
180     next if $beginning == 0 && $ending == 4294967295;
181
182     if ($cgi->param($field."_invert")) {
183       push @extra_sql,
184         "(svc_acct.$field IS NULL OR ".
185         "svc_acct.$field < $beginning AND ".
186         "svc_acct.$field > $ending)";
187     } else {
188       push @extra_sql,
189         "svc_acct.$field IS NOT NULL",
190         "svc_acct.$field >= $beginning",
191         "svc_acct.$field <= $ending";
192     }
193   
194     $orderby ||= "ORDER BY svc_acct.$field" .
195       ($cgi->param($field."_invert") ? ' DESC' : '');
196
197   }
198
199   $orderby ||= "ORDER BY svcnum";
200
201 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
202   push @extra_sql, "popnum = $1";
203   $orderby = "ORDER BY LOWER(username)";
204 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
205   push @extra_sql, "svcpart = $1";
206   $orderby = "ORDER BY uid";
207   #$orderby = "ORDER BY svcnum";
208 } else {
209   $orderby = "ORDER BY uid";
210
211   my @username_sql;
212
213   my %username_type;
214   foreach ( $cgi->param('username_type') ) {
215     $username_type{$_}++;
216   }
217
218   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
219   my $username = $1;
220
221   push @username_sql, "username ILIKE '$username'"
222     if $username_type{'Exact'}
223     || $username_type{'Fuzzy'};
224
225   push @username_sql, "username ILIKE '\%$username\%'"
226     if $username_type{'Substring'}
227     || $username_type{'All'};
228
229   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
230     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
231     my $all_username = &FS::svc_acct::all_username;
232
233     my %username;
234     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
235       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
236         $username{$_}++; 
237       }
238     }
239
240     #if ($username_type{'Sound-alike'}) {
241     #}
242
243     push @username_sql, "username = '$_'"
244       foreach (keys %username);
245
246   }
247
248   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
249
250 }
251
252 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
253 push @fields, \&FS::UI::Web::cust_fields,
254 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
255                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
256 $align .= FS::UI::Web::cust_aligns();
257 push @color, FS::UI::Web::cust_colors();
258 push @style, FS::UI::Web::cust_styles();
259
260 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
261                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
262                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
263                 ' LEFT JOIN cust_main USING ( custnum ) ';
264
265 #here is the agent virtualization
266 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
267                    'null_right' => 'View/link unlinked services'
268                  );
269
270 my $extra_sql = 
271   scalar(@extra_sql)
272     ? ' WHERE '. join(' AND ', @extra_sql )
273     : '';
274
275 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
276 #if ( keys %svc_acct ) {
277 #  $count_query .= ' WHERE '.
278 #                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
279 #                                      keys %svc_acct
280 #                        );
281 #}
282
283 my $sql_query = {
284   'table' => 'svc_acct',
285   'hashref'   => {}, # \%svc_acct,
286   'select'    => join(', ',
287                     'svc_acct.*',
288                     'part_svc.svc',
289                     'cust_main.custnum',
290                     FS::UI::Web::cust_sql_fields(),
291                   ),
292   'extra_sql' => "$extra_sql $orderby",
293   'addl_from' => $addl_from,
294 };
295
296 </%init>