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 last';
64 } elsif ( $query eq 'company' ) {
65 $sortby=\*company_sort;
66 $orderby = 'ORDER BY company';
68 die "unknown browse field $query";
71 $sortby = \*last_sort; #??
72 $orderby = 'ORDER BY last'; #??
73 if ( $cgi->param('otaker_on') ) {
74 $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
77 die "unknown query...";
83 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
84 || ( $conf->exists('hidecancelledcustomers')
85 && ! $cgi->param('showcancelledcustomers') )
87 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
88 #needed for MySQL??? OR cust_pkg.cancel = \"\"
90 0 < ( SELECT COUNT(*) FROM cust_pkg
91 WHERE cust_pkg.custnum = cust_main.custnum
92 AND ( cust_pkg.cancel IS NULL
93 OR cust_pkg.cancel = 0
96 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
97 WHERE cust_pkg.custnum = cust_main.custnum
103 my $qual = join(' AND ',
104 map { "$_ = ". dbh->quote($search{$_}) } keys %search );
107 $qual .= ' AND ' if $qual;
108 $qual .= $ncancelled;
111 $qual = " WHERE $qual" if $qual;
113 my $statement = "SELECT COUNT(*) FROM cust_main $qual";
114 my $sth = dbh->prepare($statement)
115 or die dbh->errstr. " doing $statement";
116 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
118 $total = $sth->fetchrow_arrayref->[0];
122 $ncancelled = " AND $ncancelled";
124 $ncancelled = " WHERE $ncancelled";
127 my @just_cust_main = qsearch('cust_main', \%search, '',
128 "$ncancelled $orderby $limit"
131 @cust_main = @just_cust_main;
133 # foreach my $cust_main ( @just_cust_main ) {
136 # $FS::Record::DEBUG=1;
137 # ( $cache, @one_cust_main ) = jsearch(
139 # { 'custnum' => $cust_main->custnum },
145 # push @cust_main, @one_cust_main;
150 $sortby = \*last_sort;
152 push @cust_main, @{&cardsearch}
153 if $cgi->param('card_on') && $cgi->param('card');
154 push @cust_main, @{&lastsearch}
155 if $cgi->param('last_on') && $cgi->param('last_text');
156 push @cust_main, @{&companysearch}
157 if $cgi->param('company_on') && $cgi->param('company_text');
158 push @cust_main, @{&referralsearch}
159 if $cgi->param('referral_custnum');
161 if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
162 $sortby = \*company_sort;
163 push @cust_main, @{&companysearch};
166 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
167 if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
168 || ( $conf->exists('hidecancelledcustomers')
169 && ! $cgi->param('showcancelledcustomers') );
172 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
176 if ( $conf->exists('hidecancelledpackages' ) ) {
177 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
179 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
183 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
184 if ( $cgi->param('quickpay') eq 'yes' ) {
185 print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
187 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
190 } elsif ( scalar(@cust_main) == 0 ) {
192 <!-- mason kludge -->
194 eidiot "No matching customers found!\n";
197 <!-- mason kludge -->
200 $total ||= scalar(@cust_main);
201 print header("Customer Search Results",menubar(
202 'Main Menu', popurl(2)
203 )), "$total matching customers found ";
207 if ( $total != scalar(@cust_main) && $maxrecords ) {
208 unless ( $offset == 0 ) {
209 $cgi->param('offset', $offset - $maxrecords);
210 $pager .= '<A HREF="'. $cgi->self_url.
211 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
215 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
217 if ( $offset == $poff ) {
218 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
220 $cgi->param('offset', $poff);
221 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
224 unless ( $offset + $maxrecords > $total ) {
225 $cgi->param('offset', $offset + $maxrecords);
226 $pager .= '<A HREF="'. $cgi->self_url.
227 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
232 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
233 || ( $conf->exists('hidecancelledcustomers')
234 && ! $cgi->param('showcancelledcustomers')
237 $cgi->param('showcancelledcustomers', 1);
238 $cgi->param('offset', 0);
239 print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
241 $cgi->param('showcancelledcustomers', 0);
242 $cgi->param('offset', 0);
243 print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
245 if ( $cgi->param('referral_custnum') ) {
246 $cgi->param('referral_custnum') =~ /^(\d+)$/
247 or eidiot "Illegal referral_custnum\n";
248 my $referral_custnum = $1;
249 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
250 print '<FORM METHOD=POST>'.
251 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
252 'referrals of <A HREF="'. popurl(2).
253 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
254 ( $cust_main->company
255 || $cust_main->last. ', '. $cust_main->first ).
259 function changed(what) {
264 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
265 my $max = 8; #config file
266 $cgi->param('referral_depth') =~ /^(\d*)$/
267 or eidiot "Illegal referral_depth";
268 my $referral_depth = $1;
270 foreach my $depth ( 1 .. $max ) {
272 ' SELECTED'x($depth == $referral_depth),
275 print "</SELECT> levels deep".
276 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
280 print "<BR><BR>". $pager. &table(). <<END;
287 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
289 <TH>(service) name</TH>
296 <TH COLSPAN=2>Services</TH>
303 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
305 my($custnum,$last,$first,$company)=(
307 $cust_main->getfield('last'),
308 $cust_main->getfield('first'),
313 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
314 foreach ( @{$all_pkgs{$custnum}} ) {
315 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
316 my @cust_svc = $_->cust_svc;
317 push @lol_cust_svc, \@cust_svc;
318 $rowspan += scalar(@cust_svc) || 1;
321 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
323 if ( defined $cgi->param('quickpay') && $cgi->param('quickpay') eq 'yes' ) {
324 $view = $p. 'edit/cust_pay.cgi?quickpay=yes;custnum='. $custnum;
326 $view = $p. 'view/cust_main.cgi?'. $custnum;
328 my $pcompany = $company
329 ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
330 : '<FONT SIZE=-1> </FONT>';
333 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
334 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
335 <TD ROWSPAN=$rowspan>$pcompany</TD>
337 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
338 my($ship_last,$ship_first,$ship_company)=(
339 $cust_main->ship_last || $cust_main->getfield('last'),
340 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
341 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
343 my $pship_company = $ship_company
344 ? qq!<A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A>!
345 : '<FONT SIZE=-1> </FONT>';
349 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
350 <TD ROWSPAN=$rowspan>$pship_company</A></TD>
355 foreach ( @{$all_pkgs{$custnum}} ) {
356 my $pkgnum = $_->pkgnum;
357 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
358 my $part_pkg = $_->part_pkg;
360 my $pkg = $part_pkg->pkg;
361 my $comment = $part_pkg->comment;
362 my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
363 my @cust_svc = @{shift @lol_cust_svc};
364 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
365 my $rowspan = scalar(@cust_svc) || 1;
367 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
369 foreach my $cust_svc ( @cust_svc ) {
370 my($label, $value, $svcdb) = $cust_svc->label;
371 my($svcnum) = $cust_svc->svcnum;
372 my($sview) = $p.'view';
373 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
374 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
377 #print qq!</TR><TR>\n!;
383 print "</TABLE>$pager</BODY></HTML>";
387 #undef $cache; #does this help?
392 $a->getfield('last') cmp $b->getfield('last');
396 return -1 if $a->company && ! $b->company;
397 return 1 if ! $a->company && $b->company;
398 $a->getfield('company') cmp $b->getfield('company');
402 $a->getfield('custnum') <=> $b->getfield('custnum');
407 my($card)=$cgi->param('card');
409 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
412 [ qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'}) ];
416 $cgi->param('referral_custnum') =~ /^(\d+)$/
417 or eidiot "Illegal referral_custnum";
418 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
419 or eidiot "Customer $1 not found";
421 if ( $cgi->param('referral_depth') ) {
422 $cgi->param('referral_depth') =~ /^(\d+)$/
423 or eidiot "Illegal referral_depth";
428 [ $cust_main->referral_cust_main($depth) ];
434 foreach ( $cgi->param('last_type') ) {
438 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
439 or eidiot "Illegal last name";
442 if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
443 push @cust_main, qsearch( 'cust_main',
444 { 'last' => { 'op' => 'ILIKE',
445 'value' => $last } } );
447 push @cust_main, qsearch( 'cust_main',
448 { 'ship_last' => { 'op' => 'ILIKE',
449 'value' => $last } } )
450 if defined dbdef->table('cust_main')->column('ship_last');
453 if ( $last_type{'Substring'} || $last_type{'All'} ) {
455 push @cust_main, qsearch( 'cust_main',
456 { 'last' => { 'op' => 'ILIKE',
457 'value' => "%$last%" } } );
459 push @cust_main, qsearch( 'cust_main',
460 { 'ship_last' => { 'op' => 'ILIKE',
461 'value' => "%$last%" } } )
462 if defined dbdef->table('cust_main')->column('ship_last');
466 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
468 &FS::cust_main::check_and_rebuild_fuzzyfiles;
469 my $all_last = &FS::cust_main::all_last;
472 if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
473 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
478 #if ($last_type{'Sound-alike'}) {
481 foreach ( keys %last ) {
482 push @cust_main, qsearch('cust_main',{'last'=>$_});
483 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
484 if defined dbdef->table('cust_main')->column('ship_last');
496 foreach ( $cgi->param('company_type') ) {
500 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
501 or eidiot "Illegal company";
504 if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
505 push @cust_main, qsearch( 'cust_main',
506 { 'company' => { 'op' => 'ILIKE',
507 'value' => $company } } );
509 push @cust_main, qsearch( 'cust_main',
510 { 'ship_company' => { 'op' => 'ILIKE',
511 'value' => $company } } )
512 if defined dbdef->table('cust_main')->column('ship_last');
515 if ( $company_type{'Substring'} || $company_type{'All'} ) {
517 push @cust_main, qsearch( 'cust_main',
518 { 'company' => { 'op' => 'ILIKE',
519 'value' => "%$company%" } } );
521 push @cust_main, qsearch( 'cust_main',
522 { 'ship_company' => { 'op' => 'ILIKE',
523 'value' => "%$company%" } })
524 if defined dbdef->table('cust_main')->column('ship_last');
528 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
530 &FS::cust_main::check_and_rebuild_fuzzyfiles;
531 my $all_company = &FS::cust_main::all_company;
534 if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
535 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
540 #if ($company_type{'Sound-alike'}) {
543 foreach ( keys %company ) {
544 push @cust_main, qsearch('cust_main',{'company'=>$_});
545 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
546 if defined dbdef->table('cust_main')->column('ship_last');