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