From da863bce6c626f566e5e33e5a8896f6ae4aba470 Mon Sep 17 00:00:00 2001 From: Christopher Burger Date: Tue, 8 Jan 2019 16:43:34 -0500 Subject: [PATCH] RT# 81853 - updated contact replace to only add or delete email if it is changed. --- FS/FS/contact.pm | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/FS/FS/contact.pm b/FS/FS/contact.pm index d6869abc8..a2fc99c34 100644 --- a/FS/FS/contact.pm +++ b/FS/FS/contact.pm @@ -534,30 +534,38 @@ sub replace { if ( defined($self->hashref->{'emailaddress'}) ) { - #ineffecient but whatever, how many email addresses can there be? - + my %contact_emails = (); foreach my $contact_email ( $self->contact_email ) { - my $error = $contact_email->delete; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; - } + $contact_emails{$contact_email->emailaddress} = '1'; } foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) { - my $contact_email = new FS::contact_email { - 'contactnum' => $self->contactnum, - 'emailaddress' => $email, - }; - $error = $contact_email->insert; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; + unless ($contact_emails{$email}) { + my $contact_email = new FS::contact_email { + 'contactnum' => $self->contactnum, + 'emailaddress' => $email, + }; + $error = $contact_email->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } } + else { delete($contact_emails{$email}); } } + foreach my $contact_email ( $self->contact_email ) { + if ($contact_emails{$contact_email->emailaddress}) { + my $error = $contact_email->delete; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } + } + } unless ( $skip_fuzzyfiles ) { #unless ( $import || $skip_fuzzyfiles ) { -- 2.11.0