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