CRM-13833 Soft-Credit Import fix
authormonishdeb <monish.deb@webaccess.co.in>
Sat, 5 Apr 2014 12:53:45 +0000 (18:23 +0530)
committermonishdeb <monish.deb@webaccess.co.in>
Sat, 5 Apr 2014 12:53:45 +0000 (18:23 +0530)
----------------------------------------
* CRM-13833:
  http://issues.civicrm.org/jira/browse/CRM-13833

CRM/Contribute/BAO/Contribution.php
CRM/Contribute/BAO/ContributionSoft.php
CRM/Contribute/Import/Form/MapField.php
CRM/Contribute/Import/Form/Preview.php
CRM/Contribute/Import/Parser.php
CRM/Contribute/Import/Parser/Contribution.php
CRM/Utils/DeprecatedUtils.php
templates/CRM/Contribute/Form/SoftCredit.tpl
templates/CRM/Contribute/Import/Form/MapTable.tpl

index fc136955b48d6994a11089b4530f8cfb20621f18..39aaa173703a1564c34f07d9f9a39e9f19571321 100644 (file)
@@ -319,11 +319,14 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
 
     // Handle soft credit and / or link to personal campaign page
     $softIDs = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id);
+
+    //Delete PCP against this contribution and create new on submitted PCP information
+    $pcpId = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id, TRUE);
+    if ($pcpId) {
+      $deleteParams = array('id' => $pcpId);
+      CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
+    }
     if ($pcp = CRM_Utils_Array::value('pcp', $params)) {
-      if ($pcpId = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id, TRUE)) {
-        $deleteParams = array('id' => $pcpId);
-        CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
-      }
       $softParams = array();
       $softParams['contribution_id'] = $contribution->id;
       $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
index 39436c64091ee6f87b752a7141f818df55619977..decd0954318c875b75702bcb9367e437171fb61e 100644 (file)
@@ -203,6 +203,9 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio
     if ($isPCP) {
       $query .= " AND pcp_id IS NOT NULL";
     }
+    else {
+      $query .= " AND pcp_id IS NULL";
+    }
     $params = array(1 => array($contributionID, 'Integer'));
 
     $dao = CRM_Core_DAO::executeQuery($query, $params);
index 84134ed103fe216a8b5bcd46af91aeeb53823f40..9c9c33be6690950b36e0cf072e2b79bcbac77649 100644 (file)
@@ -187,7 +187,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
       CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization',
     );
 
-    $contactType = $contactTypes[$contactTypeId];
+    $contactType = isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : '';
 
     // get importable fields for contact type
     $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL);
@@ -198,21 +198,13 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
       'used' => 'Unsupervised',
     );
     $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams);
-    $softCreditFields = array();
-    if (is_array($fieldsArray)) {
-      foreach ($fieldsArray as $value) {
-        //skip if there is no dupe rule
-        if ($value == 'none') {
-          continue;
-        }
-        $softCreditFields[$value] = $contactFields[trim($value)]['title'];
-      }
-    }
 
     $softCreditFields['contact_id'] = ts('Contact ID');
     $softCreditFields['external_identifier'] = ts('External Identifier');
 
     $sel2['soft_credit'] = $softCreditFields;
+    $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = CRM_Core_OptionGroup::values('soft_credit_type');
+    $sel4 = NULL;
 
     // end of soft credit section
 
@@ -290,7 +282,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
           );
         }
       }
-      $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : ""));
+      $sel->setOptions(array($sel1, $sel2, $sel3,$sel4));
     }
     $js .= "</script>\n";
     $this->assign('initHideBoxes', $js);
