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