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