12d5991ea828d27ea7735f9e1b5d51f0509c3a30
[freeside.git] / httemplate / search / svc_acct.cgi
1 <& elements/search.html,
2                  'title'       => emt('Account Search Results'),
3                  'name'        => emt('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                  'footer'      => \@footer,
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 my $curuser =  $FS::CurrentUser::CurrentUser;
55
56 die "access denied" unless $curuser->access_right('List services');
57
58 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
59 my $link_cust = sub {
60   my $svc_acct = shift;
61   if ( $svc_acct->custnum ) {
62     [ "${p}view/cust_main.cgi?", 'custnum' ];
63   } else {
64     '';
65   }
66 };
67
68 my %search_hash = ();
69 my @extra_sql = ();
70
71 my @header = ( 'Service', 'Account' );
72 my @fields = ( 'svc', 'email' );
73 my @links = ( $link, $link );
74 my $align = 'll';
75 my @color = ( '', '' );
76 my @style = ( '', '' );
77 my @footer = ();
78
79 my $conf = new FS::Conf;
80
81 if ( $conf->exists('report-showpasswords') #its a terrible idea
82      && $curuser->access_right('List service passwords') #but if you insist...
83    )
84 {
85   push @header, emt('Password');
86   push @fields, 'get_cleartext_password';
87   push @links, $link;
88   $align .= 'l';
89   push @color, '';
90   push @style, '';
91 }
92
93 push @header, emt('Real Name');
94 push @fields, 'finger';
95 push @links, $link;
96 $align .= 'l';
97 push @color, '';
98 push @style, '';
99
100 #hide the UID, its much less useful these days
101 if ( $cgi->param('show_uid') ) { #XXX add a checkbox
102   push @header, emt('UID');
103   push @fields, 'uid';
104   push @links, $link;
105   $align .= 'l';
106   push @color, '';
107   push @style, '';
108 }
109
110 push @header, emt('Last Login');
111 push @fields, 'last_login_text';
112 push @links, $link;
113 $align .= 'r';
114 push @color, '';
115 push @style, '';
116
117
118 for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
119   $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
120 }
121
122 my $timepermonth = '';
123
124 my $orderby = 'ORDER BY svcnum';
125 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
126
127   $search_hash{'unlinked'} = 1
128     if $cgi->param('magic') eq 'unlinked';
129
130   my $sortby = '';
131   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
132     $sortby = $1;
133     $sortby = "LOWER($sortby)"
134       if $sortby eq 'username';
135     push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
136       if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
137     $orderby = "ORDER BY $sortby";
138   }
139
140   if ( $sortby eq 'seconds' ) {
141     my $tot_time = 0;
142     push @header, emt('Time');
143     push @fields, sub { my $svc_acct = shift;
144                         $tot_time += $svc_acct->seconds;
145                         format_time($svc_acct->seconds);
146                       };
147     push @links, '';
148     $align .= 'r';
149     push @color, '';
150     push @style, '';
151
152     @footer = ( 'Total', '', '', '',
153                 sub { format_time($tot_time) }, #time
154               );
155
156     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
157       my $tot_paid_time = 0;
158       my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
159       push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
160       push @fields,
161         sub {
162           my $svc_acct = shift;
163           my $seconds = $svc_acct->seconds;
164           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
165           my $part_pkg = $cust_pkg->part_pkg;
166
167           #my $timepermonth = $part_pkg->option('seconds');
168           $timepermonth = $part_pkg->option('seconds');
169           $timepermonth = $timepermonth / $part_pkg->freq
170             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
171
172           #my $recur = $part_pkg->calc_recur($cust_pkg);
173           my $recur = $part_pkg->base_recur($cust_pkg);
174
175           return format_time($seconds) unless $timepermonth && $recur;
176
177           my $balance = $cust_pkg->cust_main->balance;
178           my $periods_unpaid = $balance / $recur;
179           my $time_unpaid = $periods_unpaid * $timepermonth;
180           $time_unpaid *= $part_pkg->freq
181             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
182           $tot_paid_time += $seconds-$time_unpaid;
183           format_time($seconds-$time_unpaid).
184             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
185         },
186         sub { timelast( shift, 30, $timepermonth ); },
187         sub { timelast( shift, 60, $timepermonth ); },
188         sub { timelast( shift, 90, $timepermonth ); },
189       ;
190       push @links, '', '', '', '';
191       $align .= 'rrrr';
192       push @color, '', '', '', '';
193       push @style, '', '', '', '';
194       push @footer, 
195         sub { format_time($tot_paid_time) }, #paid time
196         '', #XXX sub { $tot{'30'} }, #30
197         '', #XXX sub { $tot{'60'} }, #60
198         '', #XXX sub { $tot{'90'} }, #90
199       ;
200     }
201
202     push @footer, '', '';
203
204   }
205
206 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
207
208   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
209     my $sortby = $1;
210     $sortby = "LOWER($sortby)"
211       if $sortby eq 'username';
212     push @extra_sql, "last_login IS NULL";
213     $orderby = "ORDER BY $sortby";
214   }
215
216 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
217
218   $orderby = "";
219
220   $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
221
222   foreach my $field (qw( last_login last_logout )) {
223
224     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
225
226     next if $beginning == 0 && $ending == 4294967295;
227
228     if ($cgi->param($field."_invert")) {
229       push @extra_sql,
230         "(svc_acct.$field IS NULL OR ".
231         "svc_acct.$field < $beginning AND ".
232         "svc_acct.$field > $ending)";
233     } else {
234       push @extra_sql,
235         "svc_acct.$field IS NOT NULL",
236         "svc_acct.$field >= $beginning",
237         "svc_acct.$field <= $ending";
238     }
239   
240     $orderby ||= "ORDER BY svc_acct.$field" .
241       ($cgi->param($field."_invert") ? ' DESC' : '');
242
243   }
244
245   $orderby ||= "ORDER BY svcnum";
246
247 } elsif ( $cgi->param('popnum') ) {
248   $orderby = "ORDER BY LOWER(username)";
249 } elsif ( $cgi->param('svcpart') ) {
250   $orderby = "ORDER BY uid";
251   #$orderby = "ORDER BY svcnum";
252 } else {
253   $orderby = "ORDER BY uid";
254
255   my @username_sql;
256
257   my %username_type;
258   foreach ( $cgi->param('username_type') ) {
259     $username_type{$_}++;
260   }
261
262   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
263   my $username = lc($1);
264
265   push @username_sql, "LOWER(username) LIKE '$username'"
266     if $username_type{'Exact'}
267     || $username_type{'Fuzzy'};
268
269   push @username_sql, "LOWER(username) LIKE '\%$username\%'"
270     if $username_type{'Substring'}
271     || $username_type{'All'};
272
273   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
274     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
275     my $all_username = &FS::svc_acct::all_username;
276
277     my %username;
278     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
279       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
280         $username{$_}++; 
281       }
282     }
283
284     #if ($username_type{'Sound-alike'}) {
285     #}
286
287     push @username_sql, "username = '$_'"
288       foreach (keys %username);
289
290   }
291
292   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
293
294 }
295
296 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
297
298 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
299 my @pkg_fields = split(',', $1);
300 foreach my $pkg_field ( @pkg_fields ) {
301   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
302   push @header, $header;
303
304   #not the most efficient to do it every field, but this is of niche use. so far
305   push @fields, sub { my $svc_acct = shift;
306                       my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
307                       my $value = $cust_pkg->get($pkg_field);#closures help alot
308                       $value ? time2str('%b %d %Y', $value ) : '';
309                     };
310
311   push @links, '';
312   $align .= 'c';
313   push @color, '';
314   push @style, '';
315   
316 }
317
318 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
319 push @fields, \&FS::UI::Web::cust_fields,
320 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
321                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
322 $align .= FS::UI::Web::cust_aligns();
323 push @color, FS::UI::Web::cust_colors();
324 push @style, FS::UI::Web::cust_styles();
325
326 $search_hash{'order_by'} = $orderby;
327 $search_hash{'where'} = \@extra_sql;
328
329 my $sql_query = FS::svc_acct->search(\%search_hash);
330 my $count_query = delete($sql_query->{'count_query'});
331
332 </%init>