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 = @FS::cust_main::fuzzyfields;
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
67 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
71 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
72 my $search = delete $options{'search'};
73 ( my $alphanum_search = $search ) =~ s/\W//g;
75 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
77 #false laziness w/Record::ut_phone
78 my $phonen = "$1-$2-$3";
79 $phonen .= " x$4" if $4;
81 push @cust_main, qsearch( {
82 'table' => 'cust_main',
83 'hashref' => { %options },
84 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
86 join(' OR ', map "$_ = '$phonen'",
88 ship_daytime ship_night ship_fax )
91 " AND $agentnums_sql", #agent virtualization
94 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
95 #try looking for matches with extensions unless one was specified
97 push @cust_main, qsearch( {
98 'table' => 'cust_main',
99 'hashref' => { %options },
100 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
102 join(' OR ', map "$_ LIKE '$phonen\%'",
104 ship_daytime ship_night )
107 " AND $agentnums_sql", #agent virtualization
112 # custnum search (also try agent_custid), with some tweaking options if your
113 # legacy cust "numbers" have letters
117 if ( $search =~ /@/ ) {
121 'table' => 'cust_main_invoice',
122 'hashref' => { 'dest' => $search },
125 } elsif ( $search =~ /^\s*(\d+)\s*$/
126 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
127 && $search =~ /^\s*(\w\w?\d+)\s*$/
129 || ( $conf->exists('address1-search' )
130 && $search =~ /^\s*(\d+\-?\w*)\s*$/ #i.e. 1234A or 9432-D
137 if ( $num =~ /^(\d+)$/ && $num <= 2147483647 ) { #need a bigint custnum? wow
138 push @cust_main, qsearch( {
139 'table' => 'cust_main',
140 'hashref' => { 'custnum' => $num, %options },
141 'extra_sql' => " AND $agentnums_sql", #agent virtualization
145 push @cust_main, qsearch( {
146 'table' => 'cust_main',
147 'hashref' => { 'agent_custid' => $num, %options },
148 'extra_sql' => " AND $agentnums_sql", #agent virtualization
151 if ( $conf->exists('address1-search') ) {
152 my $len = length($num);
154 foreach my $prefix ( '', 'ship_' ) {
155 push @cust_main, qsearch( {
156 'table' => 'cust_main',
157 'hashref' => { %options, },
159 ( keys(%options) ? ' AND ' : ' WHERE ' ).
160 " LOWER(SUBSTRING(${prefix}address1 FROM 1 FOR $len)) = '$num' ".
161 " AND $agentnums_sql",
166 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
168 my($company, $last, $first) = ( $1, $2, $3 );
170 # "Company (Last, First)"
171 #this is probably something a browser remembered,
172 #so just do an exact search (but case-insensitive, so USPS standardization
173 #doesn't throw a wrench in the works)
175 foreach my $prefix ( '', 'ship_' ) {
176 push @cust_main, qsearch( {
177 'table' => 'cust_main',
178 'hashref' => { %options },
180 ( keys(%options) ? ' AND ' : ' WHERE ' ).
182 " LOWER(${prefix}first) = ". dbh->quote(lc($first)),
183 " LOWER(${prefix}last) = ". dbh->quote(lc($last)),
184 " LOWER(${prefix}company) = ". dbh->quote(lc($company)),
190 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
191 # try (ship_){last,company}
195 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
196 # # full strings the browser remembers won't work
197 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
199 use Lingua::EN::NameParse;
200 my $NameParse = new Lingua::EN::NameParse(
205 my($last, $first) = ( '', '' );
206 #maybe disable this too and just rely on NameParse?
207 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
209 ($last, $first) = ( $1, $2 );
211 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
212 } elsif ( ! $NameParse->parse($value) ) {
214 my %name = $NameParse->components;
215 $first = $name{'given_name_1'} || $name{'initials_1'}; #wtf NameParse, Ed?
216 $last = $name{'surname_1'};
220 if ( $first && $last ) {
222 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
225 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
227 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
228 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
231 push @cust_main, qsearch( {
232 'table' => 'cust_main',
233 'hashref' => \%options,
234 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
237 # or it just be something that was typed in... (try that in a sec)
241 my $q_value = dbh->quote($value);
244 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
245 $sql .= " ( LOWER(last) = $q_value
246 OR LOWER(company) = $q_value
247 OR LOWER(ship_last) = $q_value
248 OR LOWER(ship_company) = $q_value
250 $sql .= " OR LOWER(address1) = $q_value
251 OR LOWER(ship_address1) = $q_value
253 if $conf->exists('address1-search');
256 push @cust_main, qsearch( {
257 'table' => 'cust_main',
258 'hashref' => \%options,
259 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
262 #no exact match, trying substring/fuzzy
263 #always do substring & fuzzy (unless they're explicity config'ed off)
264 #getting complaints searches are not returning enough
265 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
267 #still some false laziness w/search (was search/cust_main.cgi)
272 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
273 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
276 if ( $first && $last ) {
279 { 'first' => { op=>'ILIKE', value=>"%$first%" },
280 'last' => { op=>'ILIKE', value=>"%$last%" },
282 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
283 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
290 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
291 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
295 if ( $conf->exists('address1-search') ) {
297 { 'address1' => { op=>'ILIKE', value=>"%$value%" }, },
298 { 'ship_address1' => { op=>'ILIKE', value=>"%$value%" }, },
302 foreach my $hashref ( @hashrefs ) {
304 push @cust_main, qsearch( {
305 'table' => 'cust_main',
306 'hashref' => { %$hashref,
309 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
318 " AND $agentnums_sql", #extra_sql #agent virtualization
321 if ( $first && $last ) {
322 push @cust_main, FS::cust_main::Search->fuzzy_search(
323 { 'last' => $last, #fuzzy hashref
324 'first' => $first }, #
328 foreach my $field ( 'last', 'company' ) {
330 FS::cust_main::Search->fuzzy_search( { $field => $value }, @fuzopts );
332 if ( $conf->exists('address1-search') ) {
334 FS::cust_main::Search->fuzzy_search( { 'address1' => $value }, @fuzopts );
341 #eliminate duplicates
343 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
351 Accepts the following options: I<email>, the email address to search for. The
352 email address will be searched for as an email invoice destination and as an
355 #Any additional options are treated as an additional qualifier on the search
358 Returns a (possibly empty) array of FS::cust_main objects (but usually just
368 my $email = delete $options{'email'};
370 #we're only being used by RT at the moment... no agent virtualization yet
371 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
375 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
377 my ( $user, $domain ) = ( $1, $2 );
379 warn "$me smart_search: searching for $user in domain $domain"
385 'table' => 'cust_main_invoice',
386 'hashref' => { 'dest' => $email },
393 map $_->cust_svc->cust_pkg,
395 'table' => 'svc_acct',
396 'hashref' => { 'username' => $user, },
398 'AND ( SELECT domain FROM svc_domain
399 WHERE svc_acct.domsvc = svc_domain.svcnum
400 ) = '. dbh->quote($domain),
406 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
408 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
425 Returns a qsearch hash expression to search for parameters specified in
426 HASHREF. Valid parameters are
442 listref of start date, end date
452 =item current_balance
454 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
467 my ($class, $params) = @_;
478 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
480 "cust_main.agentnum = $1";
484 # do the same for user
487 if ( $params->{'usernum'} =~ /^(\d+)$/ and $1 ) {
489 "cust_main.usernum = $1";
496 #prospect ordered active inactive suspended cancelled
497 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
498 my $method = $params->{'status'}. '_sql';
499 #push @where, $class->$method();
500 push @where, FS::cust_main->$method();
506 if ( $params->{'address'} =~ /\S/ ) {
507 my $address = dbh->quote('%'. lc($params->{'address'}). '%');
508 push @where, '('. join(' OR ',
509 map "LOWER($_) LIKE $address",
510 qw(address1 address2 ship_address1 ship_address2)
516 # parse cancelled package checkbox
521 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
522 unless $params->{'cancelled_pkgs'};
525 # parse without census tract checkbox
528 push @where, "(censustract = '' or censustract is null)"
529 if $params->{'no_censustract'};
532 # parse with hardcoded tax location checkbox
535 push @where, "geocode is not null"
536 if $params->{'with_geocode'};
542 foreach my $field (qw( signupdate )) {
544 next unless exists($params->{$field});
546 my($beginning, $ending, $hour) = @{$params->{$field}};
549 "cust_main.$field IS NOT NULL",
550 "cust_main.$field >= $beginning",
551 "cust_main.$field <= $ending";
554 if ($dbh->{Driver}->{Name} =~ /Pg/i) {
555 push @where, "extract(hour from to_timestamp(cust_main.$field)) = $hour";
557 elsif( $dbh->{Driver}->{Name} =~ /mysql/i) {
558 push @where, "hour(from_unixtime(cust_main.$field)) = $hour"
561 warn "search by time of day not supported on ".$dbh->{Driver}->{Name}." databases";
565 $orderby ||= "ORDER BY cust_main.$field";
573 if ( $params->{'classnum'} ) {
575 my @classnum = ref( $params->{'classnum'} )
576 ? @{ $params->{'classnum'} }
577 : ( $params->{'classnum'} );
579 @classnum = grep /^(\d*)$/, @classnum;
582 push @where, '( '. join(' OR ', map {
583 $_ ? "cust_main.classnum = $_"
584 : "cust_main.classnum IS NULL"
597 if ( $params->{'payby'} ) {
599 my @payby = ref( $params->{'payby'} )
600 ? @{ $params->{'payby'} }
601 : ( $params->{'payby'} );
603 @payby = grep /^([A-Z]{4})$/, @payby;
605 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )'
611 # paydate_year / paydate_month
614 if ( $params->{'paydate_year'} =~ /^(\d{4})$/ ) {
616 $params->{'paydate_month'} =~ /^(\d\d?)$/
617 or die "paydate_year without paydate_month?";
621 'paydate IS NOT NULL',
623 "CAST(paydate AS timestamp) < CAST('$year-$month-01' AS timestamp )"
631 if ( $params->{'invoice_terms'} =~ /^([\w ]+)$/ ) {
633 if ( $1 eq 'NULL' ) {
635 "( cust_main.invoice_terms IS NULL OR cust_main.invoice_terms = '' )";
638 "cust_main.invoice_terms IS NOT NULL",
639 "cust_main.invoice_terms = '$1'";
647 if ( $params->{'current_balance'} ) {
649 #my $balance_sql = $class->balance_sql();
650 my $balance_sql = FS::cust_main->balance_sql();
652 my @current_balance =
653 ref( $params->{'current_balance'} )
654 ? @{ $params->{'current_balance'} }
655 : ( $params->{'current_balance'} );
657 push @where, map { s/current_balance/$balance_sql/; $_ }
666 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
668 "cust_main.custbatch = '$1'";
671 if ( $params->{'tagnum'} ) {
672 my @tagnums = ref( $params->{'tagnum'} ) ? @{ $params->{'tagnum'} } : ( $params->{'tagnum'} );
674 @tagnums = grep /^(\d+)$/, @tagnums;
677 my $tags_where = "0 < (select count(1) from cust_tag where "
678 . " cust_tag.custnum = cust_main.custnum and tagnum in ("
679 . join(',', @tagnums) . "))";
681 push @where, $tags_where;
687 # setup queries, subs, etc. for the search
690 $orderby ||= 'ORDER BY custnum';
692 # here is the agent virtualization
693 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
695 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
697 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
699 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
703 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
706 my(@extra_headers) = ();
707 my(@extra_fields) = ();
709 if ($params->{'flattened_pkgs'}) {
711 if ($dbh->{Driver}->{Name} eq 'Pg') {
713 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";
715 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
716 push @select, "GROUP_CONCAT(pkg SEPARATOR '|') as magic";
717 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
719 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
720 "omitting packing information from report.";
723 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";
725 my $sth = dbh->prepare($header_query) or die dbh->errstr;
726 $sth->execute() or die $sth->errstr;
727 my $headerrow = $sth->fetchrow_arrayref;
728 my $headercount = $headerrow ? $headerrow->[0] : 0;
729 while($headercount) {
730 unshift @extra_headers, "Package ". $headercount;
731 unshift @extra_fields, eval q!sub {my $c = shift;
732 my @a = split '\|', $c->magic;
733 my $p = $a[!.--$headercount. q!];
740 if ( $params->{'with_geocode'} ) {
742 unshift @extra_headers, 'Tax location override', 'Calculated tax location';
743 unshift @extra_fields, sub { my $c = shift; $c->get('geocode'); },
745 $c->set('geocode', '');
746 $c->geocode('cch'); #XXX only cch right now
748 push @select, 'geocode';
749 push @select, 'zip' unless grep { $_ eq 'zip' } @select;
750 push @select, 'ship_zip' unless grep { $_ eq 'ship_zip' } @select;
753 my $select = join(', ', @select);
756 'table' => 'cust_main',
759 'extra_sql' => $extra_sql,
760 'order_by' => $orderby,
761 'count_query' => $count_query,
762 'extra_headers' => \@extra_headers,
763 'extra_fields' => \@extra_fields,
768 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
770 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
771 records. Currently, I<first>, I<last>, I<company> and/or I<address1> may be
772 specified (the appropriate ship_ field is also searched).
774 Additional options are the same as FS::Record::qsearch
779 my( $self, $fuzzy, $hash, @opt) = @_;
784 check_and_rebuild_fuzzyfiles();
785 foreach my $field ( keys %$fuzzy ) {
787 my $all = $self->all_X($field);
788 next unless scalar(@$all);
791 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
794 foreach ( keys %match ) {
795 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
796 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
799 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
802 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
804 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
812 =head1 UTILITY SUBROUTINES
816 =item check_and_rebuild_fuzzyfiles
820 sub check_and_rebuild_fuzzyfiles {
821 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
822 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
825 =item rebuild_fuzzyfiles
829 sub rebuild_fuzzyfiles {
831 use Fcntl qw(:flock);
833 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
834 mkdir $dir, 0700 unless -d $dir;
836 foreach my $fuzzy ( @fuzzyfields ) {
838 open(LOCK,">>$dir/cust_main.$fuzzy")
839 or die "can't open $dir/cust_main.$fuzzy: $!";
841 or die "can't lock $dir/cust_main.$fuzzy: $!";
843 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
844 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
846 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
847 my $sth = dbh->prepare("SELECT $field FROM cust_main".
848 " WHERE $field != '' AND $field IS NOT NULL");
849 $sth->execute or die $sth->errstr;
851 while ( my $row = $sth->fetchrow_arrayref ) {
852 print CACHE $row->[0]. "\n";
857 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
859 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
870 my( $self, $field ) = @_;
871 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
872 open(CACHE,"<$dir/cust_main.$field")
873 or die "can't open $dir/cust_main.$field: $!";
874 my @array = map { chomp; $_; } <CACHE>;
885 L<FS::cust_main>, L<FS::Record>