-my $conf = new FS::Conf;
-my $maxrecords = $conf->config('maxsearchrecordsperpage');
-
-#my $cache;
-
-#my $monsterjoin = <<END;
-#cust_main left outer join (
-# ( cust_pkg left outer join part_pkg using(pkgpart)
-# ) left outer join (
-# (
-# (
-# ( cust_svc left outer join part_svc using (svcpart)
-# ) left outer join svc_acct using (svcnum)
-# ) left outer join svc_domain using(svcnum)
-# ) left outer join svc_forward using(svcnum)
-# ) using (pkgnum)
-#) using (custnum)
-#END
-
-#my $monsterjoin = <<END;
-#cust_main left outer join (
-# ( cust_pkg left outer join part_pkg using(pkgpart)
-# ) left outer join (
-# (
-# (
-# ( cust_svc left outer join part_svc using (svcpart)
-# ) left outer join (
-# svc_acct left outer join (
-# select svcnum, domain, catchall from svc_domain
-# ) as svc_acct_domsvc (
-# svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
-# ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
-# ) using (svcnum)
-# ) left outer join svc_domain using(svcnum)
-# ) left outer join svc_forward using(svcnum)
-# ) using (pkgnum)
-#) using (custnum)
-#END
-
-my $limit = '';
-$limit .= "LIMIT $maxrecords" if $maxrecords;
-
-my $offset = $cgi->param('offset') || 0;
-$limit .= " OFFSET $offset" if $offset;
-
-my $total = 0;
-
-my(@cust_main, $sortby, $orderby);
-if ( $cgi->param('browse')
- || $cgi->param('otaker_on')
-) {
-
- my %search = ();
- if ( $cgi->param('browse') ) {
- my $query = $cgi->param('browse');
- if ( $query eq 'custnum' ) {
- $sortby=\*custnum_sort;
- $orderby = "ORDER BY custnum";
- } elsif ( $query eq 'last' ) {
- $sortby=\*last_sort;
- $orderby = "ORDER BY LOWER(last || ' ' || first)";
- } elsif ( $query eq 'company' ) {
- $sortby=\*company_sort;
- $orderby = "ORDER BY LOWER(company || ' ' || last || ' ' || first )";
- } else {
- die "unknown browse field $query";
- }
- } else {
- $sortby = \*last_sort; #??
- $orderby = "ORDER BY LOWER(last || ' ' || first)"; #??
- if ( $cgi->param('otaker_on') ) {
- $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
- $search{otaker} = $1;
- } else {
- die "unknown query...";
- }
- }
-
- my $ncancelled = '';
-
- if ( driver_name eq 'mysql' ) {
-
- my $sql = "CREATE TEMPORARY TABLE temp1_$$ TYPE=MYISAM
- SELECT cust_pkg.custnum,COUNT(*) as count
- FROM cust_pkg,cust_main
- WHERE cust_pkg.custnum = cust_main.custnum
- AND ( cust_pkg.cancel IS NULL
- OR cust_pkg.cancel = 0 )
- GROUP BY cust_pkg.custnum";
- my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
- $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
- $sql = "CREATE TEMPORARY TABLE temp2_$$ TYPE=MYISAM
- SELECT cust_pkg.custnum,COUNT(*) as count
- FROM cust_pkg,cust_main
- WHERE cust_pkg.custnum = cust_main.custnum
- GROUP BY cust_pkg.custnum";
- $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
- $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
- }
-
- if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
- || ( $conf->exists('hidecancelledcustomers')
- && ! $cgi->param('showcancelledcustomers') )
- ) {
- #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
- if ( driver_name eq 'mysql' ) {
- $ncancelled = "
- temp1_$$.custnum = cust_main.custnum
- AND temp2_$$.custnum = cust_main.custnum
- AND (temp1_$$.count > 0
- OR temp2_$$.count = 0 )
- ";
- } else {
- $ncancelled = "
- 0 < ( SELECT COUNT(*) FROM cust_pkg
- WHERE cust_pkg.custnum = cust_main.custnum
- AND ( cust_pkg.cancel IS NULL
- OR cust_pkg.cancel = 0
- )
- )
- OR 0 = ( SELECT COUNT(*) FROM cust_pkg
- WHERE cust_pkg.custnum = cust_main.custnum
- )
- ";
- }
-
- }
-
- #EWWWWWW
- my $qual = join(' AND ',
- map { "$_ = ". dbh->quote($search{$_}) } keys %search );
-
- if ( $ncancelled ) {
- $qual .= ' AND ' if $qual;
- $qual .= $ncancelled;
- }
-
- $qual = " WHERE $qual" if $qual;
- my $statement;
- if ( driver_name eq 'mysql' ) {
- $statement = "SELECT COUNT(*) FROM cust_main";
- $statement .= ", temp1_$$, temp2_$$ $qual" if $qual;
- } else {
- $statement = "SELECT COUNT(*) FROM cust_main $qual";
- }
- my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
- $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
-
- $total = $sth->fetchrow_arrayref->[0];
-
- if ( $ncancelled ) {
- if ( %search ) {
- $ncancelled = " AND $ncancelled";
- } else {
- $ncancelled = " WHERE $ncancelled";
- }
- }
-
- my @just_cust_main;
- if ( driver_name eq 'mysql' ) {
- @just_cust_main = qsearch('cust_main', \%search, 'cust_main.*',
- ",temp1_$$,temp2_$$ $ncancelled $orderby $limit");
- } else {
- @just_cust_main = qsearch('cust_main', \%search, '',
- "$ncancelled $orderby $limit" );
- }
- if ( driver_name eq 'mysql' ) {
- my $sql = "DROP TABLE temp1_$$,temp2_$$;";
- my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
- $sth->execute or die "Error executing \"$sql\": ". $sth->errstr;
- }
- @cust_main = @just_cust_main;
-
-# foreach my $cust_main ( @just_cust_main ) {
-#
-# my @one_cust_main;
-# $FS::Record::DEBUG=1;
-# ( $cache, @one_cust_main ) = jsearch(
-# "$monsterjoin",
-# { 'custnum' => $cust_main->custnum },
-# '',
-# '',
-# 'cust_main',
-# 'custnum',
-# );
-# push @cust_main, @one_cust_main;
-# }
-
-} else {
- @cust_main=();
- $sortby = \*last_sort;
-
- push @cust_main, @{&custnumsearch}
- if $cgi->param('custnum_on') && $cgi->param('custnum_text');
- push @cust_main, @{&cardsearch}
- if $cgi->param('card_on') && $cgi->param('card');
- push @cust_main, @{&lastsearch}
- if $cgi->param('last_on') && $cgi->param('last_text');
- push @cust_main, @{&companysearch}
- if $cgi->param('company_on') && $cgi->param('company_text');
- push @cust_main, @{&address2search}
- if $cgi->param('address2_on') && $cgi->param('address2_text');
- push @cust_main, @{&phonesearch}
- if $cgi->param('phone_on') && $cgi->param('phone_text');
- push @cust_main, @{&referralsearch}
- if $cgi->param('referral_custnum');
-
- if ( $cgi->param('company_on') && $cgi->param('company_text') ) {
- $sortby = \*company_sort;
- push @cust_main, @{&companysearch};
- }
-
- @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
- if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
- || ( $conf->exists('hidecancelledcustomers')
- && ! $cgi->param('showcancelledcustomers') );
-
- my %saw = ();
- @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
-}
-
-my %all_pkgs;
-if ( $conf->exists('hidecancelledpackages' ) ) {
- %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
-} else {
- %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
-}
-#%all_pkgs = ();
-
-if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
- if ( $cgi->param('quickpay') eq 'yes' ) {
- print $cgi->redirect(popurl(2). "edit/cust_pay.cgi?quickpay=yes;custnum=". $cust_main[0]->custnum);
- } else {
- print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
- }
- #exit;
-} elsif ( scalar(@cust_main) == 0 ) {
-%>