Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
74ab7ba8 | 35 | * Class to parse contribution csv files. |
6a488035 TO |
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 | /** | |
ceb10dc7 | 49 | * Array of successfully imported contribution id's |
6a488035 TO |
50 | * |
51 | * @array | |
52 | */ | |
53 | protected $_newContributions; | |
54 | ||
55 | /** | |
74ab7ba8 EM |
56 | * Class constructor. |
57 | * | |
58 | * @param $mapperKeys | |
59 | * @param null $mapperSoftCredit | |
60 | * @param null $mapperPhoneType | |
61 | * @param null $mapperSoftCreditType | |
6a488035 | 62 | */ |
00be9182 | 63 | public function __construct(&$mapperKeys, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL) { |
6a488035 TO |
64 | parent::__construct(); |
65 | $this->_mapperKeys = &$mapperKeys; | |
66 | $this->_mapperSoftCredit = &$mapperSoftCredit; | |
1221efe9 | 67 | $this->_mapperSoftCreditType = &$mapperSoftCreditType; |
6a488035 TO |
68 | } |
69 | ||
70 | /** | |
100fef9d | 71 | * The initializer code, called before the processing |
6a488035 | 72 | */ |
00be9182 | 73 | public function init() { |
6a488035 TO |
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', | |
21dfd5f5 | 82 | ), |
91bb24a7 | 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 | /** | |
fe482240 | 139 | * Handle the values in mapField mode. |
6a488035 | 140 | * |
014c4014 TO |
141 | * @param array $values |
142 | * The array of values belonging to this line. | |
6a488035 | 143 | * |
acb1052e | 144 | * @return bool |
6a488035 | 145 | */ |
00be9182 | 146 | public function mapField(&$values) { |
a05662ef | 147 | return CRM_Import_Parser::VALID; |
6a488035 TO |
148 | } |
149 | ||
150 | /** | |
fe482240 | 151 | * Handle the values in preview mode. |
6a488035 | 152 | * |
014c4014 TO |
153 | * @param array $values |
154 | * The array of values belonging to this line. | |
6a488035 | 155 | * |
acb1052e | 156 | * @return bool |
a6c01b45 | 157 | * the result of this processing |
6a488035 | 158 | */ |
00be9182 | 159 | public function preview(&$values) { |
6a488035 TO |
160 | return $this->summary($values); |
161 | } | |
162 | ||
163 | /** | |
fe482240 | 164 | * Handle the values in summary mode. |
6a488035 | 165 | * |
014c4014 TO |
166 | * @param array $values |
167 | * The array of values belonging to this line. | |
6a488035 | 168 | * |
acb1052e | 169 | * @return bool |
a6c01b45 | 170 | * the result of this processing |
6a488035 | 171 | */ |
00be9182 | 172 | public function summary(&$values) { |
6a488035 TO |
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 { | |
719a6fec | 190 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Receive Date', $errorMessage); |
6a488035 TO |
191 | } |
192 | break; | |
193 | ||
194 | case 'cancel_date': | |
195 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
ceb10dc7 | 196 | $params[$key] = $dateValue; |
6a488035 TO |
197 | } |
198 | else { | |
719a6fec | 199 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Cancel Date', $errorMessage); |
6a488035 TO |
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 { | |
719a6fec | 208 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Receipt date', $errorMessage); |
6a488035 TO |
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 { | |
719a6fec | 217 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Thankyou Date', $errorMessage); |
6a488035 TO |
218 | } |
219 | break; | |
220 | } | |
221 | } | |
222 | } | |
223 | //date-Format part ends | |
224 | ||
225 | $params['contact_type'] = 'Contribution'; | |
226 | ||
227 | //checking error in custom data | |
719a6fec | 228 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
229 | |
230 | if ($errorMessage) { | |
231 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
232 | array_unshift($values, $tempMsg); | |
233 | $errorMessage = NULL; | |
a05662ef | 234 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
235 | } |
236 | ||
a05662ef | 237 | return CRM_Import_Parser::VALID; |
6a488035 TO |
238 | } |
239 | ||
240 | /** | |
fe482240 | 241 | * Handle the values in import mode. |
6a488035 | 242 | * |
014c4014 TO |
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. | |
6a488035 | 247 | * |
acb1052e | 248 | * @return bool |
a6c01b45 | 249 | * the result of this processing |
6a488035 | 250 | */ |
00be9182 | 251 | public function import($onDuplicate, &$values) { |
6a488035 TO |
252 | // first make sure this is a valid line |
253 | $response = $this->summary($values); | |
a05662ef | 254 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
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 | ||
6ae6166d | 268 | $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Contribution'; |
269 | $customFields = CRM_Core_BAO_CustomField::getFields($customDataType); | |
6a488035 | 270 | |
c2e1f9ef | 271 | //CRM-10994 |
272 | if (isset($params['total_amount']) && $params['total_amount'] == 0) { | |
1ba834a8 | 273 | $params['total_amount'] = '0.00'; |
c2e1f9ef | 274 | } |
6a488035 TO |
275 | foreach ($params as $key => $val) { |
276 | if ($val) { | |
277 | switch ($key) { | |
353ffa53 TO |
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; | |
ea100cb5 | 284 | |
353ffa53 TO |
285 | case 'pledge_payment': |
286 | $params[$key] = CRM_Utils_String::strtobool($val); | |
287 | break; | |
ea100cb5 | 288 | |
6a488035 TO |
289 | } |
290 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
291 | if ($customFields[$customFieldID]['data_type'] == 'Date') { | |
719a6fec | 292 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
6a488035 TO |
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 | |
a05662ef | 318 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP && |
8cc574cf | 319 | (!empty($paramValues['contribution_contact_id']) || !empty($paramValues['external_identifier'])) |
6a488035 TO |
320 | ) { |
321 | $paramValues['contact_type'] = $this->_contactType; | |
322 | } | |
a05662ef | 323 | elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && |
1221efe9 | 324 | (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id'])) |
6a488035 TO |
325 | ) { |
326 | $paramValues['contact_type'] = $this->_contactType; | |
327 | } | |
328 | elseif (!empty($params['soft_credit'])) { | |
329 | $paramValues['contact_type'] = $this->_contactType; | |
330 | } | |
a7488080 | 331 | elseif (!empty($paramValues['pledge_payment'])) { |
6a488035 TO |
332 | $paramValues['contact_type'] = $this->_contactType; |
333 | } | |
334 | ||
335 | //need to pass $onDuplicate to check import mode. | |
a7488080 | 336 | if (!empty($paramValues['pledge_payment'])) { |
6a488035 TO |
337 | $paramValues['onDuplicate'] = $onDuplicate; |
338 | } | |
339 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
1221efe9 | 340 | $formatError = _civicrm_api3_deprecated_formatted_param($paramValues, $formatted, TRUE, $onDuplicate); |
6a488035 TO |
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 | } | |
a05662ef | 350 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
351 | } |
352 | ||
a05662ef | 353 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 354 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
6a488035 TO |
355 | NULL, |
356 | 'Contribution' | |
357 | ); | |
358 | } | |
359 | else { | |
360 | //fix for CRM-2219 - Update Contribution | |
a05662ef | 361 | // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE |
1221efe9 | 362 | if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) { |
6a488035 TO |
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, | |
6a488035 TO |
374 | $formatted['id'], |
375 | 'Contribution' | |
376 | ); | |
377 | //process note | |
a7488080 | 378 | if (!empty($paramValues['note'])) { |
6a488035 TO |
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 | |
1221efe9 | 399 | if (!empty($formatted['soft_credit'])) { |
6a488035 | 400 | $dupeSoftCredit = array( |
1221efe9 | 401 | 'contact_id' => $formatted['soft_credit'], |
6a488035 TO |
402 | 'contribution_id' => $ids['contribution'], |
403 | ); | |
8ef12e64 | 404 | |
1221efe9 | 405 | //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null |
91bb24a7 | 406 | $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']); |
9b873358 TO |
407 | if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) { |
408 | foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) { | |
1221efe9 | 409 | if (!empty($existingSoftCreditValues['soft_credit_id'])) { |
fae69387 | 410 | civicrm_api3('ContributionSoft', 'delete', array( |
1221efe9 | 411 | 'id' => $existingSoftCreditValues['soft_credit_id'], |
412 | 'pcp_id' => NULL, | |
fae69387 | 413 | )); |
1221efe9 | 414 | } |
415 | } | |
6a488035 TO |
416 | } |
417 | } | |
418 | ||
419 | $newContribution = CRM_Contribute_BAO_Contribution::create($formatted, $ids); | |
6a488035 TO |
420 | $this->_newContributions[] = $newContribution->id; |
421 | ||
422 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 423 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
424 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
425 | } | |
426 | ||
427 | // process pledge payment assoc w/ the contribution | |
428 | return self::processPledgePayments($formatted); | |
429 | ||
a05662ef | 430 | return CRM_Import_Parser::VALID; |
6a488035 TO |
431 | } |
432 | else { | |
433 | $labels = array( | |
434 | 'id' => 'Contribution ID', | |
435 | 'trxn_id' => 'Transaction ID', | |
436 | 'invoice_id' => 'Invoice ID', | |
437 | ); | |
438 | foreach ($dupeIds as $k => $v) { | |
439 | if ($v) { | |
440 | $errorMsg[] = "$labels[$k] $v"; | |
441 | } | |
442 | } | |
443 | $errorMsg = implode(' AND ', $errorMsg); | |
444 | array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.'); | |
a05662ef | 445 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
446 | } |
447 | } | |
448 | } | |
449 | ||
450 | if ($this->_contactIdIndex < 0) { | |
451 | // set the contact type if its not set | |
452 | if (!isset($paramValues['contact_type'])) { | |
453 | $paramValues['contact_type'] = $this->_contactType; | |
454 | } | |
455 | ||
56316747 | 456 | $error = $this->checkContactDuplicate($paramValues); |
6a488035 TO |
457 | |
458 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
459 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
460 | if (count($matchedIDs) > 1) { | |
461 | array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported'); | |
a05662ef | 462 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
463 | } |
464 | else { | |
465 | $cid = $matchedIDs[0]; | |
466 | $formatted['contact_id'] = $cid; | |
467 | ||
468 | $newContribution = civicrm_api('contribution', 'create', $formatted); | |
469 | if (civicrm_error($newContribution)) { | |
470 | if (is_array($newContribution['error_message'])) { | |
471 | array_unshift($values, $newContribution['error_message']['message']); | |
472 | if ($newContribution['error_message']['params'][0]) { | |
a05662ef | 473 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
474 | } |
475 | } | |
476 | else { | |
477 | array_unshift($values, $newContribution['error_message']); | |
a05662ef | 478 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
479 | } |
480 | } | |
481 | ||
482 | $this->_newContributions[] = $newContribution['id']; | |
483 | $formatted['contribution_id'] = $newContribution['id']; | |
484 | ||
485 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 486 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
487 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
488 | } | |
489 | ||
490 | // process pledge payment assoc w/ the contribution | |
491 | return self::processPledgePayments($formatted); | |
492 | ||
a05662ef | 493 | return CRM_Import_Parser::VALID; |
6a488035 TO |
494 | } |
495 | } | |
496 | else { | |
497 | // Using new Dedupe rule. | |
498 | $ruleParams = array( | |
499 | 'contact_type' => $this->_contactType, | |
353ffa53 | 500 | 'used' => 'Unsupervised', |
6a488035 TO |
501 | ); |
502 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
1221efe9 | 503 | $disp = NULL; |
6a488035 TO |
504 | foreach ($fieldsArray as $value) { |
505 | if (array_key_exists(trim($value), $params)) { | |
506 | $paramValue = $params[trim($value)]; | |
507 | if (is_array($paramValue)) { | |
508 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
509 | } | |
510 | else { | |
511 | $disp .= $params[trim($value)] . " "; | |
512 | } | |
513 | } | |
514 | } | |
515 | ||
a7488080 | 516 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
517 | if ($disp) { |
518 | $disp .= "AND {$params['external_identifier']}"; | |
519 | } | |
520 | else { | |
521 | $disp = $params['external_identifier']; | |
522 | } | |
523 | } | |
524 | ||
525 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 526 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
527 | } |
528 | } | |
529 | else { | |
a7488080 | 530 | if (!empty($paramValues['external_identifier'])) { |
6a488035 TO |
531 | $checkCid = new CRM_Contact_DAO_Contact(); |
532 | $checkCid->external_identifier = $paramValues['external_identifier']; | |
533 | $checkCid->find(TRUE); | |
534 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 535 | array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 536 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
537 | } |
538 | } | |
539 | $newContribution = civicrm_api('contribution', 'create', $formatted); | |
540 | if (civicrm_error($newContribution)) { | |
541 | if (is_array($newContribution['error_message'])) { | |
542 | array_unshift($values, $newContribution['error_message']['message']); | |
543 | if ($newContribution['error_message']['params'][0]) { | |
a05662ef | 544 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
545 | } |
546 | } | |
547 | else { | |
548 | array_unshift($values, $newContribution['error_message']); | |
a05662ef | 549 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
550 | } |
551 | } | |
552 | ||
553 | $this->_newContributions[] = $newContribution['id']; | |
554 | $formatted['contribution_id'] = $newContribution['id']; | |
555 | ||
556 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 557 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
558 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
559 | } | |
560 | ||
561 | // process pledge payment assoc w/ the contribution | |
562 | return self::processPledgePayments($formatted); | |
563 | ||
a05662ef | 564 | return CRM_Import_Parser::VALID; |
6a488035 TO |
565 | } |
566 | } | |
567 | ||
568 | /** | |
74ab7ba8 EM |
569 | * Process pledge payments. |
570 | * | |
571 | * @param array $formatted | |
572 | * | |
573 | * @return int | |
6a488035 | 574 | */ |
00be9182 | 575 | public function processPledgePayments(&$formatted) { |
8cc574cf | 576 | if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) { |
6a488035 TO |
577 | //get completed status |
578 | $completeStatusID = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); | |
579 | ||
580 | //need to update payment record to map contribution_id | |
581 | CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'], | |
582 | 'contribution_id', $formatted['contribution_id'] | |
583 | ); | |
584 | ||
585 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($formatted['pledge_id'], | |
586 | array($formatted['pledge_payment_id']), | |
587 | $completeStatusID, | |
588 | NULL, | |
589 | $formatted['total_amount'] | |
590 | ); | |
591 | ||
592 | return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT; | |
593 | } | |
594 | } | |
595 | ||
596 | /** | |
ceb10dc7 | 597 | * Get the array of successfully imported contribution id's |
6a488035 TO |
598 | * |
599 | * @return array | |
6a488035 | 600 | */ |
00be9182 | 601 | public function &getImportedContributions() { |
6a488035 TO |
602 | return $this->_newContributions; |
603 | } | |
604 | ||
605 | /** | |
347e061b | 606 | * The initializer code, called before the processing. |
6a488035 | 607 | */ |
6ea503d4 TO |
608 | public function fini() { |
609 | } | |
96025800 | 610 | |
6a488035 | 611 | } |