Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
[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 /**
107 * @param $contact_id
108 * @param int $isTest
109 *
110 * @return array
111 */
112 static function getSoftContributionTotals($contact_id, $isTest = 0) {
113 $query = '
114 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
115 FROM civicrm_contribution_soft ccs
116 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
117 WHERE cc.is_test = %2 AND ccs.contact_id = %1
118 GROUP BY currency';
119
120 $params = array(1 => array($contact_id, 'Integer'),
121 2 => array($isTest, 'Integer'));
122
123 $cs = CRM_Core_DAO::executeQuery($query, $params);
124
125 $count = 0;
126 $amount = $average = array();
127
128 while ($cs->fetch()) {
129 if ($cs->amount > 0) {
130 $count++;
131 $amount[] = $cs->amount;
132 $average[] = $cs->average;
133 $currency[] = $cs->currency;
134 }
135 }
136
137 if ($count > 0) {
138 return array(
139 implode(',&nbsp;', $amount),
140 implode(',&nbsp;', $average),
141 implode(',&nbsp;', $currency),
142 );
143 }
144 return array(0, 0);
145 }
146
147 /**
148 * Function to retrieve soft contributions for contribution record.
149 *
150 * @param $contributionID
151 * @param boolean $all include PCP data
152 *
153 * @internal param array $params an associated array
154 * @return array of soft contribution ids, amounts, and associated contact ids
155 * @static
156 */
157 static function getSoftContribution($contributionID, $all = FALSE) {
158 $pcpFields = array(
159 'pcp_id',
160 'pcp_title',
161 'pcp_display_in_roll',
162 'pcp_roll_nickname',
163 'pcp_personal_note',
164 );
165
166 $query = '
167 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
168 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
169 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
170 WHERE contribution_id = %1;
171 ';
172
173 $params = array(1 => array($contributionID, 'Integer'));
174
175 $dao = CRM_Core_DAO::executeQuery($query, $params);
176
177 $softContribution = array();
178 $count = 1;
179 while ($dao->fetch()) {
180 if ($dao->pcp_id) {
181 if ($all) {
182 foreach ($pcpFields as $val) {
183 $softContribution[$val] = $dao->$val;
184 }
185 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name;
186 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id;
187 }
188 }
189 else {
190 $softContribution['soft_credit'][$count] = array(
191 'contact_id' => $dao->contact_id,
192 'soft_credit_id' => $dao->id,
193 'currency' => $dao->currency,
194 'amount' => $dao->amount,
195 'contact_name' => $dao->display_name,
196 'soft_credit_type' => $dao->soft_credit_type_id,
197 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id)
198 );
199 $count++;
200 }
201 }
202
203 return $softContribution;
204 }
205
206 /**
207 * @param $contributionID
208 * @param bool $isPCP
209 *
210 * @return array
211 */
212 static function getSoftCreditIds($contributionID , $isPCP = FALSE) {
213 $query = "
214 SELECT id
215 FROM civicrm_contribution_soft
216 WHERE contribution_id = %1
217 ";
218
219 if ($isPCP) {
220 $query .= " AND pcp_id IS NOT NULL";
221 }
222 else {
223 $query .= " AND pcp_id IS NULL";
224 }
225 $params = array(1 => array($contributionID, 'Integer'));
226
227 $dao = CRM_Core_DAO::executeQuery($query, $params);
228 $id = array();
229 $type = '';
230 while ($dao->fetch()) {
231 if ($isPCP) {
232 return $dao->id;
233 }
234 $id[] = $dao->id;
235 }
236 return $id;
237 }
238
239 /**
240 * Function to retrieve the list of soft contributions for given contact.
241 *
242 * @param int $contact_id contact id
243 * @param int $isTest
244 * @param string $filter additional filter criteria, later used in where clause
245 *
246 * @return array
247 * @static
248 */
249 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 $form
323 * @param $params
324 * @param $honoreeprofileId
325 * @param null $honorId
326 */
327 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 case 'Organization':
364 $honorName = $params['organization_name'];
365 unset($profileFields['organization_name']);
366 break;
367 case 'Household':
368 $honorName = $params['household_name'];
369 unset($profileFields['household_name']);
370 break;
371 }
372
373 if ($honorId) {
374 $honoreeProfileFields['Name'] = $honorName;
375 foreach ($profileFields as $name => $field) {
376 $title = $field['title'];
377 $honoreeProfileFields[$title] = $values[$title];
378 }
379 $form->assign('honoreeProfile', $honoreeProfileFields);
380 }
381 else {
382 $form->assign('honorName', $honorName);
383 }
384 }
385 }
386