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