Skip to content

Commit

Permalink
Simplify parameters for duplicate - use class functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 7, 2022
1 parent 01f12b3 commit de41a63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
43 changes: 14 additions & 29 deletions CRM/Contribute/Import/Parser/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++;
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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']);
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit de41a63

Please sign in to comment.