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