CRM-16189, added link on contribution edit form to record payment for pending pay...
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 * $Id$
33 *
34 */
35
36 /**
37 * This form used for changing / updating fee selections for the events
38 * event/contribution is partially paid
39 *
40 */
41 class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
42
43 public $useLivePageJS = TRUE;
44
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
57 public $_eventId = NULL;
58
59 public $_action = NULL;
60
61 public $_values = NULL;
62
63 public $_participantId = NULL;
64
65 protected $_participantStatus = NULL;
66
67 protected $_paidAmount = NULL;
68
69 public $_isPaidEvent = NULL;
70
71 protected $contributionAmt = NULL;
72
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);
76 $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
77 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
78
79 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id');
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
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);
93 $this->assign('email', $this->_contributorEmail);
94
95 $this->_participantStatus = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'status_id');
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');
103 $this->_paidAmount = $paymentInfo['paid'];
104 $this->assign('paymentInfo', $paymentInfo);
105 $this->assign('feePaid', $this->_paidAmount);
106
107 $ids = CRM_Event_BAO_Participant::getParticipantIds($this->_contributionId);
108 if (count($ids) > 1) {
109 $total = CRM_Price_BAO_LineItem::getLineTotal($this->_contributionId);
110 $this->assign('totalLineTotal', $total);
111 $this->assign('lineItemTotal', $total);
112 }
113
114 $title = ts("Change selections for %1", array(1 => $this->_contributorDisplayName));
115 if ($title) {
116 CRM_Utils_System::setTitle($title);
117 }
118 }
119
120 public function setDefaultValues() {
121 $params = array('id' => $this->_participantId);
122
123 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
124 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);
125
126 $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId, FALSE);
127 $priceFieldId = (array_keys($this->_values['fee']));
128 if (!empty($priceSetValues)) {
129 $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues);
130 }
131 else {
132 foreach ($priceFieldId as $key => $value) {
133 if (!empty($value) && ($this->_values['fee'][$value]['html_type'] == 'Radio' || $this->_values['fee'][$value]['html_type'] == 'Select') && !$this->_values['fee'][$value]['is_required']) {
134 $fee_keys = array_keys($this->_values['fee']);
135 $defaults[$this->_participantId]['price_' . $fee_keys[$key]] = 0;
136 }
137 }
138 }
139 $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
140 if ($this->_action == CRM_Core_Action::UPDATE) {
141 $fee_level = $defaults[$this->_participantId]['fee_level'];
142 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
143 $this->assign('fee_level', $fee_level);
144 $this->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
145 }
146 $defaults = $defaults[$this->_participantId];
147 return $defaults;
148 }
149
150 public function buildQuickForm() {
151
152 $statuses = CRM_Event_PseudoConstant::participantStatus();
153 $this->assign('partiallyPaid', array_search('Partially paid', $statuses));
154 $this->assign('pendingRefund', array_search('Pending refund', $statuses));
155 $this->assign('participantStatus', $this->_participantStatus);
156
157 $config = CRM_Core_Config::singleton();
158 $this->assign('currencySymbol', $config->defaultCurrencySymbol);
159
160 // line items block
161 $lineItem = $event = array();
162 $params = array('id' => $this->_eventId);
163 CRM_Event_BAO_Event::retrieve($params, $event);
164
165 //retrieve custom information
166 $this->_values = array();
167 CRM_Event_Form_Registration::initEventFee($this, $event['id']);
168 CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);
169
170 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) {
171 $lineItem[] = $this->_values['line_items'];
172 }
173 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
174 $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId);
175 $this->assign('eventName', $event[$this->_eventId]);
176
177 $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
178 $this->add('select', 'status_id', ts('Participant Status'),
179 array(
180 '' => ts('- select -'),
181 ) + $statusOptions,
182 TRUE
183 );
184
185 $this->addElement('checkbox',
186 'send_receipt',
187 ts('Send Confirmation?'), NULL,
188 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
189 );
190
191 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
192
193 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
194
195 $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
196 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
197
198 $buttons[] = array(
199 'type' => 'upload',
200 'name' => ts('Save'),
201 'isDefault' => TRUE,
202 );
203
204 if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_participantId)) {
205 $buttons[] = array(
206 'type' => 'upload',
207 'name' => ts('Save and Record Payment'),
208 'subName' => 'new',
209 );
210 }
211 $buttons[] = array(
212 'type' => 'cancel',
213 'name' => ts('Cancel'),
214 );
215
216 $this->addButtons($buttons);
217 $this->addFormRule(array('CRM_Event_Form_ParticipantFeeSelection', 'formRule'), $this);
218 }
219
220 /**
221 * @param $fields
222 * @param $files
223 * @param $self
224 *
225 * @return array
226 */
227 public static function formRule($fields, $files, $self) {
228 $errors = array();
229 return $errors;
230 }
231
232 public function postProcess() {
233 $params = $this->controller->exportValues($this->_name);
234
235 $feeBlock = $this->_values['fee'];
236 $lineItems = $this->_values['line_items'];
237 CRM_Event_BAO_Participant::changeFeeSelections($params, $this->_participantId, $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount, $params['priceSetId']);
238 $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
239 // email sending
240 if (!empty($params['send_receipt'])) {
241 $fetchParticipantVals = array('id' => $this->_participantId);
242 CRM_Event_BAO_Participant::getValues($fetchParticipantVals, $participantDetails, CRM_Core_DAO::$_nullArray);
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']);
249 if (!empty($params['note'])) {
250 $noteParams = array(
251 'entity_table' => 'civicrm_participant',
252 'note' => $params['note'],
253 'entity_id' => $this->_participantId,
254 'contact_id' => $this->_contactId,
255 'modified_date' => date('Ymd'),
256 );
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
261 $buttonName = $this->controller->getButtonName();
262 if ($buttonName == $this->getButtonName('upload', 'new')) {
263 $session = CRM_Core_Session::singleton();
264 $session->pushUserContext(CRM_Utils_System::url('civicrm/payment/add',
265 "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
266 ));
267 }
268 }
269
270 /**
271 * @param array $params
272 *
273 * @return mixed
274 */
275 public function emailReceipt(&$params) {
276 $updatedLineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant', FALSE, FALSE);
277 $lineItem = array();
278 if ($updatedLineItem) {
279 $lineItem[] = $updatedLineItem;
280 }
281 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
282
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
290 $event = $events = array();
291 $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title');
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)) {
302 $selectedRoles = array();
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) {
322 $locationParams = array(
323 'entity_id' => $params['event_id'],
324 'entity_table' => 'civicrm_event',
325 );
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
364 $sendTemplateParams = array(
365 'groupName' => 'msg_tpl_workflow_event',
366 'valueName' => 'event_offline_receipt',
367 'contactId' => $this->_contactId,
368 'isTest' => FALSE,
369 'PDFFilename' => ts('confirmation') . '.pdf',
370 );
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 }
381
382 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
383 return $mailSent;
384 }
385
386 }