| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant { |
| 34 | |
| 35 | /** |
| 36 | * Static field for all the participant information that we can potentially import. |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | static $_importableFields = NULL; |
| 41 | |
| 42 | /** |
| 43 | * Static field for all the participant information that we can potentially export. |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | static $_exportableFields = NULL; |
| 48 | |
| 49 | /** |
| 50 | * Static array for valid status transitions rules. |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | static $_statusTransitionsRules = array( |
| 55 | 'Pending from pay later' => array('Registered', 'Cancelled'), |
| 56 | 'Pending from incomplete transaction' => array('Registered', 'Cancelled'), |
| 57 | 'On waitlist' => array('Cancelled', 'Pending from waitlist'), |
| 58 | 'Pending from waitlist' => array('Registered', 'Cancelled'), |
| 59 | 'Awaiting approval' => array('Cancelled', 'Pending from approval'), |
| 60 | 'Pending from approval' => array('Registered', 'Cancelled'), |
| 61 | ); |
| 62 | |
| 63 | /** |
| 64 | */ |
| 65 | public function __construct() { |
| 66 | parent::__construct(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Takes an associative array and creates a participant object. |
| 71 | * |
| 72 | * the function extract all the params it needs to initialize the create a |
| 73 | * participant object. the params array could contain additional unused name/value |
| 74 | * pairs |
| 75 | * |
| 76 | * @param array $params |
| 77 | * (reference ) an assoc array of name/value pairs. |
| 78 | * |
| 79 | * @return CRM_Event_BAO_Participant |
| 80 | */ |
| 81 | public static function &add(&$params) { |
| 82 | |
| 83 | if (!empty($params['id'])) { |
| 84 | CRM_Utils_Hook::pre('edit', 'Participant', $params['id'], $params); |
| 85 | } |
| 86 | else { |
| 87 | CRM_Utils_Hook::pre('create', 'Participant', NULL, $params); |
| 88 | } |
| 89 | |
| 90 | // converting dates to mysql format |
| 91 | if (!empty($params['register_date'])) { |
| 92 | $params['register_date'] = CRM_Utils_Date::isoToMysql($params['register_date']); |
| 93 | } |
| 94 | |
| 95 | if (!empty($params['participant_fee_amount'])) { |
| 96 | $params['participant_fee_amount'] = CRM_Utils_Rule::cleanMoney($params['participant_fee_amount']); |
| 97 | } |
| 98 | |
| 99 | if (!empty($params['fee_amount'])) { |
| 100 | $params['fee_amount'] = CRM_Utils_Rule::cleanMoney($params['fee_amount']); |
| 101 | } |
| 102 | |
| 103 | // ensure that role ids are encoded as a string |
| 104 | if (isset($params['role_id']) && is_array($params['role_id'])) { |
| 105 | if (in_array(key($params['role_id']), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
| 106 | $op = key($params['role_id']); |
| 107 | $params['role_id'] = $params['role_id'][$op]; |
| 108 | } |
| 109 | else { |
| 110 | $params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $participantBAO = new CRM_Event_BAO_Participant(); |
| 115 | if (!empty($params['id'])) { |
| 116 | $participantBAO->id = CRM_Utils_Array::value('id', $params); |
| 117 | $participantBAO->find(TRUE); |
| 118 | $participantBAO->register_date = CRM_Utils_Date::isoToMysql($participantBAO->register_date); |
| 119 | } |
| 120 | |
| 121 | $participantBAO->copyValues($params); |
| 122 | |
| 123 | //CRM-6910 |
| 124 | //1. If currency present, it should be valid one. |
| 125 | //2. We should have currency when amount is not null. |
| 126 | $currency = $participantBAO->fee_currency; |
| 127 | if ($currency || |
| 128 | !CRM_Utils_System::isNull($participantBAO->fee_amount) |
| 129 | ) { |
| 130 | if (!CRM_Utils_Rule::currencyCode($currency)) { |
| 131 | $config = CRM_Core_Config::singleton(); |
| 132 | $currency = $config->defaultCurrency; |
| 133 | } |
| 134 | } |
| 135 | $participantBAO->fee_currency = $currency; |
| 136 | |
| 137 | $participantBAO->save(); |
| 138 | |
| 139 | $session = CRM_Core_Session::singleton(); |
| 140 | |
| 141 | // reset the group contact cache for this group |
| 142 | CRM_Contact_BAO_GroupContactCache::remove(); |
| 143 | |
| 144 | if (!empty($params['id'])) { |
| 145 | CRM_Utils_Hook::post('edit', 'Participant', $participantBAO->id, $participantBAO); |
| 146 | } |
| 147 | else { |
| 148 | CRM_Utils_Hook::post('create', 'Participant', $participantBAO->id, $participantBAO); |
| 149 | } |
| 150 | |
| 151 | return $participantBAO; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Given the list of params in the params array, fetch the object |
| 156 | * and store the values in the values array |
| 157 | * |
| 158 | * @param array $params |
| 159 | * Input parameters to find object. |
| 160 | * @param array $values |
| 161 | * Output values of the object. |
| 162 | * |
| 163 | * @param $ids |
| 164 | * |
| 165 | * @return CRM_Event_BAO_Participant|null the found object or null |
| 166 | */ |
| 167 | public static function getValues(&$params, &$values, &$ids) { |
| 168 | if (empty($params)) { |
| 169 | return NULL; |
| 170 | } |
| 171 | $participant = new CRM_Event_BAO_Participant(); |
| 172 | $participant->copyValues($params); |
| 173 | $participant->find(); |
| 174 | $participants = array(); |
| 175 | while ($participant->fetch()) { |
| 176 | $ids['participant'] = $participant->id; |
| 177 | CRM_Core_DAO::storeValues($participant, $values[$participant->id]); |
| 178 | $participants[$participant->id] = $participant; |
| 179 | } |
| 180 | return $participants; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Takes an associative array and creates a participant object. |
| 185 | * |
| 186 | * @param array $params |
| 187 | * (reference ) an assoc array of name/value pairs. |
| 188 | * |
| 189 | * @return CRM_Event_BAO_Participant |
| 190 | */ |
| 191 | public static function create(&$params) { |
| 192 | |
| 193 | $transaction = new CRM_Core_Transaction(); |
| 194 | $status = NULL; |
| 195 | |
| 196 | if (!empty($params['id'])) { |
| 197 | $status = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $params['id'], 'status_id'); |
| 198 | } |
| 199 | |
| 200 | $participant = self::add($params); |
| 201 | |
| 202 | if (is_a($participant, 'CRM_Core_Error')) { |
| 203 | $transaction->rollback(); |
| 204 | return $participant; |
| 205 | } |
| 206 | |
| 207 | if ((!CRM_Utils_Array::value('id', $params)) || |
| 208 | (isset($params['status_id']) && $params['status_id'] != $status) |
| 209 | ) { |
| 210 | CRM_Activity_BAO_Activity::addActivity($participant); |
| 211 | } |
| 212 | |
| 213 | //CRM-5403 |
| 214 | //for update mode |
| 215 | if (self::isPrimaryParticipant($participant->id) && $status) { |
| 216 | self::updateParticipantStatus($participant->id, $status, $participant->status_id); |
| 217 | } |
| 218 | |
| 219 | $session = CRM_Core_Session::singleton(); |
| 220 | $id = $session->get('userID'); |
| 221 | if (!$id) { |
| 222 | $id = CRM_Utils_Array::value('contact_id', $params); |
| 223 | } |
| 224 | |
| 225 | // add custom field values |
| 226 | if (!empty($params['custom']) && |
| 227 | is_array($params['custom']) |
| 228 | ) { |
| 229 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_participant', $participant->id); |
| 230 | } |
| 231 | |
| 232 | //process note, CRM-7634 |
| 233 | $noteId = NULL; |
| 234 | if (!empty($params['id'])) { |
| 235 | $note = CRM_Core_BAO_Note::getNote($params['id'], 'civicrm_participant'); |
| 236 | $noteId = key($note); |
| 237 | } |
| 238 | $noteValue = NULL; |
| 239 | $hasNoteField = FALSE; |
| 240 | foreach (array( |
| 241 | 'note', |
| 242 | 'participant_note', |
| 243 | ) as $noteFld) { |
| 244 | if (array_key_exists($noteFld, $params)) { |
| 245 | $noteValue = $params[$noteFld]; |
| 246 | $hasNoteField = TRUE; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | if ($noteId || $noteValue) { |
| 251 | if ($noteValue) { |
| 252 | $noteParams = array( |
| 253 | 'entity_table' => 'civicrm_participant', |
| 254 | 'note' => $noteValue, |
| 255 | 'entity_id' => $participant->id, |
| 256 | 'contact_id' => $id, |
| 257 | 'modified_date' => date('Ymd'), |
| 258 | ); |
| 259 | $noteIDs = array(); |
| 260 | if ($noteId) { |
| 261 | $noteIDs['id'] = $noteId; |
| 262 | } |
| 263 | CRM_Core_BAO_Note::add($noteParams, $noteIDs); |
| 264 | } |
| 265 | elseif ($noteId && $hasNoteField) { |
| 266 | CRM_Core_BAO_Note::del($noteId, FALSE); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Log the information on successful add/edit of Participant data. |
| 271 | $logParams = array( |
| 272 | 'entity_table' => 'civicrm_participant', |
| 273 | 'entity_id' => $participant->id, |
| 274 | 'data' => CRM_Event_PseudoConstant::participantStatus($participant->status_id), |
| 275 | 'modified_id' => $id, |
| 276 | 'modified_date' => date('Ymd'), |
| 277 | ); |
| 278 | |
| 279 | CRM_Core_BAO_Log::add($logParams); |
| 280 | |
| 281 | $params['participant_id'] = $participant->id; |
| 282 | |
| 283 | $transaction->commit(); |
| 284 | |
| 285 | // do not add to recent items for import, CRM-4399 |
| 286 | if (empty($params['skipRecentView'])) { |
| 287 | |
| 288 | $url = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 289 | "action=view&reset=1&id={$participant->id}&cid={$participant->contact_id}&context=home" |
| 290 | ); |
| 291 | |
| 292 | $recentOther = array(); |
| 293 | if (CRM_Core_Permission::check('edit event participants')) { |
| 294 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 295 | "action=update&reset=1&id={$participant->id}&cid={$participant->contact_id}&context=home" |
| 296 | ); |
| 297 | } |
| 298 | if (CRM_Core_Permission::check('delete in CiviEvent')) { |
| 299 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 300 | "action=delete&reset=1&id={$participant->id}&cid={$participant->contact_id}&context=home" |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); |
| 305 | |
| 306 | if ($participant->role_id) { |
| 307 | $role = explode(CRM_Core_DAO::VALUE_SEPARATOR, $participant->role_id); |
| 308 | |
| 309 | foreach ($role as & $roleValue) { |
| 310 | if (isset($roleValue)) { |
| 311 | $roleValue = $participantRoles[$roleValue]; |
| 312 | } |
| 313 | } |
| 314 | $roles = implode(', ', $role); |
| 315 | } |
| 316 | |
| 317 | $roleString = empty($roles) ? '' : $roles; |
| 318 | $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $participant->event_id, 'title'); |
| 319 | $title = CRM_Contact_BAO_Contact::displayName($participant->contact_id) . ' (' . $roleString . ' - ' . $eventTitle . ')'; |
| 320 | |
| 321 | // add the recently created Participant |
| 322 | CRM_Utils_Recent::add($title, |
| 323 | $url, |
| 324 | $participant->id, |
| 325 | 'Participant', |
| 326 | $participant->contact_id, |
| 327 | NULL, |
| 328 | $recentOther |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | return $participant; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Check whether the event is full for participation and return as. |
| 337 | * per requirements. |
| 338 | * |
| 339 | * @param int $eventId |
| 340 | * Event id. |
| 341 | * @param bool $returnEmptySeats |
| 342 | * Are we require number if empty seats. |
| 343 | * @param bool $includeWaitingList |
| 344 | * Consider waiting list in event full. |
| 345 | * calculation or not. (it is for cron job purpose) |
| 346 | * |
| 347 | * @param bool $returnWaitingCount |
| 348 | * @param bool $considerTestParticipant |
| 349 | * |
| 350 | * @return bool|int|null|string |
| 351 | * 1. false => If event having some empty spaces. |
| 352 | */ |
| 353 | public static function eventFull( |
| 354 | $eventId, |
| 355 | $returnEmptySeats = FALSE, |
| 356 | $includeWaitingList = TRUE, |
| 357 | $returnWaitingCount = FALSE, |
| 358 | $considerTestParticipant = FALSE |
| 359 | ) { |
| 360 | $result = NULL; |
| 361 | if (!$eventId) { |
| 362 | return $result; |
| 363 | } |
| 364 | |
| 365 | // consider event is full when. |
| 366 | // 1. (count(is_counted) >= event_size) or |
| 367 | // 2. (count(participants-with-status-on-waitlist) > 0) |
| 368 | // It might be case there are some empty spaces and still event |
| 369 | // is full, as waitlist might represent group require spaces > empty. |
| 370 | |
| 371 | $participantRoles = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1'); |
| 372 | $countedStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); |
| 373 | $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); |
| 374 | $onWaitlistStatusId = array_search('On waitlist', $waitingStatuses); |
| 375 | |
| 376 | //when we do require only waiting count don't consider counted. |
| 377 | if (!$returnWaitingCount && !empty($countedStatuses)) { |
| 378 | $allStatusIds = array_keys($countedStatuses); |
| 379 | } |
| 380 | |
| 381 | $where = array(' event.id = %1 '); |
| 382 | if (!$considerTestParticipant) { |
| 383 | $where[] = ' ( participant.is_test = 0 OR participant.is_test IS NULL ) '; |
| 384 | } |
| 385 | if (!empty($participantRoles)) { |
| 386 | $where[] = ' participant.role_id IN ( ' . implode(', ', array_keys($participantRoles)) . ' ) '; |
| 387 | } |
| 388 | |
| 389 | $eventParams = array(1 => array($eventId, 'Positive')); |
| 390 | |
| 391 | //in case any waiting, straight forward event is full. |
| 392 | if ($includeWaitingList && $onWaitlistStatusId) { |
| 393 | |
| 394 | //build the where clause. |
| 395 | $whereClause = ' WHERE ' . implode(' AND ', $where); |
| 396 | $whereClause .= " AND participant.status_id = $onWaitlistStatusId "; |
| 397 | $eventSeatsWhere = implode(' AND ', $where) . " AND ( participant.status_id = $onWaitlistStatusId )"; |
| 398 | |
| 399 | $query = " |
| 400 | SELECT participant.id id, |
| 401 | event.event_full_text as event_full_text |
| 402 | FROM civicrm_participant participant |
| 403 | INNER JOIN civicrm_event event ON ( event.id = participant.event_id ) |
| 404 | {$whereClause}"; |
| 405 | |
| 406 | $eventFullText = ts('This event is full.'); |
| 407 | $participants = CRM_Core_DAO::executeQuery($query, $eventParams); |
| 408 | while ($participants->fetch()) { |
| 409 | //oops here event is full and we don't want waiting count. |
| 410 | if ($returnWaitingCount) { |
| 411 | return CRM_Event_BAO_Event::eventTotalSeats($eventId, $eventSeatsWhere); |
| 412 | } |
| 413 | else { |
| 414 | return ($participants->event_full_text) ? $participants->event_full_text : $eventFullText; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | //consider only counted participants. |
| 420 | $where[] = ' participant.status_id IN ( ' . implode(', ', array_keys($countedStatuses)) . ' ) '; |
| 421 | $whereClause = ' WHERE ' . implode(' AND ', $where); |
| 422 | $eventSeatsWhere = implode(' AND ', $where); |
| 423 | |
| 424 | $query = " |
| 425 | SELECT participant.id id, |
| 426 | event.event_full_text as event_full_text, |
| 427 | event.max_participants as max_participants |
| 428 | FROM civicrm_participant participant |
| 429 | INNER JOIN civicrm_event event ON ( event.id = participant.event_id ) |
| 430 | {$whereClause}"; |
| 431 | |
| 432 | $eventMaxSeats = NULL; |
| 433 | $eventFullText = ts('This event is full.'); |
| 434 | $participants = CRM_Core_DAO::executeQuery($query, $eventParams); |
| 435 | while ($participants->fetch()) { |
| 436 | if ($participants->event_full_text) { |
| 437 | $eventFullText = $participants->event_full_text; |
| 438 | } |
| 439 | $eventMaxSeats = $participants->max_participants; |
| 440 | //don't have limit for event seats. |
| 441 | if ($participants->max_participants == NULL) { |
| 442 | return $result; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | //get the total event seats occupied by these participants. |
| 447 | $eventRegisteredSeats = CRM_Event_BAO_Event::eventTotalSeats($eventId, $eventSeatsWhere); |
| 448 | |
| 449 | if ($eventRegisteredSeats) { |
| 450 | if ($eventRegisteredSeats >= $eventMaxSeats) { |
| 451 | $result = $eventFullText; |
| 452 | } |
| 453 | elseif ($returnEmptySeats) { |
| 454 | $result = $eventMaxSeats - $eventRegisteredSeats; |
| 455 | } |
| 456 | return $result; |
| 457 | } |
| 458 | else { |
| 459 | $query = ' |
| 460 | SELECT event.event_full_text, |
| 461 | event.max_participants |
| 462 | FROM civicrm_event event |
| 463 | WHERE event.id = %1'; |
| 464 | $event = CRM_Core_DAO::executeQuery($query, $eventParams); |
| 465 | while ($event->fetch()) { |
| 466 | $eventFullText = $event->event_full_text; |
| 467 | $eventMaxSeats = $event->max_participants; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // no limit for registration. |
| 472 | if ($eventMaxSeats == NULL) { |
| 473 | return $result; |
| 474 | } |
| 475 | if ($eventMaxSeats) { |
| 476 | return ($returnEmptySeats) ? (int) $eventMaxSeats : FALSE; |
| 477 | } |
| 478 | |
| 479 | return $eventFullText; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Return the array of all price set field options, |
| 484 | * with total participant count that field going to carry. |
| 485 | * |
| 486 | * @param int $eventId |
| 487 | * Event id. |
| 488 | * @param array $skipParticipantIds |
| 489 | * An array of participant ids those we should skip. |
| 490 | * @param bool $considerCounted |
| 491 | * @param bool $considerWaiting |
| 492 | * @param bool $considerTestParticipants |
| 493 | * |
| 494 | * @return array |
| 495 | * an array of each option id and total count |
| 496 | */ |
| 497 | public static function priceSetOptionsCount( |
| 498 | $eventId, |
| 499 | $skipParticipantIds = array(), |
| 500 | $considerCounted = TRUE, |
| 501 | $considerWaiting = TRUE, |
| 502 | $considerTestParticipants = FALSE |
| 503 | ) { |
| 504 | $optionsCount = array(); |
| 505 | if (!$eventId) { |
| 506 | return $optionsCount; |
| 507 | } |
| 508 | |
| 509 | $allStatusIds = array(); |
| 510 | if ($considerCounted) { |
| 511 | $countedStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); |
| 512 | $allStatusIds = array_merge($allStatusIds, array_keys($countedStatuses)); |
| 513 | } |
| 514 | if ($considerWaiting) { |
| 515 | $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); |
| 516 | $allStatusIds = array_merge($allStatusIds, array_keys($waitingStatuses)); |
| 517 | } |
| 518 | $statusIdClause = NULL; |
| 519 | if (!empty($allStatusIds)) { |
| 520 | $statusIdClause = ' AND participant.status_id IN ( ' . implode(', ', array_values($allStatusIds)) . ')'; |
| 521 | } |
| 522 | |
| 523 | $isTestClause = NULL; |
| 524 | if (!$considerTestParticipants) { |
| 525 | $isTestClause = ' AND ( participant.is_test IS NULL OR participant.is_test = 0 )'; |
| 526 | } |
| 527 | |
| 528 | $skipParticipantClause = NULL; |
| 529 | if (is_array($skipParticipantIds) && !empty($skipParticipantIds)) { |
| 530 | $skipParticipantClause = ' AND participant.id NOT IN ( ' . implode(', ', $skipParticipantIds) . ')'; |
| 531 | } |
| 532 | |
| 533 | $sql = " |
| 534 | SELECT line.id as lineId, |
| 535 | line.entity_id as entity_id, |
| 536 | line.qty, |
| 537 | value.id as valueId, |
| 538 | value.count, |
| 539 | field.html_type |
| 540 | FROM civicrm_line_item line |
| 541 | INNER JOIN civicrm_participant participant ON ( line.entity_table = 'civicrm_participant' |
| 542 | AND participant.id = line.entity_id ) |
| 543 | INNER JOIN civicrm_price_field_value value ON ( value.id = line.price_field_value_id ) |
| 544 | INNER JOIN civicrm_price_field field ON ( value.price_field_id = field.id ) |
| 545 | WHERE participant.event_id = %1 |
| 546 | {$statusIdClause} |
| 547 | {$isTestClause} |
| 548 | {$skipParticipantClause}"; |
| 549 | |
| 550 | $lineItem = CRM_Core_DAO::executeQuery($sql, array(1 => array($eventId, 'Positive'))); |
| 551 | while ($lineItem->fetch()) { |
| 552 | $count = $lineItem->count; |
| 553 | if (!$count) { |
| 554 | $count = 1; |
| 555 | } |
| 556 | if ($lineItem->html_type == 'Text') { |
| 557 | $count *= $lineItem->qty; |
| 558 | } |
| 559 | $optionsCount[$lineItem->valueId] = $count + CRM_Utils_Array::value($lineItem->valueId, $optionsCount, 0); |
| 560 | } |
| 561 | |
| 562 | return $optionsCount; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Get the empty spaces for event those we can allocate |
| 567 | * to pending participant to become confirm. |
| 568 | * |
| 569 | * @param int $eventId |
| 570 | * Event id. |
| 571 | * |
| 572 | * @return int |
| 573 | * $spaces Number of Empty Seats/null. |
| 574 | */ |
| 575 | public static function pendingToConfirmSpaces($eventId) { |
| 576 | $emptySeats = 0; |
| 577 | if (!$eventId) { |
| 578 | return $emptySeats; |
| 579 | } |
| 580 | |
| 581 | $positiveStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Positive'"); |
| 582 | $statusIds = '(' . implode(',', array_keys($positiveStatuses)) . ')'; |
| 583 | |
| 584 | $query = " |
| 585 | SELECT count(participant.id) as registered, |
| 586 | civicrm_event.max_participants |
| 587 | FROM civicrm_participant participant, civicrm_event |
| 588 | WHERE participant.event_id = {$eventId} |
| 589 | AND civicrm_event.id = participant.event_id |
| 590 | AND participant.status_id IN {$statusIds} |
| 591 | GROUP BY participant.event_id |
| 592 | "; |
| 593 | $dao = CRM_Core_DAO::executeQuery($query); |
| 594 | if ($dao->fetch()) { |
| 595 | |
| 596 | //unlimited space. |
| 597 | if ($dao->max_participants == NULL || $dao->max_participants <= 0) { |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | //no space. |
| 602 | if ($dao->registered >= $dao->max_participants) { |
| 603 | return $emptySeats; |
| 604 | } |
| 605 | |
| 606 | //difference. |
| 607 | return $dao->max_participants - $dao->registered; |
| 608 | } |
| 609 | |
| 610 | //space in case no registeration yet. |
| 611 | return CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'max_participants'); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Combine all the importable fields from the lower levels object. |
| 616 | * |
| 617 | * @param string $contactType |
| 618 | * @param bool $status |
| 619 | * @param bool $onlyParticipant |
| 620 | * |
| 621 | * @return array |
| 622 | * array of importable Fields |
| 623 | */ |
| 624 | public static function &importableFields($contactType = 'Individual', $status = TRUE, $onlyParticipant = FALSE) { |
| 625 | if (!self::$_importableFields) { |
| 626 | if (!$onlyParticipant) { |
| 627 | if (!$status) { |
| 628 | $fields = array('' => array('title' => ts('- do not import -'))); |
| 629 | } |
| 630 | else { |
| 631 | $fields = array('' => array('title' => ts('- Participant Fields -'))); |
| 632 | } |
| 633 | } |
| 634 | else { |
| 635 | $fields = array(); |
| 636 | } |
| 637 | |
| 638 | $tmpFields = CRM_Event_DAO_Participant::import(); |
| 639 | |
| 640 | $note = array( |
| 641 | 'participant_note' => array( |
| 642 | 'title' => ts('Participant Note'), |
| 643 | 'name' => 'participant_note', |
| 644 | 'headerPattern' => '/(participant.)?note$/i', |
| 645 | ), |
| 646 | ); |
| 647 | |
| 648 | // Split status and status id into 2 fields |
| 649 | // Fixme: it would be better to leave as 1 field and intelligently handle both during import |
| 650 | $participantStatus = array( |
| 651 | 'participant_status' => array( |
| 652 | 'title' => ts('Participant Status'), |
| 653 | 'name' => 'participant_status', |
| 654 | 'data_type' => CRM_Utils_Type::T_STRING, |
| 655 | ), |
| 656 | ); |
| 657 | $tmpFields['participant_status_id']['title'] = ts('Participant Status Id'); |
| 658 | |
| 659 | // Split role and role id into 2 fields |
| 660 | // Fixme: it would be better to leave as 1 field and intelligently handle both during import |
| 661 | $participantRole = array( |
| 662 | 'participant_role' => array( |
| 663 | 'title' => ts('Participant Role'), |
| 664 | 'name' => 'participant_role', |
| 665 | 'data_type' => CRM_Utils_Type::T_STRING, |
| 666 | ), |
| 667 | ); |
| 668 | $tmpFields['participant_role_id']['title'] = ts('Participant Role Id'); |
| 669 | |
| 670 | $eventType = array( |
| 671 | 'event_type' => array( |
| 672 | 'title' => ts('Event Type'), |
| 673 | 'name' => 'event_type', |
| 674 | 'data_type' => CRM_Utils_Type::T_STRING, |
| 675 | ), |
| 676 | ); |
| 677 | |
| 678 | $tmpContactField = $contactFields = array(); |
| 679 | $contactFields = array(); |
| 680 | if (!$onlyParticipant) { |
| 681 | $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); |
| 682 | |
| 683 | // Using new Dedupe rule. |
| 684 | $ruleParams = array( |
| 685 | 'contact_type' => $contactType, |
| 686 | 'used' => 'Unsupervised', |
| 687 | ); |
| 688 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
| 689 | |
| 690 | if (is_array($fieldsArray)) { |
| 691 | foreach ($fieldsArray as $value) { |
| 692 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 693 | $value, |
| 694 | 'id', |
| 695 | 'column_name' |
| 696 | ); |
| 697 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; |
| 698 | $tmpContactField[trim($value)] = CRM_Utils_Array::value(trim($value), $contactFields); |
| 699 | if (!$status) { |
| 700 | $title = $tmpContactField[trim($value)]['title'] . ' (match to contact)'; |
| 701 | } |
| 702 | else { |
| 703 | $title = $tmpContactField[trim($value)]['title']; |
| 704 | } |
| 705 | |
| 706 | $tmpContactField[trim($value)]['title'] = $title; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | $extIdentifier = CRM_Utils_Array::value('external_identifier', $contactFields); |
| 711 | if ($extIdentifier) { |
| 712 | $tmpContactField['external_identifier'] = $extIdentifier; |
| 713 | $tmpContactField['external_identifier']['title'] = CRM_Utils_Array::value('title', $extIdentifier) . ' (match to contact)'; |
| 714 | } |
| 715 | $tmpFields['participant_contact_id']['title'] = $tmpFields['participant_contact_id']['title'] . ' (match to contact)'; |
| 716 | |
| 717 | $fields = array_merge($fields, $tmpContactField); |
| 718 | $fields = array_merge($fields, $tmpFields); |
| 719 | $fields = array_merge($fields, $note, $participantStatus, $participantRole, $eventType); |
| 720 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Participant')); |
| 721 | |
| 722 | self::$_importableFields = $fields; |
| 723 | } |
| 724 | |
| 725 | return self::$_importableFields; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Combine all the exportable fields from the lower levels object. |
| 730 | * |
| 731 | * @return array |
| 732 | * array of exportable Fields |
| 733 | */ |
| 734 | public static function &exportableFields() { |
| 735 | if (!self::$_exportableFields) { |
| 736 | if (!self::$_exportableFields) { |
| 737 | self::$_exportableFields = array(); |
| 738 | } |
| 739 | |
| 740 | $fields = array(); |
| 741 | |
| 742 | $participantFields = CRM_Event_DAO_Participant::export(); |
| 743 | $eventFields = CRM_Event_DAO_Event::export(); |
| 744 | $noteField = array( |
| 745 | 'participant_note' => array( |
| 746 | 'title' => 'Participant Note', |
| 747 | 'name' => 'participant_note', |
| 748 | 'type' => CRM_Utils_Type::T_STRING, |
| 749 | ), |
| 750 | ); |
| 751 | |
| 752 | $participantStatus = array( |
| 753 | 'participant_status' => array( |
| 754 | 'title' => 'Participant Status', |
| 755 | 'name' => 'participant_status', |
| 756 | 'type' => CRM_Utils_Type::T_STRING, |
| 757 | ), |
| 758 | ); |
| 759 | |
| 760 | $participantRole = array( |
| 761 | 'participant_role' => array( |
| 762 | 'title' => 'Participant Role', |
| 763 | 'name' => 'participant_role', |
| 764 | 'type' => CRM_Utils_Type::T_STRING, |
| 765 | ), |
| 766 | ); |
| 767 | |
| 768 | $discountFields = CRM_Core_DAO_Discount::export(); |
| 769 | |
| 770 | $fields = array_merge($participantFields, $participantStatus, $participantRole, $eventFields, $noteField, $discountFields); |
| 771 | |
| 772 | // add custom data |
| 773 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Participant')); |
| 774 | self::$_exportableFields = $fields; |
| 775 | } |
| 776 | |
| 777 | return self::$_exportableFields; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Get the event name/sort name for a particular participation / participant |
| 782 | * |
| 783 | * @param int $participantId |
| 784 | * Id of the participant. |
| 785 | * |
| 786 | * @return array |
| 787 | * associated array with sort_name and event title |
| 788 | */ |
| 789 | public static function participantDetails($participantId) { |
| 790 | $query = " |
| 791 | SELECT civicrm_contact.sort_name as name, civicrm_event.title as title, civicrm_contact.id as cid |
| 792 | FROM civicrm_participant |
| 793 | LEFT JOIN civicrm_event ON (civicrm_participant.event_id = civicrm_event.id) |
| 794 | LEFT JOIN civicrm_contact ON (civicrm_participant.contact_id = civicrm_contact.id) |
| 795 | WHERE civicrm_participant.id = {$participantId} |
| 796 | "; |
| 797 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 798 | |
| 799 | $details = array(); |
| 800 | while ($dao->fetch()) { |
| 801 | $details['name'] = $dao->name; |
| 802 | $details['title'] = $dao->title; |
| 803 | $details['cid'] = $dao->cid; |
| 804 | } |
| 805 | |
| 806 | return $details; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Get the values for pseudoconstants for name->value and reverse. |
| 811 | * |
| 812 | * @param array $defaults |
| 813 | * (reference) the default values, some of which need to be resolved. |
| 814 | * @param bool $reverse |
| 815 | * True if we want to resolve the values in the reverse direction (value -> name). |
| 816 | */ |
| 817 | public static function resolveDefaults(&$defaults, $reverse = FALSE) { |
| 818 | self::lookupValue($defaults, 'event', CRM_Event_PseudoConstant::event(), $reverse); |
| 819 | self::lookupValue($defaults, 'status', CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'), $reverse); |
| 820 | self::lookupValue($defaults, 'role', CRM_Event_PseudoConstant::participantRole(), $reverse); |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Convert associative array names to values and vice-versa. |
| 825 | * |
| 826 | * This function is used by both the web form layer and the api. Note that |
| 827 | * the api needs the name => value conversion, also the view layer typically |
| 828 | * requires value => name conversion |
| 829 | */ |
| 830 | public static function lookupValue(&$defaults, $property, $lookup, $reverse) { |
| 831 | $id = $property . '_id'; |
| 832 | |
| 833 | $src = $reverse ? $property : $id; |
| 834 | $dst = $reverse ? $id : $property; |
| 835 | |
| 836 | if (!array_key_exists($src, $defaults)) { |
| 837 | return FALSE; |
| 838 | } |
| 839 | |
| 840 | $look = $reverse ? array_flip($lookup) : $lookup; |
| 841 | |
| 842 | if (is_array($look)) { |
| 843 | if (!array_key_exists($defaults[$src], $look)) { |
| 844 | return FALSE; |
| 845 | } |
| 846 | } |
| 847 | $defaults[$dst] = $look[$defaults[$src]]; |
| 848 | return TRUE; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Delete the records that are associated with this participation. |
| 853 | * |
| 854 | * @param int $id |
| 855 | * Id of the participation to delete. |
| 856 | * |
| 857 | * @return \CRM_Event_DAO_Participant |
| 858 | */ |
| 859 | public static function deleteParticipant($id) { |
| 860 | CRM_Utils_Hook::pre('delete', 'Participant', $id, CRM_Core_DAO::$_nullArray); |
| 861 | |
| 862 | $transaction = new CRM_Core_Transaction(); |
| 863 | |
| 864 | //delete activity record |
| 865 | $params = array( |
| 866 | 'source_record_id' => $id, |
| 867 | // activity type id for event registration |
| 868 | 'activity_type_id' => 5, |
| 869 | ); |
| 870 | |
| 871 | CRM_Activity_BAO_Activity::deleteActivity($params); |
| 872 | |
| 873 | // delete the participant payment record |
| 874 | // we need to do this since the cascaded constraints |
| 875 | // dont work with join tables |
| 876 | $p = array('participant_id' => $id); |
| 877 | CRM_Event_BAO_ParticipantPayment::deleteParticipantPayment($p); |
| 878 | |
| 879 | // cleanup line items. |
| 880 | $participantsId = array(); |
| 881 | $participantsId = self::getAdditionalParticipantIds($id); |
| 882 | $participantsId[] = $id; |
| 883 | CRM_Price_BAO_LineItem::deleteLineItems($participantsId, 'civicrm_participant'); |
| 884 | |
| 885 | //delete note when participant deleted. |
| 886 | $note = CRM_Core_BAO_Note::getNote($id, 'civicrm_participant'); |
| 887 | $noteId = key($note); |
| 888 | if ($noteId) { |
| 889 | CRM_Core_BAO_Note::del($noteId, FALSE); |
| 890 | } |
| 891 | |
| 892 | $participant = new CRM_Event_DAO_Participant(); |
| 893 | $participant->id = $id; |
| 894 | $participant->delete(); |
| 895 | |
| 896 | $transaction->commit(); |
| 897 | |
| 898 | CRM_Utils_Hook::post('delete', 'Participant', $participant->id, $participant); |
| 899 | |
| 900 | // delete the recently created Participant |
| 901 | $participantRecent = array( |
| 902 | 'id' => $id, |
| 903 | 'type' => 'Participant', |
| 904 | ); |
| 905 | |
| 906 | CRM_Utils_Recent::del($participantRecent); |
| 907 | |
| 908 | return $participant; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Checks duplicate participants. |
| 913 | * |
| 914 | * @param array $input |
| 915 | * An assosiative array of name /value pairs. |
| 916 | * from other function |
| 917 | * @param array $duplicates |
| 918 | * (reference ) an assoc array of name/value pairs. |
| 919 | * |
| 920 | * @return CRM_Contribute_BAO_Contribution |
| 921 | */ |
| 922 | public static function checkDuplicate($input, &$duplicates) { |
| 923 | $eventId = CRM_Utils_Array::value('event_id', $input); |
| 924 | $contactId = CRM_Utils_Array::value('contact_id', $input); |
| 925 | |
| 926 | $clause = array(); |
| 927 | $input = array(); |
| 928 | |
| 929 | if ($eventId) { |
| 930 | $clause[] = "event_id = %1"; |
| 931 | $input[1] = array($eventId, 'Integer'); |
| 932 | } |
| 933 | |
| 934 | if ($contactId) { |
| 935 | $clause[] = "contact_id = %2"; |
| 936 | $input[2] = array($contactId, 'Integer'); |
| 937 | } |
| 938 | |
| 939 | if (empty($clause)) { |
| 940 | return FALSE; |
| 941 | } |
| 942 | |
| 943 | $clause = implode(' AND ', $clause); |
| 944 | |
| 945 | $query = "SELECT id FROM civicrm_participant WHERE $clause"; |
| 946 | $dao = CRM_Core_DAO::executeQuery($query, $input); |
| 947 | $result = FALSE; |
| 948 | while ($dao->fetch()) { |
| 949 | $duplicates[] = $dao->id; |
| 950 | $result = TRUE; |
| 951 | } |
| 952 | return $result; |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Fix the event level. |
| 957 | * |
| 958 | * When price sets are used as event fee, fee_level is set as ^A |
| 959 | * separated string. We need to change that string to comma |
| 960 | * separated string before using fee_level in view mode. |
| 961 | * |
| 962 | * @param string $eventLevel |
| 963 | * Event_level string from db. |
| 964 | */ |
| 965 | public static function fixEventLevel(&$eventLevel) { |
| 966 | if ((substr($eventLevel, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && |
| 967 | (substr($eventLevel, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) |
| 968 | ) { |
| 969 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($eventLevel, 1, -1))); |
| 970 | $pos = strrpos($eventLevel, '(multiple participants)', 0); |
| 971 | if ($pos) { |
| 972 | $eventLevel = substr_replace($eventLevel, "", $pos - 3, 1); |
| 973 | } |
| 974 | } |
| 975 | elseif ((substr($eventLevel, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR)) { |
| 976 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 977 | substr($eventLevel, 0, 1) |
| 978 | )); |
| 979 | } |
| 980 | elseif ((substr($eventLevel, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)) { |
| 981 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 982 | substr($eventLevel, 0, -1) |
| 983 | )); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * Get the additional participant ids. |
| 989 | * |
| 990 | * @param int $primaryParticipantId |
| 991 | * Primary partycipant Id. |
| 992 | * @param bool $excludeCancel |
| 993 | * Do not include participant those are cancelled. |
| 994 | * |
| 995 | * @param int $oldStatusId |
| 996 | * |
| 997 | * @return array |
| 998 | */ |
| 999 | public static function getAdditionalParticipantIds($primaryParticipantId, $excludeCancel = TRUE, $oldStatusId = NULL) { |
| 1000 | $additionalParticipantIds = array(); |
| 1001 | if (!$primaryParticipantId) { |
| 1002 | return $additionalParticipantIds; |
| 1003 | } |
| 1004 | |
| 1005 | $where = "participant.registered_by_id={$primaryParticipantId}"; |
| 1006 | if ($excludeCancel) { |
| 1007 | $cancelStatusId = 0; |
| 1008 | $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"); |
| 1009 | $cancelStatusId = array_search('Cancelled', $negativeStatuses); |
| 1010 | $where .= " AND participant.status_id != {$cancelStatusId}"; |
| 1011 | } |
| 1012 | |
| 1013 | if ($oldStatusId) { |
| 1014 | $where .= " AND participant.status_id = {$oldStatusId}"; |
| 1015 | } |
| 1016 | |
| 1017 | $query = " |
| 1018 | SELECT participant.id |
| 1019 | FROM civicrm_participant participant |
| 1020 | WHERE {$where}"; |
| 1021 | |
| 1022 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1023 | while ($dao->fetch()) { |
| 1024 | $additionalParticipantIds[$dao->id] = $dao->id; |
| 1025 | } |
| 1026 | return $additionalParticipantIds; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * Get the event fee info for given participant ids |
| 1031 | * either from line item table / participant table. |
| 1032 | * |
| 1033 | * @param array $participantIds |
| 1034 | * Participant ids. |
| 1035 | * @param bool $hasLineItems |
| 1036 | * Do fetch from line items. |
| 1037 | * |
| 1038 | * @return array |
| 1039 | */ |
| 1040 | public function getFeeDetails($participantIds, $hasLineItems = FALSE) { |
| 1041 | $feeDetails = array(); |
| 1042 | if (!is_array($participantIds) || empty($participantIds)) { |
| 1043 | return $feeDetails; |
| 1044 | } |
| 1045 | |
| 1046 | $select = ' |
| 1047 | SELECT participant.id as id, |
| 1048 | participant.fee_level as fee_level, |
| 1049 | participant.fee_amount as fee_amount'; |
| 1050 | $from = 'FROM civicrm_participant participant'; |
| 1051 | if ($hasLineItems) { |
| 1052 | $select .= ' , |
| 1053 | lineItem.id as lineId, |
| 1054 | lineItem.label as label, |
| 1055 | lineItem.qty as qty, |
| 1056 | lineItem.unit_price as unit_price, |
| 1057 | lineItem.line_total as line_total, |
| 1058 | field.label as field_title, |
| 1059 | field.html_type as html_type, |
| 1060 | field.id as price_field_id, |
| 1061 | value.id as price_field_value_id, |
| 1062 | value.description as description, |
| 1063 | IF( value.count, value.count, 0 ) as participant_count'; |
| 1064 | $from .= " |
| 1065 | INNER JOIN civicrm_line_item lineItem ON ( lineItem.entity_table = 'civicrm_participant' |
| 1066 | AND lineItem.entity_id = participant.id ) |
| 1067 | INNER JOIN civicrm_price_field field ON ( field.id = lineItem.price_field_id ) |
| 1068 | INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_value_id ) |
| 1069 | "; |
| 1070 | } |
| 1071 | $where = 'WHERE participant.id IN ( ' . implode(', ', $participantIds) . ' )'; |
| 1072 | $query = "$select $from $where"; |
| 1073 | |
| 1074 | $feeInfo = CRM_Core_DAO::executeQuery($query); |
| 1075 | $feeProperties = array('fee_level', 'fee_amount'); |
| 1076 | $lineProperties = array( |
| 1077 | 'lineId', |
| 1078 | 'label', |
| 1079 | 'qty', |
| 1080 | 'unit_price', |
| 1081 | 'line_total', |
| 1082 | 'field_title', |
| 1083 | 'html_type', |
| 1084 | 'price_field_id', |
| 1085 | 'participant_count', |
| 1086 | 'price_field_value_id', |
| 1087 | 'description', |
| 1088 | ); |
| 1089 | while ($feeInfo->fetch()) { |
| 1090 | if ($hasLineItems) { |
| 1091 | foreach ($lineProperties as $property) { |
| 1092 | $feeDetails[$feeInfo->id][$feeInfo->lineId][$property] = $feeInfo->$property; |
| 1093 | } |
| 1094 | } |
| 1095 | else { |
| 1096 | foreach ($feeProperties as $property) { |
| 1097 | $feeDetails[$feeInfo->id][$property] = $feeInfo->$property; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | return $feeDetails; |
| 1103 | } |
| 1104 | |
| 1105 | /** |
| 1106 | * Retrieve additional participants display-names and URL to view their participant records. |
| 1107 | * (excludes cancelled participants automatically) |
| 1108 | * |
| 1109 | * @param int $primaryParticipantID |
| 1110 | * Id of primary participant record. |
| 1111 | * |
| 1112 | * @return array |
| 1113 | * $displayName => $viewUrl |
| 1114 | */ |
| 1115 | public static function getAdditionalParticipants($primaryParticipantID) { |
| 1116 | $additionalParticipantIDs = array(); |
| 1117 | $additionalParticipantIDs = self::getAdditionalParticipantIds($primaryParticipantID); |
| 1118 | if (!empty($additionalParticipantIDs)) { |
| 1119 | foreach ($additionalParticipantIDs as $additionalParticipantID) { |
| 1120 | $additionalContactID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', |
| 1121 | $additionalParticipantID, |
| 1122 | 'contact_id', 'id' |
| 1123 | ); |
| 1124 | $additionalContactName = CRM_Contact_BAO_Contact::displayName($additionalContactID); |
| 1125 | $pViewURL = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 1126 | "action=view&reset=1&id={$additionalParticipantID}&cid={$additionalContactID}" |
| 1127 | ); |
| 1128 | |
| 1129 | $additionalParticipants[$additionalContactName] = $pViewURL; |
| 1130 | } |
| 1131 | } |
| 1132 | return $additionalParticipants; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * Function for update primary and additional participant status. |
| 1137 | * |
| 1138 | * @param int $participantID |
| 1139 | * Primary participant's id. |
| 1140 | * @param int $oldStatusID |
| 1141 | * @param int $newStatusID |
| 1142 | * @param bool $updatePrimaryStatus |
| 1143 | * |
| 1144 | * @return bool|NULL |
| 1145 | */ |
| 1146 | public static function updateParticipantStatus($participantID, $oldStatusID, $newStatusID = NULL, $updatePrimaryStatus = FALSE) { |
| 1147 | if (!$participantID || !$oldStatusID) { |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | |
| 1151 | if (!$newStatusID) { |
| 1152 | $newStatusID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id'); |
| 1153 | } |
| 1154 | elseif ($updatePrimaryStatus) { |
| 1155 | CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id', $newStatusID); |
| 1156 | } |
| 1157 | |
| 1158 | $cascadeAdditionalIds = self::getValidAdditionalIds($participantID, $oldStatusID, $newStatusID); |
| 1159 | |
| 1160 | if (!empty($cascadeAdditionalIds)) { |
| 1161 | $cascadeAdditionalIds = implode(',', $cascadeAdditionalIds); |
| 1162 | $query = "UPDATE civicrm_participant cp SET cp.status_id = %1 WHERE cp.id IN ({$cascadeAdditionalIds})"; |
| 1163 | $params = array(1 => array($newStatusID, 'Integer')); |
| 1164 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1165 | return TRUE; |
| 1166 | } |
| 1167 | return FALSE; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * Function for update status for given participant ids. |
| 1172 | * |
| 1173 | * @param int $participantIds |
| 1174 | * Array of participant ids. |
| 1175 | * @param int $statusId |
| 1176 | * Status id for participant. |
| 1177 | * @param bool $updateRegisterDate |
| 1178 | */ |
| 1179 | public static function updateStatus($participantIds, $statusId, $updateRegisterDate = FALSE) { |
| 1180 | if (!is_array($participantIds) || empty($participantIds) || !$statusId) { |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | //lets update register date as we update status to keep track |
| 1185 | //when we did update status, useful for moving participant |
| 1186 | //from pending to expired. |
| 1187 | $setClause = "status_id = {$statusId}"; |
| 1188 | if ($updateRegisterDate) { |
| 1189 | $setClause .= ", register_date = NOW()"; |
| 1190 | } |
| 1191 | |
| 1192 | $participantIdClause = '( ' . implode(',', $participantIds) . ' )'; |
| 1193 | |
| 1194 | $query = " |
| 1195 | UPDATE civicrm_participant |
| 1196 | SET {$setClause} |
| 1197 | WHERE id IN {$participantIdClause}"; |
| 1198 | |
| 1199 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * Function takes participant ids and statuses |
| 1204 | * update status from $fromStatusId to $toStatusId |
| 1205 | * and send mail + create activities. |
| 1206 | * |
| 1207 | * @param array $participantIds |
| 1208 | * Participant ids. |
| 1209 | * @param int $toStatusId |
| 1210 | * Update status id. |
| 1211 | * @param int $fromStatusId |
| 1212 | * From status id. |
| 1213 | * @param bool $returnResult |
| 1214 | * @param bool $skipCascadeRule |
| 1215 | * |
| 1216 | * @return array|NULL |
| 1217 | */ |
| 1218 | public static function transitionParticipants( |
| 1219 | $participantIds, $toStatusId, |
| 1220 | $fromStatusId = NULL, $returnResult = FALSE, $skipCascadeRule = FALSE |
| 1221 | ) { |
| 1222 | if (!is_array($participantIds) || empty($participantIds) || !$toStatusId) { |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | |
| 1226 | //thumb rule is if we triggering primary participant need to triggered additional |
| 1227 | $allParticipantIds = $primaryANDAdditonalIds = array(); |
| 1228 | foreach ($participantIds as $id) { |
| 1229 | $allParticipantIds[] = $id; |
| 1230 | if (self::isPrimaryParticipant($id)) { |
| 1231 | //filter additional as per status transition rules, CRM-5403 |
| 1232 | if ($skipCascadeRule) { |
| 1233 | $additionalIds = self::getAdditionalParticipantIds($id); |
| 1234 | } |
| 1235 | else { |
| 1236 | $additionalIds = self::getValidAdditionalIds($id, $fromStatusId, $toStatusId); |
| 1237 | } |
| 1238 | if (!empty($additionalIds)) { |
| 1239 | $allParticipantIds = array_merge($allParticipantIds, $additionalIds); |
| 1240 | $primaryANDAdditonalIds[$id] = $additionalIds; |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | //get the unique participant ids, |
| 1246 | $allParticipantIds = array_unique($allParticipantIds); |
| 1247 | |
| 1248 | //pull required participants, contacts, events data, if not in hand |
| 1249 | static $eventDetails = array(); |
| 1250 | static $domainValues = array(); |
| 1251 | static $contactDetails = array(); |
| 1252 | |
| 1253 | $contactIds = $eventIds = $participantDetails = array(); |
| 1254 | |
| 1255 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(); |
| 1256 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); |
| 1257 | $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, |
| 1258 | "class = 'Pending'" |
| 1259 | ); |
| 1260 | |
| 1261 | //first thing is pull all necessory data from db. |
| 1262 | $participantIdClause = '(' . implode(',', $allParticipantIds) . ')'; |
| 1263 | |
| 1264 | //get all participants data. |
| 1265 | $query = "SELECT * FROM civicrm_participant WHERE id IN {$participantIdClause}"; |
| 1266 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1267 | while ($dao->fetch()) { |
| 1268 | $participantDetails[$dao->id] = array( |
| 1269 | 'id' => $dao->id, |
| 1270 | 'role' => $participantRoles[$dao->role_id], |
| 1271 | 'is_test' => $dao->is_test, |
| 1272 | 'event_id' => $dao->event_id, |
| 1273 | 'status_id' => $dao->status_id, |
| 1274 | 'fee_amount' => $dao->fee_amount, |
| 1275 | 'contact_id' => $dao->contact_id, |
| 1276 | 'register_date' => $dao->register_date, |
| 1277 | 'registered_by_id' => $dao->registered_by_id, |
| 1278 | ); |
| 1279 | if (!array_key_exists($dao->contact_id, $contactDetails)) { |
| 1280 | $contactIds[$dao->contact_id] = $dao->contact_id; |
| 1281 | } |
| 1282 | |
| 1283 | if (!array_key_exists($dao->event_id, $eventDetails)) { |
| 1284 | $eventIds[$dao->event_id] = $dao->event_id; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | //get the domain values. |
| 1289 | if (empty($domainValues)) { |
| 1290 | // making all tokens available to templates. |
| 1291 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 1292 | $tokens = array( |
| 1293 | 'domain' => array('name', 'phone', 'address', 'email'), |
| 1294 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
| 1295 | ); |
| 1296 | |
| 1297 | foreach ($tokens['domain'] as $token) { |
| 1298 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | //get all required contacts detail. |
| 1303 | if (!empty($contactIds)) { |
| 1304 | // get the contact details. |
| 1305 | list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, |
| 1306 | FALSE, FALSE, NULL, |
| 1307 | array(), |
| 1308 | 'CRM_Event_BAO_Participant' |
| 1309 | ); |
| 1310 | foreach ($currentContactDetails as $contactId => $contactValues) { |
| 1311 | $contactDetails[$contactId] = $contactValues; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | //get all required events detail. |
| 1316 | if (!empty($eventIds)) { |
| 1317 | foreach ($eventIds as $eventId) { |
| 1318 | //retrieve event information |
| 1319 | $eventParams = array('id' => $eventId); |
| 1320 | CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$eventId]); |
| 1321 | |
| 1322 | //get default participant role. |
| 1323 | $eventDetails[$eventId]['participant_role'] = CRM_Utils_Array::value($eventDetails[$eventId]['default_role_id'], $participantRoles); |
| 1324 | |
| 1325 | //get the location info |
| 1326 | $locParams = array('entity_id' => $eventId, 'entity_table' => 'civicrm_event'); |
| 1327 | $eventDetails[$eventId]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | //now we are ready w/ all required data. |
| 1332 | //take a decision as per statuses. |
| 1333 | |
| 1334 | $emailType = NULL; |
| 1335 | $toStatus = $statusTypes[$toStatusId]; |
| 1336 | $fromStatus = CRM_Utils_Array::value($fromStatusId, $statusTypes); |
| 1337 | |
| 1338 | switch ($toStatus) { |
| 1339 | case 'Pending from waitlist': |
| 1340 | case 'Pending from approval': |
| 1341 | switch ($fromStatus) { |
| 1342 | case 'On waitlist': |
| 1343 | case 'Awaiting approval': |
| 1344 | $emailType = 'Confirm'; |
| 1345 | break; |
| 1346 | } |
| 1347 | break; |
| 1348 | |
| 1349 | case 'Expired': |
| 1350 | //no matter from where u come send expired mail. |
| 1351 | $emailType = $toStatus; |
| 1352 | break; |
| 1353 | |
| 1354 | case 'Cancelled': |
| 1355 | //no matter from where u come send cancel mail. |
| 1356 | $emailType = $toStatus; |
| 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | //as we process additional w/ primary, there might be case if user |
| 1361 | //select primary as well as additionals, so avoid double processing. |
| 1362 | $processedParticipantIds = array(); |
| 1363 | $mailedParticipants = array(); |
| 1364 | |
| 1365 | //send mails and update status. |
| 1366 | foreach ($participantDetails as $participantId => $participantValues) { |
| 1367 | $updateParticipantIds = array(); |
| 1368 | if (in_array($participantId, $processedParticipantIds)) { |
| 1369 | continue; |
| 1370 | } |
| 1371 | |
| 1372 | //check is it primary and has additional. |
| 1373 | if (array_key_exists($participantId, $primaryANDAdditonalIds)) { |
| 1374 | foreach ($primaryANDAdditonalIds[$participantId] as $additonalId) { |
| 1375 | |
| 1376 | if ($emailType) { |
| 1377 | $mail = self::sendTransitionParticipantMail($additonalId, |
| 1378 | $participantDetails[$additonalId], |
| 1379 | $eventDetails[$participantDetails[$additonalId]['event_id']], |
| 1380 | $contactDetails[$participantDetails[$additonalId]['contact_id']], |
| 1381 | $domainValues, |
| 1382 | $emailType |
| 1383 | ); |
| 1384 | |
| 1385 | //get the mail participant ids |
| 1386 | if ($mail) { |
| 1387 | $mailedParticipants[$additonalId] = $contactDetails[$participantDetails[$additonalId]['contact_id']]['display_name']; |
| 1388 | } |
| 1389 | } |
| 1390 | $updateParticipantIds[] = $additonalId; |
| 1391 | $processedParticipantIds[] = $additonalId; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | //now send email appropriate mail to primary. |
| 1396 | if ($emailType) { |
| 1397 | $mail = self::sendTransitionParticipantMail($participantId, |
| 1398 | $participantValues, |
| 1399 | $eventDetails[$participantValues['event_id']], |
| 1400 | $contactDetails[$participantValues['contact_id']], |
| 1401 | $domainValues, |
| 1402 | $emailType |
| 1403 | ); |
| 1404 | |
| 1405 | //get the mail participant ids |
| 1406 | if ($mail) { |
| 1407 | $mailedParticipants[$participantId] = $contactDetails[$participantValues['contact_id']]['display_name']; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | //now update status of group/one at once. |
| 1412 | $updateParticipantIds[] = $participantId; |
| 1413 | |
| 1414 | //update the register date only when we, |
| 1415 | //move participant to pending class, CRM-6496 |
| 1416 | $updateRegisterDate = FALSE; |
| 1417 | if (array_key_exists($toStatusId, $pendingStatuses)) { |
| 1418 | $updateRegisterDate = TRUE; |
| 1419 | } |
| 1420 | self::updateStatus($updateParticipantIds, $toStatusId, $updateRegisterDate); |
| 1421 | $processedParticipantIds[] = $participantId; |
| 1422 | } |
| 1423 | |
| 1424 | //return result for cron. |
| 1425 | if ($returnResult) { |
| 1426 | $results = array( |
| 1427 | 'mailedParticipants' => $mailedParticipants, |
| 1428 | 'updatedParticipantIds' => $processedParticipantIds, |
| 1429 | ); |
| 1430 | |
| 1431 | return $results; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | * Send mail and create activity |
| 1437 | * when participant status changed. |
| 1438 | * |
| 1439 | * @param int $participantId |
| 1440 | * Participant id. |
| 1441 | * @param array $participantValues |
| 1442 | * Participant detail values. status id for participants. |
| 1443 | * @param array $eventDetails |
| 1444 | * Required event details. |
| 1445 | * @param array $contactDetails |
| 1446 | * Required contact details. |
| 1447 | * @param array $domainValues |
| 1448 | * Required domain values. |
| 1449 | * @param string $mailType |
| 1450 | * (eg 'approval', 'confirm', 'expired' ). |
| 1451 | * |
| 1452 | * @return bool |
| 1453 | */ |
| 1454 | public static function sendTransitionParticipantMail( |
| 1455 | $participantId, |
| 1456 | $participantValues, |
| 1457 | $eventDetails, |
| 1458 | $contactDetails, |
| 1459 | &$domainValues, |
| 1460 | $mailType |
| 1461 | ) { |
| 1462 | //send emails. |
| 1463 | $mailSent = FALSE; |
| 1464 | |
| 1465 | //don't send confirmation mail to additional |
| 1466 | //since only primary able to confirm registration. |
| 1467 | if (!empty($participantValues['registered_by_id']) && |
| 1468 | $mailType == 'Confirm' |
| 1469 | ) { |
| 1470 | return $mailSent; |
| 1471 | } |
| 1472 | $toEmail = CRM_Utils_Array::value('email', $contactDetails); |
| 1473 | if ($toEmail) { |
| 1474 | |
| 1475 | $contactId = $participantValues['contact_id']; |
| 1476 | $participantName = $contactDetails['display_name']; |
| 1477 | |
| 1478 | //calculate the checksum value. |
| 1479 | $checksumValue = NULL; |
| 1480 | if ($mailType == 'Confirm' && !$participantValues['registered_by_id']) { |
| 1481 | $checksumLife = 'inf'; |
| 1482 | $endDate = CRM_Utils_Array::value('end_date', $eventDetails); |
| 1483 | if ($endDate) { |
| 1484 | $checksumLife = (CRM_Utils_Date::unixTime($endDate) - time()) / (60 * 60); |
| 1485 | } |
| 1486 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId, NULL, $checksumLife); |
| 1487 | } |
| 1488 | |
| 1489 | //take a receipt from as event else domain. |
| 1490 | $receiptFrom = $domainValues['name'] . ' <' . $domainValues['email'] . '>'; |
| 1491 | if (!empty($eventDetails['confirm_from_name']) && !empty($eventDetails['confirm_from_email'])) { |
| 1492 | $receiptFrom = $eventDetails['confirm_from_name'] . ' <' . $eventDetails['confirm_from_email'] . '>'; |
| 1493 | } |
| 1494 | |
| 1495 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 1496 | array( |
| 1497 | 'groupName' => 'msg_tpl_workflow_event', |
| 1498 | 'valueName' => 'participant_' . strtolower($mailType), |
| 1499 | 'contactId' => $contactId, |
| 1500 | 'tplParams' => array( |
| 1501 | 'contact' => $contactDetails, |
| 1502 | 'domain' => $domainValues, |
| 1503 | 'participant' => $participantValues, |
| 1504 | 'event' => $eventDetails, |
| 1505 | 'paidEvent' => CRM_Utils_Array::value('is_monetary', $eventDetails), |
| 1506 | 'isShowLocation' => CRM_Utils_Array::value('is_show_location', $eventDetails), |
| 1507 | 'isAdditional' => $participantValues['registered_by_id'], |
| 1508 | 'isExpired' => $mailType == 'Expired', |
| 1509 | 'isConfirm' => $mailType == 'Confirm', |
| 1510 | 'checksumValue' => $checksumValue, |
| 1511 | ), |
| 1512 | 'from' => $receiptFrom, |
| 1513 | 'toName' => $participantName, |
| 1514 | 'toEmail' => $toEmail, |
| 1515 | 'cc' => CRM_Utils_Array::value('cc_confirm', $eventDetails), |
| 1516 | 'bcc' => CRM_Utils_Array::value('bcc_confirm', $eventDetails), |
| 1517 | ) |
| 1518 | ); |
| 1519 | |
| 1520 | // 3. create activity record. |
| 1521 | if ($mailSent) { |
| 1522 | $now = date('YmdHis'); |
| 1523 | $activityType = 'Event Registration'; |
| 1524 | $activityParams = array( |
| 1525 | 'subject' => $subject, |
| 1526 | 'source_contact_id' => $contactId, |
| 1527 | 'source_record_id' => $participantId, |
| 1528 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', |
| 1529 | $activityType, |
| 1530 | 'name' |
| 1531 | ), |
| 1532 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), |
| 1533 | 'due_date_time' => CRM_Utils_Date::isoToMysql($participantValues['register_date']), |
| 1534 | 'is_test' => $participantValues['is_test'], |
| 1535 | 'status_id' => 2, |
| 1536 | ); |
| 1537 | |
| 1538 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { |
| 1539 | CRM_Core_Error::fatal('Failed creating Activity for expiration mail'); |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | return $mailSent; |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * Get participant status change message. |
| 1549 | * |
| 1550 | * @param int $participantId |
| 1551 | * @param $statusChangeTo |
| 1552 | * @param int $fromStatusId |
| 1553 | * |
| 1554 | * @return string |
| 1555 | */ |
| 1556 | public function updateStatusMessage($participantId, $statusChangeTo, $fromStatusId) { |
| 1557 | $statusMsg = NULL; |
| 1558 | $results = self::transitionParticipants(array($participantId), |
| 1559 | $statusChangeTo, $fromStatusId, TRUE |
| 1560 | ); |
| 1561 | |
| 1562 | $allStatuses = CRM_Event_PseudoConstant::participantStatus(); |
| 1563 | //give user message only when mail has sent. |
| 1564 | if (is_array($results) && !empty($results)) { |
| 1565 | if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) { |
| 1566 | foreach ($results['updatedParticipantIds'] as $processedId) { |
| 1567 | if (is_array($results['mailedParticipants']) && |
| 1568 | array_key_exists($processedId, $results['mailedParticipants']) |
| 1569 | ) { |
| 1570 | $statusMsg .= '<br /> ' . ts("Participant status has been updated to '%1'. An email has been sent to %2.", |
| 1571 | array( |
| 1572 | 1 => $allStatuses[$statusChangeTo], |
| 1573 | 2 => $results['mailedParticipants'][$processedId], |
| 1574 | ) |
| 1575 | ); |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | return $statusMsg; |
| 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * Get event full and waiting list message. |
| 1586 | * |
| 1587 | * @param int $eventId |
| 1588 | * @param int $participantId |
| 1589 | * |
| 1590 | * @return string |
| 1591 | */ |
| 1592 | public static function eventFullMessage($eventId, $participantId = NULL) { |
| 1593 | $eventfullMsg = $dbStatusId = NULL; |
| 1594 | $checkEventFull = TRUE; |
| 1595 | if ($participantId) { |
| 1596 | $dbStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantId, 'status_id'); |
| 1597 | if (array_key_exists($dbStatusId, CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'))) { |
| 1598 | //participant already in counted status no need to check for event full messages. |
| 1599 | $checkEventFull = FALSE; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | //early return. |
| 1604 | if (!$eventId || !$checkEventFull) { |
| 1605 | return $eventfullMsg; |
| 1606 | } |
| 1607 | |
| 1608 | //event is truly full. |
| 1609 | $emptySeats = self::eventFull($eventId, FALSE, FALSE); |
| 1610 | if (is_string($emptySeats) && $emptySeats !== NULL) { |
| 1611 | $maxParticipants = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'max_participants'); |
| 1612 | $eventfullMsg = ts("This event currently has the maximum number of participants registered (%1). However, you can still override this limit and register additional participants using this form.", array( |
| 1613 | 1 => $maxParticipants, |
| 1614 | )) . '<br />'; |
| 1615 | } |
| 1616 | |
| 1617 | $hasWaiting = FALSE; |
| 1618 | $waitListedCount = self::eventFull($eventId, FALSE, TRUE, TRUE); |
| 1619 | if (is_numeric($waitListedCount)) { |
| 1620 | $hasWaiting = TRUE; |
| 1621 | //only current processing participant is on waitlist. |
| 1622 | if ($waitListedCount == 1 && CRM_Event_PseudoConstant::participantStatus($dbStatusId) == 'On waitlist') { |
| 1623 | $hasWaiting = FALSE; |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | if ($hasWaiting) { |
| 1628 | $waitingStatusId = array_search('On waitlist', |
| 1629 | CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'") |
| 1630 | ); |
| 1631 | $viewWaitListUrl = CRM_Utils_System::url('civicrm/event/search', |
| 1632 | "reset=1&force=1&event={$eventId}&status={$waitingStatusId}" |
| 1633 | ); |
| 1634 | |
| 1635 | $eventfullMsg .= ts("There are %2 people currently on the waiting list for this event. You can <a href='%1'>view waitlisted registrations here</a>, or you can continue and register additional participants using this form.", |
| 1636 | array( |
| 1637 | 1 => $viewWaitListUrl, |
| 1638 | 2 => $waitListedCount, |
| 1639 | ) |
| 1640 | ); |
| 1641 | } |
| 1642 | |
| 1643 | return $eventfullMsg; |
| 1644 | } |
| 1645 | |
| 1646 | /** |
| 1647 | * Check for whether participant is primary or not. |
| 1648 | * |
| 1649 | * @param int $participantId |
| 1650 | * |
| 1651 | * @return bool |
| 1652 | * true if participant is primary |
| 1653 | */ |
| 1654 | public static function isPrimaryParticipant($participantId) { |
| 1655 | |
| 1656 | $participant = new CRM_Event_DAO_Participant(); |
| 1657 | $participant->registered_by_id = $participantId; |
| 1658 | |
| 1659 | if ($participant->find(TRUE)) { |
| 1660 | return TRUE; |
| 1661 | } |
| 1662 | return FALSE; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * Get additional participant Ids for cascading with primary participant status. |
| 1667 | * |
| 1668 | * @param int $participantId |
| 1669 | * Participant id. |
| 1670 | * @param int $oldStatusId |
| 1671 | * Previous status. |
| 1672 | * @param int $newStatusId |
| 1673 | * New status. |
| 1674 | * |
| 1675 | * @return bool |
| 1676 | * true if allowed |
| 1677 | */ |
| 1678 | public static function getValidAdditionalIds($participantId, $oldStatusId, $newStatusId) { |
| 1679 | |
| 1680 | $additionalParticipantIds = array(); |
| 1681 | |
| 1682 | static $participantStatuses = array(); |
| 1683 | |
| 1684 | if (empty($participantStatuses)) { |
| 1685 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); |
| 1686 | } |
| 1687 | |
| 1688 | if (CRM_Utils_Array::value($participantStatuses[$oldStatusId], self::$_statusTransitionsRules) && |
| 1689 | in_array($participantStatuses[$newStatusId], self::$_statusTransitionsRules[$participantStatuses[$oldStatusId]]) |
| 1690 | ) { |
| 1691 | $additionalParticipantIds = self::getAdditionalParticipantIds($participantId, TRUE, $oldStatusId); |
| 1692 | } |
| 1693 | |
| 1694 | return $additionalParticipantIds; |
| 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * Get participant record count for a Contact. |
| 1699 | * |
| 1700 | * @param int $contactID |
| 1701 | * |
| 1702 | * @return int |
| 1703 | * count of participant records |
| 1704 | */ |
| 1705 | public static function getContactParticipantCount($contactID) { |
| 1706 | $query = "SELECT count(*) |
| 1707 | FROM civicrm_participant |
| 1708 | WHERE civicrm_participant.contact_id = {$contactID} AND |
| 1709 | civicrm_participant.is_test = 0"; |
| 1710 | return CRM_Core_DAO::singleValueQuery($query); |
| 1711 | } |
| 1712 | |
| 1713 | /** |
| 1714 | * Get participant ids by contribution id. |
| 1715 | * |
| 1716 | * @param int $contributionId |
| 1717 | * Contribution Id. |
| 1718 | * @param bool $excludeCancelled |
| 1719 | * Exclude cancelled additional participant. |
| 1720 | * |
| 1721 | * @return array |
| 1722 | */ |
| 1723 | public static function getParticipantIds($contributionId, $excludeCancelled = FALSE) { |
| 1724 | |
| 1725 | $ids = array(); |
| 1726 | if (!$contributionId) { |
| 1727 | return $ids; |
| 1728 | } |
| 1729 | |
| 1730 | // get primary participant id |
| 1731 | $query = "SELECT participant_id FROM civicrm_participant_payment WHERE contribution_id = {$contributionId}"; |
| 1732 | $participantId = CRM_Core_DAO::singleValueQuery($query); |
| 1733 | |
| 1734 | // get additional participant ids (including cancelled) |
| 1735 | if ($participantId) { |
| 1736 | $ids = array_merge(array( |
| 1737 | $participantId, |
| 1738 | ), self::getAdditionalParticipantIds($participantId, |
| 1739 | $excludeCancelled |
| 1740 | )); |
| 1741 | } |
| 1742 | |
| 1743 | return $ids; |
| 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * Get additional Participant edit & view url . |
| 1748 | * |
| 1749 | * @param array $participantIds |
| 1750 | * An array of additional participant ids. |
| 1751 | * |
| 1752 | * @return array |
| 1753 | * Array of Urls. |
| 1754 | */ |
| 1755 | public static function getAdditionalParticipantUrl($participantIds) { |
| 1756 | foreach ($participantIds as $value) { |
| 1757 | $links = array(); |
| 1758 | $details = self::participantDetails($value); |
| 1759 | $viewUrl = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 1760 | "action=view&reset=1&id={$value}&cid={$details['cid']}" |
| 1761 | ); |
| 1762 | $editUrl = CRM_Utils_System::url('civicrm/contact/view/participant', |
| 1763 | "action=update&reset=1&id={$value}&cid={$details['cid']}" |
| 1764 | ); |
| 1765 | $links[] = "<td><a href='{$viewUrl}'>" . $details['name'] . "</a></td><td></td><td><a href='{$editUrl}'>" . ts('Edit') . "</a></td>"; |
| 1766 | $links = "<table><tr>" . implode("</tr><tr>", $links) . "</tr></table>"; |
| 1767 | return $links; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * create trxn entry if an event has discount. |
| 1773 | * |
| 1774 | * @param int $eventID |
| 1775 | * Event id. |
| 1776 | * @param array $contributionParams |
| 1777 | * Contribution params. |
| 1778 | * |
| 1779 | * @param $feeLevel |
| 1780 | * |
| 1781 | */ |
| 1782 | public static function createDiscountTrxn($eventID, $contributionParams, $feeLevel) { |
| 1783 | // CRM-11124 |
| 1784 | $checkDiscount = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); |
| 1785 | if (!empty($checkDiscount)) { |
| 1786 | $feeLevel = current($feeLevel); |
| 1787 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID, NULL); |
| 1788 | $query = "SELECT cpfv.amount FROM `civicrm_price_field_value` cpfv |
| 1789 | LEFT JOIN civicrm_price_field cpf ON cpfv.price_field_id = cpf.id |
| 1790 | WHERE cpf.price_set_id = %1 AND cpfv.label LIKE %2"; |
| 1791 | $params = array( |
| 1792 | 1 => array($priceSetId, 'Integer'), |
| 1793 | 2 => array($feeLevel, 'String'), |
| 1794 | ); |
| 1795 | $mainAmount = CRM_Core_DAO::singleValueQuery($query, $params); |
| 1796 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Discounts Account is' ")); |
| 1797 | $contributionParams['trxnParams']['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( |
| 1798 | $contributionParams['contribution']->financial_type_id, $relationTypeId); |
| 1799 | if (!empty($contributionParams['trxnParams']['from_financial_account_id'])) { |
| 1800 | $contributionParams['trxnParams']['total_amount'] = $mainAmount - $contributionParams['total_amount']; |
| 1801 | $contributionParams['trxnParams']['payment_processor_id'] = NULL; |
| 1802 | $contributionParams['trxnParams']['payment_instrument_id'] = NULL; |
| 1803 | $contributionParams['trxnParams']['check_number'] = NULL; |
| 1804 | $contributionParams['trxnParams']['trxn_id'] = NULL; |
| 1805 | $contributionParams['trxnParams']['net_amount'] = NULL; |
| 1806 | $contributionParams['trxnParams']['fee_amount'] = NULL; |
| 1807 | |
| 1808 | CRM_Core_BAO_FinancialTrxn::create($contributionParams['trxnParams']); |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * Delete participants of contact. |
| 1815 | * |
| 1816 | * CRM-12155 |
| 1817 | * |
| 1818 | * @param int $contactId |
| 1819 | * Contact id. |
| 1820 | * |
| 1821 | */ |
| 1822 | public static function deleteContactParticipant($contactId) { |
| 1823 | $participant = new CRM_Event_DAO_Participant(); |
| 1824 | $participant->contact_id = $contactId; |
| 1825 | $participant->find(); |
| 1826 | while ($participant->fetch()) { |
| 1827 | self::deleteParticipant($participant->id); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * @param array $params |
| 1833 | * @param int $participantId |
| 1834 | * @param int $contributionId |
| 1835 | * @param $feeBlock |
| 1836 | * @param array $lineItems |
| 1837 | * @param $paidAmount |
| 1838 | * @param int $priceSetId |
| 1839 | */ |
| 1840 | public static function changeFeeSelections($params, $participantId, $contributionId, $feeBlock, $lineItems, $paidAmount, $priceSetId) { |
| 1841 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 1842 | $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); |
| 1843 | $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses); |
| 1844 | $previousLineItems = CRM_Price_BAO_LineItem::getLineItems($participantId, 'participant'); |
| 1845 | CRM_Price_BAO_PriceSet::processAmount($feeBlock, |
| 1846 | $params, $lineItems |
| 1847 | ); |
| 1848 | |
| 1849 | // get the submitted |
| 1850 | foreach ($feeBlock as $id => $values) { |
| 1851 | CRM_Price_BAO_LineItem::format($id, $params, $values, $submittedLineItems); |
| 1852 | $submittedFieldId[] = CRM_Utils_Array::retrieveValueRecursive($submittedLineItems, 'price_field_id'); |
| 1853 | } |
| 1854 | if (!empty($submittedLineItems)) { |
| 1855 | $insertLines = $submittedLineItems; |
| 1856 | |
| 1857 | $submittedFieldValueIds = array_keys($submittedLineItems); |
| 1858 | $updateLines = array(); |
| 1859 | foreach ($previousLineItems as $id => $previousLineItem) { |
| 1860 | // check through the submitted items if the previousItem exists, |
| 1861 | // if found in submitted items, do not use it for new item creations |
| 1862 | if (in_array($previousLineItem['price_field_value_id'], $submittedFieldValueIds)) { |
| 1863 | // if submitted line items are existing don't fire INSERT query |
| 1864 | unset($insertLines[$previousLineItem['price_field_value_id']]); |
| 1865 | // for updating the line items i.e. use-case - once deselect-option selecting again |
| 1866 | if (($previousLineItem['line_total'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['line_total']) || |
| 1867 | ($submittedLineItems[$previousLineItem['price_field_value_id']]['line_total'] == 0 && $submittedLineItems[$previousLineItem['price_field_value_id']]['qty'] == 1) |
| 1868 | ) { |
| 1869 | $updateLines[$previousLineItem['price_field_value_id']] = $submittedLineItems[$previousLineItem['price_field_value_id']]; |
| 1870 | $updateLines[$previousLineItem['price_field_value_id']]['id'] = $id; |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | $submittedFields = implode(', ', $submittedFieldId); |
| 1876 | $submittedFieldValues = implode(', ', $submittedFieldValueIds); |
| 1877 | } |
| 1878 | if (!empty($submittedFields) && !empty($submittedFieldValues)) { |
| 1879 | $updateLineItem = "UPDATE civicrm_line_item li |
| 1880 | INNER JOIN civicrm_financial_item fi |
| 1881 | ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') |
| 1882 | SET li.qty = 0, |
| 1883 | li.line_total = 0.00, |
| 1884 | li.tax_amount = NULL |
| 1885 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) AND |
| 1886 | (price_field_value_id NOT IN ({$submittedFieldValues})) |
| 1887 | "; |
| 1888 | CRM_Core_DAO::executeQuery($updateLineItem); |
| 1889 | |
| 1890 | // gathering necessary info to record negative (deselected) financial_item |
| 1891 | $updateFinancialItem = " |
| 1892 | SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount |
| 1893 | FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') |
| 1894 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) |
| 1895 | GROUP BY li.entity_table, li.entity_id, price_field_value_id |
| 1896 | "; |
| 1897 | $updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem); |
| 1898 | $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId, 'DESC', TRUE); |
| 1899 | $trxnId['id'] = $trxn['financialTrxnId']; |
| 1900 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
| 1901 | $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); |
| 1902 | $updateFinancialItemInfoValues = array(); |
| 1903 | $financialItemsArray = array(); |
| 1904 | while ($updateFinancialItemInfoDAO->fetch()) { |
| 1905 | $updateFinancialItemInfoValues = (array) $updateFinancialItemInfoDAO; |
| 1906 | $updateFinancialItemInfoValues['transaction_date'] = date('YmdHis'); |
| 1907 | // the below params are not needed |
| 1908 | unset($updateFinancialItemInfoValues['id']); |
| 1909 | unset($updateFinancialItemInfoValues['created_date']); |
| 1910 | // if not submitted and difference is not 0 make it negative |
| 1911 | if (!in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] != 0) { |
| 1912 | // INSERT negative financial_items |
| 1913 | $updateFinancialItemInfoValues['amount'] = -$updateFinancialItemInfoValues['amount']; |
| 1914 | if ($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']) { |
| 1915 | $updateFinancialItemInfoValues['tax']['amount'] = -($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']); |
| 1916 | $updateFinancialItemInfoValues['tax']['description'] = $taxTerm; |
| 1917 | if ($updateFinancialItemInfoValues['financial_type_id']) { |
| 1918 | $updateFinancialItemInfoValues['tax']['financial_account_id'] = CRM_Contribute_BAO_Contribution::getFinancialAccountId($updateFinancialItemInfoValues['financial_type_id']); |
| 1919 | } |
| 1920 | } |
| 1921 | // INSERT negative financial_items for tax amount |
| 1922 | $financialItemsArray[] = $updateFinancialItemInfoValues; |
| 1923 | } |
| 1924 | // if submitted and difference is 0 add a positive entry again |
| 1925 | elseif (in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] == 0) { |
| 1926 | $updateFinancialItemInfoValues['amount'] = $updateFinancialItemInfoValues['amount']; |
| 1927 | // INSERT financial_items for tax amount |
| 1928 | if ($updateFinancialItemInfoValues['entity_id'] == $updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['id'] && |
| 1929 | isset($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['tax_amount']) |
| 1930 | ) { |
| 1931 | $updateFinancialItemInfoValues['tax']['amount'] = $updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['tax_amount']; |
| 1932 | $updateFinancialItemInfoValues['tax']['description'] = $taxTerm; |
| 1933 | if ($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['financial_type_id']) { |
| 1934 | $updateFinancialItemInfoValues['tax']['financial_account_id'] = CRM_Contribute_BAO_Contribution::getFinancialAccountId($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['financial_type_id']); |
| 1935 | } |
| 1936 | } |
| 1937 | $financialItemsArray[] = $updateFinancialItemInfoValues; |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | elseif (empty($submittedFields) && empty($submittedFieldValues)) { |
| 1942 | $updateLineItem = "UPDATE civicrm_line_item li |
| 1943 | INNER JOIN civicrm_financial_item fi |
| 1944 | ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') |
| 1945 | SET li.qty = 0, |
| 1946 | li.line_total = 0.00, |
| 1947 | li.tax_amount = NULL |
| 1948 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})"; |
| 1949 | CRM_Core_DAO::executeQuery($updateLineItem); |
| 1950 | } |
| 1951 | $amountLevel = array(); |
| 1952 | $totalParticipant = $participantCount = 0; |
| 1953 | if (!empty($updateLines)) { |
| 1954 | foreach ($updateLines as $valueId => $vals) { |
| 1955 | $taxAmount = "NULL"; |
| 1956 | if (isset($vals['tax_amount'])) { |
| 1957 | $taxAmount = $vals['tax_amount']; |
| 1958 | } |
| 1959 | $amountLevel[] = $vals['label'] . ' - ' . (float) $vals['qty']; |
| 1960 | if (isset($vals['participant_count'])) { |
| 1961 | $participantCount = $vals['participant_count']; |
| 1962 | $totalParticipant += $vals['participant_count']; |
| 1963 | } |
| 1964 | $updateLineItem = " |
| 1965 | UPDATE civicrm_line_item li |
| 1966 | SET li.qty = {$vals['qty']}, |
| 1967 | li.line_total = {$vals['line_total']}, |
| 1968 | li.tax_amount = {$taxAmount}, |
| 1969 | li.unit_price = {$vals['unit_price']}, |
| 1970 | li.participant_count = {$participantCount}, |
| 1971 | li.label = %1 |
| 1972 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) AND |
| 1973 | (price_field_value_id = {$valueId}) |
| 1974 | "; |
| 1975 | CRM_Core_DAO::executeQuery($updateLineItem, array(1 => array($vals['label'], 'String'))); |
| 1976 | } |
| 1977 | } |
| 1978 | // insert new 'adjusted amount' transaction entry and update contribution entry. |
| 1979 | // ensure entity_financial_trxn table has a linking of it. |
| 1980 | // insert new line items |
| 1981 | if (!empty($insertLines)) { |
| 1982 | foreach ($insertLines as $valueId => $lineParams) { |
| 1983 | $lineParams['entity_table'] = 'civicrm_participant'; |
| 1984 | $lineParams['entity_id'] = $participantId; |
| 1985 | $lineParams['contribution_id'] = $contributionId; |
| 1986 | $lineObj = CRM_Price_BAO_LineItem::create($lineParams); |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | // the recordAdjustedAmt code would execute over here |
| 1991 | $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); |
| 1992 | if (count($ids) > 1) { |
| 1993 | $total = 0; |
| 1994 | foreach ($ids as $val) { |
| 1995 | $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant'); |
| 1996 | } |
| 1997 | $updatedAmount = $total; |
| 1998 | } |
| 1999 | else { |
| 2000 | $updatedAmount = $params['amount']; |
| 2001 | } |
| 2002 | if (strlen($params['tax_amount']) != 0) { |
| 2003 | $taxAmount = $params['tax_amount']; |
| 2004 | } |
| 2005 | else { |
| 2006 | $taxAmount = "NULL"; |
| 2007 | } |
| 2008 | $displayParticipantCount = ''; |
| 2009 | if ($totalParticipant > 0) { |
| 2010 | $displayParticipantCount = ' Participant Count -' . $totalParticipant; |
| 2011 | } |
| 2012 | $updateAmountLevel = NULL; |
| 2013 | if (!empty($amountLevel)) { |
| 2014 | $updateAmountLevel = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR; |
| 2015 | } |
| 2016 | $trxn = self::recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount, $updateAmountLevel); |
| 2017 | $trxnId = array(); |
| 2018 | if ($trxn) { |
| 2019 | $trxnId['id'] = $trxn->id; |
| 2020 | foreach ($financialItemsArray as $updateFinancialItemInfoValues) { |
| 2021 | CRM_Financial_BAO_FinancialItem::create($updateFinancialItemInfoValues, NULL, $trxnId); |
| 2022 | if (!empty($updateFinancialItemInfoValues['tax'])) { |
| 2023 | $updateFinancialItemInfoValues['tax']['amount'] = $updateFinancialItemInfoValues['amount']; |
| 2024 | $updateFinancialItemInfoValues['tax']['description'] = $updateFinancialItemInfoValues['description']; |
| 2025 | if (!empty($updateFinancialItemInfoValues['financial_account_id'])) { |
| 2026 | $updateFinancialItemInfoValues['financial_account_id'] = $updateFinancialItemInfoValues['tax']['financial_account_id']; |
| 2027 | } |
| 2028 | CRM_Financial_BAO_FinancialItem::create($updateFinancialItemInfoValues, NULL, $trxnId); |
| 2029 | } |
| 2030 | } |
| 2031 | } |
| 2032 | $fetchCon = array('id' => $contributionId); |
| 2033 | $updatedContribution = CRM_Contribute_BAO_Contribution::retrieve($fetchCon, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); |
| 2034 | // insert financial items |
| 2035 | if (!empty($insertLines)) { |
| 2036 | foreach ($insertLines as $valueId => $lineParams) { |
| 2037 | $lineParams['entity_table'] = 'civicrm_participant'; |
| 2038 | $lineParams['entity_id'] = $participantId; |
| 2039 | $lineObj = CRM_Price_BAO_LineItem::retrieve($lineParams, CRM_Core_DAO::$_nullArray); |
| 2040 | // insert financial items |
| 2041 | // ensure entity_financial_trxn table has a linking of it. |
| 2042 | $prevItem = CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, NULL, $trxnId); |
| 2043 | if (isset($lineObj->tax_amount)) { |
| 2044 | CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, TRUE, $trxnId); |
| 2045 | } |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | // update participant fee_amount column |
| 2050 | $partUpdateFeeAmt['id'] = $participantId; |
| 2051 | $getUpdatedLineItems = "SELECT * |
| 2052 | FROM civicrm_line_item |
| 2053 | WHERE (entity_table = 'civicrm_participant' AND entity_id = {$participantId} AND qty > 0)"; |
| 2054 | $getUpdatedLineItemsDAO = CRM_Core_DAO::executeQuery($getUpdatedLineItems); |
| 2055 | while ($getUpdatedLineItemsDAO->fetch()) { |
| 2056 | $line[$getUpdatedLineItemsDAO->price_field_value_id] = $getUpdatedLineItemsDAO->label . ' - ' . (float) $getUpdatedLineItemsDAO->qty; |
| 2057 | } |
| 2058 | |
| 2059 | $partUpdateFeeAmt['fee_level'] = implode(', ', $line); |
| 2060 | $partUpdateFeeAmt['fee_amount'] = $params['amount']; |
| 2061 | self::add($partUpdateFeeAmt); |
| 2062 | |
| 2063 | //activity creation |
| 2064 | self::addActivityForSelection($participantId, 'Change Registration'); |
| 2065 | } |
| 2066 | |
| 2067 | /** |
| 2068 | * @param $updatedAmount |
| 2069 | * @param $paidAmount |
| 2070 | * @param int $contributionId |
| 2071 | */ |
| 2072 | public static function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) { |
| 2073 | $pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); |
| 2074 | $pendingAmount = CRM_Utils_Array::value('total_amount', $pendingAmount, 0); |
| 2075 | $balanceAmt = $updatedAmount - $paidAmount; |
| 2076 | if ($paidAmount != $pendingAmount) { |
| 2077 | $balanceAmt -= $pendingAmount; |
| 2078 | } |
| 2079 | |
| 2080 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 2081 | $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); |
| 2082 | $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses); |
| 2083 | $completedStatusId = array_search('Completed', $contributionStatuses); |
| 2084 | |
| 2085 | $updatedContributionDAO = new CRM_Contribute_BAO_Contribution(); |
| 2086 | $adjustedTrxn = $skip = FALSE; |
| 2087 | if ($balanceAmt) { |
| 2088 | if ($balanceAmt > 0 && $paidAmount != 0) { |
| 2089 | $contributionStatusVal = $partiallyPaidStatusId; |
| 2090 | } |
| 2091 | elseif ($balanceAmt < 0 && $paidAmount != 0) { |
| 2092 | $contributionStatusVal = $pendingRefundStatusId; |
| 2093 | } |
| 2094 | elseif ($paidAmount == 0) { |
| 2095 | //skip updating the contribution status if no payment is made |
| 2096 | $skip = TRUE; |
| 2097 | $updatedContributionDAO->cancel_date = 'null'; |
| 2098 | $updatedContributionDAO->cancel_reason = NULL; |
| 2099 | } |
| 2100 | // update contribution status and total amount without trigger financial code |
| 2101 | // as this is handled in current BAO function used for change selection |
| 2102 | $updatedContributionDAO->id = $contributionId; |
| 2103 | if (!$skip) { |
| 2104 | $updatedContributionDAO->contribution_status_id = $contributionStatusVal; |
| 2105 | } |
| 2106 | $updatedContributionDAO->total_amount = $updatedContributionDAO->net_amount = $updatedAmount; |
| 2107 | $updatedContributionDAO->fee_amount = 0; |
| 2108 | $updatedContributionDAO->tax_amount = $taxAmount; |
| 2109 | if (!empty($updateAmountLevel)) { |
| 2110 | $updatedContributionDAO->amount_level = $updateAmountLevel; |
| 2111 | } |
| 2112 | $updatedContributionDAO->save(); |
| 2113 | // adjusted amount financial_trxn creation |
| 2114 | $updatedContribution = CRM_Contribute_BAO_Contribution::getValues( |
| 2115 | array('id' => $contributionId), |
| 2116 | CRM_Core_DAO::$_nullArray, |
| 2117 | CRM_Core_DAO::$_nullArray |
| 2118 | ); |
| 2119 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
| 2120 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId); |
| 2121 | $adjustedTrxnValues = array( |
| 2122 | 'from_financial_account_id' => NULL, |
| 2123 | 'to_financial_account_id' => $toFinancialAccount, |
| 2124 | 'total_amount' => $balanceAmt, |
| 2125 | 'status_id' => $completedStatusId, |
| 2126 | 'payment_instrument_id' => $updatedContribution->payment_instrument_id, |
| 2127 | 'contribution_id' => $updatedContribution->id, |
| 2128 | 'trxn_date' => date('YmdHis'), |
| 2129 | 'currency' => $updatedContribution->currency, |
| 2130 | ); |
| 2131 | $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues); |
| 2132 | } |
| 2133 | return $adjustedTrxn; |
| 2134 | } |
| 2135 | |
| 2136 | /** |
| 2137 | * @param int $participantId |
| 2138 | * @param $activityType |
| 2139 | * |
| 2140 | * @throws CRM_Core_Exception |
| 2141 | */ |
| 2142 | public static function addActivityForSelection($participantId, $activityType) { |
| 2143 | $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'event_id'); |
| 2144 | $contactId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'contact_id'); |
| 2145 | |
| 2146 | $date = CRM_Utils_Date::currentDBDate(); |
| 2147 | $event = CRM_Event_BAO_Event::getEvents(0, $eventId); |
| 2148 | $eventTitle = $event[$eventId]; |
| 2149 | $subject = "Registration selections changed for $eventTitle"; |
| 2150 | $targetCid = $contactId; |
| 2151 | $srcRecId = $participantId; |
| 2152 | |
| 2153 | // activity params |
| 2154 | $activityParams = array( |
| 2155 | 'source_contact_id' => $targetCid, |
| 2156 | 'source_record_id' => $srcRecId, |
| 2157 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', |
| 2158 | $activityType, |
| 2159 | 'name' |
| 2160 | ), |
| 2161 | 'subject' => $subject, |
| 2162 | 'activity_date_time' => $date, |
| 2163 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', |
| 2164 | 'Completed', |
| 2165 | 'name' |
| 2166 | ), |
| 2167 | 'skipRecentView' => TRUE, |
| 2168 | ); |
| 2169 | |
| 2170 | // create activity with target contacts |
| 2171 | $session = CRM_Core_Session::singleton(); |
| 2172 | $id = $session->get('userID'); |
| 2173 | if ($id) { |
| 2174 | $activityParams['source_contact_id'] = $id; |
| 2175 | $activityParams['target_contact_id'][] = $targetCid; |
| 2176 | } |
| 2177 | CRM_Activity_BAO_Activity::create($activityParams); |
| 2178 | } |
| 2179 | |
| 2180 | /** |
| 2181 | * Get options for a given field. |
| 2182 | * @see CRM_Core_DAO::buildOptions |
| 2183 | * |
| 2184 | * @param string $fieldName |
| 2185 | * @param string $context |
| 2186 | * @see CRM_Core_DAO::buildOptionsContext |
| 2187 | * @param array $props |
| 2188 | * whatever is known about this dao object. |
| 2189 | * |
| 2190 | * @return array|bool |
| 2191 | */ |
| 2192 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { |
| 2193 | $params = array('condition' => array()); |
| 2194 | |
| 2195 | if ($fieldName == 'status_id' && $context != 'validate') { |
| 2196 | // Get rid of cart-related option if disabled |
| 2197 | // FIXME: Why does this option even exist if cart is disabled? |
| 2198 | if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, 'enable_cart')) { |
| 2199 | $params['condition'][] = "name <> 'Pending in cart'"; |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); |
| 2204 | } |
| 2205 | |
| 2206 | } |