Merge pull request #6664 from eileenmcnaughton/comments-3
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
CommitLineData
a45a51e3 1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
a45a51e3 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
a45a51e3 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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
a45a51e3 32 */
33class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft {
34
8cb2c285 35 /**
fe482240 36 * Construct method.
8cb2c285 37 */
00be9182 38 public function __construct() {
a45a51e3 39 parent::__construct();
40 }
41
42 /**
fe482240 43 * Add contribution soft credit record.
a45a51e3 44 *
014c4014
TO
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
a45a51e3 47 *
a6c01b45
CW
48 * @return object
49 * soft contribution of object that is added
a45a51e3 50 */
07a88eec 51 public static function add(&$params) {
a45a51e3 52 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
53 $contributionSoft->copyValues($params);
6dcc4d9d 54
55 // set currency for CRM-1496
56 if (!isset($contributionSoft->currency)) {
57 $config = CRM_Core_Config::singleton();
58 $contributionSoft->currency = $config->defaultCurrency;
59 }
a45a51e3 60 return $contributionSoft->save();
61 }
62
63 /**
fe482240 64 * Fetch object based on array of properties.
a45a51e3 65 *
014c4014
TO
66 * @param array $params
67 * (reference ) an assoc array of name/value pairs.
68 * @param array $defaults
69 * (reference ) an assoc array to hold the flattened values.
a45a51e3 70 *
16b10e64 71 * @return CRM_Contribute_BAO_ContributionSoft
a45a51e3 72 */
00be9182 73 public static function retrieve(&$params, &$defaults) {
a45a51e3 74 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
75 $contributionSoft->copyValues($params);
76 if ($contributionSoft->find(TRUE)) {
77 CRM_Core_DAO::storeValues($contributionSoft, $defaults);
78 return $contributionSoft;
79 }
80 return NULL;
81 }
82
83 /**
fe482240 84 * Delete soft credits.
a45a51e3 85 *
c490a46a 86 * @param array $params
6c8f6e67 87 *
a45a51e3 88 */
00be9182 89 public static function del($params) {
a45a51e3 90 //delete from contribution soft table
91 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
22e263ad 92 foreach ($params as $column => $value) {
2cfc0f58 93 $contributionSoft->$column = $value;
94 }
a45a51e3 95 $contributionSoft->delete();
96 }
6dcc4d9d 97
186c9c17 98 /**
100fef9d 99 * @param int $contact_id
186c9c17
EM
100 * @param int $isTest
101 *
102 * @return array
103 */
00be9182 104 public 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
874c9be7 112 $params = array(
353ffa53 113 1 => array($contact_id, 'Integer'),
389bcebf 114 2 => array($isTest, 'Integer'),
353ffa53 115 );
6dcc4d9d 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++;
91bb24a7 125 $amount[] = $cs->amount;
126 $average[] = $cs->average;
6dcc4d9d 127 $currency[] = $cs->currency;
128 }
129 }
130
131 if ($count > 0) {
91bb24a7 132 return array(
133 implode(',&nbsp;', $amount),
6dcc4d9d 134 implode(',&nbsp;', $average),
135 implode(',&nbsp;', $currency),
136 );
137 }
138 return array(0, 0);
139 }
140
141 /**
100fef9d 142 * Retrieve soft contributions for contribution record.
6dcc4d9d 143 *
100fef9d 144 * @param int $contributionID
014c4014
TO
145 * @param bool $all
146 * Include PCP data.
dd244018 147 *
a6c01b45 148 * @return array
16b10e64 149 * Array of soft contribution ids, amounts, and associated contact ids
6dcc4d9d 150 */
00be9182 151 public static function getSoftContribution($contributionID, $all = FALSE) {
17db9f82
KJ
152 $pcpFields = array(
153 'pcp_id',
b6545333 154 'pcp_title',
17db9f82
KJ
155 'pcp_display_in_roll',
156 'pcp_roll_nickname',
157 'pcp_personal_note',
158 );
159
7ccf8829 160 $query = '
b6545333 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
7ccf8829 162 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
b6545333 163 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
7ccf8829
KJ
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()) {
b6545333 174 if ($dao->pcp_id) {
6dcc4d9d 175 if ($all) {
17db9f82 176 foreach ($pcpFields as $val) {
b6545333 177 $softContribution[$val] = $dao->$val;
6dcc4d9d 178 }
b6545333 179 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name;
180 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id;
6dcc4d9d 181 }
b6545333 182 }
183 else {
35729234 184 $softContribution['soft_credit'][$count] = array(
b6545333 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,
21dfd5f5 191 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id),
35729234
KJ
192 );
193 $count++;
6dcc4d9d 194 }
195 }
7ccf8829 196
6dcc4d9d 197 return $softContribution;
198 }
199
186c9c17 200 /**
100fef9d 201 * @param int $contributionID
186c9c17
EM
202 * @param bool $isPCP
203 *
204 * @return array
205 */
874c9be7 206 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
91bb24a7 207 $query = "
b6545333 208 SELECT id
2cfc0f58 209 FROM civicrm_contribution_soft
210 WHERE contribution_id = %1
211 ";
b6545333 212
213 if ($isPCP) {
214 $query .= " AND pcp_id IS NOT NULL";
215 }
1221efe9 216 else {
217 $query .= " AND pcp_id IS NULL";
218 }
2cfc0f58 219 $params = array(1 => array($contributionID, 'Integer'));
220
221 $dao = CRM_Core_DAO::executeQuery($query, $params);
222 $id = array();
7f880799 223 $type = '';
2cfc0f58 224 while ($dao->fetch()) {
b6545333 225 if ($isPCP) {
226 return $dao->id;
2cfc0f58 227 }
228 $id[] = $dao->id;
229 }
b6545333 230 return $id;
2cfc0f58 231 }
91bb24a7 232
6dcc4d9d 233 /**
91bb24a7 234 * Function to retrieve the list of soft contributions for given contact.
6dcc4d9d 235 *
014c4014
TO
236 * @param int $contact_id
237 * Contact id.
014c4014 238 * @param string $filter
389bcebf 239 * @param int $isTest
014c4014 240 * Additional filter criteria, later used in where clause.
dd244018
EM
241 *
242 * @return array
6dcc4d9d 243 */
00be9182 244 public static function getSoftContributionList($contact_id, $filter = NULL, $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
91ef9be0 268 ';
269
270 $where = "
271 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
272 if ($filter) {
273 $where .= $filter;
274 }
275
276 $query .= "{$where} ORDER BY cc.receive_date DESC";
8cb2c285
KJ
277
278 $params = array(
279 1 => array($contact_id, 'Integer'),
21dfd5f5 280 2 => array($isTest, 'Integer'),
8cb2c285
KJ
281 );
282 $cs = CRM_Core_DAO::executeQuery($query, $params);
6dcc4d9d 283 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
8cb2c285 284 $result = array();
6dcc4d9d 285 while ($cs->fetch()) {
286 $result[$cs->id]['amount'] = $cs->amount;
287 $result[$cs->id]['currency'] = $cs->currency;
288 $result[$cs->id]['contributor_id'] = $cs->contributor_id;
289 $result[$cs->id]['contribution_id'] = $cs->contribution_id;
290 $result[$cs->id]['contributor_name'] = $cs->display_name;
291 $result[$cs->id]['financial_type'] = $cs->contributionType;
292 $result[$cs->id]['receive_date'] = $cs->receive_date;
293 $result[$cs->id]['pcp_id'] = $cs->pcp_id;
294 $result[$cs->id]['pcp_title'] = $cs->pcp_title;
295 $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll;
296 $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname;
297 $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note;
298 $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus);
51fa20cb 299 $result[$cs->id]['sct_label'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $cs->soft_credit_type_id);
6dcc4d9d 300
301 if ($isTest) {
302 $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)';
303 }
304 }
305 return $result;
306 }
1421174e 307
186c9c17 308 /**
d424ffde
CW
309 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
310 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
311 * is assigned
312 *
c490a46a
CW
313 * @param CRM_Core_Form $form
314 * @param array $params
100fef9d
CW
315 * @param int $honoreeprofileId
316 * @param int $honorId
186c9c17 317 */
00be9182 318 public static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) {
1421174e 319 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($honoreeprofileId);
320 $profileFields = CRM_Core_BAO_UFGroup::getFields($honoreeprofileId);
321 $honoreeProfileFields = $values = array();
8af73472 322 $honorName = NULL;
1421174e 323
324 if ($honorId) {
325 CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params);
8af73472 326 if (empty($params)) {
327 foreach ($profileFields as $name => $field) {
328 $title = $field['title'];
329 $params[$field['name']] = $values[$title];
330 }
331 }
1421174e 332 }
333
334 //remove name related fields and construct name string with prefix/suffix
335 //which will be later assigned to template
336 switch ($profileContactType) {
337 case 'Individual':
338 if (array_key_exists('prefix_id', $params)) {
339 $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params),
340 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
341 );
342 unset($profileFields['prefix_id']);
343 }
874c9be7
TO
344 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
345 unset($profileFields['first_name']);
346 unset($profileFields['last_name']);
347 if (array_key_exists('suffix_id', $params)) {
348 $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params),
1421174e 349 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
350 );
874c9be7
TO
351 unset($profileFields['suffix_id']);
352 }
353 break;
354
355 case 'Organization':
356 $honorName = $params['organization_name'];
357 unset($profileFields['organization_name']);
358 break;
359
360 case 'Household':
361 $honorName = $params['household_name'];
362 unset($profileFields['household_name']);
363 break;
1421174e 364 }
365
366 if ($honorId) {
367 $honoreeProfileFields['Name'] = $honorName;
368 foreach ($profileFields as $name => $field) {
369 $title = $field['title'];
370 $honoreeProfileFields[$title] = $values[$title];
371 }
372 $form->assign('honoreeProfile', $honoreeProfileFields);
373 }
374 else {
375 $form->assign('honorName', $honorName);
376 }
377 }
96025800 378
a45a51e3 379}