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