3 my $conf = new FS::Conf;
4 my $maxrecords = $conf->config('maxsearchrecordsperpage');
8 #my $monsterjoin = <<END;
9 #cust_main left outer join (
10 # ( cust_pkg left outer join part_pkg using(pkgpart)
14 # ( cust_svc left outer join part_svc using (svcpart)
15 # ) left outer join svc_acct using (svcnum)
16 # ) left outer join svc_domain using(svcnum)
17 # ) left outer join svc_forward using(svcnum)
22 #my $monsterjoin = <<END;
23 #cust_main left outer join (
24 # ( cust_pkg left outer join part_pkg using(pkgpart)
28 # ( cust_svc left outer join part_svc using (svcpart)
30 # svc_acct left outer join (
31 # select svcnum, domain, catchall from svc_domain
32 # ) as svc_acct_domsvc (
33 # svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
34 # ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
36 # ) left outer join svc_domain using(svcnum)
37 # ) left outer join svc_forward using(svcnum)
43 $limit .= "LIMIT $maxrecords" if $maxrecords;
45 my $offset = $cgi->param('offset') || 0;
46 $limit .= " OFFSET $offset" if $offset;
50 my(@cust_main, $sortby, $orderby);
51 if ( $cgi->param('browse')
52 || $cgi->param('otaker_on')
56 if ( $cgi->param('browse') ) {
57 my $query = $cgi->param('browse');
58 if ( $query eq 'custnum' ) {
59 $sortby=\*custnum_sort;
60 $orderby = "ORDER BY custnum";
61 } elsif ( $query eq 'last' ) {
63 $orderby = "ORDER BY LOWER(last || ' ' || first)";
64 } elsif ( $query eq 'company' ) {
65 $sortby=\*company_sort;
66 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
68 die "unknown browse field $query";
71 $sortby = \*last_sort; #??
72 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
73 if ( $cgi->param('otaker_on') ) {
74 $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
77 die "unknown query...";
83 if ( driver_name eq 'mysql' ) {
85 my $query = "CREATE TEMPORARY TABLE temp1_$$ TYPE=MYISAM
86 SELECT cust_pkg.custnum,COUNT(*) as count
87 FROM cust_pkg,cust_main
88 WHERE cust_pkg.custnum = cust_main.custnum
89 AND ( cust_pkg.cancel IS NULL
90 OR cust_pkg.cancel = 0 )
91 GROUP BY cust_pkg.custnum";
92 my $sth = dbh->prepare($query) or die dbh->errstr. " preparing $query";
93 $sth->execute or die "Error executing \"$query\": ". $sth->errstr;
94 $query = "CREATE TEMPORARY TABLE temp2_$$ TYPE=MYISAM
95 SELECT cust_pkg.custnum,COUNT(*) as count
96 FROM cust_pkg,cust_main
97 WHERE cust_pkg.custnum = cust_main.custnum
98 GROUP BY cust_pkg.custnum";
99 my $sth = dbh->prepare($query) or die dbh->errstr. " preparing $query";
100 $sth->execute or die "Error executing \"$query\": ". $sth->errstr;
103 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
104 || ( $conf->exists('hidecancelledcustomers')
105 && ! $cgi->param('showcancelledcustomers') )
107 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
108 if ( driver_name eq 'mysql' ) {
110 temp1_$$.custnum = cust_main.custnum
111 AND temp2_$$.custnum = cust_main.custnum
112 AND (temp1_$$.count > 0
113 OR temp2_$$.count = 0 )
117 0 < ( SELECT COUNT(*) FROM cust_pkg
118 WHERE cust_pkg.custnum = cust_main.custnum
119 AND ( cust_pkg.cancel IS NULL
120 OR cust_pkg.cancel = 0
123 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
124 WHERE cust_pkg.custnum = cust_main.custnum
132 my $qual = join(' AND ',
133 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
136 $qual .= ' AND ' if $qual;
137 $qual .= $ncancelled;
140 $qual = " WHERE $qual" if $qual;
142 if ( driver_name eq 'mysql' ) {
143 $statement = "SELECT COUNT(*) FROM cust_main";
144 $statement .= ", temp1_$$, temp2_$$ $qual" if $qual;
146 $statement = "SELECT COUNT(*) FROM cust_main $qual";
148 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
149 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
151 $total = $sth->fetchrow_arrayref->[0];
155 $ncancelled = " AND $ncancelled";
157 $ncancelled = " WHERE $ncancelled";
162 if ( driver_name eq 'mysql' ) {
163 @just_cust_main = qsearch('cust_main', \%search, 'cust_main.*',
164 ",temp1_$$,temp2_$$ $ncancelled $orderby $limit");
166 @just_cust_main = qsearch('cust_main', \%search, '',
167 "$ncancelled $orderby $limit" );
169 if ( driver_name eq 'mysql' ) {
170 $query = "DROP TABLE temp1_$$,temp2_$$;";
171 my $sth = dbh->prepare($query) or die dbh->errstr. " preparing $query";
172 $sth->execute or die "Error executing \"$query\": ". $sth->errstr;
174 @cust_main = @just_cust_main;
176 # foreach my $cust_main ( @just_cust_main ) {
179 # $FS::Record::DEBUG=1;
180 # ( $cache, @one_cust_main ) = jsearch(
182 # { 'custnum' => $cust_main->custnum },
188 # push @cust_main, @one_cust_main;
193 $sortby = \*last_sort;
195 push @cust_main, @{&custnumsearch}
196 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
197 push @cust_main, @{&cardsearch}
198 if $cgi->param('card_on') && $cgi->param('card');
199 push @cust_main, @{&lastsearch}
200 if $cgi->param('last_on') && $cgi->param('last_text');
201 push @cust_main, @{&companysearch}
202 if $cgi->param('company_on') && $cgi->param('company_text');
203 push @cust_main, @{&referralsearch}
204 if $cgi->param('referral_custnum');
206 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
207 $sortby = \*company_sort;
208 push @cust_main, @{&companysearch};
211 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
212 if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
213 || ( $conf->exists('hidecancelledcustomers')
214 && ! $cgi->param('showcancelledcustomers') );
217 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
221 if ( $conf->exists('hidecancelledpackages' ) ) {
222 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
224 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
228 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
229 if ( $cgi->param('quickpay') eq 'yes' ) {
230 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
232 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
235 } elsif ( scalar(@cust_main) == 0 ) {
237 <!-- mason kludge -->
239 eidiot "No matching customers found!\n";
242 <!-- mason kludge -->
245 $total ||= scalar(@cust_main);
246 print header("Customer Search Results",menubar(
247 'Main Menu', popurl(2)
248 )), "$total matching customers found ";
252 if ( $total != scalar(@cust_main) && $maxrecords ) {
253 unless ( $offset == 0 ) {
254 $cgi->param('offset', $offset - $maxrecords);
255 $pager .= '<A HREF="'. $cgi->self_url.
256 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
260 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
262 if ( $offset == $poff ) {
263 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
265 $cgi->param('offset', $poff);
266 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
269 unless ( $offset + $maxrecords > $total ) {
270 $cgi->param('offset', $offset + $maxrecords);
271 $pager .= '<A HREF="'. $cgi->self_url.
272 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
277 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
278 || ( $conf->exists('hidecancelledcustomers')
279 && ! $cgi->param('showcancelledcustomers')
282 $cgi->param('showcancelledcustomers', 1);
283 $cgi->param('offset', 0);
284 print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
286 $cgi->param('showcancelledcustomers', 0);
287 $cgi->param('offset', 0);
288 print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
290 if ( $cgi->param('referral_custnum') ) {
291 $cgi->param('referral_custnum') =~ /^(\d+)$/
292 or eidiot "Illegal referral_custnum\n";
293 my $referral_custnum = $1;
294 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
295 print '<FORM METHOD=POST>'.
296 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
297 'referrals of <A HREF="'. popurl(2).
298 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
299 ( $cust_main->company
300 || $cust_main->last. ', '. $cust_main->first ).
304 function changed(what) {
309 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
310 my $max = 8; #config file
311 $cgi->param('referral_depth') =~ /^(\d*)$/
312 or eidiot "Illegal referral_depth";
313 my $referral_depth = $1;
315 foreach my $depth ( 1 .. $max ) {
317 ' SELECTED'x($depth == $referral_depth),
320 print "</SELECT> levels deep".
321 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
325 print "<BR><BR>". $pager. &table(). <<END;
332 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
334 <TH>(service) name</TH>
341 <TH COLSPAN=2>Services</TH>
348 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
350 my($custnum,$last,$first,$company)=(
352 $cust_main->getfield('last'),
353 $cust_main->getfield('first'),
358 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
359 foreach ( @{$all_pkgs{$custnum}} ) {
360 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
361 my @cust_svc = $_->cust_svc;
362 push @lol_cust_svc, \@cust_svc;
363 $rowspan += scalar(@cust_svc) || 1;
366 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
368 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
369 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
371 $view = $p. 'view/cust_main.cgi?'. $custnum;
373 my $pcompany = $company
374 ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
375 : '<FONT SIZE=-1> </FONT>';
378 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
379 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
380 <TD ROWSPAN=$rowspan>$pcompany</TD>
382 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
383 my($ship_last,$ship_first,$ship_company)=(
384 $cust_main->ship_last || $cust_main->getfield('last'),
385 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
386 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
388 my $pship_company = $ship_company
389 ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
390 : '<FONT SIZE=-1> </FONT>';
392 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
393 <TD ROWSPAN=$rowspan>$pship_company</A></TD>
398 foreach ( @{$all_pkgs{$custnum}} ) {
399 my $pkgnum = $_->pkgnum;
400 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
401 my $part_pkg = $_->part_pkg;
403 my $pkg = $part_pkg->pkg;
404 my $comment = $part_pkg->comment;
405 my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
406 my @cust_svc = @{shift @lol_cust_svc};
407 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
408 my $rowspan = scalar(@cust_svc) || 1;
410 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
412 foreach my $cust_svc ( @cust_svc ) {
413 my($label, $value, $svcdb) = $cust_svc->label;
414 my($svcnum) = $cust_svc->svcnum;
415 my($sview) = $p.'view';
416 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
417 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
420 #print qq!</TR><TR>\n!;
426 print "</TABLE>$pager</BODY></HTML>";
430 #undef $cache; #does this help?
435 lc($a->getfield('last')) cmp lc($b->getfield('last'))
436 || lc($a->first) cmp lc($b->first);
440 return -1 if $a->company && ! $b->company;
441 return 1 if ! $a->company && $b->company;
442 lc($a->company) cmp lc($b->company)
443 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
444 || lc($a->first) cmp lc($b->first);;
448 $a->getfield('custnum') <=> $b->getfield('custnum');
453 my $custnum = $cgi->param('custnum_text');
455 $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
458 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
463 my($card)=$cgi->param('card');
465 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
468 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}) ];
472 $cgi->param('referral_custnum') =~ /^(\d+)$/
473 or eidiot "Illegal referral_custnum";
474 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
475 or eidiot "Customer $1 not found";
477 if ( $cgi->param('referral_depth') ) {
478 $cgi->param('referral_depth') =~ /^(\d+)$/
479 or eidiot "Illegal referral_depth";
484 [ $cust_main->referral_cust_main($depth) ];
490 foreach ( $cgi->param('last_type') ) {
494 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
495 or eidiot "Illegal last name";
498 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
499 push @cust_main, qsearch( 'cust_main',
500 { 'last' => { 'op' => 'ILIKE',
501 'value' => $last } } );
503 push @cust_main, qsearch( 'cust_main',
504 { 'ship_last' => { 'op' => 'ILIKE',
505 'value' => $last } } )
506 if defined dbdef->table('cust_main')->column('ship_last');
509 if ( $last_type{'Substring'} || $last_type{'All'} ) {
511 push @cust_main, qsearch( 'cust_main',
512 { 'last' => { 'op' => 'ILIKE',
513 'value' => "%$last%" } } );
515 push @cust_main, qsearch( 'cust_main',
516 { 'ship_last' => { 'op' => 'ILIKE',
517 'value' => "%$last%" } } )
518 if defined dbdef->table('cust_main')->column('ship_last');
522 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
524 &FS::cust_main::check_and_rebuild_fuzzyfiles;
525 my $all_last = &FS::cust_main::all_last;
528 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
529 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
534 #if ($last_type{'Sound-alike'}) {
537 foreach ( keys %last ) {
538 push @cust_main, qsearch('cust_main',{'last'=>$_});
539 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
540 if defined dbdef->table('cust_main')->column('ship_last');
552 foreach ( $cgi->param('company_type') ) {
556 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
557 or eidiot "Illegal company";
560 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
561 push @cust_main, qsearch( 'cust_main',
562 { 'company' => { 'op' => 'ILIKE',
563 'value' => $company } } );
565 push @cust_main, qsearch( 'cust_main',
566 { 'ship_company' => { 'op' => 'ILIKE',
567 'value' => $company } } )
568 if defined dbdef->table('cust_main')->column('ship_last');
571 if ( $company_type{'Substring'} || $company_type{'All'} ) {
573 push @cust_main, qsearch( 'cust_main',
574 { 'company' => { 'op' => 'ILIKE',
575 'value' => "%$company%" } } );
577 push @cust_main, qsearch( 'cust_main',
578 { 'ship_company' => { 'op' => 'ILIKE',
579 'value' => "%$company%" } })
580 if defined dbdef->table('cust_main')->column('ship_last');
584 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
586 &FS::cust_main::check_and_rebuild_fuzzyfiles;
587 my $all_company = &FS::cust_main::all_company;
590 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
591 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
596 #if ($company_type{'Sound-alike'}) {
599 foreach ( keys %company ) {
600 push @cust_main, qsearch('cust_main',{'company'=>$_});
601 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
602 if defined dbdef->table('cust_main')->column('ship_last');