2 use base qw( FS::Record );
5 use vars qw( $skip_fuzzyfiles );
6 use Scalar::Util qw( blessed );
7 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::contact_class;
11 use FS::cust_location;
12 use FS::contact_phone;
13 use FS::contact_email;
21 FS::contact - Object methods for contact records
27 $record = new FS::contact \%hash;
28 $record = new FS::contact { 'column' => 'value' };
30 $error = $record->insert;
32 $error = $new_record->replace($old_record);
34 $error = $record->delete;
36 $error = $record->check;
40 An FS::contact object represents an example. FS::contact inherits from
41 FS::Record. The following fields are currently supported:
77 =item selfservice_access
83 =item _password_encoding
100 Creates a new example. To add the example to the database, see L<"insert">.
102 Note that this stores the hash reference, not a distinct copy of the hash it
103 points to. You can ask the object for a copy with the I<hash> method.
107 # the new method can be inherited from FS::Record, if a table method is defined
109 sub table { 'contact'; }
113 Adds this record to the database. If there is an error, returns the error,
114 otherwise returns false.
121 local $SIG{INT} = 'IGNORE';
122 local $SIG{QUIT} = 'IGNORE';
123 local $SIG{TERM} = 'IGNORE';
124 local $SIG{TSTP} = 'IGNORE';
125 local $SIG{PIPE} = 'IGNORE';
127 my $oldAutoCommit = $FS::UID::AutoCommit;
128 local $FS::UID::AutoCommit = 0;
131 my $error = $self->SUPER::insert;
133 $dbh->rollback if $oldAutoCommit;
137 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
138 keys %{ $self->hashref } ) {
139 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
140 my $phonetypenum = $1;
142 my $contact_phone = new FS::contact_phone {
143 'contactnum' => $self->contactnum,
144 'phonetypenum' => $phonetypenum,
145 _parse_phonestring( $self->get($pf) ),
147 $error = $contact_phone->insert;
149 $dbh->rollback if $oldAutoCommit;
154 if ( $self->get('emailaddress') =~ /\S/ ) {
156 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
158 my $contact_email = new FS::contact_email {
159 'contactnum' => $self->contactnum,
160 'emailaddress' => $email,
162 $error = $contact_email->insert;
164 $dbh->rollback if $oldAutoCommit;
172 unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) {
173 #warn " queueing fuzzyfiles update\n"
175 $error = $self->queue_fuzzyfiles_update;
177 $dbh->rollback if $oldAutoCommit;
178 return "updating fuzzy search cache: $error";
182 if ( $self->selfservice_access ) {
183 my $error = $self->send_reset_email( queue=>1 );
185 $dbh->rollback if $oldAutoCommit;
190 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
198 Delete this record from the database.
202 # the delete method can be inherited from FS::Record
207 local $SIG{HUP} = 'IGNORE';
208 local $SIG{INT} = 'IGNORE';
209 local $SIG{QUIT} = 'IGNORE';
210 local $SIG{TERM} = 'IGNORE';
211 local $SIG{TSTP} = 'IGNORE';
212 local $SIG{PIPE} = 'IGNORE';
214 my $oldAutoCommit = $FS::UID::AutoCommit;
215 local $FS::UID::AutoCommit = 0;
218 foreach my $cust_pkg ( $self->cust_pkg ) {
219 $cust_pkg->contactnum('');
220 my $error = $cust_pkg->replace;
222 $dbh->rollback if $oldAutoCommit;
227 foreach my $object ( $self->contact_phone, $self->contact_email ) {
228 my $error = $object->delete;
230 $dbh->rollback if $oldAutoCommit;
235 my $error = $self->SUPER::delete;
237 $dbh->rollback if $oldAutoCommit;
241 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
246 =item replace OLD_RECORD
248 Replaces the OLD_RECORD with this one in the database. If there is an error,
249 returns the error, otherwise returns false.
256 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
258 : $self->replace_old;
260 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
262 local $SIG{INT} = 'IGNORE';
263 local $SIG{QUIT} = 'IGNORE';
264 local $SIG{TERM} = 'IGNORE';
265 local $SIG{TSTP} = 'IGNORE';
266 local $SIG{PIPE} = 'IGNORE';
268 my $oldAutoCommit = $FS::UID::AutoCommit;
269 local $FS::UID::AutoCommit = 0;
272 my $error = $self->SUPER::replace($old);
274 $dbh->rollback if $oldAutoCommit;
278 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
279 keys %{ $self->hashref } ) {
280 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
281 my $phonetypenum = $1;
283 my %cp = ( 'contactnum' => $self->contactnum,
284 'phonetypenum' => $phonetypenum,
286 my $contact_phone = qsearchs('contact_phone', \%cp)
287 || new FS::contact_phone \%cp;
289 my %cpd = _parse_phonestring( $self->get($pf) );
290 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
292 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
294 $error = $contact_phone->$method;
296 $dbh->rollback if $oldAutoCommit;
301 if ( defined($self->hashref->{'emailaddress'}) ) {
303 #ineffecient but whatever, how many email addresses can there be?
305 foreach my $contact_email ( $self->contact_email ) {
306 my $error = $contact_email->delete;
308 $dbh->rollback if $oldAutoCommit;
313 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
315 my $contact_email = new FS::contact_email {
316 'contactnum' => $self->contactnum,
317 'emailaddress' => $email,
319 $error = $contact_email->insert;
321 $dbh->rollback if $oldAutoCommit;
329 unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) {
330 #warn " queueing fuzzyfiles update\n"
332 $error = $self->queue_fuzzyfiles_update;
334 $dbh->rollback if $oldAutoCommit;
335 return "updating fuzzy search cache: $error";
339 if ( ( $old->selfservice_access eq '' && $self->selfservice_access
340 && ! $self->_password
345 my $error = $self->send_reset_email( queue=>1 );
347 $dbh->rollback if $oldAutoCommit;
352 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
358 #i probably belong in contact_phone.pm
359 sub _parse_phonestring {
362 my($countrycode, $extension) = ('1', '');
365 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
371 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
375 ( 'countrycode' => $countrycode,
376 'phonenum' => $value,
377 'extension' => $extension,
381 =item queue_fuzzyfiles_update
383 Used by insert & replace to update the fuzzy search cache
387 use FS::cust_main::Search;
388 sub queue_fuzzyfiles_update {
391 local $SIG{HUP} = 'IGNORE';
392 local $SIG{INT} = 'IGNORE';
393 local $SIG{QUIT} = 'IGNORE';
394 local $SIG{TERM} = 'IGNORE';
395 local $SIG{TSTP} = 'IGNORE';
396 local $SIG{PIPE} = 'IGNORE';
398 my $oldAutoCommit = $FS::UID::AutoCommit;
399 local $FS::UID::AutoCommit = 0;
402 foreach my $field ( 'first', 'last' ) {
403 my $queue = new FS::queue {
404 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
406 my @args = "contact.$field", $self->get($field);
407 my $error = $queue->insert( @args );
409 $dbh->rollback if $oldAutoCommit;
410 return "queueing job (transaction rolled back): $error";
414 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
421 Checks all fields to make sure this is a valid example. If there is
422 an error, returns the error, otherwise returns false. Called by the insert
427 # the check method should currently be supplied - FS::Record contains some
428 # data checking routines
433 if ( $self->selfservice_access eq 'R' ) {
434 $self->selfservice_access('Y');
439 $self->ut_numbern('contactnum')
440 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
441 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
442 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
443 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
444 || $self->ut_namen('last')
445 || $self->ut_namen('first')
446 || $self->ut_textn('title')
447 || $self->ut_textn('comment')
448 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
449 || $self->ut_textn('_password')
450 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
451 || $self->ut_enum('disabled', [ '', 'Y' ])
453 return $error if $error;
455 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
456 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
458 return "One of first name, last name, or title must have a value"
459 if ! grep $self->$_(), qw( first last title);
466 my $data = $self->first. ' '. $self->last;
467 $data .= ', '. $self->title
469 $data .= ' ('. $self->comment. ')'
476 return '' unless $self->locationnum;
477 qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
482 return '' unless $self->classnum;
483 qsearchs('contact_class', { 'classnum' => $self->classnum } );
488 $self->first . ' ' . $self->last;
491 sub contact_classname {
493 my $contact_class = $self->contact_class or return '';
494 $contact_class->classname;
499 qsearch('contact_phone', { 'contactnum' => $self->contactnum } );
504 qsearch('contact_email', { 'contactnum' => $self->contactnum } );
509 qsearchs('cust_main', { 'custnum' => $self->custnum } );
514 qsearch('cust_pkg', { 'contactnum' => $self->contactnum } );
518 sub by_selfservice_email {
519 my($class, $email) = @_;
521 my $contact_email = qsearchs({
522 'table' => 'contact_email',
523 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
524 'hashref' => { 'emailaddress' => $email, },
525 'extra_sql' => " AND selfservice_access = 'Y' ".
526 " AND ( disabled IS NULL OR disabled = '' )",
529 $contact_email->contact;
533 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
534 # and should maybe be libraried in some way for other password needs
536 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
538 sub authenticate_password {
539 my($self, $check_password) = @_;
541 if ( $self->_password_encoding eq 'bcrypt' ) {
543 my( $cost, $salt, $hash ) = split(',', $self->_password);
545 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
547 salt => de_base64($salt),
553 $hash eq $check_hash;
557 return 0 if $self->_password eq '';
559 $self->_password eq $check_password;
565 sub change_password {
566 my($self, $new_password) = @_;
568 $self->change_password_fields( $new_password );
574 sub change_password_fields {
575 my($self, $new_password) = @_;
577 $self->_password_encoding('bcrypt');
581 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
583 my $hash = bcrypt_hash( { key_nul => 1,
591 join(',', $cost, en_base64($salt), en_base64($hash) )
596 # end of false laziness w/FS/FS/Auth/internal.pm
599 #false laziness w/ClientAPI/MyAccount/reset_passwd
600 use Digest::SHA qw(sha512_hex);
602 use FS::ClientAPI_SessionCache;
603 sub send_reset_email {
604 my( $self, %opt ) = @_;
606 my @contact_email = $self->contact_email or return '';
608 my $reset_session = {
609 'contactnum' => $self->contactnum,
610 'svcnum' => $opt{'svcnum'},
613 my $timeout = '24 hours'; #?
615 my $reset_session_id;
617 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
618 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
621 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
625 my $conf = new FS::Conf;
627 my $cust_main = $self->cust_main
628 or die "no customer"; #reset a password for a prospect contact? someday
630 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
631 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
632 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
633 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
635 'to' => join(',', map $_->emailaddress, @contact_email ),
636 'cust_main' => $cust_main,
638 'substitutions' => { 'session_id' => $reset_session_id }
641 if ( $opt{'queue'} ) { #or should queueing just be the default?
643 my $queue = new FS::queue {
644 'job' => 'FS::Misc::process_send_email',
645 'custnum' => $cust_main->custnum,
647 $queue->insert( $msg_template->prepare( %msg_template ) );
651 $msg_template->send( %msg_template );
657 use vars qw( $myaccount_cache );
658 sub myaccount_cache {
660 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
661 'namespace' => 'FS::ClientAPI::MyAccount',
671 L<FS::Record>, schema.html from the base documentation.