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')
53 || $cgi->param('agentnum_on')
57 if ( $cgi->param('browse') ) {
58 my $query = $cgi->param('browse');
59 if ( $query eq 'custnum' ) {
60 $sortby=\*custnum_sort;
61 $orderby = "ORDER BY custnum";
62 } elsif ( $query eq 'last' ) {
64 $orderby = "ORDER BY LOWER(last || ' ' || first)";
65 } elsif ( $query eq 'company' ) {
66 $sortby=\*company_sort;
67 $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
69 die "unknown browse field $query";
72 $sortby = \*last_sort; #??
73 $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
74 if ( $cgi->param('otaker_on') ) {
75 $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
77 } elsif ( $cgi->param('agentnum_on') ) {
78 $cgi->param('agentnum') =~ /^(\d+)$/ or eidiot "Illegal agentnum\n";
79 $search{agentnum} = $1;
81 die "unknown query...";
87 if ( driver_name eq 'mysql' ) {
89 my $sql = "CREATE TEMPORARY TABLE temp1_$$ TYPE=MYISAM
90 SELECT cust_pkg.custnum,COUNT(*) as count
91 FROM cust_pkg,cust_main
92 WHERE cust_pkg.custnum = cust_main.custnum
93 AND ( cust_pkg.cancel IS NULL
94 OR cust_pkg.cancel = 0 )
95 GROUP BY cust_pkg.custnum";
96 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
97 $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
98 $sql = "CREATE TEMPORARY TABLE temp2_$$ TYPE=MYISAM
99 SELECT cust_pkg.custnum,COUNT(*) as count
100 FROM cust_pkg,cust_main
101 WHERE cust_pkg.custnum = cust_main.custnum
102 GROUP BY cust_pkg.custnum";
103 $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
104 $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
107 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
108 || ( $conf->exists('hidecancelledcustomers')
109 && ! $cgi->param('showcancelledcustomers') )
111 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
112 if ( driver_name eq 'mysql' ) {
114 temp1_$$.custnum = cust_main.custnum
115 AND temp2_$$.custnum = cust_main.custnum
116 AND (temp1_$$.count > 0
117 OR temp2_$$.count = 0 )
122 0 < ( SELECT COUNT(*) FROM cust_pkg
123 WHERE cust_pkg.custnum = cust_main.custnum
124 AND ( cust_pkg.cancel IS NULL
125 OR cust_pkg.cancel = 0
128 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
129 WHERE cust_pkg.custnum = cust_main.custnum
136 if ( $cgi->param('cancelled') ) {
138 0 = ( SELECT COUNT(*) FROM cust_pkg
139 WHERE cust_pkg.custnum = cust_main.custnum
140 AND ( cust_pkg.cancel IS NULL
141 OR cust_pkg.cancel = 0
144 AND 0 < ( SELECT COUNT(*) FROM cust_pkg
145 WHERE cust_pkg.custnum = cust_main.custnum
151 my $qual = join(' AND ',
152 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
155 $qual .= ' AND ' if $qual;
157 } elsif ( $ncancelled ) {
158 $qual .= ' AND ' if $qual;
159 $qual .= $ncancelled;
162 $qual = " WHERE $qual" if $qual;
164 if ( driver_name eq 'mysql' ) {
165 $statement = "SELECT COUNT(*) FROM cust_main";
166 $statement .= ", temp1_$$, temp2_$$ $qual" if $qual;
168 $statement = "SELECT COUNT(*) FROM cust_main $qual";
170 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
171 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
173 $total = $sth->fetchrow_arrayref->[0];
175 my $rqual = $cancelled || $ncancelled;
178 $rqual = " AND $rqual";
180 $rqual = " WHERE $rqual";
185 if ( driver_name eq 'mysql' ) {
186 @just_cust_main = qsearch('cust_main', \%search, 'cust_main.*',
187 ",temp1_$$,temp2_$$ $rqual $orderby $limit");
189 @just_cust_main = qsearch('cust_main', \%search, '',
190 "$rqual $orderby $limit" );
192 if ( driver_name eq 'mysql' ) {
193 my $sql = "DROP TABLE temp1_$$,temp2_$$;";
194 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
195 $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
197 @cust_main = @just_cust_main;
199 # foreach my $cust_main ( @just_cust_main ) {
202 # $FS::Record::DEBUG=1;
203 # ( $cache, @one_cust_main ) = jsearch(
205 # { 'custnum' => $cust_main->custnum },
211 # push @cust_main, @one_cust_main;
216 $sortby = \*last_sort;
218 push @cust_main, @{&custnumsearch}
219 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
220 push @cust_main, @{&cardsearch}
221 if $cgi->param('card_on') && $cgi->param('card');
222 push @cust_main, @{&lastsearch}
223 if $cgi->param('last_on') && $cgi->param('last_text');
224 push @cust_main, @{&companysearch}
225 if $cgi->param('company_on') && $cgi->param('company_text');
226 push @cust_main, @{&address2search}
227 if $cgi->param('address2_on') && $cgi->param('address2_text');
228 push @cust_main, @{&phonesearch}
229 if $cgi->param('phone_on') && $cgi->param('phone_text');
230 push @cust_main, @{&referralsearch}
231 if $cgi->param('referral_custnum');
233 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
234 $sortby = \*company_sort;
235 push @cust_main, @{&companysearch};
238 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
239 if ! $cgi->param('cancelled')
241 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
242 || ( $conf->exists('hidecancelledcustomers')
243 && ! $cgi->param('showcancelledcustomers') )
247 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
251 if ( $conf->exists('hidecancelledpackages' ) ) {
252 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
254 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
258 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
259 if ( $cgi->param('quickpay') eq 'yes' ) {
260 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
262 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
265 } elsif ( scalar(@cust_main) == 0 ) {
267 <!-- mason kludge -->
269 eidiot "No matching customers found!\n";
272 <!-- mason kludge -->
275 $total ||= scalar(@cust_main);
276 print header("Customer Search Results",menubar(
277 'Main Menu', popurl(2)
278 )), "$total matching customers found ";
282 if ( $total != scalar(@cust_main) && $maxrecords ) {
283 unless ( $offset == 0 ) {
284 $cgi->param('offset', $offset - $maxrecords);
285 $pager .= '<A HREF="'. $cgi->self_url.
286 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
290 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
292 if ( $offset == $poff ) {
293 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
295 $cgi->param('offset', $poff);
296 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
299 unless ( $offset + $maxrecords > $total ) {
300 $cgi->param('offset', $offset + $maxrecords);
301 $pager .= '<A HREF="'. $cgi->self_url.
302 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
307 unless ( $cgi->param('cancelled') ) {
308 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
309 || ( $conf->exists('hidecancelledcustomers')
310 && ! $cgi->param('showcancelledcustomers')
313 $cgi->param('showcancelledcustomers', 1);
314 $cgi->param('offset', 0);
315 print qq!( <a href="!. $cgi->self_url. qq!">show!;
317 $cgi->param('showcancelledcustomers', 0);
318 $cgi->param('offset', 0);
319 print qq!( <a href="!. $cgi->self_url. qq!">hide!;
321 print ' cancelled customers</a> )';
323 if ( $cgi->param('referral_custnum') ) {
324 $cgi->param('referral_custnum') =~ /^(\d+)$/
325 or eidiot "Illegal referral_custnum\n";
326 my $referral_custnum = $1;
327 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
328 print '<FORM METHOD=POST>'.
329 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
330 'referrals of <A HREF="'. popurl(2).
331 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
332 ( $cust_main->company
333 || $cust_main->last. ', '. $cust_main->first ).
337 function changed(what) {
342 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
343 my $max = 8; #config file
344 $cgi->param('referral_depth') =~ /^(\d*)$/
345 or eidiot "Illegal referral_depth";
346 my $referral_depth = $1;
348 foreach my $depth ( 1 .. $max ) {
350 ' SELECTED'x($depth == $referral_depth),
353 print "</SELECT> levels deep".
354 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
358 print "<BR><BR>". $pager. &table(). <<END;
365 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
367 <TH>(service) name</TH>
374 <TH COLSPAN=2>Services</TH>
381 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
383 my($custnum,$last,$first,$company)=(
385 $cust_main->getfield('last'),
386 $cust_main->getfield('first'),
391 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
392 foreach ( @{$all_pkgs{$custnum}} ) {
393 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
394 my @cust_svc = $_->cust_svc;
395 push @lol_cust_svc, \@cust_svc;
396 $rowspan += scalar(@cust_svc) || 1;
399 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
401 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
402 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
404 $view = $p. 'view/cust_main.cgi?'. $custnum;
406 my $pcompany = $company
407 ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
408 : '<FONT SIZE=-1> </FONT>';
411 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
412 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
413 <TD ROWSPAN=$rowspan>$pcompany</TD>
415 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
416 my($ship_last,$ship_first,$ship_company)=(
417 $cust_main->ship_last || $cust_main->getfield('last'),
418 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
419 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
421 my $pship_company = $ship_company
422 ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
423 : '<FONT SIZE=-1> </FONT>';
425 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
426 <TD ROWSPAN=$rowspan>$pship_company</A></TD>
431 foreach ( @{$all_pkgs{$custnum}} ) {
432 my $pkgnum = $_->pkgnum;
433 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
434 my $part_pkg = $_->part_pkg;
436 my $pkg = $part_pkg->pkg;
437 my $comment = $part_pkg->comment;
438 my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
439 my @cust_svc = @{shift @lol_cust_svc};
440 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
441 my $rowspan = scalar(@cust_svc) || 1;
443 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
445 foreach my $cust_svc ( @cust_svc ) {
446 my($label, $value, $svcdb) = $cust_svc->label;
447 my($svcnum) = $cust_svc->svcnum;
448 my($sview) = $p.'view';
449 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
450 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
453 #print qq!</TR><TR>\n!;
459 print "</TABLE>$pager</BODY></HTML>";
463 #undef $cache; #does this help?
468 lc($a->getfield('last')) cmp lc($b->getfield('last'))
469 || lc($a->first) cmp lc($b->first);
473 return -1 if $a->company && ! $b->company;
474 return 1 if ! $a->company && $b->company;
475 lc($a->company) cmp lc($b->company)
476 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
477 || lc($a->first) cmp lc($b->first);;
481 $a->getfield('custnum') <=> $b->getfield('custnum');
486 my $custnum = $cgi->param('custnum_text');
488 $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
491 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
496 my($card)=$cgi->param('card');
498 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
501 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}) ];
505 $cgi->param('referral_custnum') =~ /^(\d+)$/
506 or eidiot "Illegal referral_custnum";
507 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
508 or eidiot "Customer $1 not found";
510 if ( $cgi->param('referral_depth') ) {
511 $cgi->param('referral_depth') =~ /^(\d+)$/
512 or eidiot "Illegal referral_depth";
517 [ $cust_main->referral_cust_main($depth) ];
523 foreach ( $cgi->param('last_type') ) {
527 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
528 or eidiot "Illegal last name";
531 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
532 push @cust_main, qsearch( 'cust_main',
533 { 'last' => { 'op' => 'ILIKE',
534 'value' => $last } } );
536 push @cust_main, qsearch( 'cust_main',
537 { 'ship_last' => { 'op' => 'ILIKE',
538 'value' => $last } } )
539 if defined dbdef->table('cust_main')->column('ship_last');
542 if ( $last_type{'Substring'} || $last_type{'All'} ) {
544 push @cust_main, qsearch( 'cust_main',
545 { 'last' => { 'op' => 'ILIKE',
546 'value' => "%$last%" } } );
548 push @cust_main, qsearch( 'cust_main',
549 { 'ship_last' => { 'op' => 'ILIKE',
550 'value' => "%$last%" } } )
551 if defined dbdef->table('cust_main')->column('ship_last');
555 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
557 &FS::cust_main::check_and_rebuild_fuzzyfiles;
558 my $all_last = &FS::cust_main::all_last;
561 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
562 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
567 #if ($last_type{'Sound-alike'}) {
570 foreach ( keys %last ) {
571 push @cust_main, qsearch('cust_main',{'last'=>$_});
572 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
573 if defined dbdef->table('cust_main')->column('ship_last');
585 foreach ( $cgi->param('company_type') ) {
589 $cgi->param('company_text') =~
590 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
591 or eidiot "Illegal company";
594 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
595 push @cust_main, qsearch( 'cust_main',
596 { 'company' => { 'op' => 'ILIKE',
597 'value' => $company } } );
599 push @cust_main, qsearch( 'cust_main',
600 { 'ship_company' => { 'op' => 'ILIKE',
601 'value' => $company } } )
602 if defined dbdef->table('cust_main')->column('ship_last');
605 if ( $company_type{'Substring'} || $company_type{'All'} ) {
607 push @cust_main, qsearch( 'cust_main',
608 { 'company' => { 'op' => 'ILIKE',
609 'value' => "%$company%" } } );
611 push @cust_main, qsearch( 'cust_main',
612 { 'ship_company' => { 'op' => 'ILIKE',
613 'value' => "%$company%" } })
614 if defined dbdef->table('cust_main')->column('ship_last');
618 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
620 &FS::cust_main::check_and_rebuild_fuzzyfiles;
621 my $all_company = &FS::cust_main::all_company;
624 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
625 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
630 #if ($company_type{'Sound-alike'}) {
633 foreach ( keys %company ) {
634 push @cust_main, qsearch('cust_main',{'company'=>$_});
635 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
636 if defined dbdef->table('cust_main')->column('ship_last');
647 $cgi->param('address2_text') =~
648 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
649 or eidiot "Illegal address2";
652 push @cust_main, qsearch( 'cust_main',
653 { 'address2' => { 'op' => 'ILIKE',
654 'value' => $address2 } } );
655 push @cust_main, qsearch( 'cust_main',
656 { 'address2' => { 'op' => 'ILIKE',
657 'value' => $address2 } } )
658 if defined dbdef->table('cust_main')->column('ship_last');
666 my $phone = $cgi->param('phone_text');
668 #(no longer really) false laziness with Record::ut_phonen
669 #only works with US/CA numbers...
671 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
673 $phone .= " x$4" if $4;
674 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
676 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
679 eidiot gettext('illegal_phone'). ": $phone";
682 my @fields = qw(daytime night fax);
683 push @fields, qw(ship_daytime ship_night ship_fax)
684 if defined dbdef->table('cust_main')->column('ship_last');
686 for my $field ( @fields ) {
687 push @cust_main, qsearch ( 'cust_main',
688 { $field => { 'op' => 'LIKE',
689 'value' => "%$phone%" } } );