Merge pull request #2414 from kurund/profile-builder
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
CommitLineData
a45a51e3 1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
a45a51e3 5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft {
36
8cb2c285 37 /**
a45a51e3 38 * construct method
8cb2c285 39 */
a45a51e3 40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
91bb24a7 45 * function to add contribution soft credit record
a45a51e3 46 *
47 * @param array $params (reference ) an assoc array of name/value pairs
48 *
49 * @return object soft contribution of object that is added
50 * @access public
51 *
52 */
07a88eec 53 public static function add(&$params) {
a45a51e3 54 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
55 $contributionSoft->copyValues($params);
6dcc4d9d 56
57 // set currency for CRM-1496
58 if (!isset($contributionSoft->currency)) {
59 $config = CRM_Core_Config::singleton();
60 $contributionSoft->currency = $config->defaultCurrency;
61 }
a45a51e3 62 return $contributionSoft->save();
63 }
64
65 /**
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
71 *
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
74 *
75 * @return object CRM_Contribute_BAO_ContributionSoft object
76 * @access public
77 * @static
78 */
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;
85 }
86 return NULL;
87 }
88
89 /**
8cb2c285 90 * Function to delete soft credits
a45a51e3 91 *
92 * @param int $contributionTypeId
93 * @static
94 */
2cfc0f58 95 static function del($params) {
a45a51e3 96 //delete from contribution soft table
97 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
2cfc0f58 98 foreach($params as $column => $value) {
99 $contributionSoft->$column = $value;
100 }
a45a51e3 101 $contributionSoft->delete();
102 }
6dcc4d9d 103
6dcc4d9d 104 static function getSoftContributionTotals($contact_id, $isTest = 0) {
8cb2c285
KJ
105 $query = '
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
110 GROUP BY currency';
6dcc4d9d 111
112 $params = array(1 => array($contact_id, 'Integer'),
113 2 => array($isTest, 'Integer'));
114
115 $cs = CRM_Core_DAO::executeQuery($query, $params);
116
117 $count = 0;
118 $amount = $average = array();
119
120 while ($cs->fetch()) {
121 if ($cs->amount > 0) {
122 $count++;
91bb24a7 123 $amount[] = $cs->amount;
124 $average[] = $cs->average;
6dcc4d9d 125 $currency[] = $cs->currency;
126 }
127 }
128
129 if ($count > 0) {
91bb24a7 130 return array(
131 implode(',&nbsp;', $amount),
6dcc4d9d 132 implode(',&nbsp;', $average),
133 implode(',&nbsp;', $currency),
134 );
135 }
136 return array(0, 0);
137 }
138
139 /**
140 * Function to retrieve soft contributions for contribution record.
141 * @param array $params an associated array
142 * @param boolean $all include PCP data
143 *
144 * @return array of soft contribution ids, amounts, and associated contact ids
145 * @static
146 */
7ccf8829 147 static function getSoftContribution($contributionID, $all = FALSE) {
17db9f82
KJ
148 $pcpFields = array(
149 'pcp_id',
150 'pcp_display_in_roll',
151 'pcp_roll_nickname',
152 'pcp_personal_note',
153 );
154
7ccf8829 155 $query = '
51fa20cb 156 SELECT ccs.id, pcp_id, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, amount, contact_id, c.display_name, ccs.soft_credit_type_id
7ccf8829
KJ
157 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
158 WHERE contribution_id = %1;
159 ';
160
161 $params = array(1 => array($contributionID, 'Integer'));
162
163 $dao = CRM_Core_DAO::executeQuery($query, $params);
164
165 $softContribution = array();
166 $count = 1;
167 while ($dao->fetch()) {
168 if ($all) {
169 foreach ($pcpFields as $val) {
76ca3345 170 $softContribution[$val] = $dao->$val;
7ccf8829
KJ
171 }
172 }
173
174 $softContribution['soft_credit'][$count] = array(
175 'contact_id' => $dao->contact_id,
176 'soft_credit_id' => $dao->id,
177 'amount' => $dao->amount,
51fa20cb 178 'contact_name' => $dao->display_name,
179 'soft_credit_type' => $dao->soft_credit_type_id,
180 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id)
7ccf8829
KJ
181 );
182 $count++;
183 }
184
185 /*
186 * FIX API before deleting this
6dcc4d9d 187 $cs = new CRM_Contribute_DAO_ContributionSoft();
188 $cs->copyValues($params);
189 $softContribution = array();
190 $cs->find();
17db9f82 191
6dcc4d9d 192 if ($cs->N > 0) {
35729234 193 $count = 1;
6dcc4d9d 194 while ($cs->fetch()) {
6dcc4d9d 195 if ($all) {
17db9f82
KJ
196 foreach ($pcpFields as $val) {
197 $softContribution['pcp'][$val] = $cs->$val;
6dcc4d9d 198 }
199 }
35729234
KJ
200
201 $softContribution['soft_credit'][$count] = array(
202 'soft_credit_to' => $cs->contact_id,
203 'soft_credit_id' => $cs->id,
204 'soft_credit_amount' => $cs->amount,
205 );
206 $count++;
6dcc4d9d 207 }
208 }
7ccf8829
KJ
209 */
210
6dcc4d9d 211 return $softContribution;
212 }
213
2cfc0f58 214 static function getSoftCreditType($contributionID) {
91bb24a7 215 $query = "
2cfc0f58 216 SELECT id, pcp_id
217 FROM civicrm_contribution_soft
218 WHERE contribution_id = %1
219 ";
220 $params = array(1 => array($contributionID, 'Integer'));
221
222 $dao = CRM_Core_DAO::executeQuery($query, $params);
223 $id = array();
7f880799 224 $type = '';
2cfc0f58 225 while ($dao->fetch()) {
226 if ($dao->pcp_id) {
227 $type = 'pcp';
228 }
229 else {
230 $type = 'soft';
231 }
232 $id[] = $dao->id;
233 }
234 return array($type, $id);
235 }
91bb24a7 236
6dcc4d9d 237 /**
91bb24a7 238 * Function to retrieve the list of soft contributions for given contact.
6dcc4d9d 239 * @param int $contact_id contact id
240 *
241 * @return array
242 * @static
243 */
244 static function getSoftContributionList($contact_id, $isTest = 0) {
8cb2c285 245 $query = '
6dcc4d9d 246 SELECT ccs.id, ccs.amount as amount,
8cb2c285
KJ
247 ccs.contribution_id,
248 ccs.pcp_id,
249 ccs.pcp_display_in_roll,
250 ccs.pcp_roll_nickname,
251 ccs.pcp_personal_note,
51fa20cb 252 ccs.soft_credit_type_id,
8cb2c285
KJ
253 cc.receive_date,
254 cc.contact_id as contributor_id,
255 cc.contribution_status_id as contribution_status_id,
256 cp.title as pcp_title,
257 cc.currency,
258 contact.display_name,
259 cct.name as contributionType
260 FROM civicrm_contribution_soft ccs
261 LEFT JOIN civicrm_contribution cc
262 ON ccs.contribution_id = cc.id
263 LEFT JOIN civicrm_pcp cp
264 ON ccs.pcp_id = cp.id
265 LEFT JOIN civicrm_contact contact ON
266 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
267 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
268 WHERE cc.is_test = %2 AND ccs.contact_id = %1
269 ORDER BY cc.receive_date DESC';
270
271 $params = array(
272 1 => array($contact_id, 'Integer'),
273 2 => array($isTest, 'Integer')
274 );
275 $cs = CRM_Core_DAO::executeQuery($query, $params);
6dcc4d9d 276 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
8cb2c285 277 $result = array();
6dcc4d9d 278 while ($cs->fetch()) {
279 $result[$cs->id]['amount'] = $cs->amount;
280 $result[$cs->id]['currency'] = $cs->currency;
281 $result[$cs->id]['contributor_id'] = $cs->contributor_id;
282 $result[$cs->id]['contribution_id'] = $cs->contribution_id;
283 $result[$cs->id]['contributor_name'] = $cs->display_name;
284 $result[$cs->id]['financial_type'] = $cs->contributionType;
285 $result[$cs->id]['receive_date'] = $cs->receive_date;
286 $result[$cs->id]['pcp_id'] = $cs->pcp_id;
287 $result[$cs->id]['pcp_title'] = $cs->pcp_title;
288 $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll;
289 $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname;
290 $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note;
291 $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus);
51fa20cb 292 $result[$cs->id]['sct_label'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $cs->soft_credit_type_id);
6dcc4d9d 293
294 if ($isTest) {
295 $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)';
296 }
297 }
298 return $result;
299 }
a45a51e3 300}
301