3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Contribute_BAO_ContributionSoft
extends CRM_Contribute_DAO_ContributionSoft
{
38 public function __construct() {
39 parent
::__construct();
43 * Add contribution soft credit record.
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
49 * soft contribution of object that is added
51 public static function add(&$params) {
52 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
53 $contributionSoft->copyValues($params);
55 // set currency for CRM-1496
56 if (!isset($contributionSoft->currency
)) {
57 $config = CRM_Core_Config
::singleton();
58 $contributionSoft->currency
= $config->defaultCurrency
;
60 return $contributionSoft->save();
64 * Process the soft contribution and/or link to personal campaign page.
66 * @param array $params
67 * @param object $contribution CRM_Contribute_DAO_Contribution
70 public static function processSoftContribution($params, $contribution) {
71 //retrieve existing soft-credit and pcp id(s) if any against $contribution
72 $softIDs = self
::getSoftCreditIds($contribution->id
);
73 $pcpId = self
::getSoftCreditIds($contribution->id
, TRUE);
75 if ($pcp = CRM_Utils_Array
::value('pcp', $params)) {
76 $softParams = array();
77 $softParams['id'] = $pcpId ?
$pcpId : NULL;
78 $softParams['contribution_id'] = $contribution->id
;
79 $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
80 $softParams['contact_id'] = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP',
81 $pcp['pcp_made_through_id'], 'contact_id'
83 $softParams['currency'] = $contribution->currency
;
84 $softParams['amount'] = $contribution->total_amount
;
85 $softParams['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $pcp);
86 $softParams['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $pcp);
87 $softParams['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $pcp);
88 $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp');
89 $contributionSoft = self
::add($softParams);
90 //Send notification to owner for PCP
91 if ($contributionSoft->pcp_id
&& empty($pcpId)) {
92 CRM_Contribute_Form_Contribution_Confirm
::pcpNotifyOwner($contribution, $contributionSoft);
95 //Delete PCP against this contribution and create new on submitted PCP information
96 elseif (array_key_exists('pcp', $params) && $pcpId) {
97 civicrm_api3('ContributionSoft', 'delete', array('id' => $pcpId));
99 if (isset($params['soft_credit'])) {
100 $softParams = $params['soft_credit'];
101 foreach ($softParams as $softParam) {
102 if (!empty($softIDs)) {
103 $key = key($softIDs);
104 $softParam['id'] = $softIDs[$key];
105 unset($softIDs[$key]);
107 $softParam['contribution_id'] = $contribution->id
;
108 $softParam['currency'] = $contribution->currency
;
109 //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default
110 if (empty($softParam['amount'])) {
111 $softParam['amount'] = $contribution->total_amount
;
113 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
116 // delete any extra soft-credit while updating back-office contribution
117 foreach ((array) $softIDs as $softID) {
118 if (!in_array($softID, $params['soft_credit_ids'])) {
119 civicrm_api3('ContributionSoft', 'delete', array('id' => $softID));
126 * Function used to save pcp / soft credit entry.
128 * This is used by contribution and also event pcps
130 * @param array $params
131 * @param object $form
134 public static function formatSoftCreditParams(&$params, &$form) {
135 $pcp = $softParams = $softIDs = array();
136 if (!empty($params['pcp_made_through_id'])) {
138 'pcp_made_through_id',
139 'pcp_display_in_roll',
143 foreach ($fields as $f) {
144 $pcp[$f] = CRM_Utils_Array
::value($f, $params);
148 if (!empty($form->_values
['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) {
151 // @todo fix use of deprecated function.
152 $contributionSoftParams['soft_credit_type_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp');
153 //check if there is any duplicate contact
154 // honoree should never be the donor
157 'onbehalf_contact_id' => 0,
159 $except = array_values(array_intersect_key($params, $exceptKeys));
160 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts(
162 CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']),
168 $honorId = CRM_Utils_Array
::value(0, $ids);
172 $honorId = CRM_Contact_BAO_Contact
::createProfileContact(
173 $params['honor'], $null,
175 $form->_values
['honoree_profile_id']
177 $softParams[] = array(
178 'contact_id' => $honorId,
179 'soft_credit_type_id' => $params['soft_credit_type_id'],
182 if (CRM_Utils_Array
::value('is_email_receipt', $form->_values
)) {
183 $form->_values
['honor'] = array(
184 'soft_credit_type' => CRM_Utils_Array
::value(
185 $params['soft_credit_type_id'],
186 CRM_Core_OptionGroup
::values("soft_credit_type")
188 'honor_id' => $honorId,
189 'honor_profile_id' => $form->_values
['honoree_profile_id'],
190 'honor_profile_values' => $params['honor'],
194 elseif (!empty($params['soft_credit_contact_id'])) {
195 //build soft credit params
196 foreach ($params['soft_credit_contact_id'] as $key => $val) {
197 if ($val && $params['soft_credit_amount'][$key]) {
198 $softParams[$key]['contact_id'] = $val;
199 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
200 $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
201 if (!empty($params['soft_credit_id'][$key])) {
202 $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key];
208 $params['pcp'] = !empty($pcp) ?
$pcp : NULL;
209 $params['soft_credit'] = $softParams;
210 $params['soft_credit_ids'] = $softIDs;
214 * Fetch object based on array of properties.
216 * @param array $params
217 * (reference ) an assoc array of name/value pairs.
218 * @param array $defaults
219 * (reference ) an assoc array to hold the flattened values.
221 * @return CRM_Contribute_BAO_ContributionSoft
223 public static function retrieve(&$params, &$defaults) {
224 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
225 $contributionSoft->copyValues($params);
226 if ($contributionSoft->find(TRUE)) {
227 CRM_Core_DAO
::storeValues($contributionSoft, $defaults);
228 return $contributionSoft;
234 * @param int $contact_id
239 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
241 $whereClause = "AND cc.cancel_date IS NULL";
244 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
245 FROM civicrm_contribution_soft ccs
246 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
247 WHERE cc.is_test = %2 AND ccs.contact_id = %1 {$whereClause}
251 1 => array($contact_id, 'Integer'),
252 2 => array($isTest, 'Integer'),
255 $cs = CRM_Core_DAO
::executeQuery($query, $params);
257 $count = $countCancelled = 0;
258 $amount = $average = $cancelAmount = array();
260 while ($cs->fetch()) {
261 if ($cs->amount
> 0) {
263 $amount[] = CRM_Utils_Money
::format($cs->amount
, $cs->currency
);
264 $average[] = CRM_Utils_Money
::format($cs->average
, $cs->currency
);
268 //to get cancel amount
269 $cancelAmountWhereClause = "AND cc.cancel_date IS NOT NULL";
270 $query = str_replace($whereClause, $cancelAmountWhereClause, $query);
271 $cancelAmountSQL = CRM_Core_DAO
::executeQuery($query, $params);
272 while ($cancelAmountSQL->fetch()) {
273 if ($cancelAmountSQL->amount
> 0) {
275 $cancelAmount[] = CRM_Utils_Money
::format($cancelAmountSQL->amount
, $cancelAmountSQL->currency
);
279 if ($count > 0 ||
$countCancelled > 0) {
283 implode(', ', $amount),
284 implode(', ', $average),
285 implode(', ', $cancelAmount),
292 * Retrieve soft contributions for contribution record.
294 * @param int $contributionID
299 * Array of soft contribution ids, amounts, and associated contact ids
301 public static function getSoftContribution($contributionID, $all = FALSE) {
305 'pcp_display_in_roll',
311 SELECT ccs.id, pcp_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
312 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
313 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
314 WHERE contribution_id = %1;
317 $params = array(1 => array($contributionID, 'Integer'));
319 $dao = CRM_Core_DAO
::executeQuery($query, $params);
321 $softContribution = array();
323 while ($dao->fetch()) {
326 foreach ($pcpFields as $val) {
327 $softContribution[$val] = $dao->$val;
329 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name
;
330 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id
;
334 $softContribution['soft_credit'][$count] = array(
335 'contact_id' => $dao->contact_id
,
336 'soft_credit_id' => $dao->id
,
337 'currency' => $dao->currency
,
338 'amount' => $dao->amount
,
339 'contact_name' => $dao->display_name
,
340 'soft_credit_type' => $dao->soft_credit_type_id
,
341 'soft_credit_type_label' => CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $dao->soft_credit_type_id
),
347 return $softContribution;
351 * @param int $contributionID
356 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
359 FROM civicrm_contribution_soft
360 WHERE contribution_id = %1
364 $query .= " AND pcp_id IS NOT NULL";
367 $query .= " AND pcp_id IS NULL";
369 $params = array(1 => array($contributionID, 'Integer'));
371 $dao = CRM_Core_DAO
::executeQuery($query, $params);
374 while ($dao->fetch()) {
384 * Wrapper for ajax soft contribution selector.
386 * @param array $params
387 * Associated array for params.
390 * Associated array of soft contributions
392 public static function getSoftContributionSelector($params) {
394 if (!empty($params['isTest'])) {
395 $isTest = $params['isTest'];
397 // Format the params.
398 $params['offset'] = ($params['page'] - 1) * $params['rp'];
399 $params['rowCount'] = $params['rp'];
400 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
401 $contactId = $params['cid'];
404 if ($params['context'] == 'membership' && !empty($params['entityID']) && $contactId) {
405 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$params['entityID']})";
408 $softCreditList = self
::getSoftContributionList($contactId, $filter, $isTest, $params);
410 $softCreditListDT = array();
411 $softCreditListDT['data'] = array_values($softCreditList);
412 $softCreditListDT['recordsTotal'] = $params['total'];
413 $softCreditListDT['recordsFiltered'] = $params['total'];
415 return $softCreditListDT;
419 * Function to retrieve the list of soft contributions for given contact.
421 * @param int $contact_id
423 * @param string $filter
425 * Additional filter criteria, later used in where clause.
426 * @param array $dTParams
430 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0, &$dTParams = NULL) {
431 $config = CRM_Core_Config
::singleton();
433 CRM_Core_Action
::VIEW
=> array(
434 'name' => ts('View'),
435 'url' => 'civicrm/contact/view/contribution',
436 'qs' => 'reset=1&id=%%contributionid%%&cid=%%contactId%%&action=view&context=contribution&selectedChild=contribute',
437 'title' => ts('View related contribution'),
440 $orderBy = 'cc.receive_date DESC';
441 if (!empty($dTParams['sort'])) {
442 $orderBy = $dTParams['sort'];
445 if (!empty($dTParams['rowCount']) && $dTParams['rowCount'] > 0) {
446 $limit = " LIMIT {$dTParams['offset']}, {$dTParams['rowCount']} ";
448 $softOgId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'soft_credit_type', 'id', 'name');
449 $statusOgId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'contribution_status', 'id', 'name');
452 SELECT SQL_CALC_FOUND_ROWS ccs.id, ccs.amount as amount,
455 ccs.pcp_display_in_roll,
456 ccs.pcp_roll_nickname,
457 ccs.pcp_personal_note,
458 ccs.soft_credit_type_id,
459 sov.label as sct_label,
461 cc.contact_id as contributor_id,
462 cc.contribution_status_id as contribution_status_id,
463 cov.label as contribution_status,
464 cp.title as pcp_title,
466 contact.display_name as contributor_name,
467 cct.name as financial_type
468 FROM civicrm_contribution_soft ccs
469 LEFT JOIN civicrm_contribution cc
470 ON ccs.contribution_id = cc.id
471 LEFT JOIN civicrm_pcp cp
472 ON ccs.pcp_id = cp.id
473 LEFT JOIN civicrm_contact contact ON
474 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
475 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
476 LEFT JOIN civicrm_option_value sov ON sov.option_group_id = %3 AND ccs.soft_credit_type_id = sov.value
477 LEFT JOIN civicrm_option_value cov ON cov.option_group_id = %4 AND cc.contribution_status_id = cov.value
481 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
486 $query .= "{$where} ORDER BY {$orderBy} {$limit}";
489 1 => array($contact_id, 'Integer'),
490 2 => array($isTest, 'Integer'),
491 3 => array($softOgId, 'Integer'),
492 4 => array($statusOgId, 'Integer'),
494 $cs = CRM_Core_DAO
::executeQuery($query, $params);
496 $dTParams['total'] = CRM_Core_DAO
::singleValueQuery('SELECT FOUND_ROWS()');
498 while ($cs->fetch()) {
499 $result[$cs->id
]['amount'] = CRM_Utils_Money
::format($cs->amount
, $cs->currency
);
500 $result[$cs->id
]['currency'] = $cs->currency
;
501 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
502 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
503 $result[$cs->id
]['contributor_name'] = CRM_Utils_System
::href(
504 $cs->contributor_name
,
505 'civicrm/contact/view',
506 "reset=1&cid={$cs->contributor_id}"
508 $result[$cs->id
]['financial_type'] = $cs->financial_type
;
509 $result[$cs->id
]['receive_date'] = CRM_Utils_Date
::customFormat($cs->receive_date
, $config->dateformatDatetime
);
510 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
511 $result[$cs->id
]['pcp_title'] = ($cs->pcp_title
) ?
$cs->pcp_title
: 'n/a';
512 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
513 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
514 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
515 $result[$cs->id
]['contribution_status'] = $cs->contribution_status
;
516 $result[$cs->id
]['sct_label'] = $cs->sct_label
;
518 'contributionid' => $cs->contribution_id
,
519 'contactId' => $cs->contributor_id
,
521 $result[$cs->id
]['links'] = CRM_Core_Action
::formLink($links, NULL, $replace);
524 $result[$cs->id
]['contribution_status'] = CRM_Core_TestEntity
::appendTestText($result[$cs->id
]['contribution_status']);
531 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
532 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
535 * @param CRM_Core_Form $form
536 * @param array $params
537 * @param int $honorId
539 public static function formatHonoreeProfileFields($form, $params, $honorId = NULL) {
540 if (empty($form->_values
['honoree_profile_id'])) {
543 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']);
544 $profileFields = CRM_Core_BAO_UFGroup
::getFields($form->_values
['honoree_profile_id']);
545 $honoreeProfileFields = $values = array();
549 CRM_Core_BAO_UFGroup
::getValues($honorId, $profileFields, $values, FALSE, $params);
550 if (empty($params)) {
551 foreach ($profileFields as $name => $field) {
552 $title = $field['title'];
553 $params[$field['name']] = $values[$title];
558 //remove name related fields and construct name string with prefix/suffix
559 //which will be later assigned to template
560 switch ($profileContactType) {
562 if (array_key_exists('prefix_id', $params)) {
563 $honorName = CRM_Utils_Array
::value(CRM_Utils_Array
::value('prefix_id', $params),
564 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
566 unset($profileFields['prefix_id']);
568 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
569 unset($profileFields['first_name']);
570 unset($profileFields['last_name']);
571 if (array_key_exists('suffix_id', $params)) {
572 $honorName .= ' ' . CRM_Utils_Array
::value(CRM_Utils_Array
::value('suffix_id', $params),
573 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
575 unset($profileFields['suffix_id']);
580 $honorName = $params['organization_name'];
581 unset($profileFields['organization_name']);
585 $honorName = $params['household_name'];
586 unset($profileFields['household_name']);
591 $honoreeProfileFields['Name'] = $honorName;
592 foreach ($profileFields as $name => $field) {
593 $title = $field['title'];
594 $honoreeProfileFields[$title] = $values[$title];
596 $form->assign('honoreeProfile', $honoreeProfileFields);
599 $form->assign('honorName', $honorName);