+ #save off and blank values that move to cust_contact / prospect_contact now
+ my $prospectnum = $self->prospectnum;
+ $self->prospectnum('');
+ my $custnum = $self->custnum;
+ $self->custnum('');
+
+ my %link_hash = ();
+ for (qw( classnum comment selfservice_access invoice_dest )) {
+ $link_hash{$_} = $self->get($_);
+ $self->$_('');
+ }
+
+ #look for an existing contact with this email address
+ my $existing_contact = '';
+ if ( $self->get('emailaddress') =~ /\S/ ) {
+
+ my %existing_contact = ();
+
+ foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
+
+ my $contact_email = qsearchs('contact_email', { emailaddress=>$email } )
+ or next;
+
+ my $contact = $contact_email->contact;
+ $existing_contact{ $contact->contactnum } = $contact;
+
+ }
+
+ if ( scalar( keys %existing_contact ) > 1 ) {
+ $dbh->rollback if $oldAutoCommit;
+ return 'Multiple email addresses specified '.
+ ' that already belong to separate contacts';
+ } elsif ( scalar( keys %existing_contact ) ) {
+ ($existing_contact) = values %existing_contact;
+ }
+
+ }
+
+ my $error;
+ if ( $existing_contact ) {
+
+ $self->$_($existing_contact->$_())
+ for qw( contactnum _password _password_encoding );
+ $error = $self->SUPER::replace($existing_contact);
+
+ } else {
+
+ $error = $self->SUPER::insert;
+
+ }
+
+ $error ||= $self->insert_password_history;
+