3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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_OptionGroup
::getValue('soft_credit_type', 'pcp', 'name');
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 $deleteParams = array('id' => $pcpId);
98 CRM_Contribute_BAO_ContributionSoft
::del($deleteParams);
100 if (isset($params['soft_credit'])) {
101 $softParams = $params['soft_credit'];
102 foreach ($softParams as $softParam) {
103 if (!empty($softIDs)) {
104 $key = key($softIDs);
105 $softParam['id'] = $softIDs[$key];
106 unset($softIDs[$key]);
108 $softParam['contribution_id'] = $contribution->id
;
109 $softParam['currency'] = $contribution->currency
;
110 //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default
111 if (empty($softParam['amount'])) {
112 $softParam['amount'] = $contribution->total_amount
;
114 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
117 // delete any extra soft-credit while updating back-office contribution
118 foreach ((array) $softIDs as $softID) {
119 if (!in_array($softID, $params['soft_credit_ids'])) {
120 $deleteParams = array('id' => $softID);
121 CRM_Contribute_BAO_ContributionSoft
::del($deleteParams);
128 * Function used to save pcp / soft credit entry.
130 * This is used by contribution and also event pcps
132 * @param array $params
133 * @param object $form
136 public static function formatSoftCreditParams(&$params, &$form) {
137 $pcp = $softParams = $softIDs = array();
138 if (!empty($params['pcp_made_through_id'])) {
140 'pcp_made_through_id',
141 'pcp_display_in_roll',
145 foreach ($fields as $f) {
146 $pcp[$f] = CRM_Utils_Array
::value($f, $params);
150 if (!empty($form->_values
['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) {
153 $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup
::getValue('soft_credit_type', 'pcp', 'name');
154 //check if there is any duplicate contact
155 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']);
156 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params['honor'], $profileContactType);
157 $dedupeParams['check_permission'] = FALSE;
158 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $profileContactType);
160 $honorId = CRM_Utils_Array
::value(0, $ids);
163 $honorId = CRM_Contact_BAO_Contact
::createProfileContact(
164 $params['honor'], CRM_Core_DAO
::$_nullArray,
166 $form->_values
['honoree_profile_id']
168 $softParams[] = array(
169 'contact_id' => $honorId,
170 'soft_credit_type_id' => $params['soft_credit_type_id'],
173 if (CRM_Utils_Array
::value('is_email_receipt', $form->_values
)) {
174 $form->_values
['honor'] = array(
175 'soft_credit_type' => CRM_Utils_Array
::value(
176 $params['soft_credit_type_id'],
177 CRM_Core_OptionGroup
::values("soft_credit_type")
179 'honor_id' => $honorId,
180 'honor_profile_id' => $form->_values
['honoree_profile_id'],
181 'honor_profile_values' => $params['honor'],
185 elseif (!empty($params['soft_credit_contact_id'])) {
186 //build soft credit params
187 foreach ($params['soft_credit_contact_id'] as $key => $val) {
188 if ($val && $params['soft_credit_amount'][$key]) {
189 $softParams[$key]['contact_id'] = $val;
190 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
191 $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
192 if (!empty($params['soft_credit_id'][$key])) {
193 $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key];
199 $params['pcp'] = !empty($pcp) ?
$pcp : NULL;
200 $params['soft_credit'] = $softParams;
201 $params['soft_credit_ids'] = $softIDs;
205 * Fetch object based on array of properties.
207 * @param array $params
208 * (reference ) an assoc array of name/value pairs.
209 * @param array $defaults
210 * (reference ) an assoc array to hold the flattened values.
212 * @return CRM_Contribute_BAO_ContributionSoft
214 public static function retrieve(&$params, &$defaults) {
215 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
216 $contributionSoft->copyValues($params);
217 if ($contributionSoft->find(TRUE)) {
218 CRM_Core_DAO
::storeValues($contributionSoft, $defaults);
219 return $contributionSoft;
225 * Delete soft credits.
227 * @param array $params
230 public static function del($params) {
231 //delete from contribution soft table
232 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
233 foreach ($params as $column => $value) {
234 $contributionSoft->$column = $value;
236 $contributionSoft->delete();
240 * @param int $contact_id
245 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
247 $whereClause = "AND cc.cancel_date IS NULL";
250 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
251 FROM civicrm_contribution_soft ccs
252 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
253 WHERE cc.is_test = %2 AND ccs.contact_id = %1 {$whereClause}
257 1 => array($contact_id, 'Integer'),
258 2 => array($isTest, 'Integer'),
261 $cs = CRM_Core_DAO
::executeQuery($query, $params);
264 $amount = $average = $cancelAmount = array();
266 while ($cs->fetch()) {
267 if ($cs->amount
> 0) {
269 $amount[] = $cs->amount
;
270 $average[] = $cs->average
;
271 $currency[] = $cs->currency
;
275 //to get cancel amount
276 $cancelAmountWhereClause = "AND cc.cancel_date IS NOT NULL";
277 $query = str_replace($whereClause, $cancelAmountWhereClause, $query);
278 $cancelAmountSQL = CRM_Core_DAO
::executeQuery($query, $params);
279 while ($cancelAmountSQL->fetch()) {
280 if ($cancelAmountSQL->amount
> 0) {
282 $cancelAmount[] = $cancelAmountSQL->amount
;
288 implode(', ', $amount),
289 implode(', ', $average),
290 implode(', ', $currency),
291 implode(', ', $cancelAmount),
298 * Retrieve soft contributions for contribution record.
300 * @param int $contributionID
305 * Array of soft contribution ids, amounts, and associated contact ids
307 public static function getSoftContribution($contributionID, $all = FALSE) {
311 'pcp_display_in_roll',
317 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
318 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
319 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
320 WHERE contribution_id = %1;
323 $params = array(1 => array($contributionID, 'Integer'));
325 $dao = CRM_Core_DAO
::executeQuery($query, $params);
327 $softContribution = array();
329 while ($dao->fetch()) {
332 foreach ($pcpFields as $val) {
333 $softContribution[$val] = $dao->$val;
335 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name
;
336 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id
;
340 $softContribution['soft_credit'][$count] = array(
341 'contact_id' => $dao->contact_id
,
342 'soft_credit_id' => $dao->id
,
343 'currency' => $dao->currency
,
344 'amount' => $dao->amount
,
345 'contact_name' => $dao->display_name
,
346 'soft_credit_type' => $dao->soft_credit_type_id
,
347 'soft_credit_type_label' => CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $dao->soft_credit_type_id
),
353 return $softContribution;
357 * @param int $contributionID
362 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
365 FROM civicrm_contribution_soft
366 WHERE contribution_id = %1
370 $query .= " AND pcp_id IS NOT NULL";
373 $query .= " AND pcp_id IS NULL";
375 $params = array(1 => array($contributionID, 'Integer'));
377 $dao = CRM_Core_DAO
::executeQuery($query, $params);
380 while ($dao->fetch()) {
390 * Wrapper for ajax soft contribution selector.
392 * @param array $params
393 * Associated array for params.
396 * Associated array of soft contributions
398 public static function getSoftContributionSelector($params) {
400 if (!empty($params['isTest'])) {
401 $isTest = $params['isTest'];
403 // Format the params.
404 $params['offset'] = ($params['page'] - 1) * $params['rp'];
405 $params['rowCount'] = $params['rp'];
406 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
407 $contactId = $params['cid'];
410 if ($params['context'] == 'membership' && !empty($params['entityID']) && $contactId) {
411 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$params['entityID']})";
414 $softCreditList = self
::getSoftContributionList($contactId, $filter, $isTest, $params);
416 $softCreditListDT = array();
417 $softCreditListDT['data'] = array_values($softCreditList);
418 $softCreditListDT['recordsTotal'] = $params['total'];
419 $softCreditListDT['recordsFiltered'] = $params['total'];
421 return $softCreditListDT;
425 * Function to retrieve the list of soft contributions for given contact.
427 * @param int $contact_id
429 * @param string $filter
431 * Additional filter criteria, later used in where clause.
432 * @param array $dTParams
436 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0, &$dTParams = NULL) {
437 $config = CRM_Core_Config
::singleton();
439 CRM_Core_Action
::VIEW
=> array(
440 'name' => ts('View'),
441 'url' => 'civicrm/contact/view/contribution',
442 'qs' => 'reset=1&id=%%contributionid%%&cid=%%contactId%%&action=view&context=contribution&selectedChild=contribute',
443 'title' => ts('View related contribution'),
446 $orderBy = 'cc.receive_date DESC';
447 if (!empty($dTParams['sort'])) {
448 $orderBy = $dTParams['sort'];
451 if (!empty($dTParams['rowCount']) && $dTParams['rowCount'] > 0) {
452 $limit = " LIMIT {$dTParams['offset']}, {$dTParams['rowCount']} ";
454 $softOgId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'soft_credit_type', 'id', 'name');
455 $statusOgId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'contribution_status', 'id', 'name');
458 SELECT SQL_CALC_FOUND_ROWS ccs.id, ccs.amount as amount,
461 ccs.pcp_display_in_roll,
462 ccs.pcp_roll_nickname,
463 ccs.pcp_personal_note,
464 ccs.soft_credit_type_id,
465 sov.label as sct_label,
467 cc.contact_id as contributor_id,
468 cc.contribution_status_id as contribution_status_id,
469 cov.label as contribution_status,
470 cp.title as pcp_title,
472 contact.display_name as contributor_name,
473 cct.name as financial_type
474 FROM civicrm_contribution_soft ccs
475 LEFT JOIN civicrm_contribution cc
476 ON ccs.contribution_id = cc.id
477 LEFT JOIN civicrm_pcp cp
478 ON ccs.pcp_id = cp.id
479 LEFT JOIN civicrm_contact contact ON
480 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
481 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
482 LEFT JOIN civicrm_option_value sov ON sov.option_group_id = %3 AND ccs.soft_credit_type_id = sov.value
483 LEFT JOIN civicrm_option_value cov ON cov.option_group_id = %4 AND cc.contribution_status_id = cov.value
487 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
492 $query .= "{$where} ORDER BY {$orderBy} {$limit}";
495 1 => array($contact_id, 'Integer'),
496 2 => array($isTest, 'Integer'),
497 3 => array($softOgId, 'Integer'),
498 4 => array($statusOgId, 'Integer'),
500 $cs = CRM_Core_DAO
::executeQuery($query, $params);
502 $dTParams['total'] = CRM_Core_DAO
::singleValueQuery('SELECT FOUND_ROWS()');
504 while ($cs->fetch()) {
505 $result[$cs->id
]['amount'] = CRM_Utils_Money
::format($cs->amount
, $cs->currency
);
506 $result[$cs->id
]['currency'] = $cs->currency
;
507 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
508 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
509 $result[$cs->id
]['contributor_name'] = CRM_Utils_System
::href(
510 $cs->contributor_name
,
511 'civicrm/contact/view',
512 "reset=1&cid={$cs->contributor_id}"
514 $result[$cs->id
]['financial_type'] = $cs->financial_type
;
515 $result[$cs->id
]['receive_date'] = CRM_Utils_Date
::customFormat($cs->receive_date
, $config->dateformatDatetime
);
516 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
517 $result[$cs->id
]['pcp_title'] = ($cs->pcp_title
) ?
$cs->pcp_title
: 'n/a';
518 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
519 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
520 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
521 $result[$cs->id
]['contribution_status'] = $cs->contribution_status
;
522 $result[$cs->id
]['sct_label'] = $cs->sct_label
;
524 'contributionid' => $cs->contribution_id
,
525 'contactId' => $cs->contributor_id
,
527 $result[$cs->id
]['links'] = CRM_Core_Action
::formLink($links, NULL, $replace);
530 $result[$cs->id
]['contribution_status'] = $result[$cs->id
]['contribution_status'] . '<br /> (test)';
537 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
538 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
541 * @param CRM_Core_Form $form
542 * @param array $params
543 * @param int $honorId
545 public static function formatHonoreeProfileFields($form, $params, $honorId = NULL) {
546 if (empty($form->_values
['honoree_profile_id'])) {
549 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($form->_values
['honoree_profile_id']);
550 $profileFields = CRM_Core_BAO_UFGroup
::getFields($form->_values
['honoree_profile_id']);
551 $honoreeProfileFields = $values = array();
555 CRM_Core_BAO_UFGroup
::getValues($honorId, $profileFields, $values, FALSE, $params);
556 if (empty($params)) {
557 foreach ($profileFields as $name => $field) {
558 $title = $field['title'];
559 $params[$field['name']] = $values[$title];
564 //remove name related fields and construct name string with prefix/suffix
565 //which will be later assigned to template
566 switch ($profileContactType) {
568 if (array_key_exists('prefix_id', $params)) {
569 $honorName = CRM_Utils_Array
::value(CRM_Utils_Array
::value('prefix_id', $params),
570 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
572 unset($profileFields['prefix_id']);
574 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
575 unset($profileFields['first_name']);
576 unset($profileFields['last_name']);
577 if (array_key_exists('suffix_id', $params)) {
578 $honorName .= ' ' . CRM_Utils_Array
::value(CRM_Utils_Array
::value('suffix_id', $params),
579 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
581 unset($profileFields['suffix_id']);
586 $honorName = $params['organization_name'];
587 unset($profileFields['organization_name']);
591 $honorName = $params['household_name'];
592 unset($profileFields['household_name']);
597 $honoreeProfileFields['Name'] = $honorName;
598 foreach ($profileFields as $name => $field) {
599 $title = $field['title'];
600 $honoreeProfileFields[$title] = $values[$title];
602 $form->assign('honoreeProfile', $honoreeProfileFields);
605 $form->assign('honorName', $honorName);