@@ -343,7 +335,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
   static function formRule($fields, $files, $self) {
     $errors = array();
     $fieldMessage = NULL;
-
+    $contactORContributionId = $self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE ? 'contribution_id' : 'contribution_contact_id';
     if (!array_key_exists('savedMapping', $fields)) {
       $importKeys = array();
       foreach ($fields['mapper'] as $mapperPart) {
@@ -358,7 +350,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
       );
       $params = array(
         'used' => 'Unsupervised',
-        'contact_type' => $contactTypes[$contactTypeId],
+        'contact_type' => isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : '',
       );
       list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
       $weightSum = 0;
@@ -366,6 +358,15 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
         if (array_key_exists($val, $ruleFields)) {
           $weightSum += $ruleFields[$val];
         }
+        if ($val == "soft_credit") {
+          $mapperKey = CRM_Utils_Array::key('soft_credit', $importKeys);
+          if (!empty($fields['mapper'][$mapperKey][1])) {
+            if (empty($errors['_qf_default'])) {
+              $errors['_qf_default'] = '';
+            }
+            $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
+          }
+        }
       }
       foreach ($ruleFields as $field => $weight) {
         $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
@@ -373,14 +374,17 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
 
       // FIXME: should use the schema titles, not redeclare them
       $requiredFields = array(
-        'contribution_contact_id' => ts('Contact ID'),
+        $contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'),
         'total_amount' => ts('Total Amount'),
         'financial_type' => ts('Financial Type')
       );
 
       foreach ($requiredFields as $field => $title) {
         if (!in_array($field, $importKeys)) {
-          if ($field == 'contribution_contact_id') {
+          if (empty($errors['_qf_default'])) {
+            $errors['_qf_default'] = '';
+          }
+          if ($field == $contactORContributionId) {
             if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
               $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
             ) {
@@ -397,11 +401,9 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
             }
           }
           else {
-            if ($self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) {
-              $errors['_qf_default'] .= ts('Missing required field: %1', array(
-                  1 => $title
-                )) . '<br />';
-            }
+            $errors['_qf_default'] .= ts('Missing required field: %1', array(
+                1 => $title
+              )) . '<br />';
           }
         }
       }
@@ -442,6 +444,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
         $assignError = new CRM_Core_Page();
         $assignError->assign('mappingDetailsError', $_flag);
       }
+      CRM_Core_Session::setStatus($errors['_qf_default'], ts("Error"), "error");
       return $errors;
     }
 
@@ -471,9 +474,11 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
     $config = CRM_Core_Config::singleton();
     $seperator = $config->fieldSeparator;
 
-    $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = array();
+    $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = array();
     $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
 
+    $softCreditTypes = CRM_Core_OptionGroup::values('soft_credit_type');
+
     for ($i = 0; $i < $this->_columnCount; $i++) {
       $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
       $mapperKeysMain[$i] = $mapperKeys[$i][0];
@@ -482,15 +487,19 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
         $mapperSoftCredit[$i] = $mapperKeys[$i][1];
         list($first, $second) = explode('_', $mapperSoftCredit[$i]);
         $softCreditFields[$i] = ucwords($first . " " . $second);
+        $mapperSoftCreditType[$i] = array(
+          'value' => isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : '',
+          'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ? $softCreditTypes[$mapperKeys[$i][2]] : '',
+        );
       }
       else {
-        $mapperSoftCredit[$i] = $softCreditFields[$i] = NULL;
-        $softCreditFields[$i] = NULL;
+        $mapperSoftCredit[$i] = $softCreditFields[$i] =  $mapperSoftCreditType[$i] = NULL;
       }
     }
 
     $this->set('mapper', $mapper);
     $this->set('softCreditFields', $softCreditFields);
+    $this->set('mapperSoftCreditType', $mapperSoftCreditType);
 
     // store mapping Id to display it in the preview page
     $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
index 6f7985902c2149cf86448edb2de48cf5b30cdf70..5703012fbe0d354f9d345c5ce4976d707813db61 100644 (file)
@@ -49,12 +49,13 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
     $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
 
     //get the data from the session
-    $dataValues       = $this->get('dataValues');
-    $mapper           = $this->get('mapper');
-    $softCreditFields = $this->get('softCreditFields');
-    $invalidRowCount  = $this->get('invalidRowCount');
-    $conflictRowCount = $this->get('conflictRowCount');
-    $mismatchCount    = $this->get('unMatchCount');
+    $dataValues           = $this->get('dataValues');
+    $mapper               = $this->get('mapper');
+    $softCreditFields     = $this->get('softCreditFields');
+    $mapperSoftCreditType = $this->get('mapperSoftCreditType');
+    $invalidRowCount      = $this->get('invalidRowCount');
+    $conflictRowCount     = $this->get('conflictRowCount');
+    $mismatchCount        = $this->get('unMatchCount');
 
     //get the mapping name displayed if the mappingId is set
     $mappingId = $this->get('loadMappingId');
