Skip to content

Commit

Permalink
Merge pull request #23688 from eileenmcnaughton/import_cont_tests
Browse files Browse the repository at this point in the history
Contribution import - tests & fixes on dates, amount
  • Loading branch information
seamuslee001 authored Jun 5, 2022
2 parents 75aa554 + 4b58c5c commit 2e04078
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 237 deletions.
2 changes: 1 addition & 1 deletion CRM/Activity/Import/Parser/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function import($onDuplicate, &$values) {
foreach ($params as $key => $val) {
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
if (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') {
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $params, $dateType, $key);
$this->formatCustomDate($params, $params, $dateType, $key);
}
elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
$params[$key] = CRM_Utils_String::strtoboolstr($val);
Expand Down
20 changes: 1 addition & 19 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private function formatCommonData($params, &$formatted) {
//we should not update Date to null, CRM-4062
if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
//CRM-21267
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
$this->formatCustomDate($params, $formatted, $dateType, $key);
}
elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
if (empty($val) && !is_numeric($val) && $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) {
Expand Down Expand Up @@ -1137,24 +1137,6 @@ public function formatParams(&$params, $onDuplicate, $cid) {
}
}

/**
* Convert any given date string to default date array.
*
* @param array $params
* Has given date-format.
* @param array $formatted
* Store formatted date in this array.
* @param int $dateType
* Type of date.
* @param string $dateParam
* Index of params.
*/
public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
//fix for CRM-2687
CRM_Utils_Date::convertToDefaultDate($params, $dateType, $dateParam);
$formatted[$dateParam] = CRM_Utils_Date::processDate($params[$dateParam]);
}

/**
* Get the message for a successful import.
*
Expand Down
40 changes: 10 additions & 30 deletions CRM/Contribute/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ protected static function checkRequiredFields($self, string $contactORContributi
*/
public function preProcess() {
parent::preProcess();
$this->_mapperFields = $this->getAvailableFields();
asort($this->_mapperFields);

$this->_columnCount = $this->get('columnCount');
$skipColumnHeader = $this->getSubmittedValue('skipColumnHeader');
Expand Down Expand Up @@ -181,7 +179,7 @@ public function buildQuickForm() {
$sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
$jsSet = FALSE;
if ($this->get('savedMapping')) {
list($mappingName, $mappingContactType) = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID);
[$mappingName, $mappingContactType] = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID);

$mappingName = $mappingName[1];
$mappingContactType = $mappingContactType[1];
Expand Down Expand Up @@ -249,9 +247,11 @@ public function buildQuickForm() {
0,
];
}
if (!empty($mapperKeysValues) && $mapperKeysValues[$i][0] == 'soft_credit') {
$js .= "cj('#mapper_" . $i . "_1').val($mapperKeysValues[$i][1]);\n";
$js .= "cj('#mapper_" . $i . "_2').val($mapperKeysValues[$i][2]);\n";
if (!empty($mapperKeysValues) && ($mapperKeysValues[$i][0] ?? NULL) === 'soft_credit') {
$softCreditField = $mapperKeysValues[$i][1];
$softCreditTypeID = $mapperKeysValues[$i][2];
$js .= "cj('#mapper_" . $i . "_1').val($softCreditField);\n";
$js .= "cj('#mapper_" . $i . "_2').val($softCreditTypeID);\n";
}
}
$sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
Expand Down Expand Up @@ -326,7 +326,7 @@ public static function formRule($fields, $files, $self) {
'used' => 'Unsupervised',
'contact_type' => $contactTypes[$contactTypeId] ?? '',
];
list($ruleFields, $threshold) = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params);
[$ruleFields, $threshold] = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params);
$weightSum = 0;
foreach ($importKeys as $key => $val) {
if (array_key_exists($val, $ruleFields)) {
Expand Down Expand Up @@ -445,27 +445,8 @@ public function postProcess() {
// store mapping Id to display it in the preview page
$this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));

//Updating Mapping Records
if (!empty($params['updateMapping'])) {
for ($i = 0; $i < $this->_columnCount; $i++) {
$this->saveMappingField($params['mappingId'], $i, TRUE);
}
}

//Saving Mapping Details and Records
if (!empty($params['saveMapping'])) {
$mappingParams = [
'name' => $params['saveMappingName'],
'description' => $params['saveMappingDesc'],
'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'),
];
$saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);

