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