CRM-17339 fix - Error when importing contributions matched on name and email
authormonishdeb <monish.deb@webaccessglobal.com>
Tue, 6 Oct 2015 15:22:36 +0000 (20:52 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Mon, 26 Oct 2015 10:21:30 +0000 (15:51 +0530)
https://issues.civicrm.org/jira/browse/CRM-17339

CRM/Utils/DeprecatedUtils.php

index 297965a127f19ecde595f02a2009523c7fca4c15..056e1b403f7becdc6ba594428239df345a33eb4e 100644 (file)
@@ -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;