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