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
21 # $Log: cust_main.cgi,v $
22 # Revision 1.2 1998-11-12 08:10:22 ivan
23 # CGI.pm instead of CGI-modules
24 # relative URLs using popurl
25 # got rid of lots of little tables
26 # s/agrep/String::Approx/;
27 # bubble up packages and services and link (slow)
31 use vars qw(%ncancelled_pkgs %all_pkgs);
33 use CGI::Carp qw(fatalsToBrowser);
35 use String::Approx qw(amatch);
36 use FS::UID qw(cgisuidsetup);
37 use FS::Record qw(qsearch qsearchs);
38 use FS::CGI qw(header menubar idiot popurl table);
47 if ( $cgi->keywords ) {
48 my($query)=$cgi->keywords;
49 if ( $query eq 'custnum' ) {
50 $sortby=\*custnum_sort;
51 @cust_main=qsearch('cust_main',{});
52 } elsif ( $query eq 'last' ) {
54 @cust_main=qsearch('cust_main',{});
55 } elsif ( $query eq 'company' ) {
56 $sortby=\*company_sort;
57 @cust_main=qsearch('cust_main',{});
60 &cardsearch if ( $cgi->param('card_on') && $cgi->param('card') );
61 &lastsearch if ( $cgi->param('last_on') && $cgi->param('last_text') );
62 &companysearch if ( $cgi->param('company_on') && $cgi->param('company_text') );
65 my(%ncancelled_pkgs) =
66 map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
68 map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
70 if ( scalar(@cust_main) == 1 ) {
71 print $cgi->redirect(popurl(2). "/view/cust_main.cgi?". $cust_main[0]->custnum);
73 } elsif ( scalar(@cust_main) == 0 ) {
74 idiot "No matching customers found!\n";
78 my($total)=scalar(@cust_main);
79 print $cgi->header, header("Customer Search Results",menubar(
80 'Main Menu', popurl(2)
81 )), "$total matching customers found<BR>", table, <<END;
87 <TH COLSPAN=2>Services</TH>
93 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
95 my($custnum,$last,$first,$company)=(
97 $cust_main->getfield('last'),
98 $cust_main->getfield('first'),
103 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
104 foreach ( @{$all_pkgs{$custnum}} ) {
105 my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
106 push @lol_cust_svc, \@cust_svc;
107 $rowspan += scalar(@cust_svc) || 1;
110 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
111 my($view) = popurl(2). "/view/cust_main.cgi?$custnum";
114 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
115 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
116 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
120 foreach ( @{$all_pkgs{$custnum}} ) {
121 my($pkgnum) = ($_->pkgnum);
122 my($pkg) = $_->part_pkg->pkg;
123 my($pkgview) = popurl(2). "/view/cust_pkg.cgi?$pkgnum";
124 #my(@cust_svc) = shift @lol_cust_svc;
125 my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
126 my($rowspan) = scalar(@cust_svc) || 1;
128 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg</FONT></A></TD>!;
130 foreach my $cust_svc ( @cust_svc ) {
131 my($label, $value, $svcdb) = $cust_svc->label;
132 my($svcnum) = $cust_svc->svcnum;
133 my($sview) = popurl(2). "/view";
134 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
135 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
138 #print qq!</TR><TR>\n!;
156 $a->getfield('last') cmp $b->getfield('last');
160 return -1 if $a->company && ! $b->company;
161 return 1 if ! $a->company && $b->company;
162 $a->getfield('company') cmp $b->getfield('company');
166 $a->getfield('custnum') <=> $b->getfield('custnum');
171 my($card)=$cgi->param('card');
173 $card =~ /^(\d{13,16})$/ or do { idiot "Illegal card number\n"; exit; };
176 push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
182 foreach ( $cgi->param('last_type') ) {
186 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
187 or do { idiot "Illegal last name"; exit; };
190 if ( $last_type{'Exact'}
191 && ! $last_type{'Fuzzy'}
192 # && ! $last_type{'Sound-alike'}
195 push @cust_main, qsearch('cust_main',{'last'=>$last});
201 my(@all_last)=map $_->getfield('last'), qsearch('cust_main',{});
202 if ($last_type{'Fuzzy'}) {
203 foreach ( amatch($last, [ qw(i) ], @all_last) ) {
208 #if ($last_type{'Sound-alike'}) {
211 foreach ( keys %last ) {
212 push @cust_main, qsearch('cust_main',{'last'=>$_});
222 foreach ( $cgi->param('company_type') ) {
226 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
227 or do { idiot "Illegal company"; exit; };
230 if ( $company_type{'Exact'}
231 && ! $company_type{'Fuzzy'}
232 # && ! $company_type{'Sound-alike'}
235 push @cust_main, qsearch('cust_main',{'company'=>$company});
240 my(@all_company)=map $_->company, qsearch('cust_main',{});
242 if ($company_type{'Fuzzy'}) {
243 foreach ( amatch($company, [ qw(i) ], @all_company ) ) {
248 #if ($company_type{'Sound-alike'}) {
251 foreach ( keys %company ) {
252 push @cust_main, qsearch('cust_main',{'company'=>$_});
256 $sortby=\*company_sort;