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