Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Contribute / Import / Parser / Contribution.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * Class to parse contribution csv files.
36 */
37 class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Parser {
38
39 protected $_mapperKeys;
40
41 private $_contactIdIndex;
42 private $_totalAmountIndex;
43 private $_contributionTypeIndex;
44
45 protected $_mapperSoftCredit;
46 //protected $_mapperPhoneType;
47
48 /**
49 * Array of successfully imported contribution id's
50 *
51 * @array
52 */
53 protected $_newContributions;
54
55 /**
56 * Class constructor.
57 *
58 * @param $mapperKeys
59 * @param null $mapperSoftCredit
60 * @param null $mapperPhoneType
61 * @param null $mapperSoftCreditType
62 */
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;
68 }
69
70 /**
71 * The initializer code, called before the processing
72 */
73 public function init() {
74 $fields = CRM_Contribute_BAO_Contribution::importableFields($this->_contactType, FALSE);
75
76 $fields = array_merge($fields,
77 array(
78 'soft_credit' => array(
79 'title' => ts('Soft Credit'),
80 'softCredit' => TRUE,
81 'headerPattern' => '/Soft Credit/i',
82 ),
83 )
84 );
85
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',
92 ),
93 'pledge_id' => array(
94 'title' => ts('Pledge ID'),
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);
112 $this->setActiveFieldSoftCreditType($this->_mapperSoftCreditType);
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
142 * The array of values belonging to this line.
143 *
144 * @return bool
145 */
146 public function mapField(&$values) {
147 return CRM_Import_Parser::VALID;
148 }
149
150 /**
151 * Handle the values in preview mode.
152 *
153 * @param array $values
154 * The array of values belonging to this line.
155 *
156 * @return bool
157 * the result of this processing
158 */
159 public function preview(&$values) {
160 return $this->summary($values);
161 }
162
163 /**
164 * Handle the values in summary mode.
165 *
166 * @param array $values
167 * The array of values belonging to this line.
168 *
169 * @return bool
170 * the result of this processing
171 */
172 public function summary(&$values) {
173 $erroneousField = NULL;
174 $response = $this->setActiveFieldValues($values, $erroneousField);
175
176 $params = &$this->getActiveFieldParams();
177 $errorMessage = NULL;
178
179 //for date-Formats
180 $session = CRM_Core_Session::singleton();
181 $dateType = $session->get('dateTypes');
182 foreach ($params as $key => $val) {
183 if ($val) {
184 switch ($key) {
185 case 'receive_date':
186 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
187 $params[$key] = $dateValue;
188 }
189 else {
190 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Receive Date', $errorMessage);
191 }
192 break;
193
194 case 'cancel_date':
195 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
196 $params[$key] = $dateValue;
197 }
198 else {
199 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Cancel Date', $errorMessage);
200 }
201 break;
202
203 case 'receipt_date':
204 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
205 $params[$key] = $dateValue;
206 }
207 else {
208 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Receipt date', $errorMessage);
209 }
210 break;
211
212 case 'thankyou_date':
213 if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) {
214 $params[$key] = $dateValue;
215 }
216 else {
217 CRM_Contact_Import_Parser_Contact::addToErrorMsg('Thankyou Date', $errorMessage);
218 }
219 break;
220 }
221 }
222 }
223 //date-Format part ends
224
225 $params['contact_type'] = 'Contribution';
226
227 //checking error in custom data
228 CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
229
230 if ($errorMessage) {
231 $tempMsg = "Invalid value for field(s) : $errorMessage";
232 array_unshift($values, $tempMsg);
233 $errorMessage = NULL;
234 return CRM_Import_Parser::ERROR;
235 }
236
237 return CRM_Import_Parser::VALID;
238 }
239
240 /**
241 * Handle the values in import mode.
242 *
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.
247 *
248 * @return bool
249 * the result of this processing
250 */
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) {
255 return $response;
256 }
257
258 $params = &$this->getActiveFieldParams();
259 $formatted = array('version' => 3);
260
261 // don't add to recent items, CRM-4399
262 $formatted['skipRecentView'] = TRUE;
263
264 //for date-Formats
265 $session = CRM_Core_Session::singleton();
266 $dateType = $session->get('dateTypes');
267
268 $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Contribution';
269 $customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
270
271 //CRM-10994
272 if (isset($params['total_amount']) && $params['total_amount'] == 0) {
273 $params['total_amount'] = '0.00';
274 }
275 foreach ($params as $key => $val) {
276 if ($val) {
277 switch ($key) {
278 case 'receive_date':
279 case 'cancel_date':
280 case 'receipt_date':
281 case 'thankyou_date':
282 $params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType);
283 break;
284
285 case 'pledge_payment':
286 $params[$key] = CRM_Utils_String::strtobool($val);
287 break;
288
289 }
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]);
294 }
295 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
296 $params[$key] = CRM_Utils_String::strtoboolstr($val);
297 }
298 }
299 }
300 }
301 //date-Format part ends
302
303 static $indieFields = NULL;
304 if ($indieFields == NULL) {
305 $tempIndieFields = CRM_Contribute_DAO_Contribution::import();
306 $indieFields = $tempIndieFields;
307 }
308
309 $paramValues = array();
310 foreach ($params as $key => $field) {
311 if ($field == NULL || $field === '') {
312 continue;
313 }
314 $paramValues[$key] = $field;
315 }
316
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']))
320 ) {
321 $paramValues['contact_type'] = $this->_contactType;
322 }
323 elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
324 (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
325 ) {
326 $paramValues['contact_type'] = $this->_contactType;
327 }
328 elseif (!empty($params['soft_credit'])) {
329 $paramValues['contact_type'] = $this->_contactType;
330 }
331 elseif (!empty($paramValues['pledge_payment'])) {
332 $paramValues['contact_type'] = $this->_contactType;
333 }
334
335 //need to pass $onDuplicate to check import mode.
336 if (!empty($paramValues['pledge_payment'])) {
337 $paramValues['onDuplicate'] = $onDuplicate;
338 }
339 require_once 'CRM/Utils/DeprecatedUtils.php';
340 $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE, $onDuplicate);
341
342 if ($formatError) {
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;
346 }
347 elseif (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') {
348 return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR;
349 }
350 return CRM_Import_Parser::ERROR;
351 }
352
353 if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) {
354 $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
355 NULL,
356 'Contribution'
357 );
358 }
359 else {
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'])) {
363 $dupeIds = array(
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),
367 );
368
369 $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds);
370
371 if ($ids['contribution']) {
372 $formatted['id'] = $ids['contribution'];
373 $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted,
374 $formatted['id'],
375 'Contribution'
376 );
377 //process note
378 if (!empty($paramValues['note'])) {
379 $noteID = array();
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;
386 }
387
388 $noteParams = array(
389 'entity_table' => 'civicrm_contribution',
390 'note' => $paramValues['note'],
391 'entity_id' => $ids['contribution'],
392 'contact_id' => $contactID,
393 );
394 CRM_Core_BAO_Note::add($noteParams, $noteID);
395 unset($formatted['note']);
396 }
397
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'],
403 );
404
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'],
412 'pcp_id' => NULL,
413 );
414 CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
415 }
416 }
417 }
418 }
419
420 $newContribution = CRM_Contribute_BAO_Contribution::create($formatted, $ids);
421 $this->_newContributions[] = $newContribution->id;
422
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;
426 }
427
428 // process pledge payment assoc w/ the contribution
429 return self::processPledgePayments($formatted);
430
431 return CRM_Import_Parser::VALID;
432 }
433 else {
434 $labels = array(
435 'id' => 'Contribution ID',
436 'trxn_id' => 'Transaction ID',
437 'invoice_id' => 'Invoice ID',
438 );
439 foreach ($dupeIds as $k => $v) {
440 if ($v) {
441 $errorMsg[] = "$labels[$k] $v";
442 }
443 }
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;
447 }
448 }
449 }
450
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;
455 }
456
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);
461
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;
467 }
468 else {
469 $cid = $matchedIDs[0];
470 $formatted['contact_id'] = $cid;
471
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;
478 }
479 }
480 else {
481 array_unshift($values, $newContribution['error_message']);
482 return CRM_Import_Parser::ERROR;
483 }
484 }
485
486 $this->_newContributions[] = $newContribution['id'];
487 $formatted['contribution_id'] = $newContribution['id'];
488
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;
492 }
493
494 // process pledge payment assoc w/ the contribution
495 return self::processPledgePayments($formatted);
496
497 return CRM_Import_Parser::VALID;
498 }
499 }
500 else {
501 // Using new Dedupe rule.
502 $ruleParams = array(
503 'contact_type' => $this->_contactType,
504 'used' => 'Unsupervised',
505 );
506 $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams);
507 $disp = NULL;
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)] . " ";
513 }
514 else {
515 $disp .= $params[trim($value)] . " ";
516 }
517 }
518 }
519
520 if (!empty($params['external_identifier'])) {
521 if ($disp) {
522 $disp .= "AND {$params['external_identifier']}";
523 }
524 else {
525 $disp = $params['external_identifier'];
526 }
527 }
528
529 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
530 return CRM_Import_Parser::ERROR;
531 }
532 }
533 else {
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;
541 }
542 }
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;
549 }
550 }
551 else {
552 array_unshift($values, $newContribution['error_message']);
553 return CRM_Import_Parser::ERROR;
554 }
555 }
556
557 $this->_newContributions[] = $newContribution['id'];
558 $formatted['contribution_id'] = $newContribution['id'];
559
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;
563 }
564
565 // process pledge payment assoc w/ the contribution
566 return self::processPledgePayments($formatted);
567
568 return CRM_Import_Parser::VALID;
569 }
570 }
571
572 /**
573 * Process pledge payments.
574 *
575 * @param array $formatted
576 *
577 * @return int
578 */
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');
583
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']
587 );
588
589 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($formatted['pledge_id'],
590 array($formatted['pledge_payment_id']),
591 $completeStatusID,
592 NULL,
593 $formatted['total_amount']
594 );
595
596 return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT;
597 }
598 }
599
600 /**
601 * Get the array of successfully imported contribution id's
602 *
603 * @return array
604 */
605 public function &getImportedContributions() {
606 return $this->_newContributions;
607 }
608
609 /**
610 * The initializer code, called before the processing.
611 */
612 public function fini() {
613 }
614
615 }