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
= 0;
197 $this->_warnings
= [];
198 $this->_pledgePaymentErrors
= [];
199 $this->_softCreditErrors
= [];
201 $this->progressImport($statusID);
202 $startTimestamp = $currTimestamp = $prevTimestamp = time();
205 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
207 if ($mode == self
::MODE_MAPFIELD
) {
211 $this->_activeFieldCount
= count($this->_activeFields
);
217 $values = fgetcsv($fd, 8192, $separator);
222 self
::encloseScrub($values);
224 // skip column header if we're not in mapfield mode
225 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
226 $skipColumnHeader = FALSE;
230 /* trim whitespace around the values */
233 foreach ($values as $k => $v) {
234 $values[$k] = trim($v, " \t\r\n");
237 if (CRM_Utils_System
::isNull($values)) {
241 $this->_totalCount++
;
243 if ($mode == self
::MODE_MAPFIELD
) {
244 $returnCode = CRM_Import_Parser
::VALID
;
246 // Note that import summary appears to be unused
247 elseif ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_SUMMARY
) {
248 $returnCode = $this->summary($values);
250 elseif ($mode == self
::MODE_IMPORT
) {
251 $returnCode = $this->import($onDuplicate, $values);
252 if ($statusID && (($this->_lineCount %
50) == 0)) {
253 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
257 $returnCode = self
::ERROR
;
260 // note that a line could be valid but still produce a warning
261 if ($returnCode == self
::VALID
) {
262 $this->_validCount++
;
263 if ($mode == self
::MODE_MAPFIELD
) {
264 $this->_rows
[] = $values;
265 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
269 if ($returnCode == self
::SOFT_CREDIT
) {
270 $this->_validSoftCreditRowCount++
;
271 $this->_validCount++
;
272 if ($mode == self
::MODE_MAPFIELD
) {
273 $this->_rows
[] = $values;
274 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
278 if ($returnCode == self
::PLEDGE_PAYMENT
) {
279 $this->_validPledgePaymentRowCount++
;
280 $this->_validCount++
;
281 if ($mode == self
::MODE_MAPFIELD
) {
282 $this->_rows
[] = $values;
283 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
287 if ($returnCode == self
::ERROR
) {
288 $this->_invalidRowCount++
;
289 $recordNumber = $this->_lineCount
;
290 if ($this->_haveColumnHeader
) {
293 array_unshift($values, $recordNumber);
294 $this->_errors
[] = $values;
297 if ($returnCode == self
::PLEDGE_PAYMENT_ERROR
) {
298 $this->_invalidPledgePaymentRowCount++
;
299 $recordNumber = $this->_lineCount
;
300 if ($this->_haveColumnHeader
) {
303 array_unshift($values, $recordNumber);
304 $this->_pledgePaymentErrors
[] = $values;
307 if ($returnCode == self
::SOFT_CREDIT_ERROR
) {
308 $this->_invalidSoftCreditRowCount++
;
309 $recordNumber = $this->_lineCount
;
310 if ($this->_haveColumnHeader
) {
313 array_unshift($values, $recordNumber);
314 $this->_softCreditErrors
[] = $values;
317 if ($returnCode == self
::DUPLICATE
) {
318 $this->_duplicateCount++
;
319 $recordNumber = $this->_lineCount
;
320 if ($this->_haveColumnHeader
) {
323 array_unshift($values, $recordNumber);
324 $this->_duplicates
[] = $values;
325 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
326 $this->_validCount++
;
330 // if we are done processing the maxNumber of lines, break
331 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
338 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
339 $customHeaders = $mapper;
341 $customfields = CRM_Core_BAO_CustomField
::getFields('Contribution');
342 foreach ($customHeaders as $key => $value) {
343 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
344 $customHeaders[$key] = $customfields[$id][0];
347 if ($this->_invalidRowCount
) {
348 // removed view url for invlaid contacts
349 $headers = array_merge([
353 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
354 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
357 if ($this->_invalidPledgePaymentRowCount
) {
358 // removed view url for invlaid contacts
359 $headers = array_merge([
363 $this->_pledgePaymentErrorsFileName
= self
::errorFileName(self
::PLEDGE_PAYMENT_ERROR
);
364 self
::exportCSV($this->_pledgePaymentErrorsFileName
, $headers, $this->_pledgePaymentErrors
);
367 if ($this->_invalidSoftCreditRowCount
) {
368 // removed view url for invlaid contacts
369 $headers = array_merge([
373 $this->_softCreditErrorsFileName
= self
::errorFileName(self
::SOFT_CREDIT_ERROR
);
374 self
::exportCSV($this->_softCreditErrorsFileName
, $headers, $this->_softCreditErrors
);
377 if ($this->_duplicateCount
) {
378 $headers = array_merge([
380 ts('View Contribution URL'),
383 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
384 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
390 * Given a list of the importable field keys that the user has selected
391 * set the active fields array to this list
393 * @param array $fieldKeys mapped array of values
395 public function setActiveFields($fieldKeys) {
396 $this->_activeFieldCount
= count($fieldKeys);
397 foreach ($fieldKeys as $key) {
398 if (empty($this->_fields
[$key])) {
399 $this->_activeFields
[] = new CRM_Contribute_Import_Field('', ts('- do not import -'));
402 $this->_activeFields
[] = clone($this->_fields
[$key]);
408 * Store the soft credit field information.
410 * This was perhaps done this way on the believe that a lot of code pain
411 * was worth it to avoid negligible-cost array iterations. Perhaps we could prioritise
412 * readability & maintainability next since we can just work with functions to retrieve
413 * data from the metadata.
415 * @param array $elements
417 public function setActiveFieldSoftCredit($elements) {
418 foreach ((array) $elements as $i => $element) {
419 $this->_activeFields
[$i]->_softCreditField
= $element;
424 * Store the soft credit field type information.
426 * This was perhaps done this way on the believe that a lot of code pain
427 * was worth it to avoid negligible-cost array iterations. Perhaps we could prioritise
428 * readability & maintainability next since we can just work with functions to retrieve
429 * data from the metadata.
431 * @param array $elements
433 public function setActiveFieldSoftCreditType($elements) {
434 foreach ((array) $elements as $i => $element) {
435 $this->_activeFields
[$i]->_softCreditType
= $element;
440 * Format the field values for input to the api.
443 * (reference ) associative array of name/value pairs
445 public function &getActiveFieldParams() {
447 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
448 if (isset($this->_activeFields
[$i]->_value
)) {
449 if (isset($this->_activeFields
[$i]->_softCreditField
)) {
450 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
451 $params[$this->_activeFields
[$i]->_name
] = [];
453 $params[$this->_activeFields
[$i]->_name
][$i][$this->_activeFields
[$i]->_softCreditField
] = $this->_activeFields
[$i]->_value
;
454 if (isset($this->_activeFields
[$i]->_softCreditType
)) {
455 $params[$this->_activeFields
[$i]->_name
][$i]['soft_credit_type_id'] = $this->_activeFields
[$i]->_softCreditType
;
459 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
460 if (!isset($this->_activeFields
[$i]->_softCreditField
)) {
461 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
470 * @param string $name
473 * @param string $headerPattern
474 * @param string $dataPattern
476 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
478 $this->_fields
['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
481 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
482 if (!array_key_exists($name, $tempField)) {
483 $this->_fields
[$name] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
486 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
487 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
494 * Store parser values.
496 * @param CRM_Core_Session $store
500 public function set($store, $mode = self
::MODE_SUMMARY
) {
501 $store->set('fileSize', $this->_fileSize
);
502 $store->set('lineCount', $this->_lineCount
);
503 $store->set('separator', $this->_separator
);
504 $store->set('fields', $this->getSelectValues());
505 $store->set('fieldTypes', $this->getSelectTypes());
507 $store->set('headerPatterns', $this->getHeaderPatterns());
508 $store->set('dataPatterns', $this->getDataPatterns());
509 $store->set('columnCount', $this->_activeFieldCount
);
511 $store->set('totalRowCount', $this->_totalCount
);
512 $store->set('validRowCount', $this->_validCount
);
513 $store->set('invalidRowCount', $this->_invalidRowCount
);
514 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount
);
515 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount
);
516 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount
);
517 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount
);
519 switch ($this->_contactType
) {
521 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
525 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
529 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
532 if ($this->_invalidRowCount
) {
533 $store->set('errorsFileName', $this->_errorFileName
);
535 if (isset($this->_rows
) && !empty($this->_rows
)) {
536 $store->set('dataValues', $this->_rows
);
539 if ($this->_invalidPledgePaymentRowCount
) {
540 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName
);
543 if ($this->_invalidSoftCreditRowCount
) {
544 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName
);
547 if ($mode == self
::MODE_IMPORT
) {
548 $store->set('duplicateRowCount', $this->_duplicateCount
);
549 if ($this->_duplicateCount
) {
550 $store->set('duplicatesFileName', $this->_duplicateFileName
);
556 * Export data to a CSV file.
558 * @param string $fileName
559 * @param array $header
562 public static function exportCSV($fileName, $header, $data) {
564 $fd = fopen($fileName, 'w');
566 foreach ($header as $key => $value) {
567 $header[$key] = "\"$value\"";
569 $config = CRM_Core_Config
::singleton();
570 $output[] = implode($config->fieldSeparator
, $header);
572 foreach ($data as $datum) {
573 foreach ($datum as $key => $value) {
574 if (isset($value[0]) && is_array($value)) {
575 foreach ($value[0] as $k1 => $v1) {
576 if ($k1 == 'location_type_id') {
583 $datum[$key] = "\"$value\"";
586 $output[] = implode($config->fieldSeparator
, $datum);
588 fwrite($fd, implode("\n", $output));
593 * Determines the file extension based on error code.
596 * Error code constant.
600 public static function errorFileName($type) {
606 $config = CRM_Core_Config
::singleton();
607 $fileName = $config->uploadDir
. "sqlImport";
610 case self
::SOFT_CREDIT_ERROR
:
611 $fileName .= '.softCreditErrors';
614 case self
::PLEDGE_PAYMENT_ERROR
:
615 $fileName .= '.pledgePaymentErrors';
619 $fileName = parent
::errorFileName($type);
627 * Determines the file name based on error code.
630 * Error code constant.
634 public static function saveFileName($type) {
641 case self
::SOFT_CREDIT_ERROR
:
642 $fileName = 'Import_Soft_Credit_Errors.csv';
645 case self
::PLEDGE_PAYMENT_ERROR
:
646 $fileName = 'Import_Pledge_Payment_Errors.csv';
650 $fileName = parent
::saveFileName($type);
658 * The initializer code, called before the processing
660 public function init() {
661 $fields = CRM_Contribute_BAO_Contribution
::importableFields($this->_contactType
, FALSE);
663 $fields = array_merge($fields,
666 'title' => ts('Soft Credit'),
667 'softCredit' => TRUE,
668 'headerPattern' => '/Soft Credit/i',
673 // add pledge fields only if its is enabled
674 if (CRM_Core_Permission
::access('CiviPledge')) {
676 'pledge_payment' => [
677 'title' => ts('Pledge Payment'),
678 'headerPattern' => '/Pledge Payment/i',
681 'title' => ts('Pledge ID'),
682 'headerPattern' => '/Pledge ID/i',
686 $fields = array_merge($fields, $pledgeFields);
688 foreach ($fields as $name => $field) {
689 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
690 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
691 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
692 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
695 $this->_newContributions
= [];
697 $this->setActiveFields($this->_mapperKeys
);
698 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit
);
699 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType
);
701 // FIXME: we should do this in one place together with Form/MapField.php
702 $this->_contactIdIndex
= -1;
705 foreach ($this->_mapperKeys
as $key) {
707 case 'contribution_contact_id':
708 $this->_contactIdIndex
= $index;
717 * Handle the values in summary mode.
719 * @param array $values
720 * The array of values belonging to this line.
723 * CRM_Import_Parser::VALID or CRM_Import_Parser::ERROR
725 public function summary(&$values) {
726 $this->setActiveFieldValues($values);
728 $params = $this->getActiveFieldParams();
731 $errorMessage = implode('; ', $this->formatDateFields($params));
732 //date-Format part ends
734 $params['contact_type'] = 'Contribution';
736 //checking error in custom data
737 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
740 $tempMsg = "Invalid value for field(s) : $errorMessage";
741 array_unshift($values, $tempMsg);
742 $errorMessage = NULL;
743 return CRM_Import_Parser
::ERROR
;
746 return CRM_Import_Parser
::VALID
;
750 * Handle the values in import mode.
752 * @param int $onDuplicate
753 * The code for what action to take on duplicates.
754 * @param array $values
755 * The array of values belonging to this line.
758 * the result of this processing - one of
759 * - CRM_Import_Parser::VALID
760 * - CRM_Import_Parser::ERROR
761 * - CRM_Import_Parser::SOFT_CREDIT_ERROR
762 * - CRM_Import_Parser::PLEDGE_PAYMENT_ERROR
763 * - CRM_Import_Parser::DUPLICATE
764 * - CRM_Import_Parser::SOFT_CREDIT (successful creation)
765 * - CRM_Import_Parser::PLEDGE_PAYMENT (successful creation)
767 public function import($onDuplicate, &$values) {
768 // first make sure this is a valid line
769 $response = $this->summary($values);
770 if ($response != CRM_Import_Parser
::VALID
) {
771 return CRM_Import_Parser
::ERROR
;
774 $params = &$this->getActiveFieldParams();
775 $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE];
778 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
779 $params['total_amount'] = '0.00';
781 $this->formatInput($params, $formatted);
783 static $indieFields = NULL;
784 if ($indieFields == NULL) {
785 $tempIndieFields = CRM_Contribute_DAO_Contribution
::import();
786 $indieFields = $tempIndieFields;
790 foreach ($params as $key => $field) {
791 if ($field == NULL ||
$field === '') {
794 $paramValues[$key] = $field;
797 //import contribution record according to select contact type
798 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
&&
799 (!empty($paramValues['contribution_contact_id']) ||
!empty($paramValues['external_identifier']))
801 $paramValues['contact_type'] = $this->_contactType
;
803 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
804 (!empty($paramValues['contribution_id']) ||
!empty($values['trxn_id']) ||
!empty($paramValues['invoice_id']))
806 $paramValues['contact_type'] = $this->_contactType
;
808 elseif (!empty($params['soft_credit'])) {
809 $paramValues['contact_type'] = $this->_contactType
;
811 elseif (!empty($paramValues['pledge_payment'])) {
812 $paramValues['contact_type'] = $this->_contactType
;
815 //need to pass $onDuplicate to check import mode.
816 if (!empty($paramValues['pledge_payment'])) {
817 $paramValues['onDuplicate'] = $onDuplicate;
819 $formatError = $this->deprecatedFormatParams($paramValues, $formatted, TRUE, $onDuplicate);
822 array_unshift($values, $formatError['error_message']);
823 if (CRM_Utils_Array
::value('error_data', $formatError) == 'soft_credit') {
824 return self
::SOFT_CREDIT_ERROR
;
826 if (CRM_Utils_Array
::value('error_data', $formatError) == 'pledge_payment') {
827 return self
::PLEDGE_PAYMENT_ERROR
;
829 return CRM_Import_Parser
::ERROR
;
832 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
833 //fix for CRM-2219 - Update Contribution
834 // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
835 if (!empty($paramValues['invoice_id']) ||
!empty($paramValues['trxn_id']) ||
!empty($paramValues['contribution_id'])) {
837 'id' => $paramValues['contribution_id'] ??
NULL,
838 'trxn_id' => $paramValues['trxn_id'] ??
NULL,
839 'invoice_id' => $paramValues['invoice_id'] ??
NULL,
841 $ids['contribution'] = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($dupeIds);
843 if ($ids['contribution']) {
844 $formatted['id'] = $ids['contribution'];
846 if (!empty($paramValues['note'])) {
848 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id');
849 $daoNote = new CRM_Core_BAO_Note();
850 $daoNote->entity_table
= 'civicrm_contribution';
851 $daoNote->entity_id
= $ids['contribution'];
852 if ($daoNote->find(TRUE)) {
853 $noteID['id'] = $daoNote->id
;
857 'entity_table' => 'civicrm_contribution',
858 'note' => $paramValues['note'],
859 'entity_id' => $ids['contribution'],
860 'contact_id' => $contactID,
862 CRM_Core_BAO_Note
::add($noteParams, $noteID);
863 unset($formatted['note']);
866 //need to check existing soft credit contribution, CRM-3968
867 if (!empty($formatted['soft_credit'])) {
869 'contact_id' => $formatted['soft_credit'],
870 'contribution_id' => $ids['contribution'],
873 //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
874 $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($dupeSoftCredit['contribution_id']);
875 if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) {
876 foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) {
877 if (!empty($existingSoftCreditValues['soft_credit_id'])) {
878 civicrm_api3('ContributionSoft', 'delete', [
879 'id' => $existingSoftCreditValues['soft_credit_id'],
887 $formatted['id'] = $ids['contribution'];
889 $newContribution = civicrm_api3('contribution', 'create', $formatted);
890 $this->_newContributions
[] = $newContribution['id'];
892 //return soft valid since we need to show how soft credits were added
893 if (!empty($formatted['soft_credit'])) {
894 return self
::SOFT_CREDIT
;
897 // process pledge payment assoc w/ the contribution
898 return $this->processPledgePayments($formatted);
901 'id' => 'Contribution ID',
902 'trxn_id' => 'Transaction ID',
903 'invoice_id' => 'Invoice ID',
905 foreach ($dupeIds as $k => $v) {
907 $errorMsg[] = "$labels[$k] $v";
910 $errorMsg = implode(' AND ', $errorMsg);
911 array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.');
912 return CRM_Import_Parser
::ERROR
;
916 if ($this->_contactIdIndex
< 0) {
918 $error = $this->checkContactDuplicate($paramValues);
920 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
921 $matchedIDs = explode(',', $error['error_message']['params'][0]);
922 if (count($matchedIDs) > 1) {
923 array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported');
924 return CRM_Import_Parser
::ERROR
;
926 $cid = $matchedIDs[0];
927 $formatted['contact_id'] = $cid;
929 $newContribution = civicrm_api('contribution', 'create', $formatted);
930 if (civicrm_error($newContribution)) {
931 if (is_array($newContribution['error_message'])) {
932 array_unshift($values, $newContribution['error_message']['message']);
933 if ($newContribution['error_message']['params'][0]) {
934 return CRM_Import_Parser
::DUPLICATE
;
938 array_unshift($values, $newContribution['error_message']);
939 return CRM_Import_Parser
::ERROR
;
943 $this->_newContributions
[] = $newContribution['id'];
944 $formatted['contribution_id'] = $newContribution['id'];
946 //return soft valid since we need to show how soft credits were added
947 if (!empty($formatted['soft_credit'])) {
948 return self
::SOFT_CREDIT
;
951 // process pledge payment assoc w/ the contribution
952 return $this->processPledgePayments($formatted);
955 // Using new Dedupe rule.
957 'contact_type' => $this->_contactType
,
958 'used' => 'Unsupervised',
960 $fieldsArray = CRM_Dedupe_BAO_DedupeRule
::dedupeRuleFields($ruleParams);
962 foreach ($fieldsArray as $value) {
963 if (array_key_exists(trim($value), $params)) {
964 $paramValue = $params[trim($value)];
965 if (is_array($paramValue)) {
966 $disp .= $params[trim($value)][0][trim($value)] . " ";
969 $disp .= $params[trim($value)] . " ";
974 if (!empty($params['external_identifier'])) {
976 $disp .= "AND {$params['external_identifier']}";
979 $disp = $params['external_identifier'];
983 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
984 return CRM_Import_Parser
::ERROR
;
987 if (!empty($paramValues['external_identifier'])) {
988 $checkCid = new CRM_Contact_DAO_Contact();
989 $checkCid->external_identifier
= $paramValues['external_identifier'];
990 $checkCid->find(TRUE);
991 if ($checkCid->id
!= $formatted['contact_id']) {
992 array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
993 return CRM_Import_Parser
::ERROR
;
996 $newContribution = civicrm_api('contribution', 'create', $formatted);
997 if (civicrm_error($newContribution)) {
998 if (is_array($newContribution['error_message'])) {
999 array_unshift($values, $newContribution['error_message']['message']);
1000 if ($newContribution['error_message']['params'][0]) {
1001 return CRM_Import_Parser
::DUPLICATE
;
1005 array_unshift($values, $newContribution['error_message']);
1006 return CRM_Import_Parser
::ERROR
;
1010 $this->_newContributions
[] = $newContribution['id'];
1011 $formatted['contribution_id'] = $newContribution['id'];
1013 //return soft valid since we need to show how soft credits were added
1014 if (!empty($formatted['soft_credit'])) {
1015 return self
::SOFT_CREDIT
;
1018 // process pledge payment assoc w/ the contribution
1019 return $this->processPledgePayments($formatted);
1023 * Process pledge payments.
1025 * @param array $formatted
1029 private function processPledgePayments(array $formatted) {
1030 if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) {
1031 //get completed status
1032 $completeStatusID = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
1034 //need to update payment record to map contribution_id
1035 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'],
1036 'contribution_id', $formatted['contribution_id']
1039 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($formatted['pledge_id'],
1040 [$formatted['pledge_payment_id']],
1043 $formatted['total_amount']
1046 return self
::PLEDGE_PAYMENT
;
1051 * Get the array of successfully imported contribution id's
1055 public function &getImportedContributions() {
1056 return $this->_newContributions
;
1060 * Format date fields from input to mysql.
1062 * @param array $params
1065 * Error messages, if any.
1067 public function formatDateFields(&$params) {
1069 $dateType = CRM_Core_Session
::singleton()->get('dateTypes');
1070 foreach ($params as $key => $val) {
1073 case 'receive_date':
1074 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1075 $params[$key] = $dateValue;
1078 $errorMessage[] = ts('Receive Date');
1083 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1084 $params[$key] = $dateValue;
1087 $errorMessage[] = ts('Cancel Date');
1091 case 'receipt_date':
1092 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1093 $params[$key] = $dateValue;
1096 $errorMessage[] = ts('Receipt date');
1100 case 'thankyou_date':
1101 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
1102 $params[$key] = $dateValue;
1105 $errorMessage[] = ts('Thankyou Date');
1111 return $errorMessage;
1115 * Format input params to suit api handling.
1117 * Over time all the parts of deprecatedFormatParams
1118 * and all the parts of the import function on this class that relate to
1119 * reformatting input should be moved here and tests should be added in
1120 * CRM_Contribute_Import_Parser_ContributionTest.
1122 * @param array $params
1123 * @param array $formatted
1125 public function formatInput(&$params, &$formatted = []) {
1126 $dateType = CRM_Core_Session
::singleton()->get('dateTypes');
1127 $customDataType = !empty($params['contact_type']) ?
$params['contact_type'] : 'Contribution';
1128 $customFields = CRM_Core_BAO_CustomField
::getFields($customDataType);
1129 // @todo call formatDateFields & move custom data handling there.
1130 // Also note error handling for dates is currently in deprecatedFormatParams
1131 // we should use the error handling in formatDateFields.
1132 foreach ($params as $key => $val) {
1133 // @todo - call formatDateFields instead.
1136 case 'receive_date':
1138 case 'receipt_date':
1139 case 'thankyou_date':
1140 $params[$key] = CRM_Utils_Date
::formatDate($params[$key], $dateType);
1143 case 'pledge_payment':
1144 $params[$key] = CRM_Utils_String
::strtobool($val);
1148 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1149 if ($customFields[$customFieldID]['data_type'] == 'Date') {
1150 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
1151 unset($params[$key]);
1153 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1154 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
1162 * take the input parameter list as specified in the data model and
1163 * convert it into the same format that we use in QF and BAO object
1165 * @param array $params
1166 * Associative array of property name/value
1167 * pairs to insert in new contact.
1168 * @param array $values
1169 * The reformatted properties that we can use internally.
1170 * @param bool $create
1171 * @param int $onDuplicate
1173 * @return array|CRM_Error
1175 private function deprecatedFormatParams($params, &$values, $create = FALSE, $onDuplicate = NULL) {
1176 require_once 'CRM/Utils/DeprecatedUtils.php';
1177 // copy all the contribution fields as is
1178 require_once 'api/v3/utils.php';
1179 $fields = CRM_Core_DAO
::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution');
1181 _civicrm_api3_store_values($fields, $params, $values);
1183 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
1185 foreach ($params as $key => $value) {
1186 // ignore empty values or empty arrays etc
1187 if (CRM_Utils_System
::isNull($value)) {
1191 // Handling Custom Data
1192 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1193 $values[$key] = $value;
1194 $type = $customFields[$customFieldID]['html_type'];
1195 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID])) {
1196 $values[$key] = self
::unserializeCustomValue($customFieldID, $value, $type);
1198 elseif ($type == 'Select' ||
$type == 'Radio' ||
1199 ($type == 'Autocomplete-Select' &&
1200 $customFields[$customFieldID]['data_type'] == 'String'
1203 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1204 foreach ($customOption as $customFldID => $customValue) {
1205 $val = $customValue['value'] ??
NULL;
1206 $label = $customValue['label'] ??
NULL;
1207 $label = strtolower($label);
1208 $value = strtolower(trim($value));
1209 if (($value == $label) ||
($value == strtolower($val))) {
1210 $values[$key] = $val;
1218 case 'contribution_contact_id':
1219 if (!CRM_Utils_Rule
::integer($value)) {
1220 return civicrm_api3_create_error("contact_id not valid: $value");
1222 $dao = new CRM_Core_DAO();
1224 $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value",
1228 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
1230 elseif ($svq == 1) {
1231 return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact.");
1234 $values['contact_id'] = $values['contribution_contact_id'];
1235 unset($values['contribution_contact_id']);
1238 case 'contact_type':
1239 // import contribution record according to select contact type
1240 require_once 'CRM/Contact/DAO/Contact.php';
1241 $contactType = new CRM_Contact_DAO_Contact();
1242 $contactId = $params['contribution_contact_id'] ??
NULL;
1243 $externalId = $params['external_identifier'] ??
NULL;
1244 $email = $params['email'] ??
NULL;
1245 //when insert mode check contact id or external identifier
1246 if ($contactId ||
$externalId) {
1247 $contactType->id
= $contactId;
1248 $contactType->external_identifier
= $externalId;
1249 if ($contactType->find(TRUE)) {
1250 if ($params['contact_type'] != $contactType->contact_type
) {
1251 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
1256 if (!CRM_Utils_Rule
::email($email)) {
1257 return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped");
1260 // get the contact id from duplicate contact rule, if more than one contact is returned
1261 // we should return error, since current interface allows only one-one mapping
1264 'contact_type' => $params['contact_type'],
1266 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
1267 if (!$checkDedupe['is_error']) {
1268 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email. Row was skipped");
1270 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
1271 if (count($matchingContactIds) > 1) {
1272 return civicrm_api3_create_error("Invalid email address(duplicate) $email. Row was skipped");
1274 if (count($matchingContactIds) == 1) {
1275 $params['contribution_contact_id'] = $matchingContactIds[0];
1278 elseif (!empty($params['contribution_id']) ||
!empty($params['trxn_id']) ||
!empty($params['invoice_id'])) {
1279 // when update mode check contribution id or trxn id or
1281 $contactId = new CRM_Contribute_DAO_Contribution();
1282 if (!empty($params['contribution_id'])) {
1283 $contactId->id
= $params['contribution_id'];
1285 elseif (!empty($params['trxn_id'])) {
1286 $contactId->trxn_id
= $params['trxn_id'];
1288 elseif (!empty($params['invoice_id'])) {
1289 $contactId->invoice_id
= $params['invoice_id'];
1291 if ($contactId->find(TRUE)) {
1292 $contactType->id
= $contactId->contact_id
;
1293 if ($contactType->find(TRUE)) {
1294 if ($params['contact_type'] != $contactType->contact_type
) {
1295 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
1301 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1302 return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
1307 case 'receive_date':
1309 case 'receipt_date':
1310 case 'thankyou_date':
1311 if (!CRM_Utils_Rule
::dateTime($value)) {
1312 return civicrm_api3_create_error("$key not a valid date: $value");
1316 case 'non_deductible_amount':
1317 case 'total_amount':
1320 // @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled.
1321 if (!CRM_Utils_Rule
::money($value)) {
1322 return civicrm_api3_create_error("$key not a valid amount: $value");
1327 if (!CRM_Utils_Rule
::currencyCode($value)) {
1328 return civicrm_api3_create_error("currency not a valid code: $value");
1332 case 'financial_type':
1333 // @todo add test like testPaymentTypeLabel & remove these lines in favour of 'default' part of switch.
1334 require_once 'CRM/Contribute/PseudoConstant.php';
1335 $contriTypes = CRM_Contribute_PseudoConstant
::financialType();
1336 foreach ($contriTypes as $val => $type) {
1337 if (strtolower($value) == strtolower($type)) {
1338 $values['financial_type_id'] = $val;
1342 if (empty($values['financial_type_id'])) {
1343 return civicrm_api3_create_error("Financial Type is not valid: $value");
1348 // import contribution record according to select contact type
1349 // validate contact id and external identifier.
1350 $value[$key] = $mismatchContactType = $softCreditContactIds = '';
1351 if (isset($params[$key]) && is_array($params[$key])) {
1352 foreach ($params[$key] as $softKey => $softParam) {
1353 $contactId = $softParam['contact_id'] ??
NULL;
1354 $externalId = $softParam['external_identifier'] ??
NULL;
1355 $email = $softParam['email'] ??
NULL;
1356 if ($contactId ||
$externalId) {
1357 require_once 'CRM/Contact/DAO/Contact.php';
1358 $contact = new CRM_Contact_DAO_Contact();
1359 $contact->id
= $contactId;
1360 $contact->external_identifier
= $externalId;
1362 if (!$contact->find(TRUE)) {
1363 $field = $contactId ?
ts('Contact ID') : ts('External ID');
1364 $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.",
1365 [1 => $field, 2 => $contactId ?
$contactId : $externalId]);
1369 return civicrm_api3_create_error($errorMsg);
1372 // finally get soft credit contact id.
1373 $values[$key][$softKey] = $softParam;
1374 $values[$key][$softKey]['contact_id'] = $contact->id
;
1377 if (!CRM_Utils_Rule
::email($email)) {
1378 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
1381 // get the contact id from duplicate contact rule, if more than one contact is returned
1382 // we should return error, since current interface allows only one-one mapping
1385 'contact_type' => $params['contact_type'],
1387 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
1388 if (!$checkDedupe['is_error']) {
1389 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped");
1391 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
1392 if (count($matchingContactIds) > 1) {
1393 return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped");
1395 if (count($matchingContactIds) == 1) {
1396 $contactId = $matchingContactIds[0];
1397 unset($softParam['email']);
1398 $values[$key][$softKey] = $softParam +
['contact_id' => $contactId];
1405 case 'pledge_payment':
1408 // giving respect to pledge_payment flag.
1409 if (empty($params['pledge_payment'])) {
1413 // get total amount of from import fields
1414 $totalAmount = $params['total_amount'] ??
NULL;
1416 $onDuplicate = $params['onDuplicate'] ??
NULL;
1418 // we need to get contact id $contributionContactID to
1419 // retrieve pledge details as well as to validate pledge ID
1421 // first need to check for update mode
1422 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
1423 ($params['contribution_id'] ||
$params['trxn_id'] ||
$params['invoice_id'])
1425 $contribution = new CRM_Contribute_DAO_Contribution();
1426 if ($params['contribution_id']) {
1427 $contribution->id
= $params['contribution_id'];
1429 elseif ($params['trxn_id']) {
1430 $contribution->trxn_id
= $params['trxn_id'];
1432 elseif ($params['invoice_id']) {
1433 $contribution->invoice_id
= $params['invoice_id'];
1436 if ($contribution->find(TRUE)) {
1437 $contributionContactID = $contribution->contact_id
;
1438 if (!$totalAmount) {
1439 $totalAmount = $contribution->total_amount
;
1443 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1447 // first get the contact id for given contribution record.
1448 if (!empty($params['contribution_contact_id'])) {
1449 $contributionContactID = $params['contribution_contact_id'];
1451 elseif (!empty($params['external_identifier'])) {
1452 require_once 'CRM/Contact/DAO/Contact.php';
1453 $contact = new CRM_Contact_DAO_Contact();
1454 $contact->external_identifier
= $params['external_identifier'];
1455 if ($contact->find(TRUE)) {
1456 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id
;
1459 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1463 // we need to get contribution contact using de dupe
1464 $error = $this->checkContactDuplicate($params);
1466 if (isset($error['error_message']['params'][0])) {
1467 $matchedIDs = explode(',', $error['error_message']['params'][0]);
1469 // check if only one contact is found
1470 if (count($matchedIDs) > 1) {
1471 return civicrm_api3_create_error($error['error_message']['message']);
1473 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
1476 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.');
1481 if (!empty($params['pledge_id'])) {
1482 if (CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
1483 return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.');
1485 $values['pledge_id'] = $params['pledge_id'];
1488 // check if there are any pledge related to this contact, with payments pending or in progress
1489 require_once 'CRM/Pledge/BAO/Pledge.php';
1490 $pledgeDetails = CRM_Pledge_BAO_Pledge
::getContactPledges($contributionContactID);
1492 if (empty($pledgeDetails)) {
1493 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.');
1495 if (count($pledgeDetails) > 1) {
1496 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.');
1499 // this mean we have only one pending / in progress pledge
1500 $values['pledge_id'] = $pledgeDetails[0];
1503 // we need to check if oldest payment amount equal to contribution amount
1504 require_once 'CRM/Pledge/BAO/PledgePayment.php';
1505 $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($values['pledge_id']);
1507 if ($pledgePaymentDetails['amount'] == $totalAmount) {
1508 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
1511 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.');
1515 case 'contribution_campaign_id':
1516 if (empty(CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Campaign', $params['contribution_campaign_id']))) {
1517 return civicrm_api3_create_error('Invalid Campaign ID provided. Contribution row was skipped.');
1519 $values['contribution_campaign_id'] = $params['contribution_campaign_id'];
1523 // Hande name or label for fields with options.
1524 if (isset($fields[$key]) &&
1525 // Yay - just for a surprise we are inconsistent on whether we pass the pseudofield (payment_instrument)
1526 // or the field name (contribution_status_id)
1527 (!empty($fields[$key]['is_pseudofield_for']) ||
!empty($fields[$key]['pseudoconstant']))
1529 $realField = $fields[$key]['is_pseudofield_for'] ??
$key;
1530 $realFieldSpec = $fields[$realField];
1531 $values[$key] = $this->parsePseudoConstantField($value, $realFieldSpec);
1537 if (array_key_exists('note', $params)) {
1538 $values['note'] = $params['note'];
1542 // CRM_Contribute_BAO_Contribution::add() handles contribution_source
1543 // So, if $values contains contribution_source, convert it to source
1544 $changes = ['contribution_source' => 'source'];
1546 foreach ($changes as $orgVal => $changeVal) {
1547 if (isset($values[$orgVal])) {
1548 $values[$changeVal] = $values[$orgVal];
1549 unset($values[$orgVal]);