CRM-17339 fix - Error when importing contributions matched on email
authormonishdeb <monish.deb@webaccessglobal.com>
Thu, 5 Nov 2015 09:58:56 +0000 (15:28 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Thu, 5 Nov 2015 09:58:56 +0000 (15:28 +0530)
https://issues.civicrm.org/jira/browse/CRM-17339

CRM/Utils/DeprecatedUtils.php

index 2f01ac8e7ef22f68fcb1f8e8a3ca5a25dad2c6d8..ce2002e8129df7e6321ed619fcd2c5239e6ebc71 100644 (file)
@@ -311,20 +311,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();
-        // 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'];
-          }
+        $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 ($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
@@ -351,9 +372,6 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F
           if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
             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.");
-          }
         }
         break;