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