Merge pull request #2744 from pratik-joshi/CRM-13992_temp_fix
[civicrm-core.git] / CRM / Contribute / Import / Parser / Contribution.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * class to parse contribution csv files
38 */
39class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Parser {
40
41 protected $_mapperKeys;
42
43 private $_contactIdIndex;
44 private $_totalAmountIndex;
45 private $_contributionTypeIndex;
46
47 protected $_mapperSoftCredit;
48 //protected $_mapperPhoneType;
49
50 /**
ceb10dc7 51 * Array of successfully imported contribution id's
6a488035
TO
52 *
53 * @array
54 */
55 protected $_newContributions;
56
57 /**
58 * class constructor
59 */
1221efe9 60 function __construct(&$mapperKeys, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL) {
6a488035
TO
61 parent::__construct();
62 $this->_mapperKeys = &$mapperKeys;
63 $this->_mapperSoftCredit = &$mapperSoftCredit;
1221efe9 64 $this->_mapperSoftCreditType = &$mapperSoftCreditType;
6a488035
TO
65 }
66
67 /**
68 * the initializer code, called before the processing
69 *
70 * @return void
71 * @access public
72 */
73 function init() {
74 $fields = CRM_Contribute_BAO_Contribution::importableFields($this->_contactType, FALSE);
75
76 $fields = array_merge($fields,
91bb24a7 77 array(
78 'soft_credit' => array(
79 'title' => ts('Soft Credit'),
80 'softCredit' => TRUE,
81 'headerPattern' => '/Soft Credit/i',
82 )
83 )
6a488035
TO
84 );
85
86 // add pledge fields only if its is enabled
87 if (CRM_Core_Permission::access('CiviPledge')) {
91bb24a7 88 $pledgeFields = array(
89 'pledge_payment' => array(
90 'title' => ts('Pledge Payment'),
6a488035
TO
91 'headerPattern' => '/Pledge Payment/i',
92 ),
91bb24a7 93 'pledge_id' => array(
94 'title' => ts('Pledge ID'),
6a488035
TO
95 'headerPattern' => '/Pledge ID/i',
96 ),
97 );
98
99 $fields = array_merge($fields, $pledgeFields);
100 }
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']);
106 }
107
108 $this->_newContributions = array();
109
110 $this->setActiveFields($this->_mapperKeys);
111 $this->setActiveFieldSoftCredit($this->_mapperSoftCredit);
1221efe9 112 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType);
6a488035
TO
113
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;
118
119 $index = 0;
120 foreach ($this->_mapperKeys as $key) {
121 switch ($key) {
122 case 'contribution_contact_id':
123 $this->_contactIdIndex = $index;
124 break;
125
126 case 'total_amount':
127 $this->_totalAmountIndex = $index;
128 break;
129
130 case 'financial_type':
131 $this->_contributionTypeIndex = $index;
132 break;
133 }
134 $index++;
135 }
136 }
137
138 /**
139 * handle the values in mapField mode
140 *
141 * @param array $values the array of values belonging to this line
142 *
143 * @return boolean
144 * @access public
145 */
146 function mapField(&$values) {
a05662ef 147 return CRM_Import_Parser::VALID;
6a488035
TO
148 }
149
150 /**
151 * handle the values in preview mode
152 *
153 * @param array $values the array of values belonging to this line
154 *
155 * @return boolean the result of this processing
156 * @access public
157 */
158 function preview(&$values) {
159 return $this->summary($values);
160 }
161
162 /**
163 * handle the values in summary mode
164 *
165 * @param array $values the array of values belonging to this line
166 *
167 * @return boolean the result of this processing
168 * @access public
169 */
170 function summary(&$values) {
171 $erroneousField = NULL;
172 $response = $this->setActiveFieldValues($values, $erroneousField);
173
174 $params = &$this->getActiveFieldParams();
175 $errorMessage = NULL;
176
177 //for date-Formats
178 $session = CRM_Core_Session::singleton();
179 $dateType = $session->get('dateTypes');
180 foreach ($params as $key => $val) {
181 if ($val) {
182 switch ($key) {
183 case 'receive_date':
184 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
185 $params[$key] = $dateValue;
186 }
187 else {
719a6fec 188 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Receive Date', $errorMessage);
6a488035
TO
189 }
190 break;
191
192 case 'cancel_date':
193 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
ceb10dc7 194 $params[$key] = $dateValue;
6a488035
TO
195 }
196 else {
719a6fec 197 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Cancel Date', $errorMessage);
6a488035
TO
198 }
199 break;
200
201 case 'receipt_date':
202 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
203 $params[$key] = $dateValue;
204 }
205 else {
719a6fec 206 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Receipt date', $errorMessage);
6a488035
TO
207 }
208 break;
209
210 case 'thankyou_date':
211 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
212 $params[$key] = $dateValue;
213 }
214 else {
719a6fec 215 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Thankyou Date', $errorMessage);
6a488035
TO
216 }
217 break;
218 }
219 }
220 }
221 //date-Format part ends
222
223 $params['contact_type'] = 'Contribution';
224
225 //checking error in custom data
719a6fec 226 CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
6a488035
TO
227
228 if ($errorMessage) {
229 $tempMsg = "Invalid value for field(s) : $errorMessage";
230 array_unshift($values, $tempMsg);
231 $errorMessage = NULL;
a05662ef 232 return CRM_Import_Parser::ERROR;
6a488035
TO
233 }
234
a05662ef 235 return CRM_Import_Parser::VALID;
6a488035
TO
236 }
237
238 /**
239 * handle the values in import mode
240 *
241 * @param int $onDuplicate the code for what action to take on duplicates
242 * @param array $values the array of values belonging to this line
243 *
244 * @return boolean the result of this processing
245 * @access public
246 */
247 function import($onDuplicate, &$values) {
248 // first make sure this is a valid line
249 $response = $this->summary($values);
a05662ef 250 if ($response != CRM_Import_Parser::VALID) {
6a488035
TO
251 return $response;
252 }
253
254 $params = &$this->getActiveFieldParams();
255 $formatted = array('version' => 3);
256
257 // don't add to recent items, CRM-4399
258 $formatted['skipRecentView'] = TRUE;
259
260 //for date-Formats
261 $session = CRM_Core_Session::singleton();
262 $dateType = $session->get('dateTypes');
263
264 $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $params));
265
c2e1f9ef 266 //CRM-10994
267 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
1ba834a8 268 $params['total_amount'] = '0.00';
c2e1f9ef 269 }
6a488035
TO
270 foreach ($params as $key => $val) {
271 if ($val) {
272 switch ($key) {
ad8eaec9 273 case 'receive_date':
274 case 'cancel_date':
275 case 'receipt_date':
276 case 'thankyou_date':
277 $params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType);
278 break;
279 case 'pledge_payment':
280 $params[$key] = CRM_Utils_String::strtobool($val);
281 break;
6a488035
TO
282 }
283 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
284 if ($customFields[$customFieldID]['data_type'] == 'Date') {
719a6fec 285 CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
6a488035
TO
286 unset($params[$key]);
287 }
288 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
289 $params[$key] = CRM_Utils_String::strtoboolstr($val);
290 }
291 }
292 }
293 }
294 //date-Format part ends
295
296 static $indieFields = NULL;
297 if ($indieFields == NULL) {
298 $tempIndieFields = CRM_Contribute_DAO_Contribution::import();
299 $indieFields = $tempIndieFields;
300 }
301
302 $paramValues = array();
303 foreach ($params as $key => $field) {
304 if ($field == NULL || $field === '') {
305 continue;
306 }
307 $paramValues[$key] = $field;
308 }
309
310 //import contribution record according to select contact type
a05662ef 311 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP &&
8cc574cf 312 (!empty($paramValues['contribution_contact_id']) || !empty($paramValues['external_identifier']))
6a488035
TO
313 ) {
314 $paramValues['contact_type'] = $this->_contactType;
315 }
a05662ef 316 elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
1221efe9 317 (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
6a488035
TO
318 ) {
319 $paramValues['contact_type'] = $this->_contactType;
320 }
321 elseif (!empty($params['soft_credit'])) {
322 $paramValues['contact_type'] = $this->_contactType;
323 }
a7488080 324 elseif (!empty($paramValues['pledge_payment'])) {
6a488035
TO
325 $paramValues['contact_type'] = $this->_contactType;
326 }
327
328 //need to pass $onDuplicate to check import mode.
a7488080 329 if (!empty($paramValues['pledge_payment'])) {
6a488035
TO
330 $paramValues['onDuplicate'] = $onDuplicate;
331 }
332 require_once 'CRM/Utils/DeprecatedUtils.php';
1221efe9 333 $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE, $onDuplicate);
6a488035
TO
334
335 if ($formatError) {
336 array_unshift($values, $formatError['error_message']);
337 if (CRM_Utils_Array::value('error_data', $formatError) == 'soft_credit') {
338 return CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR;
339 }
340 elseif (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') {
341 return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR;
342 }
a05662ef 343 return CRM_Import_Parser::ERROR;
6a488035
TO
344 }
345
a05662ef 346 if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) {
6a488035
TO
347 $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
348 CRM_Core_DAO::$_nullObject,
349 NULL,
350 'Contribution'
351 );
352 }
353 else {
354 //fix for CRM-2219 - Update Contribution
a05662ef 355 // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
1221efe9 356 if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) {
6a488035
TO
357 $dupeIds = array(
358 'id' => CRM_Utils_Array::value('contribution_id', $paramValues),
359 'trxn_id' => CRM_Utils_Array::value('trxn_id', $paramValues),
360 'invoice_id' => CRM_Utils_Array::value('invoice_id', $paramValues),
361 );
362
363 $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds);
364
365 if ($ids['contribution']) {
366 $formatted['id'] = $ids['contribution'];
367 $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
368 CRM_Core_DAO::$_nullObject,
369 $formatted['id'],
370 'Contribution'
371 );
372 //process note
a7488080 373 if (!empty($paramValues['note'])) {
6a488035
TO
374 $noteID = array();
375 $contactID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id');
376 $daoNote = new CRM_Core_BAO_Note();
377 $daoNote->entity_table = 'civicrm_contribution';
378 $daoNote->entity_id = $ids['contribution'];
379 if ($daoNote->find(TRUE)) {
380 $noteID['id'] = $daoNote->id;
381 }
382
383 $noteParams = array(
384 'entity_table' => 'civicrm_contribution',
385 'note' => $paramValues['note'],
386 'entity_id' => $ids['contribution'],
387 'contact_id' => $contactID,
388 );
389 CRM_Core_BAO_Note::add($noteParams, $noteID);
390 unset($formatted['note']);
391 }
392
393 //need to check existing soft credit contribution, CRM-3968
1221efe9 394 if (!empty($formatted['soft_credit'])) {
6a488035 395 $dupeSoftCredit = array(
1221efe9 396 'contact_id' => $formatted['soft_credit'],
6a488035
TO
397 'contribution_id' => $ids['contribution'],
398 );
8ef12e64 399
1221efe9 400 //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null
91bb24a7 401 $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']);
1221efe9 402 if(isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])){
403 foreach($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues){
404 if (!empty($existingSoftCreditValues['soft_credit_id'])) {
405 $deleteParams = array(
406 'id' => $existingSoftCreditValues['soft_credit_id'],
407 'pcp_id' => NULL,
408 );
409 CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
410 }
411 }
6a488035
TO
412 }
413 }
414
415 $newContribution = CRM_Contribute_BAO_Contribution::create($formatted, $ids);
6a488035
TO
416 $this->_newContributions[] = $newContribution->id;
417
418 //return soft valid since we need to show how soft credits were added
1221efe9 419 if (!empty($formatted['soft_credit'])) {
6a488035
TO
420 return CRM_Contribute_Import_Parser::SOFT_CREDIT;
421 }
422
423 // process pledge payment assoc w/ the contribution
424 return self::processPledgePayments($formatted);
425
a05662ef 426 return CRM_Import_Parser::VALID;
6a488035
TO
427 }
428 else {
429 $labels = array(
430 'id' => 'Contribution ID',
431 'trxn_id' => 'Transaction ID',
432 'invoice_id' => 'Invoice ID',
433 );
434 foreach ($dupeIds as $k => $v) {
435 if ($v) {
436 $errorMsg[] = "$labels[$k] $v";
437 }
438 }
439 $errorMsg = implode(' AND ', $errorMsg);
440 array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.');
a05662ef 441 return CRM_Import_Parser::ERROR;
6a488035
TO
442 }
443 }
444 }
445
446 if ($this->_contactIdIndex < 0) {
447 // set the contact type if its not set
448 if (!isset($paramValues['contact_type'])) {
449 $paramValues['contact_type'] = $this->_contactType;
450 }
451
452 $paramValues['version'] = 3;
453 //retrieve contact id using contact dedupe rule
454 require_once 'CRM/Utils/DeprecatedUtils.php';
455 $error = _civicrm_api3_deprecated_check_contact_dedupe($paramValues);
456
457 if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
458 $matchedIDs = explode(',', $error['error_message']['params'][0]);
459 if (count($matchedIDs) > 1) {
460 array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported');
a05662ef 461 return CRM_Import_Parser::ERROR;
6a488035
TO
462 }
463 else {
464 $cid = $matchedIDs[0];
465 $formatted['contact_id'] = $cid;
466
467 $newContribution = civicrm_api('contribution', 'create', $formatted);
468 if (civicrm_error($newContribution)) {
469 if (is_array($newContribution['error_message'])) {
470 array_unshift($values, $newContribution['error_message']['message']);
471 if ($newContribution['error_message']['params'][0]) {
a05662ef 472 return CRM_Import_Parser::DUPLICATE;
6a488035
TO
473 }
474 }
475 else {
476 array_unshift($values, $newContribution['error_message']);
a05662ef 477 return CRM_Import_Parser::ERROR;
6a488035
TO
478 }
479 }
480
481 $this->_newContributions[] = $newContribution['id'];
482 $formatted['contribution_id'] = $newContribution['id'];
483
484 //return soft valid since we need to show how soft credits were added
1221efe9 485 if (!empty($formatted['soft_credit'])) {
6a488035
TO
486 return CRM_Contribute_Import_Parser::SOFT_CREDIT;
487 }
488
489 // process pledge payment assoc w/ the contribution
490 return self::processPledgePayments($formatted);
491
a05662ef 492 return CRM_Import_Parser::VALID;
6a488035
TO
493 }
494 }
495 else {
496 // Using new Dedupe rule.
497 $ruleParams = array(
498 'contact_type' => $this->_contactType,
499 'used' => 'Unsupervised',
500 );
501 $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams);
1221efe9 502 $disp = NULL;
6a488035
TO
503 foreach ($fieldsArray as $value) {
504 if (array_key_exists(trim($value), $params)) {
505 $paramValue = $params[trim($value)];
506 if (is_array($paramValue)) {
507 $disp .= $params[trim($value)][0][trim($value)] . " ";
508 }
509 else {
510 $disp .= $params[trim($value)] . " ";
511 }
512 }
513 }
514
a7488080 515 if (!empty($params['external_identifier'])) {
6a488035
TO
516 if ($disp) {
517 $disp .= "AND {$params['external_identifier']}";
518 }
519 else {
520 $disp = $params['external_identifier'];
521 }
522 }
523
524 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
a05662ef 525 return CRM_Import_Parser::ERROR;
6a488035
TO
526 }
527 }
528 else {
a7488080 529 if (!empty($paramValues['external_identifier'])) {
6a488035
TO
530 $checkCid = new CRM_Contact_DAO_Contact();
531 $checkCid->external_identifier = $paramValues['external_identifier'];
532 $checkCid->find(TRUE);
533 if ($checkCid->id != $formatted['contact_id']) {
534 array_unshift($values, 'Mismatch of External identifier :' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
a05662ef 535 return CRM_Import_Parser::ERROR;
6a488035
TO
536 }
537 }
538 $newContribution = civicrm_api('contribution', 'create', $formatted);
539 if (civicrm_error($newContribution)) {
540 if (is_array($newContribution['error_message'])) {
541 array_unshift($values, $newContribution['error_message']['message']);
542 if ($newContribution['error_message']['params'][0]) {
a05662ef 543 return CRM_Import_Parser::DUPLICATE;
6a488035
TO
544 }
545 }
546 else {
547 array_unshift($values, $newContribution['error_message']);
a05662ef 548 return CRM_Import_Parser::ERROR;
6a488035
TO
549 }
550 }
551
552 $this->_newContributions[] = $newContribution['id'];
553 $formatted['contribution_id'] = $newContribution['id'];
554
555 //return soft valid since we need to show how soft credits were added
1221efe9 556 if (!empty($formatted['soft_credit'])) {
6a488035
TO
557 return CRM_Contribute_Import_Parser::SOFT_CREDIT;
558 }
559
560 // process pledge payment assoc w/ the contribution
561 return self::processPledgePayments($formatted);
562
a05662ef 563 return CRM_Import_Parser::VALID;
6a488035
TO
564 }
565 }
566
567 /**
568 * Function to process pledge payments
569 */
570 function processPledgePayments(&$formatted) {
8cc574cf 571 if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) {
6a488035
TO
572 //get completed status
573 $completeStatusID = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
574
575 //need to update payment record to map contribution_id
576 CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'],
577 'contribution_id', $formatted['contribution_id']
578 );
579
580 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($formatted['pledge_id'],
581 array($formatted['pledge_payment_id']),
582 $completeStatusID,
583 NULL,
584 $formatted['total_amount']
585 );
586
587 return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT;
588 }
589 }
590
591 /**
ceb10dc7 592 * Get the array of successfully imported contribution id's
6a488035
TO
593 *
594 * @return array
595 * @access public
596 */
597 function &getImportedContributions() {
598 return $this->_newContributions;
599 }
600
601 /**
602 * the initializer code, called before the processing
603 *
604 * @return void
605 * @access public
606 */
607 function fini() {}
608}
609