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