1 package FS::cust_main::Search;
4 use base qw( Exporter );
5 use vars qw( @EXPORT_OK $DEBUG $me $conf @fuzzyfields );
6 use String::Approx qw(amatch);
8 use FS::Record qw( qsearch );
10 use FS::cust_main_invoice;
13 @EXPORT_OK = qw( smart_search );
15 # 1 is mostly method/subroutine entry and options
16 # 2 traces progress of some operations
17 # 3 is even more information including possibly sensitive data
19 $me = '[FS::cust_main::Search]';
21 @fuzzyfields = ( 'first', 'last', 'company', 'address1' );
23 install_callback FS::UID sub {
25 #yes, need it for stuff below (prolly should be cached)
30 FS::cust_main::Search - Customer searching
34 use FS::cust_main::Search;
36 FS::cust_main::Search::smart_search(%options);
38 FS::cust_main::Search::email_search(%options);
40 FS::cust_main::Search->search( \%options );
42 FS::cust_main::Search->fuzzy_search( \%fuzzy_hashref );
48 =item smart_search OPTION => VALUE ...
50 Accepts the following options: I<search>, the string to search for. The string
51 will be searched for as a customer number, phone number, name or company name,
52 as an exact, or, in some cases, a substring or fuzzy match (see the source code
53 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
54 skip fuzzy matching when an exact match is found.
56 Any additional options are treated as an additional qualifier on the search
59 Returns a (possibly empty) array of FS::cust_main objects.
66 #here is the agent virtualization
68 $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main');
72 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
73 my $search = delete $options{'search'};
74 ( my $alphanum_search = $search ) =~ s/\W//g;
76 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
78 #false laziness w/Record::ut_phone
79 my $phonen = "$1-$2-$3";
80 $phonen .= " x$4" if $4;
82 push @cust_main, qsearch( {
83 'table' => 'cust_main',
84 'hashref' => { %options },
85 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
87 join(' OR ', map "$_ = '$phonen'",
89 ship_daytime ship_night ship_fax )
92 " AND $agentnums_sql", #agent virtualization
95 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
96 #try looking for matches with extensions unless one was specified
98 push @cust_main, qsearch( {
99 'table' => 'cust_main',
100 'hashref' => { %options },
101 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
103 join(' OR ', map "$_ LIKE '$phonen\%'",
105 ship_daytime ship_night )
108 " AND $agentnums_sql", #agent virtualization
113 # custnum search (also try agent_custid), with some tweaking options if your
114 # legacy cust "numbers" have letters
118 if ( $search =~ /@/ ) {
122 'table' => 'cust_main_invoice',
123 'hashref' => { 'dest' => $search },
126 } elsif ( $search =~ /^\s*(\d+)\s*$/
127 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
128 && $search =~ /^\s*(\w\w?\d+)\s*$/
130 || ( $conf->config('cust_main-custnum-display_special')
131 # it's not currently possible for special prefixes to contain
132 # digits, so just strip off any alphabetic prefix and match
133 # the rest to custnum
134 && $search =~ /^\s*[[:alpha:]]*(\d+)\s*$/
136 || ( $conf->exists('address1-search' )
137 && $search =~ /^\s*(\d+\-?\w*)\s*$/ #i.e. 1234A or 9432-D
144 if ( $num =~ /^(\d+)$/ && $num <= 2147483647 ) { #need a bigint custnum? wow
145 push @cust_main, qsearch( {
146 'table' => 'cust_main',
147 'hashref' => { 'custnum' => $num, %options },
148 'extra_sql' => " AND $agentnums_sql", #agent virtualization
152 # for all agents this user can see, if any of them have custnum prefixes
153 # that match the search string, include customers that match the rest
154 # of the custnum and belong to that agent
155 foreach my $agentnum ( $FS::CurrentUser::CurrentUser->agentnums ) {
156 my $p = $conf->config('cust_main-custnum-display_prefix', $agentnum);
158 if ( $p eq substr($num, 0, length($p)) ) {
159 push @cust_main, qsearch( {
160 'table' => 'cust_main',
161 'hashref' => { 'custnum' => 0 + substr($num, length($p)),
162 'agentnum' => $agentnum,
169 push @cust_main, qsearch( {
170 'table' => 'cust_main',
171 'hashref' => { 'agent_custid' => $num, %options },
172 'extra_sql' => " AND $agentnums_sql", #agent virtualization
175 if ( $conf->exists('address1-search') ) {
176 my $len = length($num);
178 foreach my $prefix ( '', 'ship_' ) {
179 push @cust_main, qsearch( {
180 'table' => 'cust_main',
181 'hashref' => { %options, },
183 ( keys(%options) ? ' AND ' : ' WHERE ' ).
184 " LOWER(SUBSTRING(${prefix}address1 FROM 1 FOR $len)) = '$num' ".
185 " AND $agentnums_sql",
190 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
192 my($company, $last, $first) = ( $1, $2, $3 );
194 # "Company (Last, First)"
195 #this is probably something a browser remembered,
196 #so just do an exact search (but case-insensitive, so USPS standardization
197 #doesn't throw a wrench in the works)
199 foreach my $prefix ( '', 'ship_' ) {
200 push @cust_main, qsearch( {
201 'table' => 'cust_main',
202 'hashref' => { %options },
204 ( keys(%options) ? ' AND ' : ' WHERE ' ).
206 " LOWER(${prefix}first) = ". dbh->quote(lc($first)),
207 " LOWER(${prefix}last) = ". dbh->quote(lc($last)),
208 " LOWER(${prefix}company) = ". dbh->quote(lc($company)),
214 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
215 # try (ship_){last,company}
219 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
220 # # full strings the browser remembers won't work
221 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
223 use Lingua::EN::NameParse;
224 my $NameParse = new Lingua::EN::NameParse(
229 my($last, $first) = ( '', '' );
230 #maybe disable this too and just rely on NameParse?
231 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
233 ($last, $first) = ( $1, $2 );
235 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
236 } elsif ( ! $NameParse->parse($value) ) {
238 my %name = $NameParse->components;
239 $first = $name{'given_name_1'} || $name{'initials_1'}; #wtf NameParse, Ed?
240 $last = $name{'surname_1'};
244 if ( $first && $last ) {
246 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
249 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
251 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
252 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
255 push @cust_main, qsearch( {
256 'table' => 'cust_main',
257 'hashref' => \%options,
258 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
261 # or it just be something that was typed in... (try that in a sec)
265 my $q_value = dbh->quote($value);
268 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
269 $sql .= " ( LOWER(last) = $q_value
270 OR LOWER(company) = $q_value
271 OR LOWER(ship_last) = $q_value
272 OR LOWER(ship_company) = $q_value
274 $sql .= " OR LOWER(address1) = $q_value
275 OR LOWER(ship_address1) = $q_value
277 if $conf->exists('address1-search');
280 push @cust_main, qsearch( {
281 'table' => 'cust_main',
282 'hashref' => \%options,
283 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
286 #no exact match, trying substring/fuzzy
287 #always do substring & fuzzy (unless they're explicity config'ed off)
288 #getting complaints searches are not returning enough
289 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
291 #still some false laziness w/search (was search/cust_main.cgi)
296 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
297 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
300 if ( $first && $last ) {
303 { 'first' => { op=>'ILIKE', value=>"%$first%" },
304 'last' => { op=>'ILIKE', value=>"%$last%" },
306 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
307 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
314 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
315 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
319 if ( $conf->exists('address1-search') ) {
321 { 'address1' => { op=>'ILIKE', value=>"%$value%" }, },
322 { 'ship_address1' => { op=>'ILIKE', value=>"%$value%" }, },
326 foreach my $hashref ( @hashrefs ) {
328 push @cust_main, qsearch( {
329 'table' => 'cust_main',
330 'hashref' => { %$hashref,
333 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
342 " AND $agentnums_sql", #extra_sql #agent virtualization
345 if ( $first && $last ) {
346 push @cust_main, FS::cust_main::Search->fuzzy_search(
347 { 'last' => $last, #fuzzy hashref
348 'first' => $first }, #
352 foreach my $field ( 'last', 'company' ) {
354 FS::cust_main::Search->fuzzy_search( { $field => $value }, @fuzopts );
356 if ( $conf->exists('address1-search') ) {
358 FS::cust_main::Search->fuzzy_search( { 'address1' => $value }, @fuzopts );
365 #eliminate duplicates
367 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
375 Accepts the following options: I<email>, the email address to search for. The
376 email address will be searched for as an email invoice destination and as an
379 #Any additional options are treated as an additional qualifier on the search
382 Returns a (possibly empty) array of FS::cust_main objects (but usually just
392 my $email = delete $options{'email'};
394 #we're only being used by RT at the moment... no agent virtualization yet
395 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
399 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
401 my ( $user, $domain ) = ( $1, $2 );
403 warn "$me smart_search: searching for $user in domain $domain"
409 'table' => 'cust_main_invoice',
410 'hashref' => { 'dest' => $email },
417 map $_->cust_svc->cust_pkg,
419 'table' => 'svc_acct',
420 'hashref' => { 'username' => $user, },
422 'AND ( SELECT domain FROM svc_domain
423 WHERE svc_acct.domsvc = svc_domain.svcnum
424 ) = '. dbh->quote($domain),
430 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
432 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
449 Returns a qsearch hash expression to search for parameters specified in
450 HASHREF. Valid parameters are
468 listref of start date, end date
472 listref of start date, end date
474 =item spouse_birthdate
476 listref of start date, end date
486 =item current_balance
488 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
501 my ($class, $params) = @_;
508 # initialize these to prevent warnings
515 'paydate_year' => '',
516 'invoice_terms' => '',
522 # explicit custnum(s)
525 if ( $params->{'custnum'} ) {
526 my @custnums = ref($params->{'custnum'}) ?
527 @{ $params->{'custnum'} } :
528 $params->{'custnum'};
530 'cust_main.custnum IN (' .
531 join(',', map { $_ =~ /^(\d+)$/ ? $1 : () } @custnums ) .
532 ')' if scalar(@custnums) > 0;
539 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
541 "cust_main.agentnum = $1";
545 # do the same for user
548 if ( $params->{'usernum'} =~ /^(\d+)$/ and $1 ) {
550 "cust_main.usernum = $1";
557 #prospect ordered active inactive suspended cancelled
558 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
559 my $method = $params->{'status'}. '_sql';
560 #push @where, $class->$method();
561 push @where, FS::cust_main->$method();
567 if ( $params->{'address'} =~ /\S/ ) {
568 my $address = dbh->quote('%'. lc($params->{'address'}). '%');
569 push @where, '('. join(' OR ',
570 map "LOWER($_) LIKE $address",
571 qw(address1 address2 ship_address1 ship_address2)
579 if ( $params->{'refnum'} =~ /^(\d+)$/ ) {
580 push @where, "refnum = $1";
584 # parse cancelled package checkbox
589 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
590 unless $params->{'cancelled_pkgs'};
593 # parse without census tract checkbox
596 push @where, "(censustract = '' or censustract is null)"
597 if $params->{'no_censustract'};
600 # parse with hardcoded tax location checkbox
603 push @where, "geocode is not null"
604 if $params->{'with_geocode'};
610 foreach my $field (qw( signupdate birthdate spouse_birthdate )) {
612 next unless exists($params->{$field});
614 my($beginning, $ending, $hour) = @{$params->{$field}};
617 "cust_main.$field IS NOT NULL",
618 "cust_main.$field >= $beginning",
619 "cust_main.$field <= $ending";
621 if($field eq 'signupdate' && defined $hour) {
622 if ($dbh->{Driver}->{Name} =~ /Pg/i) {
623 push @where, "extract(hour from to_timestamp(cust_main.$field)) = $hour";
625 elsif( $dbh->{Driver}->{Name} =~ /mysql/i) {
626 push @where, "hour(from_unixtime(cust_main.$field)) = $hour"
629 warn "search by time of day not supported on ".$dbh->{Driver}->{Name}." databases";
633 $orderby ||= "ORDER BY cust_main.$field";
641 if ( $params->{'classnum'} ) {
643 my @classnum = ref( $params->{'classnum'} )
644 ? @{ $params->{'classnum'} }
645 : ( $params->{'classnum'} );
647 @classnum = grep /^(\d*)$/, @classnum;
650 push @where, '( '. join(' OR ', map {
651 $_ ? "cust_main.classnum = $_"
652 : "cust_main.classnum IS NULL"
665 if ( $params->{'payby'} ) {
667 my @payby = ref( $params->{'payby'} )
668 ? @{ $params->{'payby'} }
669 : ( $params->{'payby'} );
671 @payby = grep /^([A-Z]{4})$/, @payby;
673 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )'
679 # paydate_year / paydate_month
682 if ( $params->{'paydate_year'} =~ /^(\d{4})$/ ) {
684 $params->{'paydate_month'} =~ /^(\d\d?)$/
685 or die "paydate_year without paydate_month?";
689 'paydate IS NOT NULL',
691 "CAST(paydate AS timestamp) < CAST('$year-$month-01' AS timestamp )"
699 if ( $params->{'invoice_terms'} =~ /^([\w ]+)$/ ) {
701 if ( $1 eq 'NULL' ) {
703 "( cust_main.invoice_terms IS NULL OR cust_main.invoice_terms = '' )";
706 "cust_main.invoice_terms IS NOT NULL",
707 "cust_main.invoice_terms = '$1'";
715 if ( $params->{'current_balance'} ) {
717 #my $balance_sql = $class->balance_sql();
718 my $balance_sql = FS::cust_main->balance_sql();
720 my @current_balance =
721 ref( $params->{'current_balance'} )
722 ? @{ $params->{'current_balance'} }
723 : ( $params->{'current_balance'} );
725 push @where, map { s/current_balance/$balance_sql/; $_ }
734 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
736 "cust_main.custbatch = '$1'";
739 if ( $params->{'tagnum'} ) {
740 my @tagnums = ref( $params->{'tagnum'} ) ? @{ $params->{'tagnum'} } : ( $params->{'tagnum'} );
742 @tagnums = grep /^(\d+)$/, @tagnums;
745 my $tags_where = "0 < (select count(1) from cust_tag where "
746 . " cust_tag.custnum = cust_main.custnum and tagnum in ("
747 . join(',', @tagnums) . "))";
749 push @where, $tags_where;
755 # setup queries, subs, etc. for the search
758 $orderby ||= 'ORDER BY custnum';
760 # here is the agent virtualization
762 $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main');
764 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
768 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
772 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
775 my(@extra_headers) = ();
776 my(@extra_fields) = ();
778 if ($params->{'flattened_pkgs'}) {
781 $addl_from .= ' LEFT JOIN cust_pkg USING ( custnum ) ';
783 if ($dbh->{Driver}->{Name} eq 'Pg') {
785 push @select, "array_to_string(array(select pkg from cust_pkg left join part_pkg using ( pkgpart ) where cust_main.custnum = cust_pkg.custnum $pkgwhere),'|') as magic";
787 } elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
788 push @select, "GROUP_CONCAT(part_pkg.pkg SEPARATOR '|') as magic";
789 $addl_from .= ' LEFT JOIN part_pkg USING ( pkgpart ) ';
790 #$pkg_join .= ' LEFT JOIN part_pkg USING ( pkgpart ) ';
792 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
793 "omitting package information from report.";
796 my $header_query = "SELECT COUNT(cust_pkg.custnum = cust_main.custnum) AS count FROM cust_main $addl_from $extra_sql $pkgwhere group by cust_main.custnum order by count desc limit 1";
798 my $sth = dbh->prepare($header_query) or die dbh->errstr;
799 $sth->execute() or die $sth->errstr;
800 my $headerrow = $sth->fetchrow_arrayref;
801 my $headercount = $headerrow ? $headerrow->[0] : 0;
802 while($headercount) {
803 unshift @extra_headers, "Package ". $headercount;
804 unshift @extra_fields, eval q!sub {my $c = shift;
805 my @a = split '\|', $c->magic;
806 my $p = $a[!.--$headercount. q!];
813 if ( $params->{'with_geocode'} ) {
815 unshift @extra_headers, 'Tax location override', 'Calculated tax location';
816 unshift @extra_fields, sub { my $c = shift; $c->get('geocode'); },
818 $c->set('geocode', '');
819 $c->geocode('cch'); #XXX only cch right now
821 push @select, 'geocode';
822 push @select, 'zip' unless grep { $_ eq 'zip' } @select;
823 push @select, 'ship_zip' unless grep { $_ eq 'ship_zip' } @select;
826 my $select = join(', ', @select);
829 'table' => 'cust_main',
831 'addl_from' => $addl_from,
833 'extra_sql' => $extra_sql,
834 'order_by' => $orderby,
835 'count_query' => $count_query,
836 'extra_headers' => \@extra_headers,
837 'extra_fields' => \@extra_fields,
842 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
844 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
845 records. Currently, I<first>, I<last>, I<company> and/or I<address1> may be
846 specified (the appropriate ship_ field is also searched).
848 Additional options are the same as FS::Record::qsearch
853 my( $self, $fuzzy, $hash, @opt) = @_;
858 check_and_rebuild_fuzzyfiles();
859 foreach my $field ( keys %$fuzzy ) {
861 my $all = $self->all_X($field);
862 next unless scalar(@$all);
865 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
868 foreach ( keys %match ) {
869 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
870 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
873 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
876 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
878 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
886 =head1 UTILITY SUBROUTINES
890 =item check_and_rebuild_fuzzyfiles
894 sub check_and_rebuild_fuzzyfiles {
895 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
896 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields;
899 =item rebuild_fuzzyfiles
903 sub rebuild_fuzzyfiles {
905 use Fcntl qw(:flock);
907 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
908 mkdir $dir, 0700 unless -d $dir;
910 foreach my $fuzzy ( @fuzzyfields ) {
912 open(LOCK,">>$dir/cust_main.$fuzzy")
913 or die "can't open $dir/cust_main.$fuzzy: $!";
915 or die "can't lock $dir/cust_main.$fuzzy: $!";
917 open (CACHE, '>:encoding(UTF-8)', "$dir/cust_main.$fuzzy.tmp")
918 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
920 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
921 my $sth = dbh->prepare("SELECT $field FROM cust_main".
922 " WHERE $field != '' AND $field IS NOT NULL");
923 $sth->execute or die $sth->errstr;
925 while ( my $row = $sth->fetchrow_arrayref ) {
926 print CACHE $row->[0]. "\n";
931 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
933 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
939 =item append_fuzzyfiles FIRSTNAME LASTNAME COMPANY ADDRESS1
943 sub append_fuzzyfiles {
944 #my( $first, $last, $company ) = @_;
946 check_and_rebuild_fuzzyfiles();
948 use Fcntl qw(:flock);
950 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
952 foreach my $field (@fuzzyfields) {
957 open(CACHE, '>>:encoding(UTF-8)', "$dir/cust_main.$field" )
958 or die "can't open $dir/cust_main.$field: $!";
960 or die "can't lock $dir/cust_main.$field: $!";
962 print CACHE "$value\n";
965 or die "can't unlock $dir/cust_main.$field: $!";
979 my( $self, $field ) = @_;
980 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
981 open(CACHE, '<:encoding(UTF-8)', "$dir/cust_main.$field")
982 or die "can't open $dir/cust_main.$field: $!";
983 my @array = map { chomp; $_; } <CACHE>;
994 L<FS::cust_main>, L<FS::Record>