Merge pull request #23468 from mattwire/contributionpagedeprecatecompletetransaction
[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
506 $store->set('headerPatterns', $this->getHeaderPatterns());
507 $store->set('dataPatterns', $this->getDataPatterns());
508 $store->set('columnCount', $this->_activeFieldCount);
509
510 $store->set('totalRowCount', $this->_totalCount);
511 $store->set('validRowCount', $this->_validCount);
512 $store->set('invalidRowCount', $this->_invalidRowCount);
513 $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount);
514 $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount);
515 $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount);
516 $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount);
517
518 switch ($this->_contactType) {
519 case 'Individual':
520 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
521 break;
522
523 case 'Household':
524 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
525 break;
526
527 case 'Organization':
528 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
529 }
530
531 if ($this->_invalidRowCount) {
532 $store->set('errorsFileName', $this->_errorFileName);
533 }
534 if (isset($this->_rows) && !empty($this->_rows)) {
535 $store->set('dataValues', $this->_rows);
536 }
537
538 if ($this->_invalidPledgePaymentRowCount) {
539 $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName);
540 }
541
542 if ($this->_invalidSoftCreditRowCount) {
543 $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName);
544 }
545
546 if ($mode == self::MODE_IMPORT) {
547 $store->set('duplicateRowCount', $this->_duplicateCount);
548 if ($this->_duplicateCount) {
549 $store->set('duplicatesFileName', $this->_duplicateFileName);
550 }
551 }
552 }
553
554 /**
555 * Export data to a CSV file.
556 *
557 * @param string $fileName
558 * @param array $header
559 * @param array $data
560 */
561 public static function exportCSV($fileName, $header, $data) {
562 $output = [];
563 $fd = fopen($fileName, 'w');
564
565 foreach ($header as $key => $value) {
566 $header[$key] = "\"$value\"";
567 }
568 $config = CRM_Core_Config::singleton();
569 $output[] = implode($config->fieldSeparator, $header);
570
571 foreach ($data as $datum) {
572 foreach ($datum as $key => $value) {
573 if (isset($value[0]) && is_array($value)) {
574 foreach ($value[0] as $k1 => $v1) {
575 if ($k1 == 'location_type_id') {
576 continue;
577 }
578 $datum[$k1] = $v1;
579 }
580 }
581 else {
582 $datum[$key] = "\"$value\"";
583 }
584 }
585 $output[] = implode($config->fieldSeparator, $datum);
586 }
587 fwrite($fd, implode("\n", $output));
588 fclose($fd);
589 }
590
591 /**
592 * Determines the file extension based on error code.
593 *
594 * @param int $type
595 * Error code constant.
596 *
597 * @return string
598 */
599 public static function errorFileName($type) {
600 $fileName = NULL;
601 if (empty($type)) {
602 return $fileName;
603 }
604
605 $config = CRM_Core_Config::singleton();
606 $fileName = $config->uploadDir . "sqlImport";
607
608 switch ($type) {
609 case self::SOFT_CREDIT_ERROR:
610 $fileName .= '.softCreditErrors';
611 break;
612
613 case self::PLEDGE_PAYMENT_ERROR:
614 $fileName .= '.pledgePaymentErrors';
615 break;
616
617 default:
618 $fileName = parent::errorFileName($type);
619 break;
620 }
621
622 return $fileName;
623 }
624
625 /**
626 * Determines the file name based on error code.
627 *
628 * @param int $type
629 * Error code constant.
630 *
631 * @return string
632 */
633 public static function saveFileName($type) {
634 $fileName = NULL;
635 if (empty($type)) {
636 return $fileName;
637 }
638
639 switch ($type) {
640 case self::SOFT_CREDIT_ERROR:
641 $fileName = 'Import_Soft_Credit_Errors.csv';
642 break;
643
644 case self::PLEDGE_PAYMENT_ERROR:
645 $fileName = 'Import_Pledge_Payment_Errors.csv';
646 break;
647
648 default:
649 $fileName = parent::saveFileName($type);
650 break;
651 }
652
653 return $fileName;
654 }
655
656 /**
657 * The initializer code, called before the processing
658 */
659 public function init() {
660 $this->setFieldMetadata();
661 foreach ($this->getImportableFieldsMetadata() as $name => $field) {
662 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
663 }
664
665 $this->_newContributions = [];
666
667 $this->setActiveFields($this->_mapperKeys);
668 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit);
669 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType);
670
671 // FIXME: we should do this in one place together with Form/MapField.php
672 $this->_contactIdIndex = -1;
673
674 $index = 0;
675 foreach ($this->_mapperKeys as $key) {
676 switch ($key) {
677 case 'contribution_contact_id':
678 $this->_contactIdIndex = $index;
679 break;
680
681 }
682 $index++;
683 }
684 }
685
686 /**
687 * Set field metadata.
688 */
689 protected function setFieldMetadata() {
690 if (empty($this->importableFieldsMetadata)) {
691 $fields = CRM_Contribute_BAO_Contribution::importableFields($this->_contactType, FALSE);
692
693 $fields = array_merge($fields,
694 [
695 'soft_credit' => [
696 'title' => ts('Soft Credit'),
697 'softCredit' => TRUE,
698 'headerPattern' => '/Soft Credit/i',
699 ],
700 ]
701 );
702
703 // add pledge fields only if its is enabled
704 if (CRM_Core_Permission::access('CiviPledge')) {
705 $pledgeFields = [
706 'pledge_payment' => [
707 'title' => ts('Pledge Payment'),
708 'headerPattern' => '/Pledge Payment/i',
709 ],
710 'pledge_id' => [
711 'title' => ts('Pledge ID'),
712 'headerPattern' => '/Pledge ID/i',
713 ],
714 ];
715
716 $fields = array_merge($fields, $pledgeFields);
717 }
718 foreach ($fields as $name => $field) {
719 $fields[$name] = array_merge([
720 'type' => CRM_Utils_Type::T_INT,
721 'dataPattern' => '//',
722 'headerPattern' => '//',
723 ], $field);
724 }
725 $this->importableFieldsMetadata = $fields;
726 }
727 }
728
729 /**
730 * Handle the values in summary mode.
731 *
732 * @param array $values
733 * The array of values belonging to this line.
734 *
735 * @return int
736 * CRM_Import_Parser::VALID or CRM_Import_Parser::ERROR
737 */
738 public function summary(&$values) {
739 $this->setActiveFieldValues($values);
740
741 $params = $this->getActiveFieldParams();
742
743 //for date-Formats
744 $errorMessage = implode('; ', $this->formatDateFields($params));
745 //date-Format part ends
746
747 $params['contact_type'] = 'Contribution';
748
749 //checking error in custom data
750 CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
751
752 if ($errorMessage) {
753 $tempMsg = "Invalid value for field(s) : $errorMessage";
754 array_unshift($values, $tempMsg);
755 $errorMessage = NULL;
756 return CRM_Import_Parser::ERROR;
757 }
758
759 return CRM_Import_Parser::VALID;
760 }
761
762 /**
763 * Handle the values in import mode.
764 *
765 * @param int $onDuplicate
766 * The code for what action to take on duplicates.
767 * @param array $values
768 * The array of values belonging to this line.
769 *
770 * @return int
771 * the result of this processing - one of
772 * - CRM_Import_Parser::VALID
773 * - CRM_Import_Parser::ERROR
774 * - CRM_Import_Parser::SOFT_CREDIT_ERROR
775 * - CRM_Import_Parser::PLEDGE_PAYMENT_ERROR
776 * - CRM_Import_Parser::DUPLICATE
777 * - CRM_Import_Parser::SOFT_CREDIT (successful creation)
778 * - CRM_Import_Parser::PLEDGE_PAYMENT (successful creation)
779 */
780 public function import($onDuplicate, &$values) {
781 // first make sure this is a valid line
782 $response = $this->summary($values);
783 if ($response != CRM_Import_Parser::VALID) {
784 return CRM_Import_Parser::ERROR;
785 }
786
787 $params = &$this->getActiveFieldParams();
788 $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE];
789
790 //CRM-10994
791 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
792 $params['total_amount'] = '0.00';
793 }
794 $this->formatInput($params, $formatted);
795
796 $paramValues = [];
797 foreach ($params as $key => $field) {
798 if ($field == NULL || $field === '') {
799 continue;
800 }
801 $paramValues[$key] = $field;
802 }
803
804 //import contribution record according to select contact type
805 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP &&
806 (!empty($paramValues['contribution_contact_id']) || !empty($paramValues['external_identifier']))
807 ) {
808 $paramValues['contact_type'] = $this->_contactType;
809 }
810 elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
811 (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
812 ) {
813 $paramValues['contact_type'] = $this->_contactType;
814 }
815 elseif (!empty($params['soft_credit'])) {
816 $paramValues['contact_type'] = $this->_contactType;
817 }
818 elseif (!empty($paramValues['pledge_payment'])) {
819 $paramValues['contact_type'] = $this->_contactType;
820 }
821
822 //need to pass $onDuplicate to check import mode.
823 if (!empty($paramValues['pledge_payment'])) {
824 $paramValues['onDuplicate'] = $onDuplicate;
825 }
826 $formatError = $this->deprecatedFormatParams($paramValues, $formatted, TRUE, $onDuplicate);
827
828 if ($formatError) {
829 array_unshift($values, $formatError['error_message']);
830 if (CRM_Utils_Array::value('error_data', $formatError) == 'soft_credit') {
831 return self::SOFT_CREDIT_ERROR;
832 }
833 if (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') {
834 return self::PLEDGE_PAYMENT_ERROR;
835 }
836 return CRM_Import_Parser::ERROR;
837 }
838
839 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
840 //fix for CRM-2219 - Update Contribution
841 // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
842 if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) {
843 $dupeIds = [
844 'id' => $paramValues['contribution_id'] ?? NULL,
845 'trxn_id' => $paramValues['trxn_id'] ?? NULL,
846 'invoice_id' => $paramValues['invoice_id'] ?? NULL,
847 ];
848 $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds);
849
850 if ($ids['contribution']) {
851 $formatted['id'] = $ids['contribution'];
852 //process note
853 if (!empty($paramValues['note'])) {
854 $noteID = [];
855 $contactID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id');
856 $daoNote = new CRM_Core_BAO_Note();
857 $daoNote->entity_table = 'civicrm_contribution';
858 $daoNote->entity_id = $ids['contribution'];
859 if ($daoNote->find(TRUE)) {
860 $noteID['id'] = $daoNote->id;
861 }
862
863 $noteParams = [
864 'entity_table' => 'civicrm_contribution',
865 'note' => $paramValues['note'],
866 'entity_id' => $ids['contribution'],
867 'contact_id' => $contactID,
868 ];
869 CRM_Core_BAO_Note::add($noteParams, $noteID);
870 unset($formatted['note']);
871 }
872
873 //need to check existing soft credit contribution, CRM-3968
874 if (!empty($formatted['soft_credit'])) {
875 $dupeSoftCredit = [
876 'contact_id' => $formatted['soft_credit'],
877 'contribution_id' => $ids['contribution'],
878 ];
879
880 //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
881 $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']);
882 if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) {
883 foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) {
884 if (!empty($existingSoftCreditValues['soft_credit_id'])) {
885 civicrm_api3('ContributionSoft', 'delete', [
886 'id' => $existingSoftCreditValues['soft_credit_id'],
887 'pcp_id' => NULL,
888 ]);
889 }
890 }
891 }
892 }
893
894 $formatted['id'] = $ids['contribution'];
895
896 $newContribution = civicrm_api3('contribution', 'create', $formatted);
897 $this->_newContributions[] = $newContribution['id'];
898
899 //return soft valid since we need to show how soft credits were added
900 if (!empty($formatted['soft_credit'])) {
901 return self::SOFT_CREDIT;
902 }
903
904 // process pledge payment assoc w/ the contribution
905 return $this->processPledgePayments($formatted);
906 }
907 $labels = [
908 'id' => 'Contribution ID',
909 'trxn_id' => 'Transaction ID',
910 'invoice_id' => 'Invoice ID',
911 ];
912 foreach ($dupeIds as $k => $v) {
913 if ($v) {
914 $errorMsg[] = "$labels[$k] $v";
915 }
916 }
917 $errorMsg = implode(' AND ', $errorMsg);
918 array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.');
919 return CRM_Import_Parser::ERROR;
920 }
921 }
922
923 if ($this->_contactIdIndex < 0) {
924
925 $error = $this->checkContactDuplicate($paramValues);
926
927 if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
928 $matchedIDs = explode(',', $error['error_message']['params'][0]);
929 if (count($matchedIDs) > 1) {
930 array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported');
931 return CRM_Import_Parser::ERROR;
932 }
933 $cid = $matchedIDs[0];
934 $formatted['contact_id'] = $cid;
935
936 $newContribution = civicrm_api('contribution', 'create', $formatted);
937 if (civicrm_error($newContribution)) {
938 if (is_array($newContribution['error_message'])) {
939 array_unshift($values, $newContribution['error_message']['message']);
940 if ($newContribution['error_message']['params'][0]) {
941 return CRM_Import_Parser::DUPLICATE;
942 }
943 }
944 else {
945 array_unshift($values, $newContribution['error_message']);
946 return CRM_Import_Parser::ERROR;
947 }
948 }
949
950 $this->_newContributions[] = $newContribution['id'];
951 $formatted['contribution_id'] = $newContribution['id'];
952
953 //return soft valid since we need to show how soft credits were added
954 if (!empty($formatted['soft_credit'])) {
955 return self::SOFT_CREDIT;
956 }
957
958 // process pledge payment assoc w/ the contribution
959 return $this->processPledgePayments($formatted);
960 }
961
962 // Using new Dedupe rule.
963 $ruleParams = [
964 'contact_type' => $this->_contactType,
965 'used' => 'Unsupervised',
966 ];
967 $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams);
968 $disp = NULL;
969 foreach ($fieldsArray as $value) {
970 if (array_key_exists(trim($value), $params)) {
971 $paramValue = $params[trim($value)];
972 if (is_array($paramValue)) {
973 $disp .= $params[trim($value)][0][trim($value)] . " ";
974 }
975 else {
976 $disp .= $params[trim($value)] . " ";
977 }
978 }
979 }
980
981 if (!empty($params['external_identifier'])) {
982 if ($disp) {
983 $disp .= "AND {$params['external_identifier']}";
984 }
985 else {
986 $disp = $params['external_identifier'];
987 }
988 }
989
990 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
991 return CRM_Import_Parser::ERROR;
992 }
993
994 if (!empty($paramValues['external_identifier'])) {
995 $checkCid = new CRM_Contact_DAO_Contact();
996 $checkCid->external_identifier = $paramValues['external_identifier'];
997 $checkCid->find(TRUE);
998 if ($checkCid->id != $formatted['contact_id']) {
999 array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
1000 return CRM_Import_Parser::ERROR;
1001 }
1002 }
1003 $newContribution = civicrm_api('contribution', 'create', $formatted);
1004 if (civicrm_error($newContribution)) {
1005 if (is_array($newContribution['error_message'])) {
1006 array_unshift($values, $newContribution['error_message']['message']);
1007 if ($newContribution['error_message']['params'][0]) {
1008 return CRM_Import_Parser::DUPLICATE;
1009 }
1010 }
1011 else {
1012 array_unshift($values, $newContribution['error_message']);
1013 return CRM_Import_Parser::ERROR;
1014 }
1015 }
1016
1017 $this->_newContributions[] = $newContribution['id'];
1018 $formatted['contribution_id'] = $newContribution['id'];
1019
1020 //return soft valid since we need to show how soft credits were added
1021 if (!empty($formatted['soft_credit'])) {
1022 return self::SOFT_CREDIT;
1023 }
1024
1025 // process pledge payment assoc w/ the contribution
1026 return $this->processPledgePayments($formatted);
1027 }
1028
1029 /**
1030 * Process pledge payments.
1031 *
1032 * @param array $formatted
1033 *
1034 * @return int
1035 */
1036 private function processPledgePayments(array $formatted) {
1037 if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) {
1038 //get completed status
1039 $completeStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
1040
1041 //need to update payment record to map contribution_id
1042 CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'],
1043 'contribution_id', $formatted['contribution_id']
1044 );
1045
1046 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($formatted['pledge_id'],
1047 [$formatted['pledge_payment_id']],
1048 $completeStatusID,
1049 NULL,
1050 $formatted['total_amount']
1051 );
1052
1053 return self::PLEDGE_PAYMENT;
1054 }
1055 }
1056
1057 /**
1058 * Get the array of successfully imported contribution id's
1059 *
1060 * @return array
1061 */
1062 public function &getImportedContributions() {
1063 return $this->_newContributions;
1064 }
1065
1066 /**
1067 * Format date fields from input to mysql.
1068 *
1069 * @param array $params
1070 *
1071 * @return array
1072 * Error messages, if any.
1073 */
1074 public function formatDateFields(&$params) {
1075 $errorMessage = [];
1076 $dateType = CRM_Core_Session::singleton()->get('dateTypes');
1077 foreach ($params as $key => $val) {
1078 if ($val) {
1079 switch ($key) {
1080 case 'receive_date':
1081 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1082 $params[$key] = $dateValue;
1083 }
1084 else {
1085 $errorMessage[] = ts('Receive Date');
1086 }
1087 break;
1088
1089 case 'cancel_date':
1090 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1091 $params[$key] = $dateValue;
1092 }
1093 else {
1094 $errorMessage[] = ts('Cancel Date');
1095 }
1096 break;
1097
1098 case 'receipt_date':
1099 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1100 $params[$key] = $dateValue;
1101 }
1102 else {
1103 $errorMessage[] = ts('Receipt date');
1104 }
1105 break;
1106
1107 case 'thankyou_date':
1108 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
1109 $params[$key] = $dateValue;
1110 }
1111 else {
1112 $errorMessage[] = ts('Thankyou Date');
1113 }
1114 break;
1115 }
1116 }
1117 }
1118 return $errorMessage;
1119 }
1120
1121 /**
1122 * Format input params to suit api handling.
1123 *
1124 * Over time all the parts of deprecatedFormatParams
1125 * and all the parts of the import function on this class that relate to
1126 * reformatting input should be moved here and tests should be added in
1127 * CRM_Contribute_Import_Parser_ContributionTest.
1128 *
1129 * @param array $params
1130 * @param array $formatted
1131 */
1132 public function formatInput(&$params, &$formatted = []) {
1133 $dateType = CRM_Core_Session::singleton()->get('dateTypes');
1134 $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Contribution';
1135 $customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
1136 // @todo call formatDateFields & move custom data handling there.
1137 // Also note error handling for dates is currently in deprecatedFormatParams
1138 // we should use the error handling in formatDateFields.
1139 foreach ($params as $key => $val) {
1140 // @todo - call formatDateFields instead.
1141 if ($val) {
1142 switch ($key) {
1143 case 'receive_date':
1144 case 'cancel_date':
1145 case 'receipt_date':
1146 case 'thankyou_date':
1147 $params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType);
1148 break;
1149
1150 case 'pledge_payment':
1151 $params[$key] = CRM_Utils_String::strtobool($val);
1152 break;
1153
1154 }
1155 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
1156 if ($customFields[$customFieldID]['data_type'] == 'Date') {
1157 CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
1158 unset($params[$key]);
1159 }
1160 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1161 $params[$key] = CRM_Utils_String::strtoboolstr($val);
1162 }
1163 }
1164 }
1165 }
1166 }
1167
1168 /**
1169 * take the input parameter list as specified in the data model and
1170 * convert it into the same format that we use in QF and BAO object
1171 *
1172 * @param array $params
1173 * Associative array of property name/value
1174 * pairs to insert in new contact.
1175 * @param array $values
1176 * The reformatted properties that we can use internally.
1177 * @param bool $create
1178 * @param int $onDuplicate
1179 *
1180 * @return array|CRM_Error
1181 */
1182 private function deprecatedFormatParams($params, &$values, $create = FALSE, $onDuplicate = NULL) {
1183 require_once 'CRM/Utils/DeprecatedUtils.php';
1184 // copy all the contribution fields as is
1185 require_once 'api/v3/utils.php';
1186 $fields = CRM_Core_DAO::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution');
1187
1188 _civicrm_api3_store_values($fields, $params, $values);
1189
1190 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
1191
1192 foreach ($params as $key => $value) {
1193 // ignore empty values or empty arrays etc
1194 if (CRM_Utils_System::isNull($value)) {
1195 continue;
1196 }
1197
1198 // Handling Custom Data
1199 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
1200 $values[$key] = $value;
1201 $type = $customFields[$customFieldID]['html_type'];
1202 if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) {
1203 $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type);
1204 }
1205 elseif ($type == 'Select' || $type == 'Radio' ||
1206 ($type == 'Autocomplete-Select' &&
1207 $customFields[$customFieldID]['data_type'] == 'String'
1208 )
1209 ) {
1210 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
1211 foreach ($customOption as $customFldID => $customValue) {
1212 $val = $customValue['value'] ?? NULL;
1213 $label = $customValue['label'] ?? NULL;
1214 $label = strtolower($label);
1215 $value = strtolower(trim($value));
1216 if (($value == $label) || ($value == strtolower($val))) {
1217 $values[$key] = $val;
1218 }
1219 }
1220 }
1221 continue;
1222 }
1223
1224 switch ($key) {
1225 case 'contribution_contact_id':
1226 if (!CRM_Utils_Rule::integer($value)) {
1227 return civicrm_api3_create_error("contact_id not valid: $value");
1228 }
1229 $dao = new CRM_Core_DAO();
1230 $qParams = [];
1231 $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value",
1232 $qParams
1233 );
1234 if (!isset($svq)) {
1235 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
1236 }
1237 elseif ($svq == 1) {
1238 return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact.");
1239 }
1240
1241 $values['contact_id'] = $values['contribution_contact_id'];
1242 unset($values['contribution_contact_id']);
1243 break;
1244
1245 case 'contact_type':
1246 // import contribution record according to select contact type
1247 require_once 'CRM/Contact/DAO/Contact.php';
1248 $contactType = new CRM_Contact_DAO_Contact();
1249 $contactId = $params['contribution_contact_id'] ?? NULL;
1250 $externalId = $params['external_identifier'] ?? NULL;
1251 $email = $params['email'] ?? NULL;
1252 //when insert mode check contact id or external identifier
1253 if ($contactId || $externalId) {
1254 $contactType->id = $contactId;
1255 $contactType->external_identifier = $externalId;
1256 if ($contactType->find(TRUE)) {
1257 if ($params['contact_type'] != $contactType->contact_type) {
1258 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
1259 }
1260 }
1261 }
1262 elseif ($email) {
1263 if (!CRM_Utils_Rule::email($email)) {
1264 return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped");
1265 }
1266
1267 // get the contact id from duplicate contact rule, if more than one contact is returned
1268 // we should return error, since current interface allows only one-one mapping
1269 $emailParams = [
1270 'email' => $email,
1271 'contact_type' => $params['contact_type'],
1272 ];
1273 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
1274 if (!$checkDedupe['is_error']) {
1275 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email. Row was skipped");
1276 }
1277 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
1278 if (count($matchingContactIds) > 1) {
1279 return civicrm_api3_create_error("Invalid email address(duplicate) $email. Row was skipped");
1280 }
1281 if (count($matchingContactIds) == 1) {
1282 $params['contribution_contact_id'] = $matchingContactIds[0];
1283 }
1284 }
1285 elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) {
1286 // when update mode check contribution id or trxn id or
1287 // invoice id
1288 $contactId = new CRM_Contribute_DAO_Contribution();
1289 if (!empty($params['contribution_id'])) {
1290 $contactId->id = $params['contribution_id'];
1291 }
1292 elseif (!empty($params['trxn_id'])) {
1293 $contactId->trxn_id = $params['trxn_id'];
1294 }
1295 elseif (!empty($params['invoice_id'])) {
1296 $contactId->invoice_id = $params['invoice_id'];
1297 }
1298 if ($contactId->find(TRUE)) {
1299 $contactType->id = $contactId->contact_id;
1300 if ($contactType->find(TRUE)) {
1301 if ($params['contact_type'] != $contactType->contact_type) {
1302 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
1303 }
1304 }
1305 }
1306 }
1307 else {
1308 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
1309 return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
1310 }
1311 }
1312 break;
1313
1314 case 'receive_date':
1315 case 'cancel_date':
1316 case 'receipt_date':
1317 case 'thankyou_date':
1318 if (!CRM_Utils_Rule::dateTime($value)) {
1319 return civicrm_api3_create_error("$key not a valid date: $value");
1320 }
1321 break;
1322
1323 case 'non_deductible_amount':
1324 case 'total_amount':
1325 case 'fee_amount':
1326 case 'net_amount':
1327 // @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled.
1328 if (!CRM_Utils_Rule::money($value)) {
1329 return civicrm_api3_create_error("$key not a valid amount: $value");
1330 }
1331 break;
1332
1333 case 'currency':
1334 if (!CRM_Utils_Rule::currencyCode($value)) {
1335 return civicrm_api3_create_error("currency not a valid code: $value");
1336 }
1337 break;
1338
1339 case 'financial_type':
1340 // @todo add test like testPaymentTypeLabel & remove these lines in favour of 'default' part of switch.
1341 require_once 'CRM/Contribute/PseudoConstant.php';
1342 $contriTypes = CRM_Contribute_PseudoConstant::financialType();
1343 foreach ($contriTypes as $val => $type) {
1344 if (strtolower($value) == strtolower($type)) {
1345 $values['financial_type_id'] = $val;
1346 break;
1347 }
1348 }
1349 if (empty($values['financial_type_id'])) {
1350 return civicrm_api3_create_error("Financial Type is not valid: $value");
1351 }
1352 break;
1353
1354 case 'soft_credit':
1355 // import contribution record according to select contact type
1356 // validate contact id and external identifier.
1357 $value[$key] = $mismatchContactType = $softCreditContactIds = '';
1358 if (isset($params[$key]) && is_array($params[$key])) {
1359 foreach ($params[$key] as $softKey => $softParam) {
1360 $contactId = $softParam['contact_id'] ?? NULL;
1361 $externalId = $softParam['external_identifier'] ?? NULL;
1362 $email = $softParam['email'] ?? NULL;
1363 if ($contactId || $externalId) {
1364 require_once 'CRM/Contact/DAO/Contact.php';
1365 $contact = new CRM_Contact_DAO_Contact();
1366 $contact->id = $contactId;
1367 $contact->external_identifier = $externalId;
1368 $errorMsg = NULL;
1369 if (!$contact->find(TRUE)) {
1370 $field = $contactId ? ts('Contact ID') : ts('External ID');
1371 $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.",
1372 [1 => $field, 2 => $contactId ? $contactId : $externalId]);
1373 }
1374
1375 if ($errorMsg) {
1376 return civicrm_api3_create_error($errorMsg);
1377 }
1378
1379 // finally get soft credit contact id.
1380 $values[$key][$softKey] = $softParam;
1381 $values[$key][$softKey]['contact_id'] = $contact->id;
1382 }
1383 elseif ($email) {
1384 if (!CRM_Utils_Rule::email($email)) {
1385 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
1386 }
1387
1388 // get the contact id from duplicate contact rule, if more than one contact is returned
1389 // we should return error, since current interface allows only one-one mapping
1390 $emailParams = [
1391 'email' => $email,
1392 'contact_type' => $params['contact_type'],
1393 ];
1394 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
1395 if (!$checkDedupe['is_error']) {
1396 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped");
1397 }
1398 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
1399 if (count($matchingContactIds) > 1) {
1400 return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped");
1401 }
1402 if (count($matchingContactIds) == 1) {
1403 $contactId = $matchingContactIds[0];
1404 unset($softParam['email']);
1405 $values[$key][$softKey] = $softParam + ['contact_id' => $contactId];
1406 }
1407 }
1408 }
1409 }
1410 break;
1411
1412 case 'pledge_payment':
1413 case 'pledge_id':
1414
1415 // giving respect to pledge_payment flag.
1416 if (empty($params['pledge_payment'])) {
1417 break;
1418 }
1419
1420 // get total amount of from import fields
1421 $totalAmount = $params['total_amount'] ?? NULL;
1422
1423 $onDuplicate = $params['onDuplicate'] ?? NULL;
1424
1425 // we need to get contact id $contributionContactID to
1426 // retrieve pledge details as well as to validate pledge ID
1427
1428 // first need to check for update mode
1429 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
1430 ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id'])
1431 ) {
1432 $contribution = new CRM_Contribute_DAO_Contribution();
1433 if ($params['contribution_id']) {
1434 $contribution->id = $params['contribution_id'];
1435 }
1436 elseif ($params['trxn_id']) {
1437 $contribution->trxn_id = $params['trxn_id'];
1438 }
1439 elseif ($params['invoice_id']) {
1440 $contribution->invoice_id = $params['invoice_id'];
1441 }
1442
1443 if ($contribution->find(TRUE)) {
1444 $contributionContactID = $contribution->contact_id;
1445 if (!$totalAmount) {
1446 $totalAmount = $contribution->total_amount;
1447 }
1448 }
1449 else {
1450 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1451 }
1452 }
1453 else {
1454 // first get the contact id for given contribution record.
1455 if (!empty($params['contribution_contact_id'])) {
1456 $contributionContactID = $params['contribution_contact_id'];
1457 }
1458 elseif (!empty($params['external_identifier'])) {
1459 require_once 'CRM/Contact/DAO/Contact.php';
1460 $contact = new CRM_Contact_DAO_Contact();
1461 $contact->external_identifier = $params['external_identifier'];
1462 if ($contact->find(TRUE)) {
1463 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id;
1464 }
1465 else {
1466 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
1467 }
1468 }
1469 else {
1470 // we need to get contribution contact using de dupe
1471 $error = $this->checkContactDuplicate($params);
1472
1473 if (isset($error['error_message']['params'][0])) {
1474 $matchedIDs = explode(',', $error['error_message']['params'][0]);
1475
1476 // check if only one contact is found
1477 if (count($matchedIDs) > 1) {
1478 return civicrm_api3_create_error($error['error_message']['message']);
1479 }
1480 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
1481 }
1482 else {
1483 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.');
1484 }
1485 }
1486 }
1487
1488 if (!empty($params['pledge_id'])) {
1489 if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
1490 return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.');
1491 }
1492 $values['pledge_id'] = $params['pledge_id'];
1493 }
1494 else {
1495 // check if there are any pledge related to this contact, with payments pending or in progress
1496 require_once 'CRM/Pledge/BAO/Pledge.php';
1497 $pledgeDetails = CRM_Pledge_BAO_Pledge::getContactPledges($contributionContactID);
1498
1499 if (empty($pledgeDetails)) {
1500 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.');
1501 }
1502 if (count($pledgeDetails) > 1) {
1503 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.');
1504 }
1505
1506 // this mean we have only one pending / in progress pledge
1507 $values['pledge_id'] = $pledgeDetails[0];
1508 }
1509
1510 // we need to check if oldest payment amount equal to contribution amount
1511 require_once 'CRM/Pledge/BAO/PledgePayment.php';
1512 $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']);
1513
1514 if ($pledgePaymentDetails['amount'] == $totalAmount) {
1515 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
1516 }
1517 else {
1518 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.');
1519 }
1520 break;
1521
1522 case 'contribution_campaign_id':
1523 if (empty(CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $params['contribution_campaign_id']))) {
1524 return civicrm_api3_create_error('Invalid Campaign ID provided. Contribution row was skipped.');
1525 }
1526 $values['contribution_campaign_id'] = $params['contribution_campaign_id'];
1527 break;
1528
1529 default:
1530 // Hande name or label for fields with options.
1531 if (isset($fields[$key]) &&
1532 // Yay - just for a surprise we are inconsistent on whether we pass the pseudofield (payment_instrument)
1533 // or the field name (contribution_status_id)
1534 (!empty($fields[$key]['is_pseudofield_for']) || !empty($fields[$key]['pseudoconstant']))
1535 ) {
1536 $realField = $fields[$key]['is_pseudofield_for'] ?? $key;
1537 $realFieldSpec = $fields[$realField];
1538 $values[$key] = $this->parsePseudoConstantField($value, $realFieldSpec);
1539 }
1540 break;
1541 }
1542 }
1543
1544 if (array_key_exists('note', $params)) {
1545 $values['note'] = $params['note'];
1546 }
1547
1548 if ($create) {
1549 // CRM_Contribute_BAO_Contribution::add() handles contribution_source
1550 // So, if $values contains contribution_source, convert it to source
1551 $changes = ['contribution_source' => 'source'];
1552
1553 foreach ($changes as $orgVal => $changeVal) {
1554 if (isset($values[$orgVal])) {
1555 $values[$changeVal] = $values[$orgVal];
1556 unset($values[$orgVal]);
1557 }
1558 }
1559 }
1560
1561 return NULL;
1562 }
1563
1564 /**
1565 * Get the civicrm_mapping_field appropriate layout for the mapper input.
1566 *
1567 * The input looks something like ['street_address', 1]
1568 * and would be mapped to ['name' => 'street_address', 'location_type_id' =>
1569 * 1]
1570 *
1571 * @param array $fieldMapping
1572 * @param int $mappingID
1573 * @param int $columnNumber
1574 *
1575 * @return array
1576 * @throws \API_Exception
1577 */
1578 public function getMappingFieldFromMapperInput(array $fieldMapping, int $mappingID, int $columnNumber): array {
1579 $isRelationshipField = preg_match('/\d*_a_b|b_a$/', $fieldMapping[0]);
1580 $fieldName = $isRelationshipField ? $fieldMapping[1] : $fieldMapping[0];
1581 return [
1582 'name' => $fieldMapping[0],
1583 'mapping_id' => $mappingID,
1584 'column_number' => $columnNumber,
1585 // The name of the field to match the soft credit on is (crazily)
1586 // stored in 'contact_type'
1587 'contact_type' => $fieldMapping[1] ?? NULL,
1588 // We also store the field in a sensible key, even if it isn't saved sensibly.
1589 'soft_credit_match_field' => $fieldMapping[1] ?? NULL,
1590 // This field is actually not saved at all :-( It is lost each time.
1591 'soft_credit_type_id' => $fieldMapping[2] ?? NULL,
1592 ];
1593 }
1594
1595 }