Merge pull request #3220 from monishdeb/CRM-13981
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft {
36
37 /**
38 * construct method
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * function to add contribution soft credit record
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 */
53 public static function add(&$params) {
54 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
55 $contributionSoft->copyValues($params);
56
57 // set currency for CRM-1496
58 if (!isset($contributionSoft->currency)) {
59 $config = CRM_Core_Config::singleton();
60 $contributionSoft->currency = $config->defaultCurrency;
61 }
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 /**
90 * Function to delete soft credits
91 *
92 * @param $params
93 *
94 * @internal param int $contributionTypeId
95 * @static
96 */
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;
102 }
103 $contributionSoft->delete();
104 }
105
106 static function getSoftContributionTotals($contact_id, $isTest = 0) {
107 $query = '
108 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
109 FROM civicrm_contribution_soft ccs
110 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
111 WHERE cc.is_test = %2 AND ccs.contact_id = %1
112 GROUP BY currency';
113
114 $params = array(1 => array($contact_id, 'Integer'),
115 2 => array($isTest, 'Integer'));
116
117 $cs = CRM_Core_DAO::executeQuery($query, $params);
118
119 $count = 0;
120 $amount = $average = array();
121
122 while ($cs->fetch()) {
123 if ($cs->amount > 0) {
124 $count++;
125 $amount[] = $cs->amount;
126 $average[] = $cs->average;
127 $currency[] = $cs->currency;
128 }
129 }
130
131 if ($count > 0) {
132 return array(
133 implode(',&nbsp;', $amount),
134 implode(',&nbsp;', $average),
135 implode(',&nbsp;', $currency),
136 );
137 }
138 return array(0, 0);
139 }
140
141 /**
142 * Function to retrieve soft contributions for contribution record.
143 *
144 * @param $contributionID
145 * @param boolean $all include PCP data
146 *
147 * @internal param array $params an associated array
148 * @return array of soft contribution ids, amounts, and associated contact ids
149 * @static
150 */
151 static function getSoftContribution($contributionID, $all = FALSE) {
152 $pcpFields = array(
153 'pcp_id',
154 'pcp_title',
155 'pcp_display_in_roll',
156 'pcp_roll_nickname',
157 'pcp_personal_note',
158 );
159
160 $query = '
161 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
162 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
163 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
164 WHERE contribution_id = %1;
165 ';
166
167 $params = array(1 => array($contributionID, 'Integer'));
168
169 $dao = CRM_Core_DAO::executeQuery($query, $params);
170
171 $softContribution = array();
172 $count = 1;
173 while ($dao->fetch()) {
174 if ($dao->pcp_id) {
175 if ($all) {
176 foreach ($pcpFields as $val) {
177 $softContribution[$val] = $dao->$val;
178 }
179 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name;
180 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id;
181 }
182 }
183 else {
184 $softContribution['soft_credit'][$count] = array(
185 'contact_id' => $dao->contact_id,
186 'soft_credit_id' => $dao->id,
187 'currency' => $dao->currency,
188 'amount' => $dao->amount,
189 'contact_name' => $dao->display_name,
190 'soft_credit_type' => $dao->soft_credit_type_id,
191 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id)
192 );
193 $count++;
194 }
195 }
196
197 return $softContribution;
198 }
199
200 static function getSoftCreditIds($contributionID , $isPCP = FALSE) {
201 $query = "
202 SELECT id
203 FROM civicrm_contribution_soft
204 WHERE contribution_id = %1
205 ";
206
207 if ($isPCP) {
208 $query .= " AND pcp_id IS NOT NULL";
209 }
210 else {
211 $query .= " AND pcp_id IS NULL";
212 }
213 $params = array(1 => array($contributionID, 'Integer'));
214
215 $dao = CRM_Core_DAO::executeQuery($query, $params);
216 $id = array();
217 $type = '';
218 while ($dao->fetch()) {
219 if ($isPCP) {
220 return $dao->id;
221 }
222 $id[] = $dao->id;
223 }
224 return $id;
225 }
226
227 /**
228 * Function to retrieve the list of soft contributions for given contact.
229 *
230 * @param int $contact_id contact id
231 * @param int $isTest
232 * @param string $filter additional filter criteria, later used in where clause
233 *
234 * @return array
235 * @static
236 */
237 static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) {
238 $query = '
239 SELECT ccs.id, ccs.amount as amount,
240 ccs.contribution_id,
241 ccs.pcp_id,
242 ccs.pcp_display_in_roll,
243 ccs.pcp_roll_nickname,
244 ccs.pcp_personal_note,
245 ccs.soft_credit_type_id,
246 cc.receive_date,
247 cc.contact_id as contributor_id,
248 cc.contribution_status_id as contribution_status_id,
249 cp.title as pcp_title,
250 cc.currency,
251 contact.display_name,
252 cct.name as contributionType
253 FROM civicrm_contribution_soft ccs
254 LEFT JOIN civicrm_contribution cc
255 ON ccs.contribution_id = cc.id
256 LEFT JOIN civicrm_pcp cp
257 ON ccs.pcp_id = cp.id
258 LEFT JOIN civicrm_contact contact ON
259 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
260 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
261 ';
262
263 $where = "
264 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
265 if ($filter) {
266 $where .= $filter;
267 }
268
269 $query .= "{$where} 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);
276 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
277 $result = array();
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);
292 $result[$cs->id]['sct_label'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $cs->soft_credit_type_id);
293
294 if ($isTest) {
295 $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)';
296 }
297 }
298 return $result;
299 }
300
301 /*
302 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
303 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
304 * is assigned
305 *
306 * @static
307 */
308
309 static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) {
310 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($honoreeprofileId);
311 $profileFields = CRM_Core_BAO_UFGroup::getFields($honoreeprofileId);
312 $honoreeProfileFields = $values = array();
313 $honorName = NULL;
314
315 if ($honorId) {
316 CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params);
317 if (empty($params)) {
318 foreach ($profileFields as $name => $field) {
319 $title = $field['title'];
320 $params[$field['name']] = $values[$title];
321 }
322 }
323 }
324
325 //remove name related fields and construct name string with prefix/suffix
326 //which will be later assigned to template
327 switch ($profileContactType) {
328 case 'Individual':
329 if (array_key_exists('prefix_id', $params)) {
330 $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params),
331 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
332 );
333 unset($profileFields['prefix_id']);
334 }
335 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
336 unset($profileFields['first_name']);
337 unset($profileFields['last_name']);
338 if (array_key_exists('suffix_id', $params)) {
339 $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params),
340 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
341 );
342 unset($profileFields['suffix_id']);
343 }
344 break;
345 case 'Organization':
346 $honorName = $params['organization_name'];
347 unset($profileFields['organization_name']);
348 break;
349 case 'Household':
350 $honorName = $params['household_name'];
351 unset($profileFields['household_name']);
352 break;
353 }
354
355 if ($honorId) {
356 $honoreeProfileFields['Name'] = $honorName;
357 foreach ($profileFields as $name => $field) {
358 $title = $field['title'];
359 $honoreeProfileFields[$title] = $values[$title];
360 }
361 $form->assign('honoreeProfile', $honoreeProfileFields);
362 }
363 else {
364 $form->assign('honorName', $honorName);
365 }
366 }
367 }
368