Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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(); | |
d586f6e9 | 259 | $params['contact_type'] = 'Contribution'; |
6a488035 TO |
260 | $formatted = array('version' => 3); |
261 | ||
262 | // don't add to recent items, CRM-4399 | |
263 | $formatted['skipRecentView'] = TRUE; | |
264 | ||
265 | //for date-Formats | |
266 | $session = CRM_Core_Session::singleton(); | |
267 | $dateType = $session->get('dateTypes'); | |
268 | ||
269 | $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $params)); | |
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'])) { |
410 | $deleteParams = array( | |
411 | 'id' => $existingSoftCreditValues['soft_credit_id'], | |
412 | 'pcp_id' => NULL, | |
413 | ); | |
414 | CRM_Contribute_BAO_ContributionSoft::del($deleteParams); | |
415 | } | |
416 | } | |
6a488035 TO |
417 | } |
418 | } | |
419 | ||
420 | $newContribution = CRM_Contribute_BAO_Contribution::create($formatted, $ids); | |
6a488035 TO |
421 | $this->_newContributions[] = $newContribution->id; |
422 | ||
423 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 424 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
425 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
426 | } | |
427 | ||
428 | // process pledge payment assoc w/ the contribution | |
429 | return self::processPledgePayments($formatted); | |
430 | ||
a05662ef | 431 | return CRM_Import_Parser::VALID; |
6a488035 TO |
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.'); | |
a05662ef | 446 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
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'); | |
a05662ef | 466 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
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]) { | |
a05662ef | 477 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
478 | } |
479 | } | |
480 | else { | |
481 | array_unshift($values, $newContribution['error_message']); | |
a05662ef | 482 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
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 | |
1221efe9 | 490 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
491 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
492 | } | |
493 | ||
494 | // process pledge payment assoc w/ the contribution | |
495 | return self::processPledgePayments($formatted); | |
496 | ||
a05662ef | 497 | return CRM_Import_Parser::VALID; |
6a488035 TO |
498 | } |
499 | } | |
500 | else { | |
501 | // Using new Dedupe rule. | |
502 | $ruleParams = array( | |
503 | 'contact_type' => $this->_contactType, | |
353ffa53 | 504 | 'used' => 'Unsupervised', |
6a488035 TO |
505 | ); |
506 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
1221efe9 | 507 | $disp = NULL; |
6a488035 TO |
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 | ||
a7488080 | 520 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
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 . ')'); | |
a05662ef | 530 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
531 | } |
532 | } | |
533 | else { | |
a7488080 | 534 | if (!empty($paramValues['external_identifier'])) { |
6a488035 TO |
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']) { | |
d79be26c | 539 | array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 540 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
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]) { | |
a05662ef | 548 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
549 | } |
550 | } | |
551 | else { | |
552 | array_unshift($values, $newContribution['error_message']); | |
a05662ef | 553 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
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 | |
1221efe9 | 561 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
562 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
563 | } | |
564 | ||
565 | // process pledge payment assoc w/ the contribution | |
566 | return self::processPledgePayments($formatted); | |
567 | ||
a05662ef | 568 | return CRM_Import_Parser::VALID; |
6a488035 TO |
569 | } |
570 | } | |
571 | ||
572 | /** | |
74ab7ba8 EM |
573 | * Process pledge payments. |
574 | * | |
575 | * @param array $formatted | |
576 | * | |
577 | * @return int | |
6a488035 | 578 | */ |
00be9182 | 579 | public function processPledgePayments(&$formatted) { |
8cc574cf | 580 | if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) { |
6a488035 TO |
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 | /** | |
ceb10dc7 | 601 | * Get the array of successfully imported contribution id's |
6a488035 TO |
602 | * |
603 | * @return array | |
6a488035 | 604 | */ |
00be9182 | 605 | public function &getImportedContributions() { |
6a488035 TO |
606 | return $this->_newContributions; |
607 | } | |
608 | ||
609 | /** | |
347e061b | 610 | * The initializer code, called before the processing. |
6a488035 | 611 | */ |
6ea503d4 TO |
612 | public function fini() { |
613 | } | |
96025800 | 614 | |
6a488035 | 615 | } |