case-insensitive and substring searching
[freeside.git] / httemplate / search / cust_main.cgi
index 89e325d..f153f02 100755 (executable)
@@ -45,7 +45,7 @@ $limit .= "LIMIT $maxrecords" if $maxrecords;
 my $offset = $cgi->param('offset') || 0;
 $limit .= " OFFSET $offset" if $offset;
 
-my $total;
+my $total = 0;
 
 my(@cust_main, $sortby, $orderby);
 if ( $cgi->param('browse') ) {
@@ -134,6 +134,9 @@ if ( $cgi->param('browse') ) {
     if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
        || ( $conf->exists('hidecancelledcustomers')
              && ! $cgi->param('showcancelledcustomers') );
+
+  my %saw = ();
+  @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
 }
 
 my %all_pkgs;
@@ -152,8 +155,14 @@ if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
   }
   #exit;
 } elsif ( scalar(@cust_main) == 0 ) {
+%>
+<!-- mason kludge -->
+<%
   eidiot "No matching customers found!\n";
 } else { 
+%>
+<!-- mason kludge -->
+<%
 
   $total ||= scalar(@cust_main);
   print header("Customer Search Results",menubar(
@@ -389,24 +398,37 @@ sub lastsearch {
     or eidiot "Illegal last name";
   my($last)=$1;
 
-#  if ( $last_type{'Exact'}
-#       && ! $last_type{'Fuzzy'} 
-#     #  && ! $last_type{'Sound-alike'}
-#  ) {
+  if ( $last_type{'Exact'} || $last_type{'Fuzzy'} ) {
+    push @cust_main, qsearch( 'cust_main',
+                              { 'last' => { 'op'    => 'ILIKE',
+                                            'value' => $last    } } );
+
+    push @cust_main, qsearch( 'cust_main',
+                              { 'ship_last' => { 'op'    => 'ILIKE',
+                                                 'value' => $last    } } )
+      if defined dbdef->table('cust_main')->column('ship_last');
+  }
+
+  if ( $last_type{'Substring'} || $last_type{'All'} ) {
 
-    push @cust_main, qsearch('cust_main',{'last'=>$last});
+    push @cust_main, qsearch( 'cust_main',
+                              { 'last' => { 'op'    => 'ILIKE',
+                                            'value' => "%$last%" } } );
 
-    push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
+    push @cust_main, qsearch( 'cust_main',
+                              { 'ship_last' => { 'op'    => 'ILIKE',
+                                                 'value' => "%$last%" } } )
       if defined dbdef->table('cust_main')->column('ship_last');
 
-#  } else {
-  if ( $last_type{'Fuzzy'} ) {
+  }
+
+  if ( $last_type{'Fuzzy'} || $last_type{'All'} ) {
 
     &FS::cust_main::check_and_rebuild_fuzzyfiles;
     my $all_last = &FS::cust_main::all_last;
 
     my %last;
-    if ($last_type{'Fuzzy'}) { 
+    if ( $last_type{'Fuzzy'} || $last_type{'All'} ) { 
       foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
         $last{$_}++; 
       }
@@ -422,6 +444,7 @@ sub lastsearch {
     }
 
   }
+
   \@cust_main;
 }
 
@@ -437,24 +460,37 @@ sub companysearch {
     or eidiot "Illegal company";
   my($company)=$1;
 
-#  if ( $company_type{'Exact'}
-#       && ! $company_type{'Fuzzy'} 
-#     #  && ! $company_type{'Sound-alike'}
-#  ) {
+  if ( $company_type{'Exact'} || $company_type{'Fuzzy'} ) {
+    push @cust_main, qsearch( 'cust_main',
+                              { 'company' => { 'op'    => 'ILIKE',
+                                               'value' => $company } } );
 
-    push @cust_main, qsearch('cust_main',{'company'=>$company});
+    push @cust_main, qsearch( 'cust_main',
+                              { 'ship_company' => { 'op'    => 'ILIKE',
+                                                    'value' => $company } } )
+      if defined dbdef->table('cust_main')->column('ship_last');
+  }
 
-    push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
+  if ( $company_type{'Substring'} || $company_type{'All'} ) {
+
+    push @cust_main, qsearch( 'cust_main',
+                              { 'company' => { 'op'    => 'ILIKE',
+                                               'value' => "%$company%" } } );
+
+    push @cust_main, qsearch( 'cust_main',
+                              { 'ship_company' => { 'op'    => 'ILIKE',
+                                                    'value' => "%$company%" } })
       if defined dbdef->table('cust_main')->column('ship_last');
 
-#  } else {
-  if ( $company_type{'Fuzzy'} ) {
+  }
+
+  if ( $company_type{'Fuzzy'} || $company_type{'All'} ) {
 
     &FS::cust_main::check_and_rebuild_fuzzyfiles;
     my $all_company = &FS::cust_main::all_company;
 
     my %company;
-    if ($company_type{'Fuzzy'}) { 
+    if ( $company_type{'Fuzzy'} || $company_type{'All'} ) { 
       foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
         $company{$_}++;
       }