add customer search by billing email, RT10399
[freeside.git] / FS / FS / cust_main / Search.pm
index a87928f..06a4522 100644 (file)
@@ -112,8 +112,17 @@ sub smart_search {
   # custnum search (also try agent_custid), with some tweaking options if your
   # legacy cust "numbers" have letters
   } 
-
-  if ( $search =~ /^\s*(\d+)\s*$/
+  
+  
+  if ( $search =~ /@/ ) {
+      push @cust_main,
+         map $_->cust_main,
+             qsearch( {
+                        'table'     => 'cust_main_invoice',
+                        'hashref'   => { 'dest' => $search },
+                      }
+                    );
+  } elsif ( $search =~ /^\s*(\d+)\s*$/
          || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
               && $search =~ /^\s*(\w\w?\d+)\s*$/
             )
@@ -520,6 +529,13 @@ sub search {
     if $params->{'no_censustract'};
 
   ##
+  # parse with hardcoded tax location checkbox
+  ##
+
+  push @where, "geocode is not null"
+    if $params->{'with_geocode'};
+
+  ##
   # dates
   ##
 
@@ -534,11 +550,13 @@ sub search {
       "cust_main.$field >= $beginning",
       "cust_main.$field <= $ending";
 
-    # XXX: do this for mysql and/or pull it out of here
     if(defined $hour) {
-      if ($dbh->{Driver}->{Name} eq 'Pg') {
+      if ($dbh->{Driver}->{Name} =~ /Pg/i) {
         push @where, "extract(hour from to_timestamp(cust_main.$field)) = $hour";
       }
+      elsif( $dbh->{Driver}->{Name} =~ /mysql/i) {
+        push @where, "hour(from_unixtime(cust_main.$field)) = $hour"
+      }
       else {
         warn "search by time of day not supported on ".$dbh->{Driver}->{Name}." databases";
       }
@@ -649,6 +667,21 @@ sub search {
     push @where,
       "cust_main.custbatch = '$1'";
   }
+  
+  if ( $params->{'tagnum'} ) {
+    my @tagnums = ref( $params->{'tagnum'} ) ? @{ $params->{'tagnum'} } : ( $params->{'tagnum'} );
+
+    @tagnums = grep /^(\d+)$/, @tagnums;
+
+    if ( @tagnums ) {
+       my $tags_where = "0 < (select count(1) from cust_tag where " 
+               . " cust_tag.custnum = cust_main.custnum and tagnum in ("
+               . join(',', @tagnums) . "))";
+
+       push @where, $tags_where;
+    }
+  }
+
 
   ##
   # setup queries, subs, etc. for the search
@@ -665,7 +698,7 @@ sub search {
 
   my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
 
-  my $select = join(', ', 
+  my @select = (
                  'cust_main.custnum',
                  FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
                );
@@ -677,10 +710,10 @@ sub search {
 
     if ($dbh->{Driver}->{Name} eq 'Pg') {
 
-      $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";
+      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) {
-      $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
+      push @select, "GROUP_CONCAT(pkg SEPARATOR '|') as magic";
       $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
     }else{
       warn "warning: unknown database type ". $dbh->{Driver}->{Name}. 
@@ -704,6 +737,21 @@ sub search {
 
   }
 
+  if ( $params->{'with_geocode'} ) {
+
+    unshift @extra_headers, 'Tax location override', 'Calculated tax location';
+    unshift @extra_fields, sub { my $c = shift; $c->get('geocode'); },
+                           sub { my $c = shift;
+                                 $c->set('geocode', '');
+                                 $c->geocode('cch'); #XXX only cch right now
+                               };
+    push @select, 'geocode';
+    push @select, 'zip' unless grep { $_ eq 'zip' } @select;
+    push @select, 'ship_zip' unless grep { $_ eq 'ship_zip' } @select;
+  }
+
+  my $select = join(', ', @select);
+
   my $sql_query = {
     'table'         => 'cust_main',
     'select'        => $select,