X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FSearch.pm;h=ef106b560bdd4e4cc6778e11f90d7cccb0cfa996;hb=2c04f1df05a1b14d9539bbae97275c1d3a0995e2;hp=362a6aa1c2bb09b1175050ef9494a9d764b6b5d9;hpb=b26d3c015c78d826fcd4ae9e7de8e761b379bcf4;p=freeside.git diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm index 362a6aa1c..ef106b560 100644 --- a/FS/FS/cust_main/Search.pm +++ b/FS/FS/cust_main/Search.pm @@ -21,6 +21,7 @@ $me = '[FS::cust_main::Search]'; @fuzzyfields = ( 'cust_main.first', 'cust_main.last', 'cust_main.company', + 'cust_main.ship_company', # if you're using it 'cust_location.address1', 'contact.first', 'contact.last', ); @@ -321,6 +322,7 @@ sub smart_search { $sql .= " ( LOWER(cust_main.first) = $q_value OR LOWER(cust_main.last) = $q_value OR LOWER(cust_main.company) = $q_value + OR LOWER(cust_main.ship_company) = $q_value "; #address1 (yes, it's a kludge) @@ -356,27 +358,33 @@ sub smart_search { #substring - my @hashrefs = ( - { 'company' => { op=>'ILIKE', value=>"%$value%" }, }, - ); + my @company_hashrefs = (); + if ( length($value) >= 3 ) { + @company_hashrefs = ( + { 'company' => { op=>'ILIKE', value=>"%$value%" }, }, + { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, }, + ); + } + my @hashrefs = (); if ( $first && $last ) { - push @hashrefs, + @hashrefs = ( { 'first' => { op=>'ILIKE', value=>"%$first%" }, 'last' => { op=>'ILIKE', value=>"%$last%" }, }, - ; + ); - } else { + } elsif ( length($value) >= 3 ) { - push @hashrefs, + @hashrefs = ( { 'first' => { op=>'ILIKE', value=>"%$value%" }, }, { 'last' => { op=>'ILIKE', value=>"%$value%" }, }, - ; + ); + } - foreach my $hashref ( @hashrefs ) { + foreach my $hashref ( @company_hashrefs, @hashrefs ) { push @cust_main, qsearch( { 'table' => 'cust_main', @@ -388,7 +396,7 @@ sub smart_search { } - if ( $conf->exists('address1-search') ) { + if ( $conf->exists('address1-search') && length($value) >= 3 ) { push @cust_main, qsearch( { table => 'cust_main', @@ -402,8 +410,6 @@ sub smart_search { #contact substring - shift @hashrefs; #no company column in contact table - foreach my $hashref ( @hashrefs ) { push @cust_main, @@ -433,15 +439,30 @@ sub smart_search { 'first' => $first }, # %fuzopts ); + push @cust_main, FS::cust_main::Search->fuzzy_search( + { 'contact.last' => $last, #fuzzy hashref + 'contact.first' => $first }, # + %fuzopts + ); + } + foreach my $field ( 'first', 'last', 'company', 'ship_company' ) { + push @cust_main, FS::cust_main::Search->fuzzy_search( + { $field => $value }, + %fuzopts + ); } - foreach my $field ( 'first', 'last', 'company' ) { - push @cust_main, - FS::cust_main::Search->fuzzy_search( { $field => $value }, %fuzopts ); + foreach my $field ( 'first', 'last' ) { + push @cust_main, FS::cust_main::Search->fuzzy_search( + { "contact.$field" => $value }, + %fuzopts + ); } if ( $conf->exists('address1-search') ) { push @cust_main, FS::cust_main::Search->fuzzy_search( - { 'cust_location.address1' => $value }, %fuzopts ); + { 'cust_location.address1' => $value }, + %fuzopts + ); } } @@ -495,11 +516,9 @@ none or one). sub email_search { my %options = @_; - local($DEBUG) = 1; - my $email = delete $options{'email'}; - #we're only being used by RT at the moment... no agent virtualization yet + #no agent virtualization yet #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql; my @cust_main = (); @@ -685,6 +704,21 @@ sub search { push @where, FS::cust_main->$method(); } + my $current = ''; + unless ( $params->{location_history} ) { + $current = ' + AND ( cust_location.locationnum IN ( cust_main.bill_locationnum, + cust_main.ship_locationnum + ) + OR cust_location.locationnum IN ( + SELECT locationnum FROM cust_pkg + WHERE cust_pkg.custnum = cust_main.custnum + AND locationnum IS NOT NULL + AND '. FS::cust_pkg->ncancelled_recurring_sql.' + ) + )'; + } + ## # address ## @@ -704,11 +738,51 @@ sub search { SELECT 1 FROM cust_location WHERE cust_location.custnum = cust_main.custnum AND (".join(' OR ',@orwhere).") + $current )"; } } ## + # 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 + $current + )"; + } + + ## + # 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 + $current + )"; + } + + ## + # 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 + $current + )"; + } + + ## # zipcode ## if ( $params->{'zip'} =~ /\S/ ) { @@ -717,6 +791,20 @@ sub search { SELECT 1 FROM cust_location WHERE cust_location.custnum = cust_main.custnum AND cust_location.zip LIKE $zip + $current + )"; + } + + ## + # 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' + $current )"; } @@ -777,6 +865,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 ## @@ -931,6 +1031,35 @@ sub search { } } + # pkg_classnum + # all_pkg_classnums + # any_pkg_status + if ( $params->{'pkg_classnum'} ) { + my @pkg_classnums = ref( $params->{'pkg_classnum'} ) ? + @{ $params->{'pkg_classnum'} } : + $params->{'pkg_classnum'}; + @pkg_classnums = grep /^(\d+)$/, @pkg_classnums; + + if ( @pkg_classnums ) { + + my @pkg_where; + if ( $params->{'all_pkg_classnums'} ) { + push @pkg_where, "part_pkg.classnum = $_" foreach @pkg_classnums; + } else { + push @pkg_where, + 'part_pkg.classnum IN('. join(',', @pkg_classnums).')'; + } + foreach (@pkg_where) { + my $select_pkg = + "SELECT 1 FROM cust_pkg JOIN part_pkg USING (pkgpart) WHERE ". + "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)"; + } + } + } ## # setup queries, subs, etc. for the search @@ -952,10 +1081,15 @@ sub search { 'ON (cust_main.'.$pre.'locationnum = '.$pre.'location.locationnum) '; } + # always make referral available in results + # (maybe we should be using FS::UI::Web::join_cust_main instead?) + $addl_from .= ' LEFT JOIN (select refnum, referral from part_referral) AS part_referral_x ON (cust_main.refnum = part_referral_x.refnum) '; + my $count_query = "SELECT COUNT(*) FROM cust_main $addl_from $extra_sql"; 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', @@ -1178,6 +1312,7 @@ sub append_fuzzyfiles { #foreach my $fuzzy (@fuzzyfields) { foreach my $fuzzy ( 'cust_main.first', 'cust_main.last', 'cust_main.company', 'cust_location.address1', + 'cust_main.ship_company', ) { append_fuzzyfiles_fuzzyfield($fuzzy, shift);