From 63bf48e9e724a55c3e045fdd6d87beb37a0f6c4b Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 8 Jun 2022 09:10:50 +1200 Subject: [PATCH] Simplify parameters for duplicate - use class functions --- CRM/Contribute/Import/Parser/Contribution.php | 45 ++++++------------- .../Import/Parser/ContributionTest.php | 2 +- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index f6ce337be6..3e087ad68e 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -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(); diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 8142ec86c8..49c3282e8f 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -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'); } /** -- 2.25.1