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