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;
13 use FS::contact::Import;
15 use FS::phone_type; #for cgi_contact_fields
17 use FS::prospect_contact;
23 FS::contact - Object methods for contact records
29 $record = new FS::contact \%hash;
30 $record = new FS::contact { 'column' => 'value' };
32 $error = $record->insert;
34 $error = $new_record->replace($old_record);
36 $error = $record->delete;
38 $error = $record->check;
42 An FS::contact object represents an specific contact person for a prospect or
43 customer. FS::contact inherits from FS::Record. The following fields are
80 =item selfservice_access
86 =item _password_encoding
102 Creates a new contact. To add the contact to the database, see L<"insert">.
104 Note that this stores the hash reference, not a distinct copy of the hash it
105 points to. You can ask the object for a copy with the I<hash> method.
109 sub table { 'contact'; }
113 Adds this record to the database. If there is an error, returns the error,
114 otherwise returns false.
116 If the object has an C<emailaddress> field, L<FS::contact_email> records
117 will be created for each (comma-separated) email address in that field. If
118 any of these coincide with an existing email address, this contact will be
119 merged with the contact with that address.
121 Then, if the object has any fields named C<phonetypenumN> an
122 L<FS::contact_phone> record will be created for each of them. Those fields
123 should contain phone numbers of the appropriate types (where N is the key of
124 an L<FS::phone_type> record identifying the type of number: daytime, night,
127 After inserting the record, if the object has a 'custnum' or 'prospectnum'
128 field, an L<FS::cust_contact> or L<FS::prospect_contact> record will be
129 created to link the contact to the customer. The following fields will also
130 be included in that record, if they are set on the object:
141 local $SIG{INT} = 'IGNORE';
142 local $SIG{QUIT} = 'IGNORE';
143 local $SIG{TERM} = 'IGNORE';
144 local $SIG{TSTP} = 'IGNORE';
145 local $SIG{PIPE} = 'IGNORE';
147 my $oldAutoCommit = $FS::UID::AutoCommit;
148 local $FS::UID::AutoCommit = 0;
151 #save off and blank values that move to cust_contact / prospect_contact now
152 my $prospectnum = $self->prospectnum;
153 $self->prospectnum('');
154 my $custnum = $self->custnum;
158 for (qw( classnum comment selfservice_access invoice_dest message_dest)) {
159 $link_hash{$_} = $self->get($_);
164 ## check for an existing contact with this email address other than current customer
165 ## if found, just add that contact to cust_contact with link_hash credentials
166 ## as email can not be tied to two contacts.
168 my $existing_contact = '';
169 my @contact_emails = ();
170 my %contact_nums = ();
171 $contact_nums{$self->contactnum} = '1' if $self->contactnum;
173 if ( $self->get('emailaddress') =~ /\S/ ) {
175 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
177 my $contact_email = qsearchs('contact_email', { emailaddress=>$email } );
178 unless ($contact_email) { push @contact_emails, $email; next; }
180 my $contact = $contact_email->contact;
181 if ($contact->contactnum eq $self->contactnum) {
182 push @contact_emails, $email;
185 $contact_nums{$contact->contactnum} = '1';
190 my $emails = join(' , ', @contact_emails);
191 $self->emailaddress($emails);
193 $no_new_contact = '1' unless $self->emailaddress;
198 $error = $self->SUPER::insert unless $no_new_contact;
201 $dbh->rollback if $oldAutoCommit;
205 my $cust_contact = '';
206 # if $self->custnum was set, then the customer-specific properties
207 # (custnum, classnum, invoice_dest, selfservice_access, comment) are in
208 # pseudo-fields, and are now in %link_hash. otherwise, ignore all those
211 foreach my $contactnum (keys %contact_nums) {
212 my %hash = ( 'contactnum' => $contactnum,
213 'custnum' => $custnum,
215 $cust_contact = qsearchs('cust_contact', \%hash )
216 || new FS::cust_contact { %hash, %link_hash };
217 my $error = $cust_contact->custcontactnum ? $cust_contact->replace
218 : $cust_contact->insert;
220 $dbh->rollback if $oldAutoCommit;
226 if ( $prospectnum && !$no_new_contact) {
227 my %hash = ( 'contactnum' => $self->contactnum,
228 'prospectnum' => $prospectnum,
230 my $prospect_contact = qsearchs('prospect_contact', \%hash )
231 || new FS::prospect_contact { %hash, %link_hash };
233 $prospect_contact->prospectcontactnum ? $prospect_contact->replace
234 : $prospect_contact->insert;
236 $dbh->rollback if $oldAutoCommit;
241 unless ($no_new_contact) {
242 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
243 keys %{ $self->hashref } ) {
244 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
245 my $phonetypenum = $1;
247 my %hash = ( 'contactnum' => $self->contactnum,
248 'phonetypenum' => $phonetypenum,
251 qsearchs('contact_phone', \%hash)
252 || new FS::contact_phone { %hash, _parse_phonestring($self->get($pf)) };
253 my $error = $contact_phone->contactphonenum ? $contact_phone->replace
254 : $contact_phone->insert;
256 $dbh->rollback if $oldAutoCommit;
262 if ( $self->get('emailaddress') =~ /\S/ ) {
264 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
266 'contactnum' => $self->contactnum,
267 'emailaddress' => $email,
269 unless ( qsearchs('contact_email', \%hash) ) {
270 my $contact_email = new FS::contact_email \%hash;
271 my $error = $contact_email->insert;
273 $dbh->rollback if $oldAutoCommit;
281 unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) {
282 #warn " queueing fuzzyfiles update\n"
284 my $error = $self->queue_fuzzyfiles_update;
286 $dbh->rollback if $oldAutoCommit;
287 return "updating fuzzy search cache: $error";
291 if ( $link_hash{'selfservice_access'} eq 'R'
292 or ( $link_hash{'selfservice_access'}
294 && ! length($self->_password)
298 my $error = $self->send_reset_email( queue=>1 );
300 $dbh->rollback if $oldAutoCommit;
305 if ( $self->get('password') ) {
306 my $error = $self->is_password_allowed($self->get('password'))
307 || $self->change_password($self->get('password'));
309 $dbh->rollback if $oldAutoCommit;
314 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
322 Delete this record from the database.
329 local $SIG{HUP} = 'IGNORE';
330 local $SIG{INT} = 'IGNORE';
331 local $SIG{QUIT} = 'IGNORE';
332 local $SIG{TERM} = 'IGNORE';
333 local $SIG{TSTP} = 'IGNORE';
334 local $SIG{PIPE} = 'IGNORE';
336 my $oldAutoCommit = $FS::UID::AutoCommit;
337 local $FS::UID::AutoCommit = 0;
340 #got a prospetnum or custnum? delete the prospect_contact or cust_contact link
342 if ( $self->prospectnum ) {
343 my $prospect_contact = qsearchs('prospect_contact', {
344 'contactnum' => $self->contactnum,
345 'prospectnum' => $self->prospectnum,
347 my $error = $prospect_contact->delete;
349 $dbh->rollback if $oldAutoCommit;
354 # if $self->custnum was set, then we're removing the contact from this
356 if ( $self->custnum ) {
357 my $cust_contact = qsearchs('cust_contact', {
358 'contactnum' => $self->contactnum,
359 'custnum' => $self->custnum,
361 my $error = $cust_contact->delete;
363 $dbh->rollback if $oldAutoCommit;
368 # then, proceed with deletion only if the contact isn't attached to any other
369 # prospects or customers
371 #inefficient, but how many prospects/customers can a single contact be
372 # attached too? (and is removing them from one a common operation?)
373 if ( $self->prospect_contact || $self->cust_contact ) {
374 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
378 #proceed with deletion
380 foreach my $cust_pkg ( $self->cust_pkg ) {
381 $cust_pkg->contactnum('');
382 my $error = $cust_pkg->replace;
384 $dbh->rollback if $oldAutoCommit;
389 foreach my $object ( $self->contact_phone, $self->contact_email ) {
390 my $error = $object->delete;
392 $dbh->rollback if $oldAutoCommit;
397 my $error = $self->delete_password_history
398 || $self->SUPER::delete;
400 $dbh->rollback if $oldAutoCommit;
404 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
409 =item replace OLD_RECORD
411 Replaces the OLD_RECORD with this one in the database. If there is an error,
412 returns the error, otherwise returns false.
419 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
421 : $self->replace_old;
423 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
425 local $SIG{INT} = 'IGNORE';
426 local $SIG{QUIT} = 'IGNORE';
427 local $SIG{TERM} = 'IGNORE';
428 local $SIG{TSTP} = 'IGNORE';
429 local $SIG{PIPE} = 'IGNORE';
431 my $oldAutoCommit = $FS::UID::AutoCommit;
432 local $FS::UID::AutoCommit = 0;
435 #save off and blank values that move to cust_contact / prospect_contact now
436 my $prospectnum = $self->prospectnum;
437 $self->prospectnum('');
438 my $custnum = $self->custnum;
439 $self->custnum(''); $old->custnum(''); # remove because now stored cust_contact
442 for (qw( classnum comment selfservice_access invoice_dest message_dest )) {
443 $link_hash{$_} = $self->get($_);
444 $old->$_(''); ##remove values from old record, causes problem with history
448 ## check for an existing contact with this email address other than current customer
449 ## if found, just add that contact to cust_contact with link_hash credentials
450 ## as email can not be tied to two contacts.
451 my @contact_emails = ();
452 my @contact_nums = ($self->contactnum,);
453 if ( $self->get('emailaddress') =~ /\S/ ) {
455 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
457 my $contact_email = qsearchs('contact_email', { emailaddress=>$email } );
458 unless ($contact_email) { push @contact_emails, $email; next; }
460 my $contact = $contact_email->contact;
461 if ($contact->contactnum eq $self->contactnum) {
462 push @contact_emails, $email;
465 push @contact_nums, $contact->contactnum;
470 my $emails = join(' , ', @contact_emails);
471 $self->emailaddress($emails);
475 my $error = $self->SUPER::replace($old);
476 if ( $old->_password ne $self->_password ) {
477 $error ||= $self->insert_password_history;
480 $dbh->rollback if $oldAutoCommit;
484 my $cust_contact = '';
485 # if $self->custnum was set, then the customer-specific properties
486 # (custnum, classnum, invoice_dest, selfservice_access, comment) are in
487 # pseudo-fields, and are now in %link_hash. otherwise, ignore all those
491 foreach my $contactnum (@contact_nums) {
493 my %hash = ( 'contactnum' => $contactnum, #$self->contactnum,
494 'custnum' => $custnum,
497 if ( $cust_contact = qsearchs('cust_contact', \%hash ) ) {
498 $cust_contact->$_($link_hash{$_}) for keys %link_hash;
499 $error = $cust_contact->replace;
501 $cust_contact = new FS::cust_contact { %hash, %link_hash };
502 $error = $cust_contact->insert;
505 $dbh->rollback if $oldAutoCommit;
511 if ( $prospectnum ) {
512 my %hash = ( 'contactnum' => $self->contactnum,
513 'prospectnum' => $prospectnum,
516 if ( my $prospect_contact = qsearchs('prospect_contact', \%hash ) ) {
517 $prospect_contact->$_($link_hash{$_}) for keys %link_hash;
518 $error = $prospect_contact->replace;
520 my $prospect_contact = new FS::prospect_contact { %hash, %link_hash };
521 $error = $prospect_contact->insert;
524 $dbh->rollback if $oldAutoCommit;
529 foreach my $pf ( grep { /^phonetypenum(\d+)$/ }
530 keys %{ $self->hashref } ) {
531 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
532 my $phonetypenum = $1;
534 my %cp = ( 'contactnum' => $self->contactnum,
535 'phonetypenum' => $phonetypenum,
537 my $contact_phone = qsearchs('contact_phone', \%cp);
539 my $pv = $self->get($pf);
542 #if new value is empty, delete old entry
544 if ($contact_phone) {
545 $error = $contact_phone->delete;
547 $dbh->rollback if $oldAutoCommit;
554 $contact_phone ||= new FS::contact_phone \%cp;
556 my %cpd = _parse_phonestring( $pv );
557 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
559 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
561 $error = $contact_phone->$method;
563 $dbh->rollback if $oldAutoCommit;
568 if ( defined($self->hashref->{'emailaddress'}) ) {
570 my %contact_emails = ();
571 foreach my $contact_email ( $self->contact_email ) {
572 $contact_emails{$contact_email->emailaddress} = '1';
575 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
577 unless ($contact_emails{$email}) {
578 my $contact_email = new FS::contact_email {
579 'contactnum' => $self->contactnum,
580 'emailaddress' => $email,
582 $error = $contact_email->insert;
584 $dbh->rollback if $oldAutoCommit;
588 else { delete($contact_emails{$email}); }
592 foreach my $contact_email ( $self->contact_email ) {
593 if ($contact_emails{$contact_email->emailaddress}) {
594 my $error = $contact_email->delete;
596 $dbh->rollback if $oldAutoCommit;
604 unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) {
605 #warn " queueing fuzzyfiles update\n"
607 $error = $self->queue_fuzzyfiles_update;
609 $dbh->rollback if $oldAutoCommit;
610 return "updating fuzzy search cache: $error";
614 if ( $cust_contact and (
615 ( $cust_contact->selfservice_access eq ''
616 && $link_hash{selfservice_access}
617 && ! length($self->_password)
619 || $cust_contact->_resend()
623 my $error = $self->send_reset_email( queue=>1 );
625 $dbh->rollback if $oldAutoCommit;
630 if ( $self->get('password') ) {
631 my $error = $self->is_password_allowed($self->get('password'))
632 || $self->change_password($self->get('password'));
634 $dbh->rollback if $oldAutoCommit;
639 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
645 =item _parse_phonestring PHONENUMBER_STRING
647 Subroutine, takes a string and returns a list (suitable for assigning to a hash)
648 with keys 'countrycode', 'phonenum' and 'extension'
650 (Should probably be moved to contact_phone.pm, hence the initial underscore.)
654 sub _parse_phonestring {
657 my($countrycode, $extension) = ('1', '');
660 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
666 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
670 ( 'countrycode' => $countrycode,
671 'phonenum' => $value,
672 'extension' => $extension,
676 =item queue_fuzzyfiles_update
678 Used by insert & replace to update the fuzzy search cache
682 use FS::cust_main::Search;
683 sub queue_fuzzyfiles_update {
686 local $SIG{HUP} = 'IGNORE';
687 local $SIG{INT} = 'IGNORE';
688 local $SIG{QUIT} = 'IGNORE';
689 local $SIG{TERM} = 'IGNORE';
690 local $SIG{TSTP} = 'IGNORE';
691 local $SIG{PIPE} = 'IGNORE';
693 my $oldAutoCommit = $FS::UID::AutoCommit;
694 local $FS::UID::AutoCommit = 0;
697 foreach my $field ( 'first', 'last' ) {
698 my $queue = new FS::queue {
699 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
701 my @args = "contact.$field", $self->get($field);
702 my $error = $queue->insert( @args );
704 $dbh->rollback if $oldAutoCommit;
705 return "queueing job (transaction rolled back): $error";
709 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
716 Checks all fields to make sure this is a valid contact. If there is
717 an error, returns the error, otherwise returns false. Called by the insert
725 if ( $self->selfservice_access eq 'R' || $self->selfservice_access eq 'P' ) {
726 $self->selfservice_access('Y');
731 $self->ut_numbern('contactnum')
732 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
733 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
734 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
735 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
736 || $self->ut_namen('last')
737 || $self->ut_namen('first')
738 || $self->ut_textn('title')
739 || $self->ut_textn('comment')
740 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
741 || $self->ut_textn('_password')
742 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
743 || $self->ut_enum('disabled', [ '', 'Y' ])
745 return $error if $error;
747 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
749 return "One of first name, last name, or title must have a value"
750 if ! grep $self->$_(), qw( first last title);
757 Returns a formatted string representing this contact, including name, title and
764 my $data = $self->first. ' '. $self->last;
765 $data .= ', '. $self->title
767 $data .= ' ('. $self->comment. ')'
774 Returns a formatted string representing this contact, with just the name.
780 $self->first . ' ' . $self->last;
783 #=item contact_classname PROSPECT_OBJ | CUST_MAIN_OBJ
785 #Returns the name of this contact's class for the specified prospect or
786 #customer (see L<FS::prospect_contact>, L<FS::cust_contact> and
787 #L<FS::contact_class>).
791 #sub contact_classname {
792 # my( $self, $prospect_or_cust ) = @_;
795 # if ( ref($prospect_or_cust) eq 'FS::prospect_main' ) {
796 # $link = qsearchs('prospect_contact', {
797 # 'contactnum' => $self->contactnum,
798 # 'prospectnum' => $prospect_or_cust->prospectnum,
800 # } elsif ( ref($prospect_or_cust) eq 'FS::cust_main' ) {
801 # $link = qsearchs('cust_contact', {
802 # 'contactnum' => $self->contactnum,
803 # 'custnum' => $prospect_or_cust->custnum,
806 # croak "$prospect_or_cust is not an FS::prospect_main or FS::cust_main object";
809 # my $contact_class = $link->contact_class or return '';
810 # $contact_class->classname;
813 #autoloaded by FK in 4.x, but not during the upgrade
816 qsearch('contact_email', { 'contactnum' => $self->contactnum } );
819 =item by_selfservice_email EMAILADDRESS
821 Alternate search constructor (class method). Given an email address, returns
822 the contact for that address. If that contact doesn't have selfservice access,
823 or there isn't one, returns the empty string.
827 sub by_selfservice_email {
828 my($class, $email) = @_;
830 my $contact_email = qsearchs({
831 'table' => 'contact_email',
832 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
833 'hashref' => { 'emailaddress' => $email, },
835 AND ( contact.disabled IS NULL )
836 AND EXISTS ( SELECT 1 FROM cust_contact
837 WHERE contact.contactnum = cust_contact.contactnum
838 AND cust_contact.selfservice_access = 'Y'
843 $contact_email->contact;
847 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
848 # and should maybe be libraried in some way for other password needs
850 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
852 sub authenticate_password {
853 my($self, $check_password) = @_;
855 if ( $self->_password_encoding eq 'bcrypt' ) {
857 my( $cost, $salt, $hash ) = split(',', $self->_password);
859 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
861 salt => de_base64($salt),
867 $hash eq $check_hash;
871 return 0 if $self->_password eq '';
873 $self->_password eq $check_password;
879 =item change_password NEW_PASSWORD
881 Changes the contact's selfservice access password to NEW_PASSWORD. This does
882 not check password policy rules (see C<is_password_allowed>) and will return
883 an error only if editing the record fails for some reason.
885 If NEW_PASSWORD is the same as the existing password, this does nothing.
889 sub change_password {
890 my($self, $new_password) = @_;
892 # do nothing if the password is unchanged
893 return if $self->authenticate_password($new_password);
895 $self->change_password_fields( $new_password );
901 sub change_password_fields {
902 my($self, $new_password) = @_;
904 $self->_password_encoding('bcrypt');
908 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
910 my $hash = bcrypt_hash( { key_nul => 1,
918 join(',', $cost, en_base64($salt), en_base64($hash) )
923 # end of false laziness w/FS/FS/Auth/internal.pm
926 #false laziness w/ClientAPI/MyAccount/reset_passwd
927 use Digest::SHA qw(sha512_hex);
929 use FS::ClientAPI_SessionCache;
930 sub send_reset_email {
931 my( $self, %opt ) = @_;
933 my @contact_email = $self->contact_email or return '';
935 my $reset_session = {
936 'contactnum' => $self->contactnum,
937 'svcnum' => $opt{'svcnum'},
941 my $conf = new FS::Conf;
943 ($conf->config('selfservice-password_reset_hours') || 24 ). ' hours';
945 my $reset_session_id;
947 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
948 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
951 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
956 my @cust_contact = grep $_->selfservice_access, $self->cust_contact;
957 $cust_main = $cust_contact[0]->cust_main if scalar(@cust_contact) == 1;
959 my $agentnum = $cust_main ? $cust_main->agentnum : '';
960 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $agentnum);
961 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
962 return "selfservice-password_reset_msgnum unset" unless $msgnum;
963 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
964 return "selfservice-password_reset_msgnum cannot be loaded" unless $msg_template;
966 'to' => join(',', map $_->emailaddress, @contact_email ),
967 'cust_main' => $cust_main,
969 'substitutions' => { 'session_id' => $reset_session_id }
972 if ( $opt{'queue'} ) { #or should queueing just be the default?
974 my $cust_msg = $msg_template->prepare( %msg_template );
975 my $error = $cust_msg->insert;
976 return $error if $error;
977 my $queue = new FS::queue {
978 'job' => 'FS::cust_msg::process_send',
979 'custnum' => $cust_main ? $cust_main->custnum : '',
981 $queue->insert( $cust_msg->custmsgnum );
985 $msg_template->send( %msg_template );
991 use vars qw( $myaccount_cache );
992 sub myaccount_cache {
994 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
995 'namespace' => 'FS::ClientAPI::MyAccount',
999 =item cgi_contact_fields
1001 Returns a list reference containing the set of contact fields used in the web
1002 interface for one-line editing (i.e. excluding contactnum, prospectnum, custnum
1003 and locationnum, as well as password fields, but including fields for
1004 contact_email and contact_phone records.)
1008 sub cgi_contact_fields {
1011 my @contact_fields = qw(
1012 classnum first last title comment emailaddress selfservice_access
1013 invoice_dest message_dest password
1016 push @contact_fields, 'phonetypenum'. $_->phonetypenum
1017 foreach qsearch({table=>'phone_type', order_by=>'weight'});
1023 use FS::upgrade_journal;
1024 sub _upgrade_data { #class method
1025 my ($class, %opts) = @_;
1027 # before anything else, migrate contact.custnum to cust_contact records
1028 unless ( FS::upgrade_journal->is_done('contact_invoice_dest') ) {
1030 local($skip_fuzzyfiles) = 1;
1032 foreach my $contact (qsearch('contact', {})) {
1033 my $error = $contact->replace;
1034 die $error if $error;
1037 FS::upgrade_journal->set_done('contact_invoice_dest');
1041 # always migrate cust_main_invoice records over
1042 local $FS::cust_main::import = 1; # override require_phone and such
1043 my $search = FS::Cursor->new('cust_main_invoice', {});
1045 while (my $cust_main_invoice = $search->fetch) {
1046 my $custnum = $cust_main_invoice->custnum;
1047 my $dest = $cust_main_invoice->dest;
1048 my $cust_main = $cust_main_invoice->cust_main;
1050 if ( $dest =~ /^\d+$/ ) {
1051 my $svc_acct = FS::svc_acct->by_key($dest);
1052 die "custnum $custnum, invoice destination svcnum $svc_acct does not exist\n"
1054 $dest = $svc_acct->email;
1056 push @{ $custnum_dest{$custnum} ||= [] }, $dest;
1058 my $error = $cust_main_invoice->delete;
1060 die "custnum $custnum, cleaning up cust_main_invoice: $error\n";
1064 foreach my $custnum (keys %custnum_dest) {
1065 my $dests = $custnum_dest{$custnum};
1066 my $cust_main = FS::cust_main->by_key($custnum);
1067 my $error = $cust_main->replace( invoicing_list => $dests );
1069 die "custnum $custnum, creating contact: $error\n";
1081 L<FS::Record>, schema.html from the base documentation.