Will things ever be the same again?
[freeside.git] / httemplate / search / svc_acct.cgi
1 %
2 %
3 %my $orderby = 'ORDER BY svcnum';
4 %
5 %my($query)=$cgi->keywords;
6 %$query ||= ''; #to avoid use of unitialized value errors
7 %
8 %my @extra_sql = ();
9 %if ( $query =~ /^UN_(.*)$/ ) {
10 %  $query = $1;
11 %  push @extra_sql, 'pkgnum IS NULL';
12 %}
13 %
14 %if ( $query eq 'svcnum' ) {
15 %  #$orderby = "ORDER BY svcnum";
16 %} elsif ( $query eq 'username' ) {
17 %  $orderby = "ORDER BY LOWER(username)";
18 %} elsif ( $query eq 'uid' ) {
19 %  $orderby = "ORDER BY uid";
20 %  push @extra_sql, "uid IS NOT NULL";
21 %} elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
22 %  push @extra_sql, "popnum = $1";
23 %  $orderby = "ORDER BY LOWER(username)";
24 %} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
25 %  push @extra_sql, "svcpart = $1";
26 %  $orderby = "ORDER BY uid";
27 %  #$orderby = "ORDER BY svcnum";
28 %} else {
29 %  $orderby = "ORDER BY uid";
30 %
31 %  my @username_sql;
32 %
33 %  my %username_type;
34 %  foreach ( $cgi->param('username_type') ) {
35 %    $username_type{$_}++;
36 %  }
37 %
38 %  $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
39 %  my $username = $1;
40 %
41 %  push @username_sql, "username ILIKE '$username'"
42 %    if $username_type{'Exact'}
43 %    || $username_type{'Fuzzy'};
44 %
45 %  push @username_sql, "username ILIKE '\%$username\%'"
46 %    if $username_type{'Substring'}
47 %    || $username_type{'All'};
48 %
49 %  if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
50 %    &FS::svc_acct::check_and_rebuild_fuzzyfiles;
51 %    my $all_username = &FS::svc_acct::all_username;
52 %
53 %    my %username;
54 %    if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
55 %      foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
56 %        $username{$_}++; 
57 %      }
58 %    }
59 %
60 %    #if ($username_type{'Sound-alike'}) {
61 %    #}
62 %
63 %    push @username_sql, "username = '$_'"
64 %      foreach (keys %username);
65 %
66 %  }
67 %
68 %  push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
69 %
70 %}
71 %
72 %my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
73 %                ' LEFT JOIN part_svc  USING ( svcpart ) '.
74 %                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
75 %                ' LEFT JOIN cust_main USING ( custnum ) ';
76 %
77 %#here is the agent virtualization
78 %push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
79 %
80 %my $extra_sql = 
81 %  scalar(@extra_sql)
82 %    ? ' WHERE '. join(' AND ', @extra_sql )
83 %    : '';
84 %
85 %my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
86 %#if ( keys %svc_acct ) {
87 %#  $count_query .= ' WHERE '.
88 %#                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
89 %#                                      keys %svc_acct
90 %#                        );
91 %#}
92 %
93 %my $sql_query = {
94 %  'table' => 'svc_acct',
95 %  'hashref'   => {}, # \%svc_acct,
96 %  'select'    => join(', ',
97 %                    'svc_acct.*',
98 %                    'part_svc.svc',
99 %                    'cust_main.custnum',
100 %                    FS::UI::Web::cust_sql_fields(),
101 %                  ),
102 %  'extra_sql' => "$extra_sql $orderby",
103 %  'addl_from' => $addl_from,
104 %};
105 %
106 %my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
107 %my $link_cust = sub {
108 %  my $svc_acct = shift;
109 %  if ( $svc_acct->custnum ) {
110 %    [ "${p}view/cust_main.cgi?", 'custnum' ];
111 %  } else {
112 %    '';
113 %  }
114 %};
115 %
116 %
117 <% include( 'elements/search.html',
118                  'title'       => 'Account Search Results',
119                  'name'        => 'accounts',
120                  'query'       => $sql_query,
121                  'count_query' => $count_query,
122                  'redirect'    => $link,
123                  'header'      => [ '#',
124                                     'Service',
125                                     'Account',
126                                     'UID',
127                                     FS::UI::Web::cust_header(),
128                                   ],
129                  'fields'      => [ 'svcnum',
130                                     'svc',
131                                     'email',
132                                     'uid',
133                                     \&FS::UI::Web::cust_fields,
134                                   ],
135                  'links'       => [ $link,
136                                     $link,
137                                     $link,
138                                     $link,
139                                     ( map { $link_cust }
140                                           FS::UI::Web::cust_header()
141                                     ),
142                                   ],
143              )
144 %>