for mod_perl: no more top-level my() variables; use vars instead
[freeside.git] / htdocs / search / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.6 1999-01-19 05:14:12 ivan Exp $
4 #
5 # Usage: post form to:
6 #        http://server.name/path/cust_main.cgi
7 #
8 # Note: Should be run setuid freeside as user nobody.
9 #
10 # ivan@voicenet.com 96-dec-12
11 #
12 # rewrite ivan@sisd.com 98-mar-4
13 #
14 # now does browsing too ivan@sisd.com 98-mar-6
15 #
16 # Changes to allow page to work at a relative position in server
17 #       bmccane@maxbaud.net     98-apr-3
18 #
19 # display total, use FS::CGI ivan@sisd.com 98-jul-17
20 #
21 # $Log: cust_main.cgi,v $
22 # Revision 1.6  1999-01-19 05:14:12  ivan
23 # for mod_perl: no more top-level my() variables; use vars instead
24 # also the last s/create/new/;
25 #
26 # Revision 1.5  1999/01/18 09:41:37  ivan
27 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
28 # (good idea anyway)
29 #
30 # Revision 1.4  1998/12/30 00:57:50  ivan
31 # bug
32 #
33 # Revision 1.3  1998/12/17 09:41:08  ivan
34 # s/CGI::(Base|Request)/CGI.pm/;
35 #
36 # Revision 1.2  1998/11/12 08:10:22  ivan
37 # CGI.pm instead of CGI-modules
38 # relative URLs using popurl
39 # got rid of lots of little tables
40 # s/agrep/String::Approx/;
41 # bubble up packages and services and link (slow)
42 #
43
44 use strict;
45 use vars qw(%ncancelled_pkgs %all_pkgs $cgi @cust_main $sortby );
46 use CGI;
47 use CGI::Carp qw(fatalsToBrowser);
48 use IO::Handle;
49 use String::Approx qw(amatch);
50 use FS::UID qw(cgisuidsetup);
51 use FS::Record qw(qsearch qsearchs);
52 use FS::CGI qw(header menubar idiot popurl table);
53 use FS::cust_main;
54
55 $cgi = new CGI;
56 cgisuidsetup($cgi);
57
58 if ( $cgi->keywords ) {
59   my($query)=$cgi->keywords;
60   if ( $query eq 'custnum' ) {
61     $sortby=\*custnum_sort;
62     @cust_main=qsearch('cust_main',{});  
63   } elsif ( $query eq 'last' ) {
64     $sortby=\*last_sort;
65     @cust_main=qsearch('cust_main',{});  
66   } elsif ( $query eq 'company' ) {
67     $sortby=\*company_sort;
68     @cust_main=qsearch('cust_main',{});
69   }
70 } else {
71   &cardsearch if ( $cgi->param('card_on') && $cgi->param('card') );
72   &lastsearch if ( $cgi->param('last_on') && $cgi->param('last_text') );
73   &companysearch if ( $cgi->param('company_on') && $cgi->param('company_text') );
74 }
75
76 %ncancelled_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
77 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
78
79 if ( scalar(@cust_main) == 1 ) {
80   print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
81   exit;
82 } elsif ( scalar(@cust_main) == 0 ) {
83   idiot "No matching customers found!\n";
84   exit;
85 } else { 
86
87   my($total)=scalar(@cust_main);
88   print $cgi->header( '-expires' => 'now' ), header("Customer Search Results",menubar(
89     'Main Menu', popurl(2)
90   )), "$total matching customers found<BR>", table, <<END;
91       <TR>
92         <TH></TH>
93         <TH>Contact name</TH>
94         <TH>Company</TH>
95         <TH>Packages</TH>
96         <TH COLSPAN=2>Services</TH>
97       </TR>
98 END
99
100   my(%saw,$cust_main);
101   foreach $cust_main (
102     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
103   ) {
104     my($custnum,$last,$first,$company)=(
105       $cust_main->custnum,
106       $cust_main->getfield('last'),
107       $cust_main->getfield('first'),
108       $cust_main->company,
109     );
110
111     my(@lol_cust_svc);
112     my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
113     foreach ( @{$all_pkgs{$custnum}} ) {
114       my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
115       push @lol_cust_svc, \@cust_svc;
116       $rowspan += scalar(@cust_svc) || 1;
117     }
118
119     #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
120     my($view) = popurl(2). "view/cust_main.cgi?$custnum";
121     print <<END;
122     <TR>
123       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
124       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
125       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
126 END
127
128     my($n1)='';
129     foreach ( @{$all_pkgs{$custnum}} ) {
130       my($pkgnum) = ($_->pkgnum);
131       my($pkg) = $_->part_pkg->pkg;
132       my($pkgview) = popurl(2). "/view/cust_pkg.cgi?$pkgnum";
133       #my(@cust_svc) = shift @lol_cust_svc;
134       my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
135       my($rowspan) = scalar(@cust_svc) || 1;
136
137       print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg</FONT></A></TD>!;
138       my($n2)='';
139       foreach my $cust_svc ( @cust_svc ) {
140          my($label, $value, $svcdb) = $cust_svc->label;
141          my($svcnum) = $cust_svc->svcnum;
142          my($sview) = popurl(2). "/view";
143          print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
144                qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
145          $n2="</TR><TR>";
146       }
147       #print qq!</TR><TR>\n!;
148       $n1="</TR><TR>";
149     }
150     print "<\TR>";
151   }
152  
153   print <<END;
154     </TABLE>
155     </CENTER>
156   </BODY>
157 </HTML>
158 END
159
160 }
161
162 #
163
164 sub last_sort {
165   $a->getfield('last') cmp $b->getfield('last');
166 }
167
168 sub company_sort {
169   return -1 if $a->company && ! $b->company;
170   return 1 if ! $a->company && $b->company;
171   $a->getfield('company') cmp $b->getfield('company');
172 }
173
174 sub custnum_sort {
175   $a->getfield('custnum') <=> $b->getfield('custnum');
176 }
177
178 sub cardsearch {
179
180   my($card)=$cgi->param('card');
181   $card =~ s/\D//g;
182   $card =~ /^(\d{13,16})$/ or do { idiot "Illegal card number\n"; exit; };
183   my($payinfo)=$1;
184
185   push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
186
187 }
188
189 sub lastsearch {
190   my(%last_type);
191   foreach ( $cgi->param('last_type') ) {
192     $last_type{$_}++;
193   }
194
195   $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
196     or do { idiot "Illegal last name"; exit; };
197   my($last)=$1;
198
199   if ( $last_type{'Exact'}
200        && ! $last_type{'Fuzzy'} 
201      #  && ! $last_type{'Sound-alike'}
202   ) {
203
204     push @cust_main, qsearch('cust_main',{'last'=>$last});
205
206   } else {
207
208     my(%last);
209
210     my(@all_last)=map $_->getfield('last'), qsearch('cust_main',{});
211     if ($last_type{'Fuzzy'}) { 
212       foreach ( amatch($last, [ qw(i) ], @all_last) ) {
213         $last{$_}++; 
214       }
215     }
216
217     #if ($last_type{'Sound-alike'}) {
218     #}
219
220     foreach ( keys %last ) {
221       push @cust_main, qsearch('cust_main',{'last'=>$_});
222     }
223
224   }
225   $sortby=\*last_sort;
226 }
227
228 sub companysearch {
229
230   my(%company_type);
231   foreach ( $cgi->param('company_type') ) {
232     $company_type{$_}++ 
233   };
234
235   $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
236     or do { idiot "Illegal company"; exit; };
237   my($company)=$1;
238
239   if ( $company_type{'Exact'}
240        && ! $company_type{'Fuzzy'} 
241      #  && ! $company_type{'Sound-alike'}
242   ) {
243
244     push @cust_main, qsearch('cust_main',{'company'=>$company});
245
246   } else {
247
248     my(%company);
249     my(@all_company)=map $_->company, qsearch('cust_main',{});
250
251     if ($company_type{'Fuzzy'}) { 
252       foreach ( amatch($company, [ qw(i) ], @all_company ) ) {
253         $company{$_}++;
254       }
255     }
256
257     #if ($company_type{'Sound-alike'}) {
258     #}
259
260     foreach ( keys %company ) {
261       push @cust_main, qsearch('cust_main',{'company'=>$_});
262     }
263
264   }
265   $sortby=\*company_sort;
266
267 }