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