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