3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class to parse contribution csv files.
21 class CRM_Contribute_Import_Parser_Contribution
extends CRM_Import_Parser
{
23 protected $_mapperKeys;
25 private $_contactIdIndex;
27 protected $_mapperSoftCredit;
28 //protected $_mapperPhoneType;
31 * Array of successfully imported contribution id's
35 protected $_newContributions;
41 * @param array $mapperSoftCredit
42 * @param null $mapperPhoneType
43 * @param array $mapperSoftCreditType
45 public function __construct(&$mapperKeys = [], $mapperSoftCredit = [], $mapperPhoneType = NULL, $mapperSoftCreditType = []) {
46 parent
::__construct();
47 $this->_mapperKeys
= &$mapperKeys;
48 $this->_mapperSoftCredit
= &$mapperSoftCredit;
49 $this->_mapperSoftCreditType
= &$mapperSoftCreditType;
53 * Contribution-specific result codes
54 * @see CRM_Import_Parser result code constants
56 const SOFT_CREDIT
= 512, SOFT_CREDIT_ERROR
= 1024, PLEDGE_PAYMENT
= 2048, PLEDGE_PAYMENT_ERROR
= 4096;
70 * Separator being used
73 protected $_separator;
76 * Total number of lines in file
79 protected $_lineCount;
82 * Running total number of valid soft credit rows
85 protected $_validSoftCreditRowCount;
88 * Running total number of invalid soft credit rows
91 protected $_invalidSoftCreditRowCount;
94 * Running total number of valid pledge payment rows
97 protected $_validPledgePaymentRowCount;
100 * Running total number of invalid pledge payment rows
103 protected $_invalidPledgePaymentRowCount;
106 * Array of pledge payment error lines, bounded by MAX_ERROR
109 protected $_pledgePaymentErrors;
112 * Array of pledge payment error lines, bounded by MAX_ERROR
115 protected $_softCreditErrors;
118 * Filename of pledge payment error data
122 protected $_pledgePaymentErrorsFileName;
125 * Filename of soft credit error data
129 protected $_softCreditErrorsFileName;
132 * Whether the file has a column header or not
136 protected $_haveColumnHeader;
139 * @param string $fileName
140 * @param string $separator
142 * @param bool $skipColumnHeader
144 * @param int $contactType
145 * @param int $onDuplicate
146 * @param int $statusID
147 * @param int $totalRowCount
156 $skipColumnHeader = FALSE,
157 $mode = self
::MODE_PREVIEW
,
158 $contactType = self
::CONTACT_INDIVIDUAL
,
159 $onDuplicate = self
::DUPLICATE_SKIP
,
161 $totalRowCount = NULL
163 if (!is_array($fileName)) {
164 throw new CRM_Core_Exception('Unable to determine import file');
166 $fileName = $fileName['name'];
168 switch ($contactType) {
169 case self
::CONTACT_INDIVIDUAL
:
170 $this->_contactType
= 'Individual';
173 case self
::CONTACT_HOUSEHOLD
:
174 $this->_contactType
= 'Household';
177 case self
::CONTACT_ORGANIZATION
:
178 $this->_contactType
= 'Organization';
183 $this->_haveColumnHeader
= $skipColumnHeader;
185 $this->_separator
= $separator;
187 $fd = fopen($fileName, "r");
192 $this->_lineCount
= $this->_validSoftCreditRowCount
= $this->_validPledgePaymentRowCount
= 0;
193 $this->_invalidRowCount
= $this->_validCount
= $this->_invalidSoftCreditRowCount
= $this->_invalidPledgePaymentRowCount
= 0;
194 $this->_totalCount
= $this->_conflictCount
= 0;
197 $this->_warnings
= [];
198 $this->_conflicts
= [];
199 $this->_pledgePaymentErrors
= [];
200 $this->_softCreditErrors
= [];
202 $this->progressImport($statusID);
203 $startTimestamp = $currTimestamp = $prevTimestamp = time();
206 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
208 if ($mode == self
::MODE_MAPFIELD
) {
212 $this->_activeFieldCount
= count($this->_activeFields
);
218 $values = fgetcsv($fd, 8192, $separator);
223 self
::encloseScrub($values);
225 // skip column header if we're not in mapfield mode
226 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
227 $skipColumnHeader = FALSE;
231 /* trim whitespace around the values */
234 foreach ($values as $k => $v) {
235 $values[$k] = trim($v, " \t\r\n");
238 if (CRM_Utils_System
::isNull($values)) {
242 $this->_totalCount++
;
244 if ($mode == self
::MODE_MAPFIELD
) {
245 $returnCode = CRM_Import_Parser
::VALID
;
247 elseif ($mode == self
::MODE_PREVIEW
) {
248 $returnCode = $this->preview($values);
250 elseif ($mode == self
::MODE_SUMMARY
) {
251 $returnCode = $this->summary($values);
253 elseif ($mode == self
::MODE_IMPORT
) {
254 $returnCode = $this->import($onDuplicate, $values);
255 if ($statusID && (($this->_lineCount %
50) == 0)) {
256 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
260 $returnCode = self
::ERROR
;
263 // note that a line could be valid but still produce a warning
264 if ($returnCode == self
::VALID
) {
265 $this->_validCount++
;
266 if ($mode == self
::MODE_MAPFIELD
) {
267 $this->_rows
[] = $values;
268 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
272 if ($returnCode == self
::SOFT_CREDIT
) {
273 $this->_validSoftCreditRowCount++
;
274 $this->_validCount++
;
275 if ($mode == self
::MODE_MAPFIELD
) {
276 $this->_rows
[] = $values;
277 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
281 if ($returnCode == self
::PLEDGE_PAYMENT
) {
282 $this->_validPledgePaymentRowCount++
;
283 $this->_validCount++
;
284 if ($mode == self
::MODE_MAPFIELD
) {
285 $this->_rows
[] = $values;
286 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
290 if ($returnCode == self
::ERROR
) {
291 $this->_invalidRowCount++
;
292 $recordNumber = $this->_lineCount
;
293 if ($this->_haveColumnHeader
) {
296 array_unshift($values, $recordNumber);
297 $this->_errors
[] = $values;
300 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
301 $this->_invalidPledgePaymentRowCount++
;
302 $recordNumber = $this->_lineCount
;
303 if ($this->_haveColumnHeader
) {
306 array_unshift($values, $recordNumber);
307 $this->_pledgePaymentErrors
[] = $values;
310 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
311 $this->_invalidSoftCreditRowCount++
;
312 $recordNumber = $this->_lineCount
;
313 if ($this->_haveColumnHeader
) {
316 array_unshift($values, $recordNumber);
317 $this->_softCreditErrors
[] = $values;
320 if ($returnCode == self
::CONFLICT
) {
321 $this->_conflictCount++
;
322 $recordNumber = $this->_lineCount
;
323 if ($this->_haveColumnHeader
) {
326 array_unshift($values, $recordNumber);
327 $this->_conflicts
[] = $values;
330 if ($returnCode == self
::DUPLICATE
) {
331 $this->_duplicateCount++
;
332 $recordNumber = $this->_lineCount
;
333 if ($this->_haveColumnHeader
) {
336 array_unshift($values, $recordNumber);
337 $this->_duplicates
[] = $values;
338 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
339 $this->_validCount++
;
343 // if we are done processing the maxNumber of lines, break
344 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
351 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
352 $customHeaders = $mapper;
354 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
355 foreach ($customHeaders as $key => $value) {
356 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
357 $customHeaders[$key] = $customfields[$id][0];
360 if ($this->_invalidRowCount
) {
361 // removed view url for invlaid contacts
362 $headers = array_merge([
366 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
367 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
370 if ($this->_invalidPledgePaymentRowCount
) {
371 // removed view url for invlaid contacts
372 $headers = array_merge([
376 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
377 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
380 if ($this->_invalidSoftCreditRowCount
) {
381 // removed view url for invlaid contacts
382 $headers = array_merge([
386 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
387 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
390 if ($this->_conflictCount
) {
391 $headers = array_merge([
395 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
396 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
398 if ($this->_duplicateCount
) {
399 $headers = array_merge([
401 ts('View Contribution URL'),
404 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
405 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
411 * Given a list of the importable field keys that the user has selected
412 * set the active fields array to this list
414 * @param array $fieldKeys mapped array of values
416 public function setActiveFields($fieldKeys) {
417 $this->_activeFieldCount
= count($fieldKeys);
418 foreach ($fieldKeys as $key) {
419 if (empty($this->_fields
[$key])) {
420 $this->_activeFields
[] = new CRM_Contribute_Import_Field('', ts('- do not import -'));
423 $this->_activeFields
[] = clone($this->_fields
[$key]);
429 * Store the soft credit field information.
431 * This was perhaps done this way on the believe that a lot of code pain
432 * was worth it to avoid negligible-cost array iterations. Perhaps we could prioritise
433 * readability & maintainability next since we can just work with functions to retrieve
434 * data from the metadata.
436 * @param array $elements
438 public function setActiveFieldSoftCredit($elements) {
439 foreach ((array) $elements as $i => $element) {
440 $this->_activeFields
[$i]->_softCreditField
= $element;
445 * Store the soft credit field type information.
447 * This was perhaps done this way on the believe that a lot of code pain
448 * was worth it to avoid negligible-cost array iterations. Perhaps we could prioritise
449 * readability & maintainability next since we can just work with functions to retrieve
450 * data from the metadata.
452 * @param array $elements
454 public function setActiveFieldSoftCreditType($elements) {
455 foreach ((array) $elements as $i => $element) {
456 $this->_activeFields
[$i]->_softCreditType
= $element;
461 * Format the field values for input to the api.
464 * (reference ) associative array of name/value pairs
466 public function &getActiveFieldParams() {
468 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
469 if (isset($this->_activeFields
[$i]->_value
)) {
470 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
471 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
472 $params[$this->_activeFields
[$i]->_name
] = [];
474 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
475 if (isset($this->_activeFields
[$i]->_softCreditType
)) {
476 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
480 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
481 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
482 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
491 * @param string $name
494 * @param string $headerPattern
495 * @param string $dataPattern
497 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
499 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
502 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
503 if (!array_key_exists($name, $tempField)) {
504 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
507 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
508 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
515 * Store parser values.
517 * @param CRM_Core_Session $store
521 public function set($store, $mode = self
::MODE_SUMMARY
) {
522 $store->set('fileSize', $this->_fileSize
);
523 $store->set('lineCount', $this->_lineCount
);
524 $store->set('separator', $this->_separator
);
525 $store->set('fields', $this->getSelectValues());
526 $store->set('fieldTypes', $this->getSelectTypes());
528 $store->set('headerPatterns', $this->getHeaderPatterns());
529 $store->set('dataPatterns', $this->getDataPatterns());
530 $store->set('columnCount', $this->_activeFieldCount
);
532 $store->set('totalRowCount', $this->_totalCount
);
533 $store->set('validRowCount', $this->_validCount
);
534 $store->set('invalidRowCount', $this->_invalidRowCount
);
535 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
536 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
537 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
538 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
539 $store->set('conflictRowCount', $this->_conflictCount
);
541 switch ($this->_contactType
) {
543 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
547 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
551 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
554 if ($this->_invalidRowCount
) {
555 $store->set('errorsFileName', $this->_errorFileName
);
557 if ($this->_conflictCount
) {
558 $store->set('conflictsFileName', $this->_conflictFileName
);
560 if (isset($this->_rows
) && !empty($this->_rows
)) {
561 $store->set('dataValues', $this->_rows
);
564 if ($this->_invalidPledgePaymentRowCount
) {
565 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
568 if ($this->_invalidSoftCreditRowCount
) {
569 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
572 if ($mode == self
::MODE_IMPORT
) {
573 $store->set('duplicateRowCount', $this->_duplicateCount
);
574 if ($this->_duplicateCount
) {
575 $store->set('duplicatesFileName', $this->_duplicateFileName
);
581 * Export data to a CSV file.
583 * @param string $fileName
584 * @param array $header
587 public static function exportCSV($fileName, $header, $data) {
589 $fd = fopen($fileName, 'w');
591 foreach ($header as $key => $value) {
592 $header[$key] = "\"$value\"";
594 $config = CRM_Core_Config
::singleton();
595 $output[] = implode($config->fieldSeparator
, $header);
597 foreach ($data as $datum) {
598 foreach ($datum as $key => $value) {
599 if (isset($value[0]) && is_array($value)) {
600 foreach ($value[0] as $k1 => $v1) {
601 if ($k1 == 'location_type_id') {
608 $datum[$key] = "\"$value\"";
611 $output[] = implode($config->fieldSeparator
, $datum);
613 fwrite($fd, implode("\n", $output));
618 * Determines the file extension based on error code.
621 * Error code constant.
625 public static function errorFileName($type) {
631 $config = CRM_Core_Config
::singleton();
632 $fileName = $config->uploadDir
. "sqlImport";
635 case self
::SOFT_CREDIT_ERROR
:
636 $fileName .= '.softCreditErrors';
639 case self
::PLEDGE_PAYMENT_ERROR
:
640 $fileName .= '.pledgePaymentErrors';
644 $fileName = parent
::errorFileName($type);
652 * Determines the file name based on error code.
655 * Error code constant.
659 public static function saveFileName($type) {
666 case self
::SOFT_CREDIT_ERROR
:
667 $fileName = 'Import_Soft_Credit_Errors.csv';
670 case self
::PLEDGE_PAYMENT_ERROR
:
671 $fileName = 'Import_Pledge_Payment_Errors.csv';
675 $fileName = parent
::saveFileName($type);
683 * The initializer code, called before the processing
685 public function init() {
686 $fields = CRM_Contribute_BAO_Contribution
::importableFields($this->_contactType
, FALSE);
688 $fields = array_merge($fields,
691 'title' => ts('Soft Credit'),
692 'softCredit' => TRUE,
693 'headerPattern' => '/Soft Credit/i',
698 // add pledge fields only if its is enabled
699 if (CRM_Core_Permission
::access('CiviPledge')) {
701 'pledge_payment' => [
702 'title' => ts('Pledge Payment'),
703 'headerPattern' => '/Pledge Payment/i',
706 'title' => ts('Pledge ID'),
707 'headerPattern' => '/Pledge ID/i',
711 $fields = array_merge($fields, $pledgeFields);
713 foreach ($fields as $name => $field) {
714 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
715 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
716 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
717 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
720 $this->_newContributions
= [];
722 $this->setActiveFields($this->_mapperKeys
);
723 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit
);
724 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType
);
726 // FIXME: we should do this in one place together with Form/MapField.php
727 $this->_contactIdIndex
= -1;
730 foreach ($this->_mapperKeys
as $key) {
732 case 'contribution_contact_id':
733 $this->_contactIdIndex
= $index;
742 * Handle the values in preview mode.
744 * @param array $values
745 * The array of values belonging to this line.
748 * the result of this processing
750 public function preview(&$values) {
751 return $this->summary($values);
755 * Handle the values in summary mode.
757 * @param array $values
758 * The array of values belonging to this line.
761 * the result of this processing
763 public function summary(&$values) {
764 $this->setActiveFieldValues($values);
766 $params = $this->getActiveFieldParams();
769 $errorMessage = implode('; ', $this->formatDateFields($params));
770 //date-Format part ends
772 $params['contact_type'] = 'Contribution';
774 //checking error in custom data
775 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
778 $tempMsg = "Invalid value for field(s) : $errorMessage";
779 array_unshift($values, $tempMsg);
780 $errorMessage = NULL;
781 return CRM_Import_Parser
::ERROR
;
784 return CRM_Import_Parser
::VALID
;
788 * Handle the values in import mode.
790 * @param int $onDuplicate
791 * The code for what action to take on duplicates.
792 * @param array $values
793 * The array of values belonging to this line.
796 * the result of this processing
798 public function import($onDuplicate, &$values) {
799 // first make sure this is a valid line
800 $response = $this->summary($values);
801 if ($response != CRM_Import_Parser
::VALID
) {
805 $params = &$this->getActiveFieldParams();
806 $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE];
809 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
810 $params['total_amount'] = '0.00';
812 $this->formatInput($params, $formatted);
814 static $indieFields = NULL;
815 if ($indieFields == NULL) {
816 $tempIndieFields = CRM_Contribute_DAO_Contribution
::import();
817 $indieFields = $tempIndieFields;
821 foreach ($params as $key => $field) {
822 if ($field == NULL ||
$field === '') {
825 $paramValues[$key] = $field;
828 //import contribution record according to select contact type
829 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
&&
830 (!empty($paramValues['contribution_contact_id']) ||
!empty($paramValues['external_identifier']))
832 $paramValues['contact_type'] = $this->_contactType
;
834 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
835 (!empty($paramValues['contribution_id']) ||
!empty($values['trxn_id']) ||
!empty($paramValues['invoice_id']))
837 $paramValues['contact_type'] = $this->_contactType
;
839 elseif (!empty($params['soft_credit'])) {
840 $paramValues['contact_type'] = $this->_contactType
;
842 elseif (!empty($paramValues['pledge_payment'])) {
843 $paramValues['contact_type'] = $this->_contactType
;
846 //need to pass $onDuplicate to check import mode.
847 if (!empty($paramValues['pledge_payment'])) {
848 $paramValues['onDuplicate'] = $onDuplicate;
850 $formatError = $this->deprecatedFormatParams($paramValues, $formatted, TRUE, $onDuplicate);
853 array_unshift($values, $formatError['error_message']);
854 if (CRM_Utils_Array
::value('error_data', $formatError) == 'soft_credit') {
855 return self
::SOFT_CREDIT_ERROR
;
857 if (CRM_Utils_Array
::value('error_data', $formatError) == 'pledge_payment') {
858 return self
::PLEDGE_PAYMENT_ERROR
;
860 return CRM_Import_Parser
::ERROR
;
863 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
864 //fix for CRM-2219 - Update Contribution
865 // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
866 if (!empty($paramValues['invoice_id']) ||
!empty($paramValues['trxn_id']) ||
!empty($paramValues['contribution_id'])) {
868 'id' => $paramValues['contribution_id'] ??
NULL,
869 'trxn_id' => $paramValues['trxn_id'] ??
NULL,
870 'invoice_id' => $paramValues['invoice_id'] ??
NULL,
872 $ids['contribution'] = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($dupeIds);
874 if ($ids['contribution']) {
875 $formatted['id'] = $ids['contribution'];
877 if (!empty($paramValues['note'])) {
879 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id');
880 $daoNote = new CRM_Core_BAO_Note();
881 $daoNote->entity_table
= 'civicrm_contribution';
882 $daoNote->entity_id
= $ids['contribution'];
883 if ($daoNote->find(TRUE)) {
884 $noteID['id'] = $daoNote->id
;
888 'entity_table' => 'civicrm_contribution',
889 'note' => $paramValues['note'],
890 'entity_id' => $ids['contribution'],
891 'contact_id' => $contactID,
893 CRM_Core_BAO_Note
::add($noteParams, $noteID);
894 unset($formatted['note']);
897 //need to check existing soft credit contribution, CRM-3968
898 if (!empty($formatted['soft_credit'])) {
900 'contact_id' => $formatted['soft_credit'],
901 'contribution_id' => $ids['contribution'],
904 //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
905 $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($dupeSoftCredit['contribution_id']);
906 if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) {
907 foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) {
908 if (!empty($existingSoftCreditValues['soft_credit_id'])) {
909 civicrm_api3('ContributionSoft', 'delete', [
910 'id' => $existingSoftCreditValues['soft_credit_id'],
918 $formatted['id'] = $ids['contribution'];
920 $newContribution = civicrm_api3('contribution', 'create', $formatted);
921 $this->_newContributions
[] = $newContribution['id'];
923 //return soft valid since we need to show how soft credits were added
924 if (!empty($formatted['soft_credit'])) {
925 return self
::SOFT_CREDIT
;
928 // process pledge payment assoc w/ the contribution
929 return $this->processPledgePayments($formatted);
932 'id' => 'Contribution ID',
933 'trxn_id' => 'Transaction ID',
934 'invoice_id' => 'Invoice ID',
936 foreach ($dupeIds as $k => $v) {
938 $errorMsg[] = "$labels[$k] $v";
941 $errorMsg = implode(' AND ', $errorMsg);
942 array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.');
943 return CRM_Import_Parser
::ERROR
;
947 if ($this->_contactIdIndex
< 0) {
949 $error = $this->checkContactDuplicate($paramValues);
951 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
952 $matchedIDs = explode(',', $error['error_message']['params'][0]);
953 if (count($matchedIDs) > 1) {
954 array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported');
955 return CRM_Import_Parser
::ERROR
;
957 $cid = $matchedIDs[0];
958 $formatted['contact_id'] = $cid;
960 $newContribution = civicrm_api('contribution', 'create', $formatted);
961 if (civicrm_error($newContribution)) {
962 if (is_array($newContribution['error_message'])) {
963 array_unshift($values, $newContribution['error_message']['message']);
964 if ($newContribution['error_message']['params'][0]) {
965 return CRM_Import_Parser
::DUPLICATE
;
969 array_unshift($values, $newContribution['error_message']);
970 return CRM_Import_Parser
::ERROR
;
974 $this->_newContributions
[] = $newContribution['id'];
975 $formatted['contribution_id'] = $newContribution['id'];
977 //return soft valid since we need to show how soft credits were added
978 if (!empty($formatted['soft_credit'])) {
979 return self
::SOFT_CREDIT
;
982 // process pledge payment assoc w/ the contribution
983 return $this->processPledgePayments($formatted);
986 // Using new Dedupe rule.
988 'contact_type' => $this->_contactType
,
989 'used' => 'Unsupervised',
991 $fieldsArray = CRM_Dedupe_BAO_DedupeRule
::dedupeRuleFields($ruleParams);
993 foreach ($fieldsArray as $value) {
994 if (array_key_exists(trim($value), $params)) {
995 $paramValue = $params[trim($value)];
996 if (is_array($paramValue)) {
997 $disp .= $params[trim($value)][0][trim($value)] . " ";
1000 $disp .= $params[trim($value)] . " ";
1005 if (!empty($params['external_identifier'])) {
1007 $disp .= "AND {$params['external_identifier']}";
1010 $disp = $params['external_identifier'];
1014 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
1015 return CRM_Import_Parser
::ERROR
;
1018 if (!empty($paramValues['external_identifier'])) {
1019 $checkCid = new CRM_Contact_DAO_Contact();
1020 $checkCid->external_identifier
= $paramValues['external_identifier'];
1021 $checkCid->find(TRUE);
1022 if ($checkCid->id
!= $formatted['contact_id']) {
1023 array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
1024 return CRM_Import_Parser
::ERROR
;
1027 $newContribution = civicrm_api('contribution', 'create', $formatted);
1028 if (civicrm_error($newContribution)) {
1029 if (is_array($newContribution['error_message'])) {
1030 array_unshift($values, $newContribution['error_message']['message']);
1031 if ($newContribution['error_message']['params'][0]) {
1032 return CRM_Import_Parser
::DUPLICATE
;
1036 array_unshift($values, $newContribution['error_message']);
1037 return CRM_Import_Parser
::ERROR
;
1041 $this->_newContributions
[] = $newContribution['id'];
1042 $formatted['contribution_id'] = $newContribution['id'];
1044 //return soft valid since we need to show how soft credits were added
1045 if (!empty($formatted['soft_credit'])) {
1046 return self
::SOFT_CREDIT
;
1049 // process pledge payment assoc w/ the contribution
1050 return $this->processPledgePayments($formatted);
1054 * Process pledge payments.
1056 * @param array $formatted
1060 private function processPledgePayments(array $formatted) {
1061 if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) {
1062 //get completed status
1063 $completeStatusID = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
1065 //need to update payment record to map contribution_id
1066 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'],
1067 'contribution_id', $formatted['contribution_id']
1070 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($formatted['pledge_id'],
1071 [$formatted['pledge_payment_id']],
1074 $formatted['total_amount']
1077 return self
::PLEDGE_PAYMENT
;
1082 * Get the array of successfully imported contribution id's
1086 public function &getImportedContributions() {
1087 return $this->_newContributions
;
1091 * Format date fields from input to mysql.
1093 * @param array $params
1096 * Error messages, if any.
1098 public function formatDateFields(&$params) {
1100 $dateType = CRM_Core_Session
::singleton()->get('dateTypes');
1101 foreach ($params as $key => $val) {
1104 case 'receive_date':
1105 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1106 $params[$key] = $dateValue;
1109 $errorMessage[] = ts('Receive Date');
1114 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1115 $params[$key] = $dateValue;
1118 $errorMessage[] = ts('Cancel Date');
1122 case 'receipt_date':
1123 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1124 $params[$key] = $dateValue;
1127 $errorMessage[] = ts('Receipt date');
1131 case 'thankyou_date':
1132 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1133 $params[$key] = $dateValue;
1136 $errorMessage[] = ts('Thankyou Date');
1142 return $errorMessage;
1146 * Format input params to suit api handling.
1148 * Over time all the parts of deprecatedFormatParams
1149 * and all the parts of the import function on this class that relate to
1150 * reformatting input should be moved here and tests should be added in
1151 * CRM_Contribute_Import_Parser_ContributionTest.
1153 * @param array $params
1154 * @param array $formatted
1156 public function formatInput(&$params, &$formatted = []) {
1157 $dateType = CRM_Core_Session
::singleton()->get('dateTypes');
1158 $customDataType = !empty($params['contact_type']) ?
$params['contact_type'] : 'Contribution';
1159 $customFields = CRM_Core_BAO_CustomField
::getFields($customDataType);
1160 // @todo call formatDateFields & move custom data handling there.
1161 // Also note error handling for dates is currently in deprecatedFormatParams
1162 // we should use the error handling in formatDateFields.
1163 foreach ($params as $key => $val) {
1164 // @todo - call formatDateFields instead.
1167 case 'receive_date':
1169 case 'receipt_date':
1170 case 'thankyou_date':
1171 $params[$key] = CRM_Utils_Date
::formatDate($params[$key], $dateType);
1174 case 'pledge_payment':
1175 $params[$key] = CRM_Utils_String
::strtobool($val);
1179 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1180 if ($customFields[$customFieldID]['data_type'] == 'Date') {
1181 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
1182 unset($params[$key]);
1184 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1185 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
1193 * take the input parameter list as specified in the data model and
1194 * convert it into the same format that we use in QF and BAO object
1196 * @param array $params
1197 * Associative array of property name/value
1198 * pairs to insert in new contact.
1199 * @param array $values
1200 * The reformatted properties that we can use internally.
1201 * @param bool $create
1202 * @param int $onDuplicate
1204 * @return array|CRM_Error
1206 private function deprecatedFormatParams($params, &$values, $create = FALSE, $onDuplicate = NULL) {
1207 require_once 'CRM/Utils/DeprecatedUtils.php';
1208 // copy all the contribution fields as is
1209 require_once 'api/v3/utils.php';
1210 $fields = CRM_Core_DAO
::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution');
1212 _civicrm_api3_store_values($fields, $params, $values);
1214 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
1216 foreach ($params as $key => $value) {
1217 // ignore empty values or empty arrays etc
1218 if (CRM_Utils_System
::isNull($value)) {
1222 // Handling Custom Data
1223 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1224 $values[$key] = $value;
1225 $type = $customFields[$customFieldID]['html_type'];
1226 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID])) {
1227 $values[$key] = self
::unserializeCustomValue($customFieldID, $value, $type);
1229 elseif ($type == 'Select' ||
$type == 'Radio' ||
1230 ($type == 'Autocomplete-Select' &&
1231 $customFields[$customFieldID]['data_type'] == 'String'
1234 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1235 foreach ($customOption as $customFldID => $customValue) {
1236 $val = $customValue['value'] ??
NULL;
1237 $label = $customValue['label'] ??
NULL;
1238 $label = strtolower($label);
1239 $value = strtolower(trim($value));
1240 if (($value == $label) ||
($value == strtolower($val))) {
1241 $values[$key] = $val;
1249 case 'contribution_contact_id':
1250 if (!CRM_Utils_Rule
::integer($value)) {
1251 return civicrm_api3_create_error("contact_id not valid: $value");
1253 $dao = new CRM_Core_DAO();
1255 $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value",
1259 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
1261 elseif ($svq == 1) {
1262 return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact.");
1265 $values['contact_id'] = $values['contribution_contact_id'];
1266 unset($values['contribution_contact_id']);
1269 case 'contact_type':
1270 // import contribution record according to select contact type
1271 require_once 'CRM/Contact/DAO/Contact.php';
1272 $contactType = new CRM_Contact_DAO_Contact();
1273 $contactId = $params['contribution_contact_id'] ??
NULL;
1274 $externalId = $params['external_identifier'] ??
NULL;
1275 $email = $params['email'] ??
NULL;
1276 //when insert mode check contact id or external identifier
1277 if ($contactId ||
$externalId) {
1278 $contactType->id
= $contactId;
1279 $contactType->external_identifier
= $externalId;
1280 if ($contactType->find(TRUE)) {
1281 if ($params['contact_type'] != $contactType->contact_type
) {
1282 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
1287 if (!CRM_Utils_Rule
::email($email)) {
1288 return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped");
1291 // get the contact id from duplicate contact rule, if more than one contact is returned
1292 // we should return error, since current interface allows only one-one mapping
1295 'contact_type' => $params['contact_type'],
1297 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
1298 if (!$checkDedupe['is_error']) {
1299 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email. Row was skipped");
1301 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
1302 if (count($matchingContactIds) > 1) {
1303 return civicrm_api3_create_error("Invalid email address(duplicate) $email. Row was skipped");
1305 if (count($matchingContactIds) == 1) {
1306 $params['contribution_contact_id'] = $matchingContactIds[0];
1309 elseif (!empty($params['contribution_id']) ||
!empty($params['trxn_id']) ||
!empty($params['invoice_id'])) {
1310 // when update mode check contribution id or trxn id or
1312 $contactId = new CRM_Contribute_DAO_Contribution();
1313 if (!empty($params['contribution_id'])) {
1314 $contactId->id
= $params['contribution_id'];
1316 elseif (!empty($params['trxn_id'])) {
1317 $contactId->trxn_id
= $params['trxn_id'];
1319 elseif (!empty($params['invoice_id'])) {
1320 $contactId->invoice_id
= $params['invoice_id'];
1322 if ($contactId->find(TRUE)) {
1323 $contactType->id
= $contactId->contact_id
;
1324 if ($contactType->find(TRUE)) {
1325 if ($params['contact_type'] != $contactType->contact_type
) {
1326 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
1332 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1333 return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
1338 case 'receive_date':
1340 case 'receipt_date':
1341 case 'thankyou_date':
1342 if (!CRM_Utils_Rule
::dateTime($value)) {
1343 return civicrm_api3_create_error("$key not a valid date: $value");
1347 case 'non_deductible_amount':
1348 case 'total_amount':
1351 // @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled.
1352 if (!CRM_Utils_Rule
::money($value)) {
1353 return civicrm_api3_create_error("$key not a valid amount: $value");
1358 if (!CRM_Utils_Rule
::currencyCode($value)) {
1359 return civicrm_api3_create_error("currency not a valid code: $value");
1363 case 'financial_type':
1364 // @todo add test like testPaymentTypeLabel & remove these lines in favour of 'default' part of switch.
1365 require_once 'CRM/Contribute/PseudoConstant.php';
1366 $contriTypes = CRM_Contribute_PseudoConstant
::financialType();
1367 foreach ($contriTypes as $val => $type) {
1368 if (strtolower($value) == strtolower($type)) {
1369 $values['financial_type_id'] = $val;
1373 if (empty($values['financial_type_id'])) {
1374 return civicrm_api3_create_error("Financial Type is not valid: $value");
1379 // import contribution record according to select contact type
1380 // validate contact id and external identifier.
1381 $value[$key] = $mismatchContactType = $softCreditContactIds = '';
1382 if (isset($params[$key]) && is_array($params[$key])) {
1383 foreach ($params[$key] as $softKey => $softParam) {
1384 $contactId = $softParam['contact_id'] ??
NULL;
1385 $externalId = $softParam['external_identifier'] ??
NULL;
1386 $email = $softParam['email'] ??
NULL;
1387 if ($contactId ||
$externalId) {
1388 require_once 'CRM/Contact/DAO/Contact.php';
1389 $contact = new CRM_Contact_DAO_Contact();
1390 $contact->id
= $contactId;
1391 $contact->external_identifier
= $externalId;
1393 if (!$contact->find(TRUE)) {
1394 $field = $contactId ?
ts('Contact ID') : ts('External ID');
1395 $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.",
1396 [1 => $field, 2 => $contactId ?
$contactId : $externalId]);
1400 return civicrm_api3_create_error($errorMsg);
1403 // finally get soft credit contact id.
1404 $values[$key][$softKey] = $softParam;
1405 $values[$key][$softKey]['contact_id'] = $contact->id
;
1408 if (!CRM_Utils_Rule
::email($email)) {
1409 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
1412 // get the contact id from duplicate contact rule, if more than one contact is returned
1413 // we should return error, since current interface allows only one-one mapping
1416 'contact_type' => $params['contact_type'],
1418 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
1419 if (!$checkDedupe['is_error']) {
1420 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped");
1422 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
1423 if (count($matchingContactIds) > 1) {
1424 return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped");
1426 if (count($matchingContactIds) == 1) {
1427 $contactId = $matchingContactIds[0];
1428 unset($softParam['email']);
1429 $values[$key][$softKey] = $softParam +
['contact_id' => $contactId];
1436 case 'pledge_payment':
1439 // giving respect to pledge_payment flag.
1440 if (empty($params['pledge_payment'])) {
1444 // get total amount of from import fields
1445 $totalAmount = $params['total_amount'] ??
NULL;
1447 $onDuplicate = $params['onDuplicate'] ??
NULL;
1449 // we need to get contact id $contributionContactID to
1450 // retrieve pledge details as well as to validate pledge ID
1452 // first need to check for update mode
1453 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
1454 ($params['contribution_id'] ||
$params['trxn_id'] ||
$params['invoice_id'])
1456 $contribution = new CRM_Contribute_DAO_Contribution();
1457 if ($params['contribution_id']) {
1458 $contribution->id
= $params['contribution_id'];
1460 elseif ($params['trxn_id']) {
1461 $contribution->trxn_id
= $params['trxn_id'];
1463 elseif ($params['invoice_id']) {
1464 $contribution->invoice_id
= $params['invoice_id'];
1467 if ($contribution->find(TRUE)) {
1468 $contributionContactID = $contribution->contact_id
;
1469 if (!$totalAmount) {
1470 $totalAmount = $contribution->total_amount
;
1474 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1478 // first get the contact id for given contribution record.
1479 if (!empty($params['contribution_contact_id'])) {
1480 $contributionContactID = $params['contribution_contact_id'];
1482 elseif (!empty($params['external_identifier'])) {
1483 require_once 'CRM/Contact/DAO/Contact.php';
1484 $contact = new CRM_Contact_DAO_Contact();
1485 $contact->external_identifier
= $params['external_identifier'];
1486 if ($contact->find(TRUE)) {
1487 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id
;
1490 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1494 // we need to get contribution contact using de dupe
1495 $error = $this->checkContactDuplicate($params);
1497 if (isset($error['error_message']['params'][0])) {
1498 $matchedIDs = explode(',', $error['error_message']['params'][0]);
1500 // check if only one contact is found
1501 if (count($matchedIDs) > 1) {
1502 return civicrm_api3_create_error($error['error_message']['message']);
1504 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
1507 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.');
1512 if (!empty($params['pledge_id'])) {
1513 if (CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
1514 return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.');
1516 $values['pledge_id'] = $params['pledge_id'];
1519 // check if there are any pledge related to this contact, with payments pending or in progress
1520 require_once 'CRM/Pledge/BAO/Pledge.php';
1521 $pledgeDetails = CRM_Pledge_BAO_Pledge
::getContactPledges($contributionContactID);
1523 if (empty($pledgeDetails)) {
1524 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.');
1526 if (count($pledgeDetails) > 1) {
1527 return civicrm_api3_create_error('This contact has more than one open pledge. Unable to determine which pledge to apply the contribution to. Contribution row was skipped.');
1530 // this mean we have only one pending / in progress pledge
1531 $values['pledge_id'] = $pledgeDetails[0];
1534 // we need to check if oldest payment amount equal to contribution amount
1535 require_once 'CRM/Pledge/BAO/PledgePayment.php';
1536 $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($values['pledge_id']);
1538 if ($pledgePaymentDetails['amount'] == $totalAmount) {
1539 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
1542 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.');
1546 case 'contribution_campaign_id':
1547 if (empty(CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $params['contribution_campaign_id']))) {
1548 return civicrm_api3_create_error('Invalid Campaign ID provided. Contribution row was skipped.');
1550 $values['contribution_campaign_id'] = $params['contribution_campaign_id'];
1554 // Hande name or label for fields with options.
1555 if (isset($fields[$key]) &&
1556 // Yay - just for a surprise we are inconsistent on whether we pass the pseudofield (payment_instrument)
1557 // or the field name (contribution_status_id)
1558 (!empty($fields[$key]['is_pseudofield_for']) ||
!empty($fields[$key]['pseudoconstant']))
1560 $realField = $fields[$key]['is_pseudofield_for'] ??
$key;
1561 $realFieldSpec = $fields[$realField];
1562 $values[$key] = $this->parsePseudoConstantField($value, $realFieldSpec);
1568 if (array_key_exists('note', $params)) {
1569 $values['note'] = $params['note'];
1573 // CRM_Contribute_BAO_Contribution::add() handles contribution_source
1574 // So, if $values contains contribution_source, convert it to source
1575 $changes = ['contribution_source' => 'source'];
1577 foreach ($changes as $orgVal => $changeVal) {
1578 if (isset($values[$orgVal])) {
1579 $values[$changeVal] = $values[$orgVal];
1580 unset($values[$orgVal]);