for ($i = 0; $i < $this->_columnCount; $i++) {
$this->saveMappingField($saveMapping->id, $i, FALSE);
}
$this->set('savedMapping', $saveMapping->id);
}
$mappingType = 'Import Contribution';
$this->saveMapping($mappingType);

$parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeysMain);
$parser->setUserJobID($this->getUserJobID());
Expand All @@ -474,8 +455,7 @@ public function postProcess() {
$this->getSubmittedValue('fieldSeparator'),
$mapper,
$this->getSubmittedValue('skipColumnHeader'),
CRM_Import_Parser::MODE_PREVIEW,
$this->get('contactType')
CRM_Import_Parser::MODE_PREVIEW
);

// add all the necessary variables to the form
Expand Down
113 changes: 4 additions & 109 deletions CRM/Contribute/Import/Parser/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ public function __construct($mapperKeys = []) {
*/
protected $_softCreditErrorsFileName;

/**
* Whether the file has a column header or not
*
* @var bool
*/
protected $_haveColumnHeader;

/**
* @param string $fileName
* @param string $separator
Expand Down Expand Up @@ -146,8 +139,6 @@ public function run(

$this->init();

$this->_haveColumnHeader = $skipColumnHeader;

$this->_lineCount = $this->_validSoftCreditRowCount = $this->_validPledgePaymentRowCount = 0;
$this->_invalidRowCount = $this->_validCount = $this->_invalidSoftCreditRowCount = $this->_invalidPledgePaymentRowCount = 0;
$this->_totalCount = 0;
Expand Down Expand Up @@ -225,39 +216,27 @@ public function run(
if ($returnCode == self::ERROR) {
$this->_invalidRowCount++;
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_errors[] = $values;
}

if ($returnCode == self::PLEDGE_PAYMENT_ERROR) {
$this->_invalidPledgePaymentRowCount++;
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_pledgePaymentErrors[] = $values;
}

if ($returnCode == self::SOFT_CREDIT_ERROR) {
$this->_invalidSoftCreditRowCount++;
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_softCreditErrors[] = $values;
}

if ($returnCode == self::DUPLICATE) {
$this->_duplicateCount++;
$recordNumber = $this->_lineCount;
if ($this->_haveColumnHeader) {
$recordNumber--;
}
array_unshift($values, $recordNumber);
$this->_duplicates[] = $values;
if ($onDuplicate != self::DUPLICATE_SKIP) {
Expand Down Expand Up @@ -376,6 +355,9 @@ protected function getFieldMappings(): array {
public function getMappedRow(array $values): array {
$params = [];
foreach ($this->getFieldMappings() as $i => $mappedField) {
if ($mappedField['name'] === 'do_not_import' || $mappedField['name'] === NULL) {
continue;
}
if (!empty($mappedField['soft_credit_match_field'])) {
$params['soft_credit'][$i] = ['soft_credit_type_id' => $mappedField['soft_credit_type_id'], $mappedField['soft_credit_match_field'] => $values[$i]];
}
Expand Down Expand Up @@ -640,7 +622,6 @@ public function summary(&$values) {
if ($errorMessage) {
$tempMsg = "Invalid value for field(s) : $errorMessage";
array_unshift($values, $tempMsg);
$errorMessage = NULL;
$this->setImportStatus($rowNumber, 'ERROR', $tempMsg);
return CRM_Import_Parser::ERROR;
}
Expand Down Expand Up @@ -675,7 +656,7 @@ public function import($onDuplicate, &$values) {
}

$params = $this->getMappedRow($values);
$formatted = array_merge(['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE, 'contribution_id' => $params['id'] ?? NULL], $params);
$formatted = array_merge(['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => TRUE, 'contribution_id' => $params['id'] ?? NULL], $params);
//CRM-10994
if (isset($params['total_amount']) && $params['total_amount'] == 0) {
$params['total_amount'] = '0.00';
Expand Down Expand Up @@ -967,61 +948,6 @@ public function &getImportedContributions() {
return $this->_newContributions;
}

/**
* Format date fields from input to mysql.
*
* @param array $params
*
* @return array
* Error messages, if any.
*/
public function formatDateFields(&$params) {
$errorMessage = [];
$dateType = CRM_Core_Session::singleton()->get('dateTypes');
foreach ($params as $key => $val) {
if ($val) {
switch ($key) {
case 'receive_date':
if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
$params[$key] = $dateValue;
}
else {
$errorMessage[] = ts('Receive Date');
}
break;

case 'cancel_date':
if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
$params[$key] = $dateValue;
}
else {
$errorMessage[] = ts('Cancel Date');
}
break;

case 'receipt_date':
if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
$params[$key] = $dateValue;
}
else {
$errorMessage[] = ts('Receipt date');
}
break;

case 'thankyou_date':
if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
$params[$key] = $dateValue;
}
else {
$errorMessage[] = ts('Thankyou Date');
}
break;
}
}
}
return $errorMessage;
}

