Simplify parameters for duplicate - use class functions
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 7 Jun 2022 21:10:50 +0000 (09:10 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 7 Jun 2022 21:18:11 +0000 (09:18 +1200)
CRM/Contribute/Import/Parser/Contribution.php
tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php

index f6ce337be697006eb9f9d0407df6a9f9365f5fc0..3e087ad68e5632ac8de80911ae4a98ba01d2e228 100644 (file)
@@ -177,7 +177,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         $returnCode = $this->summary($values);
       }
       elseif ($mode == self::MODE_IMPORT) {
-        $returnCode = $this->import($onDuplicate, $values);
+        $returnCode = $this->import($values);
         if ($statusID && (($this->_lineCount % 50) == 0)) {
           $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
         }
@@ -239,7 +239,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         $recordNumber = $this->_lineCount;
         array_unshift($values, $recordNumber);
         $this->_duplicates[] = $values;
-        if ($onDuplicate != self::DUPLICATE_SKIP) {
+        if (!$this->isSkipDuplicates()) {
           $this->_validCount++;
         }
       }
@@ -632,8 +632,6 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
   /**
    * Handle the values in import mode.
    *
-   * @param int $onDuplicate
-   *   The code for what action to take on duplicates.
    * @param array $values
    *   The array of values belonging to this line.
    *
@@ -647,7 +645,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
    *   - CRM_Import_Parser::SOFT_CREDIT (successful creation)
    *   - CRM_Import_Parser::PLEDGE_PAYMENT (successful creation)
    */
-  public function import($onDuplicate, &$values) {
+  public function import(&$values) {
     $rowNumber = (int) ($values[array_key_last($values)]);
     try {
       $params = $this->getMappedRow($values);
@@ -667,12 +665,12 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
       }
 
       //import contribution record according to select contact type
-      if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP &&
+      if ($this->isSkipDuplicates() &&
         (!empty($paramValues['contribution_contact_id']) || !empty($paramValues['external_identifier']))
       ) {
         $paramValues['contact_type'] = $this->_contactType;
       }
-      elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
+      elseif ($this->isUpdateExisting() &&
         (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
       ) {
         $paramValues['contact_type'] = $this->_contactType;
@@ -681,11 +679,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         $paramValues['contact_type'] = $this->_contactType;
       }
 
-      //need to pass $onDuplicate to check import mode.
-      if (!empty($paramValues['pledge_payment'])) {
-        $paramValues['onDuplicate'] = $onDuplicate;
-      }
-      $formatError = $this->deprecatedFormatParams($paramValues, $formatted, TRUE, $onDuplicate);
+      $formatError = $this->deprecatedFormatParams($paramValues, $formatted);
 
       if ($formatError) {
         array_unshift($values, $formatError['error_message']);
@@ -698,7 +692,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         throw new CRM_Core_Exception('', CRM_Import_Parser::ERROR);
       }
 
-      if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
+      if ($this->isUpdateExisting()) {
         //fix for CRM-2219 - Update Contribution
         // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
         if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) {
@@ -796,10 +790,8 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
           $newContribution = civicrm_api('contribution', 'create', $formatted);
           if (civicrm_error($newContribution)) {
             if (is_array($newContribution['error_message'])) {
-              array_unshift($values, $newContribution['error_message']['message']);
               if ($newContribution['error_message']['params'][0]) {
-                $this->setImportStatus($rowNumber, 'DUPLICATE', $newContribution['error_message']['message']);
-                return CRM_Import_Parser::DUPLICATE;
+                throw new CRM_Core_Exception($newContribution['error_message']['message'], CRM_Import_Parser::DUPLICATE);
               }
             }
             else {
@@ -862,10 +854,8 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
       $newContribution = civicrm_api('contribution', 'create', $formatted);
       if (civicrm_error($newContribution)) {
         if (is_array($newContribution['error_message'])) {
-          array_unshift($values, $newContribution['error_message']['message']);
           if ($newContribution['error_message']['params'][0]) {
-            $this->setImportStatus($rowNumber, 'DUPLICATE', '');
-            return CRM_Import_Parser::DUPLICATE;
+            throw new CRM_Core_Exception('', CRM_Import_Parser::DUPLICATE);
           }
         }
         else {
@@ -886,8 +876,8 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
     }
     catch (CRM_Core_Exception $e) {
       array_unshift($values, $e->getMessage());
-      $errorMapping = ['soft_credit' => self::SOFT_CREDIT_ERROR, 'pledge_payment' => self::PLEDGE_PAYMENT_ERROR];
-      $this->setImportStatus($rowNumber, $errorMapping[$e->getErrorCode()] ?? CRM_Import_Parser::ERROR, $e->getMessage());
+      $errorMapping = [self::SOFT_CREDIT_ERROR => 'soft_credit_error', self::PLEDGE_PAYMENT_ERROR => 'pledge_payment_error', CRM_Import_Parser::DUPLICATE => 'DUPLICATE'];
+      $this->setImportStatus($rowNumber, $errorMapping[$e->getErrorCode()] ?? 'ERROR', $e->getMessage());
       return $errorMapping[$e->getErrorCode()] ?? CRM_Import_Parser::ERROR;
     }
   }
@@ -965,12 +955,11 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
    * @param array $values
    *   The reformatted properties that we can use internally.
    * @param bool $create
-   * @param int $onDuplicate
    *
    * @return array|CRM_Error
    * @throws \CRM_Core_Exception
    */
-  private function deprecatedFormatParams($params, &$values, $create = FALSE, $onDuplicate = NULL) {
+  private function deprecatedFormatParams($params, &$values, $create = FALSE) {
     require_once 'CRM/Utils/DeprecatedUtils.php';
     // copy all the contribution fields as is
     require_once 'api/v3/utils.php';
@@ -1063,18 +1052,12 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
             }
           }
           else {
-            if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
+            if ($this->isUpdateExisting()) {
               return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
             }
           }
           break;
 
-        case 'currency':
-          if (!CRM_Utils_Rule::currencyCode($value)) {
-            return civicrm_api3_create_error("currency not a valid code: $value");
-          }
-          break;
-
         case 'soft_credit':
           // import contribution record according to select contact type
           // validate contact id and external identifier.
@@ -1103,7 +1086,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
           // retrieve pledge details as well as to validate pledge ID
 
           // first need to check for update mode
-          if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
+          if ($this->isUpdateExisting() &&
             ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id'])
           ) {
             $contribution = new CRM_Contribute_DAO_Contribution();
index 8142ec86c8fae29e3d87eb6697a1770451efae25..49c3282e8f25f1d57f53f72367af5323bea4bac2 100644 (file)
@@ -324,7 +324,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     ]));
     $parser->init();
 
-    $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
+    $this->assertEquals($expectedResult, $parser->import($values), 'Return code from parser import was not as expected');
   }
 
   /**