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