search by state (and county, country), RT#30312
[freeside.git] / FS / FS / cust_main / Search.pm
index b52b359..02f3b64 100644 (file)
@@ -727,6 +727,30 @@ sub search {
   }
 
   ##
+  # 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 +762,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
   ###
@@ -950,6 +986,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 +1004,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 +1039,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',