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...";
89 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
90 || ( $conf->exists('hidecancelledcustomers')
91 && ! $cgi->param('showcancelledcustomers') )
93 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
95 ( 0 < ( SELECT COUNT(*) FROM cust_pkg
96 WHERE cust_pkg.custnum = cust_main.custnum
97 AND ( cust_pkg.cancel IS NULL
98 OR cust_pkg.cancel = 0
101 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
102 WHERE cust_pkg.custnum = cust_main.custnum
108 push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
109 push @qual, FS::cust_main->prospect_sql if $cgi->param('prospect');
110 push @qual, FS::cust_main->active_sql if $cgi->param('active');
111 push @qual, FS::cust_main->susp_sql if $cgi->param('suspended');
114 my $qual = join(' AND ',
115 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
117 my $addl_qual = join(' AND ', @qual);
120 $qual .= ' AND ' if $qual;
124 $qual = " WHERE $qual" if $qual;
125 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
126 my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
127 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
129 $total = $sth->fetchrow_arrayref->[0];
133 $addl_qual = " AND $addl_qual";
135 $addl_qual = " WHERE $addl_qual";
139 @cust_main = qsearch('cust_main', \%search, '',
140 "$addl_qual $orderby $limit" );
142 # foreach my $cust_main ( @just_cust_main ) {
145 # $FS::Record::DEBUG=1;
146 # ( $cache, @one_cust_main ) = jsearch(
148 # { 'custnum' => $cust_main->custnum },
154 # push @cust_main, @one_cust_main;
159 $sortby = \*last_sort;
161 push @cust_main, @{&custnumsearch}
162 if $cgi->param('custnum_on') && $cgi->param('custnum_text');
163 push @cust_main, @{&cardsearch}
164 if $cgi->param('card_on') && $cgi->param('card');
165 push @cust_main, @{&lastsearch}
166 if $cgi->param('last_on') && $cgi->param('last_text');
167 push @cust_main, @{&companysearch}
168 if $cgi->param('company_on') && $cgi->param('company_text');
169 push @cust_main, @{&address2search}
170 if $cgi->param('address2_on') && $cgi->param('address2_text');
171 push @cust_main, @{&phonesearch}
172 if $cgi->param('phone_on') && $cgi->param('phone_text');
173 push @cust_main, @{&referralsearch}
174 if $cgi->param('referral_custnum');
176 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
177 $sortby = \*company_sort;
178 push @cust_main, @{&companysearch};
181 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
182 if ! $cgi->param('cancelled')
184 $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
185 || ( $conf->exists('hidecancelledcustomers')
186 && ! $cgi->param('showcancelledcustomers') )
190 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
194 if ( $conf->exists('hidecancelledpackages' ) ) {
195 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
197 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
201 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
202 if ( $cgi->param('quickpay') eq 'yes' ) {
203 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
205 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
208 } elsif ( scalar(@cust_main) == 0 ) {
210 <!-- mason kludge -->
212 eidiot "No matching customers found!\n";
215 <!-- mason kludge -->
218 $total ||= scalar(@cust_main);
219 print header("Customer Search Results",menubar(
220 'Main Menu', popurl(2)
221 )), "$total matching customers found ";
225 if ( $total != scalar(@cust_main) && $maxrecords ) {
226 unless ( $offset == 0 ) {
227 $cgi->param('offset', $offset - $maxrecords);
228 $pager .= '<A HREF="'. $cgi->self_url.
229 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
233 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
235 if ( $offset == $poff ) {
236 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
238 $cgi->param('offset', $poff);
239 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
242 unless ( $offset + $maxrecords > $total ) {
243 $cgi->param('offset', $offset + $maxrecords);
244 $pager .= '<A HREF="'. $cgi->self_url.
245 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
250 unless ( $cgi->param('cancelled') ) {
251 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
252 || ( $conf->exists('hidecancelledcustomers')
253 && ! $cgi->param('showcancelledcustomers')
256 $cgi->param('showcancelledcustomers', 1);
257 $cgi->param('offset', 0);
258 print qq!( <a href="!. $cgi->self_url. qq!">show!;
260 $cgi->param('showcancelledcustomers', 0);
261 $cgi->param('offset', 0);
262 print qq!( <a href="!. $cgi->self_url. qq!">hide!;
264 print ' cancelled customers</a> )';
266 if ( $cgi->param('referral_custnum') ) {
267 $cgi->param('referral_custnum') =~ /^(\d+)$/
268 or eidiot "Illegal referral_custnum\n";
269 my $referral_custnum = $1;
270 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
271 print '<FORM METHOD=POST>'.
272 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
273 'referrals of <A HREF="'. popurl(2).
274 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
275 ( $cust_main->company
276 || $cust_main->last. ', '. $cust_main->first ).
280 function changed(what) {
285 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
286 my $max = 8; #config file
287 $cgi->param('referral_depth') =~ /^(\d*)$/
288 or eidiot "Illegal referral_depth";
289 my $referral_depth = $1;
291 foreach my $depth ( 1 .. $max ) {
293 ' SELECTED'x($depth == $referral_depth),
296 print "</SELECT> levels deep".
297 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
301 print "<BR><BR>". $pager. &table(). <<END;
308 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
310 <TH>(service) name</TH>
317 <TH COLSPAN=2>Services</TH>
324 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
326 my($custnum,$last,$first,$company)=(
328 $cust_main->getfield('last'),
329 $cust_main->getfield('first'),
334 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
335 foreach ( @{$all_pkgs{$custnum}} ) {
336 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
337 my @cust_svc = $_->cust_svc;
338 push @lol_cust_svc, \@cust_svc;
339 $rowspan += scalar(@cust_svc) || 1;
342 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
344 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
345 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
347 $view = $p. 'view/cust_main.cgi?'. $custnum;
349 my $pcompany = $company
350 ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
351 : '<FONT SIZE=-1> </FONT>';
354 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
355 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
356 <TD ROWSPAN=$rowspan>$pcompany</TD>
358 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
359 my($ship_last,$ship_first,$ship_company)=(
360 $cust_main->ship_last || $cust_main->getfield('last'),
361 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
362 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
364 my $pship_company = $ship_company
365 ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
366 : '<FONT SIZE=-1> </FONT>';
368 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
369 <TD ROWSPAN=$rowspan>$pship_company</A></TD>
374 foreach ( @{$all_pkgs{$custnum}} ) {
375 my $pkgnum = $_->pkgnum;
376 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
377 my $part_pkg = $_->part_pkg;
379 my $pkg = $part_pkg->pkg;
380 my $comment = $part_pkg->comment;
381 my $pkgview = "${p}view/cust_main.cgi?$custnum#cust_pkg$pkgnum";
382 my @cust_svc = @{shift @lol_cust_svc};
383 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
384 my $rowspan = scalar(@cust_svc) || 1;
386 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
388 foreach my $cust_svc ( @cust_svc ) {
389 my($label, $value, $svcdb) = $cust_svc->label;
390 my($svcnum) = $cust_svc->svcnum;
391 my($sview) = $p.'view';
392 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
393 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
396 #print qq!</TR><TR>\n!;
402 print "</TABLE>$pager</BODY></HTML>";
406 #undef $cache; #does this help?
411 lc($a->getfield('last')) cmp lc($b->getfield('last'))
412 || lc($a->first) cmp lc($b->first);
416 return -1 if $a->company && ! $b->company;
417 return 1 if ! $a->company && $b->company;
418 lc($a->company) cmp lc($b->company)
419 || lc($a->getfield('last')) cmp lc($b->getfield('last'))
420 || lc($a->first) cmp lc($b->first);;
424 $a->getfield('custnum') <=> $b->getfield('custnum');
429 my $custnum = $cgi->param('custnum_text');
431 $custnum =~ /^(\d{1,23})$/ or eidiot "Illegal customer number\n";
434 [ qsearchs('cust_main', { 'custnum' => $custnum } ) ];
439 my($card)=$cgi->param('card');
441 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
444 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}),
445 qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'DCRD'})
450 $cgi->param('referral_custnum') =~ /^(\d+)$/
451 or eidiot "Illegal referral_custnum";
452 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
453 or eidiot "Customer $1 not found";
455 if ( $cgi->param('referral_depth') ) {
456 $cgi->param('referral_depth') =~ /^(\d+)$/
457 or eidiot "Illegal referral_depth";
462 [ $cust_main->referral_cust_main($depth) ];
468 foreach ( $cgi->param('last_type') ) {
472 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
473 or eidiot "Illegal last name";
476 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
477 push @cust_main, qsearch( 'cust_main',
478 { 'last' => { 'op' => 'ILIKE',
479 'value' => $last } } );
481 push @cust_main, qsearch( 'cust_main',
482 { 'ship_last' => { 'op' => 'ILIKE',
483 'value' => $last } } )
484 if defined dbdef->table('cust_main')->column('ship_last');
487 if ( $last_type{'Substring'} || $last_type{'All'} ) {
489 push @cust_main, qsearch( 'cust_main',
490 { 'last' => { 'op' => 'ILIKE',
491 'value' => "%$last%" } } );
493 push @cust_main, qsearch( 'cust_main',
494 { 'ship_last' => { 'op' => 'ILIKE',
495 'value' => "%$last%" } } )
496 if defined dbdef->table('cust_main')->column('ship_last');
500 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
502 &FS::cust_main::check_and_rebuild_fuzzyfiles;
503 my $all_last = &FS::cust_main::all_last;
506 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
507 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
512 #if ($last_type{'Sound-alike'}) {
515 foreach ( keys %last ) {
516 push @cust_main, qsearch('cust_main',{'last'=>$_});
517 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
518 if defined dbdef->table('cust_main')->column('ship_last');
530 foreach ( $cgi->param('company_type') ) {
534 $cgi->param('company_text') =~
535 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
536 or eidiot "Illegal company";
539 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
540 push @cust_main, qsearch( 'cust_main',
541 { 'company' => { 'op' => 'ILIKE',
542 'value' => $company } } );
544 push @cust_main, qsearch( 'cust_main',
545 { 'ship_company' => { 'op' => 'ILIKE',
546 'value' => $company } } )
547 if defined dbdef->table('cust_main')->column('ship_last');
550 if ( $company_type{'Substring'} || $company_type{'All'} ) {
552 push @cust_main, qsearch( 'cust_main',
553 { 'company' => { 'op' => 'ILIKE',
554 'value' => "%$company%" } } );
556 push @cust_main, qsearch( 'cust_main',
557 { 'ship_company' => { 'op' => 'ILIKE',
558 'value' => "%$company%" } })
559 if defined dbdef->table('cust_main')->column('ship_last');
563 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
565 &FS::cust_main::check_and_rebuild_fuzzyfiles;
566 my $all_company = &FS::cust_main::all_company;
569 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
570 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
575 #if ($company_type{'Sound-alike'}) {
578 foreach ( keys %company ) {
579 push @cust_main, qsearch('cust_main',{'company'=>$_});
580 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
581 if defined dbdef->table('cust_main')->column('ship_last');
592 $cgi->param('address2_text') =~
593 /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
594 or eidiot "Illegal address2";
597 push @cust_main, qsearch( 'cust_main',
598 { 'address2' => { 'op' => 'ILIKE',
599 'value' => $address2 } } );
600 push @cust_main, qsearch( 'cust_main',
601 { 'address2' => { 'op' => 'ILIKE',
602 'value' => $address2 } } )
603 if defined dbdef->table('cust_main')->column('ship_last');
611 my $phone = $cgi->param('phone_text');
613 #(no longer really) false laziness with Record::ut_phonen
614 #only works with US/CA numbers...
616 if ( $phone =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ ) {
618 $phone .= " x$4" if $4;
619 } elsif ( $phone =~ /^(\d{3})(\d{4})$/ ) {
621 } elsif ( $phone =~ /^(\d{3,4})$/ ) {
624 eidiot gettext('illegal_phone'). ": $phone";
627 my @fields = qw(daytime night fax);
628 push @fields, qw(ship_daytime ship_night ship_fax)
629 if defined dbdef->table('cust_main')->column('ship_last');
631 for my $field ( @fields ) {
632 push @cust_main, qsearch ( 'cust_main',
633 { $field => { 'op' => 'LIKE',
634 'value' => "%$phone%" } } );