new 'jsearch' call for big joined searches & caching support
[freeside.git] / httemplate / search / cust_main.cgi
1 <%
2 #<!-- $Id: cust_main.cgi,v 1.14 2001-11-03 17:49:52 ivan Exp $ -->
3
4 use strict;
5 #use vars qw( $conf %ncancelled_pkgs %all_pkgs $cgi @cust_main $sortby );
6 use vars qw( $conf %all_pkgs $cgi @cust_main $sortby );
7 use CGI;
8 use CGI::Carp qw(fatalsToBrowser);
9 use IO::Handle;
10 use String::Approx qw(amatch);
11 use FS::UID qw(cgisuidsetup);
12 use FS::Record qw(qsearch qsearchs dbdef jsearch);
13 use FS::CGI qw(header menubar eidiot popurl table);
14 use FS::cust_main;
15 use FS::cust_svc;
16
17 $cgi = new CGI;
18 cgisuidsetup($cgi);
19
20 $conf = new FS::Conf;
21
22 my $cache;
23
24 #my $monsterjoin = <<END;
25 #cust_main left outer join (
26 #  ( cust_pkg left outer join part_pkg using(pkgpart)
27 #  ) left outer join (
28 #    (
29 #      (
30 #        ( cust_svc left outer join part_svc using (svcpart)
31 #        ) left outer join svc_acct using (svcnum)
32 #      ) left outer join svc_domain using(svcnum)
33 #    ) left outer join svc_forward using(svcnum)
34 #  ) using (pkgnum)
35 #) using (custnum)
36 #END
37
38 my $monsterjoin = <<END;
39 cust_main left outer join (
40   ( cust_pkg left outer join part_pkg using(pkgpart)
41   ) left outer join (
42     (
43       (
44         ( cust_svc left outer join part_svc using (svcpart)
45         ) left outer join (
46           svc_acct left outer join (
47             select svcnum, domain, catchall from svc_domain
48             ) as svc_acct_domsvc (
49               svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
50           ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
51         ) using (svcnum)
52       ) left outer join svc_domain using(svcnum)
53     ) left outer join svc_forward using(svcnum)
54   ) using (pkgnum)
55 ) using (custnum)
56 END
57
58 if ( $cgi->param('browse') ) {
59   my $query = $cgi->param('browse');
60   if ( $query eq 'custnum' ) {
61     $sortby=\*custnum_sort;
62 #    @cust_main=qsearch('cust_main',{});  
63     ( $cache, @cust_main ) =
64       jsearch($monsterjoin, {}, '', '', 'cust_main', 'custnum' );  
65   } elsif ( $query eq 'last' ) {
66     $sortby=\*last_sort;
67 #    @cust_main=qsearch('cust_main',{});  
68     ( $cache, @cust_main ) =
69       jsearch($monsterjoin, {}, '', '', 'cust_main', 'custnum' );  
70   } elsif ( $query eq 'company' ) {
71     $sortby=\*company_sort;
72 #    @cust_main=qsearch('cust_main',{});
73     ( $cache, @cust_main ) =
74       jsearch($monsterjoin, {}, '', '', 'cust_main', 'custnum' );  
75   } else {
76     die "unknown browse field $query";
77   }
78 } else {
79   @cust_main=();
80   &cardsearch if $cgi->param('card_on') && $cgi->param('card');
81   &lastsearch if $cgi->param('last_on') && $cgi->param('last_text');
82   &companysearch if $cgi->param('company_on') && $cgi->param('company_text');
83   &referralsearch if $cgi->param('referral_custnum');
84 }
85
86 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
87   if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
88      || ( $conf->exists('hidecancelledcustomers')
89            && ! $cgi->param('showcancelledcustomers') );
90 if ( $conf->exists('hidecancelledpackages' ) ) {
91   %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
92 } else {
93   %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
94 }
95 #%all_pkgs = ();
96
97 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
98   print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
99   exit;
100 } elsif ( scalar(@cust_main) == 0 ) {
101   eidiot "No matching customers found!\n";
102 } else { 
103
104   my $total = scalar(@cust_main);
105   print header("Customer Search Results",menubar(
106     'Main Menu', popurl(2)
107   )), "$total matching customers found ";
108   if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
109        || ( $conf->exists('hidecancelledcustomers')
110             && ! $cgi->param('showcancelledcustomers')
111           )
112      ) {
113     $cgi->param('showcancelledcustomers', 1);
114     print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
115   } else {
116     $cgi->param('showcancelledcustomers', 0);
117     print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
118   }
119   if ( $cgi->param('referral_custnum') ) {
120     $cgi->param('referral_custnum') =~ /^(\d+)$/
121       or eidiot "Illegal referral_custnum\n";
122     my $referral_custnum = $1;
123     my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
124     print '<FORM METHOD=POST>'.
125           qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
126           'referrals of <A HREF="'. popurl(2).
127           "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
128           ( $cust_main->company
129             || $cust_main->last. ', '. $cust_main->first ).
130           '</A>';
131     print "\n",<<END;
132       <SCRIPT>
133       function changed(what) {
134         what.form.submit();
135       }
136       </SCRIPT>
137 END
138     print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
139     my $max = 8; #config file
140     $cgi->param('referral_depth') =~ /^(\d*)$/ 
141       or eidiot "Illegal referral_depth";
142     my $referral_depth = $1;
143
144     foreach my $depth ( 1 .. $max ) {
145       print '<OPTION',
146             ' SELECTED'x($depth == $referral_depth),
147             ">$depth";
148     }
149     print "</SELECT> levels deep".
150           '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
151           '</FORM>';
152   }
153   print "<BR>", &table(), <<END;
154       <TR>
155         <TH></TH>
156         <TH>(bill) name</TH>
157         <TH>company</TH>
158 END
159
160 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
161   print <<END;
162       <TH>(service) name</TH>
163       <TH>company</TH>
164 END
165 }
166
167 print <<END;
168         <TH>Packages</TH>
169         <TH COLSPAN=2>Services</TH>
170       </TR>
171 END
172
173   my(%saw,$cust_main);
174   my $p = popurl(2);
175   foreach $cust_main (
176     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
177   ) {
178     my($custnum,$last,$first,$company)=(
179       $cust_main->custnum,
180       $cust_main->getfield('last'),
181       $cust_main->getfield('first'),
182       $cust_main->company,
183     );
184
185     my(@lol_cust_svc);
186     my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
187     foreach ( @{$all_pkgs{$custnum}} ) {
188       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
189       my @cust_svc = $_->cust_svc;
190       push @lol_cust_svc, \@cust_svc;
191       $rowspan += scalar(@cust_svc) || 1;
192     }
193
194     #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
195     my $view = $p. 'view/cust_main.cgi?'. $custnum;
196     print <<END;
197     <TR>
198       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
199       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
200       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
201 END
202     if ( defined dbdef->table('cust_main')->column('ship_last') ) {
203       my($ship_last,$ship_first,$ship_company)=(
204         $cust_main->ship_last || $cust_main->getfield('last'),
205         $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
206         $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
207       );
208 print <<END;
209       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
210       <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
211 END
212     }
213
214     my($n1)='';
215     foreach ( @{$all_pkgs{$custnum}} ) {
216       my $pkgnum = $_->pkgnum;
217 #      my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
218       my $part_pkg = $_->part_pkg;
219
220       my $pkg = $part_pkg->pkg;
221       my $comment = $part_pkg->comment;
222       my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
223       my @cust_svc = @{shift @lol_cust_svc};
224       #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
225       my $rowspan = scalar(@cust_svc) || 1;
226
227       print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
228       my($n2)='';
229       foreach my $cust_svc ( @cust_svc ) {
230          my($label, $value, $svcdb) = $cust_svc->label;
231          my($svcnum) = $cust_svc->svcnum;
232          my($sview) = $p.'view';
233          print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
234                qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
235          $n2="</TR><TR>";
236       }
237       #print qq!</TR><TR>\n!;
238       $n1="</TR><TR>";
239     }
240     print "</TR>";
241   }
242  
243   print <<END;
244     </TABLE>
245   </BODY>
246 </HTML>
247 END
248
249 }
250
251 #
252
253 sub last_sort {
254   $a->getfield('last') cmp $b->getfield('last');
255 }
256
257 sub company_sort {
258   return -1 if $a->company && ! $b->company;
259   return 1 if ! $a->company && $b->company;
260   $a->getfield('company') cmp $b->getfield('company');
261 }
262
263 sub custnum_sort {
264   $a->getfield('custnum') <=> $b->getfield('custnum');
265 }
266
267 sub cardsearch {
268
269   my($card)=$cgi->param('card');
270   $card =~ s/\D//g;
271   $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
272   my($payinfo)=$1;
273
274   push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
275   $sortby=\*last_sort;
276 }
277
278 sub referralsearch {
279   $cgi->param('referral_custnum') =~ /^(\d+)$/
280     or eidiot "Illegal referral_custnum";
281   my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
282     or eidiot "Customer $1 not found";
283   my $depth;
284   if ( $cgi->param('referral_depth') ) {
285     $cgi->param('referral_depth') =~ /^(\d+)$/
286       or eidiot "Illegal referral_depth";
287     $depth = $1;
288   } else {
289     $depth = 1;
290   }
291   push @cust_main, $cust_main->referral_cust_main($depth);
292   $sortby=\*last_sort;
293 }
294
295 sub lastsearch {
296   my(%last_type);
297   foreach ( $cgi->param('last_type') ) {
298     $last_type{$_}++;
299   }
300
301   $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
302     or eidiot "Illegal last name";
303   my($last)=$1;
304
305   if ( $last_type{'Exact'}
306        && ! $last_type{'Fuzzy'} 
307      #  && ! $last_type{'Sound-alike'}
308   ) {
309
310     push @cust_main, qsearch('cust_main',{'last'=>$last});
311
312     push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
313       if defined dbdef->table('cust_main')->column('ship_last');
314
315   } else {
316
317     &FS::cust_main::check_and_rebuild_fuzzyfiles;
318     my $all_last = &FS::cust_main::all_last;
319
320     my %last;
321     if ($last_type{'Fuzzy'}) { 
322       foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
323         $last{$_}++; 
324       }
325     }
326
327     #if ($last_type{'Sound-alike'}) {
328     #}
329
330     foreach ( keys %last ) {
331       push @cust_main, qsearch('cust_main',{'last'=>$_});
332       push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
333         if defined dbdef->table('cust_main')->column('ship_last');
334     }
335
336   }
337   $sortby=\*last_sort;
338 }
339
340 sub companysearch {
341
342   my(%company_type);
343   foreach ( $cgi->param('company_type') ) {
344     $company_type{$_}++ 
345   };
346
347   $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
348     or eidiot "Illegal company";
349   my($company)=$1;
350
351   if ( $company_type{'Exact'}
352        && ! $company_type{'Fuzzy'} 
353      #  && ! $company_type{'Sound-alike'}
354   ) {
355
356     push @cust_main, qsearch('cust_main',{'company'=>$company});
357
358     push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
359       if defined dbdef->table('cust_main')->column('ship_last');
360
361   } else {
362
363     &FS::cust_main::check_and_rebuild_fuzzyfiles;
364     my $all_company = &FS::cust_main::all_company;
365
366     my %company;
367     if ($company_type{'Fuzzy'}) { 
368       foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
369         $company{$_}++;
370       }
371     }
372
373     #if ($company_type{'Sound-alike'}) {
374     #}
375
376     foreach ( keys %company ) {
377       push @cust_main, qsearch('cust_main',{'company'=>$_});
378       push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
379         if defined dbdef->table('cust_main')->column('ship_last');
380     }
381
382   }
383   $sortby=\*company_sort;
384
385 }
386 %>