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 | |
ad37ac8e | 829 | * |
830 | * @param array $defaults | |
831 | * @param string $property | |
832 | * @param string $lookup | |
833 | * @param bool $reverse | |
834 | * | |
835 | * @return bool | |
6a488035 | 836 | */ |
00be9182 | 837 | public static function lookupValue(&$defaults, $property, $lookup, $reverse) { |
6a488035 TO |
838 | $id = $property . '_id'; |
839 | ||
840 | $src = $reverse ? $property : $id; | |
841 | $dst = $reverse ? $id : $property; | |
842 | ||
843 | if (!array_key_exists($src, $defaults)) { | |
844 | return FALSE; | |
845 | } | |
846 | ||
847 | $look = $reverse ? array_flip($lookup) : $lookup; | |
848 | ||
849 | if (is_array($look)) { | |
850 | if (!array_key_exists($defaults[$src], $look)) { | |
851 | return FALSE; | |
852 | } | |
853 | } | |
854 | $defaults[$dst] = $look[$defaults[$src]]; | |
855 | return TRUE; | |
856 | } | |
857 | ||
858 | /** | |
3bdf1f3a | 859 | * Delete the records that are associated with this participation. |
6a488035 | 860 | * |
d4dd1e85 TO |
861 | * @param int $id |
862 | * Id of the participation to delete. | |
6a488035 | 863 | * |
3bdf1f3a | 864 | * @return \CRM_Event_DAO_Participant |
6a488035 | 865 | */ |
00be9182 | 866 | public static function deleteParticipant($id) { |
6a488035 TO |
867 | CRM_Utils_Hook::pre('delete', 'Participant', $id, CRM_Core_DAO::$_nullArray); |
868 | ||
869 | $transaction = new CRM_Core_Transaction(); | |
870 | ||
871 | //delete activity record | |
872 | $params = array( | |
873 | 'source_record_id' => $id, | |
874 | // activity type id for event registration | |
875 | 'activity_type_id' => 5, | |
876 | ); | |
877 | ||
878 | CRM_Activity_BAO_Activity::deleteActivity($params); | |
879 | ||
880 | // delete the participant payment record | |
881 | // we need to do this since the cascaded constraints | |
882 | // dont work with join tables | |
883 | $p = array('participant_id' => $id); | |
884 | CRM_Event_BAO_ParticipantPayment::deleteParticipantPayment($p); | |
885 | ||
886 | // cleanup line items. | |
ddca8f33 TO |
887 | $participantsId = array(); |
888 | $participantsId = self::getAdditionalParticipantIds($id); | |
6a488035 TO |
889 | $participantsId[] = $id; |
890 | CRM_Price_BAO_LineItem::deleteLineItems($participantsId, 'civicrm_participant'); | |
891 | ||
892 | //delete note when participant deleted. | |
893 | $note = CRM_Core_BAO_Note::getNote($id, 'civicrm_participant'); | |
894 | $noteId = key($note); | |
895 | if ($noteId) { | |
896 | CRM_Core_BAO_Note::del($noteId, FALSE); | |
897 | } | |
898 | ||
899 | $participant = new CRM_Event_DAO_Participant(); | |
900 | $participant->id = $id; | |
901 | $participant->delete(); | |
902 | ||
903 | $transaction->commit(); | |
904 | ||
905 | CRM_Utils_Hook::post('delete', 'Participant', $participant->id, $participant); | |
906 | ||
907 | // delete the recently created Participant | |
908 | $participantRecent = array( | |
909 | 'id' => $id, | |
910 | 'type' => 'Participant', | |
911 | ); | |
912 | ||
913 | CRM_Utils_Recent::del($participantRecent); | |
914 | ||
915 | return $participant; | |
916 | } | |
917 | ||
918 | /** | |
66f9e52b | 919 | * Checks duplicate participants. |
6a488035 | 920 | * |
d4dd1e85 TO |
921 | * @param array $input |
922 | * An assosiative array of name /value pairs. | |
16b10e64 | 923 | * from other function |
a1a2a83d TO |
924 | * @param array $duplicates |
925 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 926 | * |
16b10e64 | 927 | * @return CRM_Contribute_BAO_Contribution |
6a488035 | 928 | */ |
00be9182 | 929 | public static function checkDuplicate($input, &$duplicates) { |
6a488035 TO |
930 | $eventId = CRM_Utils_Array::value('event_id', $input); |
931 | $contactId = CRM_Utils_Array::value('contact_id', $input); | |
932 | ||
933 | $clause = array(); | |
934 | $input = array(); | |
935 | ||
936 | if ($eventId) { | |
937 | $clause[] = "event_id = %1"; | |
938 | $input[1] = array($eventId, 'Integer'); | |
939 | } | |
940 | ||
941 | if ($contactId) { | |
942 | $clause[] = "contact_id = %2"; | |
943 | $input[2] = array($contactId, 'Integer'); | |
944 | } | |
945 | ||
946 | if (empty($clause)) { | |
947 | return FALSE; | |
948 | } | |
949 | ||
950 | $clause = implode(' AND ', $clause); | |
951 | ||
ddca8f33 TO |
952 | $query = "SELECT id FROM civicrm_participant WHERE $clause"; |
953 | $dao = CRM_Core_DAO::executeQuery($query, $input); | |
6a488035 TO |
954 | $result = FALSE; |
955 | while ($dao->fetch()) { | |
956 | $duplicates[] = $dao->id; | |
957 | $result = TRUE; | |
958 | } | |
959 | return $result; | |
960 | } | |
961 | ||
962 | /** | |
66f9e52b | 963 | * Fix the event level. |
6a488035 TO |
964 | * |
965 | * When price sets are used as event fee, fee_level is set as ^A | |
77035e4b | 966 | * separated string. We need to change that string to comma |
6a488035 TO |
967 | * separated string before using fee_level in view mode. |
968 | * | |
d4dd1e85 | 969 | * @param string $eventLevel |
3bdf1f3a | 970 | * Event_level string from db. |
6a488035 | 971 | */ |
00be9182 | 972 | public static function fixEventLevel(&$eventLevel) { |
6a488035 TO |
973 | if ((substr($eventLevel, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) && |
974 | (substr($eventLevel, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR) | |
975 | ) { | |
9747cc7e | 976 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($eventLevel, 1, -1))); |
0cca0bf4 J |
977 | $pos = strrpos($eventLevel, '(multiple participants)', 0); |
978 | if ($pos) { | |
6a488035 TO |
979 | $eventLevel = substr_replace($eventLevel, "", $pos - 3, 1); |
980 | } | |
981 | } | |
982 | elseif ((substr($eventLevel, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR)) { | |
983 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
ddca8f33 TO |
984 | substr($eventLevel, 0, 1) |
985 | )); | |
6a488035 TO |
986 | } |
987 | elseif ((substr($eventLevel, -1, 1) == CRM_Core_DAO::VALUE_SEPARATOR)) { | |
988 | $eventLevel = implode(', ', explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
ddca8f33 TO |
989 | substr($eventLevel, 0, -1) |
990 | )); | |
6a488035 TO |
991 | } |
992 | } | |
993 | ||
994 | /** | |
100fef9d | 995 | * Get the additional participant ids. |
6a488035 | 996 | * |
d4dd1e85 TO |
997 | * @param int $primaryParticipantId |
998 | * Primary partycipant Id. | |
999 | * @param bool $excludeCancel | |
1000 | * Do not include participant those are cancelled. | |
654ca41c | 1001 | * |
100fef9d | 1002 | * @param int $oldStatusId |
6a488035 | 1003 | * |
a6c01b45 | 1004 | * @return array |
6a488035 | 1005 | */ |
00be9182 | 1006 | public static function getAdditionalParticipantIds($primaryParticipantId, $excludeCancel = TRUE, $oldStatusId = NULL) { |
6a488035 TO |
1007 | $additionalParticipantIds = array(); |
1008 | if (!$primaryParticipantId) { | |
1009 | return $additionalParticipantIds; | |
1010 | } | |
1011 | ||
1012 | $where = "participant.registered_by_id={$primaryParticipantId}"; | |
1013 | if ($excludeCancel) { | |
ddca8f33 | 1014 | $cancelStatusId = 0; |
6a488035 | 1015 | $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"); |
ddca8f33 | 1016 | $cancelStatusId = array_search('Cancelled', $negativeStatuses); |
6a488035 TO |
1017 | $where .= " AND participant.status_id != {$cancelStatusId}"; |
1018 | } | |
1019 | ||
1020 | if ($oldStatusId) { | |
1021 | $where .= " AND participant.status_id = {$oldStatusId}"; | |
1022 | } | |
1023 | ||
1024 | $query = " | |
1025 | SELECT participant.id | |
1026 | FROM civicrm_participant participant | |
1027 | WHERE {$where}"; | |
1028 | ||
1029 | $dao = CRM_Core_DAO::executeQuery($query); | |
1030 | while ($dao->fetch()) { | |
1031 | $additionalParticipantIds[$dao->id] = $dao->id; | |
1032 | } | |
1033 | return $additionalParticipantIds; | |
1034 | } | |
1035 | ||
1036 | /** | |
1037 | * Get the event fee info for given participant ids | |
1038 | * either from line item table / participant table. | |
1039 | * | |
d4dd1e85 TO |
1040 | * @param array $participantIds |
1041 | * Participant ids. | |
1042 | * @param bool $hasLineItems | |
1043 | * Do fetch from line items. | |
6a488035 | 1044 | * |
a6c01b45 | 1045 | * @return array |
6a488035 | 1046 | */ |
00be9182 | 1047 | public function getFeeDetails($participantIds, $hasLineItems = FALSE) { |
6a488035 TO |
1048 | $feeDetails = array(); |
1049 | if (!is_array($participantIds) || empty($participantIds)) { | |
1050 | return $feeDetails; | |
1051 | } | |
1052 | ||
1053 | $select = ' | |
1054 | SELECT participant.id as id, | |
1055 | participant.fee_level as fee_level, | |
1056 | participant.fee_amount as fee_amount'; | |
1057 | $from = 'FROM civicrm_participant participant'; | |
1058 | if ($hasLineItems) { | |
1059 | $select .= ' , | |
1060 | lineItem.id as lineId, | |
1061 | lineItem.label as label, | |
1062 | lineItem.qty as qty, | |
1063 | lineItem.unit_price as unit_price, | |
1064 | lineItem.line_total as line_total, | |
1065 | field.label as field_title, | |
1066 | field.html_type as html_type, | |
1067 | field.id as price_field_id, | |
1068 | value.id as price_field_value_id, | |
1069 | value.description as description, | |
1070 | IF( value.count, value.count, 0 ) as participant_count'; | |
1071 | $from .= " | |
1072 | INNER JOIN civicrm_line_item lineItem ON ( lineItem.entity_table = 'civicrm_participant' | |
1073 | AND lineItem.entity_id = participant.id ) | |
1074 | INNER JOIN civicrm_price_field field ON ( field.id = lineItem.price_field_id ) | |
1075 | INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_value_id ) | |
1076 | "; | |
1077 | } | |
1078 | $where = 'WHERE participant.id IN ( ' . implode(', ', $participantIds) . ' )'; | |
1079 | $query = "$select $from $where"; | |
1080 | ||
ddca8f33 TO |
1081 | $feeInfo = CRM_Core_DAO::executeQuery($query); |
1082 | $feeProperties = array('fee_level', 'fee_amount'); | |
6a488035 | 1083 | $lineProperties = array( |
ddca8f33 TO |
1084 | 'lineId', |
1085 | 'label', | |
1086 | 'qty', | |
1087 | 'unit_price', | |
1088 | 'line_total', | |
1089 | 'field_title', | |
1090 | 'html_type', | |
1091 | 'price_field_id', | |
1092 | 'participant_count', | |
1093 | 'price_field_value_id', | |
1094 | 'description', | |
6a488035 TO |
1095 | ); |
1096 | while ($feeInfo->fetch()) { | |
1097 | if ($hasLineItems) { | |
1098 | foreach ($lineProperties as $property) { | |
1099 | $feeDetails[$feeInfo->id][$feeInfo->lineId][$property] = $feeInfo->$property; | |
1100 | } | |
1101 | } | |
1102 | else { | |
ddca8f33 TO |
1103 | foreach ($feeProperties as $property) { |
1104 | $feeDetails[$feeInfo->id][$property] = $feeInfo->$property; | |
0479b4c8 | 1105 | } |
6a488035 TO |
1106 | } |
1107 | } | |
1108 | ||
1109 | return $feeDetails; | |
1110 | } | |
1111 | ||
1112 | /** | |
1113 | * Retrieve additional participants display-names and URL to view their participant records. | |
1114 | * (excludes cancelled participants automatically) | |
1115 | * | |
d4dd1e85 TO |
1116 | * @param int $primaryParticipantID |
1117 | * Id of primary participant record. | |
6a488035 | 1118 | * |
a6c01b45 CW |
1119 | * @return array |
1120 | * $displayName => $viewUrl | |
6a488035 | 1121 | */ |
00be9182 | 1122 | public static function getAdditionalParticipants($primaryParticipantID) { |
6a488035 TO |
1123 | $additionalParticipantIDs = array(); |
1124 | $additionalParticipantIDs = self::getAdditionalParticipantIds($primaryParticipantID); | |
1125 | if (!empty($additionalParticipantIDs)) { | |
1126 | foreach ($additionalParticipantIDs as $additionalParticipantID) { | |
1127 | $additionalContactID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
1128 | $additionalParticipantID, | |
1129 | 'contact_id', 'id' | |
1130 | ); | |
1131 | $additionalContactName = CRM_Contact_BAO_Contact::displayName($additionalContactID); | |
1132 | $pViewURL = CRM_Utils_System::url('civicrm/contact/view/participant', | |
1133 | "action=view&reset=1&id={$additionalParticipantID}&cid={$additionalContactID}" | |
1134 | ); | |
1135 | ||
1136 | $additionalParticipants[$additionalContactName] = $pViewURL; | |
1137 | } | |
1138 | } | |
1139 | return $additionalParticipants; | |
1140 | } | |
1141 | ||
1142 | /** | |
66f9e52b | 1143 | * Function for update primary and additional participant status. |
6a488035 | 1144 | * |
d4dd1e85 TO |
1145 | * @param int $participantID |
1146 | * Primary participant's id. | |
100fef9d CW |
1147 | * @param int $oldStatusID |
1148 | * @param int $newStatusID | |
654ca41c EM |
1149 | * @param bool $updatePrimaryStatus |
1150 | * | |
a1a2a83d | 1151 | * @return bool|NULL |
6a488035 | 1152 | */ |
00be9182 | 1153 | public static function updateParticipantStatus($participantID, $oldStatusID, $newStatusID = NULL, $updatePrimaryStatus = FALSE) { |
6a488035 | 1154 | if (!$participantID || !$oldStatusID) { |
a1a2a83d | 1155 | return NULL; |
6a488035 TO |
1156 | } |
1157 | ||
1158 | if (!$newStatusID) { | |
1159 | $newStatusID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id'); | |
1160 | } | |
1161 | elseif ($updatePrimaryStatus) { | |
1162 | CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id', $newStatusID); | |
1163 | } | |
1164 | ||
1165 | $cascadeAdditionalIds = self::getValidAdditionalIds($participantID, $oldStatusID, $newStatusID); | |
1166 | ||
1167 | if (!empty($cascadeAdditionalIds)) { | |
1168 | $cascadeAdditionalIds = implode(',', $cascadeAdditionalIds); | |
1169 | $query = "UPDATE civicrm_participant cp SET cp.status_id = %1 WHERE cp.id IN ({$cascadeAdditionalIds})"; | |
1170 | $params = array(1 => array($newStatusID, 'Integer')); | |
1171 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1172 | return TRUE; | |
1173 | } | |
1174 | return FALSE; | |
1175 | } | |
1176 | ||
1177 | /** | |
66f9e52b | 1178 | * Function for update status for given participant ids. |
6a488035 | 1179 | * |
d4dd1e85 TO |
1180 | * @param int $participantIds |
1181 | * Array of participant ids. | |
1182 | * @param int $statusId | |
1183 | * Status id for participant. | |
654ca41c | 1184 | * @param bool $updateRegisterDate |
6a488035 | 1185 | */ |
00be9182 | 1186 | public static function updateStatus($participantIds, $statusId, $updateRegisterDate = FALSE) { |
6a488035 TO |
1187 | if (!is_array($participantIds) || empty($participantIds) || !$statusId) { |
1188 | return; | |
1189 | } | |
1190 | ||
1191 | //lets update register date as we update status to keep track | |
1192 | //when we did update status, useful for moving participant | |
1193 | //from pending to expired. | |
1194 | $setClause = "status_id = {$statusId}"; | |
1195 | if ($updateRegisterDate) { | |
1196 | $setClause .= ", register_date = NOW()"; | |
1197 | } | |
1198 | ||
1199 | $participantIdClause = '( ' . implode(',', $participantIds) . ' )'; | |
1200 | ||
1201 | $query = " | |
1202 | UPDATE civicrm_participant | |
1203 | SET {$setClause} | |
1204 | WHERE id IN {$participantIdClause}"; | |
1205 | ||
1206 | $dao = CRM_Core_DAO::executeQuery($query); | |
1207 | } | |
1208 | ||
654ca41c | 1209 | /** |
6a488035 TO |
1210 | * Function takes participant ids and statuses |
1211 | * update status from $fromStatusId to $toStatusId | |
1212 | * and send mail + create activities. | |
1213 | * | |
d4dd1e85 TO |
1214 | * @param array $participantIds |
1215 | * Participant ids. | |
1216 | * @param int $toStatusId | |
1217 | * Update status id. | |
1218 | * @param int $fromStatusId | |
1219 | * From status id. | |
654ca41c EM |
1220 | * @param bool $returnResult |
1221 | * @param bool $skipCascadeRule | |
1222 | * | |
a1a2a83d | 1223 | * @return array|NULL |
6a488035 | 1224 | */ |
2da40d21 | 1225 | public static function transitionParticipants( |
ddca8f33 | 1226 | $participantIds, $toStatusId, |
6a488035 TO |
1227 | $fromStatusId = NULL, $returnResult = FALSE, $skipCascadeRule = FALSE |
1228 | ) { | |
1229 | if (!is_array($participantIds) || empty($participantIds) || !$toStatusId) { | |
a1a2a83d | 1230 | return NULL; |
6a488035 TO |
1231 | } |
1232 | ||
1233 | //thumb rule is if we triggering primary participant need to triggered additional | |
1234 | $allParticipantIds = $primaryANDAdditonalIds = array(); | |
1235 | foreach ($participantIds as $id) { | |
1236 | $allParticipantIds[] = $id; | |
1237 | if (self::isPrimaryParticipant($id)) { | |
1238 | //filter additional as per status transition rules, CRM-5403 | |
1239 | if ($skipCascadeRule) { | |
1240 | $additionalIds = self::getAdditionalParticipantIds($id); | |
1241 | } | |
1242 | else { | |
1243 | $additionalIds = self::getValidAdditionalIds($id, $fromStatusId, $toStatusId); | |
1244 | } | |
1245 | if (!empty($additionalIds)) { | |
1246 | $allParticipantIds = array_merge($allParticipantIds, $additionalIds); | |
1247 | $primaryANDAdditonalIds[$id] = $additionalIds; | |
1248 | } | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | //get the unique participant ids, | |
1253 | $allParticipantIds = array_unique($allParticipantIds); | |
1254 | ||
1255 | //pull required participants, contacts, events data, if not in hand | |
1256 | static $eventDetails = array(); | |
1257 | static $domainValues = array(); | |
1258 | static $contactDetails = array(); | |
1259 | ||
1260 | $contactIds = $eventIds = $participantDetails = array(); | |
1261 | ||
ddca8f33 | 1262 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 1263 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); |
ddca8f33 | 1264 | $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, |
6a488035 TO |
1265 | "class = 'Pending'" |
1266 | ); | |
1267 | ||
1268 | //first thing is pull all necessory data from db. | |
1269 | $participantIdClause = '(' . implode(',', $allParticipantIds) . ')'; | |
1270 | ||
1271 | //get all participants data. | |
1272 | $query = "SELECT * FROM civicrm_participant WHERE id IN {$participantIdClause}"; | |
1273 | $dao = CRM_Core_DAO::executeQuery($query); | |
1274 | while ($dao->fetch()) { | |
1275 | $participantDetails[$dao->id] = array( | |
1276 | 'id' => $dao->id, | |
1277 | 'role' => $participantRoles[$dao->role_id], | |
1278 | 'is_test' => $dao->is_test, | |
1279 | 'event_id' => $dao->event_id, | |
1280 | 'status_id' => $dao->status_id, | |
1281 | 'fee_amount' => $dao->fee_amount, | |
1282 | 'contact_id' => $dao->contact_id, | |
1283 | 'register_date' => $dao->register_date, | |
1284 | 'registered_by_id' => $dao->registered_by_id, | |
1285 | ); | |
1286 | if (!array_key_exists($dao->contact_id, $contactDetails)) { | |
1287 | $contactIds[$dao->contact_id] = $dao->contact_id; | |
1288 | } | |
1289 | ||
1290 | if (!array_key_exists($dao->event_id, $eventDetails)) { | |
1291 | $eventIds[$dao->event_id] = $dao->event_id; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | //get the domain values. | |
1296 | if (empty($domainValues)) { | |
1297 | // making all tokens available to templates. | |
1298 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
0479b4c8 | 1299 | $tokens = array( |
ddca8f33 | 1300 | 'domain' => array('name', 'phone', 'address', 'email'), |
6a488035 TO |
1301 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
1302 | ); | |
1303 | ||
1304 | foreach ($tokens['domain'] as $token) { | |
1305 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | //get all required contacts detail. | |
1310 | if (!empty($contactIds)) { | |
1311 | // get the contact details. | |
1312 | list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, | |
1313 | FALSE, FALSE, NULL, | |
1314 | array(), | |
1315 | 'CRM_Event_BAO_Participant' | |
1316 | ); | |
1317 | foreach ($currentContactDetails as $contactId => $contactValues) { | |
1318 | $contactDetails[$contactId] = $contactValues; | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | //get all required events detail. | |
1323 | if (!empty($eventIds)) { | |
1324 | foreach ($eventIds as $eventId) { | |
1325 | //retrieve event information | |
1326 | $eventParams = array('id' => $eventId); | |
1327 | CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$eventId]); | |
1328 | ||
1329 | //get default participant role. | |
1330 | $eventDetails[$eventId]['participant_role'] = CRM_Utils_Array::value($eventDetails[$eventId]['default_role_id'], $participantRoles); | |
1331 | ||
1332 | //get the location info | |
1333 | $locParams = array('entity_id' => $eventId, 'entity_table' => 'civicrm_event'); | |
1334 | $eventDetails[$eventId]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE); | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | //now we are ready w/ all required data. | |
1339 | //take a decision as per statuses. | |
1340 | ||
ddca8f33 TO |
1341 | $emailType = NULL; |
1342 | $toStatus = $statusTypes[$toStatusId]; | |
6a488035 TO |
1343 | $fromStatus = CRM_Utils_Array::value($fromStatusId, $statusTypes); |
1344 | ||
1345 | switch ($toStatus) { | |
1346 | case 'Pending from waitlist': | |
1347 | case 'Pending from approval': | |
1348 | switch ($fromStatus) { | |
1349 | case 'On waitlist': | |
1350 | case 'Awaiting approval': | |
1351 | $emailType = 'Confirm'; | |
1352 | break; | |
1353 | } | |
1354 | break; | |
1355 | ||
1356 | case 'Expired': | |
1357 | //no matter from where u come send expired mail. | |
1358 | $emailType = $toStatus; | |
1359 | break; | |
1360 | ||
1361 | case 'Cancelled': | |
1362 | //no matter from where u come send cancel mail. | |
1363 | $emailType = $toStatus; | |
1364 | break; | |
1365 | } | |
1366 | ||
1367 | //as we process additional w/ primary, there might be case if user | |
1368 | //select primary as well as additionals, so avoid double processing. | |
1369 | $processedParticipantIds = array(); | |
1370 | $mailedParticipants = array(); | |
1371 | ||
1372 | //send mails and update status. | |
1373 | foreach ($participantDetails as $participantId => $participantValues) { | |
1374 | $updateParticipantIds = array(); | |
1375 | if (in_array($participantId, $processedParticipantIds)) { | |
1376 | continue; | |
1377 | } | |
1378 | ||
1379 | //check is it primary and has additional. | |
1380 | if (array_key_exists($participantId, $primaryANDAdditonalIds)) { | |
1381 | foreach ($primaryANDAdditonalIds[$participantId] as $additonalId) { | |
1382 | ||
1383 | if ($emailType) { | |
1384 | $mail = self::sendTransitionParticipantMail($additonalId, | |
1385 | $participantDetails[$additonalId], | |
1386 | $eventDetails[$participantDetails[$additonalId]['event_id']], | |
1387 | $contactDetails[$participantDetails[$additonalId]['contact_id']], | |
1388 | $domainValues, | |
1389 | $emailType | |
1390 | ); | |
1391 | ||
1392 | //get the mail participant ids | |
1393 | if ($mail) { | |
1394 | $mailedParticipants[$additonalId] = $contactDetails[$participantDetails[$additonalId]['contact_id']]['display_name']; | |
1395 | } | |
1396 | } | |
1397 | $updateParticipantIds[] = $additonalId; | |
1398 | $processedParticipantIds[] = $additonalId; | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | //now send email appropriate mail to primary. | |
1403 | if ($emailType) { | |
1404 | $mail = self::sendTransitionParticipantMail($participantId, | |
1405 | $participantValues, | |
1406 | $eventDetails[$participantValues['event_id']], | |
1407 | $contactDetails[$participantValues['contact_id']], | |
1408 | $domainValues, | |
1409 | $emailType | |
1410 | ); | |
1411 | ||
1412 | //get the mail participant ids | |
1413 | if ($mail) { | |
1414 | $mailedParticipants[$participantId] = $contactDetails[$participantValues['contact_id']]['display_name']; | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | //now update status of group/one at once. | |
1419 | $updateParticipantIds[] = $participantId; | |
1420 | ||
1421 | //update the register date only when we, | |
1422 | //move participant to pending class, CRM-6496 | |
1423 | $updateRegisterDate = FALSE; | |
1424 | if (array_key_exists($toStatusId, $pendingStatuses)) { | |
1425 | $updateRegisterDate = TRUE; | |
1426 | } | |
1427 | self::updateStatus($updateParticipantIds, $toStatusId, $updateRegisterDate); | |
1428 | $processedParticipantIds[] = $participantId; | |
1429 | } | |
1430 | ||
1431 | //return result for cron. | |
1432 | if ($returnResult) { | |
1433 | $results = array( | |
1434 | 'mailedParticipants' => $mailedParticipants, | |
1435 | 'updatedParticipantIds' => $processedParticipantIds, | |
1436 | ); | |
1437 | ||
1438 | return $results; | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | /** | |
100fef9d | 1443 | * Send mail and create activity |
6a488035 TO |
1444 | * when participant status changed. |
1445 | * | |
d4dd1e85 TO |
1446 | * @param int $participantId |
1447 | * Participant id. | |
1448 | * @param array $participantValues | |
1449 | * Participant detail values. status id for participants. | |
1450 | * @param array $eventDetails | |
1451 | * Required event details. | |
1452 | * @param array $contactDetails | |
1453 | * Required contact details. | |
1454 | * @param array $domainValues | |
1455 | * Required domain values. | |
1456 | * @param string $mailType | |
1457 | * (eg 'approval', 'confirm', 'expired' ). | |
6a488035 | 1458 | * |
654ca41c | 1459 | * @return bool |
6a488035 | 1460 | */ |
2da40d21 | 1461 | public static function sendTransitionParticipantMail( |
6a488035 TO |
1462 | $participantId, |
1463 | $participantValues, | |
1464 | $eventDetails, | |
1465 | $contactDetails, | |
1466 | &$domainValues, | |
1467 | $mailType | |
1468 | ) { | |
1469 | //send emails. | |
1470 | $mailSent = FALSE; | |
1471 | ||
1472 | //don't send confirmation mail to additional | |
1473 | //since only primary able to confirm registration. | |
a7488080 | 1474 | if (!empty($participantValues['registered_by_id']) && |
6a488035 TO |
1475 | $mailType == 'Confirm' |
1476 | ) { | |
1477 | return $mailSent; | |
1478 | } | |
0cca0bf4 J |
1479 | $toEmail = CRM_Utils_Array::value('email', $contactDetails); |
1480 | if ($toEmail) { | |
6a488035 TO |
1481 | |
1482 | $contactId = $participantValues['contact_id']; | |
1483 | $participantName = $contactDetails['display_name']; | |
1484 | ||
1485 | //calculate the checksum value. | |
1486 | $checksumValue = NULL; | |
1487 | if ($mailType == 'Confirm' && !$participantValues['registered_by_id']) { | |
1488 | $checksumLife = 'inf'; | |
0cca0bf4 J |
1489 | $endDate = CRM_Utils_Array::value('end_date', $eventDetails); |
1490 | if ($endDate) { | |
6a488035 TO |
1491 | $checksumLife = (CRM_Utils_Date::unixTime($endDate) - time()) / (60 * 60); |
1492 | } | |
1493 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId, NULL, $checksumLife); | |
1494 | } | |
1495 | ||
1496 | //take a receipt from as event else domain. | |
1497 | $receiptFrom = $domainValues['name'] . ' <' . $domainValues['email'] . '>'; | |
8cc574cf | 1498 | if (!empty($eventDetails['confirm_from_name']) && !empty($eventDetails['confirm_from_email'])) { |
6a488035 TO |
1499 | $receiptFrom = $eventDetails['confirm_from_name'] . ' <' . $eventDetails['confirm_from_email'] . '>'; |
1500 | } | |
1501 | ||
c6327d7d | 1502 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
1503 | array( |
1504 | 'groupName' => 'msg_tpl_workflow_event', | |
1505 | 'valueName' => 'participant_' . strtolower($mailType), | |
1506 | 'contactId' => $contactId, | |
1507 | 'tplParams' => array( | |
1508 | 'contact' => $contactDetails, | |
1509 | 'domain' => $domainValues, | |
1510 | 'participant' => $participantValues, | |
1511 | 'event' => $eventDetails, | |
1512 | 'paidEvent' => CRM_Utils_Array::value('is_monetary', $eventDetails), | |
1513 | 'isShowLocation' => CRM_Utils_Array::value('is_show_location', $eventDetails), | |
1514 | 'isAdditional' => $participantValues['registered_by_id'], | |
1515 | 'isExpired' => $mailType == 'Expired', | |
1516 | 'isConfirm' => $mailType == 'Confirm', | |
1517 | 'checksumValue' => $checksumValue, | |
1518 | ), | |
1519 | 'from' => $receiptFrom, | |
1520 | 'toName' => $participantName, | |
1521 | 'toEmail' => $toEmail, | |
1522 | 'cc' => CRM_Utils_Array::value('cc_confirm', $eventDetails), | |
1523 | 'bcc' => CRM_Utils_Array::value('bcc_confirm', $eventDetails), | |
1524 | ) | |
1525 | ); | |
1526 | ||
1527 | // 3. create activity record. | |
1528 | if ($mailSent) { | |
ddca8f33 TO |
1529 | $now = date('YmdHis'); |
1530 | $activityType = 'Event Registration'; | |
6a488035 TO |
1531 | $activityParams = array( |
1532 | 'subject' => $subject, | |
1533 | 'source_contact_id' => $contactId, | |
1534 | 'source_record_id' => $participantId, | |
1535 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
1536 | $activityType, | |
1537 | 'name' | |
1538 | ), | |
1539 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), | |
1540 | 'due_date_time' => CRM_Utils_Date::isoToMysql($participantValues['register_date']), | |
1541 | 'is_test' => $participantValues['is_test'], | |
1542 | 'status_id' => 2, | |
1543 | ); | |
1544 | ||
1545 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { | |
1546 | CRM_Core_Error::fatal('Failed creating Activity for expiration mail'); | |
1547 | } | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | return $mailSent; | |
1552 | } | |
1553 | ||
1554 | /** | |
100fef9d | 1555 | * Get participant status change message. |
6a488035 | 1556 | * |
100fef9d | 1557 | * @param int $participantId |
654ca41c | 1558 | * @param $statusChangeTo |
100fef9d | 1559 | * @param int $fromStatusId |
654ca41c | 1560 | * |
6a488035 | 1561 | * @return string |
6a488035 | 1562 | */ |
00be9182 | 1563 | public function updateStatusMessage($participantId, $statusChangeTo, $fromStatusId) { |
6a488035 TO |
1564 | $statusMsg = NULL; |
1565 | $results = self::transitionParticipants(array($participantId), | |
1566 | $statusChangeTo, $fromStatusId, TRUE | |
1567 | ); | |
1568 | ||
1569 | $allStatuses = CRM_Event_PseudoConstant::participantStatus(); | |
1570 | //give user message only when mail has sent. | |
1571 | if (is_array($results) && !empty($results)) { | |
1572 | if (is_array($results['updatedParticipantIds']) && !empty($results['updatedParticipantIds'])) { | |
1573 | foreach ($results['updatedParticipantIds'] as $processedId) { | |
1574 | if (is_array($results['mailedParticipants']) && | |
1575 | array_key_exists($processedId, $results['mailedParticipants']) | |
1576 | ) { | |
1577 | $statusMsg .= '<br /> ' . ts("Participant status has been updated to '%1'. An email has been sent to %2.", | |
ddca8f33 TO |
1578 | array( |
1579 | 1 => $allStatuses[$statusChangeTo], | |
1580 | 2 => $results['mailedParticipants'][$processedId], | |
1581 | ) | |
1582 | ); | |
6a488035 TO |
1583 | } |
1584 | } | |
1585 | } | |
1586 | } | |
1587 | ||
1588 | return $statusMsg; | |
1589 | } | |
1590 | ||
1591 | /** | |
100fef9d | 1592 | * Get event full and waiting list message. |
6a488035 | 1593 | * |
100fef9d CW |
1594 | * @param int $eventId |
1595 | * @param int $participantId | |
654ca41c | 1596 | * |
6a488035 | 1597 | * @return string |
6a488035 | 1598 | */ |
00be9182 | 1599 | public static function eventFullMessage($eventId, $participantId = NULL) { |
6a488035 TO |
1600 | $eventfullMsg = $dbStatusId = NULL; |
1601 | $checkEventFull = TRUE; | |
1602 | if ($participantId) { | |
1603 | $dbStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantId, 'status_id'); | |
1604 | if (array_key_exists($dbStatusId, CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'))) { | |
1605 | //participant already in counted status no need to check for event full messages. | |
1606 | $checkEventFull = FALSE; | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | //early return. | |
1611 | if (!$eventId || !$checkEventFull) { | |
1612 | return $eventfullMsg; | |
1613 | } | |
1614 | ||
1615 | //event is truly full. | |
1616 | $emptySeats = self::eventFull($eventId, FALSE, FALSE); | |
1617 | if (is_string($emptySeats) && $emptySeats !== NULL) { | |
1618 | $maxParticipants = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'max_participants'); | |
1619 | $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 | 1620 | 1 => $maxParticipants, |
ddca8f33 | 1621 | )) . '<br />'; |
6a488035 TO |
1622 | } |
1623 | ||
1624 | $hasWaiting = FALSE; | |
1625 | $waitListedCount = self::eventFull($eventId, FALSE, TRUE, TRUE); | |
1626 | if (is_numeric($waitListedCount)) { | |
1627 | $hasWaiting = TRUE; | |
1628 | //only current processing participant is on waitlist. | |
1629 | if ($waitListedCount == 1 && CRM_Event_PseudoConstant::participantStatus($dbStatusId) == 'On waitlist') { | |
1630 | $hasWaiting = FALSE; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | if ($hasWaiting) { | |
1635 | $waitingStatusId = array_search('On waitlist', | |
1636 | CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'") | |
1637 | ); | |
1638 | $viewWaitListUrl = CRM_Utils_System::url('civicrm/event/search', | |
1639 | "reset=1&force=1&event={$eventId}&status={$waitingStatusId}" | |
1640 | ); | |
1641 | ||
1642 | $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.", | |
1643 | array( | |
1644 | 1 => $viewWaitListUrl, | |
1645 | 2 => $waitListedCount, | |
1646 | ) | |
1647 | ); | |
1648 | } | |
1649 | ||
1650 | return $eventfullMsg; | |
1651 | } | |
1652 | ||
1653 | /** | |
66f9e52b | 1654 | * Check for whether participant is primary or not. |
6a488035 | 1655 | * |
100fef9d | 1656 | * @param int $participantId |
6a488035 | 1657 | * |
72b3a70c CW |
1658 | * @return bool |
1659 | * true if participant is primary | |
6a488035 | 1660 | */ |
00be9182 | 1661 | public static function isPrimaryParticipant($participantId) { |
6a488035 TO |
1662 | |
1663 | $participant = new CRM_Event_DAO_Participant(); | |
1664 | $participant->registered_by_id = $participantId; | |
1665 | ||
1666 | if ($participant->find(TRUE)) { | |
1667 | return TRUE; | |
1668 | } | |
1669 | return FALSE; | |
1670 | } | |
1671 | ||
1672 | /** | |
66f9e52b | 1673 | * Get additional participant Ids for cascading with primary participant status. |
6a488035 | 1674 | * |
d4dd1e85 TO |
1675 | * @param int $participantId |
1676 | * Participant id. | |
1677 | * @param int $oldStatusId | |
1678 | * Previous status. | |
1679 | * @param int $newStatusId | |
1680 | * New status. | |
6a488035 | 1681 | * |
72b3a70c CW |
1682 | * @return bool |
1683 | * true if allowed | |
6a488035 | 1684 | */ |
00be9182 | 1685 | public static function getValidAdditionalIds($participantId, $oldStatusId, $newStatusId) { |
6a488035 TO |
1686 | |
1687 | $additionalParticipantIds = array(); | |
1688 | ||
1689 | static $participantStatuses = array(); | |
1690 | ||
1691 | if (empty($participantStatuses)) { | |
1692 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); | |
1693 | } | |
1694 | ||
1695 | if (CRM_Utils_Array::value($participantStatuses[$oldStatusId], self::$_statusTransitionsRules) && | |
1696 | in_array($participantStatuses[$newStatusId], self::$_statusTransitionsRules[$participantStatuses[$oldStatusId]]) | |
1697 | ) { | |
1698 | $additionalParticipantIds = self::getAdditionalParticipantIds($participantId, TRUE, $oldStatusId); | |
1699 | } | |
1700 | ||
1701 | return $additionalParticipantIds; | |
1702 | } | |
1703 | ||
1704 | /** | |
66f9e52b | 1705 | * Get participant record count for a Contact. |
6a488035 | 1706 | * |
c490a46a | 1707 | * @param int $contactID |
6a488035 | 1708 | * |
a6c01b45 CW |
1709 | * @return int |
1710 | * count of participant records | |
6a488035 | 1711 | */ |
00be9182 | 1712 | public static function getContactParticipantCount($contactID) { |
6a488035 TO |
1713 | $query = "SELECT count(*) |
1714 | FROM civicrm_participant | |
1715 | WHERE civicrm_participant.contact_id = {$contactID} AND | |
1716 | civicrm_participant.is_test = 0"; | |
1717 | return CRM_Core_DAO::singleValueQuery($query); | |
1718 | } | |
1719 | ||
1720 | /** | |
66f9e52b | 1721 | * Get participant ids by contribution id. |
6a488035 | 1722 | * |
d4dd1e85 TO |
1723 | * @param int $contributionId |
1724 | * Contribution Id. | |
1725 | * @param bool $excludeCancelled | |
1726 | * Exclude cancelled additional participant. | |
6a488035 | 1727 | * |
a6c01b45 | 1728 | * @return array |
6a488035 | 1729 | */ |
00be9182 | 1730 | public static function getParticipantIds($contributionId, $excludeCancelled = FALSE) { |
6a488035 TO |
1731 | |
1732 | $ids = array(); | |
1733 | if (!$contributionId) { | |
1734 | return $ids; | |
1735 | } | |
1736 | ||
1737 | // get primary participant id | |
1738 | $query = "SELECT participant_id FROM civicrm_participant_payment WHERE contribution_id = {$contributionId}"; | |
1739 | $participantId = CRM_Core_DAO::singleValueQuery($query); | |
1740 | ||
1741 | // get additional participant ids (including cancelled) | |
1742 | if ($participantId) { | |
1743 | $ids = array_merge(array( | |
21dfd5f5 | 1744 | $participantId, |
ddca8f33 TO |
1745 | ), self::getAdditionalParticipantIds($participantId, |
1746 | $excludeCancelled | |
1747 | )); | |
6a488035 TO |
1748 | } |
1749 | ||
1750 | return $ids; | |
1751 | } | |
1752 | ||
1753 | /** | |
100fef9d | 1754 | * Get additional Participant edit & view url . |
6a488035 | 1755 | * |
d4dd1e85 TO |
1756 | * @param array $participantIds |
1757 | * An array of additional participant ids. | |
6a488035 | 1758 | * |
a6c01b45 | 1759 | * @return array |
16b10e64 | 1760 | * Array of Urls. |
6a488035 | 1761 | */ |
00be9182 | 1762 | public static function getAdditionalParticipantUrl($participantIds) { |
6a488035 | 1763 | foreach ($participantIds as $value) { |
ddca8f33 | 1764 | $links = array(); |
6a488035 TO |
1765 | $details = self::participantDetails($value); |
1766 | $viewUrl = CRM_Utils_System::url('civicrm/contact/view/participant', | |
1767 | "action=view&reset=1&id={$value}&cid={$details['cid']}" | |
1768 | ); | |
1769 | $editUrl = CRM_Utils_System::url('civicrm/contact/view/participant', | |
1770 | "action=update&reset=1&id={$value}&cid={$details['cid']}" | |
1771 | ); | |
1772 | $links[] = "<td><a href='{$viewUrl}'>" . $details['name'] . "</a></td><td></td><td><a href='{$editUrl}'>" . ts('Edit') . "</a></td>"; | |
1773 | $links = "<table><tr>" . implode("</tr><tr>", $links) . "</tr></table>"; | |
1774 | return $links; | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | /** | |
dc195289 | 1779 | * create trxn entry if an event has discount. |
6a488035 | 1780 | * |
d4dd1e85 TO |
1781 | * @param int $eventID |
1782 | * Event id. | |
1783 | * @param array $contributionParams | |
1784 | * Contribution params. | |
654ca41c EM |
1785 | * |
1786 | * @param $feeLevel | |
6a488035 | 1787 | * |
6a488035 | 1788 | */ |
00be9182 | 1789 | public static function createDiscountTrxn($eventID, $contributionParams, $feeLevel) { |
6a488035 | 1790 | // CRM-11124 |
0479b4c8 | 1791 | $checkDiscount = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); |
6a488035 TO |
1792 | if (!empty($checkDiscount)) { |
1793 | $feeLevel = current($feeLevel); | |
9da8dc8c | 1794 | $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID, NULL); |
6a488035 TO |
1795 | $query = "SELECT cpfv.amount FROM `civicrm_price_field_value` cpfv |
1796 | LEFT JOIN civicrm_price_field cpf ON cpfv.price_field_id = cpf.id | |
1797 | WHERE cpf.price_set_id = %1 AND cpfv.label LIKE %2"; | |
0479b4c8 | 1798 | $params = array( |
ddca8f33 | 1799 | 1 => array($priceSetId, 'Integer'), |
21dfd5f5 | 1800 | 2 => array($feeLevel, 'String'), |
ddca8f33 | 1801 | ); |
6a488035 | 1802 | $mainAmount = CRM_Core_DAO::singleValueQuery($query, $params); |
f743a6eb | 1803 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Discounts Account is' ")); |
6a488035 TO |
1804 | $contributionParams['trxnParams']['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( |
1805 | $contributionParams['contribution']->financial_type_id, $relationTypeId); | |
a7488080 | 1806 | if (!empty($contributionParams['trxnParams']['from_financial_account_id'])) { |
6a488035 | 1807 | $contributionParams['trxnParams']['total_amount'] = $mainAmount - $contributionParams['total_amount']; |
6c552737 TO |
1808 | $contributionParams['trxnParams']['payment_processor_id'] = NULL; |
1809 | $contributionParams['trxnParams']['payment_instrument_id'] = NULL; | |
1810 | $contributionParams['trxnParams']['check_number'] = NULL; | |
1811 | $contributionParams['trxnParams']['trxn_id'] = NULL; | |
1812 | $contributionParams['trxnParams']['net_amount'] = NULL; | |
1813 | $contributionParams['trxnParams']['fee_amount'] = NULL; | |
6a488035 TO |
1814 | |
1815 | CRM_Core_BAO_FinancialTrxn::create($contributionParams['trxnParams']); | |
1816 | } | |
1817 | } | |
6a488035 | 1818 | } |
c3d24ba7 PN |
1819 | |
1820 | /** | |
66f9e52b | 1821 | * Delete participants of contact. |
c3d24ba7 PN |
1822 | * |
1823 | * CRM-12155 | |
1824 | * | |
d4dd1e85 TO |
1825 | * @param int $contactId |
1826 | * Contact id. | |
c3d24ba7 | 1827 | * |
c3d24ba7 | 1828 | */ |
00be9182 | 1829 | public static function deleteContactParticipant($contactId) { |
c3d24ba7 PN |
1830 | $participant = new CRM_Event_DAO_Participant(); |
1831 | $participant->contact_id = $contactId; | |
1832 | $participant->find(); | |
1833 | while ($participant->fetch()) { | |
1834 | self::deleteParticipant($participant->id); | |
1835 | } | |
1836 | } | |
6a488035 | 1837 | |
0cf587a7 | 1838 | /** |
c490a46a | 1839 | * @param array $params |
100fef9d CW |
1840 | * @param int $participantId |
1841 | * @param int $contributionId | |
0cf587a7 | 1842 | * @param $feeBlock |
100fef9d | 1843 | * @param array $lineItems |
0cf587a7 | 1844 | * @param $paidAmount |
100fef9d | 1845 | * @param int $priceSetId |
0cf587a7 | 1846 | */ |
00be9182 | 1847 | public static function changeFeeSelections($params, $participantId, $contributionId, $feeBlock, $lineItems, $paidAmount, $priceSetId) { |
0aaf8fe9 PJ |
1848 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
1849 | $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); | |
e59da3a9 | 1850 | $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses); |
0aaf8fe9 PJ |
1851 | $previousLineItems = CRM_Price_BAO_LineItem::getLineItems($participantId, 'participant'); |
1852 | CRM_Price_BAO_PriceSet::processAmount($feeBlock, | |
1853 | $params, $lineItems | |
1854 | ); | |
1855 | ||
1856 | // get the submitted | |
1857 | foreach ($feeBlock as $id => $values) { | |
1858 | CRM_Price_BAO_LineItem::format($id, $params, $values, $submittedLineItems); | |
1859 | $submittedFieldId[] = CRM_Utils_Array::retrieveValueRecursive($submittedLineItems, 'price_field_id'); | |
1860 | } | |
5b22467a RK |
1861 | if (!empty($submittedLineItems)) { |
1862 | $insertLines = $submittedLineItems; | |
1863 | ||
1864 | $submittedFieldValueIds = array_keys($submittedLineItems); | |
1865 | $updateLines = array(); | |
1866 | foreach ($previousLineItems as $id => $previousLineItem) { | |
1867 | // check through the submitted items if the previousItem exists, | |
1868 | // if found in submitted items, do not use it for new item creations | |
1869 | if (in_array($previousLineItem['price_field_value_id'], $submittedFieldValueIds)) { | |
1870 | // if submitted line items are existing don't fire INSERT query | |
1871 | unset($insertLines[$previousLineItem['price_field_value_id']]); | |
1872 | // for updating the line items i.e. use-case - once deselect-option selecting again | |
1873 | if (($previousLineItem['line_total'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['line_total']) || | |
8ab8920e BS |
1874 | ($submittedLineItems[$previousLineItem['price_field_value_id']]['line_total'] == 0 && $submittedLineItems[$previousLineItem['price_field_value_id']]['qty'] == 1) || |
1875 | ($previousLineItem['qty'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['qty']) | |
ddca8f33 | 1876 | ) { |
5b22467a RK |
1877 | $updateLines[$previousLineItem['price_field_value_id']] = $submittedLineItems[$previousLineItem['price_field_value_id']]; |
1878 | $updateLines[$previousLineItem['price_field_value_id']]['id'] = $id; | |
1879 | } | |
0b2b58ea | 1880 | } |
0aaf8fe9 | 1881 | } |
b0a9575b | 1882 | |
5b22467a RK |
1883 | $submittedFields = implode(', ', $submittedFieldId); |
1884 | $submittedFieldValues = implode(', ', $submittedFieldValueIds); | |
1885 | } | |
0aaf8fe9 | 1886 | if (!empty($submittedFields) && !empty($submittedFieldValues)) { |
13d4664b | 1887 | $updateLineItem = "UPDATE civicrm_line_item li |
0aaf8fe9 PJ |
1888 | INNER JOIN civicrm_financial_item fi |
1889 | ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') | |
0aaf8fe9 | 1890 | SET li.qty = 0, |
5a18a545 | 1891 | li.line_total = 0.00, |
1892 | li.tax_amount = NULL | |
0aaf8fe9 | 1893 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) AND |
13d4664b | 1894 | (price_field_value_id NOT IN ({$submittedFieldValues})) |
0aaf8fe9 PJ |
1895 | "; |
1896 | CRM_Core_DAO::executeQuery($updateLineItem); | |
13d4664b PJ |
1897 | |
1898 | // gathering necessary info to record negative (deselected) financial_item | |
1899 | $updateFinancialItem = " | |
5a18a545 | 1900 | SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount |
13d4664b PJ |
1901 | FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') |
1902 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) | |
1903 | GROUP BY li.entity_table, li.entity_id, price_field_value_id | |
1904 | "; | |
1905 | $updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem); | |
a191ff3d | 1906 | $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId, 'DESC', TRUE); |
13d4664b | 1907 | $trxnId['id'] = $trxn['financialTrxnId']; |
aaffa79f | 1908 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
5a18a545 | 1909 | $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); |
13d4664b | 1910 | $updateFinancialItemInfoValues = array(); |
a191ff3d | 1911 | $financialItemsArray = array(); |
13d4664b PJ |
1912 | while ($updateFinancialItemInfoDAO->fetch()) { |
1913 | $updateFinancialItemInfoValues = (array) $updateFinancialItemInfoDAO; | |
1914 | $updateFinancialItemInfoValues['transaction_date'] = date('YmdHis'); | |
1915 | // the below params are not needed | |
1916 | unset($updateFinancialItemInfoValues['id']); | |
1917 | unset($updateFinancialItemInfoValues['created_date']); | |
1918 | // if not submitted and difference is not 0 make it negative | |
1919 | if (!in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] != 0) { | |
1920 | // INSERT negative financial_items | |
ddca8f33 | 1921 | $updateFinancialItemInfoValues['amount'] = -$updateFinancialItemInfoValues['amount']; |
5a18a545 | 1922 | if ($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']) { |
b4df5f60 PN |
1923 | $updateFinancialItemInfoValues['tax']['amount'] = -($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']); |
1924 | $updateFinancialItemInfoValues['tax']['description'] = $taxTerm; | |
5a18a545 | 1925 | if ($updateFinancialItemInfoValues['financial_type_id']) { |
b4df5f60 | 1926 | $updateFinancialItemInfoValues['tax']['financial_account_id'] = CRM_Contribute_BAO_Contribution::getFinancialAccountId($updateFinancialItemInfoValues['financial_type_id']); |
5a18a545 | 1927 | } |
5a18a545 | 1928 | } |
b4df5f60 PN |
1929 | // INSERT negative financial_items for tax amount |
1930 | $financialItemsArray[] = $updateFinancialItemInfoValues; | |
13d4664b PJ |
1931 | } |
1932 | // if submitted and difference is 0 add a positive entry again | |
1933 | elseif (in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] == 0) { | |
1934 | $updateFinancialItemInfoValues['amount'] = $updateFinancialItemInfoValues['amount']; | |
5a18a545 | 1935 | // INSERT financial_items for tax amount |
1936 | if ($updateFinancialItemInfoValues['entity_id'] == $updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['id'] && | |
1937 | isset($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['tax_amount']) | |
1938 | ) { | |
a191ff3d PN |
1939 | $updateFinancialItemInfoValues['tax']['amount'] = $updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['tax_amount']; |
1940 | $updateFinancialItemInfoValues['tax']['description'] = $taxTerm; | |
5a18a545 | 1941 | if ($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['financial_type_id']) { |
a191ff3d | 1942 | $updateFinancialItemInfoValues['tax']['financial_account_id'] = CRM_Contribute_BAO_Contribution::getFinancialAccountId($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['financial_type_id']); |
5a18a545 | 1943 | } |
5a18a545 | 1944 | } |
2b76f3da | 1945 | $financialItemsArray[] = $updateFinancialItemInfoValues; |
13d4664b PJ |
1946 | } |
1947 | } | |
0aaf8fe9 | 1948 | } |
ddca8f33 | 1949 | elseif (empty($submittedFields) && empty($submittedFieldValues)) { |
5b22467a RK |
1950 | $updateLineItem = "UPDATE civicrm_line_item li |
1951 | INNER JOIN civicrm_financial_item fi | |
1952 | ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') | |
1953 | SET li.qty = 0, | |
1954 | li.line_total = 0.00, | |
1955 | li.tax_amount = NULL | |
1956 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})"; | |
1957 | CRM_Core_DAO::executeQuery($updateLineItem); | |
1958 | } | |
b612d89b | 1959 | $amountLevel = array(); |
1960 | $totalParticipant = $participantCount = 0; | |
0b2b58ea PJ |
1961 | if (!empty($updateLines)) { |
1962 | foreach ($updateLines as $valueId => $vals) { | |
b612d89b | 1963 | $taxAmount = "NULL"; |
5525990d | 1964 | if (isset($vals['tax_amount'])) { |
5a18a545 | 1965 | $taxAmount = $vals['tax_amount']; |
1966 | } | |
b612d89b | 1967 | $amountLevel[] = $vals['label'] . ' - ' . (float) $vals['qty']; |
1968 | if (isset($vals['participant_count'])) { | |
1969 | $participantCount = $vals['participant_count']; | |
1970 | $totalParticipant += $vals['participant_count']; | |
5a18a545 | 1971 | } |
0b2b58ea PJ |
1972 | $updateLineItem = " |
1973 | UPDATE civicrm_line_item li | |
0b2b58ea | 1974 | SET li.qty = {$vals['qty']}, |
8dfe9fe3 | 1975 | li.line_total = {$vals['line_total']}, |
3b5db8ce | 1976 | li.tax_amount = {$taxAmount}, |
8dfe9fe3 | 1977 | li.unit_price = {$vals['unit_price']}, |
b612d89b | 1978 | li.participant_count = {$participantCount}, |
122a36b9 | 1979 | li.label = %1 |
0b2b58ea PJ |
1980 | WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) AND |
1981 | (price_field_value_id = {$valueId}) | |
1982 | "; | |
122a36b9 | 1983 | CRM_Core_DAO::executeQuery($updateLineItem, array(1 => array($vals['label'], 'String'))); |
0b2b58ea PJ |
1984 | } |
1985 | } | |
b75ef57e PJ |
1986 | // insert new 'adjusted amount' transaction entry and update contribution entry. |
1987 | // ensure entity_financial_trxn table has a linking of it. | |
28e4e707 | 1988 | // insert new line items |
5b22467a RK |
1989 | if (!empty($insertLines)) { |
1990 | foreach ($insertLines as $valueId => $lineParams) { | |
1991 | $lineParams['entity_table'] = 'civicrm_participant'; | |
1992 | $lineParams['entity_id'] = $participantId; | |
1993 | $lineParams['contribution_id'] = $contributionId; | |
1994 | $lineObj = CRM_Price_BAO_LineItem::create($lineParams); | |
1995 | } | |
28e4e707 PJ |
1996 | } |
1997 | ||
1998 | // the recordAdjustedAmt code would execute over here | |
1999 | $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); | |
2000 | if (count($ids) > 1) { | |
2001 | $total = 0; | |
2002 | foreach ($ids as $val) { | |
2003 | $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant'); | |
2004 | } | |
2005 | $updatedAmount = $total; | |
2006 | } | |
2007 | else { | |
2008 | $updatedAmount = $params['amount']; | |
2009 | } | |
5a18a545 | 2010 | if (strlen($params['tax_amount']) != 0) { |
2011 | $taxAmount = $params['tax_amount']; | |
2012 | } | |
2013 | else { | |
2014 | $taxAmount = "NULL"; | |
2015 | } | |
b612d89b | 2016 | $displayParticipantCount = ''; |
2017 | if ($totalParticipant > 0) { | |
2018 | $displayParticipantCount = ' Participant Count -' . $totalParticipant; | |
2019 | } | |
2020 | $updateAmountLevel = NULL; | |
2021 | if (!empty($amountLevel)) { | |
2022 | $updateAmountLevel = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR; | |
2023 | } | |
2024 | $trxn = self::recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount, $updateAmountLevel); | |
a191ff3d PN |
2025 | $trxnId = array(); |
2026 | if ($trxn) { | |
2027 | $trxnId['id'] = $trxn->id; | |
2028 | foreach ($financialItemsArray as $updateFinancialItemInfoValues) { | |
2029 | CRM_Financial_BAO_FinancialItem::create($updateFinancialItemInfoValues, NULL, $trxnId); | |
2030 | if (!empty($updateFinancialItemInfoValues['tax'])) { | |
2031 | $updateFinancialItemInfoValues['tax']['amount'] = $updateFinancialItemInfoValues['amount']; | |
2032 | $updateFinancialItemInfoValues['tax']['description'] = $updateFinancialItemInfoValues['description']; | |
2033 | if (!empty($updateFinancialItemInfoValues['financial_account_id'])) { | |
2034 | $updateFinancialItemInfoValues['financial_account_id'] = $updateFinancialItemInfoValues['tax']['financial_account_id']; | |
2035 | } | |
2b76f3da | 2036 | CRM_Financial_BAO_FinancialItem::create($updateFinancialItemInfoValues, NULL, $trxnId); |
a191ff3d PN |
2037 | } |
2038 | } | |
2039 | } | |
b75ef57e PJ |
2040 | $fetchCon = array('id' => $contributionId); |
2041 | $updatedContribution = CRM_Contribute_BAO_Contribution::retrieve($fetchCon, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); | |
28e4e707 | 2042 | // insert financial items |
5b22467a RK |
2043 | if (!empty($insertLines)) { |
2044 | foreach ($insertLines as $valueId => $lineParams) { | |
2045 | $lineParams['entity_table'] = 'civicrm_participant'; | |
2046 | $lineParams['entity_id'] = $participantId; | |
2047 | $lineObj = CRM_Price_BAO_LineItem::retrieve($lineParams, CRM_Core_DAO::$_nullArray); | |
2048 | // insert financial items | |
2049 | // ensure entity_financial_trxn table has a linking of it. | |
a191ff3d | 2050 | $prevItem = CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, NULL, $trxnId); |
5b22467a | 2051 | if (isset($lineObj->tax_amount)) { |
a191ff3d | 2052 | CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, TRUE, $trxnId); |
5b22467a | 2053 | } |
5a18a545 | 2054 | } |
0aaf8fe9 | 2055 | } |
f6bae84f | 2056 | |
d5397f2f PJ |
2057 | // update participant fee_amount column |
2058 | $partUpdateFeeAmt['id'] = $participantId; | |
a1acfd53 | 2059 | $getUpdatedLineItems = "SELECT * |
2060 | FROM civicrm_line_item | |
e990bc36 | 2061 | WHERE (entity_table = 'civicrm_participant' AND entity_id = {$participantId} AND qty > 0)"; |
2062 | $getUpdatedLineItemsDAO = CRM_Core_DAO::executeQuery($getUpdatedLineItems); | |
a1acfd53 | 2063 | while ($getUpdatedLineItemsDAO->fetch()) { |
2064 | $line[$getUpdatedLineItemsDAO->price_field_value_id] = $getUpdatedLineItemsDAO->label . ' - ' . (float) $getUpdatedLineItemsDAO->qty; | |
3ce0c12e C |
2065 | } |
2066 | ||
2067 | $partUpdateFeeAmt['fee_level'] = implode(', ', $line); | |
d5397f2f PJ |
2068 | $partUpdateFeeAmt['fee_amount'] = $params['amount']; |
2069 | self::add($partUpdateFeeAmt); | |
2070 | ||
f6bae84f PJ |
2071 | //activity creation |
2072 | self::addActivityForSelection($participantId, 'Change Registration'); | |
2073 | } | |
2074 | ||
0cf587a7 | 2075 | /** |
54957108 | 2076 | * Record adjusted amount. |
2077 | * | |
2078 | * @param int $updatedAmount | |
2079 | * @param int $paidAmount | |
100fef9d | 2080 | * @param int $contributionId |
54957108 | 2081 | * |
2082 | * @param int $taxAmount | |
2083 | * @param bool $updateAmountLevel | |
2084 | * | |
2085 | * @return bool|\CRM_Core_BAO_FinancialTrxn | |
0cf587a7 | 2086 | */ |
b612d89b | 2087 | public static function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) { |
18fdfcc3 | 2088 | $pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); |
9cf473a4 | 2089 | $pendingAmount = CRM_Utils_Array::value('total_amount', $pendingAmount, 0); |
2090 | $balanceAmt = $updatedAmount - $paidAmount; | |
2091 | if ($paidAmount != $pendingAmount) { | |
2092 | $balanceAmt -= $pendingAmount; | |
2093 | } | |
2094 | ||
f6bae84f PJ |
2095 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
2096 | $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); | |
e59da3a9 | 2097 | $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses); |
921bca19 DG |
2098 | $completedStatusId = array_search('Completed', $contributionStatuses); |
2099 | ||
2100 | $updatedContributionDAO = new CRM_Contribute_BAO_Contribution(); | |
2b76f3da | 2101 | $adjustedTrxn = $skip = FALSE; |
0aaf8fe9 | 2102 | if ($balanceAmt) { |
c6191312 | 2103 | if ($balanceAmt > 0 && $paidAmount != 0) { |
0aaf8fe9 PJ |
2104 | $contributionStatusVal = $partiallyPaidStatusId; |
2105 | } | |
c6191312 | 2106 | elseif ($balanceAmt < 0 && $paidAmount != 0) { |
e59da3a9 | 2107 | $contributionStatusVal = $pendingRefundStatusId; |
0aaf8fe9 | 2108 | } |
921bca19 | 2109 | elseif ($paidAmount == 0) { |
e59da3a9 DG |
2110 | //skip updating the contribution status if no payment is made |
2111 | $skip = TRUE; | |
921bca19 DG |
2112 | $updatedContributionDAO->cancel_date = 'null'; |
2113 | $updatedContributionDAO->cancel_reason = NULL; | |
2114 | } | |
0aaf8fe9 PJ |
2115 | // update contribution status and total amount without trigger financial code |
2116 | // as this is handled in current BAO function used for change selection | |
0aaf8fe9 | 2117 | $updatedContributionDAO->id = $contributionId; |
e59da3a9 DG |
2118 | if (!$skip) { |
2119 | $updatedContributionDAO->contribution_status_id = $contributionStatusVal; | |
2120 | } | |
b8adb851 | 2121 | $updatedContributionDAO->total_amount = $updatedContributionDAO->net_amount = $updatedAmount; |
2122 | $updatedContributionDAO->fee_amount = 0; | |
5a18a545 | 2123 | $updatedContributionDAO->tax_amount = $taxAmount; |
b612d89b | 2124 | if (!empty($updateAmountLevel)) { |
2125 | $updatedContributionDAO->amount_level = $updateAmountLevel; | |
2126 | } | |
0aaf8fe9 | 2127 | $updatedContributionDAO->save(); |
13d4664b | 2128 | // adjusted amount financial_trxn creation |
18fdfcc3 PN |
2129 | $updatedContribution = CRM_Contribute_BAO_Contribution::getValues( |
2130 | array('id' => $contributionId), | |
2131 | CRM_Core_DAO::$_nullArray, | |
2132 | CRM_Core_DAO::$_nullArray | |
2133 | ); | |
2134 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); | |
2135 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId); | |
2136 | $adjustedTrxnValues = array( | |
2137 | 'from_financial_account_id' => NULL, | |
2138 | 'to_financial_account_id' => $toFinancialAccount, | |
2139 | 'total_amount' => $balanceAmt, | |
9cf473a4 | 2140 | 'status_id' => $completedStatusId, |
18fdfcc3 PN |
2141 | 'payment_instrument_id' => $updatedContribution->payment_instrument_id, |
2142 | 'contribution_id' => $updatedContribution->id, | |
2143 | 'trxn_date' => date('YmdHis'), | |
2144 | 'currency' => $updatedContribution->currency, | |
2145 | ); | |
2146 | $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues); | |
0aaf8fe9 | 2147 | } |
a191ff3d | 2148 | return $adjustedTrxn; |
0aaf8fe9 PJ |
2149 | } |
2150 | ||
0cf587a7 | 2151 | /** |
100fef9d | 2152 | * @param int $participantId |
0cf587a7 EM |
2153 | * @param $activityType |
2154 | * | |
2155 | * @throws CRM_Core_Exception | |
2156 | */ | |
00be9182 | 2157 | public static function addActivityForSelection($participantId, $activityType) { |
0aaf8fe9 PJ |
2158 | $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'event_id'); |
2159 | $contactId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'contact_id'); | |
2160 | ||
2161 | $date = CRM_Utils_Date::currentDBDate(); | |
2162 | $event = CRM_Event_BAO_Event::getEvents(0, $eventId); | |
2163 | $eventTitle = $event[$eventId]; | |
2164 | $subject = "Registration selections changed for $eventTitle"; | |
2165 | $targetCid = $contactId; | |
2166 | $srcRecId = $participantId; | |
2167 | ||
2168 | // activity params | |
2169 | $activityParams = array( | |
2170 | 'source_contact_id' => $targetCid, | |
2171 | 'source_record_id' => $srcRecId, | |
2172 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
2173 | $activityType, | |
2174 | 'name' | |
2175 | ), | |
2176 | 'subject' => $subject, | |
2177 | 'activity_date_time' => $date, | |
2178 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', | |
2179 | 'Completed', | |
2180 | 'name' | |
2181 | ), | |
2182 | 'skipRecentView' => TRUE, | |
2183 | ); | |
2184 | ||
2185 | // create activity with target contacts | |
2186 | $session = CRM_Core_Session::singleton(); | |
2187 | $id = $session->get('userID'); | |
2188 | if ($id) { | |
2189 | $activityParams['source_contact_id'] = $id; | |
2190 | $activityParams['target_contact_id'][] = $targetCid; | |
2191 | } | |
2192 | CRM_Activity_BAO_Activity::create($activityParams); | |
2193 | } | |
f76b27fe CW |
2194 | |
2195 | /** | |
2196 | * Get options for a given field. | |
2197 | * @see CRM_Core_DAO::buildOptions | |
2198 | * | |
d4dd1e85 TO |
2199 | * @param string $fieldName |
2200 | * @param string $context | |
a1a2a83d | 2201 | * @see CRM_Core_DAO::buildOptionsContext |
d4dd1e85 | 2202 | * @param array $props |
16b10e64 | 2203 | * whatever is known about this dao object. |
f76b27fe | 2204 | * |
5c766a0b | 2205 | * @return array|bool |
f76b27fe CW |
2206 | */ |
2207 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
2208 | $params = array('condition' => array()); | |
2209 | ||
2210 | if ($fieldName == 'status_id' && $context != 'validate') { | |
2211 | // Get rid of cart-related option if disabled | |
2212 | // FIXME: Why does this option even exist if cart is disabled? | |
aaffa79f | 2213 | if (!Civi::settings()->get('enable_cart')) { |
f76b27fe CW |
2214 | $params['condition'][] = "name <> 'Pending in cart'"; |
2215 | } | |
2216 | } | |
2217 | ||
2218 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); | |
2219 | } | |
96025800 | 2220 | |
37d5fc55 | 2221 | } |