From 1221efe9f6893778358a1a7bb11d9ad907ac8b61 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Sat, 5 Apr 2014 18:23:45 +0530 Subject: [PATCH] CRM-13833 Soft-Credit Import fix ---------------------------------------- * CRM-13833: http://issues.civicrm.org/jira/browse/CRM-13833 --- CRM/Contribute/BAO/Contribution.php | 11 +- CRM/Contribute/BAO/ContributionSoft.php | 3 + CRM/Contribute/Import/Form/MapField.php | 57 ++++++---- CRM/Contribute/Import/Form/Preview.php | 24 ++-- CRM/Contribute/Import/Parser.php | 12 +- CRM/Contribute/Import/Parser/Contribution.php | 36 +++--- CRM/Utils/DeprecatedUtils.php | 105 ++++++++++-------- templates/CRM/Contribute/Form/SoftCredit.tpl | 2 +- .../CRM/Contribute/Import/Form/MapTable.tpl | 2 +- 9 files changed, 151 insertions(+), 101 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index fc136955b4..39aaa17370 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -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']; diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 39436c6409..decd095431 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -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); diff --git a/CRM/Contribute/Import/Form/MapField.php b/CRM/Contribute/Import/Form/MapField.php index 84134ed103..9c9c33be66 100644 --- a/CRM/Contribute/Import/Form/MapField.php +++ b/CRM/Contribute/Import/Form/MapField.php @@ -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 .= "\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') . '
'; + } + } } 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 - )) . '
'; - } + $errors['_qf_default'] .= ts('Missing required field: %1', array( + 1 => $title + )) . '
'; } } } @@ -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)); diff --git a/CRM/Contribute/Import/Form/Preview.php b/CRM/Contribute/Import/Form/Preview.php index 6f7985902c..5703012fbe 100644 --- a/CRM/Contribute/Import/Form/Preview.php +++ b/CRM/Contribute/Import/Form/Preview.php @@ -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'); diff --git a/CRM/Contribute/Import/Parser.php b/CRM/Contribute/Import/Parser.php index f6df518de1..a81b4de3c8 100644 --- a/CRM/Contribute/Import/Parser.php +++ b/CRM/Contribute/Import/Parser.php @@ -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; diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index 3e3491d3ad..2d8daaeaad 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -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; } diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index cdba75d417..786e123f72 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -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; diff --git a/templates/CRM/Contribute/Form/SoftCredit.tpl b/templates/CRM/Contribute/Form/SoftCredit.tpl index b9e762e6f9..2fe9ff9abe 100644 --- a/templates/CRM/Contribute/Form/SoftCredit.tpl +++ b/templates/CRM/Contribute/Form/SoftCredit.tpl @@ -109,7 +109,7 @@ }) // 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 () { diff --git a/templates/CRM/Contribute/Import/Form/MapTable.tpl b/templates/CRM/Contribute/Import/Form/MapTable.tpl index 3523dfe45a..b170e0104b 100644 --- a/templates/CRM/Contribute/Import/Form/MapTable.tpl +++ b/templates/CRM/Contribute/Import/Form/MapTable.tpl @@ -61,7 +61,7 @@ {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} -- 2.25.1