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