@@ -91,6 +92,7 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
 
     $properties = array(
       'mapper', 'softCreditFields',
+      'mapperSoftCreditType',
       'dataValues', 'columnCount',
       'totalRowCount', 'validRowCount',
       'invalidRowCount', 'conflictRowCount',
@@ -117,6 +119,7 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
     $invalidRowCount  = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate      = $this->get('onDuplicate');
+    $mapperSoftCreditType = $this->get('mapperSoftCreditType');
 
     $config = CRM_Core_Config::singleton();
     $seperator = $config->fieldSeparator;
@@ -128,15 +131,16 @@ class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
 
     foreach ($mapper as $key => $value) {
       $mapperKeys[$key] = $mapper[$key][0];
-      if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit') {
-        $mapperSoftCredit[$key] = $mapper[$key][1];
+      if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
+        $mapperSoftCredit[$key] = isset($mapper[$key][1]) ? $mapper[$key][1] : '';
+        $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
       }
       else {
-        $mapperSoftCredit[$key] = NULL;
+        $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] =  NULL;
       }
     }
 
-    $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType);
+    $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
 
     $mapFields = $this->get('fields');
 
index f6df518de1e2392d8b2aa749f31ec25cb6f005c2..a81b4de3c8ad2c7a3b070e48adf88d12acb63555 100644 (file)
@@ -420,6 +420,11 @@ pppp   * @return void
     }
   }
 
+  function setActiveFieldSoftCreditType($elements) {
+    for ($i = 0; $i < count($elements); $i++) {
+      $this->_activeFields[$i]->_softCreditType = $elements[$i];
+    }
+  }
   /**
    * function to format the field values for input to the api
    *
@@ -434,7 +439,10 @@ pppp   * @return void
           if (!isset($params[$this->_activeFields[$i]->_name])) {
             $params[$this->_activeFields[$i]->_name] = array();
           }
-          $params[$this->_activeFields[$i]->_name][$this->_activeFields[$i]->_softCreditField] = $this->_activeFields[$i]->_value;
+          $params[$this->_activeFields[$i]->_name][$i][$this->_activeFields[$i]->_softCreditField] = $this->_activeFields[$i]->_value;
+          if(isset($this->_activeFields[$i]->_softCreditType)){
+            $params[$this->_activeFields[$i]->_name][$i]['soft_credit_type_id'] = $this->_activeFields[$i]->_softCreditType;
+          }
         }
 
         if (!isset($params[$this->_activeFields[$i]->_name])) {
@@ -554,7 +562,7 @@ pppp   * @return void
 
     foreach ($data as $datum) {
       foreach ($datum as $key => $value) {
-        if (is_array($value[0])) {
+        if (isset($value[0]) && is_array($value)) {
           foreach ($value[0] as $k1 => $v1) {
             if ($k1 == 'location_type_id') {
               continue;
index 3e3491d3ad3d1be1c71076e95f16ad2be1a4fa7e..2d8daaeaad9e1c814e709dd2367e992d78b7912d 100644 (file)
@@ -57,10 +57,11 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
   /**
    * class constructor
    */
