3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contribute_BAO_ContributionSoft
extends CRM_Contribute_DAO_ContributionSoft
{
22 public function __construct() {
23 parent
::__construct();
27 * Add contribution soft credit record.
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
33 * soft contribution of object that is added
35 public static function add(&$params) {
36 $hook = empty($params['id']) ?
'create' : 'edit';
37 CRM_Utils_Hook
::pre($hook, 'ContributionSoft', CRM_Utils_Array
::value('id', $params), $params);
39 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
40 $contributionSoft->copyValues($params);
42 // set currency for CRM-1496
43 if (!isset($contributionSoft->currency
)) {
44 $config = CRM_Core_Config
::singleton();
45 $contributionSoft->currency
= $config->defaultCurrency
;
47 $result = $contributionSoft->save();
48 CRM_Utils_Hook
::post($hook, 'ContributionSoft', $contributionSoft->id
, $contributionSoft);
53 * Process the soft contribution and/or link to personal campaign page.
55 * @param array $params
56 * @param object $contribution CRM_Contribute_DAO_Contribution
59 public static function processSoftContribution($params, $contribution) {
60 //retrieve existing soft-credit and pcp id(s) if any against $contribution
61 $softIDs = self
::getSoftCreditIds($contribution->id
);
62 $pcpId = self
::getSoftCreditIds($contribution->id
, TRUE);
64 if ($pcp = CRM_Utils_Array
::value('pcp', $params)) {
66 $softParams['id'] = $pcpId ?
$pcpId : NULL;
67 $softParams['contribution_id'] = $contribution->id
;
68 $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
69 $softParams['contact_id'] = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP',
70 $pcp['pcp_made_through_id'], 'contact_id'
72 $softParams['currency'] = $contribution->currency
;
73 $softParams['amount'] = $contribution->total_amount
;
74 $softParams['pcp_display_in_roll'] = $pcp['pcp_display_in_roll'] ??
NULL;
75 $softParams['pcp_roll_nickname'] = $pcp['pcp_roll_nickname'] ??
NULL;
76 $softParams['pcp_personal_note'] = $pcp['pcp_personal_note'] ??
NULL;
77 $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp');
78 $contributionSoft = self
::add($softParams);
79 //Send notification to owner for PCP
80 if ($contributionSoft->pcp_id
&& empty($pcpId)) {
81 CRM_Contribute_Form_Contribution_Confirm
::pcpNotifyOwner($contribution, $contributionSoft);
84 //Delete PCP against this contribution and create new on submitted PCP information
85 elseif (array_key_exists('pcp', $params) && $pcpId) {
86 civicrm_api3('ContributionSoft', 'delete', ['id' => $pcpId]);
88 if (isset($params['soft_credit'])) {
89 $softParams = $params['soft_credit'];
90 foreach ($softParams as $softParam) {
91 if (!empty($softIDs)) {
93 $softParam['id'] = $softIDs[$key];
94 unset($softIDs[$key]);
96 $softParam['contribution_id'] = $contribution->id
;
97 $softParam['currency'] = $contribution->currency
;
98 //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default
99 if (empty($softParam['amount'])) {
100 $softParam['amount'] = $contribution->total_amount
;
102 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
105 // delete any extra soft-credit while updating back-office contribution
106 foreach ((array) $softIDs as $softID) {
107 if (!in_array($softID, $params['soft_credit_ids'])) {
108 civicrm_api3('ContributionSoft', 'delete', ['id' => $softID]);
115 * Function used to save pcp / soft credit entry.
117 * This is used by contribution and also event pcps
119 * @param array $params
120 * @param object $form
123 public static function formatSoftCreditParams(&$params, &$form) {
124 $pcp = $softParams = $softIDs = [];
125 if (!empty($params['pcp_made_through_id'])) {
127 'pcp_made_through_id',
128 'pcp_display_in_roll',
132 foreach ($fields as $f) {
133 $pcp[$f] = $params[$f] ??
NULL;
137 if (!empty($form->_values
['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) {
140 // @todo fix use of deprecated function.
141 $contributionSoftParams['soft_credit_type_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp');
142 //check if there is any duplicate contact
143 // honoree should never be the donor
146 'onbehalf_contact_id' => 0,
148 $except = array_values(array_intersect_key($params, $exceptKeys));
149 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts(
151 CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']),
157 $honorId = $ids[0] ??
NULL;
161 $honorId = CRM_Contact_BAO_Contact
::createProfileContact(
162 $params['honor'], $null,
164 $form->_values
['honoree_profile_id']
167 'contact_id' => $honorId,
168 'soft_credit_type_id' => $params['soft_credit_type_id'],
171 if (!empty($form->_values
['is_email_receipt'])) {
172 $form->_values
['honor'] = [
173 'soft_credit_type' => CRM_Utils_Array
::value(
174 $params['soft_credit_type_id'],
175 CRM_Core_OptionGroup
::values("soft_credit_type")
177 'honor_id' => $honorId,
178 'honor_profile_id' => $form->_values
['honoree_profile_id'],
179 'honor_profile_values' => $params['honor'],
183 elseif (!empty($params['soft_credit_contact_id'])) {
184 //build soft credit params
185 foreach ($params['soft_credit_contact_id'] as $key => $val) {
186 if ($val && $params['soft_credit_amount'][$key]) {
187 $softParams[$key]['contact_id'] = $val;
188 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
189 $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
190 if (!empty($params['soft_credit_id'][$key])) {
191 $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key];
197 $params['pcp'] = !empty($pcp) ?
$pcp : NULL;
198 $params['soft_credit'] = $softParams;
199 $params['soft_credit_ids'] = $softIDs;
203 * Fetch object based on array of properties.
205 * @param array $params
206 * (reference ) an assoc array of name/value pairs.
207 * @param array $defaults
208 * (reference ) an assoc array to hold the flattened values.
210 * @return CRM_Contribute_BAO_ContributionSoft
212 public static function retrieve(&$params, &$defaults) {
213 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
214 $contributionSoft->copyValues($params);
215 if ($contributionSoft->find(TRUE)) {
216 CRM_Core_DAO
::storeValues($contributionSoft, $defaults);
217 return $contributionSoft;
223 * @param int $contact_id
228 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
230 $whereClause = "AND cc.cancel_date IS NULL";
233 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
234 FROM civicrm_contribution_soft ccs
235 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
236 WHERE cc.is_test = %2 AND ccs.contact_id = %1 {$whereClause}
240 1 => [$contact_id, 'Integer'],
241 2 => [$isTest, 'Integer'],
244 $cs = CRM_Core_DAO
::executeQuery($query, $params);
246 $count = $countCancelled = 0;
247 $amount = $average = $cancelAmount = [];
249 while ($cs->fetch()) {
250 if ($cs->amount
> 0) {
252 $amount[] = CRM_Utils_Money
::format($cs->amount
, $cs->currency
);
253 $average[] = CRM_Utils_Money
::format($cs->average
, $cs->currency
);
257 //to get cancel amount
258 $cancelAmountWhereClause = "AND cc.cancel_date IS NOT NULL";
259 $query = str_replace($whereClause, $cancelAmountWhereClause, $query);
260 $cancelAmountSQL = CRM_Core_DAO
::executeQuery($query, $params);
261 while ($cancelAmountSQL->fetch()) {
262 if ($cancelAmountSQL->amount
> 0) {
264 $cancelAmount[] = CRM_Utils_Money
::format($cancelAmountSQL->amount
, $cancelAmountSQL->currency
);
268 if ($count > 0 ||
$countCancelled > 0) {
272 implode(', ', $amount),
273 implode(', ', $average),
274 implode(', ', $cancelAmount),
281 * Retrieve soft contributions for contribution record.
283 * @param int $contributionID
288 * Array of soft contribution ids, amounts, and associated contact ids
290 public static function getSoftContribution($contributionID, $all = FALSE) {
291 $softContributionFields = self
::getSoftCreditContributionFields([$contributionID], $all);
292 return $softContributionFields[$contributionID] ??
[];
296 * Retrieve soft contributions for an array of contribution records.
298 * @param array $contributionIDs
303 * Array of soft contribution ids, amounts, and associated contact ids
305 public static function getSoftCreditContributionFields($contributionIDs, $all = FALSE) {
309 'pcp_display_in_roll',
315 SELECT ccs.id, pcp_id, ccs.contribution_id as contribution_id, cpcp.title as pcp_title, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, ccs.currency as currency, amount, ccs.contact_id as contact_id, c.display_name, ccs.soft_credit_type_id
316 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
317 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
318 WHERE contribution_id IN (%1)
320 $queryParams = [1 => [implode(',', $contributionIDs), 'CommaSeparatedIntegers']];
321 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
323 $softContribution = $indexes = [];
324 while ($dao->fetch()) {
327 foreach ($pcpFields as $val) {
328 $softContribution[$dao->contribution_id
][$val] = $dao->$val;
330 $softContribution[$dao->contribution_id
]['pcp_soft_credit_to_name'] = $dao->display_name
;
331 $softContribution[$dao->contribution_id
]['pcp_soft_credit_to_id'] = $dao->contact_id
;
335 // Use a 1-based array because that's what this function returned before refactoring in https://github.com/civicrm/civicrm-core/pull/14747
336 $indexes[$dao->contribution_id
] = isset($indexes[$dao->contribution_id
]) ?
$indexes[$dao->contribution_id
] +
1 : 1;
337 $softContribution[$dao->contribution_id
]['soft_credit'][$indexes[$dao->contribution_id
]] = [
338 'contact_id' => $dao->contact_id
,
339 'soft_credit_id' => $dao->id
,
340 'currency' => $dao->currency
,
341 'amount' => $dao->amount
,
342 'contact_name' => $dao->display_name
,
343 'soft_credit_type' => $dao->soft_credit_type_id
,
344 'soft_credit_type_label' => CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $dao->soft_credit_type_id
),
349 return $softContribution;
353 * @param int $contributionID
358 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
361 FROM civicrm_contribution_soft
362 WHERE contribution_id = %1
366 $query .= " AND pcp_id IS NOT NULL";
369 $query .= " AND pcp_id IS NULL";
371 $params = [1 => [$contributionID, 'Integer']];
373 $dao = CRM_Core_DAO
::executeQuery($query, $params);
376 while ($dao->fetch()) {
386 * Wrapper for ajax soft contribution selector.
388 * @param array $params
389 * Associated array for params.
392 * Associated array of soft contributions
394 public static function getSoftContributionSelector($params) {
396 if (!empty($params['isTest'])) {
397 $isTest = $params['isTest'];
399 // Format the params.
400 $params['offset'] = ($params['page'] - 1) * $params['rp'];
401 $params['rowCount'] = $params['rp'];
402 $params['sort'] = $params['sortBy'] ??
NULL;
403 $contactId = $params['cid'];
406 if ($params['context'] == 'membership' && !empty($params['entityID']) && $contactId) {
407 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$params['entityID']})";
410 $softCreditList = self
::getSoftContributionList($contactId, $filter, $isTest, $params);
412 $softCreditListDT = [];
413 $softCreditListDT['data'] = array_values($softCreditList);
414 $softCreditListDT['recordsTotal'] = $params['total'];
415 $softCreditListDT['recordsFiltered'] = $params['total'];
417 return $softCreditListDT;
421 * Function to retrieve the list of soft contributions for given contact.
423 * @param int $contact_id
425 * @param string $filter
427 * Additional filter criteria, later used in where clause.
428 * @param array $dTParams
432 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0, &$dTParams = NULL) {
433 $config = CRM_Core_Config
::singleton();
435 CRM_Core_Action
::VIEW
=> [
436 'name' => ts('View'),
437 'url' => 'civicrm/contact/view/contribution',
438 'qs' => 'reset=1&id=%%contributionid%%&cid=%%contactId%%&action=view&context=contribution&selectedChild=contribute',
439 'title' => ts('View related contribution'),
442 $orderBy = 'cc.receive_date DESC';
443 if (!empty($dTParams['sort'])) {
444 $orderBy = $dTParams['sort'];
447 if (!empty($dTParams['rowCount']) && $dTParams['rowCount'] > 0) {
448 $limit = " LIMIT {$dTParams['offset']}, {$dTParams['rowCount']} ";
450 $softOgId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'soft_credit_type', 'id', 'name');
451 $statusOgId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'contribution_status', 'id', 'name');
454 SELECT SQL_CALC_FOUND_ROWS ccs.id, ccs.amount as amount,
457 ccs.pcp_display_in_roll,
458 ccs.pcp_roll_nickname,
459 ccs.pcp_personal_note,
460 ccs.soft_credit_type_id,
461 sov.label as sct_label,
463 cc.contact_id as contributor_id,
464 cc.contribution_status_id as contribution_status_id,
465 cov.label as contribution_status,
466 cp.title as pcp_title,
468 contact.display_name as contributor_name,
469 cct.name as financial_type
470 FROM civicrm_contribution_soft ccs
471 LEFT JOIN civicrm_contribution cc
472 ON ccs.contribution_id = cc.id
473 LEFT JOIN civicrm_pcp cp
474 ON ccs.pcp_id = cp.id
475 LEFT JOIN civicrm_contact contact ON
476 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
477 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
478 LEFT JOIN civicrm_option_value sov ON sov.option_group_id = %3 AND ccs.soft_credit_type_id = sov.value
479 LEFT JOIN civicrm_option_value cov ON cov.option_group_id = %4 AND cc.contribution_status_id = cov.value
483 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
488 $query .= "{$where} ORDER BY {$orderBy} {$limit}";
491 1 => [$contact_id, 'Integer'],
492 2 => [$isTest, 'Integer'],
493 3 => [$softOgId, 'Integer'],
494 4 => [$statusOgId, 'Integer'],
496 $cs = CRM_Core_DAO
::executeQuery($query, $params);
498 $dTParams['total'] = CRM_Core_DAO
::singleValueQuery('SELECT FOUND_ROWS()');
500 while ($cs->fetch()) {
501 $result[$cs->id
]['amount'] = CRM_Utils_Money
::format($cs->amount
, $cs->currency
);
502 $result[$cs->id
]['currency'] = $cs->currency
;
503 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
504 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
505 $result[$cs->id
]['contributor_name'] = CRM_Utils_System
::href(
506 $cs->contributor_name
,
507 'civicrm/contact/view',
508 "reset=1&cid={$cs->contributor_id}"
510 $result[$cs->id
]['financial_type'] = $cs->financial_type
;
511 $result[$cs->id
]['receive_date'] = CRM_Utils_Date
::customFormat($cs->receive_date
, $config->dateformatDatetime
);
512 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
513 $result[$cs->id
]['pcp_title'] = ($cs->pcp_title
) ?
$cs->pcp_title
: 'n/a';
514 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
515 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
516 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
517 $result[$cs->id
]['contribution_status'] = $cs->contribution_status
;
518 $result[$cs->id
]['sct_label'] = $cs->sct_label
;
520 'contributionid' => $cs->contribution_id
,
521 'contactId' => $cs->contributor_id
,
523 $result[$cs->id
]['links'] = CRM_Core_Action
::formLink($links, NULL, $replace);
526 $result[$cs->id
]['contribution_status'] = CRM_Core_TestEntity
::appendTestText($result[$cs->id
]['contribution_status']);
533 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
534 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
537 * @param CRM_Core_Form $form
538 * @param array $params
539 * @param int $honorId
541 public static function formatHonoreeProfileFields($form, $params, $honorId = NULL) {
542 if (empty($form->_values
['honoree_profile_id'])) {
545 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']);
546 $profileFields = CRM_Core_BAO_UFGroup
::getFields($form->_values
['honoree_profile_id']);
547 $honoreeProfileFields = $values = [];
551 CRM_Core_BAO_UFGroup
::getValues($honorId, $profileFields, $values, FALSE, $params);
552 if (empty($params)) {
553 foreach ($profileFields as $name => $field) {
554 $title = $field['title'];
555 $params[$field['name']] = $values[$title];
560 //remove name related fields and construct name string with prefix/suffix
561 //which will be later assigned to template
562 switch ($profileContactType) {
564 if (array_key_exists('prefix_id', $params)) {
565 $honorName = CRM_Utils_Array
::value($params['prefix_id'],
566 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
568 unset($profileFields['prefix_id']);
570 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
571 unset($profileFields['first_name']);
572 unset($profileFields['last_name']);
573 if (array_key_exists('suffix_id', $params)) {
574 $honorName .= ' ' . CRM_Utils_Array
::value($params['suffix_id'],
575 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
577 unset($profileFields['suffix_id']);
582 $honorName = $params['organization_name'];
583 unset($profileFields['organization_name']);
587 $honorName = $params['household_name'];
588 unset($profileFields['household_name']);
593 $honoreeProfileFields['Name'] = $honorName;
594 foreach ($profileFields as $name => $field) {
595 $title = $field['title'];
596 $honoreeProfileFields[$title] = $values[$title];
598 $form->assign('honoreeProfile', $honoreeProfileFields);
601 $form->assign('honorName', $honorName);