2 use base qw( FS::Password_Mixin
6 use vars qw( $skip_fuzzyfiles );
8 use Scalar::Util qw( blessed );
9 use FS::Record qw( qsearch qsearchs dbh );
11 use FS::contact_phone;
12 use FS::contact_email;
14 use FS::phone_type; #for cgi_contact_fields
16 use FS::prospect_contact;
22 FS::contact - Object methods for contact records
28 $record = new FS::contact \%hash;
29 $record = new FS::contact { 'column' => 'value' };
31 $error = $record->insert;
33 $error = $new_record->replace($old_record);
35 $error = $record->delete;
37 $error = $record->check;
41 An FS::contact object represents an specific contact person for a prospect or
42 customer. FS::contact inherits from FS::Record. The following fields are
79 =item selfservice_access
85 =item _password_encoding
101 Creates a new contact. To add the contact to the database, see L<"insert">.
103 Note that this stores the hash reference, not a distinct copy of the hash it
104 points to. You can ask the object for a copy with the I<hash> method.
108 sub table { 'contact'; }
112 Adds this record to the database. If there is an error, returns the error,
113 otherwise returns false.
115 If the object has an C<emailaddress> field, L<FS::contact_email> records will
116 be created for each (comma-separated) email address in that field. If any of
117 these coincide with an existing email address, this contact will be merged with
118 the contact with that address.
120 Then, if the object has any fields named C<phonetypenumN> an
121 L<FS::contact_phone> record will be created for each of them. Those fields
122 should contain phone numbers of the appropriate types (where N is the key of
123 an L<FS::phone_type> record identifying the type of number: daytime, night,
126 After inserting the record, if the object has a 'custnum' or 'prospectnum'
127 field, an L<FS::cust_contact> or L<FS::prospect_contact> record will be
128 created to link the contact to the customer. The following fields will also
129 be included in that record, if they are set on the object:
140 local $SIG{INT} = 'IGNORE';
141 local $SIG{QUIT} = 'IGNORE';
142 local $SIG{TERM} = 'IGNORE';
143 local $SIG{TSTP} = 'IGNORE';
144 local $SIG{PIPE} = 'IGNORE';
146 my $oldAutoCommit = $FS::UID::AutoCommit;
147 local $FS::UID::AutoCommit = 0;
150 #save off and blank values that move to cust_contact / prospect_contact now
151 my $prospectnum = $self->prospectnum;
152 $self->prospectnum('');
153 my $custnum = $self->custnum;
157 for (qw( classnum comment selfservice_access invoice_dest )) {
158 $link_hash{$_} = $self->get($_);
162 #look for an existing contact with this email address
163 my $existing_contact = '';
164 if ( $self->get('emailaddress') =~ /\S/ ) {
166 my %existing_contact = ();
168 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
170 my $contact_email = qsearchs('contact_email', { emailaddress=>$email } )
173 my $contact = $contact_email->contact;
174 $existing_contact{ $contact->contactnum } = $contact;
178 if ( scalar( keys %existing_contact ) > 1 ) {
179 $dbh->rollback if $oldAutoCommit;
180 return 'Multiple email addresses specified '.
181 ' that already belong to separate contacts';
182 } elsif ( scalar( keys %existing_contact ) ) {
183 ($existing_contact) = values %existing_contact;
189 if ( $existing_contact ) {
191 $self->$_($existing_contact->$_())
192 for qw( contactnum _password _password_encoding );
193 $error = $self->SUPER::replace($existing_contact);
197 $error = $self->SUPER::insert;
201 $error ||= $self->insert_password_history;
204 $dbh->rollback if $oldAutoCommit;
208 my $cust_contact = '';
210 my %hash = ( 'contactnum' => $self->contactnum,
211 'custnum' => $custnum,
213 $cust_contact = qsearchs('cust_contact', \%hash )
214 || new FS::cust_contact { %hash, %link_hash };
215 my $error = $cust_contact->custcontactnum ? $cust_contact->replace
216 : $cust_contact->insert;
218 $dbh->rollback if $oldAutoCommit;
223 if ( $prospectnum ) {
224 my %hash = ( 'contactnum' => $self->contactnum,
225 'prospectnum' => $prospectnum,
227 my $prospect_contact = qsearchs('prospect_contact', \%hash )
228 || new FS::prospect_contact { %hash, %link_hash };
230 $prospect_contact->prospectcontactnum ? $prospect_contact->replace
231 : $prospect_contact->insert;
233 $dbh->rollback if $oldAutoCommit;
238 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
239 keys %{ $self->hashref } ) {
240 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
241 my $phonetypenum = $1;
243 my %hash = ( 'contactnum' => $self->contactnum,
244 'phonetypenum' => $phonetypenum,
247 qsearchs('contact_phone', \%hash)
248 || new FS::contact_phone { %hash, _parse_phonestring($self->get($pf)) };
249 my $error = $contact_phone->contactphonenum ? $contact_phone->replace
250 : $contact_phone->insert;
252 $dbh->rollback if $oldAutoCommit;
257 if ( $self->get('emailaddress') =~ /\S/ ) {
259 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
261 'contactnum' => $self->contactnum,
262 'emailaddress' => $email,
264 unless ( qsearchs('contact_email', \%hash) ) {
265 my $contact_email = new FS::contact_email \%hash;
266 my $error = $contact_email->insert;
268 $dbh->rollback if $oldAutoCommit;
276 unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) {
277 #warn " queueing fuzzyfiles update\n"
279 my $error = $self->queue_fuzzyfiles_update;
281 $dbh->rollback if $oldAutoCommit;
282 return "updating fuzzy search cache: $error";
286 if ( $link_hash{'selfservice_access'} eq 'R'
287 or ( $link_hash{'selfservice_access'}
289 && ! length($self->_password)
293 my $error = $self->send_reset_email( queue=>1 );
295 $dbh->rollback if $oldAutoCommit;
300 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
308 Delete this record from the database.
315 local $SIG{HUP} = 'IGNORE';
316 local $SIG{INT} = 'IGNORE';
317 local $SIG{QUIT} = 'IGNORE';
318 local $SIG{TERM} = 'IGNORE';
319 local $SIG{TSTP} = 'IGNORE';
320 local $SIG{PIPE} = 'IGNORE';
322 my $oldAutoCommit = $FS::UID::AutoCommit;
323 local $FS::UID::AutoCommit = 0;
326 #got a prospetnum or custnum? delete the prospect_contact or cust_contact link
328 if ( $self->prospectnum ) {
329 my $prospect_contact = qsearchs('prospect_contact', {
330 'contactnum' => $self->contactnum,
331 'prospectnum' => $self->prospectnum,
333 my $error = $prospect_contact->delete;
335 $dbh->rollback if $oldAutoCommit;
340 if ( $self->custnum ) {
341 my $cust_contact = qsearchs('cust_contact', {
342 'contactnum' => $self->contactnum,
343 'custnum' => $self->custnum,
345 my $error = $cust_contact->delete;
347 $dbh->rollback if $oldAutoCommit;
352 # then, proceed with deletion only if the contact isn't attached to any other
353 # prospects or customers
355 #inefficient, but how many prospects/customers can a single contact be
356 # attached too? (and is removing them from one a common operation?)
357 if ( $self->prospect_contact || $self->cust_contact ) {
358 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
362 #proceed with deletion
364 foreach my $cust_pkg ( $self->cust_pkg ) {
365 $cust_pkg->contactnum('');
366 my $error = $cust_pkg->replace;
368 $dbh->rollback if $oldAutoCommit;
373 foreach my $object ( $self->contact_phone, $self->contact_email ) {
374 my $error = $object->delete;
376 $dbh->rollback if $oldAutoCommit;
381 my $error = $self->delete_password_history
382 || $self->SUPER::delete;
384 $dbh->rollback if $oldAutoCommit;
388 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
393 =item replace OLD_RECORD
395 Replaces the OLD_RECORD with this one in the database. If there is an error,
396 returns the error, otherwise returns false.
403 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
405 : $self->replace_old;
407 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
409 local $SIG{INT} = 'IGNORE';
410 local $SIG{QUIT} = 'IGNORE';
411 local $SIG{TERM} = 'IGNORE';
412 local $SIG{TSTP} = 'IGNORE';
413 local $SIG{PIPE} = 'IGNORE';
415 my $oldAutoCommit = $FS::UID::AutoCommit;
416 local $FS::UID::AutoCommit = 0;
419 #save off and blank values that move to cust_contact / prospect_contact now
420 my $prospectnum = $self->prospectnum;
421 $self->prospectnum('');
422 my $custnum = $self->custnum;
426 for (qw( classnum comment selfservice_access invoice_dest )) {
427 $link_hash{$_} = $self->get($_);
431 my $error = $self->SUPER::replace($old);
432 if ( $old->_password ne $self->_password ) {
433 $error ||= $self->insert_password_history;
436 $dbh->rollback if $oldAutoCommit;
440 my $cust_contact = '';
442 my %hash = ( 'contactnum' => $self->contactnum,
443 'custnum' => $custnum,
446 if ( $cust_contact = qsearchs('cust_contact', \%hash ) ) {
447 $cust_contact->$_($link_hash{$_}) for keys %link_hash;
448 $error = $cust_contact->replace;
450 $cust_contact = new FS::cust_contact { %hash, %link_hash };
451 $error = $cust_contact->insert;
454 $dbh->rollback if $oldAutoCommit;
459 if ( $prospectnum ) {
460 my %hash = ( 'contactnum' => $self->contactnum,
461 'prospectnum' => $prospectnum,
464 if ( my $prospect_contact = qsearchs('prospect_contact', \%hash ) ) {
465 $prospect_contact->$_($link_hash{$_}) for keys %link_hash;
466 $error = $prospect_contact->replace;
468 my $prospect_contact = new FS::prospect_contact { %hash, %link_hash };
469 $error = $prospect_contact->insert;
472 $dbh->rollback if $oldAutoCommit;
477 foreach my $pf ( grep { /^phonetypenum(\d+)$/ }
478 keys %{ $self->hashref } ) {
479 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
480 my $phonetypenum = $1;
482 my %cp = ( 'contactnum' => $self->contactnum,
483 'phonetypenum' => $phonetypenum,
485 my $contact_phone = qsearchs('contact_phone', \%cp);
487 my $pv = $self->get($pf);
490 #if new value is empty, delete old entry
492 if ($contact_phone) {
493 $error = $contact_phone->delete;
495 $dbh->rollback if $oldAutoCommit;
502 $contact_phone ||= new FS::contact_phone \%cp;
504 my %cpd = _parse_phonestring( $pv );
505 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
507 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
509 $error = $contact_phone->$method;
511 $dbh->rollback if $oldAutoCommit;
516 if ( defined($self->hashref->{'emailaddress'}) ) {
518 #ineffecient but whatever, how many email addresses can there be?
520 foreach my $contact_email ( $self->contact_email ) {
521 my $error = $contact_email->delete;
523 $dbh->rollback if $oldAutoCommit;
528 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
530 my $contact_email = new FS::contact_email {
531 'contactnum' => $self->contactnum,
532 'emailaddress' => $email,
534 $error = $contact_email->insert;
536 $dbh->rollback if $oldAutoCommit;
544 unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) {
545 #warn " queueing fuzzyfiles update\n"
547 $error = $self->queue_fuzzyfiles_update;
549 $dbh->rollback if $oldAutoCommit;
550 return "updating fuzzy search cache: $error";
554 if ( $cust_contact and (
555 ( $cust_contact->selfservice_access eq ''
556 && $link_hash{selfservice_access}
557 && ! length($self->_password)
559 || $cust_contact->_resend()
563 my $error = $self->send_reset_email( queue=>1 );
565 $dbh->rollback if $oldAutoCommit;
570 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
576 =item _parse_phonestring PHONENUMBER_STRING
578 Subroutine, takes a string and returns a list (suitable for assigning to a hash)
579 with keys 'countrycode', 'phonenum' and 'extension'
581 (Should probably be moved to contact_phone.pm, hence the initial underscore.)
585 sub _parse_phonestring {
588 my($countrycode, $extension) = ('1', '');
591 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
597 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
601 ( 'countrycode' => $countrycode,
602 'phonenum' => $value,
603 'extension' => $extension,
607 =item queue_fuzzyfiles_update
609 Used by insert & replace to update the fuzzy search cache
613 use FS::cust_main::Search;
614 sub queue_fuzzyfiles_update {
617 local $SIG{HUP} = 'IGNORE';
618 local $SIG{INT} = 'IGNORE';
619 local $SIG{QUIT} = 'IGNORE';
620 local $SIG{TERM} = 'IGNORE';
621 local $SIG{TSTP} = 'IGNORE';
622 local $SIG{PIPE} = 'IGNORE';
624 my $oldAutoCommit = $FS::UID::AutoCommit;
625 local $FS::UID::AutoCommit = 0;
628 foreach my $field ( 'first', 'last' ) {
629 my $queue = new FS::queue {
630 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
632 my @args = "contact.$field", $self->get($field);
633 my $error = $queue->insert( @args );
635 $dbh->rollback if $oldAutoCommit;
636 return "queueing job (transaction rolled back): $error";
640 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
647 Checks all fields to make sure this is a valid contact. If there is
648 an error, returns the error, otherwise returns false. Called by the insert
656 if ( $self->selfservice_access eq 'R' ) {
657 $self->selfservice_access('Y');
662 $self->ut_numbern('contactnum')
663 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
664 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
665 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
666 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
667 || $self->ut_namen('last')
668 || $self->ut_namen('first')
669 || $self->ut_textn('title')
670 || $self->ut_textn('comment')
671 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
672 || $self->ut_textn('_password')
673 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
674 || $self->ut_enum('disabled', [ '', 'Y' ])
676 return $error if $error;
678 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
680 return "One of first name, last name, or title must have a value"
681 if ! grep $self->$_(), qw( first last title);
688 Returns a formatted string representing this contact, including name, title and
695 my $data = $self->first. ' '. $self->last;
696 $data .= ', '. $self->title
698 $data .= ' ('. $self->comment. ')'
705 Returns a formatted string representing this contact, with just the name.
711 $self->first . ' ' . $self->last;
714 #=item contact_classname PROSPECT_OBJ | CUST_MAIN_OBJ
716 #Returns the name of this contact's class for the specified prospect or
717 #customer (see L<FS::prospect_contact>, L<FS::cust_contact> and
718 #L<FS::contact_class>).
722 #sub contact_classname {
723 # my( $self, $prospect_or_cust ) = @_;
726 # if ( ref($prospect_or_cust) eq 'FS::prospect_main' ) {
727 # $link = qsearchs('prospect_contact', {
728 # 'contactnum' => $self->contactnum,
729 # 'prospectnum' => $prospect_or_cust->prospectnum,
731 # } elsif ( ref($prospect_or_cust) eq 'FS::cust_main' ) {
732 # $link = qsearchs('cust_contact', {
733 # 'contactnum' => $self->contactnum,
734 # 'custnum' => $prospect_or_cust->custnum,
737 # croak "$prospect_or_cust is not an FS::prospect_main or FS::cust_main object";
740 # my $contact_class = $link->contact_class or return '';
741 # $contact_class->classname;
744 =item by_selfservice_email EMAILADDRESS
746 Alternate search constructor (class method). Given an email address,
747 returns the contact for that address, or the empty string if no contact
748 has that email address.
752 sub by_selfservice_email {
753 my($class, $email) = @_;
755 my $contact_email = qsearchs({
756 'table' => 'contact_email',
757 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
758 'hashref' => { 'emailaddress' => $email, },
759 'extra_sql' => " AND ( disabled IS NULL OR disabled = '' )",
762 $contact_email->contact;
766 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
767 # and should maybe be libraried in some way for other password needs
769 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
771 sub authenticate_password {
772 my($self, $check_password) = @_;
774 if ( $self->_password_encoding eq 'bcrypt' ) {
776 my( $cost, $salt, $hash ) = split(',', $self->_password);
778 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
780 salt => de_base64($salt),
786 $hash eq $check_hash;
790 return 0 if $self->_password eq '';
792 $self->_password eq $check_password;
798 =item change_password NEW_PASSWORD
800 Changes the contact's selfservice access password to NEW_PASSWORD. This does
801 not check password policy rules (see C<is_password_allowed>) and will return
802 an error only if editing the record fails for some reason.
804 If NEW_PASSWORD is the same as the existing password, this does nothing.
808 sub change_password {
809 my($self, $new_password) = @_;
811 # do nothing if the password is unchanged
812 return if $self->authenticate_password($new_password);
814 $self->change_password_fields( $new_password );
820 sub change_password_fields {
821 my($self, $new_password) = @_;
823 $self->_password_encoding('bcrypt');
827 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
829 my $hash = bcrypt_hash( { key_nul => 1,
837 join(',', $cost, en_base64($salt), en_base64($hash) )
842 # end of false laziness w/FS/FS/Auth/internal.pm
845 #false laziness w/ClientAPI/MyAccount/reset_passwd
846 use Digest::SHA qw(sha512_hex);
848 use FS::ClientAPI_SessionCache;
849 sub send_reset_email {
850 my( $self, %opt ) = @_;
852 my @contact_email = $self->contact_email or return '';
854 my $reset_session = {
855 'contactnum' => $self->contactnum,
856 'svcnum' => $opt{'svcnum'},
859 my $timeout = '24 hours'; #?
861 my $reset_session_id;
863 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
864 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
867 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
871 my $conf = new FS::Conf;
874 my @cust_contact = grep $_->selfservice_access, $self->cust_contact;
875 $cust_main = $cust_contact[0]->cust_main if scalar(@cust_contact) == 1;
877 my $agentnum = $cust_main ? $cust_main->agentnum : '';
878 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $agentnum);
879 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
880 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
881 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
882 return { 'error' => "selfservice-password_reset_msgnum cannot be loaded" } unless $msg_template;
884 'to' => join(',', map $_->emailaddress, @contact_email ),
885 'cust_main' => $cust_main,
887 'substitutions' => { 'session_id' => $reset_session_id }
890 if ( $opt{'queue'} ) { #or should queueing just be the default?
892 my $cust_msg = $msg_template->prepare( %msg_template );
893 my $error = $cust_msg->insert;
894 return { 'error' => $error } if $error;
895 my $queue = new FS::queue {
896 'job' => 'FS::cust_msg::process_send',
897 'custnum' => $cust_main ? $cust_main->custnum : '',
899 $queue->insert( $cust_msg->custmsgnum );
903 $msg_template->send( %msg_template );
909 use vars qw( $myaccount_cache );
910 sub myaccount_cache {
912 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
913 'namespace' => 'FS::ClientAPI::MyAccount',
917 =item cgi_contact_fields
919 Returns a list reference containing the set of contact fields used in the web
920 interface for one-line editing (i.e. excluding contactnum, prospectnum, custnum
921 and locationnum, as well as password fields, but including fields for
922 contact_email and contact_phone records.)
926 sub cgi_contact_fields {
929 my @contact_fields = qw(
930 classnum first last title comment emailaddress selfservice_access
934 push @contact_fields, 'phonetypenum'. $_->phonetypenum
935 foreach qsearch({table=>'phone_type', order_by=>'weight'});
941 use FS::upgrade_journal;
942 sub _upgrade_data { #class method
943 my ($class, %opts) = @_;
945 # always migrate cust_main_invoice records over
946 local $FS::cust_main::import = 1; # override require_phone and such
947 my $search = FS::Cursor->new('cust_main_invoice', {});
949 while (my $cust_main_invoice = $search->fetch) {
950 my $custnum = $cust_main_invoice->custnum;
951 my $dest = $cust_main_invoice->dest;
952 my $cust_main = $cust_main_invoice->cust_main;
954 if ( $dest =~ /^\d+$/ ) {
955 my $svc_acct = FS::svc_acct->by_key($dest);
956 die "custnum $custnum, invoice destination svcnum $svc_acct does not exist\n"
958 $dest = $svc_acct->email;
960 push @{ $custnum_dest{$custnum} ||= [] }, $dest;
962 my $error = $cust_main_invoice->delete;
964 die "custnum $custnum, cleaning up cust_main_invoice: $error\n";
968 foreach my $custnum (keys %custnum_dest) {
969 my $dests = $custnum_dest{$custnum};
970 my $cust_main = FS::cust_main->by_key($custnum);
971 my $error = $cust_main->replace( invoicing_list => $dests );
973 die "custnum $custnum, creating contact: $error\n";
977 unless ( FS::upgrade_journal->is_done('contact_invoice_dest') ) {
979 local($skip_fuzzyfiles) = 1;
981 foreach my $contact (qsearch('contact', {})) {
982 my $error = $contact->replace;
983 die $error if $error;
986 FS::upgrade_journal->set_done('contact_invoice_dest');
997 L<FS::Record>, schema.html from the base documentation.