Merge pull request #19181 from colemanw/api4DateRange
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.php
CommitLineData
bc2eeabb
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
bc2eeabb 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
bc2eeabb 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
bc2eeabb
PJ
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
bc2eeabb
PJ
16 */
17
18/**
2429e40c 19 * This form used for changing / updating fee selections for the events
bc2eeabb
PJ
20 * event/contribution is partially paid
21 *
22 */
23class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
bc2eeabb 24
96f50de2
CW
25 public $useLivePageJS = TRUE;
26
bc2eeabb
PJ
27 protected $_contactId = NULL;
28
29 protected $_contributorDisplayName = NULL;
30
31 protected $_contributorEmail = NULL;
32
33 protected $_toDoNotEmail = NULL;
34
35 protected $_contributionId = NULL;
36
37 protected $fromEmailId = NULL;
38
d06f3157 39 public $_eventId = NULL;
bc2eeabb
PJ
40
41 public $_action = NULL;
42
43 public $_values = NULL;
44
bc2eeabb
PJ
45 public $_participantId = NULL;
46
b17ee3ee
PJ
47 protected $_participantStatus = NULL;
48
0aaf8fe9
PJ
49 protected $_paidAmount = NULL;
50
51 public $_isPaidEvent = NULL;
52
53 protected $contributionAmt = NULL;
54
bc2eeabb
PJ
55 public function preProcess() {
56 $this->_participantId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
57 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
bc2eeabb 58 $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
2429e40c
PJ
59 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
60
bc2eeabb 61 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id');
ae53df5f
PJ
62 if (!$this->_contributionId) {
63 if ($primaryParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'registered_by_id')) {
64 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $primaryParticipantId, 'contribution_id', 'participant_id');
65 }
66 }
67
bc2eeabb
PJ
68 if ($this->_contributionId) {
69 $this->_isPaidEvent = TRUE;
70 }
71 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, TRUE);
72
73 list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
74 $this->assign('displayName', $this->_contributorDisplayName);
2429e40c
PJ
75 $this->assign('email', $this->_contributorEmail);
76
b17ee3ee 77 $this->_participantStatus = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'status_id');
bc2eeabb
PJ
78 //set the payment mode - _mode property is defined in parent class
79 $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
80
81 $this->assign('contactId', $this->_contactId);
82 $this->assign('id', $this->_participantId);
83
84 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantId, 'event');
0aaf8fe9 85 $this->_paidAmount = $paymentInfo['paid'];
bc2eeabb 86 $this->assign('paymentInfo', $paymentInfo);
1010c4e1 87 $this->assign('feePaid', $this->_paidAmount);
bc2eeabb 88
ae53df5f
PJ
89 $ids = CRM_Event_BAO_Participant::getParticipantIds($this->_contributionId);
90 if (count($ids) > 1) {
685dc433 91 $total = CRM_Price_BAO_LineItem::getLineTotal($this->_contributionId);
ae53df5f 92 $this->assign('totalLineTotal', $total);
685dc433 93 $this->assign('lineItemTotal', $total);
ae53df5f
PJ
94 }
95
be2fb01f 96 $title = ts("Change selections for %1", [1 => $this->_contributorDisplayName]);
bc2eeabb 97 if ($title) {
58438e5b 98 CRM_Utils_System::setTitle($title);
bc2eeabb
PJ
99 }
100 }
101
f2ac86d1 102 /**
103 * Set default values for the form.
104 *
105 * @return array
106 */
bc2eeabb 107 public function setDefaultValues() {
be2fb01f 108 $params = ['id' => $this->_participantId];
bc2eeabb
PJ
109
110 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
111 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);
112
8d13d078 113 $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId, FALSE);
5b22467a 114 $priceFieldId = (array_keys($this->_values['fee']));
bc2eeabb
PJ
115 if (!empty($priceSetValues)) {
116 $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues);
117 }
5b22467a 118 else {
22e263ad 119 foreach ($priceFieldId as $key => $value) {
5b22467a 120 if (!empty($value) && ($this->_values['fee'][$value]['html_type'] == 'Radio' || $this->_values['fee'][$value]['html_type'] == 'Select') && !$this->_values['fee'][$value]['is_required']) {
9a5bb08d
ML
121 $fee_keys = array_keys($this->_values['fee']);
122 $defaults[$this->_participantId]['price_' . $fee_keys[$key]] = 0;
5b22467a
RK
123 }
124 }
125 }
bc2eeabb
PJ
126 $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
127 if ($this->_action == CRM_Core_Action::UPDATE) {
128 $fee_level = $defaults[$this->_participantId]['fee_level'];
129 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
130 $this->assign('fee_level', $fee_level);
131 $this->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
132 }
133 $defaults = $defaults[$this->_participantId];
134 return $defaults;
135 }
136
1cb5e2d3 137 /**
138 * Build form.
139 *
140 * @throws \CRM_Core_Exception
141 */
bc2eeabb 142 public function buildQuickForm() {
2429e40c
PJ
143
144 $statuses = CRM_Event_PseudoConstant::participantStatus();
0479b4c8
TO
145 $this->assign('partiallyPaid', array_search('Partially paid', $statuses));
146 $this->assign('pendingRefund', array_search('Pending refund', $statuses));
1010c4e1 147 $this->assign('participantStatus', $this->_participantStatus);
2429e40c 148
a5dfa653 149 $this->assign('currencySymbol', CRM_Core_BAO_Country::defaultCurrencySymbol());
bc2eeabb
PJ
150
151 // line items block
be2fb01f
CW
152 $lineItem = $event = [];
153 $params = ['id' => $this->_eventId];
bc2eeabb
PJ
154 CRM_Event_BAO_Event::retrieve($params, $event);
155
156 //retrieve custom information
be2fb01f 157 $this->_values = [];
1cb5e2d3 158
159 CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE);
bc2eeabb
PJ
160 CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);
161
162 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) {
163 $lineItem[] = $this->_values['line_items'];
164 }
165 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
166 $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId);
167 $this->assign('eventName', $event[$this->_eventId]);
168
169 $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
170 $this->add('select', 'status_id', ts('Participant Status'),
be2fb01f 171 [
795492f3 172 '' => ts('- select -'),
be2fb01f 173 ] + $statusOptions,
bc2eeabb
PJ
174 TRUE
175 );
176
2429e40c
PJ
177 $this->addElement('checkbox',
178 'send_receipt',
179 ts('Send Confirmation?'), NULL,
be2fb01f 180 ['onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);"]
2429e40c
PJ
181 );
182
183 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
184
185 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
186
187 $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
188 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
189
be2fb01f 190 $buttons[] = [
bc2eeabb
PJ
191 'type' => 'upload',
192 'name' => ts('Save'),
193 'isDefault' => TRUE,
be2fb01f 194 ];
bc2eeabb 195
7a081b60 196 if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_participantId)) {
be2fb01f 197 $buttons[] = [
7a081b60
PJ
198 'type' => 'upload',
199 'name' => ts('Save and Record Payment'),
21dfd5f5 200 'subName' => 'new',
be2fb01f 201 ];
7a081b60 202 }
be2fb01f 203 $buttons[] = [
bc2eeabb
PJ
204 'type' => 'cancel',
205 'name' => ts('Cancel'),
be2fb01f 206 ];
bc2eeabb
PJ
207
208 $this->addButtons($buttons);
be2fb01f 209 $this->addFormRule(['CRM_Event_Form_ParticipantFeeSelection', 'formRule'], $this);
bc2eeabb
PJ
210 }
211
0cf587a7
EM
212 /**
213 * @param $fields
214 * @param $files
215 * @param $self
216 *
217 * @return array
218 */
00be9182 219 public static function formRule($fields, $files, $self) {
be2fb01f 220 $errors = [];
bc2eeabb
PJ
221 return $errors;
222 }
223
1cb5e2d3 224 /**
225 * Post process form.
226 *
227 * @throws \CRM_Core_Exception
228 * @throws \CiviCRM_API3_Exception
229 */
bc2eeabb
PJ
230 public function postProcess() {
231 $params = $this->controller->exportValues($this->_name);
0aaf8fe9
PJ
232
233 $feeBlock = $this->_values['fee'];
234 $lineItems = $this->_values['line_items'];
6dde7f04 235 CRM_Price_BAO_LineItem::changeFeeSelections($params, $this->_participantId, 'participant', $this->_contributionId, $feeBlock, $lineItems);
0aaf8fe9
PJ
236 $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
237 // email sending
b53cbfbc 238 if (!empty($params['send_receipt'])) {
be2fb01f 239 $fetchParticipantVals = ['id' => $this->_participantId];
db62d3a5 240 CRM_Event_BAO_Participant::getValues($fetchParticipantVals, $participantDetails);
0aaf8fe9
PJ
241 $participantParams = array_merge($params, $participantDetails[$this->_participantId]);
242 $mailSent = $this->emailReceipt($participantParams);
243 }
244
245 // update participant
246 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'status_id', $params['status_id']);
22e263ad 247 if (!empty($params['note'])) {
be2fb01f 248 $noteParams = [
0aaf8fe9
PJ
249 'entity_table' => 'civicrm_participant',
250 'note' => $params['note'],
251 'entity_id' => $this->_participantId,
252 'contact_id' => $this->_contactId,
be2fb01f 253 ];
0aaf8fe9
PJ
254 CRM_Core_BAO_Note::add($noteParams);
255 }
256 CRM_Core_Session::setStatus(ts("The fee selection has been changed for this participant"), ts('Saved'), 'success');
257
2429e40c
PJ
258 $buttonName = $this->controller->getButtonName();
259 if ($buttonName == $this->getButtonName('upload', 'new')) {
0aaf8fe9 260 $session = CRM_Core_Session::singleton();
1010c4e1 261 $session->pushUserContext(CRM_Utils_System::url('civicrm/payment/add',
353ffa53
TO
262 "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
263 ));
2429e40c 264 }
0aaf8fe9
PJ
265 }
266
0cf587a7 267 /**
c490a46a 268 * @param array $params
0cf587a7
EM
269 *
270 * @return mixed
271 */
00be9182 272 public function emailReceipt(&$params) {
77dbdcbc 273 $updatedLineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant', FALSE, FALSE);
be2fb01f 274 $lineItem = [];
d7b68882
PJ
275 if ($updatedLineItem) {
276 $lineItem[] = $updatedLineItem;
277 }
278 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
279
0aaf8fe9
PJ
280 // offline receipt sending
281 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
282 $receiptFrom = $params['from_email_address'];
283 }
284
285 $this->assign('module', 'Event Registration');
286 //use of the message template below requires variables in different format
be2fb01f
CW
287 $event = $events = [];
288 $returnProperties = ['fee_label', 'start_date', 'end_date', 'is_show_location', 'title'];
0aaf8fe9
PJ
289
290 //get all event details.
291 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties);
292 $event = $events[$params['event_id']];
293 unset($event['start_date']);
294 unset($event['end_date']);
295
296 $role = CRM_Event_PseudoConstant::participantRole();
9c1bc317 297 $participantRoles = $params['role_id'] ?? NULL;
0aaf8fe9 298 if (is_array($participantRoles)) {
be2fb01f 299 $selectedRoles = [];
0aaf8fe9
PJ
300 foreach (array_keys($participantRoles) as $roleId) {
301 $selectedRoles[] = $role[$roleId];
302 }
303 $event['participant_role'] = implode(', ', $selectedRoles);
304 }
305 else {
9c1bc317 306 $event['participant_role'] = $role[$participantRoles] ?? NULL;
0aaf8fe9
PJ
307 }
308 $event['is_monetary'] = $this->_isPaidEvent;
309
310 if ($params['receipt_text']) {
311 $event['confirm_email_text'] = $params['receipt_text'];
312 }
313
314 $this->assign('isAmountzero', 1);
315 $this->assign('event', $event);
316
317 $this->assign('isShowLocation', $event['is_show_location']);
318 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
be2fb01f 319 $locationParams = [
0aaf8fe9
PJ
320 'entity_id' => $params['event_id'],
321 'entity_table' => 'civicrm_event',
be2fb01f 322 ];
0aaf8fe9
PJ
323 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
324 $this->assign('location', $location);
325 }
326
327 $status = CRM_Event_PseudoConstant::participantStatus();
328 if ($this->_isPaidEvent) {
329 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
330 if (!$this->_mode) {
331 if (isset($params['payment_instrument_id'])) {
332 $this->assign('paidBy',
333 CRM_Utils_Array::value($params['payment_instrument_id'],
334 $paymentInstrument
335 )
336 );
337 }
338 }
339
340 $this->assign('totalAmount', $this->contributionAmt);
341
342 $this->assign('isPrimary', 1);
343 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
344 }
345
346 $this->assign('register_date', $params['register_date']);
347 $template = CRM_Core_Smarty::singleton();
348
349 // Retrieve the name and email of the contact - this will be the TO for receipt email
350 list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
351
352 $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName;
353
354 $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
355 if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) {
356 $this->assign('isOnWaitlist', TRUE);
357 }
358 $this->assign('contactID', $this->_contactId);
359 $this->assign('participantID', $this->_participantId);
360
be2fb01f 361 $sendTemplateParams = [
0aaf8fe9
PJ
362 'groupName' => 'msg_tpl_workflow_event',
363 'valueName' => 'event_offline_receipt',
364 'contactId' => $this->_contactId,
365 'isTest' => FALSE,
92fcb95f 366 'PDFFilename' => ts('confirmation') . '.pdf',
be2fb01f 367 ];
0aaf8fe9
PJ
368
369 // try to send emails only if email id is present
370 // and the do-not-email option is not checked for that contact
371 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
372 $sendTemplateParams['from'] = $receiptFrom;
373 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
374 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
9c1bc317
CW
375 $sendTemplateParams['cc'] = $this->_fromEmails['cc'] ?? NULL;
376 $sendTemplateParams['bcc'] = $this->_fromEmails['bcc'] ?? NULL;
0aaf8fe9 377 }
bc2eeabb 378
0aaf8fe9
PJ
379 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
380 return $mailSent;
bc2eeabb 381 }
96025800 382
d5397f2f 383}