3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * Class to parse contribution csv files.
37 class CRM_Contribute_Import_Parser_Contribution
extends CRM_Contribute_Import_Parser
{
39 protected $_mapperKeys;
41 private $_contactIdIndex;
42 private $_totalAmountIndex;
43 private $_contributionTypeIndex;
45 protected $_mapperSoftCredit;
46 //protected $_mapperPhoneType;
49 * Array of successfully imported contribution id's
53 protected $_newContributions;
59 * @param null $mapperSoftCredit
60 * @param null $mapperPhoneType
61 * @param null $mapperSoftCreditType
63 public function __construct(&$mapperKeys, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL) {
64 parent
::__construct();
65 $this->_mapperKeys
= &$mapperKeys;
66 $this->_mapperSoftCredit
= &$mapperSoftCredit;
67 $this->_mapperSoftCreditType
= &$mapperSoftCreditType;
71 * The initializer code, called before the processing
73 public function init() {
74 $fields = CRM_Contribute_BAO_Contribution
::importableFields($this->_contactType
, FALSE);
76 $fields = array_merge($fields,
78 'soft_credit' => array(
79 'title' => ts('Soft Credit'),
81 'headerPattern' => '/Soft Credit/i',
86 // add pledge fields only if its is enabled
87 if (CRM_Core_Permission
::access('CiviPledge')) {
88 $pledgeFields = array(
89 'pledge_payment' => array(
90 'title' => ts('Pledge Payment'),
91 'headerPattern' => '/Pledge Payment/i',
94 'title' => ts('Pledge ID'),
95 'headerPattern' => '/Pledge ID/i',
99 $fields = array_merge($fields, $pledgeFields);
101 foreach ($fields as $name => $field) {
102 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
103 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
104 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
105 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
108 $this->_newContributions
= array();
110 $this->setActiveFields($this->_mapperKeys
);
111 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit
);
112 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType
);
114 // FIXME: we should do this in one place together with Form/MapField.php
115 $this->_contactIdIndex
= -1;
116 $this->_totalAmountIndex
= -1;
117 $this->_contributionTypeIndex
= -1;
120 foreach ($this->_mapperKeys
as $key) {
122 case 'contribution_contact_id':
123 $this->_contactIdIndex
= $index;
127 $this->_totalAmountIndex
= $index;
130 case 'financial_type':
131 $this->_contributionTypeIndex
= $index;
139 * Handle the values in mapField mode.
141 * @param array $values
142 * The array of values belonging to this line.
146 public function mapField(&$values) {
147 return CRM_Import_Parser
::VALID
;
151 * Handle the values in preview mode.
153 * @param array $values
154 * The array of values belonging to this line.
157 * the result of this processing
159 public function preview(&$values) {
160 return $this->summary($values);
164 * Handle the values in summary mode.
166 * @param array $values
167 * The array of values belonging to this line.
170 * the result of this processing
172 public function summary(&$values) {
173 $erroneousField = NULL;
174 $response = $this->setActiveFieldValues($values, $erroneousField);
176 $params = &$this->getActiveFieldParams();
177 $errorMessage = NULL;
180 $session = CRM_Core_Session
::singleton();
181 $dateType = $session->get('dateTypes');
182 foreach ($params as $key => $val) {
186 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
187 $params[$key] = $dateValue;
190 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Receive Date', $errorMessage);
195 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
196 $params[$key] = $dateValue;
199 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Cancel Date', $errorMessage);
204 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
205 $params[$key] = $dateValue;
208 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Receipt date', $errorMessage);
212 case 'thankyou_date':
213 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
214 $params[$key] = $dateValue;
217 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Thankyou Date', $errorMessage);
223 //date-Format part ends
225 $params['contact_type'] = 'Contribution';
227 //checking error in custom data
228 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
231 $tempMsg = "Invalid value for field(s) : $errorMessage";
232 array_unshift($values, $tempMsg);
233 $errorMessage = NULL;
234 return CRM_Import_Parser
::ERROR
;
237 return CRM_Import_Parser
::VALID
;
241 * Handle the values in import mode.
243 * @param int $onDuplicate
244 * The code for what action to take on duplicates.
245 * @param array $values
246 * The array of values belonging to this line.
249 * the result of this processing
251 public function import($onDuplicate, &$values) {
252 // first make sure this is a valid line
253 $response = $this->summary($values);
254 if ($response != CRM_Import_Parser
::VALID
) {
258 $params = &$this->getActiveFieldParams();
259 $formatted = array('version' => 3);
261 // don't add to recent items, CRM-4399
262 $formatted['skipRecentView'] = TRUE;
265 $session = CRM_Core_Session
::singleton();
266 $dateType = $session->get('dateTypes');
268 $customDataType = !empty($params['contact_type']) ?
$params['contact_type'] : 'Contribution';
269 $customFields = CRM_Core_BAO_CustomField
::getFields($customDataType);
272 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
273 $params['total_amount'] = '0.00';
275 foreach ($params as $key => $val) {
281 case 'thankyou_date':
282 $params[$key] = CRM_Utils_Date
::formatDate($params[$key], $dateType);
285 case 'pledge_payment':
286 $params[$key] = CRM_Utils_String
::strtobool($val);
290 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
291 if ($customFields[$customFieldID]['data_type'] == 'Date') {
292 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
293 unset($params[$key]);
295 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
296 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
301 //date-Format part ends
303 static $indieFields = NULL;
304 if ($indieFields == NULL) {
305 $tempIndieFields = CRM_Contribute_DAO_Contribution
::import();
306 $indieFields = $tempIndieFields;
309 $paramValues = array();
310 foreach ($params as $key => $field) {
311 if ($field == NULL ||
$field === '') {
314 $paramValues[$key] = $field;
317 //import contribution record according to select contact type
318 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
&&
319 (!empty($paramValues['contribution_contact_id']) ||
!empty($paramValues['external_identifier']))
321 $paramValues['contact_type'] = $this->_contactType
;
323 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
324 (!empty($paramValues['contribution_id']) ||
!empty($values['trxn_id']) ||
!empty($paramValues['invoice_id']))
326 $paramValues['contact_type'] = $this->_contactType
;
328 elseif (!empty($params['soft_credit'])) {
329 $paramValues['contact_type'] = $this->_contactType
;
331 elseif (!empty($paramValues['pledge_payment'])) {
332 $paramValues['contact_type'] = $this->_contactType
;
335 //need to pass $onDuplicate to check import mode.
336 if (!empty($paramValues['pledge_payment'])) {
337 $paramValues['onDuplicate'] = $onDuplicate;
339 require_once 'CRM/Utils/DeprecatedUtils.php';
340 $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE, $onDuplicate);
343 array_unshift($values, $formatError['error_message']);
344 if (CRM_Utils_Array
::value('error_data', $formatError) == 'soft_credit') {
345 return CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
;
347 elseif (CRM_Utils_Array
::value('error_data', $formatError) == 'pledge_payment') {
348 return CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
;
350 return CRM_Import_Parser
::ERROR
;
353 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_UPDATE
) {
354 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($formatted,
360 //fix for CRM-2219 - Update Contribution
361 // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
362 if (!empty($paramValues['invoice_id']) ||
!empty($paramValues['trxn_id']) ||
!empty($paramValues['contribution_id'])) {
364 'id' => CRM_Utils_Array
::value('contribution_id', $paramValues),
365 'trxn_id' => CRM_Utils_Array
::value('trxn_id', $paramValues),
366 'invoice_id' => CRM_Utils_Array
::value('invoice_id', $paramValues),
369 $ids['contribution'] = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($dupeIds);
371 if ($ids['contribution']) {
372 $formatted['id'] = $ids['contribution'];
373 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($formatted,
378 if (!empty($paramValues['note'])) {
380 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id');
381 $daoNote = new CRM_Core_BAO_Note();
382 $daoNote->entity_table
= 'civicrm_contribution';
383 $daoNote->entity_id
= $ids['contribution'];
384 if ($daoNote->find(TRUE)) {
385 $noteID['id'] = $daoNote->id
;
389 'entity_table' => 'civicrm_contribution',
390 'note' => $paramValues['note'],
391 'entity_id' => $ids['contribution'],
392 'contact_id' => $contactID,
394 CRM_Core_BAO_Note
::add($noteParams, $noteID);
395 unset($formatted['note']);
398 //need to check existing soft credit contribution, CRM-3968
399 if (!empty($formatted['soft_credit'])) {
400 $dupeSoftCredit = array(
401 'contact_id' => $formatted['soft_credit'],
402 'contribution_id' => $ids['contribution'],
405 //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
406 $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($dupeSoftCredit['contribution_id']);
407 if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) {
408 foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) {
409 if (!empty($existingSoftCreditValues['soft_credit_id'])) {
410 $deleteParams = array(
411 'id' => $existingSoftCreditValues['soft_credit_id'],
414 CRM_Contribute_BAO_ContributionSoft
::del($deleteParams);
420 $newContribution = CRM_Contribute_BAO_Contribution
::create($formatted, $ids);
421 $this->_newContributions
[] = $newContribution->id
;
423 //return soft valid since we need to show how soft credits were added
424 if (!empty($formatted['soft_credit'])) {
425 return CRM_Contribute_Import_Parser
::SOFT_CREDIT
;
428 // process pledge payment assoc w/ the contribution
429 return self
::processPledgePayments($formatted);
431 return CRM_Import_Parser
::VALID
;
435 'id' => 'Contribution ID',
436 'trxn_id' => 'Transaction ID',
437 'invoice_id' => 'Invoice ID',
439 foreach ($dupeIds as $k => $v) {
441 $errorMsg[] = "$labels[$k] $v";
444 $errorMsg = implode(' AND ', $errorMsg);
445 array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.');
446 return CRM_Import_Parser
::ERROR
;
451 if ($this->_contactIdIndex
< 0) {
452 // set the contact type if its not set
453 if (!isset($paramValues['contact_type'])) {
454 $paramValues['contact_type'] = $this->_contactType
;
457 $paramValues['version'] = 3;
458 //retrieve contact id using contact dedupe rule
459 require_once 'CRM/Utils/DeprecatedUtils.php';
460 $error = _civicrm_api3_deprecated_check_contact_dedupe($paramValues);
462 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
463 $matchedIDs = explode(',', $error['error_message']['params'][0]);
464 if (count($matchedIDs) > 1) {
465 array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported');
466 return CRM_Import_Parser
::ERROR
;
469 $cid = $matchedIDs[0];
470 $formatted['contact_id'] = $cid;
472 $newContribution = civicrm_api('contribution', 'create', $formatted);
473 if (civicrm_error($newContribution)) {
474 if (is_array($newContribution['error_message'])) {
475 array_unshift($values, $newContribution['error_message']['message']);
476 if ($newContribution['error_message']['params'][0]) {
477 return CRM_Import_Parser
::DUPLICATE
;
481 array_unshift($values, $newContribution['error_message']);
482 return CRM_Import_Parser
::ERROR
;
486 $this->_newContributions
[] = $newContribution['id'];
487 $formatted['contribution_id'] = $newContribution['id'];
489 //return soft valid since we need to show how soft credits were added
490 if (!empty($formatted['soft_credit'])) {
491 return CRM_Contribute_Import_Parser
::SOFT_CREDIT
;
494 // process pledge payment assoc w/ the contribution
495 return self
::processPledgePayments($formatted);
497 return CRM_Import_Parser
::VALID
;
501 // Using new Dedupe rule.
503 'contact_type' => $this->_contactType
,
504 'used' => 'Unsupervised',
506 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
508 foreach ($fieldsArray as $value) {
509 if (array_key_exists(trim($value), $params)) {
510 $paramValue = $params[trim($value)];
511 if (is_array($paramValue)) {
512 $disp .= $params[trim($value)][0][trim($value)] . " ";
515 $disp .= $params[trim($value)] . " ";
520 if (!empty($params['external_identifier'])) {
522 $disp .= "AND {$params['external_identifier']}";
525 $disp = $params['external_identifier'];
529 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
530 return CRM_Import_Parser
::ERROR
;
534 if (!empty($paramValues['external_identifier'])) {
535 $checkCid = new CRM_Contact_DAO_Contact();
536 $checkCid->external_identifier
= $paramValues['external_identifier'];
537 $checkCid->find(TRUE);
538 if ($checkCid->id
!= $formatted['contact_id']) {
539 array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
540 return CRM_Import_Parser
::ERROR
;
543 $newContribution = civicrm_api('contribution', 'create', $formatted);
544 if (civicrm_error($newContribution)) {
545 if (is_array($newContribution['error_message'])) {
546 array_unshift($values, $newContribution['error_message']['message']);
547 if ($newContribution['error_message']['params'][0]) {
548 return CRM_Import_Parser
::DUPLICATE
;
552 array_unshift($values, $newContribution['error_message']);
553 return CRM_Import_Parser
::ERROR
;
557 $this->_newContributions
[] = $newContribution['id'];
558 $formatted['contribution_id'] = $newContribution['id'];
560 //return soft valid since we need to show how soft credits were added
561 if (!empty($formatted['soft_credit'])) {
562 return CRM_Contribute_Import_Parser
::SOFT_CREDIT
;
565 // process pledge payment assoc w/ the contribution
566 return self
::processPledgePayments($formatted);
568 return CRM_Import_Parser
::VALID
;
573 * Process pledge payments.
575 * @param array $formatted
579 public function processPledgePayments(&$formatted) {
580 if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) {
581 //get completed status
582 $completeStatusID = CRM_Core_OptionGroup
::getValue('contribution_status', 'Completed', 'name');
584 //need to update payment record to map contribution_id
585 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'],
586 'contribution_id', $formatted['contribution_id']
589 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($formatted['pledge_id'],
590 array($formatted['pledge_payment_id']),
593 $formatted['total_amount']
596 return CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT
;
601 * Get the array of successfully imported contribution id's
605 public function &getImportedContributions() {
606 return $this->_newContributions
;
610 * The initializer code, called before the processing.
612 public function fini() {