Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * | |
31 | * @package CRM | |
0f03f337 | 32 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for processing a participation | |
39 | * in an event | |
40 | */ | |
6bd6d108 | 41 | class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment { |
6a488035 | 42 | |
96f50de2 CW |
43 | public $useLivePageJS = TRUE; |
44 | ||
6a488035 | 45 | /** |
66f9e52b | 46 | * The values for the contribution db object. |
6a488035 TO |
47 | * |
48 | * @var array | |
6a488035 TO |
49 | */ |
50 | public $_values; | |
51 | ||
52 | /** | |
66f9e52b | 53 | * The values for the quickconfig for priceset. |
6a488035 TO |
54 | * |
55 | * @var boolean | |
6a488035 TO |
56 | */ |
57 | public $_quickConfig = NULL; | |
58 | ||
59 | /** | |
60 | * Price Set ID, if the new price set method is used | |
61 | * | |
62 | * @var int | |
6a488035 TO |
63 | */ |
64 | public $_priceSetId; | |
65 | ||
66 | /** | |
66f9e52b | 67 | * Array of fields for the price set. |
6a488035 TO |
68 | * |
69 | * @var array | |
6a488035 TO |
70 | */ |
71 | public $_priceSet; | |
72 | ||
73 | /** | |
66f9e52b | 74 | * The id of the participation that we are proceessing. |
6a488035 TO |
75 | * |
76 | * @var int | |
6a488035 TO |
77 | */ |
78 | public $_id; | |
79 | ||
80 | /** | |
66f9e52b | 81 | * The id of the note. |
6a488035 TO |
82 | * |
83 | * @var int | |
6a488035 TO |
84 | */ |
85 | protected $_noteId = NULL; | |
86 | ||
87 | /** | |
0dc4ef42 | 88 | * @deprecated |
89 | * | |
90 | * Use parent $this->contactID | |
91 | * | |
66f9e52b | 92 | * The id of the contact associated with this participation. |
6a488035 TO |
93 | * |
94 | * @var int | |
6a488035 TO |
95 | */ |
96 | public $_contactId; | |
97 | ||
98 | /** | |
66f9e52b | 99 | * Array of event values. |
6a488035 TO |
100 | * |
101 | * @var array | |
6a488035 TO |
102 | */ |
103 | protected $_event; | |
104 | ||
105 | /** | |
106 | * Are we operating in "single mode", i.e. adding / editing only | |
107 | * one participant record, or is this a batch add operation | |
108 | * | |
109 | * @var boolean | |
110 | */ | |
111 | public $_single = FALSE; | |
112 | ||
113 | /** | |
66f9e52b | 114 | * If event is paid or unpaid. |
6a488035 TO |
115 | */ |
116 | public $_isPaidEvent; | |
117 | ||
118 | /** | |
66f9e52b | 119 | * Page action. |
6a488035 TO |
120 | */ |
121 | public $_action; | |
122 | ||
123 | /** | |
66f9e52b | 124 | * Role Id. |
6a488035 TO |
125 | */ |
126 | protected $_roleId = NULL; | |
127 | ||
128 | /** | |
66f9e52b | 129 | * Event Type Id. |
6a488035 TO |
130 | */ |
131 | protected $_eventTypeId = NULL; | |
132 | ||
133 | /** | |
66f9e52b | 134 | * Participant status Id. |
6a488035 TO |
135 | */ |
136 | protected $_statusId = NULL; | |
137 | ||
138 | /** | |
66f9e52b | 139 | * Cache all the participant statuses. |
6a488035 TO |
140 | */ |
141 | protected $_participantStatuses; | |
142 | ||
143 | /** | |
66f9e52b | 144 | * Participant mode. |
6a488035 TO |
145 | */ |
146 | public $_mode = NULL; | |
147 | ||
148 | /** | |
66f9e52b | 149 | * Event ID preselect. |
6a488035 TO |
150 | */ |
151 | public $_eID = NULL; | |
152 | ||
c490a46a | 153 | /** |
66f9e52b | 154 | * Line Item for Price Set. |
c490a46a | 155 | */ |
6a488035 | 156 | public $_lineItem = NULL; |
6a488035 | 157 | |
d424ffde | 158 | /** |
66f9e52b | 159 | * Contribution mode for event registration for offline mode. |
0f2b049e | 160 | * |
161 | * @deprecated | |
c490a46a | 162 | */ |
6a488035 TO |
163 | public $_contributeMode = 'direct'; |
164 | ||
165 | public $_online; | |
166 | ||
167 | /** | |
100fef9d | 168 | * Store id of role custom data type ( option value ) |
6a488035 TO |
169 | */ |
170 | protected $_roleCustomDataTypeID; | |
171 | ||
172 | /** | |
100fef9d | 173 | * Store id of event Name custom data type ( option value) |
6a488035 TO |
174 | */ |
175 | protected $_eventNameCustomDataTypeID; | |
176 | ||
177 | /** | |
66f9e52b | 178 | * Selected discount id. |
6a488035 TO |
179 | */ |
180 | public $_originalDiscountId = NULL; | |
181 | ||
182 | /** | |
66f9e52b | 183 | * Event id. |
6a488035 TO |
184 | */ |
185 | public $_eventId = NULL; | |
186 | ||
187 | /** | |
100fef9d | 188 | * Id of payment, if any |
6a488035 TO |
189 | */ |
190 | public $_paymentId = NULL; | |
191 | ||
9a4df24c EM |
192 | /** |
193 | * @todo add explanatory note about this | |
194 | * @var null | |
195 | */ | |
196 | public $_onlinePendingContributionId = NULL; | |
d5965a37 | 197 | |
6e62b28c TM |
198 | /** |
199 | * Explicitly declare the entity api name. | |
200 | */ | |
201 | public function getDefaultEntity() { | |
202 | return 'Participant'; | |
203 | } | |
9a4df24c | 204 | |
6a488035 | 205 | /** |
66f9e52b | 206 | * Set variables up before form is built. |
6a488035 TO |
207 | * |
208 | * @return void | |
6a488035 TO |
209 | */ |
210 | public function preProcess() { | |
0dc4ef42 | 211 | parent::preProcess(); |
6a488035 TO |
212 | $this->_showFeeBlock = CRM_Utils_Array::value('eventId', $_GET); |
213 | $this->assign('showFeeBlock', FALSE); | |
214 | $this->assign('feeBlockPaid', FALSE); | |
215 | ||
0dc4ef42 | 216 | // @todo eliminate this duplication. |
217 | $this->_contactId = $this->_contactID; | |
353ffa53 TO |
218 | $this->_eID = CRM_Utils_Request::retrieve('eid', 'Positive', $this); |
219 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
6a488035 TO |
220 | $this->assign('context', $this->_context); |
221 | ||
0dc4ef42 | 222 | if ($this->_contactID) { |
223 | $this->setPageTitle(ts('Event Registration for %1', array(1 => $this->userDisplayName))); | |
e2046b33 CW |
224 | } |
225 | else { | |
226 | $this->setPageTitle(ts('Event Registration')); | |
6a488035 TO |
227 | } |
228 | ||
229 | // check the current path, if search based, then dont get participantID | |
230 | // CRM-5792 | |
231 | $path = CRM_Utils_System::currentPath(); | |
0edbaec6 JM |
232 | if ( |
233 | strpos($path, 'civicrm/contact/search') === 0 || | |
234 | strpos($path, 'civicrm/group/search') === 0 | |
353ffa53 | 235 | ) { |
6a488035 TO |
236 | $this->_id = NULL; |
237 | } | |
238 | else { | |
239 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
240 | } | |
241 | ||
242 | if ($this->_id) { | |
29c61b58 | 243 | $this->assign('participantId', $this->_id); |
29c61b58 | 244 | |
6a488035 TO |
245 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
246 | $this->_id, 'id', 'participant_id' | |
247 | ); | |
0b2b58ea PJ |
248 | |
249 | $this->assign('hasPayment', $this->_paymentId); | |
685dc433 PN |
250 | $this->assign('componentId', $this->_id); |
251 | $this->assign('component', 'event'); | |
0b2b58ea | 252 | |
a4e0d7b5 DG |
253 | // CRM-12615 - Get payment information from the primary registration |
254 | if ((!$this->_paymentId) && ($this->_action == CRM_Core_Action::UPDATE)) { | |
255 | $registered_by_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
256 | $this->_id, 'registered_by_id', 'id' | |
257 | ); | |
258 | if ($registered_by_id) { | |
259 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
260 | $registered_by_id, 'id', 'participant_id' | |
261 | ); | |
262 | $this->assign('registeredByParticipantId', $registered_by_id); | |
263 | } | |
264 | } | |
6a488035 | 265 | } |
5fda4b16 | 266 | $this->setCustomDataTypes(); |
6a488035 TO |
267 | |
268 | if ($this->_mode) { | |
269 | $this->assign('participantMode', $this->_mode); | |
6a488035 TO |
270 | } |
271 | ||
272 | if ($this->_showFeeBlock) { | |
273 | $this->assign('showFeeBlock', TRUE); | |
274 | $isMonetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_showFeeBlock, 'is_monetary'); | |
275 | if ($isMonetary) { | |
276 | $this->assign('feeBlockPaid', TRUE); | |
277 | } | |
278 | return CRM_Event_Form_EventFees::preProcess($this); | |
279 | } | |
280 | ||
6a488035 TO |
281 | //check the mode when this form is called either single or as |
282 | //search task action | |
283 | if ($this->_id || $this->_contactId || $this->_context == 'standalone') { | |
284 | $this->_single = TRUE; | |
285 | $this->assign('urlPath', 'civicrm/contact/view/participant'); | |
286 | if (!$this->_id && !$this->_contactId) { | |
353ffa53 TO |
287 | $breadCrumbs = array( |
288 | array( | |
289 | 'title' => ts('CiviEvent Dashboard'), | |
6a488035 | 290 | 'url' => CRM_Utils_System::url('civicrm/event', 'reset=1'), |
a130e045 | 291 | ), |
353ffa53 | 292 | ); |
6a488035 TO |
293 | |
294 | CRM_Utils_System::appendBreadCrumb($breadCrumbs); | |
295 | } | |
296 | } | |
297 | else { | |
298 | //set the appropriate action | |
353ffa53 TO |
299 | $context = $this->get('context'); |
300 | $urlString = 'civicrm/contact/search'; | |
6a488035 TO |
301 | $this->_action = CRM_Core_Action::BASIC; |
302 | switch ($context) { | |
303 | case 'advanced': | |
304 | $urlString = 'civicrm/contact/search/advanced'; | |
305 | $this->_action = CRM_Core_Action::ADVANCED; | |
306 | break; | |
307 | ||
308 | case 'builder': | |
309 | $urlString = 'civicrm/contact/search/builder'; | |
310 | $this->_action = CRM_Core_Action::PROFILE; | |
311 | break; | |
312 | ||
313 | case 'basic': | |
314 | $urlString = 'civicrm/contact/search/basic'; | |
315 | $this->_action = CRM_Core_Action::BASIC; | |
316 | break; | |
317 | ||
318 | case 'custom': | |
319 | $urlString = 'civicrm/contact/search/custom'; | |
320 | $this->_action = CRM_Core_Action::COPY; | |
321 | break; | |
322 | } | |
42e8b05c | 323 | CRM_Contact_Form_Task::preProcessCommon($this); |
6a488035 TO |
324 | |
325 | $this->_single = FALSE; | |
326 | $this->_contactId = NULL; | |
327 | ||
328 | //set ajax path, this used for custom data building | |
329 | $this->assign('urlPath', $urlString); | |
330 | $this->assign('urlPathVar', "_qf_Participant_display=true&qfKey={$this->controller->_key}"); | |
331 | } | |
332 | ||
333 | $this->assign('single', $this->_single); | |
334 | ||
335 | if (!$this->_id) { | |
336 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
337 | } | |
338 | $this->assign('action', $this->_action); | |
339 | ||
340 | // check for edit permission | |
341 | if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) { | |
0499b0ad | 342 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
343 | } |
344 | ||
345 | if ($this->_action & CRM_Core_Action::DELETE) { | |
346 | // check delete permission for contribution | |
347 | if ($this->_id && $this->_paymentId && !CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { | |
348 | CRM_Core_Error::fatal(ts("This Participant is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record.")); | |
349 | } | |
350 | return; | |
351 | } | |
352 | ||
353 | if ($this->_id) { | |
354 | // assign participant id to the template | |
355 | $this->assign('participantId', $this->_id); | |
356 | $this->_roleId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'role_id'); | |
357 | } | |
358 | ||
359 | // when fee amount is included in form | |
8cc574cf | 360 | if (!empty($_POST['hidden_feeblock']) || !empty($_POST['send_receipt'])) { |
6a488035 TO |
361 | CRM_Event_Form_EventFees::preProcess($this); |
362 | CRM_Event_Form_EventFees::buildQuickForm($this); | |
363 | CRM_Event_Form_EventFees::setDefaultValues($this); | |
364 | } | |
365 | ||
366 | // when custom data is included in this page | |
a7488080 | 367 | if (!empty($_POST['hidden_custom'])) { |
7df021e5 | 368 | // Custom data of type participant role |
369 | // Note: Some earlier commits imply $_POST['role_id'] could be a comma separated string, | |
370 | // not sure if that ever really happens | |
a7488080 | 371 | if (!empty($_POST['role_id'])) { |
7df021e5 | 372 | foreach ($_POST['role_id'] as $roleID) { |
373 | CRM_Custom_Form_CustomData::preProcess($this, $this->_roleCustomDataTypeID, $roleID, 1, 'Participant', $this->_id); | |
6a488035 TO |
374 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
375 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
376 | } | |
377 | } | |
378 | ||
379 | //custom data of type participant event | |
380 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventNameCustomDataTypeID, $_POST['event_id'], 1, 'Participant', $this->_id); | |
381 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
382 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
383 | ||
384 | // custom data of type participant event type | |
385 | $eventTypeId = NULL; | |
386 | if ($eventId = CRM_Utils_Array::value('event_id', $_POST)) { | |
387 | $eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'event_type_id', 'id'); | |
388 | } | |
389 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventTypeCustomDataTypeID, $eventTypeId, | |
390 | 1, 'Participant', $this->_id | |
391 | ); | |
392 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
393 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
394 | ||
395 | //custom data of type participant, ( we 'null' to reset subType and subName) | |
396 | CRM_Custom_Form_CustomData::preProcess($this, 'null', 'null', 1, 'Participant', $this->_id); | |
397 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
398 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
399 | } | |
400 | ||
401 | // CRM-4395, get the online pending contribution id. | |
402 | $this->_onlinePendingContributionId = NULL; | |
403 | if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { | |
404 | $this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id, | |
405 | 'Event' | |
406 | ); | |
407 | } | |
408 | $this->set('onlinePendingContributionId', $this->_onlinePendingContributionId); | |
6a488035 TO |
409 | } |
410 | ||
411 | /** | |
412 | * This function sets the default values for the form in edit/view mode | |
413 | * the default values are retrieved from the database | |
414 | * | |
6a488035 | 415 | * |
355ba699 | 416 | * @return void |
6a488035 TO |
417 | */ |
418 | public function setDefaultValues() { | |
419 | if ($this->_showFeeBlock) { | |
420 | return CRM_Event_Form_EventFees::setDefaultValues($this); | |
421 | } | |
422 | ||
6a488035 TO |
423 | $defaults = array(); |
424 | ||
425 | if ($this->_action & CRM_Core_Action::DELETE) { | |
426 | return $defaults; | |
427 | } | |
428 | ||
429 | if ($this->_id) { | |
430 | $ids = array(); | |
431 | $params = array('id' => $this->_id); | |
432 | ||
433 | CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); | |
434 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
435 | if ($defaults[$this->_id]['role_id']) { | |
bdc20176 | 436 | $roleIDs = explode($sep, $defaults[$this->_id]['role_id']); |
6a488035 TO |
437 | } |
438 | $this->_contactId = $defaults[$this->_id]['contact_id']; | |
439 | $this->_statusId = $defaults[$this->_id]['participant_status_id']; | |
440 | ||
441 | //set defaults for note | |
442 | $noteDetails = CRM_Core_BAO_Note::getNote($this->_id, 'civicrm_participant'); | |
443 | $defaults[$this->_id]['note'] = array_pop($noteDetails); | |
444 | ||
445 | // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859) | |
446 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
447 | $this->assign('additionalParticipants', CRM_Event_BAO_Participant::getAdditionalParticipants($this->_id)); | |
448 | } | |
449 | ||
450 | // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859) | |
a7488080 | 451 | if (!empty($defaults[$this->_id]['participant_registered_by_id'])) { |
6a488035 TO |
452 | $registered_by_contact_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', |
453 | $defaults[$this->_id]['participant_registered_by_id'], | |
454 | 'contact_id', 'id' | |
455 | ); | |
456 | $this->assign('participant_registered_by_id', $defaults[$this->_id]['participant_registered_by_id']); | |
457 | $this->assign('registered_by_contact_id', $registered_by_contact_id); | |
458 | $this->assign('registered_by_display_name', CRM_Contact_BAO_Contact::displayName($registered_by_contact_id)); | |
459 | } | |
460 | } | |
461 | ||
462 | if ($this->_action & (CRM_Core_Action::VIEW | CRM_Core_Action::BROWSE)) { | |
463 | $inactiveNeeded = TRUE; | |
464 | $viewMode = TRUE; | |
465 | } | |
466 | else { | |
467 | $viewMode = FALSE; | |
468 | $inactiveNeeded = FALSE; | |
469 | } | |
470 | ||
471 | //setting default register date | |
472 | if ($this->_action == CRM_Core_Action::ADD) { | |
e4659866 | 473 | $statuses = array_flip(CRM_Event_PseudoConstant::participantStatus()); |
474 | $defaults[$this->_id]['status_id'] = CRM_Utils_Array::value('Registered', $statuses); | |
a7488080 | 475 | if (!empty($defaults[$this->_id]['event_id'])) { |
6a488035 TO |
476 | $contributionTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', |
477 | $defaults[$this->_id]['event_id'], | |
bdc20176 | 478 | 'financial_type_id' |
d96cf288 | 479 | ); |
6a488035 | 480 | if ($contributionTypeId) { |
d96cf288 | 481 | $defaults[$this->_id]['financial_type_id'] = $contributionTypeId; |
6a488035 TO |
482 | } |
483 | } | |
484 | ||
485 | if ($this->_mode) { | |
486 | $fields["email-{$this->_bltID}"] = 1; | |
487 | $fields['email-Primary'] = 1; | |
488 | ||
489 | if ($this->_contactId) { | |
490 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $fields, $defaults); | |
491 | } | |
492 | ||
493 | if (empty($defaults["email-{$this->_bltID}"]) && | |
494 | !empty($defaults['email-Primary']) | |
495 | ) { | |
496 | $defaults[$this->_id]["email-{$this->_bltID}"] = $defaults['email-Primary']; | |
497 | } | |
498 | } | |
499 | ||
500 | $submittedRole = $this->getElementValue('role_id'); | |
a7488080 | 501 | if (!empty($submittedRole[0])) { |
6a488035 TO |
502 | $roleID = $submittedRole[0]; |
503 | } | |
504 | $submittedEvent = $this->getElementValue('event_id'); | |
a4e49a48 | 505 | if (!empty($submittedEvent[0])) { |
6a488035 TO |
506 | $eventID = $submittedEvent[0]; |
507 | } | |
508 | } | |
509 | else { | |
510 | $defaults[$this->_id]['record_contribution'] = 0; | |
511 | ||
512 | if ($defaults[$this->_id]['participant_is_pay_later']) { | |
513 | $this->assign('participant_is_pay_later', TRUE); | |
514 | } | |
515 | ||
516 | $this->assign('participant_status_id', $defaults[$this->_id]['participant_status_id']); | |
6a488035 TO |
517 | $eventID = $defaults[$this->_id]['event_id']; |
518 | ||
519 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
520 | ||
521 | $this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id'); | |
522 | if ($this->_discountId) { | |
523 | $this->set('discountId', $this->_discountId); | |
524 | } | |
525 | } | |
526 | ||
006389de TO |
527 | list($defaults[$this->_id]['register_date'], $defaults[$this->_id]['register_date_time']) |
528 | = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value('register_date', $defaults[$this->_id]), 'activityDateTime'); | |
6a488035 TO |
529 | |
530 | //assign event and role id, this is needed for Custom data building | |
531 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
a7488080 | 532 | if (!empty($defaults[$this->_id]['participant_role_id'])) { |
6a488035 TO |
533 | $roleIDs = explode($sep, $defaults[$this->_id]['participant_role_id']); |
534 | } | |
6a488035 TO |
535 | if (isset($_POST['event_id'])) { |
536 | $eventID = $_POST['event_id']; | |
bdc20176 PJ |
537 | } |
538 | ||
22e263ad | 539 | if ($this->_eID) { |
bdc20176 PJ |
540 | $eventID = $this->_eID; |
541 | //@todo - rationalise the $this->_eID with $POST['event_id'], $this->_eid is set when eid=x is in the url | |
542 | $roleID = CRM_Core_DAO::getFieldValue( | |
543 | 'CRM_Event_DAO_Event', | |
544 | $this->_eID, | |
545 | 'default_role_id' | |
546 | ); | |
22e263ad | 547 | if (empty($roleIDs)) { |
bdc20176 | 548 | $roleIDs = (array) $defaults[$this->_id]['participant_role_id'] = $roleID; |
6a488035 | 549 | } |
bdc20176 PJ |
550 | $defaults[$this->_id]['event_id'] = $eventID; |
551 | } | |
552 | if (!empty($eventID)) { | |
553 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
554 | } | |
555 | //these should take precedence so we state them last | |
556 | $urlRoleIDS = CRM_Utils_Request::retrieve('roles', 'String'); | |
22e263ad | 557 | if ($urlRoleIDS) { |
bdc20176 PJ |
558 | $roleIDs = explode(',', $urlRoleIDS); |
559 | } | |
560 | if (isset($roleIDs)) { | |
cd120ea0 | 561 | $defaults[$this->_id]['role_id'] = implode(',', $roleIDs); |
6a488035 TO |
562 | } |
563 | ||
564 | if (isset($eventID)) { | |
565 | $this->assign('eventID', $eventID); | |
566 | $this->set('eventId', $eventID); | |
567 | } | |
568 | ||
569 | if (isset($this->_eventTypeId)) { | |
570 | $this->assign('eventTypeID', $this->_eventTypeId); | |
571 | } | |
572 | ||
573 | $this->assign('event_is_test', CRM_Utils_Array::value('event_is_test', $defaults[$this->_id])); | |
574 | return $defaults[$this->_id]; | |
575 | } | |
576 | ||
577 | /** | |
66f9e52b | 578 | * Build the form object. |
6a488035 | 579 | * |
355ba699 | 580 | * @return void |
6a488035 TO |
581 | */ |
582 | public function buildQuickForm() { | |
e8cf3013 PJ |
583 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); |
584 | $partiallyPaidStatusId = array_search('Partially paid', $participantStatuses); | |
81f3d017 | 585 | $this->assign('partiallyPaidStatusId', $partiallyPaidStatusId); |
e8cf3013 | 586 | |
6a488035 TO |
587 | if ($this->_showFeeBlock) { |
588 | return CRM_Event_Form_EventFees::buildQuickForm($this); | |
589 | } | |
590 | ||
6a488035 TO |
591 | //need to assign custom data type to the template |
592 | $this->assign('customDataType', 'Participant'); | |
593 | ||
594 | $this->applyFilter('__ALL__', 'trim'); | |
595 | ||
596 | if ($this->_action & CRM_Core_Action::DELETE) { | |
597 | if ($this->_single) { | |
598 | $additionalParticipant = count(CRM_Event_BAO_Event::buildCustomProfile($this->_id, | |
599 | NULL, | |
600 | $this->_contactId, | |
601 | FALSE, | |
602 | TRUE | |
603 | )) - 1; | |
604 | if ($additionalParticipant) { | |
0479b4c8 | 605 | $deleteParticipants = array( |
353ffa53 | 606 | 1 => ts('Delete this participant record along with associated participant record(s).'), |
6a488035 TO |
607 | 2 => ts('Delete only this participant record.'), |
608 | ); | |
609 | $this->addRadio('delete_participant', NULL, $deleteParticipants, NULL, '<br />'); | |
610 | $this->setDefaults(array('delete_participant' => 1)); | |
611 | $this->assign('additionalParticipant', $additionalParticipant); | |
612 | } | |
613 | } | |
614 | $this->addButtons(array( | |
615 | array( | |
616 | 'type' => 'next', | |
617 | 'name' => ts('Delete'), | |
618 | 'spacing' => ' ', | |
619 | 'isDefault' => TRUE, | |
620 | ), | |
621 | array( | |
622 | 'type' => 'cancel', | |
623 | 'name' => ts('Cancel'), | |
624 | ), | |
625 | ) | |
626 | ); | |
627 | return; | |
628 | } | |
629 | ||
01bfdd10 | 630 | if ($this->_single && $this->_context == 'standalone') { |
353ffa53 TO |
631 | $this->addEntityRef('contact_id', ts('Contact'), array( |
632 | 'create' => TRUE, | |
a130e045 | 633 | 'api' => array('extra' => array('email')), |
353ffa53 | 634 | ), TRUE); |
6a488035 | 635 | } |
6a488035 | 636 | |
1beb268a CW |
637 | $eventFieldParams = array( |
638 | 'entity' => 'event', | |
639 | 'select' => array('minimumInputLength' => 0), | |
640 | 'api' => array( | |
641 | 'extra' => array('campaign_id', 'default_role_id', 'event_type_id'), | |
21dfd5f5 | 642 | ), |
1beb268a | 643 | ); |
6a488035 TO |
644 | |
645 | if ($this->_mode) { | |
1beb268a CW |
646 | // exclude events which are not monetary when credit card registration is used |
647 | $eventFieldParams['api']['params']['is_monetary'] = 1; | |
6a488035 | 648 | } |
b15c60e1 | 649 | $this->addPaymentProcessorSelect(TRUE, FALSE, FALSE); |
6a488035 | 650 | |
1beb268a | 651 | $element = $this->addEntityRef('event_id', ts('Event'), $eventFieldParams, TRUE); |
6a488035 TO |
652 | |
653 | //frozen the field fix for CRM-4171 | |
654 | if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
655 | if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
353ffa53 TO |
656 | $this->_id, 'contribution_id', 'participant_id' |
657 | ) | |
658 | ) { | |
6a488035 TO |
659 | $element->freeze(); |
660 | } | |
661 | } | |
662 | ||
663 | //CRM-7362 --add campaigns. | |
664 | $campaignId = NULL; | |
665 | if ($this->_id) { | |
666 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'campaign_id'); | |
667 | } | |
668 | if (!$campaignId) { | |
669 | $eventId = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
670 | if ($eventId) { | |
671 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'campaign_id'); | |
672 | } | |
673 | } | |
674 | CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId); | |
675 | ||
676 | $this->addDateTime('register_date', ts('Registration Date'), TRUE, array('formatType' => 'activityDateTime')); | |
677 | ||
678 | if ($this->_id) { | |
679 | $this->assign('entityID', $this->_id); | |
680 | } | |
681 | ||
cd120ea0 | 682 | $this->addSelect('role_id', array('multiple' => TRUE, 'class' => 'huge'), TRUE); |
6a488035 TO |
683 | |
684 | // CRM-4395 | |
685 | $checkCancelledJs = array('onchange' => "return sendNotification( );"); | |
686 | $confirmJS = NULL; | |
687 | if ($this->_onlinePendingContributionId) { | |
688 | $cancelledparticipantStatusId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus()); | |
689 | $cancelledContributionStatusId = array_search('Cancelled', | |
690 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
691 | ); | |
692 | $checkCancelledJs = array( | |
a130e045 | 693 | 'onchange' => "checkCancelled( this.value, {$cancelledparticipantStatusId},{$cancelledContributionStatusId});", |
6a488035 TO |
694 | ); |
695 | ||
696 | $participantStatusId = array_search('Pending from pay later', | |
697 | CRM_Event_PseudoConstant::participantStatus() | |
698 | ); | |
699 | $contributionStatusId = array_search('Completed', | |
700 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
701 | ); | |
702 | $confirmJS = array('onclick' => "return confirmStatus( {$participantStatusId}, {$contributionStatusId} );"); | |
703 | } | |
704 | ||
453ed6d1 KJ |
705 | // get the participant status names to build special status array which is used to show notification |
706 | // checkbox below participant status select | |
707 | $participantStatusName = CRM_Event_PseudoConstant::participantStatus(); | |
708 | $notificationStatuses = array( | |
709 | 'Cancelled', | |
710 | 'Pending from waitlist', | |
711 | 'Pending from approval', | |
712 | 'Expired', | |
713 | ); | |
714 | ||
715 | // get the required status and then implode only ids | |
716 | $notificationStatusIds = implode(',', array_keys(array_intersect($participantStatusName, $notificationStatuses))); | |
717 | $this->assign('notificationStatusIds', $notificationStatusIds); | |
718 | ||
f76b27fe | 719 | $this->_participantStatuses = $statusOptions = CRM_Event_BAO_Participant::buildOptions('status_id', 'create'); |
f6bae84f | 720 | |
f76b27fe CW |
721 | // Only show refund status when editing |
722 | if ($this->_action & CRM_Core_Action::ADD) { | |
723 | $pendingRefundStatusId = array_search('Pending refund', $participantStatusName); | |
724 | if ($pendingRefundStatusId) { | |
725 | unset($statusOptions[$pendingRefundStatusId]); | |
726 | } | |
727 | } | |
728 | ||
353ffa53 TO |
729 | $this->addSelect('status_id', $checkCancelledJs + array( |
730 | 'options' => $statusOptions, | |
a130e045 | 731 | 'option_url' => 'civicrm/admin/participant_status', |
353ffa53 | 732 | ), TRUE); |
6a488035 TO |
733 | |
734 | $this->addElement('checkbox', 'is_notify', ts('Send Notification'), NULL); | |
735 | ||
736 | $this->add('text', 'source', ts('Event Source')); | |
737 | $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); | |
738 | $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']); | |
739 | ||
740 | $buttons[] = array( | |
741 | 'type' => 'upload', | |
742 | 'name' => ts('Save'), | |
743 | 'isDefault' => TRUE, | |
744 | 'js' => $confirmJS, | |
745 | ); | |
746 | ||
747 | $path = CRM_Utils_System::currentPath(); | |
239a5027 PJ |
748 | $excludeForPaths = array( |
749 | 'civicrm/contact/search', | |
21dfd5f5 | 750 | 'civicrm/group/search', |
239a5027 PJ |
751 | ); |
752 | if (!in_array($path, $excludeForPaths)) { | |
6a488035 TO |
753 | $buttons[] = array( |
754 | 'type' => 'upload', | |
755 | 'name' => ts('Save and New'), | |
756 | 'subName' => 'new', | |
757 | 'js' => $confirmJS, | |
758 | ); | |
759 | } | |
239a5027 | 760 | |
6a488035 TO |
761 | $buttons[] = array( |
762 | 'type' => 'cancel', | |
763 | 'name' => ts('Cancel'), | |
764 | ); | |
765 | ||
766 | $this->addButtons($buttons); | |
767 | if ($this->_action == CRM_Core_Action::VIEW) { | |
768 | $this->freeze(); | |
769 | } | |
770 | } | |
771 | ||
772 | /** | |
66f9e52b | 773 | * Add local and global form rules. |
6a488035 | 774 | * |
6a488035 TO |
775 | * |
776 | * @return void | |
777 | */ | |
00be9182 | 778 | public function addRules() { |
6a488035 TO |
779 | $this->addFormRule(array('CRM_Event_Form_Participant', 'formRule'), $this); |
780 | } | |
781 | ||
782 | /** | |
66f9e52b | 783 | * Global validation rules for the form. |
6a488035 | 784 | * |
d4dd1e85 TO |
785 | * @param array $values |
786 | * Posted values of the form. | |
2a6da8d7 EM |
787 | * @param $files |
788 | * @param $self | |
789 | * | |
a6c01b45 CW |
790 | * @return array |
791 | * list of errors to be posted back to the form | |
6a488035 | 792 | */ |
00be9182 | 793 | public static function formRule($values, $files, $self) { |
6a488035 TO |
794 | // If $values['_qf_Participant_next'] is Delete or |
795 | // $values['event_id'] is empty, then return | |
796 | // instead of proceeding further. | |
797 | ||
798 | if ((CRM_Utils_Array::value('_qf_Participant_next', $values) == 'Delete') || | |
799 | (!$values['event_id']) | |
800 | ) { | |
801 | return TRUE; | |
802 | } | |
803 | ||
804 | $errorMsg = array(); | |
6a488035 | 805 | |
a7488080 | 806 | if (!empty($values['payment_processor_id'])) { |
a479fe60 | 807 | // make sure that payment instrument values (e.g. credit card number and cvv) are valid |
f48e6cf7 | 808 | CRM_Core_Payment_Form::validatePaymentInstrument($values['payment_processor_id'], $values, $errorMsg, NULL); |
6a488035 TO |
809 | } |
810 | ||
a7488080 CW |
811 | if (!empty($values['record_contribution'])) { |
812 | if (empty($values['financial_type_id'])) { | |
d96cf288 DG |
813 | $errorMsg['financial_type_id'] = ts('Please enter the associated Financial Type'); |
814 | } | |
a7488080 | 815 | if (empty($values['payment_instrument_id'])) { |
4db803dd | 816 | $errorMsg['payment_instrument_id'] = ts('Payment Method is a required field.'); |
d96cf288 | 817 | } |
610d2dac JP |
818 | if (!empty($values['priceSetId'])) { |
819 | CRM_Price_BAO_PriceField::priceSetValidation($values['priceSetId'], $values, $errorMsg); | |
820 | } | |
6a488035 TO |
821 | } |
822 | ||
823 | // validate contribution status for 'Failed'. | |
8cc574cf | 824 | if ($self->_onlinePendingContributionId && !empty($values['record_contribution']) && |
6a488035 TO |
825 | (CRM_Utils_Array::value('contribution_status_id', $values) == |
826 | array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
827 | ) | |
828 | ) { | |
829 | $errorMsg['contribution_status_id'] = ts('Please select a valid payment status before updating.'); | |
830 | } | |
831 | ||
832 | // do the amount validations. | |
833 | //skip for update mode since amount is freeze, CRM-6052 | |
8cc574cf | 834 | if ((!$self->_id && empty($values['total_amount']) && |
6a488035 TO |
835 | empty($self->_values['line_items']) |
836 | ) || | |
837 | ($self->_id && !$self->_paymentId && isset($self->_values['line_items']) && is_array($self->_values['line_items'])) | |
838 | ) { | |
839 | if ($priceSetId = CRM_Utils_Array::value('priceSetId', $values)) { | |
9da8dc8c | 840 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $values, $errorMsg, TRUE); |
6a488035 TO |
841 | } |
842 | } | |
4d3f1e42 | 843 | // For single additions - show validation error if the contact has already been registered |
c0558721 | 844 | // for this event. |
22e263ad | 845 | if ($self->_single && ($self->_action & CRM_Core_Action::ADD)) { |
c0558721 TS |
846 | if ($self->_context == 'standalone') { |
847 | $contactId = CRM_Utils_Array::value('contact_id', $values); | |
848 | } | |
849 | else { | |
850 | $contactId = $self->_contactId; | |
851 | } | |
852 | ||
4d3f1e42 | 853 | $eventId = CRM_Utils_Array::value('event_id', $values); |
22e263ad | 854 | if (!empty($contactId) && !empty($eventId)) { |
81767ac6 | 855 | $cancelledStatusID = CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Cancelled'); |
a130e045 | 856 | $dupeCheck = new CRM_Event_BAO_Participant(); |
4d3f1e42 JM |
857 | $dupeCheck->contact_id = $contactId; |
858 | $dupeCheck->event_id = $eventId; | |
81767ac6 | 859 | $dupeCheck->whereAdd("status_id != {$cancelledStatusID} "); |
4d3f1e42 | 860 | $dupeCheck->find(TRUE); |
22e263ad | 861 | if (!empty($dupeCheck->id)) { |
4d3f1e42 JM |
862 | $errorMsg['event_id'] = ts("This contact has already been assigned to this event."); |
863 | } | |
864 | } | |
865 | } | |
6a488035 TO |
866 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; |
867 | } | |
868 | ||
869 | /** | |
66f9e52b | 870 | * Process the form submission. |
6a488035 TO |
871 | */ |
872 | public function postProcess() { | |
873 | // get the submitted form values. | |
874 | $params = $this->controller->exportValues($this->_name); | |
875 | ||
876 | if ($this->_action & CRM_Core_Action::DELETE) { | |
877 | if (CRM_Utils_Array::value('delete_participant', $params) == 2) { | |
878 | $additionalId = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id)); | |
879 | $participantLinks = (CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId)); | |
880 | } | |
881 | if (CRM_Utils_Array::value('delete_participant', $params) == 1) { | |
882 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
883 | foreach ($additionalIds as $value) { | |
884 | CRM_Event_BAO_Participant::deleteParticipant($value); | |
885 | } | |
886 | } | |
887 | CRM_Event_BAO_Participant::deleteParticipant($this->_id); | |
888 | CRM_Core_Session::setStatus(ts('Selected participant was deleted successfully.'), ts('Record Deleted'), 'success'); | |
889 | if (!empty($participantLinks)) { | |
d3e86119 | 890 | $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 |
891 | CRM_Core_Session::setStatus($status, ts('Group Payment Deleted')); |
892 | } | |
893 | return; | |
894 | } | |
4d3f1e42 JM |
895 | // When adding a single contact, the formRule prevents you from adding duplicates |
896 | // (See above in formRule()). When adding more than one contact, the duplicates are | |
897 | // removed automatically and the user receives one notification. | |
898 | if ($this->_action & CRM_Core_Action::ADD) { | |
aae2f9e6 | 899 | $event_id = $this->_eventId; |
22e263ad | 900 | if (empty($event_id) && !empty($params['event_id'])) { |
aae2f9e6 JM |
901 | $event_id = $params['event_id']; |
902 | } | |
22e263ad | 903 | if (!$this->_single && !empty($event_id)) { |
4d3f1e42 | 904 | $duplicateContacts = 0; |
22e263ad | 905 | while (list($k, $dupeCheckContactId) = each($this->_contactIds)) { |
4d3f1e42 | 906 | // Eliminate contacts that have already been assigned to this event. |
a130e045 | 907 | $dupeCheck = new CRM_Event_BAO_Participant(); |
4d3f1e42 | 908 | $dupeCheck->contact_id = $dupeCheckContactId; |
aae2f9e6 | 909 | $dupeCheck->event_id = $event_id; |
4d3f1e42 | 910 | $dupeCheck->find(TRUE); |
22e263ad | 911 | if (!empty($dupeCheck->id)) { |
4d3f1e42 JM |
912 | $duplicateContacts++; |
913 | unset($this->_contactIds[$k]); | |
914 | } | |
915 | } | |
22e263ad | 916 | if ($duplicateContacts > 0) { |
4d3f1e42 JM |
917 | $msg = ts( |
918 | "%1 contacts have already been assigned to this event. They were not added a second time.", | |
919 | array(1 => $duplicateContacts) | |
920 | ); | |
921 | CRM_Core_Session::setStatus($msg); | |
922 | } | |
22e263ad | 923 | if (count($this->_contactIds) == 0) { |
4d3f1e42 JM |
924 | CRM_Core_Session::setStatus(ts("No participants were added.")); |
925 | return; | |
926 | } | |
aef8149f JM |
927 | // We have to re-key $this->_contactIds so each contact has the same |
928 | // key as their corresponding record in the $participants array that | |
929 | // will be created below. | |
930 | $this->_contactIds = array_values($this->_contactIds); | |
4d3f1e42 JM |
931 | } |
932 | } | |
933 | ||
5fda4b16 | 934 | $statusMsg = $this->submit($params); |
935 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
936 | $session = CRM_Core_Session::singleton(); | |
937 | $buttonName = $this->controller->getButtonName(); | |
938 | if ($this->_context == 'standalone') { | |
939 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
940 | $urlParams = 'reset=1&action=add&context=standalone'; | |
941 | if ($this->_mode) { | |
942 | $urlParams .= '&mode=' . $this->_mode; | |
943 | } | |
944 | if ($this->_eID) { | |
945 | $urlParams .= '&eid=' . $this->_eID; | |
946 | } | |
947 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); | |
948 | } | |
949 | else { | |
950 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
951 | "reset=1&cid={$this->_contactId}&selectedChild=participant" | |
952 | )); | |
953 | } | |
954 | } | |
955 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
956 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', | |
957 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" | |
958 | )); | |
959 | } | |
960 | } | |
961 | ||
962 | /** | |
963 | * Submit form. | |
964 | * | |
965 | * @param array $params | |
966 | * | |
967 | * @return array | |
968 | */ | |
969 | public function submit($params) { | |
aac57c29 | 970 | $participantStatus = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 971 | // set the contact, when contact is selected |
479696ed | 972 | if (!empty($params['contact_id'])) { |
0dc4ef42 | 973 | $this->_contactID = $this->_contactId = $params['contact_id']; |
6a488035 | 974 | } |
9da8dc8c | 975 | if ($this->_priceSetId && $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 TO |
976 | $this->_quickConfig = $isQuickConfig; |
977 | } | |
978 | ||
979 | if ($this->_id) { | |
980 | $params['id'] = $this->_id; | |
981 | } | |
982 | ||
983 | $config = CRM_Core_Config::singleton(); | |
1675ba6d PN |
984 | if (isset($params['total_amount'])) { |
985 | $params['total_amount'] = CRM_Utils_Rule::cleanMoney($params['total_amount']); | |
986 | } | |
6a488035 TO |
987 | if ($this->_isPaidEvent) { |
988 | ||
989 | $contributionParams = array(); | |
990 | $lineItem = array(); | |
991 | $additionalParticipantDetails = array(); | |
8cf6bd83 PN |
992 | if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) { |
993 | $eventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'start_date'); | |
a493ffb6 | 994 | if (strtotime($eventStartDate) > strtotime(date('Ymt'))) { |
8cf6bd83 PN |
995 | $contributionParams['revenue_recognition_date'] = date('Ymd', strtotime($eventStartDate)); |
996 | } | |
997 | } | |
6a488035 TO |
998 | if (($this->_id && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId) { |
999 | $participantBAO = new CRM_Event_BAO_Participant(); | |
1000 | $participantBAO->id = $this->_id; | |
1001 | $participantBAO->find(TRUE); | |
1002 | $contributionParams['total_amount'] = $participantBAO->fee_amount; | |
1003 | ||
1004 | $params['discount_id'] = NULL; | |
1005 | //re-enter the values for UPDATE mode | |
1006 | $params['fee_level'] = $params['amount_level'] = $participantBAO->fee_level; | |
1007 | $params['fee_amount'] = $participantBAO->fee_amount; | |
1008 | if (isset($params['priceSetId'])) { | |
1009 | $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id); | |
1010 | } | |
1011 | //also add additional participant's fee level/priceset | |
1012 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
1013 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
1014 | $hasLineItems = CRM_Utils_Array::value('priceSetId', $params, FALSE); | |
1015 | $additionalParticipantDetails = CRM_Event_BAO_Participant::getFeeDetails($additionalIds, | |
1016 | $hasLineItems | |
1017 | ); | |
1018 | } | |
1019 | } | |
1020 | else { | |
1021 | ||
1022 | //check if discount is selected | |
a7488080 | 1023 | if (!empty($params['discount_id'])) { |
6a488035 TO |
1024 | $discountId = $params['discount_id']; |
1025 | } | |
1026 | else { | |
1027 | $discountId = $params['discount_id'] = 'null'; | |
1028 | } | |
1029 | ||
1030 | //lets carry currency, CRM-4453 | |
1031 | $params['fee_currency'] = $config->defaultCurrency; | |
f660d2ad | 1032 | if (!isset($lineItem[0])) { |
1033 | $lineItem[0] = array(); | |
1034 | } | |
9da8dc8c | 1035 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], |
8567d0f8 PN |
1036 | $params, $lineItem[0] |
1037 | ); | |
6a488035 TO |
1038 | //CRM-11529 for quick config backoffice transactions |
1039 | //when financial_type_id is passed in form, update the | |
1040 | //lineitems with the financial type selected in form | |
1041 | $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $params); | |
1042 | $isPaymentRecorded = CRM_Utils_Array::value('record_contribution', $params); | |
1043 | if ($isPaymentRecorded && $this->_quickConfig && $submittedFinancialType) { | |
1044 | foreach ($lineItem[0] as &$values) { | |
1045 | $values['financial_type_id'] = $submittedFinancialType; | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | $params['fee_level'] = $params['amount_level']; | |
1050 | $contributionParams['total_amount'] = $params['amount']; | |
f6bae84f | 1051 | if ($this->_quickConfig && !empty($params['total_amount']) && |
353ffa53 TO |
1052 | $params['status_id'] != array_search('Partially paid', $participantStatus) |
1053 | ) { | |
6a488035 | 1054 | $params['fee_amount'] = $params['total_amount']; |
0db6c3e1 TO |
1055 | } |
1056 | else { | |
6a488035 TO |
1057 | //fix for CRM-3086 |
1058 | $params['fee_amount'] = $params['amount']; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | if (isset($params['priceSetId'])) { | |
1063 | if (!empty($lineItem[0])) { | |
1064 | $this->set('lineItem', $lineItem); | |
1065 | ||
353ffa53 TO |
1066 | $this->_lineItem = $lineItem; |
1067 | $lineItem = array_merge($lineItem, $additionalParticipantDetails); | |
6a488035 TO |
1068 | |
1069 | $participantCount = array(); | |
1070 | foreach ($lineItem as $k) { | |
1071 | foreach ($k as $v) { | |
1072 | if (CRM_Utils_Array::value('participant_count', $v) > 0) { | |
1073 | $participantCount[] = $v['participant_count']; | |
1074 | } | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | if (isset($participantCount)) { | |
1079 | $this->assign('pricesetFieldsCount', $participantCount); | |
1080 | } | |
0479b4c8 | 1081 | $this->assign('lineItem', empty($lineItem[0]) || $this->_quickConfig ? FALSE : $lineItem); |
6a488035 TO |
1082 | } |
1083 | else { | |
1084 | $this->assign('amount_level', $params['amount_level']); | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | $this->_params = $params; | |
5d3a2b9f | 1089 | parent::beginPostProcess(); |
fbc54416 PJ |
1090 | $amountOwed = NULL; |
1091 | if (isset($params['amount'])) { | |
1092 | $amountOwed = $params['amount']; | |
1093 | unset($params['amount']); | |
1094 | } | |
6a488035 | 1095 | $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']); |
53f56880 | 1096 | $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params), CRM_Utils_Array::value('receive_date_time', $params)); |
6a488035 TO |
1097 | $params['contact_id'] = $this->_contactId; |
1098 | ||
1099 | // overwrite actual payment amount if entered | |
a7488080 | 1100 | if (!empty($params['total_amount'])) { |
6a488035 TO |
1101 | $contributionParams['total_amount'] = CRM_Utils_Array::value('total_amount', $params); |
1102 | } | |
1103 | ||
6a488035 | 1104 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email |
46929486 | 1105 | $userName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName(); |
6a488035 TO |
1106 | |
1107 | if ($this->_contactId) { | |
1108 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); | |
1109 | } | |
1110 | ||
7df021e5 | 1111 | //modify params according to parameter used in create |
1112 | //participant method (addParticipant) | |
1113 | $this->_params['participant_status_id'] = $params['status_id']; | |
1114 | $this->_params['participant_role_id'] = is_array($params['role_id']) ? $params['role_id'] : explode(',', $params['role_id']); | |
1115 | $this->_params['participant_register_date'] = $params['register_date']; | |
1116 | $roleIdWithSeparator = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['participant_role_id']); | |
1117 | ||
6a488035 TO |
1118 | if ($this->_mode) { |
1119 | if (!$this->_isPaidEvent) { | |
1120 | CRM_Core_Error::fatal(ts('Selected Event is not Paid Event ')); | |
1121 | } | |
6a488035 | 1122 | |
a130e045 DG |
1123 | $eventTitle |
1124 | = CRM_Core_DAO::getFieldValue( | |
6a488035 TO |
1125 | 'CRM_Event_DAO_Event', |
1126 | $params['event_id'], | |
1127 | 'title' | |
1128 | ); | |
1129 | ||
1130 | // set source if not set | |
1131 | if (empty($params['source'])) { | |
353ffa53 TO |
1132 | $this->_params['participant_source'] = ts('Offline Registration for Event: %2 by: %1', array( |
1133 | 1 => $userName, | |
a130e045 | 1134 | 2 => $eventTitle, |
353ffa53 | 1135 | )); |
6a488035 TO |
1136 | } |
1137 | else { | |
03e04002 | 1138 | $this->_params['participant_source'] = $params['source']; |
6a488035 TO |
1139 | } |
1140 | $this->_params['description'] = $this->_params['participant_source']; | |
1141 | ||
1142 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], | |
1143 | $this->_mode | |
1144 | ); | |
1145 | $now = date('YmdHis'); | |
1146 | $fields = array(); | |
1147 | ||
1148 | // set email for primary location. | |
1149 | $fields['email-Primary'] = 1; | |
1150 | $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail; | |
1151 | ||
1152 | $params['register_date'] = $now; | |
1153 | ||
1154 | // now set the values for the billing location. | |
1155 | foreach ($this->_fields as $name => $dontCare) { | |
1156 | $fields[$name] = 1; | |
1157 | } | |
1158 | ||
1159 | // also add location name to the array | |
a130e045 DG |
1160 | $params["address_name-{$this->_bltID}"] |
1161 | = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . | |
6a488035 TO |
1162 | CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . |
1163 | CRM_Utils_Array::value('billing_last_name', $params); | |
1164 | ||
1165 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); | |
1166 | $fields["address_name-{$this->_bltID}"] = 1; | |
1167 | $fields["email-{$this->_bltID}"] = 1; | |
1168 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); | |
1169 | ||
1170 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1171 | ||
1172 | foreach ($nameFields as $name) { | |
1173 | $fields[$name] = 1; | |
1174 | if (array_key_exists("billing_$name", $params)) { | |
1175 | $params[$name] = $params["billing_{$name}"]; | |
1176 | $params['preserveDBName'] = TRUE; | |
1177 | } | |
1178 | } | |
1179 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); | |
1180 | } | |
1181 | ||
e03317f1 | 1182 | if (!empty($this->_params['participant_role_id'])) { |
7df021e5 | 1183 | $customFieldsRole = array(); |
1184 | foreach ($this->_params['participant_role_id'] as $roleKey) { | |
1185 | $customFieldsRole = CRM_Utils_Array::crmArrayMerge(CRM_Core_BAO_CustomField::getFields('Participant', | |
1186 | FALSE, FALSE, $roleKey, $this->_roleCustomDataTypeID), $customFieldsRole); | |
6a488035 | 1187 | } |
7df021e5 | 1188 | $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', |
1189 | FALSE, | |
1190 | FALSE, | |
1191 | CRM_Utils_Array::value('event_id', $params), | |
1192 | $this->_eventNameCustomDataTypeID | |
1193 | ); | |
1194 | $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', | |
1195 | FALSE, | |
1196 | FALSE, | |
1197 | $this->_eventTypeId, | |
1198 | $this->_eventTypeCustomDataTypeID | |
1199 | ); | |
1200 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, | |
1201 | CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE) | |
1202 | ); | |
1203 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields); | |
1204 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields); | |
1205 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
7df021e5 | 1206 | $this->_id, |
1207 | 'Participant' | |
1208 | ); | |
6a488035 TO |
1209 | } |
1210 | ||
1211 | //do cleanup line items if participant edit the Event Fee. | |
0479b4c8 | 1212 | if (($this->_lineItem || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) { |
6a488035 TO |
1213 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant'); |
1214 | } | |
03e04002 | 1215 | |
6a488035 | 1216 | if ($this->_mode) { |
dc8cdf3a | 1217 | // add all the additional payment params we need |
6a488035 TO |
1218 | $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); |
1219 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1220 | ||
6a488035 TO |
1221 | $this->_params['amount'] = $params['fee_amount']; |
1222 | $this->_params['amount_level'] = $params['amount_level']; | |
1223 | $this->_params['currencyID'] = $config->defaultCurrency; | |
6a488035 TO |
1224 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); |
1225 | ||
1226 | // at this point we've created a contact and stored its address etc | |
1227 | // all the payment processors expect the name and address to be in the | |
1228 | // so we copy stuff over to first_name etc. | |
1229 | $paymentParams = $this->_params; | |
a7488080 | 1230 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1231 | $paymentParams['email'] = $this->_contributorEmail; |
1232 | } | |
e7309307 EM |
1233 | |
1234 | // The only reason for merging in the 'contact_id' rather than ensuring it is set | |
1235 | // is that this patch is being done around the time of the stable release | |
1236 | // so more conservative approach is called for. | |
1237 | // In fact the use of $params and $this->_params & $this->_contactId vs $contactID | |
1238 | // needs rationalising. | |
1239 | $mapParams = array_merge(array('contact_id' => $contactID), $this->_params); | |
1240 | CRM_Core_Payment_Form::mapParams($this->_bltID, $mapParams, $paymentParams, TRUE); | |
6a488035 | 1241 | |
077017db | 1242 | $payment = $this->_paymentProcessor['object']; |
6a488035 | 1243 | |
9b7e7d4c PH |
1244 | // CRM-15622: fix for incorrect contribution.fee_amount |
1245 | $paymentParams['fee_amount'] = NULL; | |
ce2511f7 | 1246 | $result = $payment->doPayment($paymentParams); |
6a488035 TO |
1247 | |
1248 | if (is_a($result, 'CRM_Core_Error')) { | |
1249 | CRM_Core_Error::displaySessionError($result); | |
1250 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/participant', | |
353ffa53 TO |
1251 | "reset=1&action=add&cid={$this->_contactId}&context=participant&mode={$this->_mode}" |
1252 | )); | |
6a488035 TO |
1253 | } |
1254 | ||
1255 | if ($result) { | |
1256 | $this->_params = array_merge($this->_params, $result); | |
1257 | } | |
1258 | ||
1259 | $this->_params['receive_date'] = $now; | |
1260 | ||
a7488080 | 1261 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1262 | $this->_params['receipt_date'] = $now; |
1263 | } | |
1264 | else { | |
1265 | $this->_params['receipt_date'] = NULL; | |
1266 | } | |
1267 | ||
1268 | $this->set('params', $this->_params); | |
1269 | $this->assign('trxn_id', $result['trxn_id']); | |
1270 | $this->assign('receive_date', | |
1271 | CRM_Utils_Date::processDate($this->_params['receive_date']) | |
1272 | ); | |
1273 | ||
1274 | //add contribution record | |
a130e045 DG |
1275 | $this->_params['financial_type_id'] |
1276 | = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'financial_type_id'); | |
6a488035 TO |
1277 | $this->_params['mode'] = $this->_mode; |
1278 | ||
431c430b | 1279 | //add contribution record |
faf44167 | 1280 | $contributions[] = $contribution = CRM_Event_Form_Registration_Confirm::processContribution( |
1281 | $this, $this->_params, | |
1282 | $result, $contactID, | |
1283 | FALSE, FALSE, | |
1284 | $this->_paymentProcessor | |
1285 | ); | |
6a488035 TO |
1286 | |
1287 | // add participant record | |
1288 | $participants = array(); | |
dc8cdf3a K |
1289 | if (!empty($this->_params['role_id']) && is_array($this->_params['role_id'])) { |
1290 | $this->_params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1291 | $this->_params['role_id'] | |
6a488035 TO |
1292 | ); |
1293 | } | |
0e81467c | 1294 | |
5c320924 | 1295 | //CRM-15372 patch to fix fee amount replacing amount |
0479b4c8 | 1296 | $this->_params['fee_amount'] = $this->_params['amount']; |
5c320924 | 1297 | |
bf2c70af | 1298 | $participants[] = CRM_Event_Form_Registration::addParticipant($this, $contactID); |
6a488035 TO |
1299 | |
1300 | //add custom data for participant | |
1301 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, | |
6a488035 TO |
1302 | 'civicrm_participant', |
1303 | $participants[0]->id, | |
1304 | 'Participant' | |
1305 | ); | |
1306 | //add participant payment | |
1307 | $paymentParticipant = array( | |
1308 | 'participant_id' => $participants[0]->id, | |
1309 | 'contribution_id' => $contribution->id, | |
1310 | ); | |
1311 | $ids = array(); | |
1312 | ||
1313 | CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids); | |
1314 | $this->_contactIds[] = $this->_contactId; | |
1315 | } | |
1316 | else { | |
1317 | $participants = array(); | |
1318 | if ($this->_single) { | |
1319 | if ($params['role_id']) { | |
7df021e5 | 1320 | $params['role_id'] = $roleIdWithSeparator; |
6a488035 TO |
1321 | } |
1322 | else { | |
1323 | $params['role_id'] = 'NULL'; | |
1324 | } | |
1325 | $participants[] = CRM_Event_BAO_Participant::create($params); | |
1326 | } | |
1327 | else { | |
1328 | foreach ($this->_contactIds as $contactID) { | |
1329 | $commonParams = $params; | |
1330 | $commonParams['contact_id'] = $contactID; | |
1331 | if ($commonParams['role_id']) { | |
dc8cdf3a | 1332 | $commonParams['role_id'] = $commonParams['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); |
6a488035 TO |
1333 | } |
1334 | else { | |
1335 | $commonParams['role_id'] = 'NULL'; | |
1336 | } | |
1337 | $participants[] = CRM_Event_BAO_Participant::create($commonParams); | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | if (isset($params['event_id'])) { | |
1342 | $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', | |
1343 | $params['event_id'], | |
1344 | 'title' | |
1345 | ); | |
1346 | } | |
1347 | ||
1348 | if ($this->_single) { | |
1349 | $this->_contactIds[] = $this->_contactId; | |
1350 | } | |
1351 | ||
1352 | $contributions = array(); | |
a7488080 CW |
1353 | if (!empty($params['record_contribution'])) { |
1354 | if (!empty($params['id'])) { | |
6a488035 TO |
1355 | if ($this->_onlinePendingContributionId) { |
1356 | $ids['contribution'] = $this->_onlinePendingContributionId; | |
1357 | } | |
1358 | else { | |
1359 | $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
1360 | $params['id'], | |
1361 | 'contribution_id', | |
1362 | 'participant_id' | |
1363 | ); | |
1364 | } | |
1365 | } | |
1366 | unset($params['note']); | |
1367 | ||
1368 | //build contribution params | |
1369 | if (!$this->_onlinePendingContributionId) { | |
0e81467c | 1370 | if (empty($params['source'])) { |
353ffa53 TO |
1371 | $contributionParams['source'] = ts('%1 : Offline registration (by %2)', array( |
1372 | 1 => $eventTitle, | |
a130e045 | 1373 | 2 => $userName, |
353ffa53 | 1374 | )); |
0e81467c EM |
1375 | } |
1376 | else { | |
1377 | $contributionParams['source'] = $params['source']; | |
1378 | } | |
1379 | } | |
6a488035 TO |
1380 | |
1381 | $contributionParams['currency'] = $config->defaultCurrency; | |
1382 | $contributionParams['non_deductible_amount'] = 'null'; | |
0d8afee2 | 1383 | $contributionParams['receipt_date'] = !empty($params['send_receipt']) ? CRM_Utils_Array::value('receive_date', $params) : 'null'; |
0dc4ef42 | 1384 | $contributionParams['contact_id'] = $this->_contactID; |
1385 | // @todo change receive_date to datepicker field. Strip out all wrangling. | |
1386 | $contributionParams['receive_date'] = $params['receive_date']; | |
6a488035 | 1387 | |
353ffa53 | 1388 | $recordContribution = array( |
353ffa53 TO |
1389 | 'financial_type_id', |
1390 | 'payment_instrument_id', | |
1391 | 'trxn_id', | |
1392 | 'contribution_status_id', | |
353ffa53 TO |
1393 | 'check_number', |
1394 | 'campaign_id', | |
0dc4ef42 | 1395 | 'pan_truncation', |
5d3a2b9f | 1396 | 'card_type_id', |
6a488035 TO |
1397 | ); |
1398 | ||
1399 | foreach ($recordContribution as $f) { | |
0dc4ef42 | 1400 | $contributionParams[$f] = CRM_Utils_Array::value($f, $this->_params); |
6a488035 TO |
1401 | if ($f == 'trxn_id') { |
1402 | $this->assign('trxn_id', $contributionParams[$f]); | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | //insert financial type name in receipt. | |
b956b310 | 1407 | $this->assign('financialTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', |
1408 | $contributionParams['financial_type_id'])); | |
1409 | // legacy support | |
6a488035 TO |
1410 | $this->assign('contributionTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id'])); |
1411 | $contributionParams['skipLineItem'] = 1; | |
7030a002 PN |
1412 | if ($this->_id) { |
1413 | $contributionParams['contribution_mode'] = 'participant'; | |
1414 | $contributionParams['participant_id'] = $this->_id; | |
1415 | } | |
6a488035 | 1416 | // Set is_pay_later flag for back-office offline Pending status contributions |
2f48e713 | 1417 | if ($contributionParams['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Pending')) { |
6a488035 TO |
1418 | $contributionParams['is_pay_later'] = 1; |
1419 | } | |
2f48e713 | 1420 | elseif ($contributionParams['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Completed')) { |
d75f2f47 EM |
1421 | $contributionParams['is_pay_later'] = 0; |
1422 | } | |
f8325309 | 1423 | |
aac57c29 | 1424 | if ($params['status_id'] == array_search('Partially paid', $participantStatus)) { |
5e494d5c PJ |
1425 | if (!$amountOwed && $this->_action & CRM_Core_Action::UPDATE) { |
1426 | $amountOwed = $params['fee_amount']; | |
1427 | } | |
1428 | ||
356579e9 | 1429 | // if multiple participants are link, consider contribution total amount as the amount Owed |
ea707c1b | 1430 | if ($this->_id && CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { |
356579e9 PJ |
1431 | $amountOwed = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', |
1432 | $ids['contribution'], | |
1433 | 'total_amount' | |
1434 | ); | |
1435 | } | |
1436 | ||
aac57c29 PJ |
1437 | // CRM-13964 partial_payment_total |
1438 | if ($amountOwed > $params['total_amount']) { | |
1439 | // the owed amount | |
1440 | $contributionParams['partial_payment_total'] = $amountOwed; | |
1441 | // the actual amount paid | |
f49cdeab | 1442 | $contributionParams['partial_amount_to_pay'] = $params['total_amount']; |
aac57c29 | 1443 | } |
f8325309 | 1444 | } |
372ad74d | 1445 | |
d91b8b33 | 1446 | if (CRM_Utils_Array::value('tax_amount', $this->_params)) { |
1447 | $contributionParams['tax_amount'] = $this->_params['tax_amount']; | |
1448 | } | |
1449 | ||
6a488035 | 1450 | if ($this->_single) { |
dbd48d4e | 1451 | if (empty($ids)) { |
1452 | $ids = array(); | |
1453 | } | |
6a488035 TO |
1454 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); |
1455 | } | |
1456 | else { | |
1457 | $ids = array(); | |
1458 | foreach ($this->_contactIds as $contactID) { | |
1459 | $contributionParams['contact_id'] = $contactID; | |
1460 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); | |
1461 | } | |
1462 | } | |
1463 | ||
1464 | //insert payment record for this participation | |
dbd48d4e | 1465 | if (empty($ids['contribution'])) { |
6a488035 TO |
1466 | foreach ($this->_contactIds as $num => $contactID) { |
1467 | $ppDAO = new CRM_Event_DAO_ParticipantPayment(); | |
1468 | $ppDAO->participant_id = $participants[$num]->id; | |
1469 | $ppDAO->contribution_id = $contributions[$num]->id; | |
1470 | $ppDAO->save(); | |
1471 | } | |
1472 | } | |
1473 | // next create the transaction record | |
1474 | $transaction = new CRM_Core_Transaction(); | |
1475 | ||
1476 | // CRM-11124 | |
362bd1b7 | 1477 | if ($this->_params['discount_id']) { |
1478 | CRM_Event_BAO_Participant::createDiscountTrxn( | |
1479 | $this->_eventId, | |
1480 | $contributionParams, | |
1481 | NULL, | |
1482 | CRM_Price_BAO_PriceSet::parseFirstPriceSetValueIDFromParams($this->_params) | |
1483 | ); | |
6a488035 TO |
1484 | } |
1485 | $transaction->commit(); | |
1486 | } | |
1487 | } | |
1488 | ||
1489 | // also store lineitem stuff here | |
1490 | if ((($this->_lineItem & $this->_action & CRM_Core_Action::ADD) || | |
1491 | ($this->_lineItem && CRM_Core_Action::UPDATE && !$this->_paymentId)) | |
1492 | ) { | |
1493 | foreach ($this->_contactIds as $num => $contactID) { | |
1494 | foreach ($this->_lineItem as $key => $value) { | |
1495 | if (is_array($value) && $value != 'skip') { | |
1496 | foreach ($value as $lineKey => $line) { | |
aac57c29 | 1497 | //10117 update the line items for participants if contribution amount is recorded |
f6bae84f | 1498 | if ($this->_quickConfig && !empty($params['total_amount']) && |
372ad74d | 1499 | ($params['status_id'] != array_search('Partially paid', $participantStatus)) |
aac57c29 PJ |
1500 | ) { |
1501 | $line['unit_price'] = $line['line_total'] = $params['total_amount']; | |
d91b8b33 | 1502 | if (!empty($params['tax_amount'])) { |
1503 | $line['unit_price'] = $line['unit_price'] - $params['tax_amount']; | |
ff9b6b50 | 1504 | $line['line_total'] = $line['line_total'] - $params['tax_amount']; |
d91b8b33 | 1505 | } |
aac57c29 | 1506 | } |
6a488035 TO |
1507 | $lineItem[$this->_priceSetId][$lineKey] = $line; |
1508 | } | |
1509 | CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); | |
955ee56e | 1510 | CRM_Contribute_BAO_Contribution::addPayments($contributions); |
6a488035 TO |
1511 | } |
1512 | } | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | $updateStatusMsg = NULL; | |
1517 | //send mail when participant status changed, CRM-4326 | |
1518 | if ($this->_id && $this->_statusId && | |
353ffa53 TO |
1519 | $this->_statusId != CRM_Utils_Array::value('status_id', $params) && !empty($params['is_notify']) |
1520 | ) { | |
6a488035 TO |
1521 | |
1522 | $updateStatusMsg = CRM_Event_BAO_Participant::updateStatusMessage($this->_id, | |
1523 | $params['status_id'], | |
1524 | $this->_statusId | |
1525 | ); | |
1526 | } | |
1527 | ||
1528 | $sent = array(); | |
1529 | $notSent = array(); | |
a7488080 | 1530 | if (!empty($params['send_receipt'])) { |
6a488035 TO |
1531 | if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { |
1532 | $receiptFrom = $params['from_email_address']; | |
1533 | } | |
1534 | ||
1535 | $this->assign('module', 'Event Registration'); | |
1536 | //use of the message template below requires variables in different format | |
1537 | $event = $events = array(); | |
408cac48 | 1538 | $returnProperties = array('event_type_id', 'fee_label', 'start_date', 'end_date', 'is_show_location', 'title'); |
6a488035 TO |
1539 | |
1540 | //get all event details. | |
1541 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties); | |
1542 | $event = $events[$params['event_id']]; | |
1543 | unset($event['start_date']); | |
1544 | unset($event['end_date']); | |
1545 | ||
1546 | $role = CRM_Event_PseudoConstant::participantRole(); | |
1547 | $participantRoles = CRM_Utils_Array::value('role_id', $params); | |
1548 | if (is_array($participantRoles)) { | |
1549 | $selectedRoles = array(); | |
c7101803 | 1550 | foreach ($participantRoles as $roleId) { |
6a488035 TO |
1551 | $selectedRoles[] = $role[$roleId]; |
1552 | } | |
1553 | $event['participant_role'] = implode(', ', $selectedRoles); | |
1554 | } | |
1555 | else { | |
1556 | $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role); | |
1557 | } | |
1558 | $event['is_monetary'] = $this->_isPaidEvent; | |
1559 | ||
1560 | if ($params['receipt_text']) { | |
1561 | $event['confirm_email_text'] = $params['receipt_text']; | |
1562 | } | |
1563 | ||
1564 | $this->assign('isAmountzero', 1); | |
1565 | $this->assign('event', $event); | |
1566 | ||
1567 | $this->assign('isShowLocation', $event['is_show_location']); | |
1568 | if (CRM_Utils_Array::value('is_show_location', $event) == 1) { | |
1569 | $locationParams = array( | |
1570 | 'entity_id' => $params['event_id'], | |
1571 | 'entity_table' => 'civicrm_event', | |
1572 | ); | |
1573 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); | |
1574 | $this->assign('location', $location); | |
1575 | } | |
1576 | ||
1577 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1578 | if ($this->_isPaidEvent) { | |
1579 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1580 | if (!$this->_mode) { | |
1581 | if (isset($params['payment_instrument_id'])) { | |
1582 | $this->assign('paidBy', | |
1583 | CRM_Utils_Array::value($params['payment_instrument_id'], | |
1584 | $paymentInstrument | |
1585 | ) | |
1586 | ); | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | $this->assign('totalAmount', $contributionParams['total_amount']); | |
81f3d017 PJ |
1591 | if (isset($contributionParams['partial_payment_total'])) { |
1592 | // balance amount | |
f49cdeab | 1593 | $balanceAmount = $contributionParams['partial_payment_total'] - $contributionParams['partial_amount_to_pay']; |
481a74f4 | 1594 | $this->assign('balanceAmount', $balanceAmount); |
81f3d017 | 1595 | } |
6a488035 TO |
1596 | $this->assign('isPrimary', 1); |
1597 | $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); | |
1598 | } | |
1599 | if ($this->_mode) { | |
0b50eca0 | 1600 | $this->assignBillingName($params); |
1601 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( | |
1602 | $this->_params, | |
1603 | $this->_bltID | |
1604 | )); | |
6a488035 TO |
1605 | |
1606 | $date = CRM_Utils_Date::format($params['credit_card_exp_date']); | |
1607 | $date = CRM_Utils_Date::mysqlToIso($date); | |
1608 | $this->assign('credit_card_exp_date', $date); | |
1609 | $this->assign('credit_card_number', | |
1610 | CRM_Utils_System::mungeCreditCard($params['credit_card_number']) | |
1611 | ); | |
1612 | $this->assign('credit_card_type', $params['credit_card_type']); | |
0f2b049e | 1613 | // The concept of contributeMode is deprecated. |
6a488035 TO |
1614 | $this->assign('contributeMode', 'direct'); |
1615 | $this->assign('isAmountzero', 0); | |
1616 | $this->assign('is_pay_later', 0); | |
1617 | $this->assign('isPrimary', 1); | |
1618 | } | |
1619 | ||
1620 | $this->assign('register_date', $params['register_date']); | |
1621 | if ($params['receive_date']) { | |
1622 | $this->assign('receive_date', $params['receive_date']); | |
1623 | } | |
1624 | ||
1625 | $participant = array(array('participant_id', '=', $participants[0]->id, 0, 0)); | |
1626 | // check whether its a test drive ref CRM-3075 | |
a7488080 | 1627 | if (!empty($this->_defaultValues['is_test'])) { |
6a488035 TO |
1628 | $participant[] = array('participant_test', '=', 1, 0, 0); |
1629 | } | |
1630 | ||
1631 | $template = CRM_Core_Smarty::singleton(); | |
1632 | $customGroup = array(); | |
1633 | //format submitted data | |
1634 | foreach ($params['custom'] as $fieldID => $values) { | |
1635 | foreach ($values as $fieldValue) { | |
af6de2f1 JP |
1636 | $isPublic = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $fieldValue['custom_group_id'], 'is_public'); |
1637 | if ($isPublic) { | |
1638 | $customFields[$fieldID]['id'] = $fieldID; | |
1639 | $formattedValue = CRM_Core_BAO_CustomField::displayValue($fieldValue['value'], $fieldID, $participants[0]->id); | |
1640 | $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); | |
1641 | } | |
6a488035 TO |
1642 | } |
1643 | } | |
1644 | ||
1645 | foreach ($this->_contactIds as $num => $contactID) { | |
1646 | // Retrieve the name and email of the contact - this will be the TO for receipt email | |
1647 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
1648 | ||
1649 | $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; | |
1650 | ||
1651 | $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
1652 | if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { | |
1653 | $this->assign('isOnWaitlist', TRUE); | |
1654 | } | |
1655 | ||
1656 | $this->assign('customGroup', $customGroup); | |
1657 | $this->assign('contactID', $contactID); | |
1658 | $this->assign('participantID', $participants[$num]->id); | |
1659 | ||
1660 | $this->_id = $participants[$num]->id; | |
1661 | ||
1662 | if ($this->_isPaidEvent) { | |
1663 | // fix amount for each of participants ( for bulk mode ) | |
1664 | $eventAmount = array(); | |
aaffa79f | 1665 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
03b412ae | 1666 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
79d001a2 | 1667 | $totalTaxAmount = 0; |
0e81467c | 1668 | |
03b412ae | 1669 | //add dataArray in the receipts in ADD and UPDATE condition |
79d001a2 PB |
1670 | $dataArray = array(); |
1671 | if ($this->_action & CRM_Core_Action::ADD) { | |
f660d2ad | 1672 | $line = isset($lineItem[0]) ? $lineItem[0] : array(); |
79d001a2 PB |
1673 | } |
1674 | elseif ($this->_action & CRM_Core_Action::UPDATE) { | |
1675 | $line = $this->_values['line_items']; | |
1676 | } | |
03b412ae PB |
1677 | if ($invoicing) { |
1678 | foreach ($line as $key => $value) { | |
1679 | if (isset($value['tax_amount'])) { | |
1680 | $totalTaxAmount += $value['tax_amount']; | |
288ec043 PB |
1681 | if (isset($dataArray[(string) $value['tax_rate']])) { |
1682 | $dataArray[(string) $value['tax_rate']] = $dataArray[(string) $value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); | |
03b412ae PB |
1683 | } |
1684 | else { | |
288ec043 | 1685 | $dataArray[(string) $value['tax_rate']] = CRM_Utils_Array::value('tax_amount', $value); |
03b412ae | 1686 | } |
79d001a2 PB |
1687 | } |
1688 | } | |
03b412ae | 1689 | $this->assign('totalTaxAmount', $totalTaxAmount); |
01604562 | 1690 | $this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings)); |
03b412ae | 1691 | $this->assign('dataArray', $dataArray); |
79d001a2 | 1692 | } |
6a488035 TO |
1693 | if (!empty($additionalParticipantDetails)) { |
1694 | $params['amount_level'] = preg_replace('/\ 1/', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; | |
1695 | } | |
1696 | ||
1697 | $eventAmount[$num] = array( | |
1698 | 'label' => preg_replace('/\ 1/', '', $params['amount_level']), | |
1699 | 'amount' => $params['fee_amount'], | |
1700 | ); | |
1701 | //as we are using same template for online & offline registration. | |
1702 | //So we have to build amount as array. | |
1703 | $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); | |
1704 | $this->assign('amount', $eventAmount); | |
1705 | } | |
1706 | ||
1707 | $sendTemplateParams = array( | |
1708 | 'groupName' => 'msg_tpl_workflow_event', | |
1709 | 'valueName' => 'event_offline_receipt', | |
1710 | 'contactId' => $contactID, | |
52f3b44b | 1711 | 'isTest' => !empty($this->_defaultValues['is_test']), |
92fcb95f | 1712 | 'PDFFilename' => ts('confirmation') . '.pdf', |
6a488035 TO |
1713 | ); |
1714 | ||
1715 | // try to send emails only if email id is present | |
1716 | // and the do-not-email option is not checked for that contact | |
1717 | if ($this->_contributorEmail and !$this->_toDoNotEmail) { | |
1718 | $sendTemplateParams['from'] = $receiptFrom; | |
1719 | $sendTemplateParams['toName'] = $this->_contributorDisplayName; | |
1720 | $sendTemplateParams['toEmail'] = $this->_contributorEmail; | |
1721 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails); | |
1722 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails); | |
1723 | } | |
1724 | ||
9161952c | 1725 | //send email with pdf invoice |
481a74f4 | 1726 | $template = CRM_Core_Smarty::singleton(); |
9161952c PD |
1727 | $taxAmt = $template->get_template_vars('dataArray'); |
1728 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
353ffa53 TO |
1729 | $this->_id, 'contribution_id', 'participant_id' |
1730 | ); | |
aaffa79f | 1731 | $prefixValue = Civi::settings()->get('contribution_invoice_settings'); |
9161952c PD |
1732 | $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); |
1733 | if (count($taxAmt) > 0 && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { | |
0479b4c8 | 1734 | $sendTemplateParams['isEmailPdf'] = TRUE; |
0e81467c | 1735 | $sendTemplateParams['contributionId'] = $contributionId; |
9161952c | 1736 | } |
c6327d7d | 1737 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
1738 | if ($mailSent) { |
1739 | $sent[] = $contactID; | |
1740 | foreach ($participants as $ids => $values) { | |
1741 | if ($values->contact_id == $contactID) { | |
641f812f | 1742 | $values->details = CRM_Utils_Array::value('receipt_text', $params); |
6a488035 TO |
1743 | CRM_Activity_BAO_Activity::addActivity($values, 'Email'); |
1744 | break; | |
1745 | } | |
1746 | } | |
1747 | } | |
1748 | else { | |
1749 | $notSent[] = $contactID; | |
1750 | } | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | // set the participant id if it is not set | |
1755 | if (!$this->_id) { | |
1756 | $this->_id = $participants[0]->id; | |
1757 | } | |
1758 | ||
5fda4b16 | 1759 | return $this->getStatusMsg($params, $sent, $updateStatusMsg, $notSent); |
1760 | } | |
1761 | ||
1762 | /** | |
1763 | * Set the various IDs relating to custom data types. | |
1764 | */ | |
1765 | public function setCustomDataTypes() { | |
1766 | $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name'); | |
1767 | $this->_roleCustomDataTypeID = array_search('ParticipantRole', $customDataType); | |
1768 | $this->_eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType); | |
1769 | $this->_eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType); | |
1770 | $this->assign('roleCustomDataTypeID', $this->_roleCustomDataTypeID); | |
1771 | $this->assign('eventNameCustomDataTypeID', $this->_eventNameCustomDataTypeID); | |
1772 | $this->assign('eventTypeCustomDataTypeID', $this->_eventTypeCustomDataTypeID); | |
1773 | } | |
1774 | ||
1775 | /** | |
1776 | * Get status message | |
1777 | * | |
1778 | * @param array $params | |
1779 | * @param int $sent | |
1780 | * @param string $updateStatusMsg | |
1781 | * @param int $notSent | |
1782 | * | |
1783 | * @return string | |
1784 | */ | |
1785 | protected function getStatusMsg($params, $sent, $updateStatusMsg, $notSent) { | |
1786 | $statusMsg = ''; | |
6a488035 TO |
1787 | if (($this->_action & CRM_Core_Action::UPDATE)) { |
1788 | $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1789 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1790 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', array(1 => $this->_contributorEmail)); |
1791 | } | |
1792 | ||
1793 | if ($updateStatusMsg) { | |
1794 | $statusMsg = "{$statusMsg} {$updateStatusMsg}"; | |
1795 | } | |
1796 | } | |
1797 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
1798 | if ($this->_single) { | |
1799 | $statusMsg = ts('Event registration for %1 has been added.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1800 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1801 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', array(1 => $this->_contributorEmail)); |
1802 | } | |
1803 | } | |
1804 | else { | |
1805 | $statusMsg = ts('Total Participant(s) added to event: %1.', array(1 => count($this->_contactIds))); | |
1806 | if (count($notSent) > 0) { | |
1807 | $statusMsg .= ' ' . ts('Email has NOT been sent to %1 contact(s) - communication preferences specify DO NOT EMAIL OR valid Email is NOT present. ', array(1 => count($notSent))); | |
1808 | } | |
1809 | elseif (isset($params['send_receipt'])) { | |
1810 | $statusMsg .= ' ' . ts('A confirmation email has been sent to ALL participants'); | |
1811 | } | |
1812 | } | |
1813 | } | |
5fda4b16 | 1814 | return $statusMsg; |
6a488035 | 1815 | } |
96025800 | 1816 | |
6a488035 | 1817 | } |