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