last login reporting (#2952)
[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'      => [ '#',
8                                     'Service',
9                                     'Account',
10                                     'UID',
11                                     'Last Login',
12                                     FS::UI::Web::cust_header(),
13                                   ],
14                  'fields'      => [ 'svcnum',
15                                     'svc',
16                                     'email',
17                                     'uid',
18                                     'last_login_text',
19                                     \&FS::UI::Web::cust_fields,
20                                   ],
21                  'links'       => [ $link,
22                                     $link,
23                                     $link,
24                                     $link,
25                                     $link,
26                                     ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
27                                           FS::UI::Web::cust_header()
28                                     ),
29                                   ],
30                  'align' => 'rlllr'. FS::UI::Web::cust_aligns(),
31                  'color' => [ 
32                               '',
33                               '',
34                               '',
35                               '',
36                               '',
37                               FS::UI::Web::cust_colors(),
38                             ],
39                  'style' => [ 
40                               '',
41                               '',
42                               '',
43                               '',
44                               '',
45                               FS::UI::Web::cust_styles(),
46                             ],
47              )
48 %>
49 <%init>
50
51 die "access denied"
52   unless $FS::CurrentUser::CurrentUser->access_right('List services');
53
54 my @extra_sql = ();
55
56  if ( $cgi->param('domain') ) { 
57    my $svc_domain =
58      qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
59    unless ( $svc_domain ) {
60      #it would be nice if this looked more like the other "not found"
61      #errors, but this will do for now.
62      errorpage("Domain ". $cgi->param('domain'). " not found at all");
63    } else {
64      push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
65    }
66  }
67
68 my $orderby = 'ORDER BY svcnum';
69 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
70
71   push @extra_sql, 'pkgnum IS NULL'
72     if $cgi->param('magic') eq 'unlinked';
73
74   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
75     my $sortby = $1;
76     $sortby = "LOWER($sortby)"
77       if $sortby eq 'username';
78     push @extra_sql, "$sortby IS NOT NULL"
79       if ($sortby eq 'uid' or $sortby eq 'last_login');
80     $orderby = "ORDER BY $sortby";
81   }
82
83 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
84   push @extra_sql, "popnum = $1";
85   $orderby = "ORDER BY LOWER(username)";
86 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
87   push @extra_sql, "svcpart = $1";
88   $orderby = "ORDER BY uid";
89   #$orderby = "ORDER BY svcnum";
90 } else {
91   $orderby = "ORDER BY uid";
92
93   my @username_sql;
94
95   my %username_type;
96   foreach ( $cgi->param('username_type') ) {
97     $username_type{$_}++;
98   }
99
100   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
101   my $username = $1;
102
103   push @username_sql, "username ILIKE '$username'"
104     if $username_type{'Exact'}
105     || $username_type{'Fuzzy'};
106
107   push @username_sql, "username ILIKE '\%$username\%'"
108     if $username_type{'Substring'}
109     || $username_type{'All'};
110
111   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
112     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
113     my $all_username = &FS::svc_acct::all_username;
114
115     my %username;
116     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
117       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
118         $username{$_}++; 
119       }
120     }
121
122     #if ($username_type{'Sound-alike'}) {
123     #}
124
125     push @username_sql, "username = '$_'"
126       foreach (keys %username);
127
128   }
129
130   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
131
132 }
133
134 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
135                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
136                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
137                 ' LEFT JOIN cust_main USING ( custnum ) ';
138
139 #here is the agent virtualization
140 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
141
142 my $extra_sql = 
143   scalar(@extra_sql)
144     ? ' WHERE '. join(' AND ', @extra_sql )
145     : '';
146
147 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
148 #if ( keys %svc_acct ) {
149 #  $count_query .= ' WHERE '.
150 #                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
151 #                                      keys %svc_acct
152 #                        );
153 #}
154
155 my $sql_query = {
156   'table' => 'svc_acct',
157   'hashref'   => {}, # \%svc_acct,
158   'select'    => join(', ',
159                     'svc_acct.*',
160                     'part_svc.svc',
161                     'cust_main.custnum',
162                     FS::UI::Web::cust_sql_fields(),
163                   ),
164   'extra_sql' => "$extra_sql $orderby",
165   'addl_from' => $addl_from,
166 };
167
168 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
169 my $link_cust = sub {
170   my $svc_acct = shift;
171   if ( $svc_acct->custnum ) {
172     [ "${p}view/cust_main.cgi?", 'custnum' ];
173   } else {
174     '';
175   }
176 };
177
178 </%init>
179