X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FSearch.pm;h=7d0e43d646c18e3188c1414cfbe55f786fdb8dd6;hb=c742a74ec444c4606006a14e814d06e0b1795ea4;hp=5c21a8e12df42c412ca45d9d82a333f7b5de3fc6;hpb=e0a759bb54e95c8b3502b33c3ac691e58de0641b;p=freeside.git diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm index 5c21a8e12..7d0e43d64 100644 --- a/FS/FS/cust_main/Search.pm +++ b/FS/FS/cust_main/Search.pm @@ -356,10 +356,14 @@ sub smart_search { #still some false laziness w/search (was search/cust_main.cgi) + my $min_len = + $FS::CurrentUser::CurrentUser->access_right('List all customers') + ? 3 : 4; + #substring my @company_hashrefs = (); - if ( length($value) >= 4 ) { + if ( length($value) >= $min_len ) { @company_hashrefs = ( { 'company' => { op=>'ILIKE', value=>"%$value%" }, }, { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, }, @@ -375,7 +379,7 @@ sub smart_search { }, ); - } elsif ( length($value) >= 4 ) { + } elsif ( length($value) >= $min_len ) { @hashrefs = ( { 'first' => { op=>'ILIKE', value=>"%$value%" }, }, @@ -396,7 +400,7 @@ sub smart_search { } - if ( $conf->exists('address1-search') && length($value) >= 4 ) { + if ( $conf->exists('address1-search') && length($value) >= $min_len ) { push @cust_main, qsearch( { table => 'cust_main', @@ -878,6 +882,29 @@ sub search { if $params->{'no_tax'}; ## + # with referrals + ## + if ( $params->{with_referrals} =~ /^\s*(\d+)\s*$/ ) { + + my $n = $1; + + # referral status + my $and_status = ''; + if ( grep { $params->{referral_status} eq $_ } FS::cust_main->statuses() ) { + my $method = $params->{referral_status}. '_sql'; + $and_status = ' AND '. FS::cust_main->$method(); + $and_status =~ s/ cust_main\./ referred_cust_main./g; + } + + push @where, + " $n <= ( SELECT COUNT(*) FROM cust_main AS referred_cust_main + WHERE cust_main.custnum = referred_cust_main.referral_custnum + $and_status + )"; + + } + + ## # dates ## @@ -1147,6 +1174,20 @@ sub search { } + if ( $params->{'with_referrals'} ) { + + #XXX next: num for each customer status + + push @select, + '( SELECT COUNT(*) FROM cust_main AS referred_cust_main + WHERE cust_main.custnum = referred_cust_main.referral_custnum + ) AS num_referrals'; + + unshift @extra_headers, 'Referrals'; + unshift @extra_fields, 'num_referrals'; + + } + my $select = join(', ', @select); my $sql_query = {