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