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