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;
12 use FS::payinfo_Mixin;
14 @EXPORT_OK = qw( smart_search );
16 # 1 is mostly method/subroutine entry and options
17 # 2 traces progress of some operations
18 # 3 is even more information including possibly sensitive data
20 $me = '[FS::cust_main::Search]';
22 @fuzzyfields = ( 'first', 'last', 'company', 'address1' );
24 install_callback FS::UID sub {
26 #yes, need it for stuff below (prolly should be cached)
31 FS::cust_main::Search - Customer searching
35 use FS::cust_main::Search;
37 FS::cust_main::Search::smart_search(%options);
39 FS::cust_main::Search::email_search(%options);
41 FS::cust_main::Search->search( \%options );
43 FS::cust_main::Search->fuzzy_search( \%fuzzy_hashref );
49 =item smart_search OPTION => VALUE ...
51 Accepts the following options: I<search>, the string to search for. The string
52 will be searched for as a customer number, phone number, name or company name,
53 address (if address1-search is on), invoicing email address, or credit card
56 Searches match as an exact, or, in some cases, a substring or fuzzy match (see
57 the source code for the exact heuristics used); I<no_fuzzy_on_exact>, causes
59 skip fuzzy matching when an exact match is found.
61 Any additional options are treated as an additional qualifier on the search
64 Returns a (possibly empty) array of FS::cust_main objects.
71 #here is the agent virtualization
73 $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main');
77 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
78 my $search = delete $options{'search'};
79 ( my $alphanum_search = $search ) =~ s/\W//g;
81 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
83 #false laziness w/Record::ut_phone
84 my $phonen = "$1-$2-$3";
85 $phonen .= " x$4" if $4;
87 push @cust_main, qsearch( {
88 'table' => 'cust_main',
89 'hashref' => { %options },
90 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
92 join(' OR ', map "$_ = '$phonen'",
93 qw( daytime night mobile fax
94 ship_daytime ship_night ship_mobile ship_fax )
97 " AND $agentnums_sql", #agent virtualization
100 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
101 #try looking for matches with extensions unless one was specified
103 push @cust_main, qsearch( {
104 'table' => 'cust_main',
105 'hashref' => { %options },
106 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
108 join(' OR ', map "$_ LIKE '$phonen\%'",
110 ship_daytime ship_night )
113 " AND $agentnums_sql", #agent virtualization
121 if ( $search =~ /@/ ) { #invoicing email address
125 'table' => 'cust_main_invoice',
126 'hashref' => { 'dest' => $search },
130 # custnum search (also try agent_custid), with some tweaking options if your
131 # legacy cust "numbers" have letters
132 } elsif ( $search =~ /^\s*(\d+)\s*$/
133 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
134 && $search =~ /^\s*(\w\w?\d+)\s*$/
136 || ( $conf->config('cust_main-custnum-display_special')
137 # it's not currently possible for special prefixes to contain
138 # digits, so just strip off any alphabetic prefix and match
139 # the rest to custnum
140 && $search =~ /^\s*[[:alpha:]]*(\d+)\s*$/
142 || ( $conf->exists('address1-search' )
143 && $search =~ /^\s*(\d+\-?\w*)\s*$/ #i.e. 1234A or 9432-D
150 if ( $num =~ /^(\d+)$/ && $num <= 2147483647 ) { #need a bigint custnum? wow
151 my $agent_custid_null = $conf->exists('cust_main-default_agent_custid')
152 ? ' AND agent_custid IS NULL ' : '';
153 push @cust_main, qsearch( {
154 'table' => 'cust_main',
155 'hashref' => { 'custnum' => $num, %options },
156 'extra_sql' => " AND $agentnums_sql $agent_custid_null",
160 # for all agents this user can see, if any of them have custnum prefixes
161 # that match the search string, include customers that match the rest
162 # of the custnum and belong to that agent
163 foreach my $agentnum ( $FS::CurrentUser::CurrentUser->agentnums ) {
164 my $p = $conf->config('cust_main-custnum-display_prefix', $agentnum);
166 if ( $p eq substr($num, 0, length($p)) ) {
167 push @cust_main, qsearch( {
168 'table' => 'cust_main',
169 'hashref' => { 'custnum' => 0 + substr($num, length($p)),
170 'agentnum' => $agentnum,
177 push @cust_main, qsearch( {
178 'table' => 'cust_main',
179 'hashref' => { 'agent_custid' => $num, %options },
180 'extra_sql' => " AND $agentnums_sql", #agent virtualization
183 if ( $conf->exists('address1-search') ) {
184 my $len = length($num);
186 foreach my $prefix ( '', 'ship_' ) {
187 push @cust_main, qsearch( {
188 'table' => 'cust_main',
189 'hashref' => { %options, },
191 ( keys(%options) ? ' AND ' : ' WHERE ' ).
192 " LOWER(SUBSTRING(${prefix}address1 FROM 1 FOR $len)) = '$num' ".
193 " AND $agentnums_sql",
198 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
200 my($company, $last, $first) = ( $1, $2, $3 );
202 # "Company (Last, First)"
203 #this is probably something a browser remembered,
204 #so just do an exact search (but case-insensitive, so USPS standardization
205 #doesn't throw a wrench in the works)
207 foreach my $prefix ( '', 'ship_' ) {
208 push @cust_main, qsearch( {
209 'table' => 'cust_main',
210 'hashref' => { %options },
212 ( keys(%options) ? ' AND ' : ' WHERE ' ).
214 " LOWER(${prefix}first) = ". dbh->quote(lc($first)),
215 " LOWER(${prefix}last) = ". dbh->quote(lc($last)),
216 " LOWER(${prefix}company) = ". dbh->quote(lc($company)),
222 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
223 # try (ship_){last,company}
227 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
228 # # full strings the browser remembers won't work
229 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
231 use Lingua::EN::NameParse;
232 my $NameParse = new Lingua::EN::NameParse(
237 my($last, $first) = ( '', '' );
238 #maybe disable this too and just rely on NameParse?
239 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
241 ($last, $first) = ( $1, $2 );
243 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
244 } elsif ( ! $NameParse->parse($value) ) {
246 my %name = $NameParse->components;
247 $first = $name{'given_name_1'} || $name{'initials_1'}; #wtf NameParse, Ed?
248 $last = $name{'surname_1'};
252 if ( $first && $last ) {
254 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
257 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
259 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
260 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
263 push @cust_main, qsearch( {
264 'table' => 'cust_main',
265 'hashref' => \%options,
266 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
269 # or it just be something that was typed in... (try that in a sec)
273 my $q_value = dbh->quote($value);
276 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
277 $sql .= " ( LOWER(last) = $q_value
278 OR LOWER(company) = $q_value
279 OR LOWER(ship_last) = $q_value
280 OR LOWER(ship_company) = $q_value
282 $sql .= " OR LOWER(address1) = $q_value
283 OR LOWER(ship_address1) = $q_value
285 if $conf->exists('address1-search');
288 push @cust_main, qsearch( {
289 'table' => 'cust_main',
290 'hashref' => \%options,
291 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
294 #no exact match, trying substring/fuzzy
295 #always do substring & fuzzy (unless they're explicity config'ed off)
296 #getting complaints searches are not returning enough
297 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
299 #still some false laziness w/search (was search/cust_main.cgi)
304 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
305 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
308 if ( $first && $last ) {
311 { 'first' => { op=>'ILIKE', value=>"%$first%" },
312 'last' => { op=>'ILIKE', value=>"%$last%" },
314 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
315 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
322 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
323 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
327 if ( $conf->exists('address1-search') ) {
329 { 'address1' => { op=>'ILIKE', value=>"%$value%" }, },
330 { 'ship_address1' => { op=>'ILIKE', value=>"%$value%" }, },
334 foreach my $hashref ( @hashrefs ) {
336 push @cust_main, qsearch( {
337 'table' => 'cust_main',
338 'hashref' => { %$hashref,
341 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
350 " AND $agentnums_sql", #extra_sql #agent virtualization
353 if ( $first && $last ) {
354 push @cust_main, FS::cust_main::Search->fuzzy_search(
355 { 'last' => $last, #fuzzy hashref
356 'first' => $first }, #
360 foreach my $field ( 'last', 'company' ) {
362 FS::cust_main::Search->fuzzy_search( { $field => $value }, @fuzopts );
364 if ( $conf->exists('address1-search') ) {
366 FS::cust_main::Search->fuzzy_search( { 'address1' => $value }, @fuzopts );
373 ( my $nospace_search = $search ) =~ s/\s//g;
374 ( my $card_search = $nospace_search ) =~ s/\-//g;
375 $card_search =~ s/[x\*\.\_]/x/gi;
377 if ( $card_search =~ /^[\dx]{15,16}$/i ) { #credit card search
379 ( my $like_search = $card_search ) =~ s/x/_/g;
380 my $mask_search = FS::payinfo_Mixin->mask_payinfo('CARD', $card_search);
382 push @cust_main, qsearch({
383 'table' => 'cust_main',
385 'extra_sql' => " WHERE ( payinfo LIKE '$like_search'
386 OR paymask = '$mask_search'
388 " AND payby IN ('CARD','DCRD') ".
389 " AND $agentnums_sql", #agent virtulization
395 #eliminate duplicates
397 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
405 Accepts the following options: I<email>, the email address to search for. The
406 email address will be searched for as an email invoice destination and as an
409 #Any additional options are treated as an additional qualifier on the search
412 Returns a (possibly empty) array of FS::cust_main objects (but usually just
422 my $email = delete $options{'email'};
424 #we're only being used by RT at the moment... no agent virtualization yet
425 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
429 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
431 my ( $user, $domain ) = ( $1, $2 );
433 warn "$me smart_search: searching for $user in domain $domain"
439 'table' => 'cust_main_invoice',
440 'hashref' => { 'dest' => $email },
447 map $_->cust_svc->cust_pkg,
449 'table' => 'svc_acct',
450 'hashref' => { 'username' => $user, },
452 'AND ( SELECT domain FROM svc_domain
453 WHERE svc_acct.domsvc = svc_domain.svcnum
454 ) = '. dbh->quote($domain),
460 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
462 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
479 Returns a qsearch hash expression to search for parameters specified in
480 HASHREF. Valid parameters are
498 listref of start date, end date
502 listref of start date, end date
504 =item spouse_birthdate
506 listref of start date, end date
508 =item anniversary_date
510 listref of start date, end date
520 =item current_balance
522 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
535 my ($class, $params) = @_;
542 # initialize these to prevent warnings
549 'paydate_year' => '',
550 'invoice_terms' => '',
556 # explicit custnum(s)
559 if ( $params->{'custnum'} ) {
560 my @custnums = ref($params->{'custnum'}) ?
561 @{ $params->{'custnum'} } :
562 $params->{'custnum'};
564 'cust_main.custnum IN (' .
565 join(',', map { $_ =~ /^(\d+)$/ ? $1 : () } @custnums ) .
566 ')' if scalar(@custnums) > 0;
573 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
575 "cust_main.agentnum = $1";
579 # do the same for user
582 if ( $params->{'usernum'} =~ /^(\d+)$/ and $1 ) {
584 "cust_main.usernum = $1";
591 #prospect ordered active inactive suspended cancelled
592 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
593 my $method = $params->{'status'}. '_sql';
594 #push @where, $class->$method();
595 push @where, FS::cust_main->$method();
601 if ( $params->{'address'} =~ /\S/ ) {
602 my $address = dbh->quote('%'. lc($params->{'address'}). '%');
603 push @where, '('. join(' OR ',
604 map "LOWER($_) LIKE $address",
605 qw(address1 address2 ship_address1 ship_address2)
613 if ( $params->{'refnum'} ) {
615 my @refnum = ref( $params->{'refnum'} )
616 ? @{ $params->{'refnum'} }
617 : ( $params->{'refnum'} );
619 @refnum = grep /^(\d*)$/, @refnum;
621 push @where, '( '. join(' OR ', map "cust_main.refnum = $_", @refnum ). ' )'
627 # parse cancelled package checkbox
632 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
633 unless $params->{'cancelled_pkgs'};
636 # parse without census tract checkbox
639 push @where, "(censustract = '' or censustract is null)"
640 if $params->{'no_censustract'};
643 # parse with hardcoded tax location checkbox
646 push @where, "geocode is not null"
647 if $params->{'with_geocode'};
650 # "with email address(es)" checkbox
654 'EXISTS ( SELECT 1 FROM cust_main_invoice
655 WHERE cust_main_invoice.custnum = cust_main.custnum
657 )' # AND dest LIKE '%@%'
658 if $params->{'with_email'};
661 # "without postal mail invoices" checkbox
665 "NOT EXISTS ( SELECT 1 FROM cust_main_invoice
666 WHERE cust_main_invoice.custnum = cust_main.custnum
668 if $params->{'no_POST'};
674 foreach my $field (qw( signupdate birthdate spouse_birthdate anniversary_date )) {
676 next unless exists($params->{$field});
678 my($beginning, $ending, $hour) = @{$params->{$field}};
681 "cust_main.$field IS NOT NULL",
682 "cust_main.$field >= $beginning",
683 "cust_main.$field <= $ending";
685 if($field eq 'signupdate' && defined $hour) {
686 if ($dbh->{Driver}->{Name} =~ /Pg/i) {
687 push @where, "extract(hour from to_timestamp(cust_main.$field)) = $hour";
689 elsif( $dbh->{Driver}->{Name} =~ /mysql/i) {
690 push @where, "hour(from_unixtime(cust_main.$field)) = $hour"
693 warn "search by time of day not supported on ".$dbh->{Driver}->{Name}." databases";
697 $orderby ||= "ORDER BY cust_main.$field";
705 if ( $params->{'classnum'} ) {
707 my @classnum = ref( $params->{'classnum'} )
708 ? @{ $params->{'classnum'} }
709 : ( $params->{'classnum'} );
711 @classnum = grep /^(\d*)$/, @classnum;
714 push @where, '( '. join(' OR ', map {
715 $_ ? "cust_main.classnum = $_"
716 : "cust_main.classnum IS NULL"
729 if ( $params->{'payby'} ) {
731 my @payby = ref( $params->{'payby'} )
732 ? @{ $params->{'payby'} }
733 : ( $params->{'payby'} );
735 @payby = grep /^([A-Z]{4})$/, @payby;
737 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )'
743 # paydate_year / paydate_month
746 if ( $params->{'paydate_year'} =~ /^(\d{4})$/ ) {
748 $params->{'paydate_month'} =~ /^(\d\d?)$/
749 or die "paydate_year without paydate_month?";
753 'paydate IS NOT NULL',
755 "CAST(paydate AS timestamp) < CAST('$year-$month-01' AS timestamp )"
763 if ( $params->{'invoice_terms'} =~ /^([\w ]+)$/ ) {
765 if ( $1 eq 'NULL' ) {
767 "( cust_main.invoice_terms IS NULL OR cust_main.invoice_terms = '' )";
770 "cust_main.invoice_terms IS NOT NULL",
771 "cust_main.invoice_terms = '$1'";
779 if ( $params->{'current_balance'} ) {
781 #my $balance_sql = $class->balance_sql();
782 my $balance_sql = FS::cust_main->balance_sql();
784 my @current_balance =
785 ref( $params->{'current_balance'} )
786 ? @{ $params->{'current_balance'} }
787 : ( $params->{'current_balance'} );
789 push @where, map { s/current_balance/$balance_sql/; $_ }
798 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
800 "cust_main.custbatch = '$1'";
803 if ( $params->{'tagnum'} ) {
804 my @tagnums = ref( $params->{'tagnum'} ) ? @{ $params->{'tagnum'} } : ( $params->{'tagnum'} );
806 @tagnums = grep /^(\d+)$/, @tagnums;
809 my $tags_where = "0 < (select count(1) from cust_tag where "
810 . " cust_tag.custnum = cust_main.custnum and tagnum in ("
811 . join(',', @tagnums) . "))";
813 push @where, $tags_where;
819 # setup queries, subs, etc. for the search
822 $orderby ||= 'ORDER BY custnum';
824 # here is the agent virtualization
826 $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main');
828 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
832 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
836 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
839 my(@extra_headers) = ();
840 my(@extra_fields) = ();
842 if ($params->{'flattened_pkgs'}) {
846 ' LEFT JOIN cust_pkg ON ( cust_main.custnum = cust_pkg.custnum ) ';
848 if ($dbh->{Driver}->{Name} eq 'Pg') {
850 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";
852 } elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
853 push @select, "GROUP_CONCAT(part_pkg.pkg SEPARATOR '|') as magic";
854 $addl_from .= ' LEFT JOIN part_pkg USING ( pkgpart ) ';
855 #$pkg_join .= ' LEFT JOIN part_pkg USING ( pkgpart ) ';
857 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
858 "omitting package information from report.";
861 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";
863 my $sth = dbh->prepare($header_query) or die dbh->errstr;
864 $sth->execute() or die $sth->errstr;
865 my $headerrow = $sth->fetchrow_arrayref;
866 my $headercount = $headerrow ? $headerrow->[0] : 0;
867 while($headercount) {
868 unshift @extra_headers, "Package ". $headercount;
869 unshift @extra_fields, eval q!sub {my $c = shift;
870 my @a = split '\|', $c->magic;
871 my $p = $a[!.--$headercount. q!];
878 if ( $params->{'with_geocode'} ) {
880 unshift @extra_headers, 'Tax location override', 'Calculated tax location';
881 unshift @extra_fields, sub { my $c = shift; $c->get('geocode'); },
883 $c->set('geocode', '');
884 $c->geocode('cch'); #XXX only cch right now
886 push @select, 'geocode';
887 push @select, 'zip' unless grep { $_ eq 'zip' } @select;
888 push @select, 'ship_zip' unless grep { $_ eq 'ship_zip' } @select;
891 my $select = join(', ', @select);
894 'table' => 'cust_main',
896 'addl_from' => $addl_from,
898 'extra_sql' => $extra_sql,
899 'order_by' => $orderby,
900 'count_query' => $count_query,
901 'extra_headers' => \@extra_headers,
902 'extra_fields' => \@extra_fields,
907 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
909 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
910 records. Currently, I<first>, I<last>, I<company> and/or I<address1> may be
911 specified (the appropriate ship_ field is also searched).
913 Additional options are the same as FS::Record::qsearch
918 my( $self, $fuzzy, $hash, @opt) = @_;
923 check_and_rebuild_fuzzyfiles();
924 foreach my $field ( keys %$fuzzy ) {
926 my $all = $self->all_X($field);
927 next unless scalar(@$all);
930 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
933 foreach ( keys %match ) {
934 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
935 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
938 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
941 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
943 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
951 =head1 UTILITY SUBROUTINES
955 =item check_and_rebuild_fuzzyfiles
959 sub check_and_rebuild_fuzzyfiles {
960 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
961 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields;
964 =item rebuild_fuzzyfiles
968 sub rebuild_fuzzyfiles {
970 use Fcntl qw(:flock);
972 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
973 mkdir $dir, 0700 unless -d $dir;
975 foreach my $fuzzy ( @fuzzyfields ) {
977 open(LOCK,">>$dir/cust_main.$fuzzy")
978 or die "can't open $dir/cust_main.$fuzzy: $!";
980 or die "can't lock $dir/cust_main.$fuzzy: $!";
982 open (CACHE, '>:encoding(UTF-8)', "$dir/cust_main.$fuzzy.tmp")
983 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
985 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
986 my $sth = dbh->prepare("SELECT $field FROM cust_main".
987 " WHERE $field != '' AND $field IS NOT NULL");
988 $sth->execute or die $sth->errstr;
990 while ( my $row = $sth->fetchrow_arrayref ) {
991 print CACHE $row->[0]. "\n";
996 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
998 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
1004 =item append_fuzzyfiles FIRSTNAME LASTNAME COMPANY ADDRESS1
1008 sub append_fuzzyfiles {
1009 #my( $first, $last, $company ) = @_;
1011 check_and_rebuild_fuzzyfiles();
1013 use Fcntl qw(:flock);
1015 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
1017 foreach my $field (@fuzzyfields) {
1022 open(CACHE, '>>:encoding(UTF-8)', "$dir/cust_main.$field" )
1023 or die "can't open $dir/cust_main.$field: $!";
1024 flock(CACHE,LOCK_EX)
1025 or die "can't lock $dir/cust_main.$field: $!";
1027 print CACHE "$value\n";
1029 flock(CACHE,LOCK_UN)
1030 or die "can't unlock $dir/cust_main.$field: $!";
1044 my( $self, $field ) = @_;
1045 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
1046 open(CACHE, '<:encoding(UTF-8)', "$dir/cust_main.$field")
1047 or die "can't open $dir/cust_main.$field: $!";
1048 my @array = map { chomp; $_; } <CACHE>;
1059 L<FS::cust_main>, L<FS::Record>