RT# 81853 - updated contact replace to only add or delete email if it is changed.
authorChristopher Burger <burgerc@freeside.biz>
Tue, 8 Jan 2019 21:43:34 +0000 (16:43 -0500)
committerChristopher Burger <burgerc@freeside.biz>
Tue, 8 Jan 2019 21:46:48 +0000 (16:46 -0500)
FS/FS/contact.pm

index 094feea..1801545 100644 (file)
@@ -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 ) {