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