diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 41a95ebadfc1..1980d227d4df 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -12,6 +12,7 @@ use Civi\Api4\Contact; use Civi\Api4\RelationshipType; use Civi\Api4\StateProvince; +use Civi\Api4\DedupeRuleGroup; require_once 'api/v3/utils.php'; @@ -1628,7 +1629,19 @@ protected function lookupContactID(array $params, bool $isMainContact): ?int { if (isset($params['relationship'])) { unset($params['relationship']); } - $id = $this->getPossibleContactMatch($params, $extIDMatch, $this->getSubmittedValue('dedupe_rule_id') ?: NULL); + $ruleId = $this->getSubmittedValue('dedupe_rule_id') ?: NULL; + // if this is not the main contact and the contact types are not the same + $mainContactType = $this->getSubmittedValue('contactType'); + if (!$isMainContact && $params['contact_type'] !== $mainContactType) { + // use the unsupervised dedupe rule for this contact type + $ruleId = DedupeRuleGroup::get(FALSE) + ->addSelect('id') + ->addWhere('contact_type', '=', $params['contact_type']) + ->addWhere('used', '=', 'Unsupervised') + ->execute() + ->first()['id']; + } + $id = $this->getPossibleContactMatch($params, $extIDMatch, $ruleId); if ($id && $isMainContact && $this->isSkipDuplicates()) { throw new CRM_Core_Exception(ts('Contact matched by dedupe rule already exists in the database.'), CRM_Import_Parser::DUPLICATE); } diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 1ba7d2ca852b..b78b6c7456a1 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -17,6 +17,7 @@ use Civi\Api4\Address; use Civi\Api4\Contact; use Civi\Api4\ContactType; +use Civi\Api4\DedupeRuleGroup; use Civi\Api4\Email; use Civi\Api4\Group; use Civi\Api4\GroupContact; @@ -79,13 +80,21 @@ public function testImportParserWithEmployeeOfRelationship(): void { $contactImportValues = [ 'first_name' => 'Alok', 'last_name' => 'Patel', + 'email' => 'alok@email.com', 'Employee of' => 'Agileware', ]; $values = array_values($contactImportValues); + $ruleGroupId = DedupeRuleGroup::get(FALSE) + ->addSelect('id') + ->addWhere('contact_type', '=', 'Individual') + ->addWhere('used', '=', 'Unsupervised') + ->execute() + ->first()['id']; $userJobID = $this->getUserJobID([ - 'mapper' => [['first_name'], ['last_name'], ['5_a_b', 'organization_name']], + 'mapper' => [['first_name'], ['last_name'], ['email'], ['5_a_b', 'organization_name']], 'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE, + 'dedupe_rule_id' => $ruleGroupId, ]); $this->importValues($userJobID, $values, 'IMPORTED');