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