adjustment to display_custnum special format, #16815
[freeside.git] / FS / FS / cust_main / Search.pm
index 04e4d24..1e9eee7 100644 (file)
@@ -127,6 +127,12 @@ sub smart_search {
          || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
               && $search =~ /^\s*(\w\w?\d+)\s*$/
             )
+         || ( $conf->config('cust_main-custnum-display_special')
+           # it's not currently possible for special prefixes to contain
+           # digits, so just strip off any alphabetic prefix and match 
+           # the rest to custnum
+              && $search =~ /^\s*[[:alpha:]]*(\d+)\s*$/
+            )
          || ( $conf->exists('address1-search' )
               && $search =~ /^\s*(\d+\-?\w*)\s*$/ #i.e. 1234A or 9432-D
             )
@@ -143,25 +149,23 @@ sub smart_search {
       } );
     }
 
-    #if this becomes agent-virt need to get a list of all prefixes the current
-    #user can see (via their agents)
-    my $prefix = $conf->config('cust_main-custnum-display_prefix');
-    if ( $prefix && $prefix eq substr($num, 0, length($prefix)) ) {
-      push @cust_main, qsearch( {
-        'table'     => 'cust_main',
-        'hashref'   => { 'custnum' => 0 + substr($num, length($prefix)),
-                         %options,
+    # for all agents this user can see, if any of them have custnum prefixes 
+    # that match the search string, include customers that match the rest 
+    # of the custnum and belong to that agent
+    foreach my $agentnum ( $FS::CurrentUser::CurrentUser->agentnums ) {
+      my $p = $conf->config('cust_main-custnum-display_prefix', $agentnum);
+      next if !$p;
+      if ( $p eq substr($num, 0, length($p)) ) {
+        push @cust_main, qsearch( {
+          'table'   => 'cust_main',
+          'hashref' => { 'custnum' => 0 + substr($num, length($p)),
+                         'agentnum' => $agentnum,
+                          %options,
                        },
-        'extra_sql' => " AND $agentnums_sql", #agent virtualization
-      } );
+        } );
+      }
     }
 
-    push @cust_main, qsearch( {
-      'table'     => 'cust_main',
-      'hashref'   => { 'agent_custid' => $num, %options },
-      'extra_sql' => " AND $agentnums_sql", #agent virtualization
-    } );
-
     if ( $conf->exists('address1-search') ) {
       my $len = length($num);
       $num = lc($num);
@@ -447,6 +451,8 @@ HASHREF.  Valid parameters are
 
 =item address
 
+=item refnum
+
 =item cancelled_pkgs
 
 bool
@@ -553,6 +559,13 @@ sub search {
                  ')';
   }
 
+  ###
+  # refnum
+  ###
+  if ( $params->{'refnum'} =~ /^(\d+)$/ ) {
+    push @where, "refnum = $1";
+  }
+
   ##
   # parse cancelled package checkbox
   ##
@@ -736,7 +749,7 @@ sub search {
 
   my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
 
-  my $pkg_join = 'LEFT JOIN cust_pkg USING ( custnum ) ';
+  my $addl_from = '';
 
   my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
 
@@ -750,19 +763,23 @@ sub search {
 
   if ($params->{'flattened_pkgs'}) {
 
+    #my $pkg_join = '';
+
     if ($dbh->{Driver}->{Name} eq 'Pg') {
 
       push @select, "array_to_string(array(select pkg from cust_pkg left join part_pkg using ( pkgpart ) where cust_main.custnum = cust_pkg.custnum $pkgwhere),'|') as magic";
 
-    }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
+    } elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
       push @select, "GROUP_CONCAT(part_pkg.pkg SEPARATOR '|') as magic";
-      $pkg_join .= " LEFT JOIN part_pkg using ( pkgpart )";
-    }else{
+      $addl_from .= ' LEFT JOIN cust_pkg USING ( custnum ) '; #Pg too w/flatpkg?
+      $addl_from .= ' LEFT JOIN part_pkg USING ( pkgpart ) ';
+      #$pkg_join  .= ' LEFT JOIN part_pkg USING ( pkgpart ) ';
+    } else {
       warn "warning: unknown database type ". $dbh->{Driver}->{Name}. 
            "omitting packing information from report.";
     }
 
-    my $header_query = "SELECT COUNT(cust_pkg.custnum = cust_main.custnum) AS count FROM cust_main $pkg_join $extra_sql $pkgwhere group by cust_main.custnum order by count desc limit 1";
+    my $header_query = "SELECT COUNT(cust_pkg.custnum = cust_main.custnum) AS count FROM cust_main $addl_from $extra_sql $pkgwhere group by cust_main.custnum order by count desc limit 1";
 
     my $sth = dbh->prepare($header_query) or die dbh->errstr;
     $sth->execute() or die $sth->errstr;
@@ -797,6 +814,7 @@ sub search {
   my $sql_query = {
     'table'         => 'cust_main',
     'select'        => $select,
+    'addl_from'     => $addl_from,
     'hashref'       => {},
     'extra_sql'     => $extra_sql,
     'order_by'      => $orderby,