7ddb3f000a4114abded825d1a0e8cf2c1a625ac9
[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 die "access denied"
55   unless $FS::CurrentUser::CurrentUser->access_right('List services');
56
57 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
58 my $link_cust = sub {
59   my $svc_acct = shift;
60   if ( $svc_acct->custnum ) {
61     [ "${p}view/cust_main.cgi?", 'custnum' ];
62   } else {
63     '';
64   }
65 };
66
67 my @extra_sql = ();
68
69 my @header = ( '#', 'Service', 'Account', 'UID', 'Last Login' );
70 my @fields = ( 'svcnum', 'svc', 'email', 'uid', 'last_login_text' );
71 my @links = ( $link, $link, $link, $link, $link );
72 my $align = 'rlllr';
73 my @color = ( '', '', '', '', '' );
74 my @style = ( '', '', '', '', '' );
75
76 if ( $cgi->param('domain') ) { 
77   my $svc_domain =
78     qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
79   unless ( $svc_domain ) {
80     #it would be nice if this looked more like the other "not found"
81     #errors, but this will do for now.
82     errorpage("Domain ". $cgi->param('domain'). " not found at all");
83   } else {
84     push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
85   }
86 }
87 if ( $cgi->param('domsvc') =~ /^(\d+)$/ ) { 
88   push @extra_sql, "domsvc = $1";
89 }
90
91 my $timepermonth = '';
92
93 my $orderby = 'ORDER BY svcnum';
94 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
95
96   push @extra_sql, 'pkgnum IS NULL'
97     if $cgi->param('magic') eq 'unlinked';
98
99   my $sortby = '';
100   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
101     $sortby = $1;
102     $sortby = "LOWER($sortby)"
103       if $sortby eq 'username';
104     push @extra_sql, "$sortby IS NOT NULL"
105       if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
106     $orderby = "ORDER BY $sortby";
107   }
108
109   if ( $sortby eq 'seconds' ) {
110     #push @header, 'Time remaining';
111     push @header, 'Time';
112     push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
113     push @links, '';
114     $align .= 'r';
115     push @color, '';
116     push @style, '';
117
118     my $conf = new FS::Conf;
119     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
120       push @header, 'Paid time', 'Last 30', 'Last 60', 'Last 90';
121       push @fields,
122         sub {
123           my $svc_acct = shift;
124           my $seconds = $svc_acct->seconds;
125           my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
126           my $part_pkg = $cust_pkg->part_pkg;
127
128           #my $timepermonth = $part_pkg->option('seconds');
129           $timepermonth = $part_pkg->option('seconds');
130           $timepermonth = $timepermonth / $part_pkg->freq
131             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
132
133           #my $recur = $part_pkg->calc_recur($cust_pkg);
134           my $recur = $part_pkg->base_recur($cust_pkg);
135
136           return format_time($seconds) unless $timepermonth && $recur;
137
138           my $balance = $cust_pkg->cust_main->balance;
139           my $periods_unpaid = $balance / $recur;
140           my $time_unpaid = $periods_unpaid * $timepermonth;
141           $time_unpaid *= $part_pkg->freq
142             if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
143           format_time($seconds-$time_unpaid).
144             sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
145         },
146         sub { timelast( shift, 30, $timepermonth ); },
147         sub { timelast( shift, 60, $timepermonth ); },
148         sub { timelast( shift, 90, $timepermonth ); },
149       ;
150       push @links, '', '', '', '';
151       $align .= 'rrrr';
152       push @color, '', '', '', '';
153       push @style, '', '', '', '';
154     }
155
156   }
157
158 } elsif ( $cgi->param('magic') =~ /^nologin$/ ) {
159
160   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
161     my $sortby = $1;
162     $sortby = "LOWER($sortby)"
163       if $sortby eq 'username';
164     push @extra_sql, "last_login IS NULL";
165     $orderby = "ORDER BY $sortby";
166   }
167
168 } elsif ( $cgi->param('magic') =~ /^advanced$/ ) {
169   $orderby = "";
170
171   if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
172     push @extra_sql, "agentnum = $1";
173   }
174
175   my $pkgpart = join (' OR cust_pkg.pkgpart=',
176                       grep {$_} map { /^(\d+)$/; } ($cgi->param('pkgpart')));
177   push @extra_sql,  '(cust_pkg.pkgpart=' . $pkgpart . ')' if $pkgpart;
178                       
179   foreach my $field (qw( last_login last_logout )) {
180
181     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);
182
183     next if $beginning == 0 && $ending == 4294967295;
184
185     if ($cgi->param($field."_invert")) {
186       push @extra_sql,
187         "(svc_acct.$field IS NULL OR ".
188         "svc_acct.$field < $beginning AND ".
189         "svc_acct.$field > $ending)";
190     } else {
191       push @extra_sql,
192         "svc_acct.$field IS NOT NULL",
193         "svc_acct.$field >= $beginning",
194         "svc_acct.$field <= $ending";
195     }
196   
197     $orderby ||= "ORDER BY svc_acct.$field" .
198       ($cgi->param($field."_invert") ? ' DESC' : '');
199
200   }
201
202   $orderby ||= "ORDER BY svcnum";
203
204 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
205   push @extra_sql, "popnum = $1";
206   $orderby = "ORDER BY LOWER(username)";
207 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
208   push @extra_sql, "svcpart = $1";
209   $orderby = "ORDER BY uid";
210   #$orderby = "ORDER BY svcnum";
211 } else {
212   $orderby = "ORDER BY uid";
213
214   my @username_sql;
215
216   my %username_type;
217   foreach ( $cgi->param('username_type') ) {
218     $username_type{$_}++;
219   }
220
221   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
222   my $username = $1;
223
224   push @username_sql, "username ILIKE '$username'"
225     if $username_type{'Exact'}
226     || $username_type{'Fuzzy'};
227
228   push @username_sql, "username ILIKE '\%$username\%'"
229     if $username_type{'Substring'}
230     || $username_type{'All'};
231
232   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
233     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
234     my $all_username = &FS::svc_acct::all_username;
235
236     my %username;
237     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
238       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
239         $username{$_}++; 
240       }
241     }
242
243     #if ($username_type{'Sound-alike'}) {
244     #}
245
246     push @username_sql, "username = '$_'"
247       foreach (keys %username);
248
249   }
250
251   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
252
253 }
254
255 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
256 push @fields, \&FS::UI::Web::cust_fields,
257 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
258                  FS::UI::Web::cust_header($cgi->param('cust_fields'));
259 $align .= FS::UI::Web::cust_aligns();
260 push @color, FS::UI::Web::cust_colors();
261 push @style, FS::UI::Web::cust_styles();
262
263 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
264                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
265                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
266                 ' LEFT JOIN cust_main USING ( custnum ) ';
267
268 #here is the agent virtualization
269 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
270                    'null_right' => 'View/link unlinked services'
271                  );
272
273 my $extra_sql = 
274   scalar(@extra_sql)
275     ? ' WHERE '. join(' AND ', @extra_sql )
276     : '';
277
278 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
279 #if ( keys %svc_acct ) {
280 #  $count_query .= ' WHERE '.
281 #                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
282 #                                      keys %svc_acct
283 #                        );
284 #}
285
286 my $sql_query = {
287   'table' => 'svc_acct',
288   'hashref'   => {}, # \%svc_acct,
289   'select'    => join(', ',
290                     'svc_acct.*',
291                     'part_svc.svc',
292                     'cust_main.custnum',
293                     FS::UI::Web::cust_sql_fields(),
294                   ),
295   'extra_sql' => "$extra_sql $orderby",
296   'addl_from' => $addl_from,
297 };
298
299 </%init>