X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FSearch.pm;h=7d0e43d646c18e3188c1414cfbe55f786fdb8dd6;hb=c742a74ec444c4606006a14e814d06e0b1795ea4;hp=04d97bda915a8414d87e08dac4ea1e99691eeac5;hpb=c6067e5ce005117ee3239eb00de0adc58efa4ee5;p=freeside.git

diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm
index 04d97bda9..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',
@@ -880,10 +884,24 @@ sub search {
   ##
   # with referrals
   ##
-  if ( $params->{'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,
-      ' EXISTS ( SELECT 1 FROM cust_main AS referred_cust_main
-                   WHERE cust_main.custnum = referred_cust_main.referral_custnum )';
+      " $n <= ( SELECT COUNT(*) FROM cust_main AS referred_cust_main
+                  WHERE cust_main.custnum = referred_cust_main.referral_custnum
+                    $and_status
+              )";
+
   }
 
   ##