Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 | 50 | * |
1330f57a | 51 | * @var array |
6a488035 TO |
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, | |
be2fb01f CW |
77 | [ |
78 | 'soft_credit' => [ | |
91bb24a7 | 79 | 'title' => ts('Soft Credit'), |
80 | 'softCredit' => TRUE, | |
81 | 'headerPattern' => '/Soft Credit/i', | |
be2fb01f CW |
82 | ], |
83 | ] | |
6a488035 TO |
84 | ); |
85 | ||
86 | // add pledge fields only if its is enabled | |
87 | if (CRM_Core_Permission::access('CiviPledge')) { | |
be2fb01f CW |
88 | $pledgeFields = [ |
89 | 'pledge_payment' => [ | |
91bb24a7 | 90 | 'title' => ts('Pledge Payment'), |
6a488035 | 91 | 'headerPattern' => '/Pledge Payment/i', |
be2fb01f CW |
92 | ], |
93 | 'pledge_id' => [ | |
91bb24a7 | 94 | 'title' => ts('Pledge ID'), |
6a488035 | 95 | 'headerPattern' => '/Pledge ID/i', |
be2fb01f CW |
96 | ], |
97 | ]; | |
6a488035 TO |
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 | ||
be2fb01f | 108 | $this->_newContributions = []; |
6a488035 TO |
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 | |
341c643b | 180 | $errorMessage = implode('; ', $this->formatDateFields($params)); |
6a488035 TO |
181 | //date-Format part ends |
182 | ||
183 | $params['contact_type'] = 'Contribution'; | |
184 | ||
185 | //checking error in custom data | |
719a6fec | 186 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
187 | |
188 | if ($errorMessage) { | |
189 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
190 | array_unshift($values, $tempMsg); | |
191 | $errorMessage = NULL; | |
a05662ef | 192 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
193 | } |
194 | ||
a05662ef | 195 | return CRM_Import_Parser::VALID; |
6a488035 TO |
196 | } |
197 | ||
198 | /** | |
fe482240 | 199 | * Handle the values in import mode. |
6a488035 | 200 | * |
014c4014 TO |
201 | * @param int $onDuplicate |
202 | * The code for what action to take on duplicates. | |
203 | * @param array $values | |
204 | * The array of values belonging to this line. | |
6a488035 | 205 | * |
acb1052e | 206 | * @return bool |
a6c01b45 | 207 | * the result of this processing |
6a488035 | 208 | */ |
00be9182 | 209 | public function import($onDuplicate, &$values) { |
6a488035 TO |
210 | // first make sure this is a valid line |
211 | $response = $this->summary($values); | |
a05662ef | 212 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
213 | return $response; |
214 | } | |
215 | ||
216 | $params = &$this->getActiveFieldParams(); | |
9d8db541 | 217 | $formatted = ['version' => 3, 'skipRecentView' => TRUE, 'skipCleanMoney' => FALSE]; |
6a488035 | 218 | |
c2e1f9ef | 219 | //CRM-10994 |
220 | if (isset($params['total_amount']) && $params['total_amount'] == 0) { | |
1ba834a8 | 221 | $params['total_amount'] = '0.00'; |
c2e1f9ef | 222 | } |
1004b689 | 223 | $this->formatInput($params); |
6a488035 TO |
224 | |
225 | static $indieFields = NULL; | |
226 | if ($indieFields == NULL) { | |
227 | $tempIndieFields = CRM_Contribute_DAO_Contribution::import(); | |
228 | $indieFields = $tempIndieFields; | |
229 | } | |
230 | ||
be2fb01f | 231 | $paramValues = []; |
6a488035 TO |
232 | foreach ($params as $key => $field) { |
233 | if ($field == NULL || $field === '') { | |
234 | continue; | |
235 | } | |
236 | $paramValues[$key] = $field; | |
237 | } | |
238 | ||
239 | //import contribution record according to select contact type | |
a05662ef | 240 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP && |
8cc574cf | 241 | (!empty($paramValues['contribution_contact_id']) || !empty($paramValues['external_identifier'])) |
6a488035 TO |
242 | ) { |
243 | $paramValues['contact_type'] = $this->_contactType; | |
244 | } | |
a05662ef | 245 | elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && |
1221efe9 | 246 | (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id'])) |
6a488035 TO |
247 | ) { |
248 | $paramValues['contact_type'] = $this->_contactType; | |
249 | } | |
250 | elseif (!empty($params['soft_credit'])) { | |
251 | $paramValues['contact_type'] = $this->_contactType; | |
252 | } | |
a7488080 | 253 | elseif (!empty($paramValues['pledge_payment'])) { |
6a488035 TO |
254 | $paramValues['contact_type'] = $this->_contactType; |
255 | } | |
256 | ||
257 | //need to pass $onDuplicate to check import mode. | |
a7488080 | 258 | if (!empty($paramValues['pledge_payment'])) { |
6a488035 TO |
259 | $paramValues['onDuplicate'] = $onDuplicate; |
260 | } | |
4fc2ce46 | 261 | $formatError = $this->deprecatedFormatParams($paramValues, $formatted, TRUE, $onDuplicate); |
6a488035 TO |
262 | |
263 | if ($formatError) { | |
264 | array_unshift($values, $formatError['error_message']); | |
265 | if (CRM_Utils_Array::value('error_data', $formatError) == 'soft_credit') { | |
266 | return CRM_Contribute_Import_Parser::SOFT_CREDIT_ERROR; | |
267 | } | |
268 | elseif (CRM_Utils_Array::value('error_data', $formatError) == 'pledge_payment') { | |
269 | return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT_ERROR; | |
270 | } | |
a05662ef | 271 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
272 | } |
273 | ||
a05662ef | 274 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 | 275 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, |
6a488035 TO |
276 | NULL, |
277 | 'Contribution' | |
278 | ); | |
279 | } | |
280 | else { | |
281 | //fix for CRM-2219 - Update Contribution | |
a05662ef | 282 | // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE |
1221efe9 | 283 | if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) { |
be2fb01f | 284 | $dupeIds = [ |
6a488035 TO |
285 | 'id' => CRM_Utils_Array::value('contribution_id', $paramValues), |
286 | 'trxn_id' => CRM_Utils_Array::value('trxn_id', $paramValues), | |
287 | 'invoice_id' => CRM_Utils_Array::value('invoice_id', $paramValues), | |
be2fb01f | 288 | ]; |
6a488035 TO |
289 | |
290 | $ids['contribution'] = CRM_Contribute_BAO_Contribution::checkDuplicateIds($dupeIds); | |
291 | ||
292 | if ($ids['contribution']) { | |
293 | $formatted['id'] = $ids['contribution']; | |
294 | $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, | |
6a488035 TO |
295 | $formatted['id'], |
296 | 'Contribution' | |
297 | ); | |
298 | //process note | |
a7488080 | 299 | if (!empty($paramValues['note'])) { |
be2fb01f | 300 | $noteID = []; |
6a488035 TO |
301 | $contactID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'contact_id'); |
302 | $daoNote = new CRM_Core_BAO_Note(); | |
303 | $daoNote->entity_table = 'civicrm_contribution'; | |
304 | $daoNote->entity_id = $ids['contribution']; | |
305 | if ($daoNote->find(TRUE)) { | |
306 | $noteID['id'] = $daoNote->id; | |
307 | } | |
308 | ||
be2fb01f | 309 | $noteParams = [ |
6a488035 TO |
310 | 'entity_table' => 'civicrm_contribution', |
311 | 'note' => $paramValues['note'], | |
312 | 'entity_id' => $ids['contribution'], | |
313 | 'contact_id' => $contactID, | |
be2fb01f | 314 | ]; |
6a488035 TO |
315 | CRM_Core_BAO_Note::add($noteParams, $noteID); |
316 | unset($formatted['note']); | |
317 | } | |
318 | ||
319 | //need to check existing soft credit contribution, CRM-3968 | |
1221efe9 | 320 | if (!empty($formatted['soft_credit'])) { |
be2fb01f | 321 | $dupeSoftCredit = [ |
1221efe9 | 322 | 'contact_id' => $formatted['soft_credit'], |
6a488035 | 323 | 'contribution_id' => $ids['contribution'], |
be2fb01f | 324 | ]; |
8ef12e64 | 325 | |
1221efe9 | 326 | //Delete all existing soft Contribution from contribution_soft table for pcp_id is_null |
91bb24a7 | 327 | $existingSoftCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($dupeSoftCredit['contribution_id']); |
9b873358 TO |
328 | if (isset($existingSoftCredit['soft_credit']) && !empty($existingSoftCredit['soft_credit'])) { |
329 | foreach ($existingSoftCredit['soft_credit'] as $key => $existingSoftCreditValues) { | |
1221efe9 | 330 | if (!empty($existingSoftCreditValues['soft_credit_id'])) { |
be2fb01f | 331 | civicrm_api3('ContributionSoft', 'delete', [ |
1221efe9 | 332 | 'id' => $existingSoftCreditValues['soft_credit_id'], |
333 | 'pcp_id' => NULL, | |
be2fb01f | 334 | ]); |
1221efe9 | 335 | } |
336 | } | |
6a488035 TO |
337 | } |
338 | } | |
339 | ||
340 | $newContribution = CRM_Contribute_BAO_Contribution::create($formatted, $ids); | |
6a488035 TO |
341 | $this->_newContributions[] = $newContribution->id; |
342 | ||
343 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 344 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
345 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
346 | } | |
347 | ||
348 | // process pledge payment assoc w/ the contribution | |
349 | return self::processPledgePayments($formatted); | |
350 | ||
a05662ef | 351 | return CRM_Import_Parser::VALID; |
6a488035 TO |
352 | } |
353 | else { | |
be2fb01f | 354 | $labels = [ |
6a488035 TO |
355 | 'id' => 'Contribution ID', |
356 | 'trxn_id' => 'Transaction ID', | |
357 | 'invoice_id' => 'Invoice ID', | |
be2fb01f | 358 | ]; |
6a488035 TO |
359 | foreach ($dupeIds as $k => $v) { |
360 | if ($v) { | |
361 | $errorMsg[] = "$labels[$k] $v"; | |
362 | } | |
363 | } | |
364 | $errorMsg = implode(' AND ', $errorMsg); | |
365 | array_unshift($values, 'Matching Contribution record not found for ' . $errorMsg . '. Row was skipped.'); | |
a05662ef | 366 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
367 | } |
368 | } | |
369 | } | |
370 | ||
371 | if ($this->_contactIdIndex < 0) { | |
372 | // set the contact type if its not set | |
373 | if (!isset($paramValues['contact_type'])) { | |
374 | $paramValues['contact_type'] = $this->_contactType; | |
375 | } | |
376 | ||
56316747 | 377 | $error = $this->checkContactDuplicate($paramValues); |
6a488035 TO |
378 | |
379 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
380 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
381 | if (count($matchedIDs) > 1) { | |
382 | array_unshift($values, 'Multiple matching contact records detected for this row. The contribution was not imported'); | |
a05662ef | 383 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
384 | } |
385 | else { | |
386 | $cid = $matchedIDs[0]; | |
387 | $formatted['contact_id'] = $cid; | |
388 | ||
389 | $newContribution = civicrm_api('contribution', 'create', $formatted); | |
390 | if (civicrm_error($newContribution)) { | |
391 | if (is_array($newContribution['error_message'])) { | |
392 | array_unshift($values, $newContribution['error_message']['message']); | |
393 | if ($newContribution['error_message']['params'][0]) { | |
a05662ef | 394 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
395 | } |
396 | } | |
397 | else { | |
398 | array_unshift($values, $newContribution['error_message']); | |
a05662ef | 399 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
400 | } |
401 | } | |
402 | ||
403 | $this->_newContributions[] = $newContribution['id']; | |
404 | $formatted['contribution_id'] = $newContribution['id']; | |
405 | ||
406 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 407 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
408 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
409 | } | |
410 | ||
411 | // process pledge payment assoc w/ the contribution | |
412 | return self::processPledgePayments($formatted); | |
413 | ||
a05662ef | 414 | return CRM_Import_Parser::VALID; |
6a488035 TO |
415 | } |
416 | } | |
417 | else { | |
418 | // Using new Dedupe rule. | |
be2fb01f | 419 | $ruleParams = [ |
6a488035 | 420 | 'contact_type' => $this->_contactType, |
353ffa53 | 421 | 'used' => 'Unsupervised', |
be2fb01f | 422 | ]; |
6a488035 | 423 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
1221efe9 | 424 | $disp = NULL; |
6a488035 TO |
425 | foreach ($fieldsArray as $value) { |
426 | if (array_key_exists(trim($value), $params)) { | |
427 | $paramValue = $params[trim($value)]; | |
428 | if (is_array($paramValue)) { | |
429 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
430 | } | |
431 | else { | |
432 | $disp .= $params[trim($value)] . " "; | |
433 | } | |
434 | } | |
435 | } | |
436 | ||
a7488080 | 437 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
438 | if ($disp) { |
439 | $disp .= "AND {$params['external_identifier']}"; | |
440 | } | |
441 | else { | |
442 | $disp = $params['external_identifier']; | |
443 | } | |
444 | } | |
445 | ||
446 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 447 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
448 | } |
449 | } | |
450 | else { | |
a7488080 | 451 | if (!empty($paramValues['external_identifier'])) { |
6a488035 TO |
452 | $checkCid = new CRM_Contact_DAO_Contact(); |
453 | $checkCid->external_identifier = $paramValues['external_identifier']; | |
454 | $checkCid->find(TRUE); | |
455 | if ($checkCid->id != $formatted['contact_id']) { | |
d79be26c | 456 | array_unshift($values, 'Mismatch of External ID:' . $paramValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']); |
a05662ef | 457 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
458 | } |
459 | } | |
460 | $newContribution = civicrm_api('contribution', 'create', $formatted); | |
461 | if (civicrm_error($newContribution)) { | |
462 | if (is_array($newContribution['error_message'])) { | |
463 | array_unshift($values, $newContribution['error_message']['message']); | |
464 | if ($newContribution['error_message']['params'][0]) { | |
a05662ef | 465 | return CRM_Import_Parser::DUPLICATE; |
6a488035 TO |
466 | } |
467 | } | |
468 | else { | |
469 | array_unshift($values, $newContribution['error_message']); | |
a05662ef | 470 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
471 | } |
472 | } | |
473 | ||
474 | $this->_newContributions[] = $newContribution['id']; | |
475 | $formatted['contribution_id'] = $newContribution['id']; | |
476 | ||
477 | //return soft valid since we need to show how soft credits were added | |
1221efe9 | 478 | if (!empty($formatted['soft_credit'])) { |
6a488035 TO |
479 | return CRM_Contribute_Import_Parser::SOFT_CREDIT; |
480 | } | |
481 | ||
482 | // process pledge payment assoc w/ the contribution | |
483 | return self::processPledgePayments($formatted); | |
484 | ||
a05662ef | 485 | return CRM_Import_Parser::VALID; |
6a488035 TO |
486 | } |
487 | } | |
488 | ||
489 | /** | |
74ab7ba8 EM |
490 | * Process pledge payments. |
491 | * | |
492 | * @param array $formatted | |
493 | * | |
494 | * @return int | |
6a488035 | 495 | */ |
00be9182 | 496 | public function processPledgePayments(&$formatted) { |
8cc574cf | 497 | if (!empty($formatted['pledge_payment_id']) && !empty($formatted['pledge_id'])) { |
6a488035 | 498 | //get completed status |
593dbb07 | 499 | $completeStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
6a488035 TO |
500 | |
501 | //need to update payment record to map contribution_id | |
502 | CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $formatted['pledge_payment_id'], | |
503 | 'contribution_id', $formatted['contribution_id'] | |
504 | ); | |
505 | ||
506 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($formatted['pledge_id'], | |
be2fb01f | 507 | [$formatted['pledge_payment_id']], |
6a488035 TO |
508 | $completeStatusID, |
509 | NULL, | |
510 | $formatted['total_amount'] | |
511 | ); | |
512 | ||
513 | return CRM_Contribute_Import_Parser::PLEDGE_PAYMENT; | |
514 | } | |
515 | } | |
516 | ||
517 | /** | |
ceb10dc7 | 518 | * Get the array of successfully imported contribution id's |
6a488035 TO |
519 | * |
520 | * @return array | |
6a488035 | 521 | */ |
00be9182 | 522 | public function &getImportedContributions() { |
6a488035 TO |
523 | return $this->_newContributions; |
524 | } | |
525 | ||
526 | /** | |
347e061b | 527 | * The initializer code, called before the processing. |
6a488035 | 528 | */ |
6ea503d4 TO |
529 | public function fini() { |
530 | } | |
96025800 | 531 | |
1004b689 | 532 | /** |
533 | * Format date fields from input to mysql. | |
534 | * | |
535 | * @param array $params | |
536 | * | |
537 | * @return array | |
538 | * Error messages, if any. | |
539 | */ | |
540 | public function formatDateFields(&$params) { | |
341c643b | 541 | $errorMessage = []; |
1004b689 | 542 | $dateType = CRM_Core_Session::singleton()->get('dateTypes'); |
543 | foreach ($params as $key => $val) { | |
544 | if ($val) { | |
545 | switch ($key) { | |
546 | case 'receive_date': | |
547 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
548 | $params[$key] = $dateValue; | |
549 | } | |
550 | else { | |
341c643b | 551 | $errorMessage[] = ts('Receive Date'); |
1004b689 | 552 | } |
553 | break; | |
554 | ||
555 | case 'cancel_date': | |
556 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
557 | $params[$key] = $dateValue; | |
558 | } | |
559 | else { | |
341c643b | 560 | $errorMessage[] = ts('Cancel Date'); |
1004b689 | 561 | } |
562 | break; | |
563 | ||
564 | case 'receipt_date': | |
565 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
566 | $params[$key] = $dateValue; | |
567 | } | |
568 | else { | |
341c643b | 569 | $errorMessage[] = ts('Receipt date'); |
1004b689 | 570 | } |
571 | break; | |
572 | ||
573 | case 'thankyou_date': | |
574 | if ($dateValue = CRM_Utils_Date::formatDate($params[$key], $dateType)) { | |
575 | $params[$key] = $dateValue; | |
576 | } | |
577 | else { | |
341c643b | 578 | $errorMessage[] = ts('Thankyou Date'); |
1004b689 | 579 | } |
580 | break; | |
581 | } | |
582 | } | |
583 | } | |
584 | return $errorMessage; | |
585 | } | |
586 | ||
587 | /** | |
588 | * Format input params to suit api handling. | |
589 | * | |
4fc2ce46 | 590 | * Over time all the parts of deprecatedFormatParams |
1004b689 | 591 | * and all the parts of the import function on this class that relate to |
592 | * reformatting input should be moved here and tests should be added in | |
593 | * CRM_Contribute_Import_Parser_ContributionTest. | |
594 | * | |
595 | * @param array $params | |
596 | */ | |
597 | public function formatInput(&$params) { | |
598 | $dateType = CRM_Core_Session::singleton()->get('dateTypes'); | |
599 | $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Contribution'; | |
600 | $customFields = CRM_Core_BAO_CustomField::getFields($customDataType); | |
601 | // @todo call formatDateFields & move custom data handling there. | |
4fc2ce46 | 602 | // Also note error handling for dates is currently in deprecatedFormatParams |
1004b689 | 603 | // we should use the error handling in formatDateFields. |
604 | foreach ($params as $key => $val) { | |
605 | // @todo - call formatDateFields instead. | |
606 | if ($val) { | |
607 | switch ($key) { | |
608 | case 'receive_date': | |
609 | case 'cancel_date': | |
610 | case 'receipt_date': | |
611 | case 'thankyou_date': | |
612 | $params[$key] = CRM_Utils_Date::formatDate($params[$key], $dateType); | |
613 | break; | |
614 | ||
615 | case 'pledge_payment': | |
616 | $params[$key] = CRM_Utils_String::strtobool($val); | |
617 | break; | |
618 | ||
619 | } | |
620 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
621 | if ($customFields[$customFieldID]['data_type'] == 'Date') { | |
622 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $params, $dateType, $key); | |
623 | unset($params[$key]); | |
624 | } | |
625 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
626 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
627 | } | |
628 | } | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
4fc2ce46 | 633 | /** |
634 | * take the input parameter list as specified in the data model and | |
635 | * convert it into the same format that we use in QF and BAO object | |
636 | * | |
637 | * @param array $params | |
638 | * Associative array of property name/value. | |
639 | * pairs to insert in new contact. | |
640 | * @param array $values | |
641 | * The reformatted properties that we can use internally. | |
642 | * ' | |
643 | * | |
644 | * @param bool $create | |
645 | * @param null $onDuplicate | |
646 | * | |
647 | * @return array|CRM_Error | |
648 | */ | |
649 | private function deprecatedFormatParams($params, &$values, $create = FALSE, $onDuplicate = NULL) { | |
650 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
651 | // copy all the contribution fields as is | |
652 | require_once 'api/v3/utils.php'; | |
653 | $fields = CRM_Contribute_DAO_Contribution::fields(); | |
654 | ||
655 | _civicrm_api3_store_values($fields, $params, $values); | |
656 | ||
657 | require_once 'CRM/Core/OptionGroup.php'; | |
658 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
659 | ||
660 | foreach ($params as $key => $value) { | |
661 | // ignore empty values or empty arrays etc | |
662 | if (CRM_Utils_System::isNull($value)) { | |
663 | continue; | |
664 | } | |
665 | ||
666 | // Handling Custom Data | |
667 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
668 | $values[$key] = $value; | |
669 | $type = $customFields[$customFieldID]['html_type']; | |
670 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
671 | $mulValues = explode(',', $value); | |
672 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
673 | $values[$key] = []; | |
674 | foreach ($mulValues as $v1) { | |
675 | foreach ($customOption as $customValueID => $customLabel) { | |
676 | $customValue = $customLabel['value']; | |
677 | if ((strtolower($customLabel['label']) == strtolower(trim($v1))) || | |
678 | (strtolower($customValue) == strtolower(trim($v1))) | |
679 | ) { | |
680 | if ($type == 'CheckBox') { | |
681 | $values[$key][$customValue] = 1; | |
682 | } | |
683 | else { | |
684 | $values[$key][] = $customValue; | |
685 | } | |
686 | } | |
687 | } | |
688 | } | |
689 | } | |
690 | elseif ($type == 'Select' || $type == 'Radio' || | |
691 | ($type == 'Autocomplete-Select' && | |
692 | $customFields[$customFieldID]['data_type'] == 'String' | |
693 | ) | |
694 | ) { | |
695 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
696 | foreach ($customOption as $customFldID => $customValue) { | |
697 | $val = CRM_Utils_Array::value('value', $customValue); | |
698 | $label = CRM_Utils_Array::value('label', $customValue); | |
699 | $label = strtolower($label); | |
700 | $value = strtolower(trim($value)); | |
701 | if (($value == $label) || ($value == strtolower($val))) { | |
702 | $values[$key] = $val; | |
703 | } | |
704 | } | |
705 | } | |
706 | } | |
707 | ||
708 | switch ($key) { | |
709 | case 'contribution_contact_id': | |
710 | if (!CRM_Utils_Rule::integer($value)) { | |
711 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
712 | } | |
713 | $dao = new CRM_Core_DAO(); | |
714 | $qParams = []; | |
715 | $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value", | |
716 | $qParams | |
717 | ); | |
718 | if (!isset($svq)) { | |
719 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
720 | } | |
721 | elseif ($svq == 1) { | |
722 | return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact."); | |
723 | } | |
724 | ||
725 | $values['contact_id'] = $values['contribution_contact_id']; | |
726 | unset($values['contribution_contact_id']); | |
727 | break; | |
728 | ||
729 | case 'contact_type': | |
730 | // import contribution record according to select contact type | |
731 | require_once 'CRM/Contact/DAO/Contact.php'; | |
732 | $contactType = new CRM_Contact_DAO_Contact(); | |
733 | $contactId = CRM_Utils_Array::value('contribution_contact_id', $params); | |
734 | $externalId = CRM_Utils_Array::value('external_identifier', $params); | |
735 | $email = CRM_Utils_Array::value('email', $params); | |
736 | //when insert mode check contact id or external identifier | |
737 | if ($contactId || $externalId) { | |
738 | $contactType->id = $contactId; | |
739 | $contactType->external_identifier = $externalId; | |
740 | if ($contactType->find(TRUE)) { | |
741 | if ($params['contact_type'] != $contactType->contact_type) { | |
742 | return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type"); | |
743 | } | |
744 | } | |
745 | } | |
746 | elseif ($email) { | |
747 | if (!CRM_Utils_Rule::email($email)) { | |
748 | return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped"); | |
749 | } | |
750 | ||
751 | // get the contact id from duplicate contact rule, if more than one contact is returned | |
752 | // we should return error, since current interface allows only one-one mapping | |
753 | $emailParams = ['email' => $email, 'contact_type' => $params['contact_type']]; | |
754 | $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams); | |
755 | if (!$checkDedupe['is_error']) { | |
756 | return civicrm_api3_create_error("Invalid email address(doesn't exist) $email. Row was skipped"); | |
757 | } | |
758 | else { | |
759 | $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]); | |
760 | if (count($matchingContactIds) > 1) { | |
761 | return civicrm_api3_create_error("Invalid email address(duplicate) $email. Row was skipped"); | |
762 | } | |
763 | elseif (count($matchingContactIds) == 1) { | |
764 | $params['contribution_contact_id'] = $matchingContactIds[0]; | |
765 | } | |
766 | } | |
767 | } | |
768 | elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) { | |
769 | // when update mode check contribution id or trxn id or | |
770 | // invoice id | |
771 | $contactId = new CRM_Contribute_DAO_Contribution(); | |
772 | if (!empty($params['contribution_id'])) { | |
773 | $contactId->id = $params['contribution_id']; | |
774 | } | |
775 | elseif (!empty($params['trxn_id'])) { | |
776 | $contactId->trxn_id = $params['trxn_id']; | |
777 | } | |
778 | elseif (!empty($params['invoice_id'])) { | |
779 | $contactId->invoice_id = $params['invoice_id']; | |
780 | } | |
781 | if ($contactId->find(TRUE)) { | |
782 | $contactType->id = $contactId->contact_id; | |
783 | if ($contactType->find(TRUE)) { | |
784 | if ($params['contact_type'] != $contactType->contact_type) { | |
785 | return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type"); | |
786 | } | |
787 | } | |
788 | } | |
789 | } | |
790 | else { | |
791 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { | |
792 | return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped."); | |
793 | } | |
794 | } | |
795 | break; | |
796 | ||
797 | case 'receive_date': | |
798 | case 'cancel_date': | |
799 | case 'receipt_date': | |
800 | case 'thankyou_date': | |
801 | if (!CRM_Utils_Rule::dateTime($value)) { | |
802 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
803 | } | |
804 | break; | |
805 | ||
806 | case 'non_deductible_amount': | |
807 | case 'total_amount': | |
808 | case 'fee_amount': | |
809 | case 'net_amount': | |
810 | if (!CRM_Utils_Rule::money($value)) { | |
811 | return civicrm_api3_create_error("$key not a valid amount: $value"); | |
812 | } | |
813 | break; | |
814 | ||
815 | case 'currency': | |
816 | if (!CRM_Utils_Rule::currencyCode($value)) { | |
817 | return civicrm_api3_create_error("currency not a valid code: $value"); | |
818 | } | |
819 | break; | |
820 | ||
821 | case 'financial_type': | |
822 | require_once 'CRM/Contribute/PseudoConstant.php'; | |
823 | $contriTypes = CRM_Contribute_PseudoConstant::financialType(); | |
824 | foreach ($contriTypes as $val => $type) { | |
825 | if (strtolower($value) == strtolower($type)) { | |
826 | $values['financial_type_id'] = $val; | |
827 | break; | |
828 | } | |
829 | } | |
830 | if (empty($values['financial_type_id'])) { | |
831 | return civicrm_api3_create_error("Financial Type is not valid: $value"); | |
832 | } | |
833 | break; | |
834 | ||
835 | case 'payment_instrument': | |
836 | require_once 'CRM/Core/PseudoConstant.php'; | |
837 | $values['payment_instrument_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $value); | |
838 | if (empty($values['payment_instrument_id'])) { | |
839 | return civicrm_api3_create_error("Payment Instrument is not valid: $value"); | |
840 | } | |
841 | break; | |
842 | ||
843 | case 'contribution_status_id': | |
844 | require_once 'CRM/Core/PseudoConstant.php'; | |
845 | if (!$values['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $value)) { | |
846 | return civicrm_api3_create_error("Contribution Status is not valid: $value"); | |
847 | } | |
848 | break; | |
849 | ||
850 | case 'soft_credit': | |
851 | // import contribution record according to select contact type | |
852 | // validate contact id and external identifier. | |
853 | $value[$key] = $mismatchContactType = $softCreditContactIds = ''; | |
854 | if (isset($params[$key]) && is_array($params[$key])) { | |
855 | foreach ($params[$key] as $softKey => $softParam) { | |
856 | $contactId = CRM_Utils_Array::value('contact_id', $softParam); | |
857 | $externalId = CRM_Utils_Array::value('external_identifier', $softParam); | |
858 | $email = CRM_Utils_Array::value('email', $softParam); | |
859 | if ($contactId || $externalId) { | |
860 | require_once 'CRM/Contact/DAO/Contact.php'; | |
861 | $contact = new CRM_Contact_DAO_Contact(); | |
862 | $contact->id = $contactId; | |
863 | $contact->external_identifier = $externalId; | |
864 | $errorMsg = NULL; | |
865 | if (!$contact->find(TRUE)) { | |
866 | $field = $contactId ? ts('Contact ID') : ts('External ID'); | |
867 | $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.", | |
868 | [1 => $field, 2 => $contactId ? $contactId : $externalId]); | |
869 | } | |
870 | ||
871 | if ($errorMsg) { | |
872 | return civicrm_api3_create_error($errorMsg); | |
873 | } | |
874 | ||
875 | // finally get soft credit contact id. | |
876 | $values[$key][$softKey] = $softParam; | |
877 | $values[$key][$softKey]['contact_id'] = $contact->id; | |
878 | } | |
879 | elseif ($email) { | |
880 | if (!CRM_Utils_Rule::email($email)) { | |
881 | return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped"); | |
882 | } | |
883 | ||
884 | // get the contact id from duplicate contact rule, if more than one contact is returned | |
885 | // we should return error, since current interface allows only one-one mapping | |
886 | $emailParams = ['email' => $email, 'contact_type' => $params['contact_type']]; | |
887 | $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams); | |
888 | if (!$checkDedupe['is_error']) { | |
889 | return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped"); | |
890 | } | |
891 | else { | |
892 | $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]); | |
893 | if (count($matchingContactIds) > 1) { | |
894 | return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped"); | |
895 | } | |
896 | elseif (count($matchingContactIds) == 1) { | |
897 | $contactId = $matchingContactIds[0]; | |
898 | unset($softParam['email']); | |
899 | $values[$key][$softKey] = $softParam + ['contact_id' => $contactId]; | |
900 | } | |
901 | } | |
902 | } | |
903 | } | |
904 | } | |
905 | break; | |
906 | ||
907 | case 'pledge_payment': | |
908 | case 'pledge_id': | |
909 | ||
910 | // giving respect to pledge_payment flag. | |
911 | if (empty($params['pledge_payment'])) { | |
912 | continue; | |
913 | } | |
914 | ||
915 | // get total amount of from import fields | |
916 | $totalAmount = CRM_Utils_Array::value('total_amount', $params); | |
917 | ||
918 | $onDuplicate = CRM_Utils_Array::value('onDuplicate', $params); | |
919 | ||
920 | // we need to get contact id $contributionContactID to | |
921 | // retrieve pledge details as well as to validate pledge ID | |
922 | ||
923 | // first need to check for update mode | |
924 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && | |
925 | ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id']) | |
926 | ) { | |
927 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
928 | if ($params['contribution_id']) { | |
929 | $contribution->id = $params['contribution_id']; | |
930 | } | |
931 | elseif ($params['trxn_id']) { | |
932 | $contribution->trxn_id = $params['trxn_id']; | |
933 | } | |
934 | elseif ($params['invoice_id']) { | |
935 | $contribution->invoice_id = $params['invoice_id']; | |
936 | } | |
937 | ||
938 | if ($contribution->find(TRUE)) { | |
939 | $contributionContactID = $contribution->contact_id; | |
940 | if (!$totalAmount) { | |
941 | $totalAmount = $contribution->total_amount; | |
942 | } | |
943 | } | |
944 | else { | |
945 | return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.'); | |
946 | } | |
947 | } | |
948 | else { | |
949 | // first get the contact id for given contribution record. | |
950 | if (!empty($params['contribution_contact_id'])) { | |
951 | $contributionContactID = $params['contribution_contact_id']; | |
952 | } | |
953 | elseif (!empty($params['external_identifier'])) { | |
954 | require_once 'CRM/Contact/DAO/Contact.php'; | |
955 | $contact = new CRM_Contact_DAO_Contact(); | |
956 | $contact->external_identifier = $params['external_identifier']; | |
957 | if ($contact->find(TRUE)) { | |
958 | $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id; | |
959 | } | |
960 | else { | |
961 | return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.'); | |
962 | } | |
963 | } | |
964 | else { | |
965 | // we need to get contribution contact using de dupe | |
966 | $error = _civicrm_api3_deprecated_check_contact_dedupe($params); | |
967 | ||
968 | if (isset($error['error_message']['params'][0])) { | |
969 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
970 | ||
971 | // check if only one contact is found | |
972 | if (count($matchedIDs) > 1) { | |
973 | return civicrm_api3_create_error($error['error_message']['message']); | |
974 | } | |
975 | else { | |
976 | $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0]; | |
977 | } | |
978 | } | |
979 | else { | |
980 | return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.'); | |
981 | } | |
982 | } | |
983 | } | |
984 | ||
985 | if (!empty($params['pledge_id'])) { | |
986 | if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) { | |
987 | return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.'); | |
988 | } | |
989 | $values['pledge_id'] = $params['pledge_id']; | |
990 | } | |
991 | else { | |
992 | // check if there are any pledge related to this contact, with payments pending or in progress | |
993 | require_once 'CRM/Pledge/BAO/Pledge.php'; | |
994 | $pledgeDetails = CRM_Pledge_BAO_Pledge::getContactPledges($contributionContactID); | |
995 | ||
996 | if (empty($pledgeDetails)) { | |
997 | return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.'); | |
998 | } | |
999 | elseif (count($pledgeDetails) > 1) { | |
1000 | return civicrm_api3_create_error('This contact has more than one open pledge. Unable to determine which pledge to apply the contribution to. Contribution row was skipped.'); | |
1001 | } | |
1002 | ||
1003 | // this mean we have only one pending / in progress pledge | |
1004 | $values['pledge_id'] = $pledgeDetails[0]; | |
1005 | } | |
1006 | ||
1007 | // we need to check if oldest payment amount equal to contribution amount | |
1008 | require_once 'CRM/Pledge/BAO/PledgePayment.php'; | |
1009 | $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']); | |
1010 | ||
1011 | if ($pledgePaymentDetails['amount'] == $totalAmount) { | |
1012 | $values['pledge_payment_id'] = $pledgePaymentDetails['id']; | |
1013 | } | |
1014 | else { | |
1015 | return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.'); | |
1016 | } | |
1017 | break; | |
1018 | ||
1019 | default: | |
1020 | break; | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | if (array_key_exists('note', $params)) { | |
1025 | $values['note'] = $params['note']; | |
1026 | } | |
1027 | ||
1028 | if ($create) { | |
1029 | // CRM_Contribute_BAO_Contribution::add() handles contribution_source | |
1030 | // So, if $values contains contribution_source, convert it to source | |
1031 | $changes = ['contribution_source' => 'source']; | |
1032 | ||
1033 | foreach ($changes as $orgVal => $changeVal) { | |
1034 | if (isset($values[$orgVal])) { | |
1035 | $values[$changeVal] = $values[$orgVal]; | |
1036 | unset($values[$orgVal]); | |
1037 | } | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | return NULL; | |
1042 | } | |
1043 | ||
6a488035 | 1044 | } |