Switch out the switch
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 19 Mar 2023 00:46:47 +0000 (13:46 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 19 Mar 2023 20:44:13 +0000 (09:44 +1300)
CRM/Contribute/Import/Parser/Contribution.php

index e85444c370f8be7b0db1211868aba392f243e41c..fcbc856cafadc1624df9f7a959ae9ca278275ce2 100644 (file)
@@ -533,60 +533,50 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
   private function deprecatedFormatParams($params, &$values, $create = FALSE): void {
     // copy all the contribution fields as is
     require_once 'api/v3/utils.php';
-
-    foreach ($params as $key => $value) {
-      // ignore empty values or empty arrays etc
-      if (CRM_Utils_System::isNull($value)) {
-        continue;
+    if (empty($params['pledge_id'])) {
+      return;
+    }
+    // get total amount of from import fields
+    $totalAmount = $params['total_amount'] ?? NULL;
+    $contributionContactID = $params['contact_id'];
+    // we need to get contact id $contributionContactID to
+    // retrieve pledge details as well as to validate pledge ID
+
+    // first need to check for update mode
+    if (!empty($params['id'])) {
+      $contribution = new CRM_Contribute_DAO_Contribution();
+      if ($params['id']) {
+        $contribution->id = $params['id'];
       }
 
-      switch ($key) {
-
-        case 'pledge_id':
-          // get total amount of from import fields
-          $totalAmount = $params['total_amount'] ?? NULL;
-          $contributionContactID = $params['contact_id'];
-          // we need to get contact id $contributionContactID to
-          // retrieve pledge details as well as to validate pledge ID
-
-          // first need to check for update mode
-          if (!empty($params['id'])) {
-            $contribution = new CRM_Contribute_DAO_Contribution();
-            if ($params['id']) {
-              $contribution->id = $params['id'];
-            }
-
-            if ($contribution->find(TRUE)) {
-              if (!$totalAmount) {
-                $totalAmount = $contribution->total_amount;
-              }
-            }
-            else {
-              throw new CRM_Core_Exception('No match found for specified contact in pledge payment data. Row was skipped.', CRM_Import_Parser::ERROR);
-            }
-          }
-
-          if (!empty($params['pledge_id'])) {
-            if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
-              throw new CRM_Core_Exception('Invalid Pledge ID provided. Contribution row was skipped.', CRM_Import_Parser::ERROR);
-            }
-            $values['pledge_id'] = $params['pledge_id'];
-          }
-
-          // we need to check if oldest payment amount equal to contribution amount
-          require_once 'CRM/Pledge/BAO/PledgePayment.php';
-          $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']);
-
-          if ($pledgePaymentDetails['amount'] == $totalAmount) {
-            $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
-          }
-          else {
-            throw new CRM_Core_Exception('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', CRM_Import_Parser::ERROR);
-          }
-          break;
+      if ($contribution->find(TRUE)) {
+        if (!$totalAmount) {
+          $totalAmount = $contribution->total_amount;
+        }
+      }
+      else {
+        throw new CRM_Core_Exception('No match found for specified contact in pledge payment data. Row was skipped.', CRM_Import_Parser::ERROR);
+      }
+    }
 
+    if (!empty($params['pledge_id'])) {
+      if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
+        throw new CRM_Core_Exception('Invalid Pledge ID provided. Contribution row was skipped.', CRM_Import_Parser::ERROR);
       }
+      $values['pledge_id'] = $params['pledge_id'];
     }
+
+    // we need to check if oldest payment amount equal to contribution amount
+    require_once 'CRM/Pledge/BAO/PledgePayment.php';
+    $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']);
+
+    if ($pledgePaymentDetails['amount'] == $totalAmount) {
+      $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
+    }
+    else {
+      throw new CRM_Core_Exception('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', CRM_Import_Parser::ERROR);
+    }
+
   }
 
   /**