5 my $conf = new FS::Conf;
6 my $maxrecords = $conf->config('maxsearchrecordsperpage');
8 my $orderby = ''; #removeme
11 $limit .= "LIMIT $maxrecords" if $maxrecords;
13 my $offset = $cgi->param('offset') || 0;
14 $limit .= " OFFSET $offset" if $offset;
18 my($query)=$cgi->keywords;
19 $query ||= ''; #to avoid use of unitialized value errors
22 if ( $query =~ /^UN_(.*)$/ ) {
24 my $empty = driver_name =~ /^Pg$/i ? qq('') : qq("");
27 ( SELECT count(*) FROM cust_svc
28 WHERE cust_svc.svcnum = svc_acct.svcnum
29 AND ( pkgnum IS NULL OR pkgnum = 0 OR pkgnum = $empty )
34 my(@svc_acct, $sortby);
35 if ( $query eq 'svcnum' ) {
36 $sortby=\*svcnum_sort;
37 $orderby = 'ORDER BY svcnum';
38 } elsif ( $query eq 'username' ) {
39 $sortby=\*username_sort;
40 $orderby = 'ORDER BY username';
41 } elsif ( $query eq 'uid' ) {
43 $orderby = ( $unlinked ? 'AND' : 'WHERE' ). ' uid IS NOT NULL ORDER BY uid';
46 @svc_acct = @{&usernamesearch};
49 if ( $query eq 'svcnum' || $query eq 'username' || $query eq 'uid' ) {
51 my $statement = "SELECT COUNT(*) FROM svc_acct $unlinked";
52 my $sth = dbh->prepare($statement)
53 or die dbh->errstr. " doing $statement";
54 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
56 $total = $sth->fetchrow_arrayref->[0];
58 @svc_acct = qsearch('svc_acct', {}, '', "$unlinked $orderby $limit");
62 if ( scalar(@svc_acct) == 1 ) {
63 my($svcnum)=$svc_acct[0]->svcnum;
64 print $cgi->redirect(popurl(2). "view/svc_acct.cgi?$svcnum"); #redirect
66 } elsif ( scalar(@svc_acct) == 0 ) { #error
70 idiot("Account not found");
75 $total ||= scalar(@svc_acct);
79 if ( $total != scalar(@svc_acct) && $maxrecords ) {
80 unless ( $offset == 0 ) {
81 $cgi->param('offset', $offset - $maxrecords);
82 $pager .= '<A HREF="'. $cgi->self_url.
83 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
87 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
89 if ( $offset == $poff ) {
90 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
92 $cgi->param('offset', $poff);
93 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
96 unless ( $offset + $maxrecords > $total ) {
97 $cgi->param('offset', $offset + $maxrecords);
98 $pager .= '<A HREF="'. $cgi->self_url.
99 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
104 print header("Account Search Results",menubar('Main Menu'=>popurl(2))),
105 "$total matching accounts found<BR><BR>$pager",
108 <TH><FONT SIZE=-1>#</FONT></TH>
109 <TH><FONT SIZE=-1>Username</FONT></TH>
110 <TH><FONT SIZE=-1>Domain</FONT></TH>
111 <TH><FONT SIZE=-1>UID</FONT></TH>
112 <TH><FONT SIZE=-1>Service</FONT></TH>
113 <TH><FONT SIZE=-1>Cust#</FONT></TH>
114 <TH><FONT SIZE=-1>(bill) name</FONT></TH>
115 <TH><FONT SIZE=-1>company</FONT></TH>
117 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
119 <TH><FONT SIZE=-1>(service) name</FONT></TH>
120 <TH><FONT SIZE=-1>company</FONT></TH>
128 sort $sortby grep(!$saw{$_->svcnum}++, @svc_acct)
130 my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_acct->svcnum })
131 or die "No cust_svc record for svcnum ". $svc_acct->svcnum;
132 my $part_svc = qsearchs('part_svc', { 'svcpart' => $cust_svc->svcpart })
133 or die "No part_svc record for svcpart ". $cust_svc->svcpart;
136 my $svc_domain = qsearchs('svc_domain', { 'svcnum' => $svc_acct->domsvc });
138 $domain = "<A HREF=\"${p}view/svc_domain.cgi?". $svc_domain->svcnum.
139 "\">". $svc_domain->domain. "</A>";
141 unless ( $mydomain ) {
142 my $conf = new FS::Conf;
143 unless ( $mydomain = $conf->config('domain') ) {
144 die "No legacy domain config file and no svc_domain.svcnum record ".
145 "for svc_acct.domsvc: ". $svc_acct->domsvc;
148 $domain = "<i>$mydomain</i><FONT COLOR=\"#FF0000\">*</FONT>";
150 my($cust_pkg,$cust_main);
151 if ( $cust_svc->pkgnum ) {
152 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_svc->pkgnum })
153 or die "No cust_pkg record for pkgnum ". $cust_svc->pkgnum;
154 $cust_main = qsearchs('cust_main', { 'custnum' => $cust_pkg->custnum })
155 or die "No cust_main record for custnum ". $cust_pkg->custnum;
157 my($svcnum, $username, $uid, $svc, $custnum, $last, $first, $company) = (
159 $svc_acct->getfield('username'),
160 $svc_acct->getfield('uid'),
162 $cust_svc->pkgnum ? $cust_main->custnum : '',
163 $cust_svc->pkgnum ? $cust_main->getfield('last') : '',
164 $cust_svc->pkgnum ? $cust_main->getfield('first') : '',
165 $cust_svc->pkgnum ? $cust_main->company : '',
167 my($pcustnum) = $custnum
168 ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\"><FONT SIZE=-1>$custnum</FONT></A>"
169 : "<I>(unlinked)</I>"
171 my $pname = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$last, $first</A>" : '';
172 my $pcompany = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$company</A>" : '';
173 my($pship_name, $pship_company);
174 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
175 my($ship_last, $ship_first, $ship_company) = (
176 $cust_svc->pkgnum ? ( $cust_main->ship_last || $last ) : '',
177 $cust_svc->pkgnum ? ( $cust_main->ship_last
178 ? $cust_main->ship_first
181 $cust_svc->pkgnum ? ( $cust_main->ship_last
182 ? $cust_main->ship_company
186 $pship_name = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$ship_last, $ship_first</A>" : '';
187 $pship_company = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$ship_company</A>" : '';
191 <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$svcnum</FONT></A></TD>
192 <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$username</FONT></A></TD>
193 <TD><FONT SIZE=-1>$domain</FONT></TD>
194 <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$uid</FONT></A></TD>
195 <TD><FONT SIZE=-1>$svc</FONT></TH>
196 <TD><FONT SIZE=-1>$pcustnum</FONT></TH>
197 <TD><FONT SIZE=-1>$pname<FONT></TH>
198 <TD><FONT SIZE=-1>$pcompany</FONT></TH>
200 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
202 <TD><FONT SIZE=-1>$pship_name<FONT></TH>
203 <TD><FONT SIZE=-1>$pship_company</FONT></TH>
210 print "</TABLE>$pager<BR>";
213 print "<BR><FONT COLOR=\"#FF0000\">*</FONT> The <I>$mydomain</I> domain ".
214 "is contained in your legacy <CODE>domain</CODE> ".
215 "<A HREF=\"${p}docs/config.html#domain\">configuration file</A>. ".
216 "You should run the <CODE>bin/fs-migrate-svc_acct_sm</CODE> script ".
217 "to create a proper svc_domain record for this domain.";
220 print '</BODY></HTML>';
225 $a->getfield('svcnum') <=> $b->getfield('svcnum');
229 $a->getfield('username') cmp $b->getfield('username');
233 $a->getfield('uid') <=> $b->getfield('uid');
238 $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
241 [ qsearch('svc_acct',{'username'=>$username}) ];