-  function __construct(&$mapperKeys, $mapperSoftCredit = NULL, $mapperPhoneType = NULL) {
+  function __construct(&$mapperKeys, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL) {
     parent::__construct();
     $this->_mapperKeys = &$mapperKeys;
     $this->_mapperSoftCredit = &$mapperSoftCredit;
+    $this->_mapperSoftCreditType = &$mapperSoftCreditType;
   }
 
   /**
@@ -108,6 +109,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
 
     $this->setActiveFields($this->_mapperKeys);
     $this->setActiveFieldSoftCredit($this->_mapperSoftCredit);
+    $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType);
 
     // FIXME: we should do this in one place together with Form/MapField.php
     $this->_contactIdIndex = -1;
@@ -312,7 +314,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
       $paramValues['contact_type'] = $this->_contactType;
     }
     elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
-      ($paramValues['contribution_id'] || $values['trxn_id'] || $paramValues['invoice_id'])
+      (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
     ) {
       $paramValues['contact_type'] = $this->_contactType;
     }
@@ -328,7 +330,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
       $paramValues['onDuplicate'] = $onDuplicate;
     }
     require_once 'CRM/Utils/DeprecatedUtils.php';
-    $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE);
+    $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE, $onDuplicate);
 
     if ($formatError) {
       array_unshift($values, $formatError['error_message']);
@@ -351,7 +353,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
     else {
       //fix for CRM-2219 - Update Contribution
       // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
-      if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || $paramValues['contribution_id']) {
+      if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) {
         $dupeIds = array(
           'id' => CRM_Utils_Array::value('contribution_id', $paramValues),
           'trxn_id' => CRM_Utils_Array::value('trxn_id', $paramValues),
@@ -389,16 +391,24 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
           }
 
           //need to check existing soft credit contribution, CRM-3968
-          if (!empty($formatted['soft_credit_to'])) {
+          if (!empty($formatted['soft_credit'])) {
             $dupeSoftCredit = array(
-              'contact_id' => $formatted['soft_credit_to'],
+              'contact_id' => $formatted['soft_credit'],
               'contribution_id' => $ids['contribution'],
             );
 
-            //FIX ME: Need to fix this logic
+            //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
             $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']);
-            if (!empty($existingSoftCredit['soft_credit_id'])) {
-              $formatted['softID'] = $existingSoftCredit['soft_credit_id'];
+            if(isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])){
+              foreach($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues){
+                if (!empty($existingSoftCreditValues['soft_credit_id'])) {
+                  $deleteParams = array(
+                    'id' => $existingSoftCreditValues['soft_credit_id'],
+                    'pcp_id' => NULL,
+                  );
+                  CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
+                }
+              }
             }
           }
 
@@ -406,7 +416,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
           $this->_newContributions[] = $newContribution->id;
 
           //return soft valid since we need to show how soft credits were added
-          if (!empty($formatted['soft_credit_to'])) {
+          if (!empty($formatted['soft_credit'])) {
             return CRM_Contribute_Import_Parser::SOFT_CREDIT;
           }
 
@@ -472,7 +482,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
           $formatted['contribution_id'] = $newContribution['id'];
 
           //return soft valid since we need to show how soft credits were added
-          if (!empty($formatted['soft_credit_to'])) {
+          if (!empty($formatted['soft_credit'])) {
             return CRM_Contribute_Import_Parser::SOFT_CREDIT;
           }
 
@@ -489,7 +499,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
           'used'         => 'Unsupervised',
         );
         $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams);
-
+        $disp = NULL;
         foreach ($fieldsArray as $value) {
           if (array_key_exists(trim($value), $params)) {
             $paramValue = $params[trim($value)];
@@ -543,7 +553,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
       $formatted['contribution_id'] = $newContribution['id'];
 
       //return soft valid since we need to show how soft credits were added
-      if (!empty($formatted['soft_credit_to'])) {
+      if (!empty($formatted['soft_credit'])) {
         return CRM_Contribute_Import_Parser::SOFT_CREDIT;
       }
 
index cdba75d417b7493ed8baaba4d5dccfe94323be80..786e123f727407a35f16cd2f62550c3b10e25d41 100644 (file)
@@ -217,7 +217,7 @@ function _civicrm_api3_deprecated_participant_formatted_param($params, &$values,
  * @return array|CRM_Error
  * @access public
  */
-function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE) {
+function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = Null) {
   // copy all the contribution fields as is
 
   $fields = CRM_Contribute_DAO_Contribution::fields();
@@ -388,61 +388,74 @@ function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = F
         }
         break;
 
-      case 'honor_type_id':
-        require_once 'CRM/Core/OptionGroup.php';
-        $values['honor_type_id'] = CRM_Core_OptionGroup::getValue('honor_type', $value);
-        if (empty($values['honor_type_id'])) {
-          return civicrm_api3_create_error("Honor Type is not valid: $value");
-        }
-        break;
-
       case 'soft_credit':
         //import contribution record according to select contact type
-
         // validate contact id and external identifier.
