3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
92 * @param int $contributionTypeId
95 static function del($params) {
96 //delete from contribution soft table
97 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
98 foreach($params as $column => $value) {
99 $contributionSoft->$column = $value;
101 $contributionSoft->delete();
104 static function getSoftContributionTotals($contact_id, $isTest = 0) {
106 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
107 FROM civicrm_contribution_soft ccs
108 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
109 WHERE cc.is_test = %2 AND ccs.contact_id = %1
112 $params = array(1 => array($contact_id, 'Integer'),
113 2 => array($isTest, 'Integer'));
115 $cs = CRM_Core_DAO
::executeQuery($query, $params);
118 $amount = $average = array();
120 while ($cs->fetch()) {
121 if ($cs->amount
> 0) {
123 $amount[] = $cs->amount
;
124 $average[] = $cs->average
;
125 $currency[] = $cs->currency
;
131 implode(', ', $amount),
132 implode(', ', $average),
133 implode(', ', $currency),
140 * Function to retrieve soft contributions for contribution record.
141 * @param array $params an associated array
142 * @param boolean $all include PCP data
144 * @return array of soft contribution ids, amounts, and associated contact ids
147 static function getSoftContribution($contributionID, $all = FALSE) {
150 'pcp_display_in_roll',
156 SELECT ccs.id, pcp_id, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, currency, amount, contact_id, c.display_name, ccs.soft_credit_type_id
157 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
158 WHERE contribution_id = %1;
161 $params = array(1 => array($contributionID, 'Integer'));
163 $dao = CRM_Core_DAO
::executeQuery($query, $params);
165 $softContribution = array();
167 while ($dao->fetch()) {
169 foreach ($pcpFields as $val) {
170 $softContribution[$val] = $dao->$val;
174 $softContribution['soft_credit'][$count] = array(
175 'contact_id' => $dao->contact_id
,
176 'soft_credit_id' => $dao->id
,
177 'currency' => $dao->currency
,
178 'amount' => $dao->amount
,
179 'contact_name' => $dao->display_name
,
180 'soft_credit_type' => $dao->soft_credit_type_id
,
181 'soft_credit_type_label' => CRM_Core_OptionGroup
::getLabel('soft_credit_type', $dao->soft_credit_type_id
)
187 * FIX API before deleting this
188 $cs = new CRM_Contribute_DAO_ContributionSoft();
189 $cs->copyValues($params);
190 $softContribution = array();
195 while ($cs->fetch()) {
197 foreach ($pcpFields as $val) {
198 $softContribution['pcp'][$val] = $cs->$val;
202 $softContribution['soft_credit'][$count] = array(
203 'soft_credit_to' => $cs->contact_id,
204 'soft_credit_id' => $cs->id,
205 'soft_credit_amount' => $cs->amount,
212 return $softContribution;
215 static function getSoftCreditType($contributionID) {
218 FROM civicrm_contribution_soft
219 WHERE contribution_id = %1
221 $params = array(1 => array($contributionID, 'Integer'));
223 $dao = CRM_Core_DAO
::executeQuery($query, $params);
226 while ($dao->fetch()) {
235 return array($type, $id);
239 * Function to retrieve the list of soft contributions for given contact.
240 * @param int $contact_id contact id
245 static function getSoftContributionList($contact_id, $isTest = 0) {
247 SELECT ccs.id, ccs.amount as amount,
250 ccs.pcp_display_in_roll,
251 ccs.pcp_roll_nickname,
252 ccs.pcp_personal_note,
253 ccs.soft_credit_type_id,
255 cc.contact_id as contributor_id,
256 cc.contribution_status_id as contribution_status_id,
257 cp.title as pcp_title,
259 contact.display_name,
260 cct.name as contributionType
261 FROM civicrm_contribution_soft ccs
262 LEFT JOIN civicrm_contribution cc
263 ON ccs.contribution_id = cc.id
264 LEFT JOIN civicrm_pcp cp
265 ON ccs.pcp_id = cp.id
266 LEFT JOIN civicrm_contact contact ON
267 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
268 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
269 WHERE cc.is_test = %2 AND ccs.contact_id = %1
270 ORDER BY cc.receive_date DESC';
273 1 => array($contact_id, 'Integer'),
274 2 => array($isTest, 'Integer')
276 $cs = CRM_Core_DAO
::executeQuery($query, $params);
277 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
279 while ($cs->fetch()) {
280 $result[$cs->id
]['amount'] = $cs->amount
;
281 $result[$cs->id
]['currency'] = $cs->currency
;
282 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
283 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
284 $result[$cs->id
]['contributor_name'] = $cs->display_name
;
285 $result[$cs->id
]['financial_type'] = $cs->contributionType
;
286 $result[$cs->id
]['receive_date'] = $cs->receive_date
;
287 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
288 $result[$cs->id
]['pcp_title'] = $cs->pcp_title
;
289 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
290 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
291 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
292 $result[$cs->id
]['contribution_status'] = CRM_Utils_Array
::value($cs->contribution_status_id
, $contributionStatus);
293 $result[$cs->id
]['sct_label'] = CRM_Core_OptionGroup
::getLabel('soft_credit_type', $cs->soft_credit_type_id
);
296 $result[$cs->id
]['contribution_status'] = $result[$cs->id
]['contribution_status'] . '<br /> (test)';
303 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
304 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
310 static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) {
311 $profileContactType = CRM_Core_BAO_UFGroup
::getContactType($honoreeprofileId);
312 $profileFields = CRM_Core_BAO_UFGroup
::getFields($honoreeprofileId);
313 $honoreeProfileFields = $values = array();
317 CRM_Core_BAO_UFGroup
::getValues($honorId, $profileFields, $values, FALSE, $params);
318 if (empty($params)) {
319 foreach ($profileFields as $name => $field) {
320 $title = $field['title'];
321 $params[$field['name']] = $values[$title];
326 //remove name related fields and construct name string with prefix/suffix
327 //which will be later assigned to template
328 switch ($profileContactType) {
330 if (array_key_exists('prefix_id', $params)) {
331 $honorName = CRM_Utils_Array
::value(CRM_Utils_Array
::value('prefix_id', $params),
332 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
334 unset($profileFields['prefix_id']);
336 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
337 unset($profileFields['first_name']);
338 unset($profileFields['last_name']);
339 if (array_key_exists('suffix_id', $params)) {
340 $honorName .= ' ' . CRM_Utils_Array
::value(CRM_Utils_Array
::value('suffix_id', $params),
341 CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
343 unset($profileFields['suffix_id']);
347 $honorName = $params['organization_name'];
348 unset($profileFields['organization_name']);
351 $honorName = $params['household_name'];
352 unset($profileFields['household_name']);
357 $honoreeProfileFields['Name'] = $honorName;
358 foreach ($profileFields as $name => $field) {
359 $title = $field['title'];
360 $honoreeProfileFields[$title] = $values[$title];
362 $form->assign('honoreeProfile', $honoreeProfileFields);
365 $form->assign('honorName', $honorName);