Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
939287ad | 13 | * Back office participant form. |
6a488035 TO |
14 | * |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | */ |
18 | ||
19 | /** | |
939287ad | 20 | * Back office participant form. |
6a488035 | 21 | */ |
6bd6d108 | 22 | class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment { |
6a488035 | 23 | |
2ee9bfad | 24 | /** |
25 | * Participant ID - use getParticipantID. | |
26 | * | |
27 | * @var int | |
28 | */ | |
29 | public $_pId; | |
30 | ||
31 | /** | |
32 | * ID of discount record. | |
33 | * | |
34 | * @var int | |
35 | */ | |
36 | public $_discountId; | |
37 | ||
96f50de2 CW |
38 | public $useLivePageJS = TRUE; |
39 | ||
6a488035 | 40 | /** |
66f9e52b | 41 | * The values for the contribution db object. |
6a488035 TO |
42 | * |
43 | * @var array | |
6a488035 TO |
44 | */ |
45 | public $_values; | |
46 | ||
47 | /** | |
66f9e52b | 48 | * The values for the quickconfig for priceset. |
6a488035 | 49 | * |
d51c6add | 50 | * @var bool |
6a488035 TO |
51 | */ |
52 | public $_quickConfig = NULL; | |
53 | ||
54 | /** | |
55 | * Price Set ID, if the new price set method is used | |
56 | * | |
57 | * @var int | |
6a488035 TO |
58 | */ |
59 | public $_priceSetId; | |
60 | ||
61 | /** | |
66f9e52b | 62 | * Array of fields for the price set. |
6a488035 TO |
63 | * |
64 | * @var array | |
6a488035 TO |
65 | */ |
66 | public $_priceSet; | |
67 | ||
68 | /** | |
6452294d | 69 | * The id of the participation that we are processing. |
6a488035 TO |
70 | * |
71 | * @var int | |
6a488035 TO |
72 | */ |
73 | public $_id; | |
74 | ||
75 | /** | |
66f9e52b | 76 | * The id of the note. |
6a488035 TO |
77 | * |
78 | * @var int | |
6a488035 TO |
79 | */ |
80 | protected $_noteId = NULL; | |
81 | ||
82 | /** | |
0dc4ef42 | 83 | * |
84 | * Use parent $this->contactID | |
85 | * | |
66f9e52b | 86 | * The id of the contact associated with this participation. |
6a488035 TO |
87 | * |
88 | * @var int | |
90b461f1 | 89 | * @deprecated |
6a488035 TO |
90 | */ |
91 | public $_contactId; | |
92 | ||
93 | /** | |
66f9e52b | 94 | * Array of event values. |
6a488035 TO |
95 | * |
96 | * @var array | |
6a488035 TO |
97 | */ |
98 | protected $_event; | |
99 | ||
100 | /** | |
101 | * Are we operating in "single mode", i.e. adding / editing only | |
8535cf1c | 102 | * one participant record, or is this a batch add operation. |
103 | * | |
104 | * Note the goal is to disentangle all the non-single stuff | |
105 | * to CRM_Event_Form_Task_Register and discontinue this param. | |
6a488035 | 106 | * |
d51c6add | 107 | * @var bool |
6a488035 | 108 | */ |
8535cf1c | 109 | public $_single = TRUE; |
6a488035 TO |
110 | |
111 | /** | |
66f9e52b | 112 | * If event is paid or unpaid. |
b2a8013e | 113 | * |
90b461f1 | 114 | * @var bool |
6a488035 TO |
115 | */ |
116 | public $_isPaidEvent; | |
117 | ||
118 | /** | |
66f9e52b | 119 | * Page action. |
b2a8013e | 120 | * |
90b461f1 | 121 | * @var int |
6a488035 TO |
122 | */ |
123 | public $_action; | |
124 | ||
6a488035 | 125 | /** |
66f9e52b | 126 | * Event Type Id. |
b2a8013e | 127 | * |
90b461f1 | 128 | * @var int |
6a488035 TO |
129 | */ |
130 | protected $_eventTypeId = NULL; | |
131 | ||
132 | /** | |
66f9e52b | 133 | * Participant status Id. |
b2a8013e | 134 | * |
90b461f1 | 135 | * @var int |
6a488035 TO |
136 | */ |
137 | protected $_statusId = NULL; | |
138 | ||
139 | /** | |
66f9e52b | 140 | * Cache all the participant statuses. |
b2a8013e | 141 | * |
90b461f1 | 142 | * @var array |
6a488035 TO |
143 | */ |
144 | protected $_participantStatuses; | |
145 | ||
146 | /** | |
66f9e52b | 147 | * Participant mode. |
b2a8013e | 148 | * |
90b461f1 | 149 | * @var string |
6a488035 TO |
150 | */ |
151 | public $_mode = NULL; | |
152 | ||
153 | /** | |
66f9e52b | 154 | * Event ID preselect. |
b2a8013e | 155 | * |
90b461f1 | 156 | * @var int |
6a488035 TO |
157 | */ |
158 | public $_eID = NULL; | |
159 | ||
c490a46a | 160 | /** |
66f9e52b | 161 | * Line Item for Price Set. |
b2a8013e | 162 | * |
90b461f1 | 163 | * @var array |
c490a46a | 164 | */ |
6a488035 | 165 | public $_lineItem = NULL; |
6a488035 | 166 | |
d424ffde | 167 | /** |
66f9e52b | 168 | * Contribution mode for event registration for offline mode. |
b2a8013e | 169 | * |
90b461f1 | 170 | * @var string |
0f2b049e | 171 | * @deprecated |
c490a46a | 172 | */ |
6a488035 TO |
173 | public $_contributeMode = 'direct'; |
174 | ||
175 | public $_online; | |
176 | ||
177 | /** | |
100fef9d | 178 | * Store id of role custom data type ( option value ) |
b2a8013e | 179 | * |
90b461f1 | 180 | * @var int |
6a488035 TO |
181 | */ |
182 | protected $_roleCustomDataTypeID; | |
183 | ||
184 | /** | |
100fef9d | 185 | * Store id of event Name custom data type ( option value) |
b2a8013e | 186 | * |
90b461f1 | 187 | * @var int |
6a488035 TO |
188 | */ |
189 | protected $_eventNameCustomDataTypeID; | |
190 | ||
191 | /** | |
66f9e52b | 192 | * Selected discount id. |
b2a8013e | 193 | * |
90b461f1 | 194 | * @var int |
6a488035 TO |
195 | */ |
196 | public $_originalDiscountId = NULL; | |
197 | ||
198 | /** | |
66f9e52b | 199 | * Event id. |
b2a8013e | 200 | * |
90b461f1 | 201 | * @var int |
6a488035 TO |
202 | */ |
203 | public $_eventId = NULL; | |
204 | ||
205 | /** | |
100fef9d | 206 | * Id of payment, if any |
b2a8013e | 207 | * |
90b461f1 | 208 | * @var int |
6a488035 TO |
209 | */ |
210 | public $_paymentId = NULL; | |
211 | ||
9a4df24c | 212 | /** |
9a4df24c | 213 | * @var null |
90b461f1 | 214 | * @todo add explanatory note about this |
9a4df24c EM |
215 | */ |
216 | public $_onlinePendingContributionId = NULL; | |
d5965a37 | 217 | |
a3aed42a | 218 | /** |
219 | * Stored participant record. | |
220 | * | |
221 | * @var array | |
222 | */ | |
223 | protected $participantRecord; | |
224 | ||
d3e6e9a4 | 225 | /** |
226 | * Params for creating a payment to add to the contribution. | |
227 | * | |
228 | * @var array | |
229 | */ | |
230 | protected $createPaymentParams = []; | |
231 | ||
232 | /** | |
233 | * Get params to create payments. | |
234 | * | |
235 | * @return array | |
236 | */ | |
237 | public function getCreatePaymentParams(): array { | |
238 | return $this->createPaymentParams; | |
239 | } | |
240 | ||
241 | /** | |
242 | * Set params to create payments. | |
243 | * | |
244 | * @param array $createPaymentParams | |
245 | */ | |
246 | public function setCreatePaymentParams(array $createPaymentParams) { | |
247 | $this->createPaymentParams = $createPaymentParams; | |
248 | } | |
249 | ||
6e62b28c TM |
250 | /** |
251 | * Explicitly declare the entity api name. | |
252 | */ | |
253 | public function getDefaultEntity() { | |
254 | return 'Participant'; | |
255 | } | |
9a4df24c | 256 | |
c1a6d6ac SL |
257 | /** |
258 | * Default form context used as part of addField() | |
259 | */ | |
260 | public function getDefaultContext() { | |
261 | return 'create'; | |
262 | } | |
263 | ||
6a488035 | 264 | /** |
66f9e52b | 265 | * Set variables up before form is built. |
6a488035 | 266 | * |
6ba1830e | 267 | * @throws \CRM_Core_Exception |
b28a34c3 | 268 | * @throws \CiviCRM_API3_Exception |
6a488035 TO |
269 | */ |
270 | public function preProcess() { | |
0dc4ef42 | 271 | parent::preProcess(); |
9c1bc317 | 272 | $this->_showFeeBlock = $_GET['eventId'] ?? NULL; |
6a488035 TO |
273 | $this->assign('showFeeBlock', FALSE); |
274 | $this->assign('feeBlockPaid', FALSE); | |
275 | ||
0dc4ef42 | 276 | // @todo eliminate this duplication. |
277 | $this->_contactId = $this->_contactID; | |
353ffa53 | 278 | $this->_eID = CRM_Utils_Request::retrieve('eid', 'Positive', $this); |
edc80cda | 279 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
280 | $this->assign('context', $this->_context); |
281 | ||
0dc4ef42 | 282 | if ($this->_contactID) { |
be2fb01f | 283 | $this->setPageTitle(ts('Event Registration for %1', [1 => $this->userDisplayName])); |
e2046b33 CW |
284 | } |
285 | else { | |
286 | $this->setPageTitle(ts('Event Registration')); | |
6a488035 TO |
287 | } |
288 | ||
289 | // check the current path, if search based, then dont get participantID | |
290 | // CRM-5792 | |
291 | $path = CRM_Utils_System::currentPath(); | |
0edbaec6 JM |
292 | if ( |
293 | strpos($path, 'civicrm/contact/search') === 0 || | |
294 | strpos($path, 'civicrm/group/search') === 0 | |
353ffa53 | 295 | ) { |
6a488035 TO |
296 | $this->_id = NULL; |
297 | } | |
298 | else { | |
675f074e | 299 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
6a488035 TO |
300 | } |
301 | ||
302 | if ($this->_id) { | |
29c61b58 | 303 | $this->assign('participantId', $this->_id); |
29c61b58 | 304 | |
6a488035 TO |
305 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
306 | $this->_id, 'id', 'participant_id' | |
307 | ); | |
0b2b58ea PJ |
308 | |
309 | $this->assign('hasPayment', $this->_paymentId); | |
685dc433 PN |
310 | $this->assign('componentId', $this->_id); |
311 | $this->assign('component', 'event'); | |
0b2b58ea | 312 | |
a4e0d7b5 DG |
313 | // CRM-12615 - Get payment information from the primary registration |
314 | if ((!$this->_paymentId) && ($this->_action == CRM_Core_Action::UPDATE)) { | |
315 | $registered_by_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
316 | $this->_id, 'registered_by_id', 'id' | |
317 | ); | |
318 | if ($registered_by_id) { | |
319 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
320 | $registered_by_id, 'id', 'participant_id' | |
321 | ); | |
322 | $this->assign('registeredByParticipantId', $registered_by_id); | |
323 | } | |
324 | } | |
6a488035 | 325 | } |
5fda4b16 | 326 | $this->setCustomDataTypes(); |
6a488035 | 327 | |
5a5d0b1a | 328 | $this->assign('participantMode', $this->_mode); |
6a488035 | 329 | |
e7adaaba | 330 | $this->assign('showFeeBlock', $this->_showFeeBlock); |
6a488035 | 331 | if ($this->_showFeeBlock) { |
6a488035 TO |
332 | $isMonetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_showFeeBlock, 'is_monetary'); |
333 | if ($isMonetary) { | |
334 | $this->assign('feeBlockPaid', TRUE); | |
335 | } | |
336 | return CRM_Event_Form_EventFees::preProcess($this); | |
337 | } | |
338 | ||
f36fd0f6 | 339 | $this->assignUrlPath(); |
6a488035 TO |
340 | |
341 | $this->assign('single', $this->_single); | |
342 | ||
343 | if (!$this->_id) { | |
344 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
345 | } | |
346 | $this->assign('action', $this->_action); | |
347 | ||
348 | // check for edit permission | |
349 | if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) { | |
e22ec653 | 350 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
6a488035 TO |
351 | } |
352 | ||
353 | if ($this->_action & CRM_Core_Action::DELETE) { | |
354 | // check delete permission for contribution | |
355 | if ($this->_id && $this->_paymentId && !CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { | |
e22ec653 | 356 | CRM_Core_Error::statusBounce(ts("This Participant is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record.")); |
6a488035 TO |
357 | } |
358 | return; | |
359 | } | |
360 | ||
e7adaaba CW |
361 | // assign participant id to the template |
362 | $this->assign('participantId', $this->_id); | |
6a488035 TO |
363 | |
364 | // when fee amount is included in form | |
8cc574cf | 365 | if (!empty($_POST['hidden_feeblock']) || !empty($_POST['send_receipt'])) { |
1db1f8e3 | 366 | if ($this->_submitValues['event_id']) { |
367 | $this->_eventId = $this->_submitValues['event_id']; | |
368 | } | |
6a488035 | 369 | CRM_Event_Form_EventFees::preProcess($this); |
eeca0339 | 370 | $this->buildEventFeeForm($this); |
6a488035 TO |
371 | CRM_Event_Form_EventFees::setDefaultValues($this); |
372 | } | |
373 | ||
374 | // when custom data is included in this page | |
a7488080 | 375 | if (!empty($_POST['hidden_custom'])) { |
2975f0aa | 376 | $eventId = (int) ($_POST['event_id'] ?? 0); |
7df021e5 | 377 | // Custom data of type participant role |
378 | // Note: Some earlier commits imply $_POST['role_id'] could be a comma separated string, | |
379 | // not sure if that ever really happens | |
a7488080 | 380 | if (!empty($_POST['role_id'])) { |
7df021e5 | 381 | foreach ($_POST['role_id'] as $roleID) { |
382 | CRM_Custom_Form_CustomData::preProcess($this, $this->_roleCustomDataTypeID, $roleID, 1, 'Participant', $this->_id); | |
6a488035 TO |
383 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
384 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
385 | } | |
386 | } | |
387 | ||
388 | //custom data of type participant event | |
6452294d | 389 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventNameCustomDataTypeID, $eventId, 1, 'Participant', $this->_id); |
6a488035 TO |
390 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
391 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
392 | ||
393 | // custom data of type participant event type | |
394 | $eventTypeId = NULL; | |
6452294d | 395 | if ($eventId) { |
6a488035 TO |
396 | $eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'event_type_id', 'id'); |
397 | } | |
398 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventTypeCustomDataTypeID, $eventTypeId, | |
399 | 1, 'Participant', $this->_id | |
400 | ); | |
401 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
402 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
403 | ||
404 | //custom data of type participant, ( we 'null' to reset subType and subName) | |
405 | CRM_Custom_Form_CustomData::preProcess($this, 'null', 'null', 1, 'Participant', $this->_id); | |
406 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
407 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
408 | } | |
409 | ||
410 | // CRM-4395, get the online pending contribution id. | |
411 | $this->_onlinePendingContributionId = NULL; | |
412 | if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { | |
413 | $this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id, | |
414 | 'Event' | |
415 | ); | |
416 | } | |
417 | $this->set('onlinePendingContributionId', $this->_onlinePendingContributionId); | |
6a488035 TO |
418 | } |
419 | ||
420 | /** | |
421 | * This function sets the default values for the form in edit/view mode | |
422 | * the default values are retrieved from the database | |
423 | * | |
6ba1830e MWMC |
424 | * @return array |
425 | * @throws \CRM_Core_Exception | |
6a488035 TO |
426 | */ |
427 | public function setDefaultValues() { | |
428 | if ($this->_showFeeBlock) { | |
429 | return CRM_Event_Form_EventFees::setDefaultValues($this); | |
430 | } | |
431 | ||
be2fb01f | 432 | $defaults = []; |
6a488035 TO |
433 | |
434 | if ($this->_action & CRM_Core_Action::DELETE) { | |
435 | return $defaults; | |
436 | } | |
437 | ||
438 | if ($this->_id) { | |
be2fb01f CW |
439 | $ids = []; |
440 | $params = ['id' => $this->_id]; | |
6a488035 TO |
441 | |
442 | CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); | |
443 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
444 | if ($defaults[$this->_id]['role_id']) { | |
bdc20176 | 445 | $roleIDs = explode($sep, $defaults[$this->_id]['role_id']); |
6a488035 TO |
446 | } |
447 | $this->_contactId = $defaults[$this->_id]['contact_id']; | |
448 | $this->_statusId = $defaults[$this->_id]['participant_status_id']; | |
449 | ||
450 | //set defaults for note | |
451 | $noteDetails = CRM_Core_BAO_Note::getNote($this->_id, 'civicrm_participant'); | |
452 | $defaults[$this->_id]['note'] = array_pop($noteDetails); | |
453 | ||
454 | // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859) | |
455 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
456 | $this->assign('additionalParticipants', CRM_Event_BAO_Participant::getAdditionalParticipants($this->_id)); | |
457 | } | |
458 | ||
459 | // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859) | |
a7488080 | 460 | if (!empty($defaults[$this->_id]['participant_registered_by_id'])) { |
6a488035 TO |
461 | $registered_by_contact_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', |
462 | $defaults[$this->_id]['participant_registered_by_id'], | |
463 | 'contact_id', 'id' | |
464 | ); | |
465 | $this->assign('participant_registered_by_id', $defaults[$this->_id]['participant_registered_by_id']); | |
466 | $this->assign('registered_by_contact_id', $registered_by_contact_id); | |
467 | $this->assign('registered_by_display_name', CRM_Contact_BAO_Contact::displayName($registered_by_contact_id)); | |
468 | } | |
469 | } | |
2944baac | 470 | elseif ($this->_contactID) { |
471 | $defaults[$this->_id]['contact_id'] = $this->_contactID; | |
472 | } | |
6a488035 | 473 | |
6a488035 TO |
474 | //setting default register date |
475 | if ($this->_action == CRM_Core_Action::ADD) { | |
e4659866 | 476 | $statuses = array_flip(CRM_Event_PseudoConstant::participantStatus()); |
9c1bc317 | 477 | $defaults[$this->_id]['status_id'] = $statuses['Registered'] ?? NULL; |
a7488080 | 478 | if (!empty($defaults[$this->_id]['event_id'])) { |
4c8f9b04 | 479 | $financialTypeID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', |
6a488035 | 480 | $defaults[$this->_id]['event_id'], |
bdc20176 | 481 | 'financial_type_id' |
d96cf288 | 482 | ); |
4c8f9b04 | 483 | if ($financialTypeID) { |
484 | $defaults[$this->_id]['financial_type_id'] = $financialTypeID; | |
6a488035 TO |
485 | } |
486 | } | |
487 | ||
488 | if ($this->_mode) { | |
489 | $fields["email-{$this->_bltID}"] = 1; | |
490 | $fields['email-Primary'] = 1; | |
491 | ||
492 | if ($this->_contactId) { | |
493 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $fields, $defaults); | |
494 | } | |
495 | ||
496 | if (empty($defaults["email-{$this->_bltID}"]) && | |
497 | !empty($defaults['email-Primary']) | |
498 | ) { | |
499 | $defaults[$this->_id]["email-{$this->_bltID}"] = $defaults['email-Primary']; | |
500 | } | |
501 | } | |
502 | ||
503 | $submittedRole = $this->getElementValue('role_id'); | |
a7488080 | 504 | if (!empty($submittedRole[0])) { |
6a488035 TO |
505 | $roleID = $submittedRole[0]; |
506 | } | |
507 | $submittedEvent = $this->getElementValue('event_id'); | |
a4e49a48 | 508 | if (!empty($submittedEvent[0])) { |
6a488035 TO |
509 | $eventID = $submittedEvent[0]; |
510 | } | |
6ba1830e | 511 | $defaults[$this->_id]['register_date'] = date('Y-m-d H:i:s'); |
6a488035 TO |
512 | } |
513 | else { | |
514 | $defaults[$this->_id]['record_contribution'] = 0; | |
515 | ||
516 | if ($defaults[$this->_id]['participant_is_pay_later']) { | |
517 | $this->assign('participant_is_pay_later', TRUE); | |
518 | } | |
519 | ||
520 | $this->assign('participant_status_id', $defaults[$this->_id]['participant_status_id']); | |
6a488035 TO |
521 | $eventID = $defaults[$this->_id]['event_id']; |
522 | ||
523 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
524 | ||
525 | $this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id'); | |
526 | if ($this->_discountId) { | |
527 | $this->set('discountId', $this->_discountId); | |
528 | } | |
529 | } | |
530 | ||
6a488035 TO |
531 | //assign event and role id, this is needed for Custom data building |
532 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
a7488080 | 533 | if (!empty($defaults[$this->_id]['participant_role_id'])) { |
6a488035 TO |
534 | $roleIDs = explode($sep, $defaults[$this->_id]['participant_role_id']); |
535 | } | |
6a488035 TO |
536 | if (isset($_POST['event_id'])) { |
537 | $eventID = $_POST['event_id']; | |
bdc20176 PJ |
538 | } |
539 | ||
22e263ad | 540 | if ($this->_eID) { |
bdc20176 PJ |
541 | $eventID = $this->_eID; |
542 | //@todo - rationalise the $this->_eID with $POST['event_id'], $this->_eid is set when eid=x is in the url | |
543 | $roleID = CRM_Core_DAO::getFieldValue( | |
544 | 'CRM_Event_DAO_Event', | |
545 | $this->_eID, | |
546 | 'default_role_id' | |
547 | ); | |
22e263ad | 548 | if (empty($roleIDs)) { |
bdc20176 | 549 | $roleIDs = (array) $defaults[$this->_id]['participant_role_id'] = $roleID; |
6a488035 | 550 | } |
bdc20176 PJ |
551 | $defaults[$this->_id]['event_id'] = $eventID; |
552 | } | |
553 | if (!empty($eventID)) { | |
554 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
555 | } | |
556 | //these should take precedence so we state them last | |
557 | $urlRoleIDS = CRM_Utils_Request::retrieve('roles', 'String'); | |
22e263ad | 558 | if ($urlRoleIDS) { |
bdc20176 PJ |
559 | $roleIDs = explode(',', $urlRoleIDS); |
560 | } | |
561 | if (isset($roleIDs)) { | |
cd120ea0 | 562 | $defaults[$this->_id]['role_id'] = implode(',', $roleIDs); |
6a488035 TO |
563 | } |
564 | ||
565 | if (isset($eventID)) { | |
566 | $this->assign('eventID', $eventID); | |
567 | $this->set('eventId', $eventID); | |
568 | } | |
569 | ||
570 | if (isset($this->_eventTypeId)) { | |
571 | $this->assign('eventTypeID', $this->_eventTypeId); | |
572 | } | |
573 | ||
574 | $this->assign('event_is_test', CRM_Utils_Array::value('event_is_test', $defaults[$this->_id])); | |
575 | return $defaults[$this->_id]; | |
576 | } | |
577 | ||
578 | /** | |
66f9e52b | 579 | * Build the form object. |
6a488035 | 580 | * |
355ba699 | 581 | * @return void |
6ba1830e MWMC |
582 | * @throws \CRM_Core_Exception |
583 | * @throws \CiviCRM_API3_Exception | |
6a488035 TO |
584 | */ |
585 | public function buildQuickForm() { | |
5774b54f | 586 | |
e8cf3013 PJ |
587 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); |
588 | $partiallyPaidStatusId = array_search('Partially paid', $participantStatuses); | |
81f3d017 | 589 | $this->assign('partiallyPaidStatusId', $partiallyPaidStatusId); |
e8cf3013 | 590 | |
6a488035 | 591 | if ($this->_showFeeBlock) { |
eeca0339 | 592 | return $this->buildEventFeeForm($this); |
6a488035 TO |
593 | } |
594 | ||
6a488035 TO |
595 | //need to assign custom data type to the template |
596 | $this->assign('customDataType', 'Participant'); | |
597 | ||
598 | $this->applyFilter('__ALL__', 'trim'); | |
599 | ||
600 | if ($this->_action & CRM_Core_Action::DELETE) { | |
601 | if ($this->_single) { | |
602 | $additionalParticipant = count(CRM_Event_BAO_Event::buildCustomProfile($this->_id, | |
603 | NULL, | |
604 | $this->_contactId, | |
605 | FALSE, | |
606 | TRUE | |
607 | )) - 1; | |
608 | if ($additionalParticipant) { | |
be2fb01f | 609 | $deleteParticipants = [ |
353ffa53 | 610 | 1 => ts('Delete this participant record along with associated participant record(s).'), |
6a488035 | 611 | 2 => ts('Delete only this participant record.'), |
be2fb01f | 612 | ]; |
6a488035 | 613 | $this->addRadio('delete_participant', NULL, $deleteParticipants, NULL, '<br />'); |
be2fb01f | 614 | $this->setDefaults(['delete_participant' => 1]); |
6a488035 TO |
615 | $this->assign('additionalParticipant', $additionalParticipant); |
616 | } | |
617 | } | |
be2fb01f | 618 | $this->addButtons([ |
90b461f1 SL |
619 | [ |
620 | 'type' => 'next', | |
621 | 'name' => ts('Delete'), | |
622 | 'spacing' => ' ', | |
623 | 'isDefault' => TRUE, | |
624 | ], | |
625 | [ | |
626 | 'type' => 'cancel', | |
627 | 'name' => ts('Cancel'), | |
628 | ], | |
629 | ]); | |
6a488035 TO |
630 | return; |
631 | } | |
632 | ||
2944baac | 633 | if ($this->_single) { |
634 | $contactField = $this->addEntityRef('contact_id', ts('Participant'), ['create' => TRUE, 'api' => ['extra' => ['email']]], TRUE); | |
635 | if ($this->_context != 'standalone') { | |
636 | $contactField->freeze(); | |
637 | } | |
6a488035 | 638 | } |
6a488035 | 639 | |
be2fb01f | 640 | $eventFieldParams = [ |
af00ced5 | 641 | 'entity' => 'Event', |
be2fb01f CW |
642 | 'select' => ['minimumInputLength' => 0], |
643 | 'api' => [ | |
644 | 'extra' => ['campaign_id', 'default_role_id', 'event_type_id'], | |
645 | ], | |
646 | ]; | |
6a488035 TO |
647 | |
648 | if ($this->_mode) { | |
1beb268a CW |
649 | // exclude events which are not monetary when credit card registration is used |
650 | $eventFieldParams['api']['params']['is_monetary'] = 1; | |
6a488035 | 651 | } |
b15c60e1 | 652 | $this->addPaymentProcessorSelect(TRUE, FALSE, FALSE); |
6a488035 | 653 | |
1beb268a | 654 | $element = $this->addEntityRef('event_id', ts('Event'), $eventFieldParams, TRUE); |
6a488035 TO |
655 | |
656 | //frozen the field fix for CRM-4171 | |
657 | if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
658 | if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
353ffa53 TO |
659 | $this->_id, 'contribution_id', 'participant_id' |
660 | ) | |
661 | ) { | |
6a488035 TO |
662 | $element->freeze(); |
663 | } | |
664 | } | |
665 | ||
666 | //CRM-7362 --add campaigns. | |
667 | $campaignId = NULL; | |
668 | if ($this->_id) { | |
669 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'campaign_id'); | |
670 | } | |
671 | if (!$campaignId) { | |
672 | $eventId = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
673 | if ($eventId) { | |
674 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'campaign_id'); | |
675 | } | |
676 | } | |
677 | CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId); | |
be2fb01f | 678 | $this->add('datepicker', 'register_date', ts('Registration Date'), [], TRUE, ['time' => TRUE]); |
6a488035 TO |
679 | |
680 | if ($this->_id) { | |
681 | $this->assign('entityID', $this->_id); | |
682 | } | |
683 | ||
be2fb01f | 684 | $this->addSelect('role_id', ['multiple' => TRUE, 'class' => 'huge'], TRUE); |
6a488035 TO |
685 | |
686 | // CRM-4395 | |
be2fb01f | 687 | $checkCancelledJs = ['onchange' => "return sendNotification( );"]; |
6a488035 TO |
688 | $confirmJS = NULL; |
689 | if ($this->_onlinePendingContributionId) { | |
690 | $cancelledparticipantStatusId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus()); | |
691 | $cancelledContributionStatusId = array_search('Cancelled', | |
692 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
693 | ); | |
be2fb01f | 694 | $checkCancelledJs = [ |
a130e045 | 695 | 'onchange' => "checkCancelled( this.value, {$cancelledparticipantStatusId},{$cancelledContributionStatusId});", |
be2fb01f | 696 | ]; |
6a488035 TO |
697 | |
698 | $participantStatusId = array_search('Pending from pay later', | |
699 | CRM_Event_PseudoConstant::participantStatus() | |
700 | ); | |
701 | $contributionStatusId = array_search('Completed', | |
702 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
703 | ); | |
be2fb01f | 704 | $confirmJS = ['onclick' => "return confirmStatus( {$participantStatusId}, {$contributionStatusId} );"]; |
6a488035 TO |
705 | } |
706 | ||
453ed6d1 KJ |
707 | // get the participant status names to build special status array which is used to show notification |
708 | // checkbox below participant status select | |
709 | $participantStatusName = CRM_Event_PseudoConstant::participantStatus(); | |
be2fb01f | 710 | $notificationStatuses = [ |
453ed6d1 KJ |
711 | 'Cancelled', |
712 | 'Pending from waitlist', | |
713 | 'Pending from approval', | |
714 | 'Expired', | |
be2fb01f | 715 | ]; |
453ed6d1 KJ |
716 | |
717 | // get the required status and then implode only ids | |
718 | $notificationStatusIds = implode(',', array_keys(array_intersect($participantStatusName, $notificationStatuses))); | |
719 | $this->assign('notificationStatusIds', $notificationStatusIds); | |
720 | ||
f76b27fe | 721 | $this->_participantStatuses = $statusOptions = CRM_Event_BAO_Participant::buildOptions('status_id', 'create'); |
f6bae84f | 722 | |
f76b27fe CW |
723 | // Only show refund status when editing |
724 | if ($this->_action & CRM_Core_Action::ADD) { | |
725 | $pendingRefundStatusId = array_search('Pending refund', $participantStatusName); | |
726 | if ($pendingRefundStatusId) { | |
727 | unset($statusOptions[$pendingRefundStatusId]); | |
728 | } | |
729 | } | |
730 | ||
be2fb01f | 731 | $this->addSelect('status_id', $checkCancelledJs + [ |
90b461f1 SL |
732 | 'options' => $statusOptions, |
733 | 'option_url' => 'civicrm/admin/participant_status', | |
734 | ], TRUE); | |
6a488035 TO |
735 | |
736 | $this->addElement('checkbox', 'is_notify', ts('Send Notification'), NULL); | |
737 | ||
be2fb01f | 738 | $this->addField('source', ['entity' => 'Participant', 'name' => 'source']); |
6a488035 TO |
739 | $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); |
740 | $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']); | |
741 | ||
be2fb01f | 742 | $buttons[] = [ |
6a488035 TO |
743 | 'type' => 'upload', |
744 | 'name' => ts('Save'), | |
745 | 'isDefault' => TRUE, | |
746 | 'js' => $confirmJS, | |
be2fb01f | 747 | ]; |
6a488035 TO |
748 | |
749 | $path = CRM_Utils_System::currentPath(); | |
be2fb01f | 750 | $excludeForPaths = [ |
239a5027 | 751 | 'civicrm/contact/search', |
21dfd5f5 | 752 | 'civicrm/group/search', |
be2fb01f | 753 | ]; |
239a5027 | 754 | if (!in_array($path, $excludeForPaths)) { |
be2fb01f | 755 | $buttons[] = [ |
6a488035 TO |
756 | 'type' => 'upload', |
757 | 'name' => ts('Save and New'), | |
758 | 'subName' => 'new', | |
759 | 'js' => $confirmJS, | |
be2fb01f | 760 | ]; |
6a488035 | 761 | } |
239a5027 | 762 | |
be2fb01f | 763 | $buttons[] = [ |
6a488035 TO |
764 | 'type' => 'cancel', |
765 | 'name' => ts('Cancel'), | |
be2fb01f | 766 | ]; |
6a488035 TO |
767 | |
768 | $this->addButtons($buttons); | |
769 | if ($this->_action == CRM_Core_Action::VIEW) { | |
770 | $this->freeze(); | |
771 | } | |
772 | } | |
773 | ||
774 | /** | |
66f9e52b | 775 | * Add local and global form rules. |
6a488035 | 776 | * |
6a488035 TO |
777 | * @return void |
778 | */ | |
00be9182 | 779 | public function addRules() { |
be2fb01f | 780 | $this->addFormRule(['CRM_Event_Form_Participant', 'formRule'], $this); |
6a488035 TO |
781 | } |
782 | ||
783 | /** | |
66f9e52b | 784 | * Global validation rules for the form. |
6a488035 | 785 | * |
d4dd1e85 TO |
786 | * @param array $values |
787 | * Posted values of the form. | |
2a6da8d7 EM |
788 | * @param $files |
789 | * @param $self | |
790 | * | |
a6c01b45 CW |
791 | * @return array |
792 | * list of errors to be posted back to the form | |
6a488035 | 793 | */ |
00be9182 | 794 | public static function formRule($values, $files, $self) { |
6a488035 TO |
795 | // If $values['_qf_Participant_next'] is Delete or |
796 | // $values['event_id'] is empty, then return | |
797 | // instead of proceeding further. | |
798 | ||
23498713 | 799 | if ((($values['_qf_Participant_next'] ?? NULL) === 'Delete') || |
800 | empty($values['event_id']) | |
6a488035 TO |
801 | ) { |
802 | return TRUE; | |
803 | } | |
804 | ||
be2fb01f | 805 | $errorMsg = []; |
6a488035 | 806 | |
a7488080 | 807 | if (!empty($values['payment_processor_id'])) { |
a479fe60 | 808 | // make sure that payment instrument values (e.g. credit card number and cvv) are valid |
f48e6cf7 | 809 | CRM_Core_Payment_Form::validatePaymentInstrument($values['payment_processor_id'], $values, $errorMsg, NULL); |
6a488035 TO |
810 | } |
811 | ||
a7488080 CW |
812 | if (!empty($values['record_contribution'])) { |
813 | if (empty($values['financial_type_id'])) { | |
d96cf288 DG |
814 | $errorMsg['financial_type_id'] = ts('Please enter the associated Financial Type'); |
815 | } | |
a7488080 | 816 | if (empty($values['payment_instrument_id'])) { |
4db803dd | 817 | $errorMsg['payment_instrument_id'] = ts('Payment Method is a required field.'); |
d96cf288 | 818 | } |
610d2dac JP |
819 | if (!empty($values['priceSetId'])) { |
820 | CRM_Price_BAO_PriceField::priceSetValidation($values['priceSetId'], $values, $errorMsg); | |
821 | } | |
6a488035 TO |
822 | } |
823 | ||
824 | // validate contribution status for 'Failed'. | |
8cc574cf | 825 | if ($self->_onlinePendingContributionId && !empty($values['record_contribution']) && |
6a488035 TO |
826 | (CRM_Utils_Array::value('contribution_status_id', $values) == |
827 | array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
828 | ) | |
829 | ) { | |
830 | $errorMsg['contribution_status_id'] = ts('Please select a valid payment status before updating.'); | |
831 | } | |
832 | ||
833 | // do the amount validations. | |
834 | //skip for update mode since amount is freeze, CRM-6052 | |
8cc574cf | 835 | if ((!$self->_id && empty($values['total_amount']) && |
6a488035 TO |
836 | empty($self->_values['line_items']) |
837 | ) || | |
838 | ($self->_id && !$self->_paymentId && isset($self->_values['line_items']) && is_array($self->_values['line_items'])) | |
839 | ) { | |
840 | if ($priceSetId = CRM_Utils_Array::value('priceSetId', $values)) { | |
9da8dc8c | 841 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $values, $errorMsg, TRUE); |
6a488035 TO |
842 | } |
843 | } | |
4d3f1e42 | 844 | // For single additions - show validation error if the contact has already been registered |
c0558721 | 845 | // for this event. |
22e263ad | 846 | if ($self->_single && ($self->_action & CRM_Core_Action::ADD)) { |
c0558721 | 847 | if ($self->_context == 'standalone') { |
9c1bc317 | 848 | $contactId = $values['contact_id'] ?? NULL; |
c0558721 TS |
849 | } |
850 | else { | |
851 | $contactId = $self->_contactId; | |
852 | } | |
853 | ||
9c1bc317 | 854 | $eventId = $values['event_id'] ?? NULL; |
7a6b2b25 ML |
855 | |
856 | $event = new CRM_Event_DAO_Event(); | |
857 | $event->id = $eventId; | |
858 | $event->find(TRUE); | |
859 | ||
860 | if (!$event->allow_same_participant_emails && !empty($contactId) && !empty($eventId)) { | |
81767ac6 | 861 | $cancelledStatusID = CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Cancelled'); |
a130e045 | 862 | $dupeCheck = new CRM_Event_BAO_Participant(); |
4d3f1e42 JM |
863 | $dupeCheck->contact_id = $contactId; |
864 | $dupeCheck->event_id = $eventId; | |
81767ac6 | 865 | $dupeCheck->whereAdd("status_id != {$cancelledStatusID} "); |
4d3f1e42 | 866 | $dupeCheck->find(TRUE); |
22e263ad | 867 | if (!empty($dupeCheck->id)) { |
4d3f1e42 JM |
868 | $errorMsg['event_id'] = ts("This contact has already been assigned to this event."); |
869 | } | |
870 | } | |
871 | } | |
6a488035 TO |
872 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; |
873 | } | |
874 | ||
875 | /** | |
66f9e52b | 876 | * Process the form submission. |
6a488035 TO |
877 | */ |
878 | public function postProcess() { | |
879 | // get the submitted form values. | |
880 | $params = $this->controller->exportValues($this->_name); | |
881 | ||
882 | if ($this->_action & CRM_Core_Action::DELETE) { | |
883 | if (CRM_Utils_Array::value('delete_participant', $params) == 2) { | |
884 | $additionalId = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id)); | |
885 | $participantLinks = (CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId)); | |
886 | } | |
887 | if (CRM_Utils_Array::value('delete_participant', $params) == 1) { | |
888 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
889 | foreach ($additionalIds as $value) { | |
890 | CRM_Event_BAO_Participant::deleteParticipant($value); | |
891 | } | |
892 | } | |
893 | CRM_Event_BAO_Participant::deleteParticipant($this->_id); | |
894 | CRM_Core_Session::setStatus(ts('Selected participant was deleted successfully.'), ts('Record Deleted'), 'success'); | |
895 | if (!empty($participantLinks)) { | |
d3e86119 | 896 | $status = ts('The following participants no longer have an event fee recorded. You can edit their registration and record a replacement contribution by clicking the links below:') . '<br/>' . $participantLinks; |
6a488035 TO |
897 | CRM_Core_Session::setStatus($status, ts('Group Payment Deleted')); |
898 | } | |
899 | return; | |
900 | } | |
4d3f1e42 JM |
901 | // When adding a single contact, the formRule prevents you from adding duplicates |
902 | // (See above in formRule()). When adding more than one contact, the duplicates are | |
903 | // removed automatically and the user receives one notification. | |
904 | if ($this->_action & CRM_Core_Action::ADD) { | |
aae2f9e6 | 905 | $event_id = $this->_eventId; |
22e263ad | 906 | if (empty($event_id) && !empty($params['event_id'])) { |
aae2f9e6 JM |
907 | $event_id = $params['event_id']; |
908 | } | |
22e263ad | 909 | if (!$this->_single && !empty($event_id)) { |
4d3f1e42 | 910 | $duplicateContacts = 0; |
5cda87ee | 911 | foreach ($this->_contactIds as $k => $dupeCheckContactId) { |
4d3f1e42 | 912 | // Eliminate contacts that have already been assigned to this event. |
a130e045 | 913 | $dupeCheck = new CRM_Event_BAO_Participant(); |
4d3f1e42 | 914 | $dupeCheck->contact_id = $dupeCheckContactId; |
aae2f9e6 | 915 | $dupeCheck->event_id = $event_id; |
4d3f1e42 | 916 | $dupeCheck->find(TRUE); |
22e263ad | 917 | if (!empty($dupeCheck->id)) { |
4d3f1e42 JM |
918 | $duplicateContacts++; |
919 | unset($this->_contactIds[$k]); | |
920 | } | |
921 | } | |
22e263ad | 922 | if ($duplicateContacts > 0) { |
4d3f1e42 JM |
923 | $msg = ts( |
924 | "%1 contacts have already been assigned to this event. They were not added a second time.", | |
be2fb01f | 925 | [1 => $duplicateContacts] |
4d3f1e42 JM |
926 | ); |
927 | CRM_Core_Session::setStatus($msg); | |
928 | } | |
22e263ad | 929 | if (count($this->_contactIds) == 0) { |
4d3f1e42 JM |
930 | CRM_Core_Session::setStatus(ts("No participants were added.")); |
931 | return; | |
932 | } | |
aef8149f JM |
933 | // We have to re-key $this->_contactIds so each contact has the same |
934 | // key as their corresponding record in the $participants array that | |
935 | // will be created below. | |
936 | $this->_contactIds = array_values($this->_contactIds); | |
4d3f1e42 JM |
937 | } |
938 | } | |
939 | ||
5fda4b16 | 940 | $statusMsg = $this->submit($params); |
941 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
942 | $session = CRM_Core_Session::singleton(); | |
943 | $buttonName = $this->controller->getButtonName(); | |
944 | if ($this->_context == 'standalone') { | |
945 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
946 | $urlParams = 'reset=1&action=add&context=standalone'; | |
947 | if ($this->_mode) { | |
948 | $urlParams .= '&mode=' . $this->_mode; | |
949 | } | |
950 | if ($this->_eID) { | |
951 | $urlParams .= '&eid=' . $this->_eID; | |
952 | } | |
953 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); | |
954 | } | |
955 | else { | |
956 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
957 | "reset=1&cid={$this->_contactId}&selectedChild=participant" | |
958 | )); | |
959 | } | |
960 | } | |
961 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
962 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', | |
963 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" | |
964 | )); | |
965 | } | |
966 | } | |
967 | ||
968 | /** | |
969 | * Submit form. | |
970 | * | |
971 | * @param array $params | |
972 | * | |
6ba1830e MWMC |
973 | * @return string |
974 | * @throws \CRM_Core_Exception | |
975 | * @throws \CiviCRM_API3_Exception | |
5fda4b16 | 976 | */ |
977 | public function submit($params) { | |
8b652293 | 978 | if ($this->_mode && !$this->_isPaidEvent) { |
979 | CRM_Core_Error::statusBounce(ts('Selected Event is not Paid Event ')); | |
980 | } | |
aac57c29 | 981 | $participantStatus = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 982 | // set the contact, when contact is selected |
479696ed | 983 | if (!empty($params['contact_id'])) { |
0dc4ef42 | 984 | $this->_contactID = $this->_contactId = $params['contact_id']; |
6a488035 | 985 | } |
89b6b472 | 986 | if (!$this->_priceSetId && $this->_isPaidEvent) { |
b28a34c3 | 987 | CRM_Core_Error::deprecatedFunctionWarning('this should never be true, handling to be removed'); |
988 | } | |
9da8dc8c | 989 | if ($this->_priceSetId && $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 TO |
990 | $this->_quickConfig = $isQuickConfig; |
991 | } | |
992 | ||
993 | if ($this->_id) { | |
994 | $params['id'] = $this->_id; | |
995 | } | |
996 | ||
997 | $config = CRM_Core_Config::singleton(); | |
1675ba6d PN |
998 | if (isset($params['total_amount'])) { |
999 | $params['total_amount'] = CRM_Utils_Rule::cleanMoney($params['total_amount']); | |
1000 | } | |
6a488035 | 1001 | if ($this->_isPaidEvent) { |
f2525f80 | 1002 | list($contributionParams, $lineItem, $additionalParticipantDetails, $params) = $this->preparePaidEventProcessing($params); |
6a488035 TO |
1003 | } |
1004 | ||
1005 | $this->_params = $params; | |
5d3a2b9f | 1006 | parent::beginPostProcess(); |
fbc54416 PJ |
1007 | $amountOwed = NULL; |
1008 | if (isset($params['amount'])) { | |
1009 | $amountOwed = $params['amount']; | |
1010 | unset($params['amount']); | |
1011 | } | |
6a488035 TO |
1012 | $params['contact_id'] = $this->_contactId; |
1013 | ||
1014 | // overwrite actual payment amount if entered | |
a7488080 | 1015 | if (!empty($params['total_amount'])) { |
9c1bc317 | 1016 | $contributionParams['total_amount'] = $params['total_amount'] ?? NULL; |
6a488035 TO |
1017 | } |
1018 | ||
6a488035 | 1019 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email |
46929486 | 1020 | $userName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName(); |
6a488035 TO |
1021 | |
1022 | if ($this->_contactId) { | |
1023 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); | |
1024 | } | |
1025 | ||
7df021e5 | 1026 | //modify params according to parameter used in create |
1027 | //participant method (addParticipant) | |
1028 | $this->_params['participant_status_id'] = $params['status_id']; | |
1029 | $this->_params['participant_role_id'] = is_array($params['role_id']) ? $params['role_id'] : explode(',', $params['role_id']); | |
7df021e5 | 1030 | $roleIdWithSeparator = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['participant_role_id']); |
1031 | ||
6ba1830e MWMC |
1032 | $now = date('YmdHis'); |
1033 | ||
6a488035 | 1034 | if ($this->_mode) { |
6a488035 TO |
1035 | // set source if not set |
1036 | if (empty($params['source'])) { | |
be2fb01f | 1037 | $this->_params['participant_source'] = ts('Offline Registration for Event: %2 by: %1', [ |
90b461f1 | 1038 | 1 => $userName, |
ef45ad8c | 1039 | 2 => $this->getEventValue('title'), |
90b461f1 | 1040 | ]); |
6a488035 TO |
1041 | } |
1042 | else { | |
03e04002 | 1043 | $this->_params['participant_source'] = $params['source']; |
6a488035 TO |
1044 | } |
1045 | $this->_params['description'] = $this->_params['participant_source']; | |
1046 | ||
1047 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], | |
1048 | $this->_mode | |
1049 | ); | |
be2fb01f | 1050 | $fields = []; |
6a488035 TO |
1051 | |
1052 | // set email for primary location. | |
1053 | $fields['email-Primary'] = 1; | |
1054 | $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail; | |
1055 | ||
6a488035 TO |
1056 | // now set the values for the billing location. |
1057 | foreach ($this->_fields as $name => $dontCare) { | |
1058 | $fields[$name] = 1; | |
1059 | } | |
1060 | ||
1061 | // also add location name to the array | |
a130e045 DG |
1062 | $params["address_name-{$this->_bltID}"] |
1063 | = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . | |
6a488035 TO |
1064 | CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . |
1065 | CRM_Utils_Array::value('billing_last_name', $params); | |
1066 | ||
1067 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); | |
1068 | $fields["address_name-{$this->_bltID}"] = 1; | |
1069 | $fields["email-{$this->_bltID}"] = 1; | |
1070 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); | |
1071 | ||
be2fb01f | 1072 | $nameFields = ['first_name', 'middle_name', 'last_name']; |
6a488035 TO |
1073 | |
1074 | foreach ($nameFields as $name) { | |
1075 | $fields[$name] = 1; | |
1076 | if (array_key_exists("billing_$name", $params)) { | |
1077 | $params[$name] = $params["billing_{$name}"]; | |
1078 | $params['preserveDBName'] = TRUE; | |
1079 | } | |
1080 | } | |
1081 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); | |
1082 | } | |
1083 | ||
e03317f1 | 1084 | if (!empty($this->_params['participant_role_id'])) { |
be2fb01f | 1085 | $customFieldsRole = []; |
7df021e5 | 1086 | foreach ($this->_params['participant_role_id'] as $roleKey) { |
1087 | $customFieldsRole = CRM_Utils_Array::crmArrayMerge(CRM_Core_BAO_CustomField::getFields('Participant', | |
1088 | FALSE, FALSE, $roleKey, $this->_roleCustomDataTypeID), $customFieldsRole); | |
6a488035 | 1089 | } |
7df021e5 | 1090 | $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', |
1091 | FALSE, | |
1092 | FALSE, | |
1093 | CRM_Utils_Array::value('event_id', $params), | |
1094 | $this->_eventNameCustomDataTypeID | |
1095 | ); | |
1096 | $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', | |
1097 | FALSE, | |
1098 | FALSE, | |
1099 | $this->_eventTypeId, | |
1100 | $this->_eventTypeCustomDataTypeID | |
1101 | ); | |
1102 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, | |
1103 | CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE) | |
1104 | ); | |
1105 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields); | |
1106 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields); | |
6452294d MW |
1107 | |
1108 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, $this->getDefaultEntity()); | |
6a488035 TO |
1109 | } |
1110 | ||
1111 | //do cleanup line items if participant edit the Event Fee. | |
0479b4c8 | 1112 | if (($this->_lineItem || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) { |
6a488035 TO |
1113 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant'); |
1114 | } | |
03e04002 | 1115 | |
6a488035 | 1116 | if ($this->_mode) { |
dc8cdf3a | 1117 | // add all the additional payment params we need |
b1b7f4e3 | 1118 | $this->_params = $this->prepareParamsForPaymentProcessor($this->_params); |
6a488035 TO |
1119 | $this->_params['amount'] = $params['fee_amount']; |
1120 | $this->_params['amount_level'] = $params['amount_level']; | |
1121 | $this->_params['currencyID'] = $config->defaultCurrency; | |
6a488035 TO |
1122 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); |
1123 | ||
1124 | // at this point we've created a contact and stored its address etc | |
1125 | // all the payment processors expect the name and address to be in the | |
1126 | // so we copy stuff over to first_name etc. | |
1127 | $paymentParams = $this->_params; | |
a7488080 | 1128 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1129 | $paymentParams['email'] = $this->_contributorEmail; |
1130 | } | |
e7309307 EM |
1131 | |
1132 | // The only reason for merging in the 'contact_id' rather than ensuring it is set | |
1133 | // is that this patch is being done around the time of the stable release | |
1134 | // so more conservative approach is called for. | |
1135 | // In fact the use of $params and $this->_params & $this->_contactId vs $contactID | |
1136 | // needs rationalising. | |
be2fb01f | 1137 | $mapParams = array_merge(['contact_id' => $contactID], $this->_params); |
e7309307 | 1138 | CRM_Core_Payment_Form::mapParams($this->_bltID, $mapParams, $paymentParams, TRUE); |
6a488035 | 1139 | |
077017db | 1140 | $payment = $this->_paymentProcessor['object']; |
6a488035 | 1141 | |
9b7e7d4c PH |
1142 | // CRM-15622: fix for incorrect contribution.fee_amount |
1143 | $paymentParams['fee_amount'] = NULL; | |
46366f16 SL |
1144 | try { |
1145 | $result = $payment->doPayment($paymentParams); | |
1146 | } | |
1147 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { | |
1148 | // @todo un comment the following line out when we are creating a contribution before we get to this point | |
1149 | // see dev/financial#53 about ensuring we create a pending contribution before we try processing payment | |
1150 | // CRM_Contribute_BAO_Contribution::failPayment($contributionID); | |
1151 | CRM_Core_Session::singleton()->setStatus($e->getMessage()); | |
6a488035 | 1152 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/participant', |
353ffa53 TO |
1153 | "reset=1&action=add&cid={$this->_contactId}&context=participant&mode={$this->_mode}" |
1154 | )); | |
6a488035 TO |
1155 | } |
1156 | ||
1157 | if ($result) { | |
1158 | $this->_params = array_merge($this->_params, $result); | |
1159 | } | |
1160 | ||
1161 | $this->_params['receive_date'] = $now; | |
1162 | ||
a7488080 | 1163 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1164 | $this->_params['receipt_date'] = $now; |
1165 | } | |
1166 | else { | |
1167 | $this->_params['receipt_date'] = NULL; | |
1168 | } | |
1169 | ||
1170 | $this->set('params', $this->_params); | |
1171 | $this->assign('trxn_id', $result['trxn_id']); | |
6ba1830e | 1172 | $this->assign('receive_date', $this->_params['receive_date']); |
6a488035 TO |
1173 | |
1174 | //add contribution record | |
a130e045 DG |
1175 | $this->_params['financial_type_id'] |
1176 | = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'financial_type_id'); | |
6a488035 TO |
1177 | $this->_params['mode'] = $this->_mode; |
1178 | ||
431c430b | 1179 | //add contribution record |
44237a29 | 1180 | $contributions[] = $contribution = $this->processContribution( |
faf44167 | 1181 | $this, $this->_params, |
1182 | $result, $contactID, | |
7a0f6c65 | 1183 | FALSE, |
faf44167 | 1184 | $this->_paymentProcessor |
1185 | ); | |
6a488035 TO |
1186 | |
1187 | // add participant record | |
be2fb01f | 1188 | $participants = []; |
dc8cdf3a K |
1189 | if (!empty($this->_params['role_id']) && is_array($this->_params['role_id'])) { |
1190 | $this->_params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1191 | $this->_params['role_id'] | |
6a488035 TO |
1192 | ); |
1193 | } | |
0e81467c | 1194 | |
5c320924 | 1195 | //CRM-15372 patch to fix fee amount replacing amount |
0479b4c8 | 1196 | $this->_params['fee_amount'] = $this->_params['amount']; |
5c320924 | 1197 | |
35f86af2 | 1198 | $participants[] = $this->addParticipant($this, $this->_params, $contactID); |
6a488035 TO |
1199 | |
1200 | //add custom data for participant | |
1201 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, | |
6a488035 TO |
1202 | 'civicrm_participant', |
1203 | $participants[0]->id, | |
1204 | 'Participant' | |
1205 | ); | |
f55a8ac8 MWMC |
1206 | |
1207 | // Add participant payment | |
1208 | $participantPaymentParams = [ | |
6a488035 TO |
1209 | 'participant_id' => $participants[0]->id, |
1210 | 'contribution_id' => $contribution->id, | |
be2fb01f | 1211 | ]; |
f55a8ac8 | 1212 | civicrm_api3('ParticipantPayment', 'create', $participantPaymentParams); |
6a488035 | 1213 | |
6a488035 TO |
1214 | $this->_contactIds[] = $this->_contactId; |
1215 | } | |
1216 | else { | |
6a488035 TO |
1217 | if ($this->_single) { |
1218 | $this->_contactIds[] = $this->_contactId; | |
1219 | } | |
d6921a22 | 1220 | $participants = []; |
1221 | foreach ($this->_contactIds as $contactID) { | |
1222 | $commonParams = $params; | |
1223 | $commonParams['contact_id'] = $contactID; | |
1224 | $participants[] = CRM_Event_BAO_Participant::create($commonParams); | |
1225 | } | |
6a488035 | 1226 | |
be2fb01f | 1227 | $contributions = []; |
a7488080 CW |
1228 | if (!empty($params['record_contribution'])) { |
1229 | if (!empty($params['id'])) { | |
6a488035 | 1230 | if ($this->_onlinePendingContributionId) { |
70d43afb | 1231 | $contributionParams['id'] = $this->_onlinePendingContributionId; |
6a488035 TO |
1232 | } |
1233 | else { | |
70d43afb | 1234 | $contributionParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
6a488035 TO |
1235 | $params['id'], |
1236 | 'contribution_id', | |
1237 | 'participant_id' | |
1238 | ); | |
1239 | } | |
1240 | } | |
1241 | unset($params['note']); | |
1242 | ||
1243 | //build contribution params | |
1244 | if (!$this->_onlinePendingContributionId) { | |
0e81467c | 1245 | if (empty($params['source'])) { |
be2fb01f | 1246 | $contributionParams['source'] = ts('%1 : Offline registration (by %2)', [ |
ef45ad8c | 1247 | 1 => $this->getEventValue('title'), |
90b461f1 SL |
1248 | 2 => $userName, |
1249 | ]); | |
0e81467c EM |
1250 | } |
1251 | else { | |
1252 | $contributionParams['source'] = $params['source']; | |
1253 | } | |
1254 | } | |
6a488035 TO |
1255 | |
1256 | $contributionParams['currency'] = $config->defaultCurrency; | |
1257 | $contributionParams['non_deductible_amount'] = 'null'; | |
0d8afee2 | 1258 | $contributionParams['receipt_date'] = !empty($params['send_receipt']) ? CRM_Utils_Array::value('receive_date', $params) : 'null'; |
0dc4ef42 | 1259 | $contributionParams['contact_id'] = $this->_contactID; |
3e223759 | 1260 | $contributionParams['receive_date'] = $params['receive_date'] ?? date('Y-m-d'); |
6a488035 | 1261 | |
be2fb01f | 1262 | $recordContribution = [ |
353ffa53 TO |
1263 | 'financial_type_id', |
1264 | 'payment_instrument_id', | |
1265 | 'trxn_id', | |
1266 | 'contribution_status_id', | |
353ffa53 TO |
1267 | 'check_number', |
1268 | 'campaign_id', | |
0dc4ef42 | 1269 | 'pan_truncation', |
5d3a2b9f | 1270 | 'card_type_id', |
be2fb01f | 1271 | ]; |
6a488035 TO |
1272 | |
1273 | foreach ($recordContribution as $f) { | |
9c1bc317 | 1274 | $contributionParams[$f] = $this->_params[$f] ?? NULL; |
6a488035 TO |
1275 | if ($f == 'trxn_id') { |
1276 | $this->assign('trxn_id', $contributionParams[$f]); | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | //insert financial type name in receipt. | |
b956b310 | 1281 | $this->assign('financialTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', |
1282 | $contributionParams['financial_type_id'])); | |
1283 | // legacy support | |
6a488035 TO |
1284 | $this->assign('contributionTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id'])); |
1285 | $contributionParams['skipLineItem'] = 1; | |
7030a002 PN |
1286 | if ($this->_id) { |
1287 | $contributionParams['contribution_mode'] = 'participant'; | |
1288 | $contributionParams['participant_id'] = $this->_id; | |
1289 | } | |
6a488035 | 1290 | // Set is_pay_later flag for back-office offline Pending status contributions |
2f48e713 | 1291 | if ($contributionParams['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Pending')) { |
6a488035 TO |
1292 | $contributionParams['is_pay_later'] = 1; |
1293 | } | |
2f48e713 | 1294 | elseif ($contributionParams['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Completed')) { |
d75f2f47 EM |
1295 | $contributionParams['is_pay_later'] = 0; |
1296 | } | |
f8325309 | 1297 | |
aac57c29 | 1298 | if ($params['status_id'] == array_search('Partially paid', $participantStatus)) { |
5e494d5c PJ |
1299 | if (!$amountOwed && $this->_action & CRM_Core_Action::UPDATE) { |
1300 | $amountOwed = $params['fee_amount']; | |
1301 | } | |
1302 | ||
356579e9 | 1303 | // if multiple participants are link, consider contribution total amount as the amount Owed |
ea707c1b | 1304 | if ($this->_id && CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { |
356579e9 | 1305 | $amountOwed = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', |
70d43afb | 1306 | $contributionParams['id'], |
356579e9 PJ |
1307 | 'total_amount' |
1308 | ); | |
1309 | } | |
1310 | ||
2602eee5 | 1311 | // CRM-13964 partial payment |
aac57c29 PJ |
1312 | if ($amountOwed > $params['total_amount']) { |
1313 | // the owed amount | |
d3e6e9a4 | 1314 | $contributionParams['total_amount'] = $amountOwed; |
1315 | $contributionParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); | |
1316 | $this->assign('balanceAmount', $amountOwed - $params['total_amount']); | |
1317 | $this->storePaymentCreateParams($params); | |
aac57c29 | 1318 | } |
f8325309 | 1319 | } |
372ad74d | 1320 | |
f3acfdd9 | 1321 | if (!empty($this->_params['tax_amount'])) { |
d91b8b33 | 1322 | $contributionParams['tax_amount'] = $this->_params['tax_amount']; |
1323 | } | |
1324 | ||
6a488035 | 1325 | if ($this->_single) { |
70d43afb | 1326 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams); |
6a488035 TO |
1327 | } |
1328 | else { | |
6a488035 TO |
1329 | foreach ($this->_contactIds as $contactID) { |
1330 | $contributionParams['contact_id'] = $contactID; | |
70d43afb | 1331 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams); |
6a488035 TO |
1332 | } |
1333 | } | |
1334 | ||
f55a8ac8 | 1335 | // Insert payment record for this participant |
70d43afb | 1336 | if (empty($contributionParams['id'])) { |
6a488035 | 1337 | foreach ($this->_contactIds as $num => $contactID) { |
f55a8ac8 MWMC |
1338 | $participantPaymentParams = [ |
1339 | 'participant_id' => $participants[$num]->id, | |
1340 | 'contribution_id' => $contributions[$num]->id, | |
1341 | ]; | |
1342 | civicrm_api3('ParticipantPayment', 'create', $participantPaymentParams); | |
6a488035 TO |
1343 | } |
1344 | } | |
6a488035 TO |
1345 | |
1346 | // CRM-11124 | |
362bd1b7 | 1347 | if ($this->_params['discount_id']) { |
1348 | CRM_Event_BAO_Participant::createDiscountTrxn( | |
1349 | $this->_eventId, | |
1350 | $contributionParams, | |
1351 | NULL, | |
1352 | CRM_Price_BAO_PriceSet::parseFirstPriceSetValueIDFromParams($this->_params) | |
1353 | ); | |
6a488035 | 1354 | } |
6a488035 TO |
1355 | } |
1356 | } | |
1357 | ||
1358 | // also store lineitem stuff here | |
e10fac94 | 1359 | if ((($this->_lineItem && $this->_action & CRM_Core_Action::ADD) || |
6a488035 TO |
1360 | ($this->_lineItem && CRM_Core_Action::UPDATE && !$this->_paymentId)) |
1361 | ) { | |
1362 | foreach ($this->_contactIds as $num => $contactID) { | |
1363 | foreach ($this->_lineItem as $key => $value) { | |
1364 | if (is_array($value) && $value != 'skip') { | |
1365 | foreach ($value as $lineKey => $line) { | |
aac57c29 | 1366 | //10117 update the line items for participants if contribution amount is recorded |
f6bae84f | 1367 | if ($this->_quickConfig && !empty($params['total_amount']) && |
372ad74d | 1368 | ($params['status_id'] != array_search('Partially paid', $participantStatus)) |
aac57c29 PJ |
1369 | ) { |
1370 | $line['unit_price'] = $line['line_total'] = $params['total_amount']; | |
d91b8b33 | 1371 | if (!empty($params['tax_amount'])) { |
1372 | $line['unit_price'] = $line['unit_price'] - $params['tax_amount']; | |
ff9b6b50 | 1373 | $line['line_total'] = $line['line_total'] - $params['tax_amount']; |
d91b8b33 | 1374 | } |
aac57c29 | 1375 | } |
6a488035 TO |
1376 | $lineItem[$this->_priceSetId][$lineKey] = $line; |
1377 | } | |
1378 | CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); | |
1379 | } | |
1380 | } | |
1381 | } | |
0623f992 | 1382 | foreach ($contributions as $contribution) { |
d3e6e9a4 | 1383 | if (!empty($this->getCreatePaymentParams())) { |
1384 | civicrm_api3('Payment', 'create', array_merge(['contribution_id' => $contribution->id], $this->getCreatePaymentParams())); | |
0623f992 | 1385 | } |
1386 | } | |
6a488035 TO |
1387 | } |
1388 | ||
1389 | $updateStatusMsg = NULL; | |
1390 | //send mail when participant status changed, CRM-4326 | |
1391 | if ($this->_id && $this->_statusId && | |
353ffa53 TO |
1392 | $this->_statusId != CRM_Utils_Array::value('status_id', $params) && !empty($params['is_notify']) |
1393 | ) { | |
6a488035 TO |
1394 | |
1395 | $updateStatusMsg = CRM_Event_BAO_Participant::updateStatusMessage($this->_id, | |
1396 | $params['status_id'], | |
1397 | $this->_statusId | |
1398 | ); | |
1399 | } | |
1400 | ||
be2fb01f CW |
1401 | $sent = []; |
1402 | $notSent = []; | |
a7488080 | 1403 | if (!empty($params['send_receipt'])) { |
6a488035 TO |
1404 | |
1405 | $this->assign('module', 'Event Registration'); | |
e0dbddb4 | 1406 | $this->assignEventDetailsToTpl($params['event_id'], CRM_Utils_Array::value('role_id', $params), CRM_Utils_Array::value('receipt_text', $params), $this->_isPaidEvent); |
6a488035 | 1407 | $this->assign('isAmountzero', 1); |
6a488035 | 1408 | |
6a488035 TO |
1409 | if ($this->_isPaidEvent) { |
1410 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1411 | if (!$this->_mode) { | |
1412 | if (isset($params['payment_instrument_id'])) { | |
1413 | $this->assign('paidBy', | |
1414 | CRM_Utils_Array::value($params['payment_instrument_id'], | |
1415 | $paymentInstrument | |
1416 | ) | |
1417 | ); | |
1418 | } | |
1419 | } | |
1420 | ||
d3e6e9a4 | 1421 | $this->assign('totalAmount', $params['total_amount'] ?? $contributionParams['total_amount']); |
6a488035 TO |
1422 | $this->assign('isPrimary', 1); |
1423 | $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); | |
1424 | } | |
1425 | if ($this->_mode) { | |
0b50eca0 | 1426 | $this->assignBillingName($params); |
1427 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( | |
1428 | $this->_params, | |
1429 | $this->_bltID | |
1430 | )); | |
6a488035 | 1431 | |
2e09448c MW |
1432 | $valuesForForm = CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($params); |
1433 | $this->assignVariables($valuesForForm, ['credit_card_exp_date', 'credit_card_type', 'credit_card_number']); | |
1434 | ||
0f2b049e | 1435 | // The concept of contributeMode is deprecated. |
6a488035 TO |
1436 | $this->assign('contributeMode', 'direct'); |
1437 | $this->assign('isAmountzero', 0); | |
1438 | $this->assign('is_pay_later', 0); | |
1439 | $this->assign('isPrimary', 1); | |
1440 | } | |
1441 | ||
1442 | $this->assign('register_date', $params['register_date']); | |
6ba1830e | 1443 | if (isset($params['receive_date'])) { |
6a488035 TO |
1444 | $this->assign('receive_date', $params['receive_date']); |
1445 | } | |
1446 | ||
be2fb01f | 1447 | $customGroup = []; |
6a488035 TO |
1448 | //format submitted data |
1449 | foreach ($params['custom'] as $fieldID => $values) { | |
1450 | foreach ($values as $fieldValue) { | |
af6de2f1 JP |
1451 | $isPublic = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $fieldValue['custom_group_id'], 'is_public'); |
1452 | if ($isPublic) { | |
1453 | $customFields[$fieldID]['id'] = $fieldID; | |
1454 | $formattedValue = CRM_Core_BAO_CustomField::displayValue($fieldValue['value'], $fieldID, $participants[0]->id); | |
1455 | $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); | |
1456 | } | |
6a488035 TO |
1457 | } |
1458 | } | |
1459 | ||
1460 | foreach ($this->_contactIds as $num => $contactID) { | |
1461 | // Retrieve the name and email of the contact - this will be the TO for receipt email | |
1462 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
1463 | ||
1464 | $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; | |
1465 | ||
1466 | $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
1467 | if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { | |
1468 | $this->assign('isOnWaitlist', TRUE); | |
1469 | } | |
1470 | ||
1471 | $this->assign('customGroup', $customGroup); | |
1472 | $this->assign('contactID', $contactID); | |
1473 | $this->assign('participantID', $participants[$num]->id); | |
1474 | ||
1475 | $this->_id = $participants[$num]->id; | |
1476 | ||
1477 | if ($this->_isPaidEvent) { | |
1478 | // fix amount for each of participants ( for bulk mode ) | |
be2fb01f | 1479 | $eventAmount = []; |
79d001a2 | 1480 | $totalTaxAmount = 0; |
0e81467c | 1481 | |
03b412ae | 1482 | //add dataArray in the receipts in ADD and UPDATE condition |
be2fb01f | 1483 | $dataArray = []; |
79d001a2 | 1484 | if ($this->_action & CRM_Core_Action::ADD) { |
2e1f50d6 | 1485 | $line = $lineItem[0] ?? []; |
79d001a2 PB |
1486 | } |
1487 | elseif ($this->_action & CRM_Core_Action::UPDATE) { | |
1488 | $line = $this->_values['line_items']; | |
1489 | } | |
fe4d1285 | 1490 | if (Civi::settings()->get('invoicing')) { |
03b412ae PB |
1491 | foreach ($line as $key => $value) { |
1492 | if (isset($value['tax_amount'])) { | |
1493 | $totalTaxAmount += $value['tax_amount']; | |
288ec043 PB |
1494 | if (isset($dataArray[(string) $value['tax_rate']])) { |
1495 | $dataArray[(string) $value['tax_rate']] = $dataArray[(string) $value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); | |
03b412ae PB |
1496 | } |
1497 | else { | |
9c1bc317 | 1498 | $dataArray[(string) $value['tax_rate']] = $value['tax_amount'] ?? NULL; |
03b412ae | 1499 | } |
79d001a2 PB |
1500 | } |
1501 | } | |
03b412ae | 1502 | $this->assign('totalTaxAmount', $totalTaxAmount); |
a6e29c95 | 1503 | $this->assign('taxTerm', $this->getSalesTaxTerm()); |
03b412ae | 1504 | $this->assign('dataArray', $dataArray); |
79d001a2 | 1505 | } |
6a488035 TO |
1506 | if (!empty($additionalParticipantDetails)) { |
1507 | $params['amount_level'] = preg_replace('/\ 1/', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; | |
1508 | } | |
1509 | ||
be2fb01f | 1510 | $eventAmount[$num] = [ |
6a488035 TO |
1511 | 'label' => preg_replace('/\ 1/', '', $params['amount_level']), |
1512 | 'amount' => $params['fee_amount'], | |
be2fb01f | 1513 | ]; |
6a488035 TO |
1514 | //as we are using same template for online & offline registration. |
1515 | //So we have to build amount as array. | |
1516 | $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); | |
1517 | $this->assign('amount', $eventAmount); | |
1518 | } | |
1519 | ||
be2fb01f | 1520 | $sendTemplateParams = [ |
6a488035 TO |
1521 | 'groupName' => 'msg_tpl_workflow_event', |
1522 | 'valueName' => 'event_offline_receipt', | |
1523 | 'contactId' => $contactID, | |
52f3b44b | 1524 | 'isTest' => !empty($this->_defaultValues['is_test']), |
92fcb95f | 1525 | 'PDFFilename' => ts('confirmation') . '.pdf', |
be2fb01f | 1526 | ]; |
6a488035 TO |
1527 | |
1528 | // try to send emails only if email id is present | |
1529 | // and the do-not-email option is not checked for that contact | |
1530 | if ($this->_contributorEmail and !$this->_toDoNotEmail) { | |
b28a34c3 | 1531 | if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { |
1532 | $receiptFrom = $params['from_email_address']; | |
1533 | } | |
6a488035 TO |
1534 | $sendTemplateParams['from'] = $receiptFrom; |
1535 | $sendTemplateParams['toName'] = $this->_contributorDisplayName; | |
1536 | $sendTemplateParams['toEmail'] = $this->_contributorEmail; | |
9c1bc317 CW |
1537 | $sendTemplateParams['cc'] = $this->_fromEmails['cc'] ?? NULL; |
1538 | $sendTemplateParams['bcc'] = $this->_fromEmails['bcc'] ?? NULL; | |
6a488035 TO |
1539 | } |
1540 | ||
9161952c | 1541 | //send email with pdf invoice |
481a74f4 | 1542 | $template = CRM_Core_Smarty::singleton(); |
9161952c PD |
1543 | $taxAmt = $template->get_template_vars('dataArray'); |
1544 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
353ffa53 TO |
1545 | $this->_id, 'contribution_id', 'participant_id' |
1546 | ); | |
aaffa79f | 1547 | $prefixValue = Civi::settings()->get('contribution_invoice_settings'); |
9c1bc317 | 1548 | $invoicing = $prefixValue['invoicing'] ?? NULL; |
0fba5cf8 | 1549 | if (Civi::settings()->get('invoice_is_email_pdf')) { |
0479b4c8 | 1550 | $sendTemplateParams['isEmailPdf'] = TRUE; |
0e81467c | 1551 | $sendTemplateParams['contributionId'] = $contributionId; |
9161952c | 1552 | } |
c6327d7d | 1553 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
1554 | if ($mailSent) { |
1555 | $sent[] = $contactID; | |
1556 | foreach ($participants as $ids => $values) { | |
1557 | if ($values->contact_id == $contactID) { | |
9c1bc317 | 1558 | $values->details = $params['receipt_text'] ?? NULL; |
6a488035 TO |
1559 | CRM_Activity_BAO_Activity::addActivity($values, 'Email'); |
1560 | break; | |
1561 | } | |
1562 | } | |
1563 | } | |
1564 | else { | |
1565 | $notSent[] = $contactID; | |
1566 | } | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | // set the participant id if it is not set | |
1571 | if (!$this->_id) { | |
1572 | $this->_id = $participants[0]->id; | |
1573 | } | |
1574 | ||
5fda4b16 | 1575 | return $this->getStatusMsg($params, $sent, $updateStatusMsg, $notSent); |
1576 | } | |
1577 | ||
1578 | /** | |
1579 | * Set the various IDs relating to custom data types. | |
1580 | */ | |
1581 | public function setCustomDataTypes() { | |
1582 | $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name'); | |
1583 | $this->_roleCustomDataTypeID = array_search('ParticipantRole', $customDataType); | |
1584 | $this->_eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType); | |
1585 | $this->_eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType); | |
1586 | $this->assign('roleCustomDataTypeID', $this->_roleCustomDataTypeID); | |
1587 | $this->assign('eventNameCustomDataTypeID', $this->_eventNameCustomDataTypeID); | |
1588 | $this->assign('eventTypeCustomDataTypeID', $this->_eventTypeCustomDataTypeID); | |
1589 | } | |
1590 | ||
1591 | /** | |
1592 | * Get status message | |
1593 | * | |
1594 | * @param array $params | |
1595 | * @param int $sent | |
1596 | * @param string $updateStatusMsg | |
1597 | * @param int $notSent | |
1598 | * | |
1599 | * @return string | |
1600 | */ | |
1601 | protected function getStatusMsg($params, $sent, $updateStatusMsg, $notSent) { | |
1602 | $statusMsg = ''; | |
6a488035 | 1603 | if (($this->_action & CRM_Core_Action::UPDATE)) { |
be2fb01f | 1604 | $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $this->_contributorDisplayName]); |
a7488080 | 1605 | if (!empty($params['send_receipt']) && count($sent)) { |
be2fb01f | 1606 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', [1 => $this->_contributorEmail]); |
6a488035 TO |
1607 | } |
1608 | ||
1609 | if ($updateStatusMsg) { | |
1610 | $statusMsg = "{$statusMsg} {$updateStatusMsg}"; | |
1611 | } | |
1612 | } | |
1613 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
1614 | if ($this->_single) { | |
be2fb01f | 1615 | $statusMsg = ts('Event registration for %1 has been added.', [1 => $this->_contributorDisplayName]); |
a7488080 | 1616 | if (!empty($params['send_receipt']) && count($sent)) { |
be2fb01f | 1617 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', [1 => $this->_contributorEmail]); |
6a488035 TO |
1618 | } |
1619 | } | |
1620 | else { | |
be2fb01f | 1621 | $statusMsg = ts('Total Participant(s) added to event: %1.', [1 => count($this->_contactIds)]); |
6a488035 | 1622 | if (count($notSent) > 0) { |
be2fb01f | 1623 | $statusMsg .= ' ' . ts('Email has NOT been sent to %1 contact(s) - communication preferences specify DO NOT EMAIL OR valid Email is NOT present. ', [1 => count($notSent)]); |
6a488035 TO |
1624 | } |
1625 | elseif (isset($params['send_receipt'])) { | |
1626 | $statusMsg .= ' ' . ts('A confirmation email has been sent to ALL participants'); | |
1627 | } | |
1628 | } | |
1629 | } | |
5fda4b16 | 1630 | return $statusMsg; |
6a488035 | 1631 | } |
96025800 | 1632 | |
775a355c | 1633 | /** |
1634 | * Build the form object. | |
1635 | * | |
eeca0339 | 1636 | * @param \CRM_Event_Form_Participant $form |
775a355c | 1637 | * |
1638 | * @return bool | |
1639 | * @throws \CRM_Core_Exception | |
1640 | * @throws \Exception | |
1641 | */ | |
eeca0339 | 1642 | public function buildEventFeeForm($form) { |
775a355c | 1643 | if ($form->_eventId) { |
1644 | $form->_isPaidEvent = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_eventId, 'is_monetary'); | |
1645 | if ($form->_isPaidEvent) { | |
1646 | $form->addElement('hidden', 'hidden_feeblock', 1); | |
1647 | } | |
1648 | ||
2ee9bfad | 1649 | $eventfullMsg = CRM_Event_BAO_Participant::eventFullMessage($form->_eventId, $this->getParticipantID()); |
eeca0339 | 1650 | $form->addElement('hidden', 'hidden_eventFullMsg', $eventfullMsg, ['id' => 'hidden_eventFullMsg']); |
775a355c | 1651 | } |
1652 | ||
775a355c | 1653 | if ($form->_isPaidEvent) { |
1654 | $params = ['id' => $form->_eventId]; | |
1655 | CRM_Event_BAO_Event::retrieve($params, $event); | |
1656 | ||
1657 | //retrieve custom information | |
1658 | $form->_values = []; | |
985c84bf | 1659 | CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE); |
775a355c | 1660 | CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->_discountId); |
1661 | $lineItem = []; | |
1662 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); | |
9c1bc317 | 1663 | $invoicing = $invoiceSettings['invoicing'] ?? NULL; |
775a355c | 1664 | $totalTaxAmount = 0; |
1665 | if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $form->_values))) { | |
1666 | $lineItem[] = $form->_values['line_items']; | |
1667 | foreach ($form->_values['line_items'] as $key => $value) { | |
1668 | $totalTaxAmount = $value['tax_amount'] + $totalTaxAmount; | |
1669 | } | |
1670 | } | |
1671 | if ($invoicing) { | |
1672 | $form->assign('totalTaxAmount', $totalTaxAmount); | |
1673 | } | |
1674 | $form->assign('lineItem', empty($lineItem) ? FALSE : $lineItem); | |
1675 | $discounts = []; | |
1676 | if (!empty($form->_values['discount'])) { | |
1677 | foreach ($form->_values['discount'] as $key => $value) { | |
1678 | $value = current($value); | |
1679 | $discounts[$key] = $value['name']; | |
1680 | } | |
1681 | ||
1682 | $element = $form->add('select', 'discount_id', | |
1683 | ts('Discount Set'), | |
1684 | [ | |
1685 | 0 => ts('- select -'), | |
1686 | ] + $discounts, | |
1687 | FALSE, | |
1688 | ['class' => "crm-select2"] | |
1689 | ); | |
775a355c | 1690 | } |
1691 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() | |
b99f3e96 | 1692 | && empty($form->_values['fee']) |
775a355c | 1693 | && CRM_Utils_Array::value('snippet', $_REQUEST) == CRM_Core_Smarty::PRINT_NOFORM |
1694 | ) { | |
1695 | CRM_Core_Session::setStatus(ts('You do not have all the permissions needed for this page.'), 'Permission Denied', 'error'); | |
1696 | return FALSE; | |
1697 | } | |
1698 | ||
1699 | CRM_Core_Payment_Form::buildPaymentForm($form, $form->_paymentProcessor, FALSE, TRUE, self::getDefaultPaymentInstrumentId()); | |
1700 | if (!$form->_mode) { | |
1701 | $form->addElement('checkbox', 'record_contribution', ts('Record Payment?'), NULL, | |
1702 | ['onclick' => "return showHideByValue('record_contribution','','payment_information','table-row','radio',false);"] | |
1703 | ); | |
1704 | // Check permissions for financial type first | |
1705 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { | |
1706 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $form->_action); | |
1707 | } | |
1708 | else { | |
1709 | $financialTypes = CRM_Contribute_PseudoConstant::financialType(); | |
1710 | } | |
1711 | ||
1712 | $form->add('select', 'financial_type_id', | |
1713 | ts('Financial Type'), | |
1714 | ['' => ts('- select -')] + $financialTypes | |
1715 | ); | |
1716 | ||
1717 | $form->add('datepicker', 'receive_date', ts('Received'), [], FALSE, ['time' => TRUE]); | |
1718 | ||
1719 | $form->add('select', 'payment_instrument_id', | |
1720 | ts('Payment Method'), | |
1721 | ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(), | |
1722 | FALSE, ['onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);"] | |
1723 | ); | |
1724 | // don't show transaction id in batch update mode | |
1725 | $path = CRM_Utils_System::currentPath(); | |
1726 | $form->assign('showTransactionId', FALSE); | |
1727 | if ($path != 'civicrm/contact/search/basic') { | |
1728 | $form->add('text', 'trxn_id', ts('Transaction ID')); | |
1729 | $form->addRule('trxn_id', ts('Transaction ID already exists in Database.'), | |
1730 | 'objectExists', ['CRM_Contribute_DAO_Contribution', $form->_eventId, 'trxn_id'] | |
1731 | ); | |
1732 | $form->assign('showTransactionId', TRUE); | |
1733 | } | |
1734 | ||
1735 | $form->add('select', 'contribution_status_id', | |
1736 | ts('Payment Status'), CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('participant') | |
1737 | ); | |
1738 | ||
1739 | $form->add('text', 'check_number', ts('Check Number'), | |
1740 | CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number') | |
1741 | ); | |
1742 | ||
1743 | $form->add('text', 'total_amount', ts('Amount'), | |
1744 | CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'total_amount') | |
1745 | ); | |
1746 | } | |
1747 | } | |
1748 | else { | |
1749 | $form->add('text', 'amount', ts('Event Fee(s)')); | |
1750 | } | |
1751 | $form->assign('onlinePendingContributionId', $form->get('onlinePendingContributionId')); | |
1752 | ||
1753 | $form->assign('paid', $form->_isPaidEvent); | |
1754 | ||
1755 | $form->addElement('checkbox', | |
1756 | 'send_receipt', | |
1757 | ts('Send Confirmation?'), NULL, | |
1758 | ['onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);"] | |
1759 | ); | |
1760 | ||
1761 | $form->add('select', 'from_email_address', ts('Receipt From'), $form->_fromEmails['from_email_id']); | |
1762 | ||
1763 | $form->add('textarea', 'receipt_text', ts('Confirmation Message')); | |
1764 | ||
1765 | // Retrieve the name and email of the contact - form will be the TO for receipt email ( only if context is not standalone) | |
1766 | if ($form->_context != 'standalone') { | |
1767 | if ($form->_contactId) { | |
1768 | list($form->_contributorDisplayName, | |
1769 | $form->_contributorEmail | |
1770 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($form->_contactId); | |
1771 | $form->assign('email', $form->_contributorEmail); | |
1772 | } | |
1773 | else { | |
1774 | //show email block for batch update for event | |
1775 | $form->assign('batchEmail', TRUE); | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | $mailingInfo = Civi::settings()->get('mailing_backend'); | |
1780 | $form->assign('outBound_option', $mailingInfo['outBound_option']); | |
1781 | $form->assign('hasPayment', $form->_paymentId); | |
1782 | } | |
1783 | ||
f2525f80 | 1784 | /** |
1785 | * Extracted code relating to paid events. | |
1786 | * | |
1787 | * @param $params | |
1788 | * | |
1789 | * @return array | |
f84f92f2 | 1790 | * |
f84f92f2 | 1791 | * @throws \CiviCRM_API3_Exception |
f2525f80 | 1792 | */ |
1793 | protected function preparePaidEventProcessing($params): array { | |
1794 | $participantStatus = CRM_Event_PseudoConstant::participantStatus(); | |
939287ad | 1795 | $contributionParams = [ |
1796 | 'skipCleanMoney' => TRUE, | |
1797 | 'revenue_recognition_date' => $this->getRevenueRecognitionDate(), | |
1798 | ]; | |
f2525f80 | 1799 | $lineItem = []; |
1800 | $additionalParticipantDetails = []; | |
939287ad | 1801 | |
3293fb2d | 1802 | if ($this->isPaymentOnExistingContribution()) { |
a3aed42a | 1803 | $contributionParams['total_amount'] = $this->getParticipantValue('fee_amount'); |
f2525f80 | 1804 | |
1805 | $params['discount_id'] = NULL; | |
1806 | //re-enter the values for UPDATE mode | |
a3aed42a | 1807 | $params['fee_level'] = $params['amount_level'] = $this->getParticipantValue('fee_level'); |
1808 | $params['fee_amount'] = $this->getParticipantValue('fee_amount'); | |
f2525f80 | 1809 | if (isset($params['priceSetId'])) { |
32b59dd0 | 1810 | CRM_Core_Error::deprecatedFunctionWarning('It seems this line is never hit & can go.'); |
f2525f80 | 1811 | $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id); |
1812 | } | |
1813 | //also add additional participant's fee level/priceset | |
1814 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
1815 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
1816 | $hasLineItems = CRM_Utils_Array::value('priceSetId', $params, FALSE); | |
2f5a251a | 1817 | $additionalParticipantDetails = $this->getFeeDetails($additionalIds, $hasLineItems); |
f2525f80 | 1818 | } |
1819 | } | |
1820 | else { | |
1821 | ||
1822 | // check that discount_id is set | |
1823 | if (empty($params['discount_id'])) { | |
1824 | $params['discount_id'] = 'null'; | |
1825 | } | |
1826 | ||
1827 | //lets carry currency, CRM-4453 | |
1828 | $params['fee_currency'] = CRM_Core_Config::singleton()->defaultCurrency; | |
1829 | if (!isset($lineItem[0])) { | |
1830 | $lineItem[0] = []; | |
1831 | } | |
1832 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], | |
1833 | $params, $lineItem[0] | |
1834 | ); | |
1835 | //CRM-11529 for quick config backoffice transactions | |
1836 | //when financial_type_id is passed in form, update the | |
1837 | //lineitems with the financial type selected in form | |
9c1bc317 CW |
1838 | $submittedFinancialType = $params['financial_type_id'] ?? NULL; |
1839 | $isPaymentRecorded = $params['record_contribution'] ?? NULL; | |
f2525f80 | 1840 | if ($isPaymentRecorded && $this->_quickConfig && $submittedFinancialType) { |
1841 | foreach ($lineItem[0] as &$values) { | |
1842 | $values['financial_type_id'] = $submittedFinancialType; | |
1843 | } | |
1844 | } | |
1845 | ||
1846 | $params['fee_level'] = $params['amount_level']; | |
1847 | $contributionParams['total_amount'] = $params['amount']; | |
1848 | if ($this->_quickConfig && !empty($params['total_amount']) && | |
1849 | $params['status_id'] != array_search('Partially paid', $participantStatus) | |
1850 | ) { | |
1851 | $params['fee_amount'] = $params['total_amount']; | |
1852 | } | |
1853 | else { | |
1854 | //fix for CRM-3086 | |
1855 | $params['fee_amount'] = $params['amount']; | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | if (isset($params['priceSetId'])) { | |
1860 | if (!empty($lineItem[0])) { | |
1861 | $this->set('lineItem', $lineItem); | |
1862 | ||
1863 | $this->_lineItem = $lineItem; | |
1864 | $lineItem = array_merge($lineItem, $additionalParticipantDetails); | |
1865 | ||
1866 | $participantCount = []; | |
1867 | foreach ($lineItem as $k) { | |
1868 | foreach ($k as $v) { | |
1869 | if (CRM_Utils_Array::value('participant_count', $v) > 0) { | |
1870 | $participantCount[] = $v['participant_count']; | |
1871 | } | |
1872 | } | |
1873 | } | |
1874 | } | |
1875 | if (isset($participantCount)) { | |
1876 | $this->assign('pricesetFieldsCount', $participantCount); | |
1877 | } | |
1878 | $this->assign('lineItem', empty($lineItem[0]) || $this->_quickConfig ? FALSE : $lineItem); | |
1879 | } | |
1880 | else { | |
1881 | $this->assign('amount_level', $params['amount_level']); | |
1882 | } | |
1883 | return [$contributionParams, $lineItem, $additionalParticipantDetails, $params]; | |
1884 | } | |
1885 | ||
e0dbddb4 | 1886 | /** |
1887 | * @param $eventID | |
1888 | * @param $participantRoles | |
1889 | * @param $receiptText | |
1890 | * @param $isPaidEvent | |
1891 | * | |
1892 | * @return void | |
1893 | */ | |
1894 | protected function assignEventDetailsToTpl($eventID, $participantRoles, $receiptText, $isPaidEvent) { | |
1895 | //use of the message template below requires variables in different format | |
1896 | $events = []; | |
1897 | $returnProperties = ['event_type_id', 'fee_label', 'start_date', 'end_date', 'is_show_location', 'title']; | |
1898 | ||
1899 | //get all event details. | |
1900 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $eventID, $events, $returnProperties); | |
1901 | $event = $events[$eventID]; | |
1902 | unset($event['start_date']); | |
1903 | unset($event['end_date']); | |
1904 | ||
1905 | $role = CRM_Event_PseudoConstant::participantRole(); | |
1906 | ||
1907 | if (is_array($participantRoles)) { | |
1908 | $selectedRoles = []; | |
1909 | foreach ($participantRoles as $roleId) { | |
1910 | $selectedRoles[] = $role[$roleId]; | |
1911 | } | |
1912 | $event['participant_role'] = implode(', ', $selectedRoles); | |
1913 | } | |
1914 | else { | |
9c1bc317 | 1915 | $event['participant_role'] = $role[$participantRoles] ?? NULL; |
e0dbddb4 | 1916 | } |
1917 | $event['is_monetary'] = $isPaidEvent; | |
1918 | ||
1919 | if ($receiptText) { | |
1920 | $event['confirm_email_text'] = $receiptText; | |
1921 | } | |
1922 | $this->assign('event', $event); | |
1923 | $this->assign('isShowLocation', $event['is_show_location']); | |
1924 | if (CRM_Utils_Array::value('is_show_location', $event) == 1) { | |
1925 | $locationParams = [ | |
1926 | 'entity_id' => $eventID, | |
1927 | 'entity_table' => 'civicrm_event', | |
1928 | ]; | |
1929 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); | |
1930 | $this->assign('location', $location); | |
1931 | } | |
1932 | } | |
1933 | ||
44237a29 | 1934 | /** |
1935 | * Process the contribution. | |
1936 | * | |
1937 | * @param CRM_Core_Form $form | |
1938 | * @param array $params | |
1939 | * @param array $result | |
1940 | * @param int $contactID | |
1941 | * @param bool $pending | |
1942 | * @param array $paymentProcessor | |
1943 | * | |
1944 | * @return \CRM_Contribute_BAO_Contribution | |
1945 | * | |
1946 | * @throws \CRM_Core_Exception | |
1947 | * @throws \CiviCRM_API3_Exception | |
1948 | */ | |
1949 | public function processContribution( | |
1950 | &$form, $params, $result, $contactID, | |
1951 | $pending = FALSE, | |
1952 | $paymentProcessor = NULL | |
1953 | ) { | |
1954 | $transaction = new CRM_Core_Transaction(); | |
1955 | ||
1956 | $now = date('YmdHis'); | |
1957 | $receiptDate = NULL; | |
1958 | ||
1959 | if (!empty($form->_values['event']['is_email_confirm'])) { | |
1960 | $receiptDate = $now; | |
1961 | } | |
1962 | ||
1963 | // CRM-20264: fetch CC type ID and number (last 4 digit) and assign it back to $params | |
1964 | CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($params); | |
1965 | ||
1966 | $contribParams = [ | |
1967 | 'contact_id' => $contactID, | |
1968 | 'financial_type_id' => !empty($form->_values['event']['financial_type_id']) ? $form->_values['event']['financial_type_id'] : $params['financial_type_id'], | |
1969 | 'receive_date' => $now, | |
1970 | 'total_amount' => $params['amount'], | |
1971 | 'tax_amount' => $params['tax_amount'], | |
1972 | 'amount_level' => $params['amount_level'], | |
1973 | 'invoice_id' => $params['invoiceID'], | |
1974 | 'currency' => $params['currencyID'], | |
1975 | 'source' => !empty($params['participant_source']) ? $params['participant_source'] : $params['description'], | |
1976 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), | |
6b409353 CW |
1977 | 'campaign_id' => $params['campaign_id'] ?? NULL, |
1978 | 'card_type_id' => $params['card_type_id'] ?? NULL, | |
1979 | 'pan_truncation' => $params['pan_truncation'] ?? NULL, | |
44237a29 | 1980 | ]; |
1981 | ||
1982 | if ($paymentProcessor) { | |
1983 | $contribParams['payment_instrument_id'] = $paymentProcessor['payment_instrument_id']; | |
1984 | $contribParams['payment_processor'] = $paymentProcessor['id']; | |
1985 | } | |
1986 | ||
1987 | if (!$pending && $result) { | |
1988 | $contribParams += [ | |
6b409353 | 1989 | 'fee_amount' => $result['fee_amount'] ?? NULL, |
44237a29 | 1990 | 'trxn_id' => $result['trxn_id'], |
1991 | 'receipt_date' => $receiptDate, | |
1992 | ]; | |
1993 | } | |
1994 | ||
1995 | $allStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
1996 | $contribParams['contribution_status_id'] = array_search('Completed', $allStatuses); | |
1997 | if ($pending) { | |
1998 | $contribParams['contribution_status_id'] = array_search('Pending', $allStatuses); | |
1999 | } | |
2000 | ||
2001 | $contribParams['is_test'] = 0; | |
2002 | if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { | |
2003 | $contribParams['is_test'] = 1; | |
2004 | } | |
2005 | ||
2006 | if (!empty($contribParams['invoice_id'])) { | |
2007 | $contribParams['id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
2008 | $contribParams['invoice_id'], | |
2009 | 'id', | |
2010 | 'invoice_id' | |
2011 | ); | |
2012 | } | |
939287ad | 2013 | $contribParams['revenue_recognition_date'] = $this->getRevenueRecognitionDate(); |
44237a29 | 2014 | |
44237a29 | 2015 | //create an contribution address |
2016 | // The concept of contributeMode is deprecated. Elsewhere we use the function processBillingAddress() - although | |
2017 | // currently that is only inherited by back-office forms. | |
2018 | if ($form->_contributeMode != 'notify' && empty($params['is_pay_later'])) { | |
2019 | $contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID); | |
2020 | } | |
2021 | ||
2022 | $contribParams['skipLineItem'] = 1; | |
2023 | $contribParams['skipCleanMoney'] = 1; | |
2024 | // create contribution record | |
2025 | $contribution = CRM_Contribute_BAO_Contribution::add($contribParams); | |
2026 | // CRM-11124 | |
2027 | CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, NULL, CRM_Price_BAO_PriceSet::parseFirstPriceSetValueIDFromParams($params)); | |
2028 | ||
2029 | // process soft credit / pcp pages | |
2030 | if (!empty($params['pcp_made_through_id'])) { | |
2031 | CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); | |
2032 | CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); | |
2033 | } | |
2034 | ||
2035 | $transaction->commit(); | |
2036 | ||
2037 | return $contribution; | |
2038 | } | |
2039 | ||
2040 | /** | |
2041 | * Process the participant. | |
2042 | * | |
2043 | * @param CRM_Core_Form $form | |
b2a8013e | 2044 | * @param array $params |
44237a29 | 2045 | * @param int $contactID |
2046 | * | |
2047 | * @return \CRM_Event_BAO_Participant | |
b2a8013e | 2048 | * @throws \CRM_Core_Exception |
fc076fe3 | 2049 | * @throws \CiviCRM_API3_Exception |
44237a29 | 2050 | */ |
35f86af2 | 2051 | protected function addParticipant(&$form, $params, $contactID) { |
44237a29 | 2052 | $transaction = new CRM_Core_Transaction(); |
2053 | ||
44237a29 | 2054 | $participantFields = CRM_Event_DAO_Participant::fields(); |
b2a8013e | 2055 | $participantParams = [ |
6b409353 | 2056 | 'id' => $params['participant_id'] ?? NULL, |
44237a29 | 2057 | 'contact_id' => $contactID, |
2058 | 'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'], | |
2059 | 'status_id' => CRM_Utils_Array::value('participant_status', | |
2060 | $params, 1 | |
2061 | ), | |
fc076fe3 | 2062 | 'role_id' => CRM_Utils_Array::value('participant_role_id', $params) ?: CRM_Event_BAO_Participant::getDefaultRoleID(), |
51c6ef2b | 2063 | 'register_date' => $params['register_date'], |
44237a29 | 2064 | 'source' => CRM_Utils_String::ellipsify( |
2065 | isset($params['participant_source']) ? CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params), | |
2066 | $participantFields['participant_source']['maxlength'] | |
2067 | ), | |
6b409353 | 2068 | 'fee_level' => $params['amount_level'] ?? NULL, |
44237a29 | 2069 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), |
6b409353 CW |
2070 | 'fee_amount' => $params['fee_amount'] ?? NULL, |
2071 | 'registered_by_id' => $params['registered_by_id'] ?? NULL, | |
2072 | 'discount_id' => $params['discount_id'] ?? NULL, | |
2073 | 'fee_currency' => $params['currencyID'] ?? NULL, | |
2074 | 'campaign_id' => $params['campaign_id'] ?? NULL, | |
b2a8013e | 2075 | ]; |
44237a29 | 2076 | |
2077 | if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { | |
2078 | $participantParams['is_test'] = 1; | |
2079 | } | |
2080 | else { | |
2081 | $participantParams['is_test'] = 0; | |
2082 | } | |
2083 | ||
2084 | if (!empty($form->_params['note'])) { | |
2085 | $participantParams['note'] = $form->_params['note']; | |
2086 | } | |
2087 | elseif (!empty($form->_params['participant_note'])) { | |
2088 | $participantParams['note'] = $form->_params['participant_note']; | |
2089 | } | |
2090 | ||
2091 | // reuse id if one already exists for this one (can happen | |
2092 | // with back button being hit etc) | |
2093 | if (!$participantParams['id'] && !empty($params['contributionID'])) { | |
2094 | $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
2095 | $params['contributionID'], | |
2096 | 'participant_id', | |
2097 | 'contribution_id' | |
2098 | ); | |
2099 | $participantParams['id'] = $pID; | |
2100 | } | |
2101 | $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event'); | |
2102 | ||
2103 | if (!$participantParams['discount_id']) { | |
2104 | $participantParams['discount_id'] = "null"; | |
2105 | } | |
2106 | ||
2107 | $participant = CRM_Event_BAO_Participant::create($participantParams); | |
2108 | ||
2109 | $transaction->commit(); | |
2110 | ||
2111 | return $participant; | |
2112 | } | |
2113 | ||
3293fb2d | 2114 | /** |
2115 | * Is a payment being made on an existing contribution. | |
2116 | * | |
2117 | * Note | |
2118 | * 1) ideally we should not permit this on this form! Perhaps we don't & this is just cruft. | |
2119 | * 2) _paymentID is the contribution id. | |
2120 | * | |
2121 | * @return bool | |
2122 | */ | |
2123 | protected function isPaymentOnExistingContribution(): bool { | |
a3aed42a | 2124 | return ($this->getParticipantID() && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId; |
3293fb2d | 2125 | } |
2126 | ||
f84f92f2 | 2127 | /** |
2128 | * Get the value for a field relating to the event. | |
2129 | * | |
2130 | * @param string $fieldName | |
2131 | * | |
2132 | * @return mixed | |
2133 | * @throws \CiviCRM_API3_Exception | |
2134 | */ | |
2135 | protected function getEventValue(string $fieldName) { | |
2136 | if (!isset($this->_event)) { | |
2137 | $this->_event = civicrm_api3('Event', 'getsingle', ['id' => $this->_eventId]); | |
2138 | } | |
2139 | return $this->_event[$fieldName]; | |
2140 | } | |
2141 | ||
a3aed42a | 2142 | /** |
2143 | * Get a value from the existing participant record (applies to edits). | |
2144 | * | |
2145 | * @param string $fieldName | |
2146 | * | |
2147 | * @return array | |
2148 | * @throws \CiviCRM_API3_Exception | |
2149 | */ | |
2150 | protected function getParticipantValue($fieldName) { | |
2151 | if (!$this->participantRecord) { | |
182df232 | 2152 | $this->participantRecord = civicrm_api3('Participant', 'getsingle', ['id' => $this->_id]); |
a3aed42a | 2153 | } |
182df232 | 2154 | return $this->participantRecord[$fieldName] ?? $this->participantRecord['participant_' . $fieldName]; |
a3aed42a | 2155 | } |
2156 | ||
2157 | /** | |
2158 | * Get id of participant being edited. | |
2159 | * | |
2160 | * @return int|null | |
2161 | */ | |
2162 | protected function getParticipantID() { | |
2ee9bfad | 2163 | return $this->_id ?? $this->_pId; |
a3aed42a | 2164 | } |
2165 | ||
939287ad | 2166 | /** |
2167 | * Get the value for the revenue recognition date field. | |
2168 | * | |
2169 | * @return string | |
2170 | * | |
2171 | * @throws \CiviCRM_API3_Exception | |
2172 | */ | |
2173 | protected function getRevenueRecognitionDate() { | |
2174 | if (Civi::settings()->get('deferred_revenue_enabled')) { | |
2175 | $eventStartDate = $this->getEventValue('start_date'); | |
2176 | if (strtotime($eventStartDate) > strtotime(date('Ymt'))) { | |
2177 | return date('Ymd', strtotime($eventStartDate)); | |
2178 | } | |
2179 | } | |
2180 | return ''; | |
2181 | } | |
2182 | ||
d3e6e9a4 | 2183 | /** |
64f4eebe | 2184 | * Store the parameters to create a payment, if appropriate, on the form. |
d3e6e9a4 | 2185 | * |
2186 | * @param array $params | |
2187 | * Params as submitted. | |
2188 | */ | |
2189 | protected function storePaymentCreateParams($params) { | |
2190 | if ('Completed' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $params['contribution_status_id'])) { | |
2191 | $this->setCreatePaymentParams([ | |
2192 | 'total_amount' => $params['total_amount'], | |
2193 | 'is_send_contribution_notification' => FALSE, | |
2194 | 'payment_instrument_id' => $params['payment_instrument_id'], | |
2195 | 'trxn_date' => $params['receive_date'] ?? date('Y-m-d'), | |
2196 | 'trxn_id' => $params['trxn_id'], | |
2197 | 'pan_truncation' => $params['pan_truncation'] ?? '', | |
2198 | 'card_type_id' => $params['card_type_id'] ?? '', | |
2199 | 'check_number' => $params['check_number'] ?? '', | |
2200 | 'skipCleanMoney' => TRUE, | |
2201 | ]); | |
2202 | } | |
2203 | } | |
2204 | ||
2f5a251a | 2205 | /** |
2206 | * Get the event fee info for given participant ids | |
2207 | * either from line item table / participant table. | |
2208 | * | |
2209 | * @param array $participantIds | |
2210 | * Participant ids. | |
2211 | * @param bool $hasLineItems | |
2212 | * Do fetch from line items. | |
2213 | * | |
2214 | * @return array | |
2215 | */ | |
2216 | public function getFeeDetails($participantIds, $hasLineItems = FALSE) { | |
2217 | $feeDetails = []; | |
2218 | if (!is_array($participantIds) || empty($participantIds)) { | |
2219 | return $feeDetails; | |
2220 | } | |
2221 | ||
2222 | $select = ' | |
2223 | SELECT participant.id as id, | |
2224 | participant.fee_level as fee_level, | |
2225 | participant.fee_amount as fee_amount'; | |
2226 | $from = 'FROM civicrm_participant participant'; | |
2227 | if ($hasLineItems) { | |
2228 | $select .= ' , | |
2229 | lineItem.id as lineId, | |
2230 | lineItem.label as label, | |
2231 | lineItem.qty as qty, | |
2232 | lineItem.unit_price as unit_price, | |
2233 | lineItem.line_total as line_total, | |
2234 | field.label as field_title, | |
2235 | field.html_type as html_type, | |
2236 | field.id as price_field_id, | |
2237 | value.id as price_field_value_id, | |
2238 | value.description as description, | |
2239 | IF( value.count, value.count, 0 ) as participant_count'; | |
2240 | $from .= " | |
2241 | INNER JOIN civicrm_line_item lineItem ON ( lineItem.entity_table = 'civicrm_participant' | |
2242 | AND lineItem.entity_id = participant.id ) | |
2243 | INNER JOIN civicrm_price_field field ON ( field.id = lineItem.price_field_id ) | |
2244 | INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_value_id ) | |
2245 | "; | |
2246 | } | |
2247 | $where = 'WHERE participant.id IN ( ' . implode(', ', $participantIds) . ' )'; | |
2248 | $query = "$select $from $where"; | |
2249 | ||
2250 | $feeInfo = CRM_Core_DAO::executeQuery($query); | |
2251 | $feeProperties = ['fee_level', 'fee_amount']; | |
2252 | $lineProperties = [ | |
2253 | 'lineId', | |
2254 | 'label', | |
2255 | 'qty', | |
2256 | 'unit_price', | |
2257 | 'line_total', | |
2258 | 'field_title', | |
2259 | 'html_type', | |
2260 | 'price_field_id', | |
2261 | 'participant_count', | |
2262 | 'price_field_value_id', | |
2263 | 'description', | |
2264 | ]; | |
2265 | while ($feeInfo->fetch()) { | |
2266 | if ($hasLineItems) { | |
2267 | foreach ($lineProperties as $property) { | |
2268 | $feeDetails[$feeInfo->id][$feeInfo->lineId][$property] = $feeInfo->$property; | |
2269 | } | |
2270 | } | |
2271 | else { | |
2272 | foreach ($feeProperties as $property) { | |
2273 | $feeDetails[$feeInfo->id][$property] = $feeInfo->$property; | |
2274 | } | |
2275 | } | |
2276 | } | |
2277 | ||
2278 | return $feeDetails; | |
2279 | } | |
2280 | ||
f36fd0f6 | 2281 | /** |
2282 | * Assign the url path to the template. | |
2283 | */ | |
2284 | protected function assignUrlPath() { | |
2285 | $this->assign('urlPath', 'civicrm/contact/view/participant'); | |
2286 | if (!$this->_id && !$this->_contactId) { | |
2287 | $breadCrumbs = [ | |
2288 | [ | |
2289 | 'title' => ts('CiviEvent Dashboard'), | |
2290 | 'url' => CRM_Utils_System::url('civicrm/event', 'reset=1'), | |
2291 | ], | |
2292 | ]; | |
2293 | ||
2294 | CRM_Utils_System::appendBreadCrumb($breadCrumbs); | |
2295 | } | |
8808dc7a | 2296 | else { |
2297 | $this->assign('id', $this->_id); | |
2298 | $this->assign('contact_id', $this->_contactId); | |
2299 | } | |
f36fd0f6 | 2300 | } |
2301 | ||
6a488035 | 2302 | } |