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