Skip to content

Commit

Permalink
Merge pull request #24166 from lemniscus/contribution-import-with-ext…
Browse files Browse the repository at this point in the history
…ernal-id

dev/core#3784 fix contribution/membership/participant import matching on external id or contact id
  • Loading branch information
seamuslee001 authored Aug 9, 2022
2 parents 1f08219 + a2f1966 commit d243fc2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CRM/Contribute/Import/Parser/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public function import($values): void {
$error = $this->checkContactDuplicate($paramValues);

if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
$matchedIDs = (array) $error['error_message']['params'];
if (count($matchedIDs) > 1) {
throw new CRM_Core_Exception('Multiple matching contact records detected for this row. The contribution was not imported', CRM_Import_Parser::ERROR);
}
Expand Down Expand Up @@ -776,7 +776,7 @@ private function deprecatedFormatParams($params, &$values, $create = FALSE) {
$error = $this->checkContactDuplicate($params);

if (isset($error['error_message']['params'][0])) {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
$matchedIDs = (array) $error['error_message']['params'];

// check if only one contact is found
if (count($matchedIDs) > 1) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/Import/Parser/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function import(&$values) {
$error = $this->checkContactDuplicate($formatValues);

if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
$matchedIDs = (array) $error['error_message']['params'];
if (count($matchedIDs) >= 1) {
foreach ($matchedIDs as $contactId) {
$formatted['contact_id'] = $contactId;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function import($values) {
$error = $this->checkContactDuplicate($formatValues);

if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
$matchedIDs = (array) $error['error_message']['params'];
if (count($matchedIDs) > 1) {
throw new CRM_Core_Exception('Multiple matching contact records detected for this row. The membership was not imported', CRM_Import_Parser::ERROR);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,49 @@ public function testImport() :void {
$this->assertEquals('No matching Contact found for ([email protected] )', $row['_status_message']);
}

public function testImportWithMatchByExternalIdentifier() :void {
CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_contact AUTO_INCREMENT = 1000000");

$contactRubyParams = [
'first_name' => 'Ruby',
'external_identifier' => 'ruby',
'contact_type' => 'Individual',
];
$contactSapphireParams = [
'first_name' => 'Sapphire',
'external_identifier' => 'sapphire',
'contact_type' => 'Individual',
];
$contactRubyId = $this->individualCreate($contactRubyParams);
$contactSapphireId = $this->individualCreate($contactSapphireParams);

// make sure we're testing dev/core#3784
self::assertEquals(1, substr($contactRubyId, 0, 1));
self::assertEquals(1, substr($contactSapphireId, 0, 1));

$mapping = [
['name' => 'external_identifier'],
['name' => 'total_amount'],
['name' => 'receive_date'],
['name' => 'financial_type_id'],
];
$this->importCSV('contributions_match_external_id.csv', $mapping);

$contributionsOfRuby = Contribution::get()
->addWhere('contact_id', '=', $contactRubyId)->execute();
$contributionsOfSapphire = Contribution::get()
->addWhere('contact_id', '=', $contactSapphireId)->execute();

$this->assertCount(1, $contributionsOfRuby, 'Wrong number of contributions imported');
$this->assertCount(1, $contributionsOfSapphire, 'Wrong number of contributions imported');
$this->assertEquals(22222, $contributionsOfRuby->first()['total_amount']);
$this->assertEquals(5, $contributionsOfSapphire->first()['total_amount']);

$dataSource = new CRM_Import_DataSource_CSV($this->userJobID);
$this->assertEquals(0, $dataSource->getRowCount([CRM_Import_Parser::ERROR]));
$this->assertEquals(2, $dataSource->getRowCount([CRM_Import_Parser::VALID]));
}

/**
* Run the import parser.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
External Identifier,Total Amount,Receive Date,Financial Type
sapphire,5,2005-05-05,Donation
ruby,22222,2022-02-22,Donation

0 comments on commit d243fc2

Please sign in to comment.