/**
* Format input params to suit api handling.
*
Expand All @@ -1034,37 +960,16 @@ public function formatDateFields(&$params) {
* @param array $formatted
*/
public function formatInput(&$params, &$formatted = []) {
$dateType = CRM_Core_Session::singleton()->get('dateTypes');
$customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Contribution';
$customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
// @todo call formatDateFields & move custom data handling there.
// Also note error handling for dates is currently in deprecatedFormatParams
// we should use the error handling in formatDateFields.
foreach ($params as $key => $val) {
// @todo - call formatDateFields instead.
if ($val) {
switch ($key) {
case 'receive_date':
case 'cancel_date':
case 'receipt_date':
case 'thankyou_date':
$params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType);
break;

case 'pledge_payment':
$params[$key] = CRM_Utils_String::strtobool($val);
break;

}
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
if ($customFields[$customFieldID]['data_type'] == 'Date') {
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
unset($params[$key]);
}
elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
$params[$key] = CRM_Utils_String::strtoboolstr($val);
}
}
}
}
}
Expand Down Expand Up @@ -1183,16 +1088,6 @@ private function deprecatedFormatParams($params, &$values, $create = FALSE, $onD
}
break;

case 'non_deductible_amount':
case 'total_amount':
case 'fee_amount':
case 'net_amount':
// @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled.
if (!CRM_Utils_Rule::money($value)) {
return civicrm_api3_create_error("$key not a valid amount: $value");
}
break;

case 'currency':
if (!CRM_Utils_Rule::currencyCode($value)) {
return civicrm_api3_create_error("currency not a valid code: $value");
Expand Down
2 changes: 1 addition & 1 deletion CRM/Custom/Import/Parser/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private function formatCommonData($params, &$formatted) {
//we should not update Date to null, CRM-4062
if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
//CRM-21267
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
$this->formatCustomDate($params, $formatted, $dateType, $key);
}
elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
if (empty($val) && !is_numeric($val)) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/Import/Parser/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function import($onDuplicate, &$values) {
if ($val) {
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
if ($customFields[$customFieldID]['data_type'] == 'Date') {
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
$this->formatCustomDate($params, $formatted, $dateType, $key);
unset($params[$key]);
}
elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
Expand Down
33 changes: 33 additions & 0 deletions CRM/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function getTitle() {
*/
public function preProcess() {
$this->assignMapFieldVariables();
$this->_mapperFields = $this->getAvailableFields();
asort($this->_mapperFields);
parent::preProcess();
}

Expand Down Expand Up @@ -241,4 +243,35 @@ protected function saveMappingField(int $mappingID, int $columnNumber, bool $isU
}
}

/**
* Save the Field Mapping.
*
* @param string $mappingType
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function saveMapping(string $mappingType): void {
//Updating Mapping Records
if ($this->getSubmittedValue('updateMapping')) {
foreach (array_keys($this->getColumnHeaders()) as $i) {
$this->saveMappingField($this->getSubmittedValue('mappingId'), $i, TRUE);
}
}
//Saving Mapping Details and Records
if ($this->getSubmittedValue('saveMapping')) {
$mappingParams = [
'name' => $this->getSubmittedValue('saveMappingName'),
'description' => $this->getSubmittedValue('saveMappingDesc'),
'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $mappingType),
];
$saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);

foreach (array_keys($this->getColumnHeaders()) as $i) {
$this->saveMappingField($saveMapping->id, $i, FALSE);
}
$this->set('savedMapping', $saveMapping->id);
}
}

}
Loading

0 comments on commit 2e04078

Please sign in to comment.