3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Contribute_BAO_ContributionSoft
extends CRM_Contribute_DAO_ContributionSoft
{
40 function __construct() {
41 parent
::__construct();
45 * function to add contribution soft credit record
47 * @param array $params (reference ) an assoc array of name/value pairs
49 * @return object soft contribution of object that is added
53 public static function add(&$params) {
54 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
55 $contributionSoft->copyValues($params);
57 // set currency for CRM-1496
58 if (!isset($contributionSoft->currency
)) {
59 $config = CRM_Core_Config
::singleton();
60 $contributionSoft->currency
= $config->defaultCurrency
;
62 return $contributionSoft->save();
66 * Takes a bunch of params that are needed to match certain criteria and
67 * retrieves the relevant objects. Typically the valid params are only
68 * contact_id. We'll tweak this function to be more full featured over a period
69 * of time. This is the inverse function of create. It also stores all the retrieved
70 * values in the default array
72 * @param array $params (reference ) an assoc array of name/value pairs
73 * @param array $defaults (reference ) an assoc array to hold the flattened values
75 * @return object CRM_Contribute_BAO_ContributionSoft object
79 static function retrieve(&$params, &$defaults) {
80 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
81 $contributionSoft->copyValues($params);
82 if ($contributionSoft->find(TRUE)) {
83 CRM_Core_DAO
::storeValues($contributionSoft, $defaults);
84 return $contributionSoft;
90 * Function to delete soft credits
94 * @internal param int $contributionTypeId
97 static function del($params) {
98 //delete from contribution soft table
99 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
100 foreach($params as $column => $value) {
101 $contributionSoft->$column = $value;
103 $contributionSoft->delete();
112 static function getSoftContributionTotals($contact_id, $isTest = 0) {
114 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
115 FROM civicrm_contribution_soft ccs
116 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
117 WHERE cc.is_test = %2 AND ccs.contact_id = %1
120 $params = array(1 => array($contact_id, 'Integer'),
121 2 => array($isTest, 'Integer'));
123 $cs = CRM_Core_DAO
::executeQuery($query, $params);
126 $amount = $average = array();
128 while ($cs->fetch()) {
129 if ($cs->amount
> 0) {
131 $amount[] = $cs->amount
;
132 $average[] = $cs->average
;
133 $currency[] = $cs->currency
;
139 implode(', ', $amount),
140 implode(', ', $average),
141 implode(', ', $currency),
148 * Function to retrieve soft contributions for contribution record.
150 * @param $contributionID
151 * @param boolean $all include PCP data
153 * @internal param array $params an associated array
154 * @return array of soft contribution ids, amounts, and associated contact ids
157 static function getSoftContribution($contributionID, $all = FALSE) {
161 'pcp_display_in_roll',
167 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
168 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
169 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
170 WHERE contribution_id = %1;
173 $params = array(1 => array($contributionID, 'Integer'));
175 $dao = CRM_Core_DAO
::executeQuery($query, $params);
177 $softContribution = array();
179 while ($dao->fetch()) {
182 foreach ($pcpFields as $val) {
183 $softContribution[$val] = $dao->$val;
185 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name
;
186 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id
;
190 $softContribution['soft_credit'][$count] = array(
191 'contact_id' => $dao->contact_id
,
192 'soft_credit_id' => $dao->id
,
193 'currency' => $dao->currency
,
194 'amount' => $dao->amount
,
195 'contact_name' => $dao->display_name
,
196 'soft_credit_type' => $dao->soft_credit_type_id
,
197 'soft_credit_type_label' => CRM_Core_OptionGroup
::getLabel('soft_credit_type', $dao->soft_credit_type_id
)
203 return $softContribution;
207 * @param $contributionID
212 static function getSoftCreditIds($contributionID , $isPCP = FALSE) {
215 FROM civicrm_contribution_soft
216 WHERE contribution_id = %1
220 $query .= " AND pcp_id IS NOT NULL";
223 $query .= " AND pcp_id IS NULL";
225 $params = array(1 => array($contributionID, 'Integer'));
227 $dao = CRM_Core_DAO
::executeQuery($query, $params);
230 while ($dao->fetch()) {
240 * Function to retrieve the list of soft contributions for given contact.
242 * @param int $contact_id contact id
244 * @param string $filter additional filter criteria, later used in where clause
249 static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) {
251 SELECT ccs.id, ccs.amount as amount,
254 ccs.pcp_display_in_roll,
255 ccs.pcp_roll_nickname,
256 ccs.pcp_personal_note,
257 ccs.soft_credit_type_id,
259 cc.contact_id as contributor_id,
260 cc.contribution_status_id as contribution_status_id,
261 cp.title as pcp_title,
263 contact.display_name,
264 cct.name as contributionType
265 FROM civicrm_contribution_soft ccs
266 LEFT JOIN civicrm_contribution cc
267 ON ccs.contribution_id = cc.id
268 LEFT JOIN civicrm_pcp cp
269 ON ccs.pcp_id = cp.id
270 LEFT JOIN civicrm_contact contact ON
271 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
272 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
276 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
281 $query .= "{$where} ORDER BY cc.receive_date DESC";
284 1 => array($contact_id, 'Integer'),
285 2 => array($isTest, 'Integer')
287 $cs = CRM_Core_DAO
::executeQuery($query, $params);
288 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
290 while ($cs->fetch()) {
291 $result[$cs->id
]['amount'] = $cs->amount
;
292 $result[$cs->id
]['currency'] = $cs->currency
;
293 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
294 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
295 $result[$cs->id
]['contributor_name'] = $cs->display_name
;
296 $result[$cs->id
]['financial_type'] = $cs->contributionType
;
297 $result[$cs->id
]['receive_date'] = $cs->receive_date
;
298 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
299 $result[$cs->id
]['pcp_title'] = $cs->pcp_title
;
300 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
301 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
302 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
303 $result[$cs->id
]['contribution_status'] = CRM_Utils_Array
::value($cs->contribution_status_id
, $contributionStatus);
304 $result[$cs->id
]['sct_label'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type', $cs->soft_credit_type_id
);
307 $result[$cs->id
]['contribution_status'] = $result[$cs->id
]['contribution_status'] . '<br /> (test)';
314 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
315 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
324 * @param $honoreeprofileId
325 * @param null $honorId
327 static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) {
328 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($honoreeprofileId);
329 $profileFields = CRM_Core_BAO_UFGroup
::getFields($honoreeprofileId);
330 $honoreeProfileFields = $values = array();
334 CRM_Core_BAO_UFGroup
::getValues($honorId, $profileFields, $values, FALSE, $params);
335 if (empty($params)) {
336 foreach ($profileFields as $name => $field) {
337 $title = $field['title'];
338 $params[$field['name']] = $values[$title];
343 //remove name related fields and construct name string with prefix/suffix
344 //which will be later assigned to template
345 switch ($profileContactType) {
347 if (array_key_exists('prefix_id', $params)) {
348 $honorName = CRM_Utils_Array
::value(CRM_Utils_Array
::value('prefix_id', $params),
349 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
351 unset($profileFields['prefix_id']);
353 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
354 unset($profileFields['first_name']);
355 unset($profileFields['last_name']);
356 if (array_key_exists('suffix_id', $params)) {
357 $honorName .= ' ' . CRM_Utils_Array
::value(CRM_Utils_Array
::value('suffix_id', $params),
358 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
360 unset($profileFields['suffix_id']);
364 $honorName = $params['organization_name'];
365 unset($profileFields['organization_name']);
368 $honorName = $params['household_name'];
369 unset($profileFields['household_name']);
374 $honoreeProfileFields['Name'] = $honorName;
375 foreach ($profileFields as $name => $field) {
376 $title = $field['title'];
377 $honoreeProfileFields[$title] = $values[$title];
379 $form->assign('honoreeProfile', $honoreeProfileFields);
382 $form->assign('honorName', $honorName);