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