INFRA-132 - Remove extra newlines from the bottom of docblocks
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Add contribution soft credit record
46 *
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 *
50 * @return object
51 * soft contribution of object that is added
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 * Fetch object based on array of properties
67 *
68 * @param array $params
69 * (reference ) an assoc array of name/value pairs.
70 * @param array $defaults
71 * (reference ) an assoc array to hold the flattened values.
72 *
73 * @return CRM_Contribute_BAO_ContributionSoft object
74 * @static
75 */
76 public static function retrieve(&$params, &$defaults) {
77 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
78 $contributionSoft->copyValues($params);
79 if ($contributionSoft->find(TRUE)) {
80 CRM_Core_DAO::storeValues($contributionSoft, $defaults);
81 return $contributionSoft;
82 }
83 return NULL;
84 }
85
86 /**
87 * Delete soft credits
88 *
89 * @param array $params
90 *
91 * @static
92 */
93 public static function del($params) {
94 //delete from contribution soft table
95 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
96 foreach ($params as $column => $value) {
97 $contributionSoft->$column = $value;
98 }
99 $contributionSoft->delete();
100 }
101
102 /**
103 * @param int $contact_id
104 * @param int $isTest
105 *
106 * @return array
107 */
108 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
109 $query = '
110 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
111 FROM civicrm_contribution_soft ccs
112 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
113 WHERE cc.is_test = %2 AND ccs.contact_id = %1
114 GROUP BY currency';
115
116 $params = array(
117 1 => array($contact_id, 'Integer'),
118 2 => array($isTest, 'Integer'));
119
120 $cs = CRM_Core_DAO::executeQuery($query, $params);
121
122 $count = 0;
123 $amount = $average = array();
124
125 while ($cs->fetch()) {
126 if ($cs->amount > 0) {
127 $count++;
128 $amount[] = $cs->amount;
129 $average[] = $cs->average;
130 $currency[] = $cs->currency;
131 }
132 }
133
134 if ($count > 0) {
135 return array(
136 implode(',&nbsp;', $amount),
137 implode(',&nbsp;', $average),
138 implode(',&nbsp;', $currency),
139 );
140 }
141 return array(0, 0);
142 }
143
144 /**
145 * Retrieve soft contributions for contribution record.
146 *
147 * @param int $contributionID
148 * @param bool $all
149 * Include PCP data.
150 *
151 * @return array
152 * of soft contribution ids, amounts, and associated contact ids
153 * @static
154 */
155 public static function getSoftContribution($contributionID, $all = FALSE) {
156 $pcpFields = array(
157 'pcp_id',
158 'pcp_title',
159 'pcp_display_in_roll',
160 'pcp_roll_nickname',
161 'pcp_personal_note',
162 );
163
164 $query = '
165 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
166 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
167 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
168 WHERE contribution_id = %1;
169 ';
170
171 $params = array(1 => array($contributionID, 'Integer'));
172
173 $dao = CRM_Core_DAO::executeQuery($query, $params);
174
175 $softContribution = array();
176 $count = 1;
177 while ($dao->fetch()) {
178 if ($dao->pcp_id) {
179 if ($all) {
180 foreach ($pcpFields as $val) {
181 $softContribution[$val] = $dao->$val;
182 }
183 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name;
184 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id;
185 }
186 }
187 else {
188 $softContribution['soft_credit'][$count] = array(
189 'contact_id' => $dao->contact_id,
190 'soft_credit_id' => $dao->id,
191 'currency' => $dao->currency,
192 'amount' => $dao->amount,
193 'contact_name' => $dao->display_name,
194 'soft_credit_type' => $dao->soft_credit_type_id,
195 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id),
196 );
197 $count++;
198 }
199 }
200
201 return $softContribution;
202 }
203
204 /**
205 * @param int $contributionID
206 * @param bool $isPCP
207 *
208 * @return array
209 */
210 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
211 $query = "
212 SELECT id
213 FROM civicrm_contribution_soft
214 WHERE contribution_id = %1
215 ";
216
217 if ($isPCP) {
218 $query .= " AND pcp_id IS NOT NULL";
219 }
220 else {
221 $query .= " AND pcp_id IS NULL";
222 }
223 $params = array(1 => array($contributionID, 'Integer'));
224
225 $dao = CRM_Core_DAO::executeQuery($query, $params);
226 $id = array();
227 $type = '';
228 while ($dao->fetch()) {
229 if ($isPCP) {
230 return $dao->id;
231 }
232 $id[] = $dao->id;
233 }
234 return $id;
235 }
236
237 /**
238 * Function to retrieve the list of soft contributions for given contact.
239 *
240 * @param int $contact_id
241 * Contact id.
242 * @param int $isTest
243 * @param string $filter
244 * Additional filter criteria, later used in where clause.
245 *
246 * @return array
247 * @static
248 */
249 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) {
250 $query = '
251 SELECT ccs.id, ccs.amount as amount,
252 ccs.contribution_id,
253 ccs.pcp_id,
254 ccs.pcp_display_in_roll,
255 ccs.pcp_roll_nickname,
256 ccs.pcp_personal_note,
257 ccs.soft_credit_type_id,
258 cc.receive_date,
259 cc.contact_id as contributor_id,
260 cc.contribution_status_id as contribution_status_id,
261 cp.title as pcp_title,
262 cc.currency,
263 contact.display_name,
264 cct.name as contributionType
265 FROM civicrm_contribution_soft ccs
266 LEFT JOIN civicrm_contribution cc
267 ON ccs.contribution_id = cc.id
268 LEFT JOIN civicrm_pcp cp
269 ON ccs.pcp_id = cp.id
270 LEFT JOIN civicrm_contact contact ON
271 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
272 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
273 ';
274
275 $where = "
276 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
277 if ($filter) {
278 $where .= $filter;
279 }
280
281 $query .= "{$where} ORDER BY cc.receive_date DESC";
282
283 $params = array(
284 1 => array($contact_id, 'Integer'),
285 2 => array($isTest, 'Integer'),
286 );
287 $cs = CRM_Core_DAO::executeQuery($query, $params);
288 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
289 $result = array();
290 while ($cs->fetch()) {
291 $result[$cs->id]['amount'] = $cs->amount;
292 $result[$cs->id]['currency'] = $cs->currency;
293 $result[$cs->id]['contributor_id'] = $cs->contributor_id;
294 $result[$cs->id]['contribution_id'] = $cs->contribution_id;
295 $result[$cs->id]['contributor_name'] = $cs->display_name;
296 $result[$cs->id]['financial_type'] = $cs->contributionType;
297 $result[$cs->id]['receive_date'] = $cs->receive_date;
298 $result[$cs->id]['pcp_id'] = $cs->pcp_id;
299 $result[$cs->id]['pcp_title'] = $cs->pcp_title;
300 $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll;
301 $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname;
302 $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note;
303 $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus);
304 $result[$cs->id]['sct_label'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $cs->soft_credit_type_id);
305
306 if ($isTest) {
307 $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)';
308 }
309 }
310 return $result;
311 }
312
313 /*
314 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
315 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
316 * is assigned
317 *
318 * @static
319 */
320
321 /**
322 * @param CRM_Core_Form $form
323 * @param array $params
324 * @param int $honoreeprofileId
325 * @param int $honorId
326 */
327 public static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) {
328 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($honoreeprofileId);
329 $profileFields = CRM_Core_BAO_UFGroup::getFields($honoreeprofileId);
330 $honoreeProfileFields = $values = array();
331 $honorName = NULL;
332
333 if ($honorId) {
334 CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params);
335 if (empty($params)) {
336 foreach ($profileFields as $name => $field) {
337 $title = $field['title'];
338 $params[$field['name']] = $values[$title];
339 }
340 }
341 }
342
343 //remove name related fields and construct name string with prefix/suffix
344 //which will be later assigned to template
345 switch ($profileContactType) {
346 case 'Individual':
347 if (array_key_exists('prefix_id', $params)) {
348 $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params),
349 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
350 );
351 unset($profileFields['prefix_id']);
352 }
353 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
354 unset($profileFields['first_name']);
355 unset($profileFields['last_name']);
356 if (array_key_exists('suffix_id', $params)) {
357 $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params),
358 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
359 );
360 unset($profileFields['suffix_id']);
361 }
362 break;
363
364 case 'Organization':
365 $honorName = $params['organization_name'];
366 unset($profileFields['organization_name']);
367 break;
368
369 case 'Household':
370 $honorName = $params['household_name'];
371 unset($profileFields['household_name']);
372 break;
373 }
374
375 if ($honorId) {
376 $honoreeProfileFields['Name'] = $honorName;
377 foreach ($profileFields as $name => $field) {
378 $title = $field['title'];
379 $honoreeProfileFields[$title] = $values[$title];
380 }
381 $form->assign('honoreeProfile', $honoreeProfileFields);
382 }
383 else {
384 $form->assign('honorName', $honorName);
385 }
386 }
387 }