3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * class to parse contribution csv files
39 class CRM_Contribute_Import_Parser_Contribution
extends CRM_Contribute_Import_Parser
{
41 protected $_mapperKeys;
43 private $_contactIdIndex;
44 private $_totalAmountIndex;
45 private $_contributionTypeIndex;
47 protected $_mapperSoftCredit;
48 //protected $_mapperPhoneType;
51 * Array of successfully imported contribution id's
55 protected $_newContributions;
60 function __construct(&$mapperKeys, $mapperSoftCredit = NULL, $mapperPhoneType = NULL) {
61 parent
::__construct();
62 $this->_mapperKeys
= &$mapperKeys;
63 $this->_mapperSoftCredit
= &$mapperSoftCredit;
67 * the initializer code, called before the processing
73 $fields = CRM_Contribute_BAO_Contribution
::importableFields($this->_contactType
, FALSE);
75 $fields = array_merge($fields,
76 array('soft_credit' => array('title' => ts('Soft Credit'),
78 'headerPattern' => '/Soft Credit/i',
82 // add pledge fields only if its is enabled
83 if (CRM_Core_Permission
::access('CiviPledge')) {
84 $pledgeFields = array('pledge_payment' => array('title' => ts('Pledge Payment'),
85 'headerPattern' => '/Pledge Payment/i',
87 'pledge_id' => array('title' => ts('Pledge ID'),
88 'headerPattern' => '/Pledge ID/i',
92 $fields = array_merge($fields, $pledgeFields);
94 foreach ($fields as $name => $field) {
95 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
96 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
97 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
98 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
101 $this->_newContributions
= array();
103 $this->setActiveFields($this->_mapperKeys
);
104 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit
);
106 // FIXME: we should do this in one place together with Form/MapField.php
107 $this->_contactIdIndex
= -1;
108 $this->_totalAmountIndex
= -1;
109 $this->_contributionTypeIndex
= -1;
112 foreach ($this->_mapperKeys
as $key) {
114 case 'contribution_contact_id':
115 $this->_contactIdIndex
= $index;
119 $this->_totalAmountIndex
= $index;
122 case 'financial_type':
123 $this->_contributionTypeIndex
= $index;
131 * handle the values in mapField mode
133 * @param array $values the array of values belonging to this line
138 function mapField(&$values) {
139 return CRM_Contribute_Import_Parser
::VALID
;
143 * handle the values in preview mode
145 * @param array $values the array of values belonging to this line
147 * @return boolean the result of this processing
150 function preview(&$values) {
151 return $this->summary($values);
155 * handle the values in summary mode
157 * @param array $values the array of values belonging to this line
159 * @return boolean the result of this processing
162 function summary(&$values) {
163 $erroneousField = NULL;
164 $response = $this->setActiveFieldValues($values, $erroneousField);
166 $params = &$this->getActiveFieldParams();
167 $errorMessage = NULL;
170 $session = CRM_Core_Session
::singleton();
171 $dateType = $session->get('dateTypes');
172 foreach ($params as $key => $val) {
176 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
177 $params[$key] = $dateValue;
180 CRM_Import_Parser_Contact
::addToErrorMsg('Receive Date', $errorMessage);
185 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
186 $params[$key] = $dateValue;
189 CRM_Import_Parser_Contact
::addToErrorMsg('Cancel Date', $errorMessage);
194 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
195 $params[$key] = $dateValue;
198 CRM_Import_Parser_Contact
::addToErrorMsg('Receipt date', $errorMessage);
202 case 'thankyou_date':
203 if ($dateValue = CRM_Utils_Date
::formatDate($params[$key], $dateType)) {
204 $params[$key] = $dateValue;
207 CRM_Import_Parser_Contact
::addToErrorMsg('Thankyou Date', $errorMessage);
213 //date-Format part ends
215 $params['contact_type'] = 'Contribution';
217 //checking error in custom data
218 CRM_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
221 $tempMsg = "Invalid value for field(s) : $errorMessage";
222 array_unshift($values, $tempMsg);
223 $errorMessage = NULL;
224 return CRM_Contribute_Import_Parser
::ERROR
;
227 return CRM_Contribute_Import_Parser
::VALID
;
231 * handle the values in import mode
233 * @param int $onDuplicate the code for what action to take on duplicates
234 * @param array $values the array of values belonging to this line
236 * @return boolean the result of this processing
239 function import($onDuplicate, &$values) {
240 // first make sure this is a valid line
241 $response = $this->summary($values);
242 if ($response != CRM_Contribute_Import_Parser
::VALID
) {
246 $params = &$this->getActiveFieldParams();
247 $formatted = array('version' => 3);
249 // don't add to recent items, CRM-4399
250 $formatted['skipRecentView'] = TRUE;
253 $session = CRM_Core_Session
::singleton();
254 $dateType = $session->get('dateTypes');
256 $customFields = CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $params));
258 foreach ($params as $key => $val) {
264 case 'thankyou_date':
265 $params[$key] = CRM_Utils_Date
::formatDate($params[$key], $dateType);
267 case 'pledge_payment':
268 $params[$key] = CRM_Utils_String
::strtobool($val);
271 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
272 if ($customFields[$customFieldID]['data_type'] == 'Date') {
273 CRM_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
274 unset($params[$key]);
276 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
277 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
282 //date-Format part ends
284 static $indieFields = NULL;
285 if ($indieFields == NULL) {
286 $tempIndieFields = CRM_Contribute_DAO_Contribution
::import();
287 $indieFields = $tempIndieFields;
290 $paramValues = array();
291 foreach ($params as $key => $field) {
292 if ($field == NULL ||
$field === '') {
295 $paramValues[$key] = $field;
298 //import contribution record according to select contact type
299 if ($onDuplicate == CRM_Contribute_Import_Parser
::DUPLICATE_SKIP
&&
300 (CRM_Utils_Array
::value('contribution_contact_id', $paramValues) ||
301 CRM_Utils_Array
::value('external_identifier', $paramValues)
304 $paramValues['contact_type'] = $this->_contactType
;
306 elseif ($onDuplicate == CRM_Contribute_Import_Parser
::DUPLICATE_UPDATE
&&
307 ($paramValues['contribution_id'] ||
$values['trxn_id'] ||
$paramValues['invoice_id'])
309 $paramValues['contact_type'] = $this->_contactType
;
311 elseif (!empty($params['soft_credit'])) {
312 $paramValues['contact_type'] = $this->_contactType
;
314 elseif (CRM_Utils_Array
::value('pledge_payment', $paramValues)) {
315 $paramValues['contact_type'] = $this->_contactType
;
318 //need to pass $onDuplicate to check import mode.
319 if (CRM_Utils_Array
::value('pledge_payment', $paramValues)) {
320 $paramValues['onDuplicate'] = $onDuplicate;
322 require_once 'CRM/Utils/DeprecatedUtils.php';
323 $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE);
326 array_unshift($values, $formatError['error_message']);
327 if (CRM_Utils_Array
::value('error_data', $formatError) == 'soft_credit') {
328 return CRM_Contribute_Import_Parser
::SOFT_CREDIT_ERROR
;
330 elseif (CRM_Utils_Array
::value('error_data', $formatError) == 'pledge_payment') {
331 return CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT_ERROR
;
333 return CRM_Contribute_Import_Parser
::ERROR
;
336 if ($onDuplicate != CRM_Contribute_Import_Parser
::DUPLICATE_UPDATE
) {
337 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($formatted,
338 CRM_Core_DAO
::$_nullObject,
344 //fix for CRM-2219 - Update Contribution
345 // onDuplicate == CRM_Contribute_Import_Parser::DUPLICATE_UPDATE
346 if (CRM_Utils_Array
::value('invoice_id',$paramValues) ||
347 CRM_Utils_Array
::value('trxn_id', $paramValues) ||
$paramValues['contribution_id']) {
349 'id' => CRM_Utils_Array
::value('contribution_id', $paramValues),
350 'trxn_id' => CRM_Utils_Array
::value('trxn_id', $paramValues),
351 'invoice_id' => CRM_Utils_Array
::value('invoice_id', $paramValues),
354 $ids['contribution'] = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($dupeIds);
356 if ($ids['contribution']) {
357 $formatted['id'] = $ids['contribution'];
358 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($formatted,
359 CRM_Core_DAO
::$_nullObject,
364 if (CRM_Utils_Array
::value('note', $paramValues)) {
366 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id');
367 $daoNote = new CRM_Core_BAO_Note();
368 $daoNote->entity_table
= 'civicrm_contribution';
369 $daoNote->entity_id
= $ids['contribution'];
370 if ($daoNote->find(TRUE)) {
371 $noteID['id'] = $daoNote->id
;
375 'entity_table' => 'civicrm_contribution',
376 'note' => $paramValues['note'],
377 'entity_id' => $ids['contribution'],
378 'contact_id' => $contactID,
380 CRM_Core_BAO_Note
::add($noteParams, $noteID);
381 unset($formatted['note']);
384 //need to check existing soft credit contribution, CRM-3968
385 if (CRM_Utils_Array
::value('soft_credit_to', $formatted)) {
386 $dupeSoftCredit = array(
387 'contact_id' => $formatted['soft_credit_to'],
388 'contribution_id' => $ids['contribution'],
391 //FIXE ME: Need to fix this logic
392 $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($dupeSoftCredit);
393 if (CRM_Utils_Array
::value('soft_credit_id', $existingSoftCredit)) {
394 $formatted['softID'] = $existingSoftCredit['soft_credit_id'];
398 $newContribution = CRM_Contribute_BAO_Contribution
::create($formatted, $ids);
400 $this->_newContributions
[] = $newContribution->id
;
402 //return soft valid since we need to show how soft credits were added
403 if (CRM_Utils_Array
::value('soft_credit_to', $formatted)) {
404 return CRM_Contribute_Import_Parser
::SOFT_CREDIT
;
407 // process pledge payment assoc w/ the contribution
408 return self
::processPledgePayments($formatted);
410 return CRM_Contribute_Import_Parser
::VALID
;
414 'id' => 'Contribution ID',
415 'trxn_id' => 'Transaction ID',
416 'invoice_id' => 'Invoice ID',
418 foreach ($dupeIds as $k => $v) {
420 $errorMsg[] = "$labels[$k] $v";
423 $errorMsg = implode(' AND ', $errorMsg);
424 array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.');
425 return CRM_Contribute_Import_Parser
::ERROR
;
430 if ($this->_contactIdIndex
< 0) {
431 // set the contact type if its not set
432 if (!isset($paramValues['contact_type'])) {
433 $paramValues['contact_type'] = $this->_contactType
;
436 $paramValues['version'] = 3;
437 //retrieve contact id using contact dedupe rule
438 require_once 'CRM/Utils/DeprecatedUtils.php';
439 $error = _civicrm_api3_deprecated_check_contact_dedupe($paramValues);
441 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
442 $matchedIDs = explode(',', $error['error_message']['params'][0]);
443 if (count($matchedIDs) > 1) {
444 array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported');
445 return CRM_Contribute_Import_Parser
::ERROR
;
448 $cid = $matchedIDs[0];
449 $formatted['contact_id'] = $cid;
451 $newContribution = civicrm_api('contribution', 'create', $formatted);
452 if (civicrm_error($newContribution)) {
453 if (is_array($newContribution['error_message'])) {
454 array_unshift($values, $newContribution['error_message']['message']);
455 if ($newContribution['error_message']['params'][0]) {
456 return CRM_Contribute_Import_Parser
::DUPLICATE
;
460 array_unshift($values, $newContribution['error_message']);
461 return CRM_Contribute_Import_Parser
::ERROR
;
465 $this->_newContributions
[] = $newContribution['id'];
466 $formatted['contribution_id'] = $newContribution['id'];
468 //return soft valid since we need to show how soft credits were added
469 if (CRM_Utils_Array
::value('soft_credit_to', $formatted)) {
470 return CRM_Contribute_Import_Parser
::SOFT_CREDIT
;
473 // process pledge payment assoc w/ the contribution
474 return self
::processPledgePayments($formatted);
476 return CRM_Contribute_Import_Parser
::VALID
;
480 // Using new Dedupe rule.
482 'contact_type' => $this->_contactType
,
483 'used' => 'Unsupervised',
485 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
487 foreach ($fieldsArray as $value) {
488 if (array_key_exists(trim($value), $params)) {
489 $paramValue = $params[trim($value)];
490 if (is_array($paramValue)) {
491 $disp .= $params[trim($value)][0][trim($value)] . " ";
494 $disp .= $params[trim($value)] . " ";
499 if (CRM_Utils_Array
::value('external_identifier', $params)) {
501 $disp .= "AND {$params['external_identifier']}";
504 $disp = $params['external_identifier'];
508 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
509 return CRM_Contribute_Import_Parser
::ERROR
;
513 if (CRM_Utils_Array
::value('external_identifier', $paramValues)) {
514 $checkCid = new CRM_Contact_DAO_Contact();
515 $checkCid->external_identifier
= $paramValues['external_identifier'];
516 $checkCid->find(TRUE);
517 if ($checkCid->id
!= $formatted['contact_id']) {
518 array_unshift($values, 'Mismatch of External identifier :' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
519 return CRM_Contribute_Import_Parser
::ERROR
;
522 $newContribution = civicrm_api('contribution', 'create', $formatted);
523 if (civicrm_error($newContribution)) {
524 if (is_array($newContribution['error_message'])) {
525 array_unshift($values, $newContribution['error_message']['message']);
526 if ($newContribution['error_message']['params'][0]) {
527 return CRM_Contribute_Import_Parser
::DUPLICATE
;
531 array_unshift($values, $newContribution['error_message']);
532 return CRM_Contribute_Import_Parser
::ERROR
;
536 $this->_newContributions
[] = $newContribution['id'];
537 $formatted['contribution_id'] = $newContribution['id'];
539 //return soft valid since we need to show how soft credits were added
540 if (CRM_Utils_Array
::value('soft_credit_to', $formatted)) {
541 return CRM_Contribute_Import_Parser
::SOFT_CREDIT
;
544 // process pledge payment assoc w/ the contribution
545 return self
::processPledgePayments($formatted);
547 return CRM_Contribute_Import_Parser
::VALID
;
552 * Function to process pledge payments
554 function processPledgePayments(&$formatted) {
555 if (CRM_Utils_Array
::value('pledge_payment_id', $formatted) &&
556 CRM_Utils_Array
::value('pledge_id', $formatted)
558 //get completed status
559 $completeStatusID = CRM_Core_OptionGroup
::getValue('contribution_status', 'Completed', 'name');
561 //need to update payment record to map contribution_id
562 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'],
563 'contribution_id', $formatted['contribution_id']
566 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($formatted['pledge_id'],
567 array($formatted['pledge_payment_id']),
570 $formatted['total_amount']
573 return CRM_Contribute_Import_Parser
::PLEDGE_PAYMENT
;
578 * Get the array of successfully imported contribution id's
583 function &getImportedContributions() {
584 return $this->_newContributions
;
588 * the initializer code, called before the processing