[REF] [Import] Contribution import - clarify return codes, remove handling for unused...
[civicrm-core.git] / CRM / Contribute / Import / Parser / Contribution.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class to parse contribution csv files.
20 */
21 class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
22
23 protected $_mapperKeys;
24
25 private $_contactIdIndex;
26
27 protected $_mapperSoftCredit;
28 //protected $_mapperPhoneType;
29
30 /**
31 * Array of successfully imported contribution id's
32 *
33 * @var array
34 */
35 protected $_newContributions;
36
37 /**
38 * Class constructor.
39 *
40 * @param $mapperKeys
41 * @param array $mapperSoftCredit
42 * @param null $mapperPhoneType
43 * @param array $mapperSoftCreditType
44 */
45 public function __construct(&$mapperKeys = [], $mapperSoftCredit = [], $mapperPhoneType = NULL, $mapperSoftCreditType = []) {
46 parent::__construct();
47 $this->_mapperKeys = &$mapperKeys;
48 $this->_mapperSoftCredit = &$mapperSoftCredit;
49 $this->_mapperSoftCreditType = &$mapperSoftCreditType;
50 }
51
52 /**
53 * Contribution-specific result codes
54 * @see CRM_Import_Parser result code constants
55 */
56 const SOFT_CREDIT = 512, SOFT_CREDIT_ERROR = 1024, PLEDGE_PAYMENT = 2048, PLEDGE_PAYMENT_ERROR = 4096;
57
58 /**
59 * @var string
60 */
61 protected $_fileName;
62
63 /**
64 * Imported file size
65 * @var int
66 */
67 protected $_fileSize;
68
69 /**
70 * Separator being used
71 * @var string
72 */
73 protected $_separator;
74
75 /**
76 * Total number of lines in file
77 * @var int
78 */
79 protected $_lineCount;
80
81 /**
82 * Running total number of valid soft credit rows
83 * @var int
84 */
85 protected $_validSoftCreditRowCount;
86
87 /**
88 * Running total number of invalid soft credit rows
89 * @var int
90 */
91 protected $_invalidSoftCreditRowCount;
92
93 /**
94 * Running total number of valid pledge payment rows
95 * @var int
96 */
97 protected $_validPledgePaymentRowCount;
98
99 /**
100 * Running total number of invalid pledge payment rows
101 * @var int
102 */
103 protected $_invalidPledgePaymentRowCount;
104
105 /**
106 * Array of pledge payment error lines, bounded by MAX_ERROR
107 * @var array
108 */
109 protected $_pledgePaymentErrors;
110
111 /**
112 * Array of pledge payment error lines, bounded by MAX_ERROR
113 * @var array
114 */
115 protected $_softCreditErrors;
116
117 /**
118 * Filename of pledge payment error data
119 *
120 * @var string
121 */
122 protected $_pledgePaymentErrorsFileName;
123
124 /**
125 * Filename of soft credit error data
126 *
127 * @var string
128 */
129 protected $_softCreditErrorsFileName;
130
131 /**
132 * Whether the file has a column header or not
133 *
134 * @var bool
135 */
136 protected $_haveColumnHeader;
137
138 /**
139 * @param string $fileName
140 * @param string $separator
141 * @param $mapper
142 * @param bool $skipColumnHeader
143 * @param int $mode
144 * @param int $contactType
145 * @param int $onDuplicate
146 * @param int $statusID
147 * @param int $totalRowCount
148 *
149 * @return mixed
150 * @throws Exception
151 */
152 public function run(
153 $fileName,
154 $separator,
155 $mapper,
156 $skipColumnHeader = FALSE,
157 $mode = self::MODE_PREVIEW,
158 $contactType = self::CONTACT_INDIVIDUAL,
159 $onDuplicate = self::DUPLICATE_SKIP,
160 $statusID = NULL,
161 $totalRowCount = NULL
162 ) {
163 if (!is_array($fileName)) {
164 throw new CRM_Core_Exception('Unable to determine import file');
165 }
166 $fileName = $fileName['name'];
167
168 switch ($contactType) {
169 case self::CONTACT_INDIVIDUAL:
170 $this->_contactType = 'Individual';
171 break;
172
173 case self::CONTACT_HOUSEHOLD:
174 $this->_contactType = 'Household';
175 break;
176
177 case self::CONTACT_ORGANIZATION:
178 $this->_contactType = 'Organization';
179 }
180
181 $this->init();
182
183 $this->_haveColumnHeader = $skipColumnHeader;
184
185 $this->_separator = $separator;
186
187 $fd = fopen($fileName, "r");
188 if (!$fd) {
189 return FALSE;
190 }
191
192 $this->_lineCount = $this->_validSoftCreditRowCount = $this->_validPledgePaymentRowCount = 0;
193 $this->_invalidRowCount = $this->_validCount = $this->_invalidSoftCreditRowCount = $this->_invalidPledgePaymentRowCount = 0;
194 $this->_totalCount = 0;
195
196 $this->_errors = [];
197 $this->_warnings = [];
198 $this->_pledgePaymentErrors = [];
199 $this->_softCreditErrors = [];
200 if ($statusID) {
201 $this->progressImport($statusID);
202 $startTimestamp = $currTimestamp = $prevTimestamp = time();
203 }
204
205 $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
206
207 if ($mode == self::MODE_MAPFIELD) {
208 $this->_rows = [];
209 }
210 else {
211 $this->_activeFieldCount = count($this->_activeFields);
212 }
213
214 while (!feof($fd)) {
215 $this->_lineCount++;
216
217 $values = fgetcsv($fd, 8192, $separator);
218 if (!$values) {
219 continue;
220 }
221
222 self::encloseScrub($values);
223
224 // skip column header if we're not in mapfield mode
225 if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
226 $skipColumnHeader = FALSE;
227 continue;
228 }
229
230 /* trim whitespace around the values */
231
232 $empty = TRUE;
233 foreach ($values as $k => $v) {
234 $values[$k] = trim($v, " \t\r\n");
235 }
236
237 if (CRM_Utils_System::isNull($values)) {
238 continue;
239 }
240
241 $this->_totalCount++;
242
243 if ($mode == self::MODE_MAPFIELD) {
244 $returnCode = CRM_Import_Parser::VALID;
245 }
246 // Note that import summary appears to be unused
247 elseif ($mode == self::MODE_PREVIEW || $mode == self::MODE_SUMMARY) {
248 $returnCode = $this->summary($values);
249 }
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);
254 }
255 }
256 else {
257 $returnCode = self::ERROR;
258 }
259
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));
266 }
267 }
268
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));
275 }
276 }
277
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));
284 }
285 }
286
287 if ($returnCode == self::ERROR) {
288 $this->_invalidRowCount++;
289 $recordNumber = $this->_lineCount;
290 if ($this->_haveColumnHeader) {
291 $recordNumber--;
292 }
293 array_unshift($values, $recordNumber);
294 $this->_errors[] = $values;
295 }
296
297 if ($returnCode == self::PLEDGE_PAYMENT_ERROR) {
298 $this->_invalidPledgePaymentRowCount++;
299 $recordNumber = $this->_lineCount;
300 if ($this->_haveColumnHeader) {
301 $recordNumber--;
302 }
303 array_unshift($values, $recordNumber);
304 $this->_pledgePaymentErrors[] = $values;
305 }
306
307 if ($returnCode == self::SOFT_CREDIT_ERROR) {
308 $this->_invalidSoftCreditRowCount++;
309 $recordNumber = $this->_lineCount;
310 if ($this->_haveColumnHeader) {
311 $recordNumber--;
312 }
313 array_unshift($values, $recordNumber);
314 $this->_softCreditErrors[] = $values;
315 }
316
317 if ($returnCode == self::DUPLICATE) {
318 $this->_duplicateCount++;
319 $recordNumber = $this->_lineCount;
320 if ($this->_haveColumnHeader) {
321 $recordNumber--;
322 }
323 array_unshift($values, $recordNumber);
324 $this->_duplicates[] = $values;
325 if ($onDuplicate != self::DUPLICATE_SKIP) {
326 $this->_validCount++;
327 }
328 }
329
330 // if we are done processing the maxNumber of lines, break
331 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
332 break;
333 }
334 }
335
336 fclose($fd);
337
338 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
339 $customHeaders = $mapper;
340
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];
345 }
346 }
347 if ($this->_invalidRowCount) {
348 // removed view url for invlaid contacts
349 $headers = array_merge([
350 ts('Line Number'),
351 ts('Reason'),
352 ], $customHeaders);
353 $this->_errorFileName = self::errorFileName(self::ERROR);
354 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
355 }
356
357 if ($this->_invalidPledgePaymentRowCount) {
358 // removed view url for invlaid contacts
359 $headers = array_merge([
360 ts('Line Number'),
361 ts('Reason'),
362 ], $customHeaders);
363 $this->_pledgePaymentErrorsFileName = self::errorFileName(self::PLEDGE_PAYMENT_ERROR);
364 self::exportCSV($this->_pledgePaymentErrorsFileName, $headers, $this->_pledgePaymentErrors);
365 }
366
367 if ($this->_invalidSoftCreditRowCount) {
368 // removed view url for invlaid contacts
369 $headers = array_merge([
370 ts('Line Number'),
371 ts('Reason'),
372 ], $customHeaders);
373 $this->_softCreditErrorsFileName = self::errorFileName(self::SOFT_CREDIT_ERROR);
374 self::exportCSV($this->_softCreditErrorsFileName, $headers, $this->_softCreditErrors);
375 }
376
377 if ($this->_duplicateCount) {
378 $headers = array_merge([
379 ts('Line Number'),
380 ts('View Contribution URL'),
381 ], $customHeaders);
382
383 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
384 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
385 }
386 }
387 }
388
389 /**
390 * Given a list of the importable field keys that the user has selected
391 * set the active fields array to this list
392 *
393 * @param array $fieldKeys mapped array of values
394 */
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 -'));
400 }
401 else {
402 $this->_activeFields[] = clone($this->_fields[$key]);
403 }
404 }
405 }
406
407 /**
408 * Store the soft credit field information.
409 *
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.
414 *
415 * @param array $elements
416 */
417 public function setActiveFieldSoftCredit($elements) {
418 foreach ((array) $elements as $i => $element) {
419 $this->_activeFields[$i]->_softCreditField = $element;
420 }
421 }
422
423 /**
424 * Store the soft credit field type information.
425 *
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.
430 *
431 * @param array $elements
432 */
433 public function setActiveFieldSoftCreditType($elements) {
434 foreach ((array) $elements as $i => $element) {
435 $this->_activeFields[$i]->_softCreditType = $element;
436 }
437 }
438
439 /**
440 * Format the field values for input to the api.
441 *
442 * @return array
443 * (reference ) associative array of name/value pairs
444 */
445 public function &getActiveFieldParams() {
446 $params = [];
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] = [];
452 }
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;
456 }
457 }
458
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;
462 }
463 }
464 }
465 }
466 return $params;
467 }
468
469 /**
470 * @param string $name
471 * @param $title
472 * @param int $type
473 * @param string $headerPattern
474 * @param string $dataPattern
475 */
476 public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
477 if (empty($name)) {
478 $this->_fields['doNotImport'] = new CRM_Contribute_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
479 }
480 else {
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);
484 }
485 else {
486 $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
487 CRM_Utils_Array::value('hasLocationType', $tempField[$name])
488 );
489 }
490 }
491 }
492
493 /**
494 * Store parser values.
495 *
496 * @param CRM_Core_Session $store
497 *
498 * @param int $mode
499 */
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());
506
507 $store->set('headerPatterns', $this->getHeaderPatterns());
508 $store->set('dataPatterns', $this->getDataPatterns());
509 $store->set('columnCount', $this->_activeFieldCount);
510
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);
518
519 switch ($this->_contactType) {
520 case 'Individual':
521 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
522 break;
523
524 case 'Household':
525 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
526 break;
527
528 case 'Organization':
529 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
530 }
531
532 if ($this->_invalidRowCount) {
533 $store->set('errorsFileName', $this->_errorFileName);
534 }
535 if (isset($this->_rows) && !empty($this->_rows)) {
536 $store->set('dataValues', $this->_rows);
537 }
538
539 if ($this->_invalidPledgePaymentRowCount) {
540 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName);
541 }
542
543 if ($this->_invalidSoftCreditRowCount) {
544 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName);
545 }
546
547 if ($mode == self::MODE_IMPORT) {
548 $store->set('duplicateRowCount', $this->_duplicateCount);
549 if ($this->_duplicateCount) {
550 $store->set('duplicatesFileName', $this->_duplicateFileName);
551 }
552 }
553 }
554
555 /**
556 * Export data to a CSV file.
557 *
558 * @param string $fileName
559 * @param array $header
560 * @param array $data
561 */
562 public static function exportCSV($fileName, $header, $data) {
563 $output = [];
564 $fd = fopen($fileName, 'w');
565
566 foreach ($header as $key => $value) {
567 $header[$key] = "\"$value\"";
568 }
569 $config = CRM_Core_Config::singleton();
570 $output[] = implode($config->fieldSeparator, $header);
571
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') {
577 continue;
578 }
579 $datum[$k1] = $v1;
580 }
581 }
582 else {
583 $datum[$key] = "\"$value\"";
584 }
585 }
586 $output[] = implode($config->fieldSeparator, $datum);
587 }
588 fwrite($fd, implode("\n", $output));
589 fclose($fd);
590 }
591
592 /**
593 * Determines the file extension based on error code.
594 *
595 * @param int $type
596 * Error code constant.
597 *
598 * @return string
599 */
600 public static function errorFileName($type) {
601 $fileName = NULL;
602 if (empty($type)) {
603 return $fileName;
604 }
605
606 $config = CRM_Core_Config::singleton();
607 $fileName = $config->uploadDir . "sqlImport";
608
609 switch ($type) {
610 case self::SOFT_CREDIT_ERROR:
611 $fileName .= '.softCreditErrors';
612 break;
613
614 case self::PLEDGE_PAYMENT_ERROR:
615 $fileName .= '.pledgePaymentErrors';
616 break;
617
618 default:
619 $fileName = parent::errorFileName($type);
620 break;
621 }
622
623 return $fileName;
624 }
625
626 /**
627 * Determines the file name based on error code.
628 *
629 * @param int $type
630 * Error code constant.
631 *
632 * @return string
633 */
634 public static function saveFileName($type) {
635 $fileName = NULL;
636 if (empty($type)) {
637 return $fileName;
638 }
639
640 switch ($type) {
641 case self::SOFT_CREDIT_ERROR:
642 $fileName = 'Import_Soft_Credit_Errors.csv';
643 break;
644
645 case self::PLEDGE_PAYMENT_ERROR:
646 $fileName = 'Import_Pledge_Payment_Errors.csv';
647 break;
648
649 default:
650 $fileName = parent::saveFileName($type);
651 break;
652 }
653
654 return $fileName;
655 }
656
657 /**
658 * The initializer code, called before the processing
659 */
660 public function init() {
661 $fields = CRM_Contribute_BAO_Contribution::importableFields($this->_contactType, FALSE);
662
663 $fields = array_merge($fields,
664 [
665 'soft_credit' => [
666 'title' => ts('Soft Credit'),
667 'softCredit' => TRUE,
668 'headerPattern' => '/Soft Credit/i',
669 ],
670 ]
671 );
672
673 // add pledge fields only if its is enabled
674 if (CRM_Core_Permission::access('CiviPledge')) {
675 $pledgeFields = [
676 'pledge_payment' => [
677 'title' => ts('Pledge Payment'),
678 'headerPattern' => '/Pledge Payment/i',
679 ],
680 'pledge_id' => [
681 'title' => ts('Pledge ID'),
682 'headerPattern' => '/Pledge ID/i',
683 ],
684 ];
685
686 $fields = array_merge($fields, $pledgeFields);
687 }
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']);
693 }
694
695 $this->_newContributions = [];
696
697 $this->setActiveFields($this->_mapperKeys);
698 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit);
699 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType);
700
701 // FIXME: we should do this in one place together with Form/MapField.php
702 $this->_contactIdIndex = -1;
703
704 $index = 0;
705 foreach ($this->_mapperKeys as $key) {
706 switch ($key) {
707 case 'contribution_contact_id':
708 $this->_contactIdIndex = $index;
709 break;
710
711 }
712 $index++;
713 }
714 }
715
716 /**
717 * Handle the values in summary mode.
718 *
719 * @param array $values
720 * The array of values belonging to this line.
721 *
722 * @return int
723 * CRM_Import_Parser::VALID or CRM_Import_Parser::ERROR
724 */
725 public function summary(&$values) {
726 $this->setActiveFieldValues($values);
727
728 $params = $this->getActiveFieldParams();
729
730 //for date-Formats
731 $errorMessage = implode('; ', $this->formatDateFields($params));
732 //date-Format part ends
733
734 $params['contact_type'] = 'Contribution';
735
736 //checking error in custom data
737 CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
738
739 if ($errorMessage) {
740 $tempMsg = "Invalid value for field(s) : $errorMessage";
741 array_unshift($values, $tempMsg);
742 $errorMessage = NULL;
743 return CRM_Import_Parser::ERROR;
744 }
745
746 return CRM_Import_Parser::VALID;
747 }
748
749 /**
750 * Handle the values in import mode.
751 *
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.
756 *
757 * @return int
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)
766 */
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;
772 }
773
774 $params = &$this->getActiveFieldParams();
775 $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE];
776
777 //CRM-10994
778 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
779 $params['total_amount'] = '0.00';
780 }
781 $this->formatInput($params, $formatted);
782
783 static $indieFields = NULL;
784 if ($indieFields == NULL) {
785 $tempIndieFields = CRM_Contribute_DAO_Contribution::import();
786 $indieFields = $tempIndieFields;
787 }
788
789 $paramValues = [];
790 foreach ($params as $key => $field) {
791 if ($field == NULL || $field === '') {
792 continue;
793 }
794 $paramValues[$key] = $field;
795 }
796
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']))
800 ) {
801 $paramValues['contact_type'] = $this->_contactType;
802 }
803 elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
804 (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
805 ) {
806 $paramValues['contact_type'] = $this->_contactType;
807 }
808 elseif (!empty($params['soft_credit'])) {
809 $paramValues['contact_type'] = $this->_contactType;
810 }
811 elseif (!empty($paramValues['pledge_payment'])) {
812 $paramValues['contact_type'] = $this->_contactType;
813 }
814
815 //need to pass $onDuplicate to check import mode.
816 if (!empty($paramValues['pledge_payment'])) {
817 $paramValues['onDuplicate'] = $onDuplicate;
818 }
819 $formatError = $this->deprecatedFormatParams($paramValues, $formatted, TRUE, $onDuplicate);
820
821 if ($formatError) {
822 array_unshift($values, $formatError['error_message']);
823 if (CRM_Utils_Array::value('error_data', $formatError) == 'soft_credit') {
824 return self::SOFT_CREDIT_ERROR;
825 }
826 if (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') {
827 return self::PLEDGE_PAYMENT_ERROR;
828 }
829 return CRM_Import_Parser::ERROR;
830 }
831
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'])) {
836 $dupeIds = [
837 'id' => $paramValues['contribution_id'] ?? NULL,
838 'trxn_id' => $paramValues['trxn_id'] ?? NULL,
839 'invoice_id' => $paramValues['invoice_id'] ?? NULL,
840 ];
841 $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds);
842
843 if ($ids['contribution']) {
844 $formatted['id'] = $ids['contribution'];
845 //process note
846 if (!empty($paramValues['note'])) {
847 $noteID = [];
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;
854 }
855
856 $noteParams = [
857 'entity_table' => 'civicrm_contribution',
858 'note' => $paramValues['note'],
859 'entity_id' => $ids['contribution'],
860 'contact_id' => $contactID,
861 ];
862 CRM_Core_BAO_Note::add($noteParams, $noteID);
863 unset($formatted['note']);
864 }
865
866 //need to check existing soft credit contribution, CRM-3968
867 if (!empty($formatted['soft_credit'])) {
868 $dupeSoftCredit = [
869 'contact_id' => $formatted['soft_credit'],
870 'contribution_id' => $ids['contribution'],
871 ];
872
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'],
880 'pcp_id' => NULL,
881 ]);
882 }
883 }
884 }
885 }
886
887 $formatted['id'] = $ids['contribution'];
888
889 $newContribution = civicrm_api3('contribution', 'create', $formatted);
890 $this->_newContributions[] = $newContribution['id'];
891
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;
895 }
896
897 // process pledge payment assoc w/ the contribution
898 return $this->processPledgePayments($formatted);
899 }
900 $labels = [
901 'id' => 'Contribution ID',
902 'trxn_id' => 'Transaction ID',
903 'invoice_id' => 'Invoice ID',
904 ];
905 foreach ($dupeIds as $k => $v) {
906 if ($v) {
907 $errorMsg[] = "$labels[$k] $v";
908 }
909 }
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;
913 }
914 }
915
916 if ($this->_contactIdIndex < 0) {
917
918 $error = $this->checkContactDuplicate($paramValues);
919
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;
925 }
926 $cid = $matchedIDs[0];
927 $formatted['contact_id'] = $cid;
928
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;
935 }
936 }
937 else {
938 array_unshift($values, $newContribution['error_message']);
939 return CRM_Import_Parser::ERROR;
940 }
941 }
942
943 $this->_newContributions[] = $newContribution['id'];
944 $formatted['contribution_id'] = $newContribution['id'];
945
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;
949 }
950
951 // process pledge payment assoc w/ the contribution
952 return $this->processPledgePayments($formatted);
953 }
954
955 // Using new Dedupe rule.
956 $ruleParams = [
957 'contact_type' => $this->_contactType,
958 'used' => 'Unsupervised',
959 ];
960 $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams);
961 $disp = NULL;
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)] . " ";
967 }
968 else {
969 $disp .= $params[trim($value)] . " ";
970 }
971 }
972 }
973
974 if (!empty($params['external_identifier'])) {
975 if ($disp) {
976 $disp .= "AND {$params['external_identifier']}";
977 }
978 else {
979 $disp = $params['external_identifier'];
980 }
981 }
982
983 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
984 return CRM_Import_Parser::ERROR;
985 }
986
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;
994 }
995 }
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;
1002 }
1003 }
1004 else {
1005 array_unshift($values, $newContribution['error_message']);
1006 return CRM_Import_Parser::ERROR;
1007 }
1008 }
1009
1010 $this->_newContributions[] = $newContribution['id'];
1011 $formatted['contribution_id'] = $newContribution['id'];
1012
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;
1016 }
1017
1018 // process pledge payment assoc w/ the contribution
1019 return $this->processPledgePayments($formatted);
1020 }
1021
1022 /**
1023 * Process pledge payments.
1024 *
1025 * @param array $formatted
1026 *
1027 * @return int
1028 */
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');
1033
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']
1037 );
1038
1039 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($formatted['pledge_id'],
1040 [$formatted['pledge_payment_id']],
1041 $completeStatusID,
1042 NULL,
1043 $formatted['total_amount']
1044 );
1045
1046 return self::PLEDGE_PAYMENT;
1047 }
1048 }
1049
1050 /**
1051 * Get the array of successfully imported contribution id's
1052 *
1053 * @return array
1054 */
1055 public function &getImportedContributions() {
1056 return $this->_newContributions;
1057 }
1058
1059 /**
1060 * Format date fields from input to mysql.
1061 *
1062 * @param array $params
1063 *
1064 * @return array
1065 * Error messages, if any.
1066 */
1067 public function formatDateFields(&$params) {
1068 $errorMessage = [];
1069 $dateType = CRM_Core_Session::singleton()->get('dateTypes');
1070 foreach ($params as $key => $val) {
1071 if ($val) {
1072 switch ($key) {
1073 case 'receive_date':
1074 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1075 $params[$key] = $dateValue;
1076 }
1077 else {
1078 $errorMessage[] = ts('Receive Date');
1079 }
1080 break;
1081
1082 case 'cancel_date':
1083 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1084 $params[$key] = $dateValue;
1085 }
1086 else {
1087 $errorMessage[] = ts('Cancel Date');
1088 }
1089 break;
1090
1091 case 'receipt_date':
1092 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1093 $params[$key] = $dateValue;
1094 }
1095 else {
1096 $errorMessage[] = ts('Receipt date');
1097 }
1098 break;
1099
1100 case 'thankyou_date':
1101 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1102 $params[$key] = $dateValue;
1103 }
1104 else {
1105 $errorMessage[] = ts('Thankyou Date');
1106 }
1107 break;
1108 }
1109 }
1110 }
1111 return $errorMessage;
1112 }
1113
1114 /**
1115 * Format input params to suit api handling.
1116 *
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.
1121 *
1122 * @param array $params
1123 * @param array $formatted
1124 */
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.
1134 if ($val) {
1135 switch ($key) {
1136 case 'receive_date':
1137 case 'cancel_date':
1138 case 'receipt_date':
1139 case 'thankyou_date':
1140 $params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType);
1141 break;
1142
1143 case 'pledge_payment':
1144 $params[$key] = CRM_Utils_String::strtobool($val);
1145 break;
1146
1147 }
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]);
1152 }
1153 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1154 $params[$key] = CRM_Utils_String::strtoboolstr($val);
1155 }
1156 }
1157 }
1158 }
1159 }
1160
1161 /**
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
1164 *
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
1172 *
1173 * @return array|CRM_Error
1174 */
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');
1180
1181 _civicrm_api3_store_values($fields, $params, $values);
1182
1183 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
1184
1185 foreach ($params as $key => $value) {
1186 // ignore empty values or empty arrays etc
1187 if (CRM_Utils_System::isNull($value)) {
1188 continue;
1189 }
1190
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);
1197 }
1198 elseif ($type == 'Select' || $type == 'Radio' ||
1199 ($type == 'Autocomplete-Select' &&
1200 $customFields[$customFieldID]['data_type'] == 'String'
1201 )
1202 ) {
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;
1211 }
1212 }
1213 }
1214 continue;
1215 }
1216
1217 switch ($key) {
1218 case 'contribution_contact_id':
1219 if (!CRM_Utils_Rule::integer($value)) {
1220 return civicrm_api3_create_error("contact_id not valid: $value");
1221 }
1222 $dao = new CRM_Core_DAO();
1223 $qParams = [];
1224 $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value",
1225 $qParams
1226 );
1227 if (!isset($svq)) {
1228 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
1229 }
1230 elseif ($svq == 1) {
1231 return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact.");
1232 }
1233
1234 $values['contact_id'] = $values['contribution_contact_id'];
1235 unset($values['contribution_contact_id']);
1236 break;
1237
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");
1252 }
1253 }
1254 }
1255 elseif ($email) {
1256 if (!CRM_Utils_Rule::email($email)) {
1257 return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped");
1258 }
1259
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
1262 $emailParams = [
1263 'email' => $email,
1264 'contact_type' => $params['contact_type'],
1265 ];
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");
1269 }
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");
1273 }
1274 if (count($matchingContactIds) == 1) {
1275 $params['contribution_contact_id'] = $matchingContactIds[0];
1276 }
1277 }
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
1280 // invoice id
1281 $contactId = new CRM_Contribute_DAO_Contribution();
1282 if (!empty($params['contribution_id'])) {
1283 $contactId->id = $params['contribution_id'];
1284 }
1285 elseif (!empty($params['trxn_id'])) {
1286 $contactId->trxn_id = $params['trxn_id'];
1287 }
1288 elseif (!empty($params['invoice_id'])) {
1289 $contactId->invoice_id = $params['invoice_id'];
1290 }
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");
1296 }
1297 }
1298 }
1299 }
1300 else {
1301 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
1302 return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
1303 }
1304 }
1305 break;
1306
1307 case 'receive_date':
1308 case 'cancel_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");
1313 }
1314 break;
1315
1316 case 'non_deductible_amount':
1317 case 'total_amount':
1318 case 'fee_amount':
1319 case 'net_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");
1323 }
1324 break;
1325
1326 case 'currency':
1327 if (!CRM_Utils_Rule::currencyCode($value)) {
1328 return civicrm_api3_create_error("currency not a valid code: $value");
1329 }
1330 break;
1331
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;
1339 break;
1340 }
1341 }
1342 if (empty($values['financial_type_id'])) {
1343 return civicrm_api3_create_error("Financial Type is not valid: $value");
1344 }
1345 break;
1346
1347 case 'soft_credit':
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;
1361 $errorMsg = NULL;
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]);
1366 }
1367
1368 if ($errorMsg) {
1369 return civicrm_api3_create_error($errorMsg);
1370 }
1371
1372 // finally get soft credit contact id.
1373 $values[$key][$softKey] = $softParam;
1374 $values[$key][$softKey]['contact_id'] = $contact->id;
1375 }
1376 elseif ($email) {
1377 if (!CRM_Utils_Rule::email($email)) {
1378 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
1379 }
1380
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
1383 $emailParams = [
1384 'email' => $email,
1385 'contact_type' => $params['contact_type'],
1386 ];
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");
1390 }
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");
1394 }
1395 if (count($matchingContactIds) == 1) {
1396 $contactId = $matchingContactIds[0];
1397 unset($softParam['email']);
1398 $values[$key][$softKey] = $softParam + ['contact_id' => $contactId];
1399 }
1400 }
1401 }
1402 }
1403 break;
1404
1405 case 'pledge_payment':
1406 case 'pledge_id':
1407
1408 // giving respect to pledge_payment flag.
1409 if (empty($params['pledge_payment'])) {
1410 break;
1411 }
1412
1413 // get total amount of from import fields
1414 $totalAmount = $params['total_amount'] ?? NULL;
1415
1416 $onDuplicate = $params['onDuplicate'] ?? NULL;
1417
1418 // we need to get contact id $contributionContactID to
1419 // retrieve pledge details as well as to validate pledge ID
1420
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'])
1424 ) {
1425 $contribution = new CRM_Contribute_DAO_Contribution();
1426 if ($params['contribution_id']) {
1427 $contribution->id = $params['contribution_id'];
1428 }
1429 elseif ($params['trxn_id']) {
1430 $contribution->trxn_id = $params['trxn_id'];
1431 }
1432 elseif ($params['invoice_id']) {
1433 $contribution->invoice_id = $params['invoice_id'];
1434 }
1435
1436 if ($contribution->find(TRUE)) {
1437 $contributionContactID = $contribution->contact_id;
1438 if (!$totalAmount) {
1439 $totalAmount = $contribution->total_amount;
1440 }
1441 }
1442 else {
1443 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1444 }
1445 }
1446 else {
1447 // first get the contact id for given contribution record.
1448 if (!empty($params['contribution_contact_id'])) {
1449 $contributionContactID = $params['contribution_contact_id'];
1450 }
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;
1457 }
1458 else {
1459 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1460 }
1461 }
1462 else {
1463 // we need to get contribution contact using de dupe
1464 $error = $this->checkContactDuplicate($params);
1465
1466 if (isset($error['error_message']['params'][0])) {
1467 $matchedIDs = explode(',', $error['error_message']['params'][0]);
1468
1469 // check if only one contact is found
1470 if (count($matchedIDs) > 1) {
1471 return civicrm_api3_create_error($error['error_message']['message']);
1472 }
1473 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
1474 }
1475 else {
1476 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.');
1477 }
1478 }
1479 }
1480
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.');
1484 }
1485 $values['pledge_id'] = $params['pledge_id'];
1486 }
1487 else {
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);
1491
1492 if (empty($pledgeDetails)) {
1493 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.');
1494 }
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.');
1497 }
1498
1499 // this mean we have only one pending / in progress pledge
1500 $values['pledge_id'] = $pledgeDetails[0];
1501 }
1502
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']);
1506
1507 if ($pledgePaymentDetails['amount'] == $totalAmount) {
1508 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
1509 }
1510 else {
1511 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.');
1512 }
1513 break;
1514
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.');
1518 }
1519 $values['contribution_campaign_id'] = $params['contribution_campaign_id'];
1520 break;
1521
1522 default:
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']))
1528 ) {
1529 $realField = $fields[$key]['is_pseudofield_for'] ?? $key;
1530 $realFieldSpec = $fields[$realField];
1531 $values[$key] = $this->parsePseudoConstantField($value, $realFieldSpec);
1532 }
1533 break;
1534 }
1535 }
1536
1537 if (array_key_exists('note', $params)) {
1538 $values['note'] = $params['note'];
1539 }
1540
1541 if ($create) {
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'];
1545
1546 foreach ($changes as $orgVal => $changeVal) {
1547 if (isset($values[$orgVal])) {
1548 $values[$changeVal] = $values[$orgVal];
1549 unset($values[$orgVal]);
1550 }
1551 }
1552 }
1553
1554 return NULL;
1555 }
1556
1557 }