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