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