From b23a93ed1f624a351f83ef16f90821d3a936915b Mon Sep 17 00:00:00 2001 From: monishdeb Date: Tue, 6 Oct 2015 20:52:36 +0530 Subject: [PATCH] CRM-17339 fix - Error when importing contributions matched on name and email https://issues.civicrm.org/jira/browse/CRM-17339 --- CRM/Utils/DeprecatedUtils.php | 37 +++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index 297965a127..056e1b403f 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -305,20 +305,41 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F //import contribution record according to select contact type require_once 'CRM/Contact/DAO/Contact.php'; $contactType = new CRM_Contact_DAO_Contact(); + $contactId = CRM_Utils_Array::value('contribution_contact_id', $params); + $externalId = CRM_Utils_Array::value('external_identifier', $params); + $email = CRM_Utils_Array::value('email', $params); //when insert mode check contact id or external identifier - if (!empty($params['contribution_contact_id']) || !empty($params['external_identifier'])) { - if (!empty($params['contribution_contact_id'])) { - $contactType->id = CRM_Utils_Array::value('contribution_contact_id', $params); - } - elseif (!empty($params['external_identifier'])) { - $contactType->external_identifier = $params['external_identifier']; - } + if ($contactId || $externalId) { + $contactType->id = $contactId; + $contactType->external_identifier = $externalId; if ($contactType->find(TRUE)) { if ($params['contact_type'] != $contactType->contact_type) { return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type"); } } } + elseif ($email) { + if (!CRM_Utils_Rule::email($email)) { + return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped"); + } + + // get the contact id from duplicate contact rule, if more than one contact is returned + // we should return error, since current interface allows only one-one mapping + $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']); + $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams); + if (!$checkDedupe['is_error']) { + return civicrm_api3_create_error("Invalid email address(doesn't exist) $email. Row was skipped"); + } + else { + $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]); + if (count($matchingContactIds) > 1) { + return civicrm_api3_create_error("Invalid email address(duplicate) $email. Row was skipped"); + } + elseif (count($matchingContactIds) == 1) { + $params['contribution_contact_id'] = $matchingContactIds[0]; + } + } + } elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) { //when update mode check contribution id or trxn id or //invoice id @@ -346,7 +367,7 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped."); } else { - return civicrm_api3_create_error("Empty Contact and External ID. Row was skipped."); + return civicrm_api3_create_error("No Contact ID, External ID and/or Email provided. Row was skipped."); } } break; -- 2.25.1