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