diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index f6ce337be697..04d7a9361e41 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -177,7 +177,7 @@ public function run( $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 @@ public function run( $recordNumber = $this->_lineCount; array_unshift($values, $recordNumber); $this->_duplicates[] = $values; - if ($onDuplicate != self::DUPLICATE_SKIP) { + if (!$this->isSkipDuplicates()) { $this->_validCount++; } } @@ -647,7 +647,7 @@ public function summary(&$values) { * - 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 +667,12 @@ public function import($onDuplicate, &$values) { } //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 +681,7 @@ public function import($onDuplicate, &$values) { $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 +694,7 @@ public function import($onDuplicate, &$values) { 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 +792,8 @@ public function import($onDuplicate, &$values) { $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 +856,8 @@ public function import($onDuplicate, &$values) { $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 +878,8 @@ public function import($onDuplicate, &$values) { } 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 +957,11 @@ public function formatInput(&$params, &$formatted = []) { * @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 +1054,12 @@ private function deprecatedFormatParams($params, &$values, $create = FALSE, $onD } } 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 +1088,7 @@ private function deprecatedFormatParams($params, &$values, $create = FALSE, $onD // 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(); diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 8142ec86c8fa..49c3282e8f25 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -324,7 +324,7 @@ protected function runImport(array $originalValues, int $onDuplicateAction, ?int ])); $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'); } /**