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