proper cdr_batch table, RT#6386
[freeside.git] / FS / FS / cust_main.pm
index 858ac14..3c4eb44 100644 (file)
@@ -8405,6 +8405,22 @@ sub search_sql {
 ;
   }
 
+  ###
+  # invoice terms
+  ###
+
+  if ( $params->{'invoice_terms'} =~ /^([\w ]+)$/ ) {
+    my $terms = $1;
+    if ( $1 eq 'NULL' ) {
+      push @where,
+        "( cust_main.invoice_terms IS NULL OR cust_main.invoice_terms = '' )";
+    } else {
+      push @where,
+        "cust_main.invoice_terms IS NOT NULL",
+        "cust_main.invoice_terms = '$1'";
+    }
+  }
+
   ##
   # amounts
   ##
@@ -8412,8 +8428,10 @@ sub search_sql {
   #my $balance_sql = $class->balance_sql();
   my $balance_sql = FS::cust_main->balance_sql();
 
+  my @current_balance = @{ $params->{'current_balance'} };
+
   push @where, map { s/current_balance/$balance_sql/; $_ }
-                   @{ $params->{'current_balance'} };
+                   @current_balance;
 
   ##
   # custbatch
@@ -8792,17 +8810,21 @@ sub smart_search {
 
     # "Company (Last, First)"
     #this is probably something a browser remembered,
-    #so just do an exact (but case-insensitive) search
+    #so just do an exact search (but case-insensitive, so USPS standardization
+    #doesn't throw a wrench in the works)
 
     foreach my $prefix ( '', 'ship_' ) {
       push @cust_main, qsearch( {
         'table'     => 'cust_main',
-        'hashref'   => { $prefix.'first'   => $first,
-                         $prefix.'last'    => $last,
-                         $prefix.'company' => $company,
-                         %options,
-                       },
-        'extra_sql' => " AND $agentnums_sql",
+        'hashref'   => { %options },
+        'extra_sql' => 
+          ( keys(%options) ? ' AND ' : ' WHERE ' ).
+          join(' AND ',
+            " LOWER(${prefix}first)   = ". dbh->quote(lc($first)),
+            " LOWER(${prefix}last)    = ". dbh->quote(lc($last)),
+            " LOWER(${prefix}company) = ". dbh->quote(lc($company)),
+            $agentnums_sql,
+          ),
       } );
     }