-        $contactId = CRM_Utils_Array::value('contact_id', $params['soft_credit']);
-        $externalId = CRM_Utils_Array::value('external_identifier', $params['soft_credit']);
-        if ($contactId || $externalId) {
-          require_once 'CRM/Contact/DAO/Contact.php';
-          $contact = new CRM_Contact_DAO_Contact();
-          $contact->id = $contactId;
-          $contact->external_identifier = $externalId;
-
-          $errorMsg = NULL;
-          if (!$contact->find(TRUE)) {
-            $errorMsg = ts("No match found for specified Soft Credit contact data. Row was skipped.");
-          }
-
-          if ($errorMsg) {
-            return civicrm_api3_create_error($errorMsg, 'soft_credit');
-          }
-
-          // finally get soft credit contact id.
-          $values['soft_credit_to'] = $contact->id;
+        $value[$key] = '';
+        if (!isset($params['contribution_id']) && empty($params['contribution_id']) && $onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
+          $errorMsg = ts("Empty Contribution Id. Row was skipped.");
+          return civicrm_api3_create_error($errorMsg, $value[$key]);
         }
-        else {
-          // 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
-
-          $softParams = $params['soft_credit'];
-          $softParams['contact_type'] = $params['contact_type'];
-
-          $error = _civicrm_api3_deprecated_duplicate_formatted_contact($softParams);
+        elseif (!isset($params['contribution_contact_id']) && empty($params['contribution_contact_id']) && $onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) {
+          $errorMsg = ts("Empty Contact Id. Row was skipped.");
+          return civicrm_api3_create_error($errorMsg, $value[$key]);
+        }
+        if (isset($params[$key]) && is_array($params[$key])) {
+          foreach ($params[$key] as $softKey => $softParam) {
+            $contactId = CRM_Utils_Array::value('contact_id', $softParam);
+            $externalId = CRM_Utils_Array::value('external_identifier', $softParam);
+            if (isset($softParam['contact_id']) && !empty($softParam['contact_id'])) {
+              $softCreditContactIds = implode(', ', array_map(function ($entry) {
+                  return $entry['contact_id'];
+              }, $params[$key]));
+            }
+            else {
+              $softCreditContactIds='';
+            }
+            if ($contactId || $externalId) {
+              require_once 'CRM/Contact/DAO/Contact.php';
+              $contact = new CRM_Contact_DAO_Contact();
+              $contact->id = $contactId;
+              $contact->external_identifier = $externalId;
+
+              $errorMsg = NULL;
+              if (!$contact->find(TRUE)) {
+                $errorMsg = (isset($softCreditContactIds) && $softCreditContactIds) ? ts("Invalid ContactId ($softCreditContactIds) specified for Soft Credit contact data. Row was skipped.") : ts("Empty ContactId specified for Soft Credit contact data. Row was skipped.");
+              }
 
-          if (isset($error['error_message']['params'][0])) {
-            $matchedIDs = explode(',', $error['error_message']['params'][0]);
+              if ($errorMsg) {
+                return civicrm_api3_create_error($errorMsg, $value[$key]);
+              }
 
-            // check if only one contact is found
-            if (count($matchedIDs) > 1) {
-              return civicrm_api3_create_error($error['error_message']['message'], 'soft_credit');
+              // finally get soft credit contact id.
+              $values[$key][$softKey] = $softParam;
+              $values[$key][$softKey]['contact_id'] = $contact->id;
             }
             else {
-              $values['soft_credit_to'] = $matchedIDs[0];
+              // 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
+              $softParams = $params['soft_credit'];
+              $softParams['contact_type'] = $params['contact_type'];
+
+              $error = _civicrm_api3_deprecated_duplicate_formatted_contact($softParams);
+
+              if (isset($error['error_message']['params'][0])) {
+                $matchedIDs = explode(',', $error['error_message']['params'][0]);
+
+                // check if only one contact is found
+                if (count($matchedIDs) > 1) {
+                  return civicrm_api3_create_error($error['error_message']['message'], $value[$key]);
+                }
+                else {
+                  $values['soft_credit'] = $matchedIDs[0];
+                }
+              }
+              else {
+                $errorMsg = (isset($softCreditContactIds) && $softCreditContactIds) ? ts("Invalid ContactId ($softCreditContactIds) specified for Soft Credit contact data. Row was skipped.") : ts("Empty ContactId specified for Soft Credit contact data. Row was skipped.");
+                return civicrm_api3_create_error($errorMsg, $value[$key]);
+              }
             }
           }
-          else {
-            return civicrm_api3_create_error('No match found for specified Soft Credit contact data. Row was skipped.', 'soft_credit');
-          }
         }
         break;
 
index b9e762e6f9f4905daf89ced8e9d720edda333f4c..2fe9ff9abed8fd777318a58a60f1b5ea504795e3 100644 (file)
     })
       // This is just a cheap trick to store the name in case of a formrule error
       .on('change', function() {
-        $('[name=pcp_made_through]', $form).val($(this).select2('data').text || '');
+        //$('[name=pcp_made_through]', $form).val($(this).select2('data').text || '');
       });
 
     $('.crm-soft-credit-block tr span').each(function () {
index 3523dfe45ad4791331356fccc9fc24fdfdf4d94d..b170e0104b780fcc84705565c233cabe1aff98e6 100644 (file)
@@ -61,7 +61,7 @@
                 <td class="form-item even-row{if $wizard.currentStepName == 'Preview'} labels{/if}">
                     {if $wizard.currentStepName == 'Preview'}
           {if $softCreditFields && $softCreditFields[$i] != ''}
-          {$mapper[$i]} - {$softCreditFields[$i]}
+          {$mapper[$i]} - {$softCreditFields[$i]} {if $mapperSoftCreditType[$i]}({$mapperSoftCreditType[$i].label}){/if}
       {else}
           {$mapper[$i]}
       {/if}