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