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