Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-18-07-54-54
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 protected $_contactId = NULL;
44
45 protected $_contributorDisplayName = NULL;
46
47 protected $_contributorEmail = NULL;
48
49 protected $_toDoNotEmail = NULL;
50
51 protected $_contributionId = NULL;
52
53 protected $fromEmailId = NULL;
54
55 protected $_eventId = NULL;
56
57 public $_action = NULL;
58
59 public $_values = NULL;
60
61 public $_participantId = NULL;
62
63 protected $_participantStatus = NULL;
64
65 protected $_paidAmount = NULL;
66
67 public $_isPaidEvent = NULL;
68
69 protected $contributionAmt = NULL;
70
71 public function preProcess() {
72 $this->_participantId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
73 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
74 $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
75 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
76
77 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id');
78 if ($this->_contributionId) {
79 $this->_isPaidEvent = TRUE;
80 }
81 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, TRUE);
82
83 list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
84 $this->assign('displayName', $this->_contributorDisplayName);
85 $this->assign('email', $this->_contributorEmail);
86
87 $this->_participantStatus = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'status_id');
88 //set the payment mode - _mode property is defined in parent class
89 $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
90
91 $this->assign('contactId', $this->_contactId);
92 $this->assign('id', $this->_participantId);
93
94 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantId, 'event');
95 $this->_paidAmount = $paymentInfo['paid'];
96 $this->assign('paymentInfo', $paymentInfo);
97 CRM_Core_Resources::singleton()->addSetting(array('feePaid' => $this->_paidAmount));
98
99 $title = "Change selections for {$this->_contributorDisplayName}";
100 if ($title) {
101 CRM_Utils_System::setTitle(ts('%1', array(1 => $title)));
102 }
103 }
104
105 public function setDefaultValues() {
106 $params = array('id' => $this->_participantId);
107
108 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
109 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);
110
111 $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId);
112 if (!empty($priceSetValues)) {
113 $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues);
114 }
115
116 $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
117 if ($this->_action == CRM_Core_Action::UPDATE) {
118 $fee_level = $defaults[$this->_participantId]['fee_level'];
119 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
120 $this->assign('fee_level', $fee_level);
121 $this->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
122 }
123 $defaults = $defaults[$this->_participantId];
124 return $defaults;
125 }
126
127 public function buildQuickForm() {
128 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
129
130 $statuses = CRM_Event_PseudoConstant::participantStatus();
131 CRM_Core_Resources::singleton()->addSetting(array(
132 'partiallyPaid' => array_search('Partially paid', $statuses),
133 'pendingRefund' => array_search('Pending refund', $statuses),
134 'participantStatus' => $this->_participantStatus
135 ));
136
137 $config = CRM_Core_Config::singleton();
138 $this->assign('currencySymbol', $config->defaultCurrencySymbol);
139
140 // line items block
141 $lineItem = $event = array();
142 $params = array('id' => $this->_eventId);
143 CRM_Event_BAO_Event::retrieve($params, $event);
144
145 //retrieve custom information
146 $this->_values = array();
147 CRM_Event_Form_Registration::initEventFee($this, $event['id']);
148 CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);
149
150 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) {
151 $lineItem[] = $this->_values['line_items'];
152 }
153 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
154 $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId);
155 $this->assign('eventName', $event[$this->_eventId]);
156
157 $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
158 $this->add('select', 'status_id', ts('Participant Status'),
159 array(
160 '' => ts('- select -')) + $statusOptions,
161 TRUE
162 );
163
164 $this->addElement('checkbox',
165 'send_receipt',
166 ts('Send Confirmation?'), NULL,
167 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
168 );
169
170 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
171
172 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
173
174 $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
175 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
176
177 $buttons[] = array(
178 'type' => 'upload',
179 'name' => ts('Save'),
180 'isDefault' => TRUE,
181 );
182
183 $buttons[] = array(
184 'type' => 'upload',
185 'name' => ts('Save and Record Payment'),
186 'subName' => 'new'
187 );
188
189 $buttons[] = array(
190 'type' => 'cancel',
191 'name' => ts('Cancel'),
192 );
193
194 $this->addButtons($buttons);
195 $this->addFormRule(array('CRM_Event_Form_ParticipantFeeSelection', 'formRule'), $this);
196 }
197
198 static function formRule($fields, $files, $self) {
199 $errors = array();
200 return $errors;
201 }
202
203 public function postProcess() {
204 $params = $this->controller->exportValues($this->_name);
205
206 $feeBlock = $this->_values['fee'];
207 $lineItems = $this->_values['line_items'];
208 CRM_Event_BAO_Participant::changeFeeSelections($params, $this->_participantId, $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount, $params['priceSetId']);
209 $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
210 // email sending
211 if (CRM_Utils_Array::value('send_receipt', $params)){
212 $fetchParticipantVals = array('id' => $this->_participantId);
213 CRM_Event_BAO_Participant::getValues($fetchParticipantVals, $participantDetails, CRM_Core_DAO::$_nullArray);
214 $participantParams = array_merge($params, $participantDetails[$this->_participantId]);
215 $mailSent = $this->emailReceipt($participantParams);
216 }
217
218 // update participant
219 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'status_id', $params['status_id']);
220 if(!empty($params['note'])) {
221 $noteParams = array(
222 'entity_table' => 'civicrm_participant',
223 'note' => $params['note'],
224 'entity_id' => $this->_participantId,
225 'contact_id' => $this->_contactId,
226 'modified_date' => date('Ymd'),
227 );
228 CRM_Core_BAO_Note::add($noteParams);
229 }
230 CRM_Core_Session::setStatus(ts("The fee selection has been changed for this participant"), ts('Saved'), 'success');
231
232 $buttonName = $this->controller->getButtonName();
233 if ($buttonName == $this->getButtonName('upload', 'new')) {
234 $session = CRM_Core_Session::singleton();
235 $session->replaceUserContext(CRM_Utils_System::url('civicrm/payment/add',
236 "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
237 ));
238 }
239 }
240
241 function emailReceipt(&$params) {
242 // offline receipt sending
243 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
244 $receiptFrom = $params['from_email_address'];
245 }
246
247 $this->assign('module', 'Event Registration');
248 //use of the message template below requires variables in different format
249 $event = $events = array();
250 $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title');
251
252 //get all event details.
253 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties);
254 $event = $events[$params['event_id']];
255 unset($event['start_date']);
256 unset($event['end_date']);
257
258 $role = CRM_Event_PseudoConstant::participantRole();
259 $participantRoles = CRM_Utils_Array::value('role_id', $params);
260 if (is_array($participantRoles)) {
261 $selectedRoles = array();
262 foreach (array_keys($participantRoles) as $roleId) {
263 $selectedRoles[] = $role[$roleId];
264 }
265 $event['participant_role'] = implode(', ', $selectedRoles);
266 }
267 else {
268 $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role);
269 }
270 $event['is_monetary'] = $this->_isPaidEvent;
271
272 if ($params['receipt_text']) {
273 $event['confirm_email_text'] = $params['receipt_text'];
274 }
275
276 $this->assign('isAmountzero', 1);
277 $this->assign('event', $event);
278
279 $this->assign('isShowLocation', $event['is_show_location']);
280 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
281 $locationParams = array(
282 'entity_id' => $params['event_id'],
283 'entity_table' => 'civicrm_event',
284 );
285 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
286 $this->assign('location', $location);
287 }
288
289 $status = CRM_Event_PseudoConstant::participantStatus();
290 if ($this->_isPaidEvent) {
291 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
292 if (!$this->_mode) {
293 if (isset($params['payment_instrument_id'])) {
294 $this->assign('paidBy',
295 CRM_Utils_Array::value($params['payment_instrument_id'],
296 $paymentInstrument
297 )
298 );
299 }
300 }
301
302 $this->assign('totalAmount', $this->contributionAmt);
303
304 $this->assign('isPrimary', 1);
305 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
306 }
307
308 $this->assign('register_date', $params['register_date']);
309 $template = CRM_Core_Smarty::singleton();
310
311 // Retrieve the name and email of the contact - this will be the TO for receipt email
312 list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
313
314 $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName;
315
316 $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
317 if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) {
318 $this->assign('isOnWaitlist', TRUE);
319 }
320 $this->assign('contactID', $this->_contactId);
321 $this->assign('participantID', $this->_participantId);
322
323 $sendTemplateParams = array(
324 'groupName' => 'msg_tpl_workflow_event',
325 'valueName' => 'event_offline_receipt',
326 'contactId' => $this->_contactId,
327 'isTest' => FALSE,
328 'PDFFilename' => ts('confirmation').'.pdf',
329 );
330
331 // try to send emails only if email id is present
332 // and the do-not-email option is not checked for that contact
333 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
334 $sendTemplateParams['from'] = $receiptFrom;
335 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
336 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
337 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails);
338 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails);
339 }
340
341 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
342 return $mailSent;
343 }
344 }