[REF] Extract code to delete existing soft credit
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 8 Sep 2022 12:05:06 +0000 (00:05 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 8 Sep 2022 21:02:05 +0000 (09:02 +1200)
CRM/Contribute/Import/Parser/Contribution.php

index dcca26129b380068d14f900c0e33acb9cb81bd8a..439779c861cea491f750c23b840c51120d3c4973 100644 (file)
@@ -403,27 +403,9 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
 
       $this->deprecatedFormatParams($paramValues, $formatted);
 
-      if ($this->isUpdateExisting()) {
+      if (!empty($params['soft_credit']) && $this->isUpdateExisting()) {
         //need to check existing soft credit contribution, CRM-3968
-        if (!empty($formatted['soft_credit'])) {
-          $dupeSoftCredit = [
-            'contact_id' => $formatted['soft_credit'],
-            'contribution_id' => $paramValues['id'],
-          ];
-
-          //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
-          $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']);
-          if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) {
-            foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) {
-              if (!empty($existingSoftCreditValues['soft_credit_id'])) {
-                civicrm_api3('ContributionSoft', 'delete', [
-                  'id' => $existingSoftCreditValues['soft_credit_id'],
-                  'pcp_id' => NULL,
-                ]);
-              }
-            }
-          }
-        }
+        $this->deleteExistingSoftCredit($contributionParams['id']);
       }
 
       $contributionID = civicrm_api3('contribution', 'create', $formatted)['id'];
@@ -432,7 +414,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         $this->processNote($contributionID, $contactID, $entityKeyedParams['Note']);
       }
       //return soft valid since we need to show how soft credits were added
-      if (!empty($formatted['soft_credit'])) {
+      if (!empty($params['soft_credit'])) {
         $this->setImportStatus($rowNumber, $this->getStatus(self::SOFT_CREDIT), '', $contributionID);
         return;
       }
@@ -641,6 +623,26 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
     ];
   }
 
+  /**
+   * @param int $contributionID
+   *
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function deleteExistingSoftCredit(int $contributionID): void {
+    //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
+    $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contributionID);
+    if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) {
+      foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) {
+        if (!empty($existingSoftCreditValues['soft_credit_id'])) {
+          civicrm_api3('ContributionSoft', 'delete', [
+            'id' => $existingSoftCreditValues['soft_credit_id'],
+            'pcp_id' => NULL,
+          ]);
+        }
+      }
+    }
+  }
+
   /**
    * Lookup matching contact.
    *