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