3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public function __construct() {
41 parent
::__construct();
45 * Add contribution soft credit record
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
51 * 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 * Fetch object based on array of properties
68 * @param array $params
69 * (reference ) an assoc array of name/value pairs.
70 * @param array $defaults
71 * (reference ) an assoc array to hold the flattened values.
73 * @return CRM_Contribute_BAO_ContributionSoft
76 public static function retrieve(&$params, &$defaults) {
77 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
78 $contributionSoft->copyValues($params);
79 if ($contributionSoft->find(TRUE)) {
80 CRM_Core_DAO
::storeValues($contributionSoft, $defaults);
81 return $contributionSoft;
89 * @param array $params
93 public static function del($params) {
94 //delete from contribution soft table
95 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
96 foreach ($params as $column => $value) {
97 $contributionSoft->$column = $value;
99 $contributionSoft->delete();
103 * @param int $contact_id
108 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
110 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
111 FROM civicrm_contribution_soft ccs
112 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
113 WHERE cc.is_test = %2 AND ccs.contact_id = %1
117 1 => array($contact_id, 'Integer'),
118 2 => array($isTest, 'Integer')
121 $cs = CRM_Core_DAO
::executeQuery($query, $params);
124 $amount = $average = array();
126 while ($cs->fetch()) {
127 if ($cs->amount
> 0) {
129 $amount[] = $cs->amount
;
130 $average[] = $cs->average
;
131 $currency[] = $cs->currency
;
137 implode(', ', $amount),
138 implode(', ', $average),
139 implode(', ', $currency),
146 * Retrieve soft contributions for contribution record.
148 * @param int $contributionID
153 * Array of soft contribution ids, amounts, and associated contact ids
156 public static function getSoftContribution($contributionID, $all = FALSE) {
160 'pcp_display_in_roll',
166 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
167 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
168 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
169 WHERE contribution_id = %1;
172 $params = array(1 => array($contributionID, 'Integer'));
174 $dao = CRM_Core_DAO
::executeQuery($query, $params);
176 $softContribution = array();
178 while ($dao->fetch()) {
181 foreach ($pcpFields as $val) {
182 $softContribution[$val] = $dao->$val;
184 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name
;
185 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id
;
189 $softContribution['soft_credit'][$count] = array(
190 'contact_id' => $dao->contact_id
,
191 'soft_credit_id' => $dao->id
,
192 'currency' => $dao->currency
,
193 'amount' => $dao->amount
,
194 'contact_name' => $dao->display_name
,
195 'soft_credit_type' => $dao->soft_credit_type_id
,
196 'soft_credit_type_label' => CRM_Core_OptionGroup
::getLabel('soft_credit_type', $dao->soft_credit_type_id
),
202 return $softContribution;
206 * @param int $contributionID
211 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
214 FROM civicrm_contribution_soft
215 WHERE contribution_id = %1
219 $query .= " AND pcp_id IS NOT NULL";
222 $query .= " AND pcp_id IS NULL";
224 $params = array(1 => array($contributionID, 'Integer'));
226 $dao = CRM_Core_DAO
::executeQuery($query, $params);
229 while ($dao->fetch()) {
239 * Function to retrieve the list of soft contributions for given contact.
241 * @param int $contact_id
244 * @param string $filter
245 * Additional filter criteria, later used in where clause.
250 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) {
252 SELECT ccs.id, ccs.amount as amount,
255 ccs.pcp_display_in_roll,
256 ccs.pcp_roll_nickname,
257 ccs.pcp_personal_note,
258 ccs.soft_credit_type_id,
260 cc.contact_id as contributor_id,
261 cc.contribution_status_id as contribution_status_id,
262 cp.title as pcp_title,
264 contact.display_name,
265 cct.name as contributionType
266 FROM civicrm_contribution_soft ccs
267 LEFT JOIN civicrm_contribution cc
268 ON ccs.contribution_id = cc.id
269 LEFT JOIN civicrm_pcp cp
270 ON ccs.pcp_id = cp.id
271 LEFT JOIN civicrm_contact contact ON
272 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
273 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
277 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
282 $query .= "{$where} ORDER BY cc.receive_date DESC";
285 1 => array($contact_id, 'Integer'),
286 2 => array($isTest, 'Integer'),
288 $cs = CRM_Core_DAO
::executeQuery($query, $params);
289 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
291 while ($cs->fetch()) {
292 $result[$cs->id
]['amount'] = $cs->amount
;
293 $result[$cs->id
]['currency'] = $cs->currency
;
294 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
295 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
296 $result[$cs->id
]['contributor_name'] = $cs->display_name
;
297 $result[$cs->id
]['financial_type'] = $cs->contributionType
;
298 $result[$cs->id
]['receive_date'] = $cs->receive_date
;
299 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
300 $result[$cs->id
]['pcp_title'] = $cs->pcp_title
;
301 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
302 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
303 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
304 $result[$cs->id
]['contribution_status'] = CRM_Utils_Array
::value($cs->contribution_status_id
, $contributionStatus);
305 $result[$cs->id
]['sct_label'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type', $cs->soft_credit_type_id
);
308 $result[$cs->id
]['contribution_status'] = $result[$cs->id
]['contribution_status'] . '<br /> (test)';
315 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
316 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
319 * @param CRM_Core_Form $form
320 * @param array $params
321 * @param int $honoreeprofileId
322 * @param int $honorId
324 public static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) {
325 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($honoreeprofileId);
326 $profileFields = CRM_Core_BAO_UFGroup
::getFields($honoreeprofileId);
327 $honoreeProfileFields = $values = array();
331 CRM_Core_BAO_UFGroup
::getValues($honorId, $profileFields, $values, FALSE, $params);
332 if (empty($params)) {
333 foreach ($profileFields as $name => $field) {
334 $title = $field['title'];
335 $params[$field['name']] = $values[$title];
340 //remove name related fields and construct name string with prefix/suffix
341 //which will be later assigned to template
342 switch ($profileContactType) {
344 if (array_key_exists('prefix_id', $params)) {
345 $honorName = CRM_Utils_Array
::value(CRM_Utils_Array
::value('prefix_id', $params),
346 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
348 unset($profileFields['prefix_id']);
350 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
351 unset($profileFields['first_name']);
352 unset($profileFields['last_name']);
353 if (array_key_exists('suffix_id', $params)) {
354 $honorName .= ' ' . CRM_Utils_Array
::value(CRM_Utils_Array
::value('suffix_id', $params),
355 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
357 unset($profileFields['suffix_id']);
362 $honorName = $params['organization_name'];
363 unset($profileFields['organization_name']);
367 $honorName = $params['household_name'];
368 unset($profileFields['household_name']);
373 $honoreeProfileFields['Name'] = $honorName;
374 foreach ($profileFields as $name => $field) {
375 $title = $field['title'];
376 $honoreeProfileFields[$title] = $values[$title];
378 $form->assign('honoreeProfile', $honoreeProfileFields);
381 $form->assign('honorName', $honorName);