search services by tower/sector, #15950
[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 for (qw( towernum sectornum )) {
123   $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
124 }
125
126 my $timepermonth = '';
127
128 my $orderby = 'ORDER BY svcnum';
129 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
130
131   $search_hash{'unlinked'} = 1
132     if $cgi->param('magic') eq 'unlinked';
133
134   my $sortby = '';
135   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
136     $sortby = $1;
137     $sortby = "LOWER($sortby)"
138       if $sortby eq 'username';
139     push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
140       if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
141     $orderby = "ORDER BY $sortby";
142   }
143
144   if ( $sortby eq 'seconds' ) {
145     my $tot_time = 0;
146     push @header, emt('Time');
147     push @fields, sub { my $svc_acct = shift;
148                         $tot_time += $svc_acct->seconds;
149                         format_time($svc_acct->seconds);
150                       };
151     push @links, '';
152     $align .= 'r';
153     push @color, '';
154     push @style, '';
155
156     @footer = ( 'Total', '', '', '',
157                 sub { format_time($tot_time) }, #time
158               );
159
160     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
161       my $tot_paid_time = 0;
162       my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
163       push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
164       push @fields,
165         sub {
166           my $svc_acct = shift;
167           my $seconds = $svc_acct->seconds;
168           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
169           my $part_pkg = $cust_pkg->part_pkg;
170
171           #my $timepermonth = $part_pkg->option('seconds');
172           $timepermonth = $part_pkg->option('seconds');
173           $timepermonth = $timepermonth / $part_pkg->freq
174             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
175
176           #my $recur = $part_pkg->calc_recur($cust_pkg);
177           my $recur = $part_pkg->base_recur($cust_pkg);
178
179           return format_time($seconds) unless $timepermonth && $recur;
180
181           my $balance = $cust_pkg->cust_main->balance;
182           my $periods_unpaid = $balance / $recur;
183           my $time_unpaid = $periods_unpaid * $timepermonth;
184           $time_unpaid *= $part_pkg->freq
185             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
186           $tot_paid_time += $seconds-$time_unpaid;
187           format_time($seconds-$time_unpaid).
188             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
189         },
190         sub { timelast( shift, 30, $timepermonth ); },
191         sub { timelast( shift, 60, $timepermonth ); },
192         sub { timelast( shift, 90, $timepermonth ); },
193       ;
194       push @links, '', '', '', '';
195       $align .= 'rrrr';
196       push @color, '', '', '', '';
197       push @style, '', '', '', '';
198       push @footer, 
199         sub { format_time($tot_paid_time) }, #paid time
200         '', #XXX sub { $tot{'30'} }, #30
201         '', #XXX sub { $tot{'60'} }, #60
202         '', #XXX sub { $tot{'90'} }, #90
203       ;
204     }
205
206     push @footer, '', '';
207
208   }
209
210 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
211
212   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
213     my $sortby = $1;
214     $sortby = "LOWER($sortby)"
215       if $sortby eq 'username';
216     push @extra_sql, "last_login IS NULL";
217     $orderby = "ORDER BY $sortby";
218   }
219
220 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
221
222   $orderby = "";
223
224   $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];
225
226   foreach my $field (qw( last_login last_logout )) {
227
228     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
229
230     next if $beginning == 0 && $ending == 4294967295;
231
232     if ($cgi->param($field."_invert")) {
233       push @extra_sql,
234         "(svc_acct.$field IS NULL OR ".
235         "svc_acct.$field < $beginning AND ".
236         "svc_acct.$field > $ending)";
237     } else {
238       push @extra_sql,
239         "svc_acct.$field IS NOT NULL",
240         "svc_acct.$field >= $beginning",
241         "svc_acct.$field <= $ending";
242     }
243   
244     $orderby ||= "ORDER BY svc_acct.$field" .
245       ($cgi->param($field."_invert") ? ' DESC' : '');
246
247   }
248
249   $orderby ||= "ORDER BY svcnum";
250
251 } elsif ( $cgi->param('popnum') ) {
252   $orderby = "ORDER BY LOWER(username)";
253 } elsif ( $cgi->param('svcpart') ) {
254   $orderby = "ORDER BY uid";
255   #$orderby = "ORDER BY svcnum";
256 } else {
257   $orderby = "ORDER BY uid";
258
259   my @username_sql;
260
261   my %username_type;
262   foreach ( $cgi->param('username_type') ) {
263     $username_type{$_}++;
264   }
265
266   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
267   my $username = lc($1);
268
269   push @username_sql, "LOWER(username) LIKE '$username'"
270     if $username_type{'Exact'}
271     || $username_type{'Fuzzy'};
272
273   push @username_sql, "LOWER(username) LIKE '\%$username\%'"
274     if $username_type{'Substring'}
275     || $username_type{'All'};
276
277   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
278     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
279     my $all_username = &FS::svc_acct::all_username;
280
281     my %username;
282     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
283       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
284         $username{$_}++; 
285       }
286     }
287
288     #if ($username_type{'Sound-alike'}) {
289     #}
290
291     push @username_sql, "username = '$_'"
292       foreach (keys %username);
293
294   }
295
296   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
297
298 }
299
300 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
301
302 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
303 my @pkg_fields = split(',', $1);
304 foreach my $pkg_field ( @pkg_fields ) {
305   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
306   push @header, $header;
307
308   #not the most efficient to do it every field, but this is of niche use. so far
309   push @fields, sub { my $svc_acct = shift;
310                       my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
311                       my $value = $cust_pkg->get($pkg_field);#closures help alot
312                       $value ? time2str('%b %d %Y', $value ) : '';
313                     };
314
315   push @links, '';
316   $align .= 'c';
317   push @color, '';
318   push @style, '';
319   
320 }
321
322 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
323 push @fields, \&FS::UI::Web::cust_fields,
324 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
325                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
326 $align .= FS::UI::Web::cust_aligns();
327 push @color, FS::UI::Web::cust_colors();
328 push @style, FS::UI::Web::cust_styles();
329
330 $search_hash{'order_by'} = $orderby;
331 $search_hash{'where'} = \@extra_sql;
332
333 my $sql_query = FS::svc_acct->search(\%search_hash);
334 my $count_query = delete($sql_query->{'count_query'});
335
336 </%init>