3 # process/cust_main.cgi: Search for customers (process form)
6 # http://server.name/path/cust_main.cgi
8 # Note: Should be run setuid freeside as user nobody.
10 # ivan@voicenet.com 96-dec-12
12 # rewrite ivan@sisd.com 98-mar-4
14 # now does browsing too ivan@sisd.com 98-mar-6
16 # Changes to allow page to work at a relative position in server
17 # bmccane@maxbaud.net 98-apr-3
19 # display total, use FS::CGI ivan@sisd.com 98-jul-17
23 use CGI::Carp qw(fatalsToBrowser);
26 use FS::UID qw(cgisuidsetup);
27 use FS::Record qw(qsearch qsearchs);
28 use FS::CGI qw(header idiot);
30 my($fuzziness)=2; #fuzziness for fuzzy searches, see man agrep
31 #0-4: 0=no fuzz, 4=very fuzzy (too much fuzz!)
33 my($req)=new CGI::Request;
34 &cgisuidsetup($req->cgi);
39 my($query)=$req->cgi->var('QUERY_STRING');
40 if ( $query eq 'custnum' ) {
41 $sortby=\*custnum_sort;
42 @cust_main=qsearch('cust_main',{});
43 } elsif ( $query eq 'last' ) {
45 @cust_main=qsearch('cust_main',{});
46 } elsif ( $query eq 'company' ) {
47 $sortby=\*company_sort;
48 @cust_main=qsearch('cust_main',{});
50 &cardsearch if ($req->param('card_on') );
51 &lastsearch if ($req->param('last_on') );
52 &companysearch if ($req->param('company_on') );
55 if ( scalar(@cust_main) == 1 ) {
56 $req->cgi->redirect("../view/cust_main.cgi?". $cust_main[0]->custnum);
58 } elsif ( scalar(@cust_main) == 0 ) {
59 idiot "No matching customers found!\n";
63 my($total)=scalar(@cust_main);
64 CGI::Base::SendHeaders(); # one guess
65 print header("Customer Search Results",''), <<END;
67 $total matching customers found
68 <TABLE BORDER=4 CELLSPACING=0 CELLPADDING=0>
80 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
82 my($custnum,$last,$first,$company)=(
84 $cust_main->getfield('last'),
85 $cust_main->getfield('first'),
90 <TD><A HREF="../view/cust_main.cgi?$custnum"><FONT SIZE=-1>$custnum</FONT></A></TD>
91 <TD><FONT SIZE=-1>$last, $first</FONT></TD>
92 <TD><FONT SIZE=-1>$company</FONT></TD>
95 if ($lcount-- == 0) { # lots of little tables instead of one big one
99 <TABLE BORDER=4 CELLSPACING=0 CELLPADDING=0>
102 <TH>Contact name</TH>
121 $a->getfield('last') cmp $b->getfield('last');
125 $a->getfield('company') cmp $b->getfield('company');
129 $a->getfield('custnum') <=> $b->getfield('custnum');
134 my($card)=$req->param('card');
136 $card =~ /^(\d{13,16})$/ or do { idiot "Illegal card number\n"; exit; };
139 push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
145 foreach ( $req->param('last_type') ) {
149 $req->param('last_text') =~ /^([\w \,\.\-\']*)$/
150 or do { idiot "Illegal last name"; exit; };
153 if ( $last_type{'Exact'}
154 && ! $last_type{'Fuzzy'}
155 # && ! $last_type{'Sound-alike'}
158 push @cust_main, qsearch('cust_main',{'last'=>$last});
164 my(@all_last)=map $_->getfield('last'), qsearch('cust_main',{});
165 if ($last_type{'Fuzzy'}) {
166 my($reader,$writer) = ( new IO::Handle, new IO::Handle );
167 open2($reader,$writer,'agrep',"-$fuzziness",'-i','-k',
169 print $writer join("\n",@all_last),"\n";
178 #if ($last_type{'Sound-alike'}) {
181 foreach ( keys %last ) {
182 push @cust_main, qsearch('cust_main',{'last'=>$_});
192 foreach ( $req->param('company_type') ) {
196 $req->param('company_text') =~ /^([\w \,\.\-\']*)$/
197 or do { idiot "Illegal company"; exit; };
200 if ( $company_type{'Exact'}
201 && ! $company_type{'Fuzzy'}
202 # && ! $company_type{'Sound-alike'}
205 push @cust_main, qsearch('cust_main',{'company'=>$company});
210 my(@all_company)=map $_->company, qsearch('cust_main',{});
212 if ($company_type{'Fuzzy'}) {
213 my($reader,$writer) = ( new IO::Handle, new IO::Handle );
214 open2($reader,$writer,'agrep',"-$fuzziness",'-i','-k',
215 substr($company,0,30));
216 print $writer join("\n",@all_company),"\n";
225 #if ($company_type{'Sound-alike'}) {
228 foreach ( keys %company ) {
229 push @cust_main, qsearch('cust_main',{'company'=>$_});
233 $sortby=\*company_sort;