3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 * funtion 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($contributionID) {
96 //delete from contribution soft table
97 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
98 $contributionSoft->contribution_id
= $contributionID;
99 $contributionSoft->delete();
102 static function getSoftContributionTotals($contact_id, $isTest = 0) {
104 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
105 FROM civicrm_contribution_soft ccs
106 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
107 WHERE cc.is_test = %2 AND ccs.contact_id = %1
110 $params = array(1 => array($contact_id, 'Integer'),
111 2 => array($isTest, 'Integer'));
113 $cs = CRM_Core_DAO
::executeQuery($query, $params);
116 $amount = $average = array();
118 while ($cs->fetch()) {
119 if ($cs->amount
> 0) {
121 $amount[] = $cs->amount
;
122 $average[] = $cs->average
;
123 $currency[] = $cs->currency
;
128 return array(implode(', ', $amount),
129 implode(', ', $average),
130 implode(', ', $currency),
137 * Function to retrieve soft contributions for contribution record.
138 * @param array $params an associated array
139 * @param boolean $all include PCP data
141 * @return array of soft contribution ids, amounts, and associated contact ids
144 static function getSoftContribution($contributionID, $all = FALSE) {
147 'pcp_display_in_roll',
153 SELECT ccs.id, pcp_id, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, amount, contact_id, c.display_name
154 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
155 WHERE contribution_id = %1;
158 $params = array(1 => array($contributionID, 'Integer'));
160 $dao = CRM_Core_DAO
::executeQuery($query, $params);
162 $softContribution = array();
164 while ($dao->fetch()) {
166 foreach ($pcpFields as $val) {
167 $softContribution['pcp'][$val] = $dao->$val;
171 $softContribution['soft_credit'][$count] = array(
172 'contact_id' => $dao->contact_id
,
173 'soft_credit_id' => $dao->id
,
174 'amount' => $dao->amount
,
175 'contact_name' => $dao->display_name
181 * FIX API before deleting this
182 $cs = new CRM_Contribute_DAO_ContributionSoft();
183 $cs->copyValues($params);
184 $softContribution = array();
189 while ($cs->fetch()) {
191 foreach ($pcpFields as $val) {
192 $softContribution['pcp'][$val] = $cs->$val;
196 $softContribution['soft_credit'][$count] = array(
197 'soft_credit_to' => $cs->contact_id,
198 'soft_credit_id' => $cs->id,
199 'soft_credit_amount' => $cs->amount,
206 return $softContribution;
210 * Function to retrieve the list of soft contributons for given contact.
211 * @param int $contact_id contact id
216 static function getSoftContributionList($contact_id, $isTest = 0) {
218 SELECT ccs.id, ccs.amount as amount,
221 ccs.pcp_display_in_roll,
222 ccs.pcp_roll_nickname,
223 ccs.pcp_personal_note,
225 cc.contact_id as contributor_id,
226 cc.contribution_status_id as contribution_status_id,
227 cp.title as pcp_title,
229 contact.display_name,
230 cct.name as contributionType
231 FROM civicrm_contribution_soft ccs
232 LEFT JOIN civicrm_contribution cc
233 ON ccs.contribution_id = cc.id
234 LEFT JOIN civicrm_pcp cp
235 ON ccs.pcp_id = cp.id
236 LEFT JOIN civicrm_contact contact ON
237 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
238 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
239 WHERE cc.is_test = %2 AND ccs.contact_id = %1
240 ORDER BY cc.receive_date DESC';
243 1 => array($contact_id, 'Integer'),
244 2 => array($isTest, 'Integer')
246 $cs = CRM_Core_DAO
::executeQuery($query, $params);
247 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
249 while ($cs->fetch()) {
250 $result[$cs->id
]['amount'] = $cs->amount
;
251 $result[$cs->id
]['currency'] = $cs->currency
;
252 $result[$cs->id
]['contributor_id'] = $cs->contributor_id
;
253 $result[$cs->id
]['contribution_id'] = $cs->contribution_id
;
254 $result[$cs->id
]['contributor_name'] = $cs->display_name
;
255 $result[$cs->id
]['financial_type'] = $cs->contributionType
;
256 $result[$cs->id
]['receive_date'] = $cs->receive_date
;
257 $result[$cs->id
]['pcp_id'] = $cs->pcp_id
;
258 $result[$cs->id
]['pcp_title'] = $cs->pcp_title
;
259 $result[$cs->id
]['pcp_display_in_roll'] = $cs->pcp_display_in_roll
;
260 $result[$cs->id
]['pcp_roll_nickname'] = $cs->pcp_roll_nickname
;
261 $result[$cs->id
]['pcp_personal_note'] = $cs->pcp_personal_note
;
262 $result[$cs->id
]['contribution_status'] = CRM_Utils_Array
::value($cs->contribution_status_id
, $contributionStatus);
265 $result[$cs->id
]['contribution_status'] = $result[$cs->id
]['contribution_status'] . '<br /> (test)';