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