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
116 if ( $search =~ /^\s*(\d+)\s*$/
117 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
118 && $search =~ /^\s*(\w\w?\d+)\s*$/
120 || ( $conf->exists('address1-search' )
121 && $search =~ /^\s*(\d+\-?\w*)\s*$/ #i.e. 1234A or 9432-D
128 if ( $num =~ /^(\d+)$/ && $num <= 2147483647 ) { #need a bigint custnum? wow
129 push @cust_main, qsearch( {
130 'table' => 'cust_main',
131 'hashref' => { 'custnum' => $num, %options },
132 'extra_sql' => " AND $agentnums_sql", #agent virtualization
136 push @cust_main, qsearch( {
137 'table' => 'cust_main',
138 'hashref' => { 'agent_custid' => $num, %options },
139 'extra_sql' => " AND $agentnums_sql", #agent virtualization
142 if ( $conf->exists('address1-search') ) {
143 my $len = length($num);
145 foreach my $prefix ( '', 'ship_' ) {
146 push @cust_main, qsearch( {
147 'table' => 'cust_main',
148 'hashref' => { %options, },
150 ( keys(%options) ? ' AND ' : ' WHERE ' ).
151 " LOWER(SUBSTRING(${prefix}address1 FROM 1 FOR $len)) = '$num' ".
152 " AND $agentnums_sql",
157 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
159 my($company, $last, $first) = ( $1, $2, $3 );
161 # "Company (Last, First)"
162 #this is probably something a browser remembered,
163 #so just do an exact search (but case-insensitive, so USPS standardization
164 #doesn't throw a wrench in the works)
166 foreach my $prefix ( '', 'ship_' ) {
167 push @cust_main, qsearch( {
168 'table' => 'cust_main',
169 'hashref' => { %options },
171 ( keys(%options) ? ' AND ' : ' WHERE ' ).
173 " LOWER(${prefix}first) = ". dbh->quote(lc($first)),
174 " LOWER(${prefix}last) = ". dbh->quote(lc($last)),
175 " LOWER(${prefix}company) = ". dbh->quote(lc($company)),
181 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
182 # try (ship_){last,company}
186 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
187 # # full strings the browser remembers won't work
188 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
190 use Lingua::EN::NameParse;
191 my $NameParse = new Lingua::EN::NameParse(
196 my($last, $first) = ( '', '' );
197 #maybe disable this too and just rely on NameParse?
198 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
200 ($last, $first) = ( $1, $2 );
202 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
203 } elsif ( ! $NameParse->parse($value) ) {
205 my %name = $NameParse->components;
206 $first = $name{'given_name_1'} || $name{'initials_1'}; #wtf NameParse, Ed?
207 $last = $name{'surname_1'};
211 if ( $first && $last ) {
213 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
216 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
218 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
219 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
222 push @cust_main, qsearch( {
223 'table' => 'cust_main',
224 'hashref' => \%options,
225 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
228 # or it just be something that was typed in... (try that in a sec)
232 my $q_value = dbh->quote($value);
235 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
236 $sql .= " ( LOWER(last) = $q_value
237 OR LOWER(company) = $q_value
238 OR LOWER(ship_last) = $q_value
239 OR LOWER(ship_company) = $q_value
241 $sql .= " OR LOWER(address1) = $q_value
242 OR LOWER(ship_address1) = $q_value
244 if $conf->exists('address1-search');
247 push @cust_main, qsearch( {
248 'table' => 'cust_main',
249 'hashref' => \%options,
250 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
253 #no exact match, trying substring/fuzzy
254 #always do substring & fuzzy (unless they're explicity config'ed off)
255 #getting complaints searches are not returning enough
256 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
258 #still some false laziness w/search (was search/cust_main.cgi)
263 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
264 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
267 if ( $first && $last ) {
270 { 'first' => { op=>'ILIKE', value=>"%$first%" },
271 'last' => { op=>'ILIKE', value=>"%$last%" },
273 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
274 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
281 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
282 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
286 if ( $conf->exists('address1-search') ) {
288 { 'address1' => { op=>'ILIKE', value=>"%$value%" }, },
289 { 'ship_address1' => { op=>'ILIKE', value=>"%$value%" }, },
293 foreach my $hashref ( @hashrefs ) {
295 push @cust_main, qsearch( {
296 'table' => 'cust_main',
297 'hashref' => { %$hashref,
300 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
309 " AND $agentnums_sql", #extra_sql #agent virtualization
312 if ( $first && $last ) {
313 push @cust_main, FS::cust_main::Search->fuzzy_search(
314 { 'last' => $last, #fuzzy hashref
315 'first' => $first }, #
319 foreach my $field ( 'last', 'company' ) {
321 FS::cust_main::Search->fuzzy_search( { $field => $value }, @fuzopts );
323 if ( $conf->exists('address1-search') ) {
325 FS::cust_main::Search->fuzzy_search( { 'address1' => $value }, @fuzopts );
332 #eliminate duplicates
334 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
342 Accepts the following options: I<email>, the email address to search for. The
343 email address will be searched for as an email invoice destination and as an
346 #Any additional options are treated as an additional qualifier on the search
349 Returns a (possibly empty) array of FS::cust_main objects (but usually just
359 my $email = delete $options{'email'};
361 #we're only being used by RT at the moment... no agent virtualization yet
362 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
366 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
368 my ( $user, $domain ) = ( $1, $2 );
370 warn "$me smart_search: searching for $user in domain $domain"
376 'table' => 'cust_main_invoice',
377 'hashref' => { 'dest' => $email },
384 map $_->cust_svc->cust_pkg,
386 'table' => 'svc_acct',
387 'hashref' => { 'username' => $user, },
389 'AND ( SELECT domain FROM svc_domain
390 WHERE svc_acct.domsvc = svc_domain.svcnum
391 ) = '. dbh->quote($domain),
397 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
399 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
416 Returns a qsearch hash expression to search for parameters specified in
417 HASHREF. Valid parameters are
433 listref of start date, end date
443 =item current_balance
445 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
458 my ($class, $params) = @_;
469 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
471 "cust_main.agentnum = $1";
475 # do the same for user
478 if ( $params->{'usernum'} =~ /^(\d+)$/ and $1 ) {
480 "cust_main.usernum = $1";
487 #prospect ordered active inactive suspended cancelled
488 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
489 my $method = $params->{'status'}. '_sql';
490 #push @where, $class->$method();
491 push @where, FS::cust_main->$method();
497 if ( $params->{'address'} =~ /\S/ ) {
498 my $address = dbh->quote('%'. lc($params->{'address'}). '%');
499 push @where, '('. join(' OR ',
500 map "LOWER($_) LIKE $address",
501 qw(address1 address2 ship_address1 ship_address2)
507 # parse cancelled package checkbox
512 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
513 unless $params->{'cancelled_pkgs'};
516 # parse without census tract checkbox
519 push @where, "(censustract = '' or censustract is null)"
520 if $params->{'no_censustract'};
523 # parse with hardcoded tax location checkbox
526 push @where, "geocode is not null"
527 if $params->{'with_geocode'};
533 foreach my $field (qw( signupdate )) {
535 next unless exists($params->{$field});
537 my($beginning, $ending, $hour) = @{$params->{$field}};
540 "cust_main.$field IS NOT NULL",
541 "cust_main.$field >= $beginning",
542 "cust_main.$field <= $ending";
545 if ($dbh->{Driver}->{Name} =~ /Pg/i) {
546 push @where, "extract(hour from to_timestamp(cust_main.$field)) = $hour";
548 elsif( $dbh->{Driver}->{Name} =~ /mysql/i) {
549 push @where, "hour(from_unixtime(cust_main.$field)) = $hour"
552 warn "search by time of day not supported on ".$dbh->{Driver}->{Name}." databases";
556 $orderby ||= "ORDER BY cust_main.$field";
564 if ( $params->{'classnum'} ) {
566 my @classnum = ref( $params->{'classnum'} )
567 ? @{ $params->{'classnum'} }
568 : ( $params->{'classnum'} );
570 @classnum = grep /^(\d*)$/, @classnum;
573 push @where, '( '. join(' OR ', map {
574 $_ ? "cust_main.classnum = $_"
575 : "cust_main.classnum IS NULL"
588 if ( $params->{'payby'} ) {
590 my @payby = ref( $params->{'payby'} )
591 ? @{ $params->{'payby'} }
592 : ( $params->{'payby'} );
594 @payby = grep /^([A-Z]{4})$/, @payby;
596 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )'
602 # paydate_year / paydate_month
605 if ( $params->{'paydate_year'} =~ /^(\d{4})$/ ) {
607 $params->{'paydate_month'} =~ /^(\d\d?)$/
608 or die "paydate_year without paydate_month?";
612 'paydate IS NOT NULL',
614 "CAST(paydate AS timestamp) < CAST('$year-$month-01' AS timestamp )"
622 if ( $params->{'invoice_terms'} =~ /^([\w ]+)$/ ) {
624 if ( $1 eq 'NULL' ) {
626 "( cust_main.invoice_terms IS NULL OR cust_main.invoice_terms = '' )";
629 "cust_main.invoice_terms IS NOT NULL",
630 "cust_main.invoice_terms = '$1'";
638 if ( $params->{'current_balance'} ) {
640 #my $balance_sql = $class->balance_sql();
641 my $balance_sql = FS::cust_main->balance_sql();
643 my @current_balance =
644 ref( $params->{'current_balance'} )
645 ? @{ $params->{'current_balance'} }
646 : ( $params->{'current_balance'} );
648 push @where, map { s/current_balance/$balance_sql/; $_ }
657 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
659 "cust_main.custbatch = '$1'";
662 if ( $params->{'tagnum'} ) {
663 my @tagnums = ref( $params->{'tagnum'} ) ? @{ $params->{'tagnum'} } : ( $params->{'tagnum'} );
665 @tagnums = grep /^(\d+)$/, @tagnums;
668 my $tags_where = "0 < (select count(1) from cust_tag where "
669 . " cust_tag.custnum = cust_main.custnum and tagnum in ("
670 . join(',', @tagnums) . "))";
672 push @where, $tags_where;
678 # setup queries, subs, etc. for the search
681 $orderby ||= 'ORDER BY custnum';
683 # here is the agent virtualization
684 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
686 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
688 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
690 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
694 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
697 my(@extra_headers) = ();
698 my(@extra_fields) = ();
700 if ($params->{'flattened_pkgs'}) {
702 if ($dbh->{Driver}->{Name} eq 'Pg') {
704 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";
706 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
707 push @select, "GROUP_CONCAT(pkg SEPARATOR '|') as magic";
708 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
710 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
711 "omitting packing information from report.";
714 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";
716 my $sth = dbh->prepare($header_query) or die dbh->errstr;
717 $sth->execute() or die $sth->errstr;
718 my $headerrow = $sth->fetchrow_arrayref;
719 my $headercount = $headerrow ? $headerrow->[0] : 0;
720 while($headercount) {
721 unshift @extra_headers, "Package ". $headercount;
722 unshift @extra_fields, eval q!sub {my $c = shift;
723 my @a = split '\|', $c->magic;
724 my $p = $a[!.--$headercount. q!];
731 if ( $params->{'with_geocode'} ) {
733 unshift @extra_headers, 'Tax location override', 'Calculated tax location';
734 unshift @extra_fields, sub { my $c = shift; $c->get('geocode'); },
736 $c->set('geocode', '');
737 $c->geocode('cch'); #XXX only cch right now
739 push @select, 'geocode';
740 push @select, 'zip' unless grep { $_ eq 'zip' } @select;
741 push @select, 'ship_zip' unless grep { $_ eq 'ship_zip' } @select;
744 my $select = join(', ', @select);
747 'table' => 'cust_main',
750 'extra_sql' => $extra_sql,
751 'order_by' => $orderby,
752 'count_query' => $count_query,
753 'extra_headers' => \@extra_headers,
754 'extra_fields' => \@extra_fields,
759 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
761 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
762 records. Currently, I<first>, I<last>, I<company> and/or I<address1> may be
763 specified (the appropriate ship_ field is also searched).
765 Additional options are the same as FS::Record::qsearch
770 my( $self, $fuzzy, $hash, @opt) = @_;
775 check_and_rebuild_fuzzyfiles();
776 foreach my $field ( keys %$fuzzy ) {
778 my $all = $self->all_X($field);
779 next unless scalar(@$all);
782 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
785 foreach ( keys %match ) {
786 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
787 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
790 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
793 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
795 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
803 =head1 UTILITY SUBROUTINES
807 =item check_and_rebuild_fuzzyfiles
811 sub check_and_rebuild_fuzzyfiles {
812 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
813 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
816 =item rebuild_fuzzyfiles
820 sub rebuild_fuzzyfiles {
822 use Fcntl qw(:flock);
824 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
825 mkdir $dir, 0700 unless -d $dir;
827 foreach my $fuzzy ( @fuzzyfields ) {
829 open(LOCK,">>$dir/cust_main.$fuzzy")
830 or die "can't open $dir/cust_main.$fuzzy: $!";
832 or die "can't lock $dir/cust_main.$fuzzy: $!";
834 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
835 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
837 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
838 my $sth = dbh->prepare("SELECT $field FROM cust_main".
839 " WHERE $field != '' AND $field IS NOT NULL");
840 $sth->execute or die $sth->errstr;
842 while ( my $row = $sth->fetchrow_arrayref ) {
843 print CACHE $row->[0]. "\n";
848 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
850 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
861 my( $self, $field ) = @_;
862 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
863 open(CACHE,"<$dir/cust_main.$field")
864 or die "can't open $dir/cust_main.$field: $!";
865 my @array = map { chomp; $_; } <CACHE>;
876 L<FS::cust_main>, L<FS::Record>