add city search, RT#31659
[freeside.git] / FS / FS / cust_main / Search.pm
index b52b359..f0a7d41 100644 (file)
@@ -477,11 +477,12 @@ sub smart_search {
 
     push @cust_main, qsearch({
       'table'     => 'cust_main',
+      'addl_from' => ' JOIN cust_payby USING (custnum)',
       'hashref'   => {},
-      'extra_sql' => " WHERE (    payinfo LIKE '$like_search'
-                               OR paymask =    '$mask_search'
+      'extra_sql' => " WHERE (    cust_payby.payinfo LIKE '$like_search'
+                               OR cust_payby.paymask =    '$mask_search'
                              ) ".
-                     " AND payby IN ('CARD','DCRD') ".
+                     " AND cust_payby.payby IN ('CARD','DCRD') ".
                      " AND $agentnums_sql", #agent virtulization
     });
 
@@ -727,6 +728,42 @@ sub search {
   }
 
   ##
+  # city
+  ##
+  if ( $params->{'city'} =~ /\S/ ) {
+    my $city = dbh->quote($params->{'city'});
+    push @where, "EXISTS(
+      SELECT 1 FROM cust_location
+      WHERE cust_location.custnum = cust_main.custnum
+        AND cust_location.city = $city
+    )";
+  }
+
+  ##
+  # county
+  ##
+  if ( $params->{'county'} =~ /\S/ ) {
+    my $county = dbh->quote($params->{'county'});
+    push @where, "EXISTS(
+      SELECT 1 FROM cust_location
+      WHERE cust_location.custnum = cust_main.custnum
+        AND cust_location.county = $county
+    )";
+  }
+
+  ##
+  # state
+  ##
+  if ( $params->{'state'} =~ /\S/ ) {
+    my $state = dbh->quote($params->{'state'});
+    push @where, "EXISTS(
+      SELECT 1 FROM cust_location
+      WHERE cust_location.custnum = cust_main.custnum
+        AND cust_location.state = $state
+    )";
+  }
+
+  ##
   # zipcode
   ##
   if ( $params->{'zip'} =~ /\S/ ) {
@@ -738,6 +775,18 @@ sub search {
     )";
   }
 
+  ##
+  # country
+  ##
+  if ( $params->{'country'} =~ /^(\w\w)$/ ) {
+    my $country = uc($1);
+    push @where, "EXISTS(
+      SELECT 1 FROM cust_location
+      WHERE cust_location.custnum = cust_main.custnum
+        AND cust_location.country = '$country'
+    )";
+  }
+
   ###
   # refnum
   ###
@@ -795,6 +844,18 @@ sub search {
     if $params->{'no_POST'};
 
   ##
+  # "tax exempt" checkbox
+  ##
+  push @where, "cust_main.tax = 'Y'"
+    if $params->{'tax'};
+
+  ##
+  # "not tax exempt" checkbox
+  ##
+  push @where, "(cust_main.tax = '' OR cust_main.tax IS NULL )"
+    if $params->{'no_tax'};
+
+  ##
   # dates
   ##
 
@@ -860,10 +921,10 @@ sub search {
                   :  ( $params->{'payby'} );
 
     @payby = grep /^([A-Z]{4})$/, @payby;
-
-    push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )'
+    my $in_payby = 'IN(' . join(',', map {"'$_'"} @payby) . ')';
+    push @where, "EXISTS( SELECT 1 FROM cust_payby WHERE payby $in_payby ".
+                 "AND cust_payby.custnum = cust_main.custnum)"
       if @payby;
-
   }
 
   ###
@@ -950,6 +1011,8 @@ sub search {
   }
 
   # pkg_classnum
+  #   all_pkg_classnums
+  #   any_pkg_status
   if ( $params->{'pkg_classnum'} ) {
     my @pkg_classnums = ref( $params->{'pkg_classnum'} ) ?
                           @{ $params->{'pkg_classnum'} } :
@@ -966,11 +1029,13 @@ sub search {
           'part_pkg.classnum IN('. join(',', @pkg_classnums).')';
       }
       foreach (@pkg_where) {
-        push @where, "EXISTS(".
+        my $select_pkg = 
           "SELECT 1 FROM cust_pkg JOIN part_pkg USING (pkgpart) WHERE ".
-          "cust_pkg.custnum = cust_main.custnum AND ".
-          $_ . ' AND ' . FS::cust_pkg->active_sql .
-        ')';
+          "cust_pkg.custnum = cust_main.custnum AND $_ ";
+        if ( not $params->{'any_pkg_status'} ) {
+          $select_pkg .= 'AND '.FS::cust_pkg->active_sql;
+        }
+        push @where, "EXISTS($select_pkg)";
       }
     }
   }
@@ -999,6 +1064,7 @@ sub search {
 
   my @select = (
                  'cust_main.custnum',
+                 'cust_main.salesnum',
                  # there's a good chance that we'll need these
                  'cust_main.bill_locationnum',
                  'cust_main.ship_locationnum',
@@ -1247,7 +1313,7 @@ sub append_fuzzyfiles_fuzzyfield {
   my ($field, $table) = reverse split('\.', $fuzzyfield);
   $table ||= 'cust_main';
 
-  return unless length($value);
+  return unless defined($value) && length($value);
 
   open(CACHE, '>>:encoding(UTF-8)', "$dir/$table.$field" )
     or die "can't open $dir/$table.$field: $!";