Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * | |
32 | * @package CRM | |
33 | * @copyright CiviCRM LLC (c) 2004-2013 | |
34 | * $Id$ | |
35 | * | |
36 | */ | |
37 | class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { | |
38 | ||
39 | /** | |
40 | * static field for all the contribution information that we can potentially import | |
41 | * | |
42 | * @var array | |
43 | * @static | |
44 | */ | |
45 | static $_importableFields = NULL; | |
46 | ||
47 | /** | |
48 | * static field for all the contribution information that we can potentially export | |
49 | * | |
50 | * @var array | |
51 | * @static | |
52 | */ | |
53 | static $_exportableFields = NULL; | |
54 | ||
55 | /** | |
56 | * field for all the objects related to this contribution | |
57 | * @var array of objects (e.g membership object, participant object) | |
58 | */ | |
59 | public $_relatedObjects = array(); | |
60 | ||
61 | /** | |
62 | * field for the component - either 'event' (participant) or 'contribute' | |
63 | * (any item related to a contribution page e.g. membership, pledge, contribution) | |
64 | * This is used for composing messages because they have dependency on the | |
65 | * contribution_page or event page - although over time we may eliminate that | |
66 | * | |
67 | * @var string component or event | |
68 | */ | |
69 | public $_component = NULL; | |
70 | ||
71 | /* | |
72 | * construct method | |
73 | */ | |
74 | function __construct() { | |
75 | parent::__construct(); | |
76 | } | |
77 | ||
78 | /** | |
79 | * takes an associative array and creates a contribution object | |
80 | * | |
81 | * the function extract all the params it needs to initialize the create a | |
82 | * contribution object. the params array could contain additional unused name/value | |
83 | * pairs | |
84 | * | |
85 | * @param array $params (reference ) an assoc array of name/value pairs | |
86 | * @param array $ids the array that holds all the db ids | |
87 | * | |
88 | * @return object CRM_Contribute_BAO_Contribution object | |
89 | * @access public | |
90 | * @static | |
91 | */ | |
92 | static function add(&$params, &$ids) { | |
93 | if (empty($params)) { | |
94 | return; | |
95 | } | |
96 | ||
97 | $duplicates = array(); | |
98 | if (self::checkDuplicate($params, $duplicates, | |
99 | CRM_Utils_Array::value('contribution', $ids) | |
100 | )) { | |
101 | $error = CRM_Core_Error::singleton(); | |
102 | $d = implode(', ', $duplicates); | |
103 | $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, | |
104 | 'Fatal', | |
105 | array($d), | |
106 | "Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: $d" | |
107 | ); | |
108 | return $error; | |
109 | } | |
110 | ||
111 | // first clean up all the money fields | |
112 | $moneyFields = array( | |
113 | 'total_amount', | |
114 | 'net_amount', | |
115 | 'fee_amount', | |
116 | 'non_deductible_amount', | |
117 | ); | |
118 | //if priceset is used, no need to cleanup money | |
119 | if (CRM_Utils_Array::value('skipCleanMoney', $params)) { | |
120 | unset($moneyFields[0]); | |
121 | } | |
122 | ||
123 | foreach ($moneyFields as $field) { | |
124 | if (isset($params[$field])) { | |
125 | $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); | |
126 | } | |
127 | } | |
128 | ||
129 | if (CRM_Utils_Array::value('payment_instrument_id', $params)) { | |
130 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name'); | |
131 | if ($params['payment_instrument_id'] != array_search('Check', $paymentInstruments)) { | |
132 | $params['check_number'] = 'null'; | |
133 | } | |
134 | } | |
135 | ||
136 | // contribution status is missing, choose Completed as default status | |
9e12d5ee | 137 | // do this for create mode only |
138 | if (!CRM_Utils_Array::value('contribution', $ids) && !CRM_Utils_Array::value('contribution_status_id', $params)) { | |
6a488035 TO |
139 | $params['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); |
140 | } | |
141 | ||
142 | if (CRM_Utils_Array::value('contribution', $ids)) { | |
143 | CRM_Utils_Hook::pre('edit', 'Contribution', $ids['contribution'], $params); | |
144 | } | |
145 | else { | |
146 | CRM_Utils_Hook::pre('create', 'Contribution', NULL, $params); | |
147 | } | |
148 | ||
149 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
150 | $contribution->copyValues($params); | |
151 | ||
152 | $contribution->id = CRM_Utils_Array::value('contribution', $ids); | |
153 | ||
154 | if (!CRM_Utils_Rule::currencyCode($contribution->currency)) { | |
155 | $config = CRM_Core_Config::singleton(); | |
156 | $contribution->currency = $config->defaultCurrency; | |
157 | } | |
158 | ||
159 | if (CRM_Utils_Array::value('contribution', $ids)) { | |
160 | $contributionId['id'] = $ids['contribution']; | |
161 | $params['prevContribution'] = self::getValues($contributionId, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); | |
162 | if (CRM_Utils_Array::value('soft_credit_to', $params)) { | |
163 | foreach (array('financial_type_id', 'total_amount') as $field) { | |
164 | if (!isset($contribution->$field)) { | |
165 | $contribution->$field = $params['prevContribution']->$field; | |
166 | } | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | $result = $contribution->save(); | |
172 | ||
173 | // Add financial_trxn details as part of fix for CRM-4724 | |
174 | $contribution->trxn_result_code = CRM_Utils_Array::value('trxn_result_code', $params); | |
175 | $contribution->payment_processor = CRM_Utils_Array::value('payment_processor', $params); | |
176 | ||
177 | //add Account details | |
178 | $params['contribution'] = $contribution; | |
179 | self::recordFinancialAccounts($params, $ids); | |
180 | ||
181 | // Add soft_contribution details as part of fix for CRM-8908 | |
182 | $contribution->soft_credit_to = CRM_Utils_Array::value('soft_credit_to', $params); | |
183 | ||
184 | // reset the group contact cache for this group | |
185 | CRM_Contact_BAO_GroupContactCache::remove(); | |
186 | ||
187 | if (CRM_Utils_Array::value('contribution', $ids)) { | |
188 | CRM_Utils_Hook::post('edit', 'Contribution', $contribution->id, $contribution); | |
189 | } | |
190 | else { | |
191 | CRM_Utils_Hook::post('create', 'Contribution', $contribution->id, $contribution); | |
192 | } | |
193 | ||
194 | return $result; | |
195 | } | |
196 | ||
197 | /** | |
198 | * Given the list of params in the params array, fetch the object | |
199 | * and store the values in the values array | |
200 | * | |
201 | * @param array $params input parameters to find object | |
202 | * @param array $values output values of the object | |
203 | * @param array $ids the array that holds all the db ids | |
204 | * | |
205 | * @return CRM_Contribute_BAO_Contribution|null the found object or null | |
206 | * @access public | |
207 | * @static | |
208 | */ | |
209 | static function &getValues(&$params, &$values, &$ids) { | |
210 | if (empty($params)) { | |
211 | return NULL; | |
212 | } | |
213 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
214 | ||
215 | $contribution->copyValues($params); | |
216 | ||
217 | if ($contribution->find(TRUE)) { | |
218 | $ids['contribution'] = $contribution->id; | |
219 | ||
220 | CRM_Core_DAO::storeValues($contribution, $values); | |
221 | ||
222 | return $contribution; | |
223 | } | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | /** | |
228 | * takes an associative array and creates a contribution object | |
229 | * | |
230 | * @param array $params (reference ) an assoc array of name/value pairs | |
231 | * @param array $ids the array that holds all the db ids | |
232 | * | |
233 | * @return object CRM_Contribute_BAO_Contribution object | |
234 | * @access public | |
235 | * @static | |
236 | */ | |
237 | static function &create(&$params, &$ids) { | |
238 | $dateFields = array('receive_date', 'cancel_date', 'receipt_date', 'thankyou_date'); | |
239 | foreach ($dateFields as $df) { | |
240 | if (isset($params[$df])) { | |
241 | $params[$df] = CRM_Utils_Date::isoToMysql($params[$df]); | |
242 | } | |
243 | } | |
244 | ||
245 | if (CRM_Utils_Array::value('contribution', $ids) && | |
246 | !CRM_Utils_Array::value('softID', $params) | |
247 | ) { | |
248 | if ($softID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionSoft', $ids['contribution'], 'id', 'contribution_id')) { | |
249 | $params['softID'] = $softID; | |
250 | } | |
251 | } | |
252 | ||
253 | $transaction = new CRM_Core_Transaction(); | |
254 | ||
255 | // delete the soft credit record if no soft credit contact ID AND no PCP is set in the form | |
256 | if (CRM_Utils_Array::value('contribution', $ids) && | |
257 | (!CRM_Utils_Array::value('soft_credit_to', $params) && | |
258 | !CRM_Utils_Array::value('pcp_made_through_id', $params) | |
259 | ) && | |
260 | CRM_Utils_Array::value('softID', $params) | |
261 | ) { | |
262 | $softCredit = new CRM_Contribute_DAO_ContributionSoft(); | |
263 | $softCredit->id = $params['softID']; | |
264 | $softCredit->delete(); | |
265 | } | |
266 | ||
267 | $contribution = self::add($params, $ids); | |
268 | ||
269 | if (is_a($contribution, 'CRM_Core_Error')) { | |
270 | $transaction->rollback(); | |
271 | return $contribution; | |
272 | } | |
273 | ||
274 | $params['contribution_id'] = $contribution->id; | |
275 | ||
276 | if (CRM_Utils_Array::value('custom', $params) && | |
277 | is_array($params['custom']) | |
278 | ) { | |
279 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id); | |
280 | } | |
281 | ||
282 | $session = CRM_Core_Session::singleton(); | |
283 | ||
284 | if (CRM_Utils_Array::value('note', $params)) { | |
285 | ||
286 | $noteParams = array( | |
287 | 'entity_table' => 'civicrm_contribution', | |
288 | 'note' => $params['note'], | |
289 | 'entity_id' => $contribution->id, | |
290 | 'contact_id' => $session->get('userID'), | |
291 | 'modified_date' => date('Ymd'), | |
292 | ); | |
293 | if (!$noteParams['contact_id']) { | |
294 | $noteParams['contact_id'] = $params['contact_id']; | |
295 | } | |
296 | CRM_Core_BAO_Note::add($noteParams, | |
297 | CRM_Utils_Array::value('note', $ids) | |
298 | ); | |
299 | } | |
300 | ||
301 | // make entry in batch entity batch table | |
302 | if (CRM_Utils_Array::value('batch_id', $params)) { | |
303 | // in some update cases we need to get extra fields - ie an update that doesn't pass in all these params | |
304 | $titleFields = array( | |
305 | 'contact_id', | |
306 | 'total_amount', | |
307 | 'currency', | |
308 | 'financial_type_id', | |
309 | ); | |
310 | $retrieverequired = 0; | |
311 | foreach ($titleFields as $titleField) { | |
312 | if(!isset($contribution->$titleField)){ | |
313 | $retrieverequired = 1; | |
314 | break; | |
315 | } | |
316 | } | |
317 | if ($retrieverequired == 1) { | |
318 | $contribution->find(true); | |
319 | } | |
320 | } | |
321 | ||
322 | // check if activity record exist for this contribution, if | |
323 | // not add activity | |
324 | $activity = new CRM_Activity_DAO_Activity(); | |
325 | $activity->source_record_id = $contribution->id; | |
326 | $activity->activity_type_id = CRM_Core_OptionGroup::getValue('activity_type', | |
327 | 'Contribution', | |
328 | 'name' | |
329 | ); | |
330 | if (!$activity->find()) { | |
331 | CRM_Activity_BAO_Activity::addActivity($contribution, 'Offline'); | |
332 | } | |
333 | // Handle soft credit and / or link to personal campaign page | |
334 | if (CRM_Utils_Array::value('soft_credit_to', $params) || | |
335 | CRM_Utils_Array::value('pcp_made_through_id', $params) | |
336 | ) { | |
337 | $csParams = array(); | |
338 | if ($id = CRM_Utils_Array::value('softID', $params)) { | |
339 | $csParams['id'] = $params['softID']; | |
340 | } | |
341 | ||
342 | $csParams['contribution_id'] = $contribution->id; | |
343 | // If pcp_made_through_id set, we define soft_credit_to contact based on selected PCP, | |
344 | // else use passed soft_credit_to | |
345 | if (CRM_Utils_Array::value('pcp_made_through_id', $params)) { | |
346 | $csParams['pcp_display_in_roll'] = $params['pcp_display_in_roll'] ? 1 : 0; | |
347 | foreach (array( | |
348 | 'pcp_roll_nickname', 'pcp_personal_note') as $val) { | |
349 | $csParams[$val] = $params[$val]; | |
350 | } | |
351 | ||
352 | $csParams['pcp_id'] = CRM_Utils_Array::value('pcp_made_through_id', $params); | |
353 | $csParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', | |
354 | $csParams['pcp_id'], 'contact_id' | |
355 | ); | |
356 | } | |
357 | else { | |
358 | $csParams['contact_id'] = $params['soft_credit_to']; | |
359 | $csParams['pcp_id'] = ''; | |
360 | } | |
361 | ||
362 | // first stage: we register whole amount as credited to given person | |
363 | $csParams['amount'] = $contribution->total_amount; | |
364 | ||
365 | self::addSoftContribution($csParams); | |
366 | } | |
367 | ||
368 | $transaction->commit(); | |
369 | ||
370 | // do not add to recent items for import, CRM-4399 | |
371 | if (!CRM_Utils_Array::value('skipRecentView', $params)) { | |
372 | $url = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
373 | "action=view&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home" | |
374 | ); | |
375 | // in some update cases we need to get extra fields - ie an update that doesn't pass in all these params | |
376 | $titleFields = array( | |
377 | 'contact_id', | |
378 | 'total_amount', | |
379 | 'currency', | |
380 | 'financial_type_id', | |
381 | ); | |
382 | $retrieverequired = 0; | |
383 | foreach ($titleFields as $titleField) { | |
384 | if(!isset($contribution->$titleField)){ | |
385 | $retrieverequired = 1; | |
386 | break; | |
387 | } | |
388 | } | |
389 | if($retrieverequired == 1){ | |
390 | $contribution->find(true); | |
391 | } | |
392 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); | |
393 | $title = CRM_Contact_BAO_Contact::displayName($contribution->contact_id) . ' - (' . CRM_Utils_Money::format($contribution->total_amount, $contribution->currency) . ' ' . ' - ' . $contributionTypes[$contribution->financial_type_id] . ')'; | |
394 | ||
395 | $recentOther = array(); | |
396 | if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) { | |
397 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
398 | "action=update&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home" | |
399 | ); | |
400 | } | |
401 | ||
402 | if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) { | |
403 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution', | |
404 | "action=delete&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home" | |
405 | ); | |
406 | } | |
407 | ||
408 | // add the recently created Contribution | |
409 | CRM_Utils_Recent::add($title, | |
410 | $url, | |
411 | $contribution->id, | |
412 | 'Contribution', | |
413 | $contribution->contact_id, | |
414 | NULL, | |
415 | $recentOther | |
416 | ); | |
417 | } | |
418 | ||
419 | return $contribution; | |
420 | } | |
421 | ||
422 | /** | |
423 | * Get the values for pseudoconstants for name->value and reverse. | |
424 | * | |
425 | * @param array $defaults (reference) the default values, some of which need to be resolved. | |
426 | * @param boolean $reverse true if we want to resolve the values in the reverse direction (value -> name) | |
427 | * | |
428 | * @return void | |
429 | * @access public | |
430 | * @static | |
431 | */ | |
432 | static function resolveDefaults(&$defaults, $reverse = FALSE) { | |
433 | self::lookupValue($defaults, 'financial_type', CRM_Contribute_PseudoConstant::financialType(), $reverse); | |
434 | self::lookupValue($defaults, 'payment_instrument', CRM_Contribute_PseudoConstant::paymentInstrument(), $reverse); | |
435 | self::lookupValue($defaults, 'contribution_status', CRM_Contribute_PseudoConstant::contributionStatus(), $reverse); | |
436 | self::lookupValue($defaults, 'pcp', CRM_Contribute_PseudoConstant::pcPage(), $reverse); | |
437 | } | |
438 | ||
439 | /** | |
440 | * This function is used to convert associative array names to values | |
441 | * and vice-versa. | |
442 | * | |
443 | * This function is used by both the web form layer and the api. Note that | |
444 | * the api needs the name => value conversion, also the view layer typically | |
445 | * requires value => name conversion | |
446 | */ | |
447 | static function lookupValue(&$defaults, $property, &$lookup, $reverse) { | |
448 | $id = $property . '_id'; | |
449 | ||
450 | $src = $reverse ? $property : $id; | |
451 | $dst = $reverse ? $id : $property; | |
452 | ||
453 | if (!array_key_exists($src, $defaults)) { | |
454 | return FALSE; | |
455 | } | |
456 | ||
457 | $look = $reverse ? array_flip($lookup) : $lookup; | |
458 | ||
459 | if (is_array($look)) { | |
460 | if (!array_key_exists($defaults[$src], $look)) { | |
461 | return FALSE; | |
462 | } | |
463 | } | |
464 | $defaults[$dst] = $look[$defaults[$src]]; | |
465 | return TRUE; | |
466 | } | |
467 | ||
468 | /** | |
469 | * Takes a bunch of params that are needed to match certain criteria and | |
470 | * retrieves the relevant objects. We'll tweak this function to be more | |
471 | * full featured over a period of time. This is the inverse function of | |
472 | * create. It also stores all the retrieved values in the default array | |
473 | * | |
474 | * @param array $params (reference ) an assoc array of name/value pairs | |
475 | * @param array $defaults (reference ) an assoc array to hold the name / value pairs | |
476 | * in a hierarchical manner | |
477 | * @param array $ids (reference) the array that holds all the db ids | |
478 | * | |
479 | * @return object CRM_Contribute_BAO_Contribution object | |
480 | * @access public | |
481 | * @static | |
482 | */ | |
483 | static function retrieve(&$params, &$defaults, &$ids) { | |
484 | $contribution = CRM_Contribute_BAO_Contribution::getValues($params, $defaults, $ids); | |
485 | return $contribution; | |
486 | } | |
487 | ||
488 | /** | |
489 | * combine all the importable fields from the lower levels object | |
490 | * | |
491 | * The ordering is important, since currently we do not have a weight | |
492 | * scheme. Adding weight is super important and should be done in the | |
493 | * next week or so, before this can be called complete. | |
494 | * | |
495 | * @return array array of importable Fields | |
496 | * @access public | |
497 | * @static | |
498 | */ | |
499 | static function &importableFields($contacType = 'Individual', $status = TRUE) { | |
500 | if (!self::$_importableFields) { | |
501 | if (!self::$_importableFields) { | |
502 | self::$_importableFields = array(); | |
503 | } | |
504 | ||
505 | if (!$status) { | |
506 | $fields = array('' => array('title' => ts('- do not import -'))); | |
507 | } | |
508 | else { | |
509 | $fields = array('' => array('title' => ts('- Contribution Fields -'))); | |
510 | } | |
511 | ||
512 | $note = CRM_Core_DAO_Note::import(); | |
513 | $tmpFields = CRM_Contribute_DAO_Contribution::import(); | |
514 | unset($tmpFields['option_value']); | |
515 | $optionFields = CRM_Core_OptionValue::getFields($mode = 'contribute'); | |
516 | $contactFields = CRM_Contact_BAO_Contact::importableFields($contacType, NULL); | |
517 | ||
518 | // Using new Dedupe rule. | |
519 | $ruleParams = array( | |
520 | 'contact_type' => $contacType, | |
521 | 'used' => 'Unsupervised', | |
522 | ); | |
523 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
524 | $tmpConatctField = array(); | |
525 | if (is_array($fieldsArray)) { | |
526 | foreach ($fieldsArray as $value) { | |
527 | //skip if there is no dupe rule | |
528 | if ($value == 'none') { | |
529 | continue; | |
530 | } | |
531 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
532 | $value, | |
533 | 'id', | |
534 | 'column_name' | |
535 | ); | |
536 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; | |
537 | $tmpConatctField[trim($value)] = $contactFields[trim($value)]; | |
538 | if (!$status) { | |
539 | $title = $tmpConatctField[trim($value)]['title'] . ' ' . ts('(match to contact)'); | |
540 | } | |
541 | else { | |
542 | $title = $tmpConatctField[trim($value)]['title']; | |
543 | } | |
544 | $tmpConatctField[trim($value)]['title'] = $title; | |
545 | } | |
546 | } | |
547 | ||
548 | $tmpConatctField['external_identifier'] = $contactFields['external_identifier']; | |
549 | $tmpConatctField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . ' ' . ts('(match to contact)'); | |
550 | $tmpFields['contribution_contact_id']['title'] = $tmpFields['contribution_contact_id']['title'] . ' ' . ts('(match to contact)'); | |
551 | $fields = array_merge($fields, $tmpConatctField); | |
552 | $fields = array_merge($fields, $tmpFields); | |
553 | $fields = array_merge($fields, $note); | |
554 | $fields = array_merge($fields, $optionFields); | |
555 | $fields = array_merge($fields, CRM_Financial_DAO_FinancialType::export()); | |
556 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); | |
557 | self::$_importableFields = $fields; | |
558 | } | |
559 | return self::$_importableFields; | |
560 | } | |
561 | ||
562 | static function &exportableFields() { | |
563 | if (!self::$_exportableFields) { | |
564 | if (!self::$_exportableFields) { | |
565 | self::$_exportableFields = array(); | |
566 | } | |
567 | ||
568 | $impFields = CRM_Contribute_DAO_Contribution::export(); | |
569 | $expFieldProduct = CRM_Contribute_DAO_Product::export(); | |
570 | $expFieldsContrib = CRM_Contribute_DAO_ContributionProduct::export(); | |
571 | $typeField = CRM_Financial_DAO_FinancialType::export(); | |
572 | $financialAccount = CRM_Financial_DAO_FinancialAccount::export(); | |
573 | $optionField = CRM_Core_OptionValue::getFields($mode = 'contribute'); | |
574 | $contributionStatus = array( | |
575 | 'contribution_status' => array( | |
576 | 'title' => ts('Contribution Status'), | |
577 | 'name' => 'contribution_status', | |
578 | 'data_type' => CRM_Utils_Type::T_STRING | |
579 | )); | |
580 | ||
581 | $contributionNote = array( | |
582 | 'contribution_note' => | |
583 | array( | |
584 | 'title' => ts('Contribution Note'), | |
585 | 'name' => 'contribution_note', | |
586 | 'data_type' => CRM_Utils_Type::T_TEXT | |
587 | ) | |
588 | ); | |
589 | ||
590 | $contributionRecurId = array( | |
591 | 'contribution_recur_id' => | |
592 | array( | |
593 | 'title' => ts('Recurring Contributions ID'), | |
594 | 'name' => 'contribution_recur_id', | |
595 | 'where' => 'civicrm_contribution.contribution_recur_id', | |
596 | 'data_type' => CRM_Utils_Type::T_INT | |
597 | )); | |
598 | ||
599 | $extraFields = array( | |
600 | 'contribution_campaign' => | |
601 | array( | |
602 | 'title' => ts('Campaign Title') | |
603 | ), | |
604 | 'contribution_batch' => | |
605 | array( | |
606 | 'title' => ts('Batch Name') | |
607 | ) | |
608 | ); | |
609 | ||
610 | $fields = array_merge($impFields, $typeField, $contributionStatus, $optionField, $expFieldProduct, | |
611 | $expFieldsContrib, $contributionNote, $contributionRecurId, $extraFields, $financialAccount, | |
612 | CRM_Core_BAO_CustomField::getFieldsForImport('Contribution') | |
613 | ); | |
614 | ||
615 | self::$_exportableFields = $fields; | |
616 | } | |
617 | ||
618 | return self::$_exportableFields; | |
619 | } | |
620 | ||
621 | static function getTotalAmountAndCount($status = NULL, $startDate = NULL, $endDate = NULL) { | |
622 | $where = array(); | |
623 | switch ($status) { | |
624 | case 'Valid': | |
625 | $where[] = 'contribution_status_id = 1'; | |
626 | break; | |
627 | ||
628 | case 'Cancelled': | |
629 | $where[] = 'contribution_status_id = 3'; | |
630 | break; | |
631 | } | |
632 | ||
633 | if ($startDate) { | |
634 | $where[] = "receive_date >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; | |
635 | } | |
636 | if ($endDate) { | |
637 | $where[] = "receive_date <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; | |
638 | } | |
639 | ||
640 | $whereCond = implode(' AND ', $where); | |
641 | ||
642 | $query = " | |
643 | SELECT sum( total_amount ) as total_amount, | |
644 | count( civicrm_contribution.id ) as total_count, | |
645 | currency | |
646 | FROM civicrm_contribution | |
647 | INNER JOIN civicrm_contact contact ON ( contact.id = civicrm_contribution.contact_id ) | |
648 | WHERE $whereCond | |
649 | AND ( is_test = 0 OR is_test IS NULL ) | |
650 | AND contact.is_deleted = 0 | |
651 | GROUP BY currency | |
652 | "; | |
653 | ||
654 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
655 | $amount = array(); | |
656 | $count = 0; | |
657 | while ($dao->fetch()) { | |
658 | $count += $dao->total_count; | |
659 | $amount[] = CRM_Utils_Money::format($dao->total_amount, $dao->currency); | |
660 | } | |
661 | if ($count) { | |
662 | return array('amount' => implode(', ', $amount), | |
663 | 'count' => $count, | |
664 | ); | |
665 | } | |
666 | return NULL; | |
667 | } | |
668 | ||
669 | /** | |
670 | * Delete the indirect records associated with this contribution first | |
671 | * | |
672 | * @return $results no of deleted Contribution on success, false otherwise | |
673 | * @access public | |
674 | * @static | |
675 | */ | |
676 | static function deleteContribution($id) { | |
677 | CRM_Utils_Hook::pre('delete', 'Contribution', $id, CRM_Core_DAO::$_nullArray); | |
678 | ||
679 | $transaction = new CRM_Core_Transaction(); | |
680 | ||
681 | $results = NULL; | |
682 | //delete activity record | |
683 | $params = array( | |
684 | 'source_record_id' => $id, | |
685 | // activity type id for contribution | |
686 | 'activity_type_id' => 6, | |
687 | ); | |
688 | ||
689 | CRM_Activity_BAO_Activity::deleteActivity($params); | |
690 | ||
691 | //delete billing address if exists for this contribution. | |
692 | self::deleteAddress($id); | |
693 | ||
694 | //update pledge and pledge payment, CRM-3961 | |
695 | CRM_Pledge_BAO_PledgePayment::resetPledgePayment($id); | |
696 | ||
697 | // remove entry from civicrm_price_set_entity, CRM-5095 | |
698 | if (CRM_Price_BAO_Set::getFor('civicrm_contribution', $id)) { | |
699 | CRM_Price_BAO_Set::removeFrom('civicrm_contribution', $id); | |
700 | } | |
701 | // cleanup line items. | |
702 | $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $id, 'participant_id', 'contribution_id'); | |
703 | ||
de1c25e1 PN |
704 | // delete any related entity_financial_trxn, financial_trxn and financial_item records. |
705 | CRM_Core_BAO_FinancialTrxn::deleteFinancialTrxn($id); | |
6a488035 TO |
706 | |
707 | if ($participantId) { | |
708 | CRM_Price_BAO_LineItem::deleteLineItems($participantId, 'civicrm_participant'); | |
709 | } | |
710 | else { | |
711 | CRM_Price_BAO_LineItem::deleteLineItems($id, 'civicrm_contribution'); | |
712 | } | |
713 | ||
714 | //delete note. | |
715 | $note = CRM_Core_BAO_Note::getNote($id, 'civicrm_contribution'); | |
716 | $noteId = key($note); | |
717 | if ($noteId) { | |
718 | CRM_Core_BAO_Note::del($noteId, FALSE); | |
719 | } | |
720 | ||
721 | $dao = new CRM_Contribute_DAO_Contribution(); | |
722 | $dao->id = $id; | |
723 | ||
724 | $results = $dao->delete(); | |
725 | ||
726 | $transaction->commit(); | |
727 | ||
728 | CRM_Utils_Hook::post('delete', 'Contribution', $dao->id, $dao); | |
729 | ||
730 | // delete the recently created Contribution | |
731 | $contributionRecent = array( | |
732 | 'id' => $id, | |
733 | 'type' => 'Contribution', | |
734 | ); | |
735 | CRM_Utils_Recent::del($contributionRecent); | |
736 | ||
737 | return $results; | |
738 | } | |
739 | ||
740 | /** | |
741 | * Check if there is a contribution with the same trxn_id or invoice_id | |
742 | * | |
743 | * @param array $params (reference ) an assoc array of name/value pairs | |
744 | * @param array $duplicates (reference ) store ids of duplicate contribs | |
745 | * | |
746 | * @return boolean true if duplicate, false otherwise | |
747 | * @access public | |
748 | * static | |
749 | */ | |
750 | static function checkDuplicate($input, &$duplicates, $id = NULL) { | |
751 | if (!$id) { | |
752 | $id = CRM_Utils_Array::value('id', $input); | |
753 | } | |
754 | $trxn_id = CRM_Utils_Array::value('trxn_id', $input); | |
755 | $invoice_id = CRM_Utils_Array::value('invoice_id', $input); | |
756 | ||
757 | $clause = array(); | |
758 | $input = array(); | |
759 | ||
760 | if ($trxn_id) { | |
761 | $clause[] = "trxn_id = %1"; | |
762 | $input[1] = array($trxn_id, 'String'); | |
763 | } | |
764 | ||
765 | if ($invoice_id) { | |
766 | $clause[] = "invoice_id = %2"; | |
767 | $input[2] = array($invoice_id, 'String'); | |
768 | } | |
769 | ||
770 | if (empty($clause)) { | |
771 | return FALSE; | |
772 | } | |
773 | ||
774 | $clause = implode(' OR ', $clause); | |
775 | if ($id) { | |
776 | $clause = "( $clause ) AND id != %3"; | |
777 | $input[3] = array($id, 'Integer'); | |
778 | } | |
779 | ||
780 | $query = "SELECT id FROM civicrm_contribution WHERE $clause"; | |
781 | $dao = CRM_Core_DAO::executeQuery($query, $input); | |
782 | $result = FALSE; | |
783 | while ($dao->fetch()) { | |
784 | $duplicates[] = $dao->id; | |
785 | $result = TRUE; | |
786 | } | |
787 | return $result; | |
788 | } | |
789 | ||
790 | /** | |
791 | * takes an associative array and creates a contribution_product object | |
792 | * | |
793 | * the function extract all the params it needs to initialize the create a | |
794 | * contribution_product object. the params array could contain additional unused name/value | |
795 | * pairs | |
796 | * | |
797 | * @param array $params (reference ) an assoc array of name/value pairs | |
798 | * | |
799 | * @return object CRM_Contribute_BAO_ContributionProduct object | |
800 | * @access public | |
801 | * @static | |
802 | */ | |
803 | static function addPremium(&$params) { | |
804 | $contributionProduct = new CRM_Contribute_DAO_ContributionProduct(); | |
805 | $contributionProduct->copyValues($params); | |
806 | return $contributionProduct->save(); | |
807 | } | |
808 | ||
809 | /** | |
810 | * Function to get list of contribution fields for profile | |
811 | * For now we only allow custom contribution fields to be in | |
812 | * profile | |
813 | * | |
814 | * @param boolean $addExtraFields true if special fields needs to be added | |
815 | * | |
816 | * @return return the list of contribution fields | |
817 | * @static | |
818 | * @access public | |
819 | */ | |
820 | static function getContributionFields($addExtraFields = TRUE) { | |
821 | $contributionFields = CRM_Contribute_DAO_Contribution::export(); | |
822 | $contributionFields = array_merge($contributionFields, CRM_Core_OptionValue::getFields($mode = 'contribute')); | |
823 | ||
824 | if ($addExtraFields) { | |
825 | $contributionFields = array_merge($contributionFields, self::getSpecialContributionFields()); | |
826 | } | |
827 | ||
828 | $contributionFields = array_merge($contributionFields, CRM_Financial_DAO_FinancialType::export()); | |
829 | ||
830 | foreach ($contributionFields as $key => $var) { | |
831 | if ($key == 'contribution_contact_id') { | |
832 | continue; | |
833 | } | |
834 | elseif ($key == 'contribution_campaign_id') { | |
835 | $var['title'] = ts('Campaign'); | |
836 | } | |
837 | $fields[$key] = $var; | |
838 | } | |
839 | ||
840 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); | |
841 | return $fields; | |
842 | } | |
843 | ||
844 | /** | |
845 | * Function to add extra fields specific to contribtion | |
846 | * | |
847 | * @static | |
848 | */ | |
849 | static function getSpecialContributionFields() { | |
850 | $extraFields = array( | |
851 | 'honor_contact_name' => array( | |
852 | 'name' => 'honor_contact_name', | |
853 | 'title' => 'Honor Contact Name', | |
854 | 'headerPattern' => '/^honor_contact_name$/i', | |
855 | 'where' => 'civicrm_contact_c.display_name', | |
856 | ), | |
857 | 'honor_contact_email' => array( | |
858 | 'name' => 'honor_contact_email', | |
859 | 'title' => 'Honor Contact Email', | |
860 | 'headerPattern' => '/^honor_contact_email$/i', | |
861 | 'where' => 'honor_email.email', | |
862 | ), | |
863 | 'honor_contact_id' => array( | |
864 | 'name' => 'honor_contact_id', | |
865 | 'title' => 'Honor Contact ID', | |
866 | 'headerPattern' => '/^honor_contact_id$/i', | |
867 | 'where' => 'civicrm_contribution.honor_contact_id', | |
868 | ), | |
869 | 'honor_type_label' => array( | |
870 | 'name' => 'honor_type_label', | |
871 | 'title' => 'Honor Type Label', | |
872 | 'headerPattern' => '/^honor_type_label$/i', | |
873 | 'where' => 'honor_type.label', | |
874 | ), | |
875 | 'soft_credit_name' => array( | |
876 | 'name' => 'soft_credit_name', | |
877 | 'title' => 'Soft Credit Name', | |
878 | 'headerPattern' => '/^soft_credit_name$/i', | |
879 | 'where' => 'civicrm_contact_d.display_name', | |
880 | ), | |
881 | 'soft_credit_email' => array( | |
882 | 'name' => 'soft_credit_email', | |
883 | 'title' => 'Soft Credit Email', | |
884 | 'headerPattern' => '/^soft_credit_email$/i', | |
885 | 'where' => 'soft_email.email', | |
886 | ), | |
887 | 'soft_credit_phone' => array( | |
888 | 'name' => 'soft_credit_phone', | |
889 | 'title' => 'Soft Credit Phone', | |
890 | 'headerPattern' => '/^soft_credit_phone$/i', | |
891 | 'where' => 'soft_phone.phone', | |
892 | ), | |
893 | 'soft_credit_contact_id' => array( | |
894 | 'name' => 'soft_credit_contact_id', | |
895 | 'title' => 'Soft Credit Contact ID', | |
896 | 'headerPattern' => '/^soft_credit_contact_id$/i', | |
897 | 'where' => 'civicrm_contribution_soft.contact_id', | |
898 | ), | |
899 | ); | |
900 | ||
901 | return $extraFields; | |
902 | } | |
903 | ||
904 | static function getCurrentandGoalAmount($pageID) { | |
905 | $query = " | |
906 | SELECT p.goal_amount as goal, sum( c.total_amount ) as total | |
907 | FROM civicrm_contribution_page p, | |
908 | civicrm_contribution c | |
909 | WHERE p.id = c.contribution_page_id | |
910 | AND p.id = %1 | |
911 | AND c.cancel_date is null | |
912 | GROUP BY p.id | |
913 | "; | |
914 | ||
915 | $config = CRM_Core_Config::singleton(); | |
916 | $params = array(1 => array($pageID, 'Integer')); | |
917 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
918 | ||
919 | if ($dao->fetch()) { | |
920 | return array($dao->goal, $dao->total); | |
921 | } | |
922 | else { | |
923 | return array(NULL, NULL); | |
924 | } | |
925 | } | |
926 | ||
927 | /** | |
928 | * Function to create is honor of | |
929 | * | |
930 | * @param array $params associated array of fields (by reference) | |
931 | * @param int $honorId honor Id | |
932 | * @param array $honorParams any params that should be send to the create function | |
933 | * | |
934 | * @return contact id | |
935 | */ | |
936 | static function createHonorContact(&$params, $honorId = NULL, $honorParams = array()) { | |
937 | $honorParams = array_merge( | |
938 | array( | |
939 | 'first_name' => $params['honor_first_name'], | |
940 | 'last_name' => $params['honor_last_name'], | |
941 | 'prefix_id' => $params['honor_prefix_id'], | |
942 | 'email-Primary' => $params['honor_email'], | |
943 | ), | |
944 | $honorParams | |
945 | ); | |
946 | if (!$honorId) { | |
947 | $honorParams['email'] = $params['honor_email']; | |
948 | ||
949 | $dedupeParams = CRM_Dedupe_Finder::formatParams($honorParams, 'Individual'); | |
950 | $dedupeParams['check_permission'] = FALSE; | |
951 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
952 | ||
953 | // if we find more than one contact, use the first one | |
954 | $honorId = CRM_Utils_Array::value(0, $ids); | |
955 | } | |
956 | ||
957 | $contactID = CRM_Contact_BAO_Contact::createProfileContact( | |
958 | $honorParams, | |
959 | CRM_Core_DAO::$_nullArray, | |
960 | $honorId | |
961 | ); | |
962 | return $contactID; | |
963 | } | |
964 | ||
965 | /** | |
966 | * Function to get list of contribution In Honor of contact Ids | |
967 | * | |
968 | * @param int $honorId In Honor of Contact ID | |
969 | * | |
970 | * @return return the list of contribution fields | |
971 | * | |
972 | * @access public | |
973 | * @static | |
974 | */ | |
975 | static function getHonorContacts($honorId) { | |
976 | $params = array(); | |
977 | $honorDAO = new CRM_Contribute_DAO_Contribution(); | |
978 | $honorDAO->honor_contact_id = $honorId; | |
979 | $honorDAO->find(); | |
980 | ||
981 | $status = CRM_Contribute_PseudoConstant::contributionStatus($honorDAO->contribution_status_id); | |
982 | $type = CRM_Contribute_PseudoConstant::financialType(); | |
983 | ||
984 | while ($honorDAO->fetch()) { | |
985 | $params[$honorDAO->id]['honorId'] = $honorDAO->contact_id; | |
986 | $params[$honorDAO->id]['display_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $honorDAO->contact_id, 'display_name'); | |
987 | $params[$honorDAO->id]['type'] = $type[$honorDAO->financial_type_id]; | |
988 | $params[$honorDAO->id]['type_id'] = $honorDAO->financial_type_id; | |
989 | $params[$honorDAO->id]['amount'] = CRM_Utils_Money::format($honorDAO->total_amount, $honorDAO->currency); | |
990 | $params[$honorDAO->id]['source'] = $honorDAO->source; | |
991 | $params[$honorDAO->id]['receive_date'] = $honorDAO->receive_date; | |
992 | $params[$honorDAO->id]['contribution_status'] = CRM_Utils_Array::value($honorDAO->contribution_status_id, $status); | |
993 | } | |
994 | ||
995 | return $params; | |
996 | } | |
997 | ||
998 | /** | |
999 | * function to get the sort name of a contact for a particular contribution | |
1000 | * | |
1001 | * @param int $id id of the contribution | |
1002 | * | |
1003 | * @return null|string sort name of the contact if found | |
1004 | * @static | |
1005 | * @access public | |
1006 | */ | |
1007 | static function sortName($id) { | |
1008 | $id = CRM_Utils_Type::escape($id, 'Integer'); | |
1009 | ||
1010 | $query = " | |
1011 | SELECT civicrm_contact.sort_name | |
1012 | FROM civicrm_contribution, civicrm_contact | |
1013 | WHERE civicrm_contribution.contact_id = civicrm_contact.id | |
1014 | AND civicrm_contribution.id = {$id} | |
1015 | "; | |
1016 | return CRM_Core_DAO::singleValueQuery($query, CRM_Core_DAO::$_nullArray); | |
1017 | } | |
1018 | ||
1019 | static function annual($contactID) { | |
1020 | if (is_array($contactID)) { | |
1021 | $contactIDs = implode(',', $contactID); | |
1022 | } | |
1023 | else { | |
1024 | $contactIDs = $contactID; | |
1025 | } | |
1026 | ||
1027 | $config = CRM_Core_Config::singleton(); | |
1028 | $startDate = $endDate = NULL; | |
1029 | ||
1030 | $currentMonth = date('m'); | |
1031 | $currentDay = date('d'); | |
1032 | if ((int ) $config->fiscalYearStart['M'] > $currentMonth || | |
1033 | ((int ) $config->fiscalYearStart['M'] == $currentMonth && | |
1034 | (int ) $config->fiscalYearStart['d'] > $currentDay | |
1035 | ) | |
1036 | ) { | |
1037 | $year = date('Y') - 1; | |
1038 | } | |
1039 | else { | |
1040 | $year = date('Y'); | |
1041 | } | |
1042 | $nextYear = $year + 1; | |
1043 | ||
1044 | if ($config->fiscalYearStart) { | |
1045 | if ($config->fiscalYearStart['M'] < 10) { | |
1046 | $config->fiscalYearStart['M'] = '0' . $config->fiscalYearStart['M']; | |
1047 | } | |
1048 | if ($config->fiscalYearStart['d'] < 10) { | |
1049 | $config->fiscalYearStart['d'] = '0' . $config->fiscalYearStart['d']; | |
1050 | } | |
1051 | $monthDay = $config->fiscalYearStart['M'] . $config->fiscalYearStart['d']; | |
1052 | } | |
1053 | else { | |
1054 | $monthDay = '0101'; | |
1055 | } | |
1056 | $startDate = "$year$monthDay"; | |
1057 | $endDate = "$nextYear$monthDay"; | |
1058 | ||
1059 | $query = " | |
1060 | SELECT count(*) as count, | |
1061 | sum(total_amount) as amount, | |
1062 | avg(total_amount) as average, | |
1063 | currency | |
1064 | FROM civicrm_contribution b | |
1065 | WHERE b.contact_id IN ( $contactIDs ) | |
1066 | AND b.contribution_status_id = 1 | |
1067 | AND b.is_test = 0 | |
1068 | AND b.receive_date >= $startDate | |
1069 | AND b.receive_date < $endDate | |
1070 | GROUP BY currency | |
1071 | "; | |
1072 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
1073 | $count = 0; | |
1074 | $amount = $average = array(); | |
1075 | while ($dao->fetch()) { | |
1076 | if ($dao->count > 0 && $dao->amount > 0) { | |
1077 | $count += $dao->count; | |
1078 | $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency); | |
1079 | $average[] = CRM_Utils_Money::format($dao->average, $dao->currency); | |
1080 | } | |
1081 | } | |
1082 | if ($count > 0) { | |
1083 | return array( | |
1084 | $count, | |
1085 | implode(', ', $amount), | |
1086 | implode(', ', $average), | |
1087 | ); | |
1088 | } | |
1089 | return array(0, 0, 0); | |
1090 | } | |
1091 | ||
1092 | /** | |
1093 | * Check if there is a contribution with the params passed in. | |
1094 | * Used for trxn_id,invoice_id and contribution_id | |
1095 | * | |
1096 | * @param array $params an assoc array of name/value pairs | |
1097 | * | |
1098 | * @return array contribution id if success else NULL | |
1099 | * @access public | |
1100 | * static | |
1101 | */ | |
1102 | static function checkDuplicateIds($params) { | |
1103 | $dao = new CRM_Contribute_DAO_Contribution(); | |
1104 | ||
1105 | $clause = array(); | |
1106 | $input = array(); | |
1107 | foreach ($params as $k => $v) { | |
1108 | if ($v) { | |
1109 | $clause[] = "$k = '$v'"; | |
1110 | } | |
1111 | } | |
1112 | $clause = implode(' AND ', $clause); | |
1113 | $query = "SELECT id FROM civicrm_contribution WHERE $clause"; | |
1114 | $dao = CRM_Core_DAO::executeQuery($query, $input); | |
1115 | ||
1116 | while ($dao->fetch()) { | |
1117 | $result = $dao->id; | |
1118 | return $result; | |
1119 | } | |
1120 | return NULL; | |
1121 | } | |
1122 | ||
1123 | /** | |
1124 | * Function to get the contribution details for component export | |
1125 | * | |
1126 | * @param int $exportMode export mode | |
1127 | * @param string $componentIds component ids | |
1128 | * | |
1129 | * @return array associated array | |
1130 | * | |
1131 | * @static | |
1132 | * @access public | |
1133 | */ | |
1134 | static function getContributionDetails($exportMode, $componentIds) { | |
1135 | $paymentDetails = array(); | |
1136 | $componentClause = ' IN ( ' . implode(',', $componentIds) . ' ) '; | |
1137 | ||
1138 | if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { | |
1139 | $componentSelect = " civicrm_participant_payment.participant_id id"; | |
1140 | $additionalClause = " | |
1141 | INNER JOIN civicrm_participant_payment ON (civicrm_contribution.id = civicrm_participant_payment.contribution_id | |
1142 | AND civicrm_participant_payment.participant_id {$componentClause} ) | |
1143 | "; | |
1144 | } | |
1145 | elseif ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT) { | |
1146 | $componentSelect = " civicrm_membership_payment.membership_id id"; | |
1147 | $additionalClause = " | |
1148 | INNER JOIN civicrm_membership_payment ON (civicrm_contribution.id = civicrm_membership_payment.contribution_id | |
1149 | AND civicrm_membership_payment.membership_id {$componentClause} ) | |
1150 | "; | |
1151 | } | |
1152 | elseif ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT) { | |
1153 | $componentSelect = " civicrm_pledge_payment.id id"; | |
1154 | $additionalClause = " | |
1155 | INNER JOIN civicrm_pledge_payment ON (civicrm_contribution.id = civicrm_pledge_payment.contribution_id | |
1156 | AND civicrm_pledge_payment.pledge_id {$componentClause} ) | |
1157 | "; | |
1158 | } | |
1159 | ||
1160 | $query = " SELECT total_amount, contribution_status.name as status_id, contribution_status.label as status, payment_instrument.name as payment_instrument, receive_date, | |
1161 | trxn_id, {$componentSelect} | |
1162 | FROM civicrm_contribution | |
1163 | LEFT JOIN civicrm_option_group option_group_payment_instrument ON ( option_group_payment_instrument.name = 'payment_instrument') | |
1164 | LEFT JOIN civicrm_option_value payment_instrument ON (civicrm_contribution.payment_instrument_id = payment_instrument.value | |
1165 | AND option_group_payment_instrument.id = payment_instrument.option_group_id ) | |
1166 | LEFT JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status') | |
1167 | LEFT JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value | |
1168 | AND option_group_contribution_status.id = contribution_status.option_group_id ) | |
1169 | {$additionalClause} | |
1170 | "; | |
1171 | ||
1172 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
1173 | ||
1174 | while ($dao->fetch()) { | |
1175 | $paymentDetails[$dao->id] = array( | |
1176 | 'total_amount' => $dao->total_amount, | |
1177 | 'contribution_status' => $dao->status, | |
1178 | 'receive_date' => $dao->receive_date, | |
1179 | 'pay_instru' => $dao->payment_instrument, | |
1180 | 'trxn_id' => $dao->trxn_id, | |
1181 | ); | |
1182 | } | |
1183 | ||
1184 | return $paymentDetails; | |
1185 | } | |
1186 | ||
1187 | /** | |
1188 | * Function to create address associated with contribution record. | |
1189 | * @param array $params an associated array | |
1190 | * @param int $billingID $billingLocationTypeID | |
1191 | * | |
1192 | * @return address id | |
1193 | * @static | |
1194 | */ | |
1195 | static function createAddress(&$params, $billingLocationTypeID) { | |
1196 | $billingFields = array( | |
1197 | 'street_address', | |
1198 | 'city', | |
1199 | 'state_province_id', | |
1200 | 'postal_code', | |
1201 | 'country_id', | |
1202 | ); | |
1203 | ||
1204 | //build address array | |
1205 | $addressParams = array(); | |
1206 | $addressParams['location_type_id'] = $billingLocationTypeID; | |
1207 | $addressParams['is_billing'] = 1; | |
3fb990f4 RN |
1208 | |
1209 | $billingFirstName = CRM_Utils_Array::value('billing_first_name', $params); | |
1210 | $billingMiddleName = CRM_Utils_Array::value('billing_middle_name', $params); | |
1211 | $billingLastName = CRM_Utils_Array::value('billing_last_name', $params); | |
1212 | $addressParams['address_name'] = "{$billingFirstName}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$billingMiddleName}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$billingLastName}"; | |
6a488035 TO |
1213 | |
1214 | foreach ($billingFields as $value) { | |
3fb990f4 | 1215 | $addressParams[$value] = CRM_Utils_Array::value("billing_{$value}-{$billingLocationTypeID}", $params); |
6a488035 TO |
1216 | } |
1217 | ||
1218 | $address = CRM_Core_BAO_Address::add($addressParams, FALSE); | |
1219 | ||
1220 | return $address->id; | |
1221 | } | |
1222 | ||
1223 | /** | |
1224 | * Function to create soft contributon with contribution record. | |
1225 | * @param array $params an associated array | |
1226 | * | |
1227 | * @return soft contribution id | |
1228 | * @static | |
1229 | */ | |
1230 | static function addSoftContribution($params) { | |
1231 | $softContribution = new CRM_Contribute_DAO_ContributionSoft(); | |
1232 | $softContribution->copyValues($params); | |
1233 | ||
1234 | // set currency for CRM-1496 | |
1235 | if (!isset($softContribution->currency)) { | |
1236 | $config = CRM_Core_Config::singleton(); | |
1237 | $softContribution->currency = $config->defaultCurrency; | |
1238 | } | |
1239 | ||
1240 | return $softContribution->save(); | |
1241 | } | |
1242 | ||
1243 | /** | |
1244 | * Function to retrieve soft contributon for contribution record. | |
1245 | * @param array $params an associated array | |
1246 | * | |
1247 | * @return soft contribution id | |
1248 | * @static | |
1249 | */ | |
1250 | static function getSoftContribution($params, $all = FALSE) { | |
1251 | $cs = new CRM_Contribute_DAO_ContributionSoft(); | |
1252 | $cs->copyValues($params); | |
1253 | $softContribution = array(); | |
1254 | if ($cs->find(TRUE)) { | |
1255 | if ($all) { | |
1256 | foreach (array( | |
1257 | 'pcp_id', 'pcp_display_in_roll', 'pcp_roll_nickname', 'pcp_personal_note') as $key => $val) { | |
1258 | $softContribution[$val] = $cs->$val; | |
1259 | } | |
1260 | } | |
1261 | $softContribution['soft_credit_to'] = $cs->contact_id; | |
1262 | $softContribution['soft_credit_id'] = $cs->id; | |
1263 | ||
1264 | } | |
1265 | return $softContribution; | |
1266 | } | |
1267 | ||
1268 | /** | |
1269 | * Function to retrieve the list of soft contributons for given contact. | |
1270 | * @param int $contact_id contact id | |
1271 | * | |
1272 | * @return array | |
1273 | * @static | |
1274 | */ | |
1275 | static function getSoftContributionList($contact_id, $isTest = 0) { | |
1276 | $query = " | |
1277 | SELECT ccs.id, ccs.amount as amount, | |
1278 | ccs.contribution_id, | |
1279 | ccs.pcp_id, | |
1280 | ccs.pcp_display_in_roll, | |
1281 | ccs.pcp_roll_nickname, | |
1282 | ccs.pcp_personal_note, | |
1283 | cc.receive_date, | |
1284 | cc.contact_id as contributor_id, | |
1285 | cc.contribution_status_id as contribution_status_id, | |
1286 | cp.title as pcp_title, | |
1287 | cc.currency, | |
1288 | contact.display_name, | |
1289 | cct.name as contributionType | |
1290 | FROM civicrm_contribution_soft ccs | |
1291 | LEFT JOIN civicrm_contribution cc | |
1292 | ON ccs.contribution_id = cc.id | |
1293 | LEFT JOIN civicrm_pcp cp | |
1294 | ON ccs.pcp_id = cp.id | |
1295 | LEFT JOIN civicrm_contact contact | |
1296 | ON ccs.contribution_id = cc.id AND | |
1297 | cc.contact_id = contact.id | |
1298 | LEFT JOIN civicrm_financial_type cct | |
1299 | ON cc.financial_type_id = cct.id | |
1300 | WHERE cc.is_test = %2 AND ccs.contact_id = %1 | |
1301 | ORDER BY cc.receive_date DESC"; | |
1302 | ||
1303 | $params = array(1 => array($contact_id, 'Integer'), | |
1304 | 2 => array($isTest, 'Integer')); | |
1305 | $cs = CRM_Core_DAO::executeQuery($query, $params); | |
1306 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); | |
1307 | $result = array(); | |
1308 | while ($cs->fetch()) { | |
1309 | $result[$cs->id]['amount'] = $cs->amount; | |
1310 | $result[$cs->id]['currency'] = $cs->currency; | |
1311 | $result[$cs->id]['contributor_id'] = $cs->contributor_id; | |
1312 | $result[$cs->id]['contribution_id'] = $cs->contribution_id; | |
1313 | $result[$cs->id]['contributor_name'] = $cs->display_name; | |
1314 | $result[$cs->id]['financial_type'] = $cs->contributionType; | |
1315 | $result[$cs->id]['receive_date'] = $cs->receive_date; | |
1316 | $result[$cs->id]['pcp_id'] = $cs->pcp_id; | |
1317 | $result[$cs->id]['pcp_title'] = $cs->pcp_title; | |
1318 | $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll; | |
1319 | $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname; | |
1320 | $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note; | |
1321 | $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus); | |
1322 | ||
1323 | if ($isTest) { | |
1324 | $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)'; | |
1325 | } | |
1326 | } | |
1327 | return $result; | |
1328 | } | |
1329 | ||
1330 | static function getSoftContributionTotals($contact_id, $isTest = 0) { | |
1331 | $query = " | |
1332 | SELECT SUM(amount) as amount, | |
1333 | AVG(total_amount) as average, | |
1334 | cc.currency | |
1335 | FROM civicrm_contribution_soft ccs | |
1336 | LEFT JOIN civicrm_contribution cc | |
1337 | ON ccs.contribution_id = cc.id | |
1338 | WHERE cc.is_test = %2 AND | |
1339 | ccs.contact_id = %1 | |
1340 | GROUP BY currency "; | |
1341 | ||
1342 | $params = array(1 => array($contact_id, 'Integer'), | |
1343 | 2 => array($isTest, 'Integer')); | |
1344 | ||
1345 | $cs = CRM_Core_DAO::executeQuery($query, $params); | |
1346 | ||
1347 | $count = 0; | |
1348 | $amount = $average = array(); | |
1349 | ||
1350 | while ($cs->fetch()) { | |
1351 | if ($cs->amount > 0) { | |
1352 | $count++; | |
1353 | $amount[] = $cs->amount; | |
1354 | $average[] = $cs->average; | |
1355 | $currency[] = $cs->currency; | |
1356 | } | |
1357 | } | |
1358 | ||
1359 | if ($count > 0) { | |
1360 | return array(implode(', ', $amount), | |
1361 | implode(', ', $average), | |
1362 | implode(', ', $currency), | |
1363 | ); | |
1364 | } | |
1365 | return array(0, 0); | |
1366 | } | |
1367 | ||
1368 | /** | |
1369 | * Delete billing address record related contribution | |
1370 | * | |
1371 | * @param int $contact_id contact id | |
1372 | * @param int $contribution_id contributionId | |
1373 | * @access public | |
1374 | * @static | |
1375 | */ | |
1376 | static function deleteAddress($contributionId = NULL, $contactId = NULL) { | |
1377 | $clauses = array(); | |
1378 | $contactJoin = NULL; | |
1379 | ||
1380 | if ($contributionId) { | |
1381 | $clauses[] = "cc.id = {$contributionId}"; | |
1382 | } | |
1383 | ||
1384 | if ($contactId) { | |
1385 | $clauses[] = "cco.id = {$contactId}"; | |
1386 | $contactJoin = "INNER JOIN civicrm_contact cco ON cc.contact_id = cco.id"; | |
1387 | } | |
1388 | ||
1389 | if (empty($clauses)) { | |
1390 | CRM_Core_Error::fatal(); | |
1391 | } | |
1392 | ||
1393 | $condition = implode(' OR ', $clauses); | |
1394 | ||
1395 | $query = " | |
1396 | SELECT ca.id | |
1397 | FROM civicrm_address ca | |
1398 | INNER JOIN civicrm_contribution cc ON cc.address_id = ca.id | |
1399 | $contactJoin | |
1400 | WHERE $condition | |
1401 | "; | |
1402 | $dao = CRM_Core_DAO::executeQuery($query); | |
1403 | ||
1404 | while ($dao->fetch()) { | |
1405 | $params = array('id' => $dao->id); | |
1406 | CRM_Core_BAO_Block::blockDelete('Address', $params); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | /** | |
1411 | * This function check online pending contribution associated w/ | |
1412 | * Online Event Registration or Online Membership signup. | |
1413 | * | |
1414 | * @param int $componentId participant/membership id. | |
1415 | * @param string $componentName Event/Membership. | |
1416 | * | |
1417 | * @return $contributionId pending contribution id. | |
1418 | * @static | |
1419 | */ | |
1420 | static function checkOnlinePendingContribution($componentId, $componentName) { | |
1421 | $contributionId = NULL; | |
1422 | if (!$componentId || | |
1423 | !in_array($componentName, array('Event', 'Membership')) | |
1424 | ) { | |
1425 | return $contributionId; | |
1426 | } | |
1427 | ||
1428 | if ($componentName == 'Event') { | |
1429 | $idName = 'participant_id'; | |
1430 | $componentTable = 'civicrm_participant'; | |
1431 | $paymentTable = 'civicrm_participant_payment'; | |
1432 | $source = ts('Online Event Registration'); | |
1433 | } | |
1434 | ||
1435 | if ($componentName == 'Membership') { | |
1436 | $idName = 'membership_id'; | |
1437 | $componentTable = 'civicrm_membership'; | |
1438 | $paymentTable = 'civicrm_membership_payment'; | |
1439 | $source = ts('Online Contribution'); | |
1440 | } | |
1441 | ||
1442 | $pendingStatusId = array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')); | |
1443 | ||
1444 | $query = " | |
1445 | SELECT component.id as {$idName}, | |
1446 | componentPayment.contribution_id as contribution_id, | |
1447 | contribution.source source, | |
1448 | contribution.contribution_status_id as contribution_status_id, | |
1449 | contribution.is_pay_later as is_pay_later | |
1450 | FROM $componentTable component | |
1451 | LEFT JOIN $paymentTable componentPayment ON ( componentPayment.{$idName} = component.id ) | |
1452 | LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_id = contribution.id ) | |
1453 | WHERE component.id = {$componentId}"; | |
1454 | ||
1455 | $dao = CRM_Core_DAO::executeQuery($query); | |
1456 | ||
1457 | while ($dao->fetch()) { | |
1458 | if ($dao->contribution_id && | |
1459 | $dao->is_pay_later && | |
1460 | $dao->contribution_status_id == $pendingStatusId && | |
1461 | strpos($dao->source, $source) !== FALSE | |
1462 | ) { | |
1463 | $contributionId = $dao->contribution_id; | |
1464 | $dao->free(); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | return $contributionId; | |
1469 | } | |
1470 | ||
1471 | /** | |
1472 | * This function update contribution as well as related objects. | |
1473 | */ | |
1474 | function transitionComponents($params, $processContributionObject = FALSE) { | |
1475 | // get minimum required values. | |
1476 | $contactId = CRM_Utils_Array::value('contact_id', $params); | |
1477 | $componentId = CRM_Utils_Array::value('component_id', $params); | |
1478 | $componentName = CRM_Utils_Array::value('componentName', $params); | |
1479 | $contributionId = CRM_Utils_Array::value('contribution_id', $params); | |
1480 | $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $params); | |
1481 | ||
1482 | // if we already processed contribution object pass previous status id. | |
1483 | $previousContriStatusId = CRM_Utils_Array::value('previous_contribution_status_id', $params); | |
1484 | ||
1485 | $updateResult = array(); | |
1486 | ||
1487 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
1488 | ||
1489 | // we process only ( Completed, Cancelled, or Failed ) contributions. | |
1490 | if (!$contributionId || | |
1491 | !in_array($contributionStatusId, array(array_search('Completed', $contributionStatuses), | |
1492 | array_search('Cancelled', $contributionStatuses), | |
1493 | array_search('Failed', $contributionStatuses), | |
1494 | )) | |
1495 | ) { | |
1496 | return $updateResult; | |
1497 | } | |
1498 | ||
1499 | if (!$componentName || !$componentId) { | |
1500 | // get the related component details. | |
1501 | $componentDetails = self::getComponentDetails($contributionId); | |
1502 | } | |
1503 | else { | |
1504 | $componentDetails['contact_id'] = $contactId; | |
1505 | $componentDetails['component'] = $componentName; | |
1506 | ||
1507 | if ($componentName == 'event') { | |
1508 | $componentDetails['participant'] = $componentId; | |
1509 | } | |
1510 | else { | |
1511 | $componentDetails['membership'] = $componentId; | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | if (CRM_Utils_Array::value('contact_id', $componentDetails)) { | |
1516 | $componentDetails['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
1517 | $contributionId, | |
1518 | 'contact_id' | |
1519 | ); | |
1520 | } | |
1521 | ||
1522 | // do check for required ids. | |
1523 | if (!CRM_Utils_Array::value('membership', $componentDetails) && | |
1524 | !CRM_Utils_Array::value('participant', $componentDetails) && | |
1525 | !CRM_Utils_Array::value('pledge_payment', $componentDetails) || | |
1526 | !CRM_Utils_Array::value('contact_id', $componentDetails) | |
1527 | ) { | |
1528 | return $updateResult; | |
1529 | } | |
1530 | ||
1531 | //now we are ready w/ required ids, start processing. | |
1532 | ||
1533 | $baseIPN = new CRM_Core_Payment_BaseIPN(); | |
1534 | ||
1535 | $input = $ids = $objects = array(); | |
1536 | ||
1537 | $input['component'] = CRM_Utils_Array::value('component', $componentDetails); | |
1538 | $ids['contribution'] = $contributionId; | |
1539 | $ids['contact'] = CRM_Utils_Array::value('contact_id', $componentDetails); | |
1540 | $ids['membership'] = CRM_Utils_Array::value('membership', $componentDetails); | |
1541 | $ids['participant'] = CRM_Utils_Array::value('participant', $componentDetails); | |
1542 | $ids['event'] = CRM_Utils_Array::value('event', $componentDetails); | |
1543 | $ids['pledge_payment'] = CRM_Utils_Array::value('pledge_payment', $componentDetails); | |
1544 | $ids['contributionRecur'] = NULL; | |
1545 | $ids['contributionPage'] = NULL; | |
1546 | ||
1547 | if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) { | |
1548 | CRM_Core_Error::fatal(); | |
1549 | } | |
1550 | ||
1551 | $memberships = &$objects['membership']; | |
1552 | $participant = &$objects['participant']; | |
1553 | $pledgePayment = &$objects['pledge_payment']; | |
1554 | $contribution = &$objects['contribution']; | |
1555 | ||
1556 | if ($pledgePayment) { | |
1557 | $pledgePaymentIDs = array(); | |
1558 | foreach ($pledgePayment as $key => $object) { | |
1559 | $pledgePaymentIDs[] = $object->id; | |
1560 | } | |
1561 | $pledgeID = $pledgePayment[0]->pledge_id; | |
1562 | } | |
1563 | ||
1564 | ||
1565 | $membershipStatuses = CRM_Member_PseudoConstant::membershipStatus(); | |
1566 | ||
1567 | if ($participant) { | |
1568 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); | |
1569 | $oldStatus = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
1570 | $participant->id, | |
1571 | 'status_id' | |
1572 | ); | |
1573 | } | |
1574 | // we might want to process contribution object. | |
1575 | $processContribution = FALSE; | |
1576 | if ($contributionStatusId == array_search('Cancelled', $contributionStatuses)) { | |
1577 | if (is_array($memberships)) { | |
1578 | foreach ($memberships as $membership) { | |
1579 | if ($membership) { | |
1580 | $membership->status_id = array_search('Cancelled', $membershipStatuses); | |
1581 | $membership->save(); | |
1582 | ||
1583 | $updateResult['updatedComponents']['CiviMember'] = $membership->status_id; | |
1584 | if ($processContributionObject) { | |
1585 | $processContribution = TRUE; | |
1586 | } | |
1587 | } | |
1588 | } | |
1589 | } | |
1590 | ||
1591 | if ($participant) { | |
1592 | $updatedStatusId = array_search('Cancelled', $participantStatuses); | |
1593 | CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE); | |
1594 | ||
1595 | $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId; | |
1596 | if ($processContributionObject) { | |
1597 | $processContribution = TRUE; | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | if ($pledgePayment) { | |
1602 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId); | |
1603 | ||
1604 | $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId; | |
1605 | if ($processContributionObject) { | |
1606 | $processContribution = TRUE; | |
1607 | } | |
1608 | } | |
1609 | } | |
1610 | elseif ($contributionStatusId == array_search('Failed', $contributionStatuses)) { | |
1611 | if (is_array($memberships)) { | |
1612 | foreach ($memberships as $membership) { | |
1613 | if ($membership) { | |
1614 | $membership->status_id = array_search('Expired', $membershipStatuses); | |
1615 | $membership->save(); | |
1616 | ||
1617 | $updateResult['updatedComponents']['CiviMember'] = $membership->status_id; | |
1618 | if ($processContributionObject) { | |
1619 | $processContribution = TRUE; | |
1620 | } | |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | if ($participant) { | |
1625 | $updatedStatusId = array_search('Cancelled', $participantStatuses); | |
1626 | CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE); | |
1627 | ||
1628 | $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId; | |
1629 | if ($processContributionObject) { | |
1630 | $processContribution = TRUE; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | if ($pledgePayment) { | |
1635 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId); | |
1636 | ||
1637 | $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId; | |
1638 | if ($processContributionObject) { | |
1639 | $processContribution = TRUE; | |
1640 | } | |
1641 | } | |
1642 | } | |
1643 | elseif ($contributionStatusId == array_search('Completed', $contributionStatuses)) { | |
1644 | ||
1645 | // only pending contribution related object processed. | |
1646 | if ($previousContriStatusId && | |
1647 | ($previousContriStatusId != array_search('Pending', $contributionStatuses)) | |
1648 | ) { | |
1649 | // this is case when we already processed contribution object. | |
1650 | return $updateResult; | |
1651 | } | |
1652 | elseif (!$previousContriStatusId && | |
1653 | $contribution->contribution_status_id != array_search('Pending', $contributionStatuses) | |
1654 | ) { | |
1655 | // this is case when we are going to process contribution object later. | |
1656 | return $updateResult; | |
1657 | } | |
1658 | ||
1659 | if (is_array($memberships)) { | |
1660 | foreach ($memberships as $membership) { | |
1661 | if ($membership) { | |
1662 | $format = '%Y%m%d'; | |
1663 | ||
1664 | //CRM-4523 | |
1665 | $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membership->contact_id, | |
1666 | $membership->membership_type_id, | |
1667 | $membership->is_test, $membership->id | |
1668 | ); | |
1669 | ||
1670 | // CRM-8141 update the membership type with the value recorded in log when membership created/renewed | |
1671 | // this picks up membership type changes during renewals | |
1672 | $sql = " | |
1673 | SELECT membership_type_id | |
1674 | FROM civicrm_membership_log | |
1675 | WHERE membership_id=$membership->id | |
1676 | ORDER BY id DESC | |
1677 | LIMIT 1;"; | |
1678 | $dao = new CRM_Core_DAO; | |
1679 | $dao->query($sql); | |
1680 | if ($dao->fetch()) { | |
1681 | if (!empty($dao->membership_type_id)) { | |
1682 | $membership->membership_type_id = $dao->membership_type_id; | |
1683 | $membership->save(); | |
1684 | } | |
1685 | } | |
1686 | // else fall back to using current membership type | |
1687 | $dao->free(); | |
1688 | ||
9c09f5b7 AH |
1689 | // Figure out number of terms |
1690 | $numterms = 1; | |
1691 | $lineitems = CRM_Price_BAO_LineItem::getLineItems($contributionId, 'contribution'); | |
1692 | foreach ($lineitems as $lineitem) { | |
1693 | if ($membership->membership_type_id == CRM_Utils_Array::value('membership_type_id', $lineitem)) { | |
1694 | $numterms = CRM_Utils_Array::value('membership_num_terms', $lineitem); | |
1695 | ||
1696 | // in case membership_num_terms comes through as null or zero | |
1697 | $numterms = $numterms >= 1 ? $numterms : 1; | |
1698 | break; | |
1699 | } | |
1700 | } | |
1701 | ||
6a488035 | 1702 | if ($currentMembership) { |
9c09f5b7 AH |
1703 | CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, NULL); |
1704 | $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id, NULL, NULL, $numterms); | |
6a488035 TO |
1705 | $dates['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format); |
1706 | } | |
1707 | else { | |
9c09f5b7 | 1708 | $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membership->membership_type_id, null, null, null, $numterms); |
6a488035 TO |
1709 | } |
1710 | ||
1711 | //get the status for membership. | |
1712 | $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], | |
1713 | $dates['end_date'], | |
1714 | $dates['join_date'], | |
1715 | 'today', | |
1716 | TRUE | |
1717 | ); | |
1718 | ||
1719 | $formatedParams = array( | |
1720 | 'status_id' => CRM_Utils_Array::value('id', $calcStatus, | |
1721 | array_search('Current', $membershipStatuses) | |
1722 | ), | |
1723 | 'join_date' => CRM_Utils_Date::customFormat($dates['join_date'], $format), | |
1724 | 'start_date' => CRM_Utils_Date::customFormat($dates['start_date'], $format), | |
1725 | 'end_date' => CRM_Utils_Date::customFormat($dates['end_date'], $format), | |
1726 | ); | |
1727 | ||
1728 | CRM_Utils_Hook::pre('edit', 'Membership', $membership->id, $formatedParams); | |
1729 | ||
1730 | $membership->copyValues($formatedParams); | |
1731 | $membership->save(); | |
1732 | ||
1733 | //updating the membership log | |
1734 | $membershipLog = array(); | |
1735 | $membershipLog = $formatedParams; | |
e5ab073b | 1736 | $logStartDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('log_start_date', $dates), $format); |
6a488035 TO |
1737 | $logStartDate = ($logStartDate) ? CRM_Utils_Date::isoToMysql($logStartDate) : $formatedParams['start_date']; |
1738 | ||
1739 | $membershipLog['start_date'] = $logStartDate; | |
1740 | $membershipLog['membership_id'] = $membership->id; | |
1741 | $membershipLog['modified_id'] = $membership->contact_id; | |
1742 | $membershipLog['modified_date'] = date('Ymd'); | |
1743 | $membershipLog['membership_type_id'] = $membership->membership_type_id; | |
1744 | ||
1745 | CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); | |
1746 | ||
1747 | //update related Memberships. | |
1748 | CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $formatedParams); | |
1749 | ||
1750 | $updateResult['membership_end_date'] = CRM_Utils_Date::customFormat($dates['end_date'], | |
1751 | '%B %E%f, %Y' | |
1752 | ); | |
1753 | $updateResult['updatedComponents']['CiviMember'] = $membership->status_id; | |
1754 | if ($processContributionObject) { | |
1755 | $processContribution = TRUE; | |
1756 | } | |
1757 | ||
1758 | CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership); | |
1759 | } | |
1760 | } | |
1761 | } | |
1762 | ||
1763 | if ($participant) { | |
1764 | $updatedStatusId = array_search('Registered', $participantStatuses); | |
1765 | CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE); | |
1766 | ||
1767 | $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId; | |
1768 | if ($processContributionObject) { | |
1769 | $processContribution = TRUE; | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | if ($pledgePayment) { | |
1774 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId); | |
1775 | ||
1776 | $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId; | |
1777 | if ($processContributionObject) { | |
1778 | $processContribution = TRUE; | |
1779 | } | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | // process contribution object. | |
1784 | if ($processContribution) { | |
1785 | $contributionParams = array(); | |
1786 | $fields = array( | |
1787 | 'contact_id', 'total_amount', 'receive_date', 'is_test', 'campaign_id', | |
1788 | 'payment_instrument_id', 'trxn_id', 'invoice_id', 'financial_type_id', | |
1789 | 'contribution_status_id', 'non_deductible_amount', 'receipt_date', 'check_number', | |
1790 | ); | |
1791 | foreach ($fields as $field) { | |
1792 | if (!CRM_Utils_Array::value($field, $params)) { | |
1793 | continue; | |
1794 | } | |
1795 | $contributionParams[$field] = $params[$field]; | |
1796 | } | |
1797 | ||
1798 | $ids = array('contribution' => $contributionId); | |
1799 | $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); | |
1800 | } | |
1801 | ||
1802 | return $updateResult; | |
1803 | } | |
1804 | ||
1805 | /** | |
1806 | * This function returns all contribution related object ids. | |
1807 | */ | |
1808 | function getComponentDetails($contributionId) { | |
1809 | $componentDetails = $pledgePayment = array(); | |
1810 | if (!$contributionId) { | |
1811 | return $componentDetails; | |
1812 | } | |
1813 | ||
1814 | $query = " | |
1815 | SELECT c.id as contribution_id, | |
1816 | c.contact_id as contact_id, | |
1817 | mp.membership_id as membership_id, | |
1818 | m.membership_type_id as membership_type_id, | |
1819 | pp.participant_id as participant_id, | |
1820 | p.event_id as event_id, | |
1821 | pgp.id as pledge_payment_id | |
1822 | FROM civicrm_contribution c | |
1823 | LEFT JOIN civicrm_membership_payment mp ON mp.contribution_id = c.id | |
1824 | LEFT JOIN civicrm_participant_payment pp ON pp.contribution_id = c.id | |
1825 | LEFT JOIN civicrm_participant p ON pp.participant_id = p.id | |
1826 | LEFT JOIN civicrm_membership m ON m.id = mp.membership_id | |
1827 | LEFT JOIN civicrm_pledge_payment pgp ON pgp.contribution_id = c.id | |
1828 | WHERE c.id = $contributionId"; | |
1829 | ||
1830 | $dao = CRM_Core_DAO::executeQuery($query); | |
1831 | $componentDetails = array(); | |
1832 | ||
1833 | while ($dao->fetch()) { | |
1834 | $componentDetails['component'] = $dao->participant_id ? 'event' : 'contribute'; | |
1835 | $componentDetails['contact_id'] = $dao->contact_id; | |
1836 | if ($dao->event_id) { | |
1837 | $componentDetails['event'] = $dao->event_id; | |
1838 | } | |
1839 | if ($dao->participant_id) { | |
1840 | $componentDetails['participant'] = $dao->participant_id; | |
1841 | } | |
1842 | if ($dao->membership_id) { | |
1843 | if (!isset($componentDetails['membership'])) { | |
1844 | $componentDetails['membership'] = $componentDetails['membership_type'] = array(); | |
1845 | } | |
1846 | $componentDetails['membership'][] = $dao->membership_id; | |
1847 | $componentDetails['membership_type'][] = $dao->membership_type_id; | |
1848 | } | |
1849 | if ($dao->pledge_payment_id) { | |
1850 | $pledgePayment[] = $dao->pledge_payment_id; | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | if ($pledgePayment) { | |
1855 | $componentDetails['pledge_payment'] = $pledgePayment; | |
1856 | } | |
1857 | ||
1858 | return $componentDetails; | |
1859 | } | |
1860 | ||
1861 | static function contributionCount($contactId, $includeSoftCredit = TRUE, $includeHonoree = TRUE) { | |
1862 | if (!$contactId) { | |
1863 | return 0; | |
1864 | } | |
1865 | ||
1866 | $fromClause = "civicrm_contribution contribution"; | |
1867 | $whereConditions = array("contribution.contact_id = {$contactId}"); | |
1868 | if ($includeSoftCredit) { | |
1869 | $fromClause .= " LEFT JOIN civicrm_contribution_soft softContribution | |
1870 | ON ( contribution.id = softContribution.contribution_id )"; | |
1871 | $whereConditions[] = " softContribution.contact_id = {$contactId}"; | |
1872 | } | |
1873 | if ($includeHonoree) { | |
1874 | $whereConditions[] = " contribution.honor_contact_id = {$contactId}"; | |
1875 | } | |
1876 | $whereClause = " contribution.is_test = 0 AND ( " . implode(' OR ', $whereConditions) . " )"; | |
1877 | ||
1878 | $query = " | |
1879 | SELECT count( contribution.id ) count | |
1880 | FROM {$fromClause} | |
1881 | WHERE {$whereClause}"; | |
1882 | ||
1883 | return CRM_Core_DAO::singleValueQuery($query); | |
1884 | } | |
1885 | ||
1886 | /** | |
1887 | * Function to get individual id for onbehalf contribution | |
1888 | * | |
1889 | * @param int $contributionId contribution id | |
1890 | * @param int $contributorId contributer id | |
1891 | * | |
1892 | * @return array $ids containing organization id and individual id | |
1893 | * @access public | |
1894 | */ | |
1895 | function getOnbehalfIds($contributionId, $contributorId = NULL) { | |
1896 | ||
1897 | $ids = array(); | |
1898 | ||
1899 | if (!$contributionId) { | |
1900 | return $ids; | |
1901 | } | |
1902 | ||
1903 | // fetch contributor id if null | |
1904 | if (!$contributorId) { | |
1905 | $contributorId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
1906 | $contributionId, 'contact_id' | |
1907 | ); | |
1908 | } | |
1909 | ||
1910 | $activityTypeIds = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); | |
1911 | $activityTypeId = array_search('Contribution', $activityTypeIds); | |
1912 | ||
1913 | if ($activityTypeId && $contributorId) { | |
1914 | $activityQuery = " | |
1915 | SELECT source_contact_id | |
1916 | FROM civicrm_activity | |
1917 | WHERE activity_type_id = %1 | |
1918 | AND source_record_id = %2"; | |
1919 | ||
1920 | $params = array(1 => array($activityTypeId, 'Integer'), | |
1921 | 2 => array($contributionId, 'Integer'), | |
1922 | ); | |
1923 | ||
1924 | $sourceContactId = CRM_Core_DAO::singleValueQuery($activityQuery, $params); | |
1925 | ||
1926 | // for on behalf contribution source is individual and contributor is organization | |
1927 | if ($sourceContactId && $sourceContactId != $contributorId) { | |
1928 | $relationshipTypeIds = CRM_Core_PseudoConstant::relationshipType('name'); | |
1929 | // get rel type id for employee of relation | |
1930 | foreach ($relationshipTypeIds as $id => $typeVals) { | |
1931 | if ($typeVals['name_a_b'] == 'Employee of') { | |
1932 | $relationshipTypeId = $id; | |
1933 | break; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | $rel = new CRM_Contact_DAO_Relationship(); | |
1938 | $rel->relationship_type_id = $relationshipTypeId; | |
1939 | $rel->contact_id_a = $sourceContactId; | |
1940 | $rel->contact_id_b = $contributorId; | |
1941 | if ($rel->find(TRUE)) { | |
1942 | $ids['individual_id'] = $rel->contact_id_a; | |
1943 | $ids['organization_id'] = $rel->contact_id_b; | |
1944 | } | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | return $ids; | |
1949 | } | |
1950 | ||
1951 | /** | |
1952 | * @return array | |
1953 | * @static | |
1954 | */ | |
1955 | static function getContributionDates() { | |
1956 | $config = CRM_Core_Config::singleton(); | |
1957 | $currentMonth = date('m'); | |
1958 | $currentDay = date('d'); | |
1959 | if ((int ) $config->fiscalYearStart['M'] > $currentMonth || | |
1960 | ((int ) $config->fiscalYearStart['M'] == $currentMonth && | |
1961 | (int ) $config->fiscalYearStart['d'] > $currentDay | |
1962 | ) | |
1963 | ) { | |
1964 | $year = date('Y') - 1; | |
1965 | } | |
1966 | else { | |
1967 | $year = date('Y'); | |
1968 | } | |
1969 | $year = array('Y' => $year); | |
1970 | $yearDate = $config->fiscalYearStart; | |
1971 | $yearDate = array_merge($year, $yearDate); | |
1972 | $yearDate = CRM_Utils_Date::format($yearDate); | |
1973 | ||
1974 | $monthDate = date('Ym') . '01'; | |
1975 | ||
1976 | $now = date('Ymd'); | |
1977 | ||
1978 | return array( | |
1979 | 'now' => $now, | |
1980 | 'yearDate' => $yearDate, | |
1981 | 'monthDate' => $monthDate, | |
1982 | ); | |
1983 | } | |
1984 | ||
1985 | /* | |
1986 | * Load objects relations to contribution object | |
1987 | * Objects are stored in the $_relatedObjects property | |
1988 | * In the first instance we are just moving functionality from BASEIpn - | |
1989 | * see http://issues.civicrm.org/jira/browse/CRM-9996 | |
1990 | * | |
1991 | * @param array $input Input as delivered from Payment Processor | |
1992 | * @param array $ids Ids as Loaded by Payment Processor | |
1993 | * @param boolean $required Is Payment processor / contribution page required | |
1994 | * @param boolean $loadAll - load all related objects - even where id not passed in? (allows API to call this) | |
1995 | * Note that the unit test for the BaseIPN class tests this function | |
1996 | */ | |
1997 | function loadRelatedObjects(&$input, &$ids, $required = FALSE, $loadAll = false) { | |
1998 | if($loadAll){ | |
1999 | $ids = array_merge($this->getComponentDetails($this->id),$ids); | |
2000 | if(empty($ids['contact']) && isset($this->contact_id)){ | |
2001 | $ids['contact'] = $this->contact_id; | |
2002 | } | |
2003 | } | |
2004 | if (empty($this->_component)) { | |
2005 | if (! empty($ids['event'])) { | |
2006 | $this->_component = 'event'; | |
2007 | } | |
2008 | else { | |
2009 | $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); | |
2010 | } | |
2011 | } | |
2012 | $paymentProcessorID = CRM_Utils_Array::value('paymentProcessor', $ids); | |
2013 | $contributionType = new CRM_Financial_BAO_FinancialType(); | |
2014 | $contributionType->id = $this->financial_type_id; | |
2015 | if (!$contributionType->find(TRUE)) { | |
2016 | throw new Exception("Could not find financial type record: " . $this->financial_type_id); | |
2017 | } | |
2018 | if (!empty($ids['contact'])) { | |
2019 | $this->_relatedObjects['contact'] = new CRM_Contact_BAO_Contact(); | |
2020 | $this->_relatedObjects['contact']->id = $ids['contact']; | |
2021 | $this->_relatedObjects['contact']->find(TRUE); | |
2022 | } | |
2023 | $this->_relatedObjects['contributionType'] = $contributionType; | |
2024 | ||
2025 | if ($this->_component == 'contribute') { | |
2026 | // retrieve the other optional objects first so | |
2027 | // stuff down the line can use this info and do things | |
2028 | // CRM-6056 | |
2029 | //in any case get the memberships associated with the contribution | |
2030 | //because we now support multiple memberships w/ price set | |
2031 | // see if there are any other memberships to be considered for same contribution. | |
2032 | $query = " | |
2033 | SELECT membership_id | |
2034 | FROM civicrm_membership_payment | |
2035 | WHERE contribution_id = %1 "; | |
2036 | $params = array(1 => array($this->id, 'Integer')); | |
2037 | ||
2038 | $dao = CRM_Core_DAO::executeQuery($query, $params ); | |
2039 | while ($dao->fetch()) { | |
2040 | if ($dao->membership_id) { | |
2041 | if (!is_array($ids['membership'])) { | |
2042 | $ids['membership'] = array(); | |
2043 | } | |
2044 | $ids['membership'][] = $dao->membership_id; | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | if (array_key_exists('membership', $ids) && is_array($ids['membership'])) { | |
2049 | foreach ($ids['membership'] as $id) { | |
2050 | if (!empty($id)) { | |
2051 | $membership = new CRM_Member_BAO_Membership(); | |
2052 | $membership->id = $id; | |
2053 | if (!$membership->find(TRUE)) { | |
2054 | throw new Exception("Could not find membership record: $id"); | |
2055 | } | |
2056 | $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date); | |
2057 | $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date); | |
2058 | $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date); | |
2059 | $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership; | |
2060 | $membership->free(); | |
2061 | } | |
2062 | } | |
2063 | } | |
2064 | ||
2065 | if (!empty($ids['pledge_payment'])) { | |
2066 | ||
2067 | foreach ($ids['pledge_payment'] as $key => $paymentID) { | |
2068 | if (empty($paymentID)) { | |
2069 | continue; | |
2070 | } | |
2071 | $payment = new CRM_Pledge_BAO_PledgePayment(); | |
2072 | $payment->id = $paymentID; | |
2073 | if (!$payment->find(TRUE)) { | |
2074 | throw new Exception("Could not find pledge payment record: " . $paymentID); | |
2075 | } | |
2076 | $this->_relatedObjects['pledge_payment'][] = $payment; | |
2077 | } | |
2078 | } | |
2079 | ||
2080 | if (!empty($ids['contributionRecur'])) { | |
2081 | $recur = new CRM_Contribute_BAO_ContributionRecur(); | |
2082 | $recur->id = $ids['contributionRecur']; | |
2083 | if (!$recur->find(TRUE)) { | |
2084 | throw new Exception("Could not find recur record: " . $ids['contributionRecur']); | |
2085 | } | |
2086 | $this->_relatedObjects['contributionRecur'] = &$recur; | |
2087 | //get payment processor id from recur object. | |
2088 | $paymentProcessorID = $recur->payment_processor_id; | |
2089 | } | |
2090 | //for normal contribution get the payment processor id. | |
2091 | if (!$paymentProcessorID) { | |
2092 | if ($this->contribution_page_id) { | |
2093 | // get the payment processor id from contribution page | |
2094 | $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', | |
2095 | $this->contribution_page_id, | |
2096 | 'payment_processor' | |
2097 | ); | |
2098 | } | |
2099 | //fail to load payment processor id. | |
2100 | elseif (!CRM_Utils_Array::value('pledge_payment', $ids)) { | |
2101 | $loadObjectSuccess = TRUE; | |
2102 | if ($required) { | |
2103 | throw new Exception("Could not find contribution page for contribution record: " . $this->id); | |
2104 | } | |
2105 | return $loadObjectSuccess; | |
2106 | } | |
2107 | } | |
2108 | } | |
2109 | else { | |
2110 | // we are in event mode | |
2111 | // make sure event exists and is valid | |
2112 | $event = new CRM_Event_BAO_Event(); | |
2113 | $event->id = $ids['event']; | |
2114 | if ($ids['event'] && | |
2115 | !$event->find(TRUE) | |
2116 | ) { | |
2117 | throw new Exception("Could not find event: " . $ids['event']); | |
2118 | } | |
2119 | ||
2120 | $this->_relatedObjects['event'] = &$event; | |
2121 | ||
2122 | $participant = new CRM_Event_BAO_Participant(); | |
2123 | $participant->id = $ids['participant']; | |
2124 | if ($ids['participant'] && | |
2125 | !$participant->find(TRUE) | |
2126 | ) { | |
2127 | throw new Exception("Could not find participant: " . $ids['participant']); | |
2128 | } | |
2129 | $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date); | |
2130 | ||
2131 | $this->_relatedObjects['participant'] = &$participant; | |
2132 | ||
2133 | if (!$paymentProcessorID) { | |
2134 | $paymentProcessorID = $this->_relatedObjects['event']->payment_processor; | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | $loadObjectSuccess = TRUE; | |
2139 | if ($paymentProcessorID) { | |
2140 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, | |
2141 | $this->is_test ? 'test' : 'live' | |
2142 | ); | |
2143 | $ids['paymentProcessor'] = $paymentProcessorID; | |
2144 | $this->_relatedObjects['paymentProcessor'] = &$paymentProcessor; | |
2145 | } | |
2146 | elseif ($required) { | |
2147 | $loadObjectSuccess = FALSE; | |
2148 | throw new Exception("Could not find payment processor for contribution record: " . $this->id); | |
2149 | } | |
2150 | ||
2151 | return $loadObjectSuccess; | |
2152 | } | |
2153 | ||
2154 | /* | |
2155 | * Create array of message information - ie. return html version, txt version, to field | |
2156 | * | |
2157 | * @param array $input incoming information | |
2158 | * - is_recur - should this be treated as recurring (not sure why you wouldn't | |
2159 | * just check presence of recur object but maintaining legacy approach | |
2160 | * to be careful) | |
2161 | * @param array $ids IDs of related objects | |
2162 | * @param array $values any values that may have already been compiled by calling process | |
2163 | * This is augmented by values 'gathered' by gatherMessageValues | |
2164 | * @param bool $returnMessageText distinguishes between whether to send message or return | |
2165 | * message text. We are working towards this function ALWAYS returning message text & calling | |
2166 | * function doing emails / pdfs with it | |
2167 | * @return array $messageArray - messages | |
2168 | */ | |
2169 | function composeMessageArray(&$input, &$ids, &$values, $recur = FALSE, $returnMessageText = TRUE) { | |
2170 | if (empty($this->_relatedObjects)) { | |
2171 | $this->loadRelatedObjects($input, $ids); | |
2172 | } | |
2173 | if (empty($this->_component)) { | |
2174 | $this->_component = CRM_Utils_Array::value('component', $input); | |
2175 | } | |
2176 | ||
2177 | //not really sure what params might be passed in but lets merge em into values | |
2178 | $values = array_merge($this->_gatherMessageValues($input, $values, $ids), $values); | |
2179 | $template = CRM_Core_Smarty::singleton(); | |
2180 | $this->_assignMessageVariablesToTemplate($values, $input, $template, $recur, $returnMessageText); | |
2181 | //what does recur 'mean here - to do with payment processor return functionality but | |
2182 | // what is the importance | |
2183 | if ($recur && !empty($this->_relatedObjects['paymentProcessor'])) { | |
2184 | $paymentObject = &CRM_Core_Payment::singleton( | |
2185 | $this->is_test ? 'test' : 'live', | |
2186 | $this->_relatedObjects['paymentProcessor'] | |
2187 | ); | |
2188 | ||
2189 | $entityID = $entity = NULL; | |
2190 | if (isset($ids['contribution'])) { | |
2191 | $entity = 'contribution'; | |
2192 | $entityID = $ids['contribution']; | |
2193 | } | |
2194 | if (isset($ids['membership']) && $ids['membership']) { | |
2195 | $entity = 'membership'; | |
2196 | $entityID = $ids['membership']; | |
2197 | } | |
2198 | ||
2199 | $url = $paymentObject->subscriptionURL($entityID, $entity); | |
2200 | $template->assign('cancelSubscriptionUrl', $url); | |
2201 | ||
2202 | $url = $paymentObject->subscriptionURL($entityID, $entity, 'billing'); | |
2203 | $template->assign('updateSubscriptionBillingUrl', $url); | |
2204 | ||
2205 | $url = $paymentObject->subscriptionURL($entityID, $entity, 'update'); | |
2206 | $template->assign('updateSubscriptionUrl', $url); | |
2207 | ||
2208 | if ($this->_relatedObjects['paymentProcessor']['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) { | |
2209 | //direct mode showing billing block, so use directIPN for temporary | |
2210 | $template->assign('contributeMode', 'directIPN'); | |
2211 | } | |
2212 | } | |
2213 | // todo remove strtolower - check consistency | |
2214 | if (strtolower($this->_component) == 'event') { | |
2215 | return CRM_Event_BAO_Event::sendMail($ids['contact'], $values, | |
2216 | $this->_relatedObjects['participant']->id, $this->is_test, $returnMessageText | |
2217 | ); | |
2218 | } | |
2219 | else { | |
2220 | $values['contribution_id'] = $this->id; | |
2221 | if (CRM_Utils_Array::value('related_contact', $ids)) { | |
2222 | $values['related_contact'] = $ids['related_contact']; | |
2223 | if (isset($ids['onbehalf_dupe_alert'])) { | |
2224 | $values['onbehalf_dupe_alert'] = $ids['onbehalf_dupe_alert']; | |
2225 | } | |
2226 | $entityBlock = array( | |
2227 | 'contact_id' => $ids['contact'], | |
2228 | 'location_type_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', | |
2229 | 'Home', 'id', 'name' | |
2230 | ), | |
2231 | ); | |
2232 | $address = CRM_Core_BAO_Address::getValues($entityBlock); | |
2233 | $template->assign('onBehalfAddress', $address[$entityBlock['location_type_id']]['display']); | |
2234 | } | |
2235 | $isTest = FALSE; | |
2236 | if ($this->is_test) { | |
2237 | $isTest = TRUE; | |
2238 | } | |
2239 | if (!empty($this->_relatedObjects['membership'])) { | |
2240 | foreach ($this->_relatedObjects['membership'] as $membership) { | |
2241 | if ($membership->id) { | |
2242 | $values['membership_id'] = $membership->id; | |
2243 | ||
2244 | // need to set the membership values here | |
2245 | $template->assign('membership_assign', 1); | |
2246 | $template->assign('membership_name', | |
2247 | CRM_Member_PseudoConstant::membershipType($membership->membership_type_id) | |
2248 | ); | |
2249 | $template->assign('mem_start_date', $membership->start_date); | |
2250 | $template->assign('mem_join_date', $membership->join_date); | |
2251 | $template->assign('mem_end_date', $membership->end_date); | |
2252 | $membership_status = CRM_Member_PseudoConstant::membershipStatus($membership->status_id, NULL, 'label'); | |
2253 | $template->assign('mem_status', $membership_status); | |
2254 | if ($membership_status == 'Pending' && $membership->is_pay_later == 1) { | |
2255 | $template->assign('is_pay_later', 1); | |
2256 | } | |
2257 | ||
2258 | // if separate payment there are two contributions recorded and the | |
2259 | // admin will need to send a receipt for each of them separately. | |
2260 | // we dont link the two in the db (but can potentially infer it if needed) | |
2261 | $template->assign('is_separate_payment', 0); | |
2262 | ||
2263 | if ($recur && $paymentObject) { | |
2264 | $url = $paymentObject->subscriptionURL($membership->id, 'membership'); | |
2265 | $template->assign('cancelSubscriptionUrl', $url); | |
2266 | $url = $paymentObject->subscriptionURL($membership->id, 'membership', 'billing'); | |
2267 | $template->assign('updateSubscriptionBillingUrl', $url); | |
2268 | $url = $paymentObject->subscriptionURL($entityID, $entity, 'update'); | |
2269 | $template->assign('updateSubscriptionUrl', $url); | |
2270 | } | |
2271 | ||
2272 | $result = CRM_Contribute_BAO_ContributionPage::sendMail($ids['contact'], $values, $isTest, $returnMessageText); | |
2273 | ||
2274 | return $result; | |
2275 | // otherwise if its about sending emails, continue sending without return, as we | |
2276 | // don't want to exit the loop. | |
2277 | } | |
2278 | } | |
2279 | } | |
2280 | else { | |
2281 | return CRM_Contribute_BAO_ContributionPage::sendMail($ids['contact'], $values, $isTest, $returnMessageText); | |
2282 | } | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | /* | |
2287 | * Gather values for contribution mail - this function has been created | |
2288 | * as part of CRM-9996 refactoring as a step towards simplifying the composeMessage function | |
2289 | * Values related to the contribution in question are gathered | |
2290 | * | |
2291 | * @param array $input input into function (probably from payment processor) | |
2292 | * @param array $ids the set of ids related to the inpurt | |
2293 | * | |
2294 | * @return array $values | |
2295 | * | |
2296 | * NB don't add direct calls to the function as we intend to change the signature | |
2297 | */ | |
2298 | function _gatherMessageValues($input, &$values, $ids = array()) { | |
2299 | // set display address of contributor | |
2300 | if ($this->address_id) { | |
2301 | $addressParams = array('id' => $this->address_id); | |
2302 | $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id'); | |
2303 | $addressDetails = array_values($addressDetails); | |
2304 | $values['address'] = $addressDetails[0]['display']; | |
2305 | } | |
2306 | if ($this->_component == 'contribute') { | |
2307 | if (isset($this->contribution_page_id)) { | |
2308 | CRM_Contribute_BAO_ContributionPage::setValues( | |
2309 | $this->contribution_page_id, | |
2310 | $values | |
2311 | ); | |
2312 | if ($this->contribution_page_id) { | |
2313 | // CRM-8254 - override default currency if applicable | |
2314 | $config = CRM_Core_Config::singleton(); | |
2315 | $config->defaultCurrency = CRM_Utils_Array::value( | |
2316 | 'currency', | |
2317 | $values, | |
2318 | $config->defaultCurrency | |
2319 | ); | |
2320 | } | |
2321 | } | |
2322 | // no contribution page -probably back office | |
2323 | else { | |
2324 | // Handle re-print receipt for offline contributions (call from PDF.php - no contribution_page_id) | |
2325 | $values['is_email_receipt'] = 1; | |
2326 | $values['title'] = 'Contribution'; | |
2327 | } | |
2328 | // set lineItem for contribution | |
2329 | if ($this->id) { | |
2330 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->id, 'contribution', 1); | |
2331 | if (!empty($lineItem)) { | |
2332 | $itemId = key($lineItem); | |
2333 | foreach ($lineItem as &$eachItem) { | |
2334 | if (array_key_exists($eachItem['membership_type_id'], $this->_relatedObjects['membership']) ) { | |
2335 | $eachItem['join_date'] = CRM_Utils_Date::customFormat($this->_relatedObjects['membership'][$eachItem['membership_type_id']]->join_date); | |
2336 | $eachItem['start_date'] = CRM_Utils_Date::customFormat($this->_relatedObjects['membership'][$eachItem['membership_type_id']]->start_date); | |
2337 | $eachItem['end_date'] = CRM_Utils_Date::customFormat($this->_relatedObjects['membership'][$eachItem['membership_type_id']]->end_date); | |
2338 | } | |
2339 | } | |
2340 | $values['lineItem'][0] = $lineItem; | |
2341 | $values['priceSetID'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $lineItem[$itemId]['price_field_id'], 'price_set_id'); | |
2342 | } | |
2343 | } | |
2344 | ||
2345 | $relatedContact = CRM_Contribute_BAO_Contribution::getOnbehalfIds( | |
2346 | $this->id, | |
2347 | $this->contact_id | |
2348 | ); | |
2349 | // if this is onbehalf of contribution then set related contact | |
2350 | if (CRM_Utils_Array::value('individual_id', $relatedContact)) { | |
2351 | $values['related_contact'] = $ids['related_contact'] = $relatedContact['individual_id']; | |
2352 | } | |
2353 | } | |
2354 | else { | |
2355 | // event | |
2356 | $eventParams = array( | |
2357 | 'id' => $this->_relatedObjects['event']->id, | |
2358 | ); | |
2359 | $values['event'] = array(); | |
2360 | ||
2361 | CRM_Event_BAO_Event::retrieve($eventParams, $values['event']); | |
2362 | ||
2363 | //get location details | |
2364 | $locationParams = array( | |
2365 | 'entity_id' => $this->_relatedObjects['event']->id, | |
2366 | 'entity_table' => 'civicrm_event', | |
2367 | ); | |
2368 | $values['location'] = CRM_Core_BAO_Location::getValues($locationParams); | |
2369 | ||
2370 | $ufJoinParams = array( | |
2371 | 'entity_table' => 'civicrm_event', | |
2372 | 'entity_id' => $ids['event'], | |
2373 | 'module' => 'CiviEvent', | |
2374 | ); | |
2375 | ||
2376 | list($custom_pre_id, | |
2377 | $custom_post_ids | |
2378 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
2379 | ||
2380 | $values['custom_pre_id'] = $custom_pre_id; | |
2381 | $values['custom_post_id'] = $custom_post_ids; | |
2382 | ||
2383 | // set lineItem for event contribution | |
2384 | if ($this->id) { | |
2385 | $participantIds = CRM_Event_BAO_Participant::getParticipantIds($this->id); | |
2386 | if (!empty($participantIds)) { | |
2387 | foreach ($participantIds as $pIDs) { | |
2388 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($pIDs); | |
2389 | if (!CRM_Utils_System::isNull($lineItem)) { | |
2390 | $values['lineItem'][] = $lineItem; | |
2391 | } | |
2392 | } | |
2393 | } | |
2394 | } | |
2395 | } | |
2396 | ||
2397 | return $values; | |
2398 | } | |
2399 | ||
2400 | /** | |
2401 | * Apply variables for message to smarty template - this function is part of analysing what is in the huge | |
2402 | * function & breaking it down into manageable chunks. Eventually it will be refactored into something else | |
2403 | * Note we send directly from this function in some cases because it is only partly refactored | |
2404 | * Don't call this function directly as the signature will change | |
2405 | */ | |
2406 | function _assignMessageVariablesToTemplate(&$values, $input, &$template, $recur = FALSE, $returnMessageText = True) { | |
2407 | $template->assign('first_name', $this->_relatedObjects['contact']->first_name); | |
2408 | $template->assign('last_name', $this->_relatedObjects['contact']->last_name); | |
2409 | $template->assign('displayName', $this->_relatedObjects['contact']->display_name); | |
2410 | if (!empty($values['lineItem']) && !empty($this->_relatedObjects['membership'])) { | |
2411 | $template->assign('useForMember', true); | |
2412 | } | |
2413 | //assign honor infomation to receiptmessage | |
2414 | $honorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
2415 | $this->id, | |
2416 | 'honor_contact_id' | |
2417 | ); | |
2418 | if (!empty($honorID)) { | |
2419 | ||
2420 | $honorDefault = $honorIds = array(); | |
2421 | $honorIds['contribution'] = $this->id; | |
2422 | $idParams = array('id' => $honorID, 'contact_id' => $honorID); | |
2423 | CRM_Contact_BAO_Contact::retrieve($idParams, $honorDefault, $honorIds); | |
2424 | $honorType = CRM_Core_PseudoConstant::honor(); | |
2425 | ||
2426 | $template->assign('honor_block_is_active', 1); | |
2427 | if (CRM_Utils_Array::value('prefix_id', $honorDefault)) { | |
2428 | $prefix = CRM_Core_PseudoConstant::individualPrefix(); | |
2429 | $template->assign('honor_prefix', $prefix[$honorDefault['prefix_id']]); | |
2430 | } | |
2431 | $template->assign('honor_first_name', CRM_Utils_Array::value('first_name', $honorDefault)); | |
2432 | $template->assign('honor_last_name', CRM_Utils_Array::value('last_name', $honorDefault)); | |
2433 | $template->assign('honor_email', CRM_Utils_Array::value('email', $honorDefault['email'][1])); | |
2434 | $template->assign('honor_type', $honorType[$this->honor_type_id]); | |
2435 | } | |
2436 | ||
2437 | $dao = new CRM_Contribute_DAO_ContributionProduct(); | |
2438 | $dao->contribution_id = $this->id; | |
2439 | if ($dao->find(TRUE)) { | |
2440 | $premiumId = $dao->product_id; | |
2441 | $template->assign('option', $dao->product_option); | |
2442 | ||
2443 | $productDAO = new CRM_Contribute_DAO_Product(); | |
2444 | $productDAO->id = $premiumId; | |
2445 | $productDAO->find(TRUE); | |
2446 | $template->assign('selectPremium', TRUE); | |
2447 | $template->assign('product_name', $productDAO->name); | |
2448 | $template->assign('price', $productDAO->price); | |
2449 | $template->assign('sku', $productDAO->sku); | |
2450 | } | |
2451 | $template->assign('title', CRM_Utils_Array::value('title',$values)); | |
2452 | $amount = CRM_Utils_Array::value('total_amount', $input,(CRM_Utils_Array::value('amount', $input)),null); | |
2453 | if(empty($amount) && isset($this->total_amount)){ | |
2454 | $amount = $this->total_amount; | |
2455 | } | |
2456 | $template->assign('amount', $amount); | |
2457 | // add the new contribution values | |
2458 | if (strtolower($this->_component) == 'contribute') { | |
2459 | //PCP Info | |
2460 | $softDAO = new CRM_Contribute_DAO_ContributionSoft(); | |
2461 | $softDAO->contribution_id = $this->id; | |
2462 | if ($softDAO->find(TRUE)) { | |
2463 | $template->assign('pcpBlock', TRUE); | |
2464 | $template->assign('pcp_display_in_roll', $softDAO->pcp_display_in_roll); | |
2465 | $template->assign('pcp_roll_nickname', $softDAO->pcp_roll_nickname); | |
2466 | $template->assign('pcp_personal_note', $softDAO->pcp_personal_note); | |
2467 | ||
2468 | //assign the pcp page title for email subject | |
2469 | $pcpDAO = new CRM_PCP_DAO_PCP(); | |
2470 | $pcpDAO->id = $softDAO->pcp_id; | |
2471 | if ($pcpDAO->find(TRUE)) { | |
2472 | $template->assign('title', $pcpDAO->title); | |
2473 | } | |
2474 | } | |
2475 | } | |
2476 | ||
2477 | if ($this->financial_type_id) { | |
2478 | $values['financial_type_id'] = $this->financial_type_id; | |
2479 | } | |
2480 | ||
2481 | ||
2482 | $template->assign('trxn_id', $this->trxn_id); | |
2483 | $template->assign('receive_date', | |
2484 | CRM_Utils_Date::mysqlToIso($this->receive_date) | |
2485 | ); | |
2486 | $template->assign('contributeMode', 'notify'); | |
2487 | $template->assign('action', $this->is_test ? 1024 : 1); | |
2488 | $template->assign('receipt_text', | |
2489 | CRM_Utils_Array::value('receipt_text', | |
2490 | $values | |
2491 | ) | |
2492 | ); | |
2493 | $template->assign('is_monetary', 1); | |
2494 | $template->assign('is_recur', (bool) $recur); | |
2495 | $template->assign('currency', $this->currency); | |
2496 | $template->assign('address', CRM_Utils_Address::format($input)); | |
2497 | if ($this->_component == 'event') { | |
2498 | $template->assign('title', $values['event']['title']); | |
2499 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); | |
2500 | $viewRoles = array(); | |
2501 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_relatedObjects['participant']->role_id) as $k => $v) { | |
2502 | $viewRoles[] = $participantRoles[$v]; | |
2503 | } | |
2504 | $values['event']['participant_role'] = implode(', ', $viewRoles); | |
2505 | $template->assign('event', $values['event']); | |
2506 | $template->assign('location', $values['location']); | |
2507 | $template->assign('customPre', $values['custom_pre_id']); | |
2508 | $template->assign('customPost', $values['custom_post_id']); | |
2509 | ||
2510 | $isTest = FALSE; | |
2511 | if ($this->_relatedObjects['participant']->is_test) { | |
2512 | $isTest = TRUE; | |
2513 | } | |
2514 | ||
2515 | $values['params'] = array(); | |
2516 | //to get email of primary participant. | |
2517 | $primaryEmail = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $this->_relatedObjects['participant']->contact_id, 'email', 'contact_id'); | |
2518 | $primaryAmount[] = array('label' => $this->_relatedObjects['participant']->fee_level . ' - ' . $primaryEmail, 'amount' => $this->_relatedObjects['participant']->fee_amount); | |
2519 | //build an array of cId/pId of participants | |
2520 | $additionalIDs = CRM_Event_BAO_Event::buildCustomProfile($this->_relatedObjects['participant']->id, NULL, $this->_relatedObjects['contact']->id, $isTest, TRUE); | |
2521 | unset($additionalIDs[$this->_relatedObjects['participant']->id]); | |
2522 | //send receipt to additional participant if exists | |
2523 | if (count($additionalIDs)) { | |
2524 | $template->assign('isPrimary', 0); | |
2525 | $template->assign('customProfile', NULL); | |
2526 | //set additionalParticipant true | |
2527 | $values['params']['additionalParticipant'] = TRUE; | |
2528 | foreach ($additionalIDs as $pId => $cId) { | |
2529 | $amount = array(); | |
2530 | //to change the status pending to completed | |
2531 | $additional = new CRM_Event_DAO_Participant(); | |
2532 | $additional->id = $pId; | |
2533 | $additional->contact_id = $cId; | |
2534 | $additional->find(TRUE); | |
2535 | $additional->register_date = $this->_relatedObjects['participant']->register_date; | |
2536 | $additional->status_id = 1; | |
2537 | $additionalParticipantInfo = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $additional->contact_id, 'email', 'contact_id'); | |
2538 | //if additional participant dont have email | |
2539 | //use display name. | |
2540 | if (!$additionalParticipantInfo) { | |
2541 | $additionalParticipantInfo = CRM_Contact_BAO_Contact::displayName($additional->contact_id); | |
2542 | } | |
2543 | $amount[0] = array('label' => $additional->fee_level, 'amount' => $additional->fee_amount); | |
2544 | $primaryAmount[] = array('label' => $additional->fee_level . ' - ' . $additionalParticipantInfo, 'amount' => $additional->fee_amount); | |
2545 | $additional->save(); | |
2546 | $additional->free(); | |
2547 | $template->assign('amount', $amount); | |
2548 | CRM_Event_BAO_Event::sendMail($cId, $values, $pId, $isTest, $returnMessageText); | |
2549 | } | |
2550 | } | |
2551 | ||
2552 | //build an array of custom profile and assigning it to template | |
2553 | $customProfile = CRM_Event_BAO_Event::buildCustomProfile($this->_relatedObjects['participant']->id, $values, NULL, $isTest); | |
2554 | ||
2555 | if (count($customProfile)) { | |
2556 | $template->assign('customProfile', $customProfile); | |
2557 | } | |
2558 | ||
2559 | // for primary contact | |
2560 | $values['params']['additionalParticipant'] = FALSE; | |
2561 | $template->assign('isPrimary', 1); | |
2562 | $template->assign('amount', $primaryAmount); | |
2563 | $template->assign('register_date', CRM_Utils_Date::isoToMysql($this->_relatedObjects['participant']->register_date)); | |
2564 | if ($this->payment_instrument_id) { | |
2565 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
2566 | $template->assign('paidBy', $paymentInstrument[$this->payment_instrument_id]); | |
2567 | } | |
2568 | // carry paylater, since we did not created billing, | |
2569 | // so need to pull email from primary location, CRM-4395 | |
2570 | $values['params']['is_pay_later'] = $this->_relatedObjects['participant']->is_pay_later; | |
2571 | } | |
2572 | return $template; | |
2573 | } | |
2574 | ||
2575 | /** | |
2576 | * Function to check whether payment processor supports | |
2577 | * cancellation of contribution subscription | |
2578 | * | |
2579 | * @param int $contributionId contribution id | |
2580 | * | |
2581 | * @return boolean | |
2582 | * @access public | |
2583 | * @static | |
2584 | */ | |
2585 | static function isCancelSubscriptionSupported($contributionId, $isNotCancelled = TRUE) { | |
2586 | $cacheKeyString = "$contributionId"; | |
2587 | $cacheKeyString .= $isNotCancelled ? '_1' : '_0'; | |
2588 | ||
2589 | static $supportsCancel = array(); | |
2590 | ||
2591 | if (!array_key_exists($cacheKeyString, $supportsCancel)) { | |
2592 | $supportsCancel[$cacheKeyString] = FALSE; | |
2593 | $isCancelled = FALSE; | |
2594 | ||
2595 | if ($isNotCancelled) { | |
2596 | $isCancelled = self::isSubscriptionCancelled($contributionId); | |
2597 | } | |
2598 | ||
2599 | $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($contributionId, 'contribute', 'obj'); | |
2600 | if (!empty($paymentObject)) { | |
2601 | $supportsCancel[$cacheKeyString] = $paymentObject->isSupported('cancelSubscription') && !$isCancelled; | |
2602 | } | |
2603 | } | |
2604 | return $supportsCancel[$cacheKeyString]; | |
2605 | } | |
2606 | ||
2607 | /** | |
2608 | * Function to check whether subscription is already cancelled | |
2609 | * | |
2610 | * @param int $contributionId contribution id | |
2611 | * | |
2612 | * @return string $status contribution status | |
2613 | * @access public | |
2614 | * @static | |
2615 | */ | |
2616 | static function isSubscriptionCancelled($contributionId) { | |
2617 | $sql = " | |
2618 | SELECT cr.contribution_status_id | |
2619 | FROM civicrm_contribution_recur cr | |
2620 | LEFT JOIN civicrm_contribution con ON ( cr.id = con.contribution_recur_id ) | |
2621 | WHERE con.id = %1 LIMIT 1"; | |
2622 | $params = array(1 => array($contributionId, 'Integer')); | |
2623 | $statusId = CRM_Core_DAO::singleValueQuery($sql, $params); | |
2624 | $status = CRM_Contribute_PseudoConstant::contributionStatus($statusId); | |
2625 | if ($status == 'Cancelled') { | |
2626 | return TRUE; | |
2627 | } | |
2628 | return FALSE; | |
2629 | } | |
2630 | ||
2631 | /** | |
2632 | * Function to create all financial accounts entry | |
2633 | * | |
2634 | * @param array $params contribution object, line item array and params for trxn | |
2635 | * | |
2636 | * @param array $ids of contribution id | |
2637 | * | |
2638 | * @access public | |
2639 | * @static | |
2640 | */ | |
2641 | static function recordFinancialAccounts(&$params, $ids) { | |
2642 | $skipRecords = $update = FALSE; | |
464bb009 | 2643 | $additionalPaticipantId = array(); |
6a488035 TO |
2644 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
2645 | ||
2646 | if (CRM_Utils_Array::value('contribution_mode', $params) == 'participant') { | |
2647 | $entityId = $params['participant_id']; | |
2648 | $entityTable = 'civicrm_participant'; | |
464bb009 | 2649 | $additionalPaticipantId = CRM_Event_BAO_Participant::getAdditionalParticipantIds($entityId); |
6a488035 TO |
2650 | } |
2651 | else { | |
2652 | $entityId = $params['contribution']->id; | |
2653 | $entityTable = 'civicrm_contribution'; | |
2654 | } | |
464bb009 PN |
2655 | $entityID[] = $entityId; |
2656 | if (!empty($additionalPaticipantId)) { | |
2657 | $entityID += $additionalPaticipantId; | |
2658 | } | |
6a488035 TO |
2659 | if (!CRM_Utils_Array::value('prevContribution', $params)) { |
2660 | $entityID = NULL; | |
2661 | } | |
e005ab6b PN |
2662 | else { |
2663 | $update = TRUE; | |
2664 | } | |
6a488035 | 2665 | // build line item array if its not set in $params |
464bb009 | 2666 | if (!CRM_Utils_Array::value('line_item', $params) || $additionalPaticipantId) { |
6a488035 TO |
2667 | CRM_Price_BAO_LineItem::getLineItemArray($params, $entityID, str_replace('civicrm_', '', $entityTable)); |
2668 | } | |
2669 | ||
2670 | if (CRM_Utils_Array::value('contribution_status_id', $params) != array_search('Failed', $contributionStatuses) && | |
2671 | !(CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Pending', $contributionStatuses) && !$params['contribution']->is_pay_later)) { | |
2672 | $skipRecords = TRUE; | |
2673 | if (CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Pending', $contributionStatuses)) { | |
2674 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); | |
2675 | $params['to_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); | |
2676 | } | |
2677 | elseif (CRM_Utils_Array::value('payment_processor', $params)) { | |
2678 | $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($params['payment_processor'], 'civicrm_payment_processor', 'financial_account_id'); | |
2679 | } | |
2680 | elseif (CRM_Utils_Array::value('payment_instrument_id', $params)) { | |
2681 | $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($params['payment_instrument_id']); | |
2682 | } | |
2683 | else { | |
ac7514c2 PN |
2684 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' ")); |
2685 | $queryParams = array(1 => array($relationTypeId, 'Integer')); | |
2686 | $params['to_financial_account_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams); | |
6a488035 TO |
2687 | } |
2688 | ||
2689 | $totalAmount = CRM_Utils_Array::value('total_amount', $params); | |
2690 | if (!isset($totalAmount) && CRM_Utils_Array::value('prevContribution', $params)) { | |
2691 | $totalAmount = $params['total_amount'] = $params['prevContribution']->total_amount; | |
2692 | } | |
2693 | //build financial transaction params | |
2694 | $trxnParams = array( | |
2695 | 'contribution_id' => $params['contribution']->id, | |
2696 | 'to_financial_account_id' => $params['to_financial_account_id'], | |
2697 | 'trxn_date' => date('YmdHis'), | |
2698 | 'total_amount' => $totalAmount, | |
2699 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params), | |
2700 | 'net_amount' => CRM_Utils_Array::value('net_amount', $params), | |
2701 | 'currency' => $params['contribution']->currency, | |
2702 | 'trxn_id' => $params['contribution']->trxn_id, | |
2703 | 'status_id' => $params['contribution']->contribution_status_id, | |
12921438 | 2704 | 'payment_instrument_id' => $params['contribution']->payment_instrument_id, |
6a488035 TO |
2705 | 'check_number' => CRM_Utils_Array::value('check_number', $params), |
2706 | ); | |
2707 | ||
2708 | if (CRM_Utils_Array::value('payment_processor', $params)) { | |
12921438 | 2709 | $trxnParams['payment_processor_id'] = $params['payment_processor']; |
6a488035 TO |
2710 | } |
2711 | $params['trxnParams'] = $trxnParams; | |
2712 | ||
2713 | if (CRM_Utils_Array::value('prevContribution', $params)) { | |
2714 | ||
2715 | //if Change contribution amount | |
2716 | if (array_key_exists('total_amount', $params) && isset($params['total_amount']) && | |
2717 | $params['total_amount'] != $params['prevContribution']->total_amount) { | |
2718 | //Update Financial Records | |
2719 | self::updateFinancialAccounts($params, 'changedAmount'); | |
2720 | } | |
2721 | ||
2722 | //Update contribution status | |
2723 | if (CRM_Utils_Array::value('contribution_status_id', $params) && | |
2724 | $params['prevContribution']->contribution_status_id != $params['contribution']->contribution_status_id) { | |
2725 | //Update Financial Records | |
2726 | self::updateFinancialAccounts($params, 'changedStatus'); | |
2727 | } | |
2728 | ||
2729 | // change Payment Instrument for a Completed contribution | |
2730 | // first handle special case when contribution is changed from Pending to Completed status when initial payment | |
2731 | // instrument is null and now new payment instrument is added along with the payment | |
2732 | if (array_key_exists('payment_instrument_id', $params)) { | |
2733 | if (CRM_Utils_System::isNull($params['prevContribution']->payment_instrument_id) && | |
2734 | !CRM_Utils_System::isNull($params['contribution']->payment_instrument_id)) { | |
2735 | //check if status is changed from Pending to Completed | |
2736 | // do not update payment instrument changes for Pending to Completed | |
2737 | if (!($params['contribution']->contribution_status_id == array_search('Completed', $contributionStatuses) && | |
2738 | $params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatuses))) { | |
2739 | // for all other statuses create new financial records | |
2740 | self::updateFinancialAccounts($params, 'changePaymentInstrument'); | |
2741 | } | |
2742 | } | |
2743 | else if ((!CRM_Utils_System::isNull($params['contribution']->payment_instrument_id) || | |
2744 | !CRM_Utils_System::isNull($params['prevContribution']->payment_instrument_id)) && | |
2745 | $params['contribution']->payment_instrument_id != $params['prevContribution']->payment_instrument_id) { | |
2746 | // for any other payment instrument changes create new financial records | |
2747 | self::updateFinancialAccounts($params, 'changePaymentInstrument'); | |
2748 | } | |
2749 | else if (!CRM_Utils_System::isNull($params['contribution']->check_number) && | |
2750 | $params['contribution']->check_number != $params['prevContribution']->check_number) { | |
2751 | // another special case when check number is changed, create new financial records | |
2752 | // create financial trxn with negative amount | |
2753 | $params['trxnParams']['total_amount'] = - $trxnParams['total_amount']; | |
2754 | $params['trxnParams']['check_number'] = $params['prevContribution']->check_number; | |
2755 | self::updateFinancialAccounts($params, 'changePaymentInstrument'); | |
2756 | // create financial trxn with positive amount | |
2757 | $params['trxnParams']['check_number'] = $params['contribution']->check_number; | |
2758 | $params['total_amount'] = $params['trxnParams']['total_amount'] = $trxnParams['total_amount']; | |
2759 | self::updateFinancialAccounts($params, 'changePaymentInstrument'); | |
2760 | } | |
2761 | } | |
2762 | ||
2763 | //if financial type is changed | |
2764 | if (CRM_Utils_Array::value('financial_type_id', $params) && | |
2765 | $params['contribution']->financial_type_id != $params['prevContribution']->financial_type_id) { | |
2766 | $incomeTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); | |
2767 | $oldFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $incomeTypeId); | |
2768 | $newFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $incomeTypeId); | |
2769 | if ($oldFinancialAccount != $newFinancialAccount) { | |
2770 | $params['total_amount'] = 0; | |
2771 | if ($params['contribution']->contribution_status_id == array_search('Pending', $contributionStatuses)) { | |
2772 | $params['trxnParams']['to_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( | |
2773 | $params['prevContribution']->financial_type_id, $relationTypeId); | |
2774 | } | |
2775 | self::updateFinancialAccounts($params, 'changeFinancialType'); | |
2776 | $params['trxnParams']['to_financial_account_id'] = $trxnParams['to_financial_account_id']; | |
2777 | $params['financial_account_id'] = $newFinancialAccount; | |
2778 | $params['total_amount'] = $params['trxnParams']['total_amount'] = $trxnParams['total_amount']; | |
2779 | self::updateFinancialAccounts($params); | |
2780 | } | |
2781 | } | |
6a488035 TO |
2782 | } |
2783 | ||
2784 | if (!$update) { | |
2785 | //records finanical trxn and entity financial trxn | |
2786 | $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams); | |
2787 | $params['entity_id'] = $financialTxn->id; | |
2788 | } | |
e005ab6b PN |
2789 | } |
2790 | // record line items and finacial items | |
2791 | if (!CRM_Utils_Array::value('skipLineItem', $params)) { | |
2792 | CRM_Price_BAO_LineItem::processPriceSet($entityId, CRM_Utils_Array::value('line_item', $params), $params['contribution'], $entityTable, $update); | |
6a488035 TO |
2793 | } |
2794 | ||
2795 | // create batch entry if batch_id is passed | |
2796 | if (CRM_Utils_Array::value('batch_id', $params)) { | |
2797 | $entityParams = array( | |
2798 | 'batch_id' => $params['batch_id'], | |
2799 | 'entity_table' => 'civicrm_financial_trxn', | |
2800 | 'entity_id' => $financialTxn->id, | |
e005ab6b | 2801 | ); |
6a488035 TO |
2802 | CRM_Batch_BAO_Batch::addBatchEntity($entityParams); |
2803 | } | |
2804 | ||
2805 | // when a fee is charged | |
2806 | if (CRM_Utils_Array::value('fee_amount', $params) && (!CRM_Utils_Array::value('prevContribution', $params) | |
2807 | || $params['contribution']->fee_amount != $params['prevContribution']->fee_amount) && $skipRecords) { | |
2808 | CRM_Core_BAO_FinancialTrxn::recordFees($params); | |
2809 | } | |
2810 | ||
2811 | if (CRM_Utils_Array::value('prevContribution', $params) && $entityTable == 'civicrm_participant' | |
2812 | && $params['prevContribution']->contribution_status_id != $params['contribution']->contribution_status_id) { | |
2813 | $eventID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $entityId, 'event_id'); | |
2814 | $feeLevel[] = str_replace('\ 1', '', $params['prevContribution']->amount_level); | |
2815 | CRM_Event_BAO_Participant::createDiscountTrxn($eventID, $params, $feeLevel); | |
2816 | } | |
2817 | unset($params['line_item']); | |
2818 | } | |
2819 | ||
2820 | /** | |
2821 | * Function to update all financial accounts entry | |
2822 | * | |
2823 | * @param array $params contribution object, line item array and params for trxn | |
2824 | * | |
2825 | * @param string $context update scenarios | |
2826 | * | |
2827 | * @access public | |
2828 | * @static | |
2829 | */ | |
2830 | static function updateFinancialAccounts(&$params, $context = NULL, $skipTrxn = NULL) { | |
2831 | $itemAmount = $trxnID = NULL; | |
2832 | //get all the statuses | |
2833 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
2834 | if ($params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus) && | |
2835 | $params['contribution']->contribution_status_id == array_search('Completed', $contributionStatus) | |
2836 | && $context == 'changePaymentInstrument') { | |
2837 | return; | |
2838 | } | |
2839 | if ($context == 'changedAmount' || $context == 'changeFinancialType') { | |
2840 | $itemAmount = $params['trxnParams']['total_amount'] = $params['total_amount'] - $params['prevContribution']->total_amount; | |
2841 | } | |
2842 | if ($context == 'changedStatus') { | |
2843 | //get all the statuses | |
2844 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
2845 | ||
2846 | if ($params['prevContribution']->contribution_status_id == array_search('Completed', $contributionStatus) | |
2847 | && ($params['contribution']->contribution_status_id == array_search('Refunded', $contributionStatus) | |
2848 | || $params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus))) { | |
2849 | ||
2850 | $params['trxnParams']['total_amount'] = - $params['total_amount']; | |
2851 | } | |
bf45dbe8 PN |
2852 | elseif ($params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus) |
2853 | && $params['prevContribution']->is_pay_later) { | |
6a488035 TO |
2854 | $financialTypeID = CRM_Utils_Array::value('financial_type_id', $params) ? $params['financial_type_id'] : $params['prevContribution']->financial_type_id; |
2855 | if ($params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) { | |
2856 | $params['trxnParams']['to_financial_account_id'] = NULL; | |
2857 | $params['trxnParams']['total_amount'] = - $params['total_amount']; | |
2858 | } | |
2859 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, | |
2860 | " AND v.name LIKE 'Accounts Receivable Account is' ")); | |
2861 | $params['trxnParams']['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( | |
2862 | $financialTypeID, $relationTypeId); | |
2863 | } | |
2864 | $itemAmount = $params['trxnParams']['total_amount']; | |
2865 | } | |
2866 | elseif ($context == 'changePaymentInstrument') { | |
2867 | if ($params['prevContribution']->payment_instrument_id != null | |
2868 | && $params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus) | |
2869 | && $params['contribution']->contribution_status_id == array_search('Pending', $contributionStatus)) { | |
2870 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); | |
2871 | $params['trxnParams']['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); | |
2872 | } | |
2873 | elseif ($params['prevContribution']->payment_instrument_id != null) { | |
2874 | $params['trxnParams']['from_financial_account_id'] = | |
2875 | CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount( | |
2876 | $params['prevContribution']->payment_instrument_id); | |
2877 | } | |
2878 | else { | |
2879 | $params['trxnParams']['from_financial_account_id'] = CRM_Core_DAO::singleValueQuery( | |
2880 | "SELECT id FROM civicrm_financial_account WHERE is_default = 1"); | |
2881 | } | |
2882 | } | |
2883 | ||
2884 | $trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']); | |
2885 | $params['entity_id'] = $trxn->id; | |
2886 | ||
2887 | if ($context == 'changedStatus') { | |
2888 | if (($params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus)) && | |
2889 | ($params['contribution']->contribution_status_id == array_search('Completed', $contributionStatus))) { | |
464bb009 PN |
2890 | $query = "UPDATE civicrm_financial_item SET status_id = %1 WHERE entity_id = %2 and entity_table = 'civicrm_line_item'"; |
2891 | $sql = "SELECT id, amount FROM civicrm_financial_item WHERE entity_id = %1 and entity_table = 'civicrm_line_item'"; | |
2892 | ||
2893 | $entityParams = array( | |
2894 | 'entity_table' => 'civicrm_financial_item', | |
2895 | 'financial_trxn_id' => $trxn->id, | |
2896 | ); | |
6a488035 TO |
2897 | foreach ($params['line_item'] as $fieldId => $fields) { |
2898 | foreach ($fields as $fieldValueId => $fieldValues) { | |
2899 | $fparams = array( | |
2900 | 1 => array(CRM_Core_OptionGroup::getValue('financial_item_status', 'Paid', 'name'), 'Integer'), | |
2901 | 2 => array($fieldValues['id'], 'Integer'), | |
2902 | ); | |
6a488035 | 2903 | CRM_Core_DAO::executeQuery($query, $fparams); |
464bb009 PN |
2904 | $fparams = array( |
2905 | 1 => array($fieldValues['id'], 'Integer'), | |
2906 | ); | |
2907 | $financialItem = CRM_Core_DAO::executeQuery($sql, $fparams); | |
2908 | while ($financialItem->fetch()) { | |
2909 | $entityParams['entity_id'] = $financialItem->id; | |
2910 | $entityParams['amount'] = $financialItem->amount; | |
2911 | CRM_Financial_BAO_FinancialItem::createEntityTrxn($entityParams); | |
2912 | } | |
6a488035 TO |
2913 | } |
2914 | } | |
2915 | return; | |
2916 | } | |
2917 | } | |
2918 | if ($context != 'changePaymentInstrument') { | |
2919 | $itemParams['entity_table'] = 'civicrm_line_item'; | |
2920 | $trxnIds['id'] = $params['entity_id']; | |
2921 | foreach ($params['line_item'] as $fieldId => $fields) { | |
2922 | foreach ($fields as $fieldValueId => $fieldValues) { | |
2923 | $prevParams['entity_id'] = $fieldValues['id']; | |
2924 | $prevfinancialItem = CRM_Financial_BAO_FinancialItem::retrieve($prevParams, CRM_Core_DAO::$_nullArray); | |
2925 | ||
2926 | $receiveDate = CRM_Utils_Date::isoToMysql($params['prevContribution']->receive_date); | |
2927 | if ($params['contribution']->receive_date) { | |
2928 | $receiveDate = CRM_Utils_Date::isoToMysql($params['contribution']->receive_date); | |
2929 | } | |
2930 | ||
2931 | $financialAccount = $prevfinancialItem->financial_account_id; | |
2932 | if (CRM_Utils_Array::value('financial_account_id', $params)) { | |
2933 | $financialAccount = $params['financial_account_id']; | |
2934 | } | |
2935 | ||
2936 | $currency = $params['prevContribution']->currency; | |
2937 | if ($params['contribution']->currency) { | |
2938 | $currency = $params['contribution']->currency; | |
2939 | } | |
2940 | if (CRM_Utils_Array::value('is_quick_config', $params)) { | |
2941 | $amount = $itemAmount; | |
2942 | if (!$amount) { | |
2943 | $amount = $params['total_amount']; | |
2944 | } | |
2945 | } | |
2946 | else { | |
2947 | $diff = 1; | |
2948 | if ($context == 'changeFinancialType' || $params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) { | |
2949 | $diff = -1; | |
2950 | } | |
2951 | $amount = $diff * $fieldValues['line_total']; | |
2952 | } | |
2953 | ||
2954 | $itemParams = array( | |
2955 | 'transaction_date' => $receiveDate, | |
2956 | 'contact_id' => $params['prevContribution']->contact_id, | |
2957 | 'currency' => $currency, | |
2958 | 'amount' => $amount, | |
2959 | 'description' => $prevfinancialItem->description, | |
2960 | 'status_id' => $prevfinancialItem->status_id, | |
2961 | 'financial_account_id' => $financialAccount, | |
2962 | 'entity_table' => 'civicrm_line_item', | |
2963 | 'entity_id' => $fieldValues['id'] | |
2964 | ); | |
2965 | CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds); | |
2966 | } | |
2967 | } | |
2968 | } | |
2969 | } | |
2970 | ||
2971 | /** | |
2972 | * Function to check status validation on update of a contribution | |
2973 | * | |
2974 | * @param array $values previous form values before submit | |
2975 | * | |
2976 | * @param array $fields the input form values | |
2977 | * | |
2978 | * @param array $errors list of errors | |
2979 | * | |
2980 | * @access public | |
2981 | * @static | |
2982 | */ | |
2983 | static function checkStatusValidation($values, &$fields, &$errors) { | |
c71ae314 PN |
2984 | if (CRM_Utils_System::isNull($values) && CRM_Utils_Array::value('id', $fields)) { |
2985 | $values['contribution_status_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $fields['id'], 'contribution_status_id'); | |
2986 | if ($values['contribution_status_id'] == $fields['contribution_status_id']) { | |
2987 | return FALSE; | |
2988 | } | |
2989 | } | |
6a488035 TO |
2990 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
2991 | $checkStatus = array( | |
2992 | 'Cancelled' => array('Completed', 'Refunded'), | |
2993 | 'Completed' => array('Cancelled', 'Refunded'), | |
2994 | 'Pending' => array('Cancelled', 'Completed', 'Failed'), | |
2995 | 'Refunded' => array('Cancelled', 'Completed') | |
2996 | ); | |
2997 | ||
2998 | if (!in_array($contributionStatuses[$fields['contribution_status_id']], $checkStatus[$contributionStatuses[$values['contribution_status_id']]])) { | |
2999 | $errors['contribution_status_id'] = ts("Cannot change contribution status from %1 to %2.", array(1 => $contributionStatuses[$values['contribution_status_id']], 2 => $contributionStatuses[$fields['contribution_status_id']])); | |
3000 | } | |
3001 | } | |
c3d24ba7 PN |
3002 | |
3003 | /** | |
3004 | * Function to delete contribution of contact | |
3005 | * | |
3006 | * CRM-12155 | |
3007 | * | |
3008 | * @param integer $contactId contact id | |
3009 | * | |
3010 | * @access public | |
3011 | * @static | |
3012 | */ | |
3013 | static function deleteContactContribution($contactId) { | |
3014 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
3015 | $contribution->contact_id = $contactId; | |
3016 | $contribution->find(); | |
3017 | while ($contribution->fetch()) { | |
3018 | self::deleteContribution($contribution->id); | |
3019 | } | |
3020 | } | |
3b67ab13 PN |
3021 | /** |
3022 | * Function to validate financial type | |
3023 | * | |
3024 | * CRM-13231 | |
3025 | * | |
3026 | * @param integer $financialTypeId Financial Type id | |
3027 | * | |
3028 | * @access public | |
3029 | * @static | |
3030 | */ | |
3031 | static function validateFinancialType($financialTypeId, $relationName = 'Expense Account is') { | |
3032 | $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '{$relationName}' ")); | |
3033 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); | |
3034 | ||
3035 | if (!$financialAccount) { | |
3036 | return CRM_Contribute_PseudoConstant::financialType($financialTypeId); | |
3037 | } | |
3038 | return FALSE; | |
3039 | } | |
6a488035 | 3040 | } |