Commit | Line | Data |
---|---|---|
a45a51e3 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
a45a51e3 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
a45a51e3 | 32 | */ |
33 | class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft { | |
34 | ||
8cb2c285 | 35 | /** |
fe482240 | 36 | * Construct method. |
8cb2c285 | 37 | */ |
00be9182 | 38 | public function __construct() { |
a45a51e3 | 39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
fe482240 | 43 | * Add contribution soft credit record. |
a45a51e3 | 44 | * |
014c4014 TO |
45 | * @param array $params |
46 | * (reference ) an assoc array of name/value pairs. | |
a45a51e3 | 47 | * |
a6c01b45 CW |
48 | * @return object |
49 | * soft contribution of object that is added | |
a45a51e3 | 50 | */ |
07a88eec | 51 | public static function add(&$params) { |
a45a51e3 | 52 | $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); |
53 | $contributionSoft->copyValues($params); | |
6dcc4d9d | 54 | |
55 | // set currency for CRM-1496 | |
56 | if (!isset($contributionSoft->currency)) { | |
57 | $config = CRM_Core_Config::singleton(); | |
58 | $contributionSoft->currency = $config->defaultCurrency; | |
59 | } | |
a45a51e3 | 60 | return $contributionSoft->save(); |
61 | } | |
62 | ||
7a13735b | 63 | /** |
64 | * Process the soft contribution and/or link to personal campaign page. | |
65 | * | |
66 | * @param array $params | |
67 | * @param object $contribution CRM_Contribute_DAO_Contribution | |
68 | * | |
69 | */ | |
70 | public static function processSoftContribution($params, $contribution) { | |
71 | //retrieve existing soft-credit and pcp id(s) if any against $contribution | |
72 | $softIDs = self::getSoftCreditIds($contribution->id); | |
73 | $pcpId = self::getSoftCreditIds($contribution->id, TRUE); | |
74 | ||
75 | if ($pcp = CRM_Utils_Array::value('pcp', $params)) { | |
76 | $softParams = array(); | |
77 | $softParams['id'] = $pcpId ? $pcpId : NULL; | |
78 | $softParams['contribution_id'] = $contribution->id; | |
79 | $softParams['pcp_id'] = $pcp['pcp_made_through_id']; | |
80 | $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', | |
81 | $pcp['pcp_made_through_id'], 'contact_id' | |
82 | ); | |
83 | $softParams['currency'] = $contribution->currency; | |
84 | $softParams['amount'] = $contribution->total_amount; | |
85 | $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp); | |
86 | $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp); | |
87 | $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp); | |
0571e734 | 88 | $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp'); |
7a13735b | 89 | $contributionSoft = self::add($softParams); |
90 | //Send notification to owner for PCP | |
91 | if ($contributionSoft->pcp_id && empty($pcpId)) { | |
92 | CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); | |
93 | } | |
94 | } | |
95 | //Delete PCP against this contribution and create new on submitted PCP information | |
96 | elseif (array_key_exists('pcp', $params) && $pcpId) { | |
fae69387 | 97 | civicrm_api3('ContributionSoft', 'delete', array('id' => $pcpId)); |
7a13735b | 98 | } |
99 | if (isset($params['soft_credit'])) { | |
100 | $softParams = $params['soft_credit']; | |
101 | foreach ($softParams as $softParam) { | |
102 | if (!empty($softIDs)) { | |
103 | $key = key($softIDs); | |
104 | $softParam['id'] = $softIDs[$key]; | |
105 | unset($softIDs[$key]); | |
106 | } | |
107 | $softParam['contribution_id'] = $contribution->id; | |
108 | $softParam['currency'] = $contribution->currency; | |
109 | //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default | |
110 | if (empty($softParam['amount'])) { | |
111 | $softParam['amount'] = $contribution->total_amount; | |
112 | } | |
113 | CRM_Contribute_BAO_ContributionSoft::add($softParam); | |
114 | } | |
115 | ||
116 | // delete any extra soft-credit while updating back-office contribution | |
117 | foreach ((array) $softIDs as $softID) { | |
118 | if (!in_array($softID, $params['soft_credit_ids'])) { | |
fae69387 | 119 | civicrm_api3('ContributionSoft', 'delete', array('id' => $softID)); |
7a13735b | 120 | } |
121 | } | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
126 | * Function used to save pcp / soft credit entry. | |
127 | * | |
128 | * This is used by contribution and also event pcps | |
129 | * | |
130 | * @param array $params | |
131 | * @param object $form | |
132 | * Form object. | |
133 | */ | |
134 | public static function formatSoftCreditParams(&$params, &$form) { | |
135 | $pcp = $softParams = $softIDs = array(); | |
136 | if (!empty($params['pcp_made_through_id'])) { | |
137 | $fields = array( | |
138 | 'pcp_made_through_id', | |
139 | 'pcp_display_in_roll', | |
140 | 'pcp_roll_nickname', | |
141 | 'pcp_personal_note', | |
142 | ); | |
143 | foreach ($fields as $f) { | |
144 | $pcp[$f] = CRM_Utils_Array::value($f, $params); | |
145 | } | |
146 | } | |
147 | ||
4779abb3 | 148 | if (!empty($form->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) { |
7a13735b | 149 | $honorId = NULL; |
150 | ||
7f90383e | 151 | // @todo fix use of deprecated function. |
0571e734 | 152 | $contributionSoftParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp'); |
7a13735b | 153 | //check if there is any duplicate contact |
d977e221 AH |
154 | // honoree should never be the donor |
155 | $exceptKeys = array( | |
156 | 'contactID' => 0, | |
157 | 'onbehalf_contact_id' => 0, | |
158 | ); | |
159 | $except = array_values(array_intersect_key($params, $exceptKeys)); | |
7f90383e | 160 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts( |
161 | $params['honor'], | |
162 | CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']), | |
163 | 'Unsupervised', | |
164 | $except, | |
165 | FALSE | |
166 | ); | |
7a13735b | 167 | if (count($ids)) { |
168 | $honorId = CRM_Utils_Array::value(0, $ids); | |
169 | } | |
170 | ||
171 | $honorId = CRM_Contact_BAO_Contact::createProfileContact( | |
172 | $params['honor'], CRM_Core_DAO::$_nullArray, | |
173 | $honorId, NULL, | |
4779abb3 | 174 | $form->_values['honoree_profile_id'] |
7a13735b | 175 | ); |
176 | $softParams[] = array( | |
177 | 'contact_id' => $honorId, | |
178 | 'soft_credit_type_id' => $params['soft_credit_type_id'], | |
179 | ); | |
180 | ||
181 | if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) { | |
182 | $form->_values['honor'] = array( | |
183 | 'soft_credit_type' => CRM_Utils_Array::value( | |
184 | $params['soft_credit_type_id'], | |
185 | CRM_Core_OptionGroup::values("soft_credit_type") | |
186 | ), | |
187 | 'honor_id' => $honorId, | |
4779abb3 | 188 | 'honor_profile_id' => $form->_values['honoree_profile_id'], |
7a13735b | 189 | 'honor_profile_values' => $params['honor'], |
190 | ); | |
191 | } | |
192 | } | |
193 | elseif (!empty($params['soft_credit_contact_id'])) { | |
194 | //build soft credit params | |
195 | foreach ($params['soft_credit_contact_id'] as $key => $val) { | |
196 | if ($val && $params['soft_credit_amount'][$key]) { | |
197 | $softParams[$key]['contact_id'] = $val; | |
198 | $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); | |
199 | $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key]; | |
200 | if (!empty($params['soft_credit_id'][$key])) { | |
201 | $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key]; | |
202 | } | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | $params['pcp'] = !empty($pcp) ? $pcp : NULL; | |
208 | $params['soft_credit'] = $softParams; | |
209 | $params['soft_credit_ids'] = $softIDs; | |
210 | } | |
211 | ||
a45a51e3 | 212 | /** |
fe482240 | 213 | * Fetch object based on array of properties. |
a45a51e3 | 214 | * |
014c4014 TO |
215 | * @param array $params |
216 | * (reference ) an assoc array of name/value pairs. | |
217 | * @param array $defaults | |
218 | * (reference ) an assoc array to hold the flattened values. | |
a45a51e3 | 219 | * |
16b10e64 | 220 | * @return CRM_Contribute_BAO_ContributionSoft |
a45a51e3 | 221 | */ |
00be9182 | 222 | public static function retrieve(&$params, &$defaults) { |
a45a51e3 | 223 | $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); |
224 | $contributionSoft->copyValues($params); | |
225 | if ($contributionSoft->find(TRUE)) { | |
226 | CRM_Core_DAO::storeValues($contributionSoft, $defaults); | |
227 | return $contributionSoft; | |
228 | } | |
229 | return NULL; | |
230 | } | |
231 | ||
186c9c17 | 232 | /** |
100fef9d | 233 | * @param int $contact_id |
186c9c17 EM |
234 | * @param int $isTest |
235 | * | |
236 | * @return array | |
237 | */ | |
00be9182 | 238 | public static function getSoftContributionTotals($contact_id, $isTest = 0) { |
879fda37 | 239 | |
971cdafb | 240 | $whereClause = "AND cc.cancel_date IS NULL"; |
879fda37 | 241 | |
971cdafb | 242 | $query = " |
8cb2c285 KJ |
243 | SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency |
244 | FROM civicrm_contribution_soft ccs | |
245 | LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id | |
971cdafb | 246 | WHERE cc.is_test = %2 AND ccs.contact_id = %1 {$whereClause} |
247 | GROUP BY currency"; | |
6dcc4d9d | 248 | |
874c9be7 | 249 | $params = array( |
353ffa53 | 250 | 1 => array($contact_id, 'Integer'), |
389bcebf | 251 | 2 => array($isTest, 'Integer'), |
353ffa53 | 252 | ); |
6dcc4d9d | 253 | |
254 | $cs = CRM_Core_DAO::executeQuery($query, $params); | |
255 | ||
256 | $count = 0; | |
971cdafb | 257 | $amount = $average = $cancelAmount = array(); |
6dcc4d9d | 258 | |
259 | while ($cs->fetch()) { | |
260 | if ($cs->amount > 0) { | |
261 | $count++; | |
91bb24a7 | 262 | $amount[] = $cs->amount; |
263 | $average[] = $cs->average; | |
6dcc4d9d | 264 | $currency[] = $cs->currency; |
265 | } | |
266 | } | |
879fda37 | 267 | |
971cdafb | 268 | //to get cancel amount |
269 | $cancelAmountWhereClause = "AND cc.cancel_date IS NOT NULL"; | |
270 | $query = str_replace($whereClause, $cancelAmountWhereClause, $query); | |
271 | $cancelAmountSQL = CRM_Core_DAO::executeQuery($query, $params); | |
272 | while ($cancelAmountSQL->fetch()) { | |
273 | if ($cancelAmountSQL->amount > 0) { | |
879fda37 | 274 | $count++; |
971cdafb | 275 | $cancelAmount[] = $cancelAmountSQL->amount; |
276 | } | |
277 | } | |
879fda37 | 278 | |
6dcc4d9d | 279 | if ($count > 0) { |
91bb24a7 | 280 | return array( |
281 | implode(', ', $amount), | |
6dcc4d9d | 282 | implode(', ', $average), |
283 | implode(', ', $currency), | |
971cdafb | 284 | implode(', ', $cancelAmount), |
6dcc4d9d | 285 | ); |
286 | } | |
287 | return array(0, 0); | |
288 | } | |
289 | ||
290 | /** | |
100fef9d | 291 | * Retrieve soft contributions for contribution record. |
6dcc4d9d | 292 | * |
100fef9d | 293 | * @param int $contributionID |
014c4014 TO |
294 | * @param bool $all |
295 | * Include PCP data. | |
dd244018 | 296 | * |
a6c01b45 | 297 | * @return array |
16b10e64 | 298 | * Array of soft contribution ids, amounts, and associated contact ids |
6dcc4d9d | 299 | */ |
00be9182 | 300 | public static function getSoftContribution($contributionID, $all = FALSE) { |
17db9f82 KJ |
301 | $pcpFields = array( |
302 | 'pcp_id', | |
b6545333 | 303 | 'pcp_title', |
17db9f82 KJ |
304 | 'pcp_display_in_roll', |
305 | 'pcp_roll_nickname', | |
306 | 'pcp_personal_note', | |
307 | ); | |
308 | ||
7ccf8829 | 309 | $query = ' |
b6545333 | 310 | 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 | 311 | FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id |
b6545333 | 312 | LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id |
7ccf8829 KJ |
313 | WHERE contribution_id = %1; |
314 | '; | |
315 | ||
316 | $params = array(1 => array($contributionID, 'Integer')); | |
317 | ||
318 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
319 | ||
320 | $softContribution = array(); | |
321 | $count = 1; | |
322 | while ($dao->fetch()) { | |
b6545333 | 323 | if ($dao->pcp_id) { |
6dcc4d9d | 324 | if ($all) { |
17db9f82 | 325 | foreach ($pcpFields as $val) { |
b6545333 | 326 | $softContribution[$val] = $dao->$val; |
6dcc4d9d | 327 | } |
b6545333 | 328 | $softContribution['pcp_soft_credit_to_name'] = $dao->display_name; |
329 | $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id; | |
6dcc4d9d | 330 | } |
b6545333 | 331 | } |
332 | else { | |
35729234 | 333 | $softContribution['soft_credit'][$count] = array( |
b6545333 | 334 | 'contact_id' => $dao->contact_id, |
335 | 'soft_credit_id' => $dao->id, | |
336 | 'currency' => $dao->currency, | |
337 | 'amount' => $dao->amount, | |
338 | 'contact_name' => $dao->display_name, | |
339 | 'soft_credit_type' => $dao->soft_credit_type_id, | |
b7617307 | 340 | 'soft_credit_type_label' => CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $dao->soft_credit_type_id), |
35729234 KJ |
341 | ); |
342 | $count++; | |
6dcc4d9d | 343 | } |
344 | } | |
7ccf8829 | 345 | |
6dcc4d9d | 346 | return $softContribution; |
347 | } | |
348 | ||
186c9c17 | 349 | /** |
100fef9d | 350 | * @param int $contributionID |
186c9c17 EM |
351 | * @param bool $isPCP |
352 | * | |
353 | * @return array | |
354 | */ | |
874c9be7 | 355 | public static function getSoftCreditIds($contributionID, $isPCP = FALSE) { |
91bb24a7 | 356 | $query = " |
b6545333 | 357 | SELECT id |
2cfc0f58 | 358 | FROM civicrm_contribution_soft |
359 | WHERE contribution_id = %1 | |
360 | "; | |
b6545333 | 361 | |
362 | if ($isPCP) { | |
363 | $query .= " AND pcp_id IS NOT NULL"; | |
364 | } | |
1221efe9 | 365 | else { |
366 | $query .= " AND pcp_id IS NULL"; | |
367 | } | |
2cfc0f58 | 368 | $params = array(1 => array($contributionID, 'Integer')); |
369 | ||
370 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
371 | $id = array(); | |
7f880799 | 372 | $type = ''; |
2cfc0f58 | 373 | while ($dao->fetch()) { |
b6545333 | 374 | if ($isPCP) { |
375 | return $dao->id; | |
2cfc0f58 | 376 | } |
377 | $id[] = $dao->id; | |
378 | } | |
b6545333 | 379 | return $id; |
2cfc0f58 | 380 | } |
91bb24a7 | 381 | |
dfe9afbf | 382 | /** |
383 | * Wrapper for ajax soft contribution selector. | |
384 | * | |
385 | * @param array $params | |
386 | * Associated array for params. | |
387 | * | |
388 | * @return array | |
389 | * Associated array of soft contributions | |
390 | */ | |
391 | public static function getSoftContributionSelector($params) { | |
392 | $isTest = 0; | |
393 | if (!empty($params['isTest'])) { | |
394 | $isTest = $params['isTest']; | |
395 | } | |
396 | // Format the params. | |
397 | $params['offset'] = ($params['page'] - 1) * $params['rp']; | |
398 | $params['rowCount'] = $params['rp']; | |
399 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); | |
400 | $contactId = $params['cid']; | |
401 | ||
402 | $filter = NULL; | |
403 | if ($params['context'] == 'membership' && !empty($params['entityID']) && $contactId) { | |
404 | $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$params['entityID']})"; | |
405 | } | |
406 | ||
407 | $softCreditList = self::getSoftContributionList($contactId, $filter, $isTest, $params); | |
408 | ||
409 | $softCreditListDT = array(); | |
410 | $softCreditListDT['data'] = array_values($softCreditList); | |
411 | $softCreditListDT['recordsTotal'] = $params['total']; | |
412 | $softCreditListDT['recordsFiltered'] = $params['total']; | |
413 | ||
414 | return $softCreditListDT; | |
415 | } | |
416 | ||
6dcc4d9d | 417 | /** |
91bb24a7 | 418 | * Function to retrieve the list of soft contributions for given contact. |
6dcc4d9d | 419 | * |
014c4014 TO |
420 | * @param int $contact_id |
421 | * Contact id. | |
014c4014 | 422 | * @param string $filter |
389bcebf | 423 | * @param int $isTest |
014c4014 | 424 | * Additional filter criteria, later used in where clause. |
dfe9afbf | 425 | * @param array $dTParams |
dd244018 EM |
426 | * |
427 | * @return array | |
6dcc4d9d | 428 | */ |
dfe9afbf | 429 | public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0, &$dTParams = NULL) { |
430 | $config = CRM_Core_Config::singleton(); | |
431 | $links = array( | |
432 | CRM_Core_Action::VIEW => array( | |
433 | 'name' => ts('View'), | |
434 | 'url' => 'civicrm/contact/view/contribution', | |
435 | 'qs' => 'reset=1&id=%%contributionid%%&cid=%%contactId%%&action=view&context=contribution&selectedChild=contribute', | |
436 | 'title' => ts('View related contribution'), | |
437 | ), | |
438 | ); | |
439 | $orderBy = 'cc.receive_date DESC'; | |
440 | if (!empty($dTParams['sort'])) { | |
441 | $orderBy = $dTParams['sort']; | |
442 | } | |
443 | $limit = ''; | |
444 | if (!empty($dTParams['rowCount']) && $dTParams['rowCount'] > 0) { | |
445 | $limit = " LIMIT {$dTParams['offset']}, {$dTParams['rowCount']} "; | |
446 | } | |
447 | $softOgId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'soft_credit_type', 'id', 'name'); | |
448 | $statusOgId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'contribution_status', 'id', 'name'); | |
449 | ||
8cb2c285 | 450 | $query = ' |
dfe9afbf | 451 | SELECT SQL_CALC_FOUND_ROWS ccs.id, ccs.amount as amount, |
8cb2c285 KJ |
452 | ccs.contribution_id, |
453 | ccs.pcp_id, | |
454 | ccs.pcp_display_in_roll, | |
455 | ccs.pcp_roll_nickname, | |
456 | ccs.pcp_personal_note, | |
51fa20cb | 457 | ccs.soft_credit_type_id, |
dfe9afbf | 458 | sov.label as sct_label, |
8cb2c285 KJ |
459 | cc.receive_date, |
460 | cc.contact_id as contributor_id, | |
461 | cc.contribution_status_id as contribution_status_id, | |
dfe9afbf | 462 | cov.label as contribution_status, |
8cb2c285 KJ |
463 | cp.title as pcp_title, |
464 | cc.currency, | |
dfe9afbf | 465 | contact.display_name as contributor_name, |
466 | cct.name as financial_type | |
8cb2c285 KJ |
467 | FROM civicrm_contribution_soft ccs |
468 | LEFT JOIN civicrm_contribution cc | |
469 | ON ccs.contribution_id = cc.id | |
470 | LEFT JOIN civicrm_pcp cp | |
471 | ON ccs.pcp_id = cp.id | |
472 | LEFT JOIN civicrm_contact contact ON | |
473 | ccs.contribution_id = cc.id AND cc.contact_id = contact.id | |
474 | LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id | |
dfe9afbf | 475 | LEFT JOIN civicrm_option_value sov ON sov.option_group_id = %3 AND ccs.soft_credit_type_id = sov.value |
476 | LEFT JOIN civicrm_option_value cov ON cov.option_group_id = %4 AND cc.contribution_status_id = cov.value | |
91ef9be0 | 477 | '; |
478 | ||
479 | $where = " | |
480 | WHERE cc.is_test = %2 AND ccs.contact_id = %1"; | |
481 | if ($filter) { | |
482 | $where .= $filter; | |
483 | } | |
484 | ||
dfe9afbf | 485 | $query .= "{$where} ORDER BY {$orderBy} {$limit}"; |
8cb2c285 KJ |
486 | |
487 | $params = array( | |
488 | 1 => array($contact_id, 'Integer'), | |
21dfd5f5 | 489 | 2 => array($isTest, 'Integer'), |
dfe9afbf | 490 | 3 => array($softOgId, 'Integer'), |
491 | 4 => array($statusOgId, 'Integer'), | |
8cb2c285 KJ |
492 | ); |
493 | $cs = CRM_Core_DAO::executeQuery($query, $params); | |
dfe9afbf | 494 | |
495 | $dTParams['total'] = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()'); | |
8cb2c285 | 496 | $result = array(); |
6dcc4d9d | 497 | while ($cs->fetch()) { |
ff8242a8 | 498 | $result[$cs->id]['amount'] = CRM_Utils_Money::format($cs->amount, $cs->currency); |
6dcc4d9d | 499 | $result[$cs->id]['currency'] = $cs->currency; |
500 | $result[$cs->id]['contributor_id'] = $cs->contributor_id; | |
501 | $result[$cs->id]['contribution_id'] = $cs->contribution_id; | |
dfe9afbf | 502 | $result[$cs->id]['contributor_name'] = CRM_Utils_System::href( |
503 | $cs->contributor_name, | |
504 | 'civicrm/contact/view', | |
505 | "reset=1&cid={$cs->contributor_id}" | |
506 | ); | |
507 | $result[$cs->id]['financial_type'] = $cs->financial_type; | |
508 | $result[$cs->id]['receive_date'] = CRM_Utils_Date::customFormat($cs->receive_date, $config->dateformatDatetime); | |
6dcc4d9d | 509 | $result[$cs->id]['pcp_id'] = $cs->pcp_id; |
dfe9afbf | 510 | $result[$cs->id]['pcp_title'] = ($cs->pcp_title) ? $cs->pcp_title : 'n/a'; |
6dcc4d9d | 511 | $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll; |
512 | $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname; | |
513 | $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note; | |
dfe9afbf | 514 | $result[$cs->id]['contribution_status'] = $cs->contribution_status; |
515 | $result[$cs->id]['sct_label'] = $cs->sct_label; | |
516 | $replace = array( | |
517 | 'contributionid' => $cs->contribution_id, | |
518 | 'contactId' => $cs->contributor_id, | |
519 | ); | |
520 | $result[$cs->id]['links'] = CRM_Core_Action::formLink($links, NULL, $replace); | |
6dcc4d9d | 521 | |
522 | if ($isTest) { | |
523 | $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)'; | |
524 | } | |
525 | } | |
526 | return $result; | |
527 | } | |
1421174e | 528 | |
186c9c17 | 529 | /** |
d424ffde CW |
530 | * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id) |
531 | * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName | |
532 | * is assigned | |
533 | * | |
c490a46a CW |
534 | * @param CRM_Core_Form $form |
535 | * @param array $params | |
100fef9d | 536 | * @param int $honorId |
186c9c17 | 537 | */ |
4779abb3 | 538 | public static function formatHonoreeProfileFields($form, $params, $honorId = NULL) { |
539 | if (empty($form->_values['honoree_profile_id'])) { | |
540 | return; | |
541 | } | |
542 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']); | |
543 | $profileFields = CRM_Core_BAO_UFGroup::getFields($form->_values['honoree_profile_id']); | |
1421174e | 544 | $honoreeProfileFields = $values = array(); |
8af73472 | 545 | $honorName = NULL; |
1421174e | 546 | |
547 | if ($honorId) { | |
548 | CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params); | |
8af73472 | 549 | if (empty($params)) { |
550 | foreach ($profileFields as $name => $field) { | |
551 | $title = $field['title']; | |
552 | $params[$field['name']] = $values[$title]; | |
553 | } | |
554 | } | |
1421174e | 555 | } |
556 | ||
557 | //remove name related fields and construct name string with prefix/suffix | |
558 | //which will be later assigned to template | |
559 | switch ($profileContactType) { | |
560 | case 'Individual': | |
561 | if (array_key_exists('prefix_id', $params)) { | |
562 | $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params), | |
563 | CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id') | |
564 | ); | |
565 | unset($profileFields['prefix_id']); | |
566 | } | |
874c9be7 TO |
567 | $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name']; |
568 | unset($profileFields['first_name']); | |
569 | unset($profileFields['last_name']); | |
570 | if (array_key_exists('suffix_id', $params)) { | |
571 | $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params), | |
1421174e | 572 | CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id') |
573 | ); | |
874c9be7 TO |
574 | unset($profileFields['suffix_id']); |
575 | } | |
576 | break; | |
577 | ||
578 | case 'Organization': | |
579 | $honorName = $params['organization_name']; | |
580 | unset($profileFields['organization_name']); | |
581 | break; | |
582 | ||
583 | case 'Household': | |
584 | $honorName = $params['household_name']; | |
585 | unset($profileFields['household_name']); | |
586 | break; | |
1421174e | 587 | } |
588 | ||
589 | if ($honorId) { | |
590 | $honoreeProfileFields['Name'] = $honorName; | |
591 | foreach ($profileFields as $name => $field) { | |
592 | $title = $field['title']; | |
593 | $honoreeProfileFields[$title] = $values[$title]; | |
594 | } | |
595 | $form->assign('honoreeProfile', $honoreeProfileFields); | |
596 | } | |
597 | else { | |
598 | $form->assign('honorName', $honorName); | |
599 | } | |
600 | } | |
96025800 | 601 | |
a45a51e3 | 602 | } |