CRM-13973-qa : don't skip the base transaction entry
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.php
CommitLineData
bc2eeabb
PJ
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/**
2429e40c 37 * This form used for changing / updating fee selections for the events
bc2eeabb
PJ
38 * event/contribution is partially paid
39 *
40 */
41class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
bc2eeabb
PJ
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
bc2eeabb
PJ
61 public $_participantId = NULL;
62
b17ee3ee
PJ
63 protected $_participantStatus = NULL;
64
0aaf8fe9
PJ
65 protected $_paidAmount = NULL;
66
67 public $_isPaidEvent = NULL;
68
69 protected $contributionAmt = NULL;
70
bc2eeabb
PJ
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);
bc2eeabb 74 $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
2429e40c
PJ
75 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
76
bc2eeabb
PJ
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);
2429e40c
PJ
85 $this->assign('email', $this->_contributorEmail);
86
b17ee3ee 87 $this->_participantStatus = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'status_id');
bc2eeabb
PJ
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');
0aaf8fe9 95 $this->_paidAmount = $paymentInfo['paid'];
bc2eeabb 96 $this->assign('paymentInfo', $paymentInfo);
1010c4e1 97 $this->assign('feePaid', $this->_paidAmount);
bc2eeabb
PJ
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() {
2429e40c
PJ
128 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
129
130 $statuses = CRM_Event_PseudoConstant::participantStatus();
1010c4e1
PJ
131 $this->assign('partiallyPaid', array_search('Partially paid', $statuses));
132 $this->assign('pendingRefund', array_search('Pending refund', $statuses));
133 $this->assign('participantStatus', $this->_participantStatus);
2429e40c 134
bc2eeabb
PJ
135 $config = CRM_Core_Config::singleton();
136 $this->assign('currencySymbol', $config->defaultCurrencySymbol);
137
138 // line items block
139 $lineItem = $event = array();
140 $params = array('id' => $this->_eventId);
141 CRM_Event_BAO_Event::retrieve($params, $event);
142
143 //retrieve custom information
144 $this->_values = array();
145 CRM_Event_Form_Registration::initEventFee($this, $event['id']);
146 CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);
147
148 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) {
149 $lineItem[] = $this->_values['line_items'];
150 }
151 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
152 $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId);
153 $this->assign('eventName', $event[$this->_eventId]);
154
155 $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
156 $this->add('select', 'status_id', ts('Participant Status'),
157 array(
158 '' => ts('- select -')) + $statusOptions,
159 TRUE
160 );
161
2429e40c
PJ
162 $this->addElement('checkbox',
163 'send_receipt',
164 ts('Send Confirmation?'), NULL,
165 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
166 );
167
168 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
169
170 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
171
172 $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
173 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
174
bc2eeabb
PJ
175 $buttons[] = array(
176 'type' => 'upload',
177 'name' => ts('Save'),
178 'isDefault' => TRUE,
179 );
180
2429e40c
PJ
181 $buttons[] = array(
182 'type' => 'upload',
183 'name' => ts('Save and Record Payment'),
184 'subName' => 'new'
185 );
186
bc2eeabb
PJ
187 $buttons[] = array(
188 'type' => 'cancel',
189 'name' => ts('Cancel'),
190 );
191
192 $this->addButtons($buttons);
193 $this->addFormRule(array('CRM_Event_Form_ParticipantFeeSelection', 'formRule'), $this);
194 }
195
196 static function formRule($fields, $files, $self) {
197 $errors = array();
198 return $errors;
199 }
200
201 public function postProcess() {
202 $params = $this->controller->exportValues($this->_name);
0aaf8fe9
PJ
203
204 $feeBlock = $this->_values['fee'];
205 $lineItems = $this->_values['line_items'];
206 CRM_Event_BAO_Participant::changeFeeSelections($params, $this->_participantId, $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount, $params['priceSetId']);
207 $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
208 // email sending
209 if (CRM_Utils_Array::value('send_receipt', $params)){
210 $fetchParticipantVals = array('id' => $this->_participantId);
211 CRM_Event_BAO_Participant::getValues($fetchParticipantVals, $participantDetails, CRM_Core_DAO::$_nullArray);
212 $participantParams = array_merge($params, $participantDetails[$this->_participantId]);
213 $mailSent = $this->emailReceipt($participantParams);
214 }
215
216 // update participant
217 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'status_id', $params['status_id']);
218 if(!empty($params['note'])) {
219 $noteParams = array(
220 'entity_table' => 'civicrm_participant',
221 'note' => $params['note'],
222 'entity_id' => $this->_participantId,
223 'contact_id' => $this->_contactId,
224 'modified_date' => date('Ymd'),
225 );
226 CRM_Core_BAO_Note::add($noteParams);
227 }
228 CRM_Core_Session::setStatus(ts("The fee selection has been changed for this participant"), ts('Saved'), 'success');
229
2429e40c
PJ
230 $buttonName = $this->controller->getButtonName();
231 if ($buttonName == $this->getButtonName('upload', 'new')) {
0aaf8fe9 232 $session = CRM_Core_Session::singleton();
1010c4e1 233 $session->pushUserContext(CRM_Utils_System::url('civicrm/payment/add',
2429e40c
PJ
234 "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
235 ));
236 }
0aaf8fe9
PJ
237 }
238
239 function emailReceipt(&$params) {
d5397f2f 240 $updatedLineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant', NULL, FALSE);
d7b68882
PJ
241 $lineItem = array();
242 if ($updatedLineItem) {
243 $lineItem[] = $updatedLineItem;
244 }
245 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
246
0aaf8fe9
PJ
247 // offline receipt sending
248 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
249 $receiptFrom = $params['from_email_address'];
250 }
251
252 $this->assign('module', 'Event Registration');
253 //use of the message template below requires variables in different format
254 $event = $events = array();
255 $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title');
256
257 //get all event details.
258 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties);
259 $event = $events[$params['event_id']];
260 unset($event['start_date']);
261 unset($event['end_date']);
262
263 $role = CRM_Event_PseudoConstant::participantRole();
264 $participantRoles = CRM_Utils_Array::value('role_id', $params);
265 if (is_array($participantRoles)) {
266 $selectedRoles = array();
267 foreach (array_keys($participantRoles) as $roleId) {
268 $selectedRoles[] = $role[$roleId];
269 }
270 $event['participant_role'] = implode(', ', $selectedRoles);
271 }
272 else {
273 $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role);
274 }
275 $event['is_monetary'] = $this->_isPaidEvent;
276
277 if ($params['receipt_text']) {
278 $event['confirm_email_text'] = $params['receipt_text'];
279 }
280
281 $this->assign('isAmountzero', 1);
282 $this->assign('event', $event);
283
284 $this->assign('isShowLocation', $event['is_show_location']);
285 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
286 $locationParams = array(
287 'entity_id' => $params['event_id'],
288 'entity_table' => 'civicrm_event',
289 );
290 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
291 $this->assign('location', $location);
292 }
293
294 $status = CRM_Event_PseudoConstant::participantStatus();
295 if ($this->_isPaidEvent) {
296 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
297 if (!$this->_mode) {
298 if (isset($params['payment_instrument_id'])) {
299 $this->assign('paidBy',
300 CRM_Utils_Array::value($params['payment_instrument_id'],
301 $paymentInstrument
302 )
303 );
304 }
305 }
306
307 $this->assign('totalAmount', $this->contributionAmt);
308
309 $this->assign('isPrimary', 1);
310 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
311 }
312
313 $this->assign('register_date', $params['register_date']);
314 $template = CRM_Core_Smarty::singleton();
315
316 // Retrieve the name and email of the contact - this will be the TO for receipt email
317 list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
318
319 $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName;
320
321 $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
322 if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) {
323 $this->assign('isOnWaitlist', TRUE);
324 }
325 $this->assign('contactID', $this->_contactId);
326 $this->assign('participantID', $this->_participantId);
327
328 $sendTemplateParams = array(
329 'groupName' => 'msg_tpl_workflow_event',
330 'valueName' => 'event_offline_receipt',
331 'contactId' => $this->_contactId,
332 'isTest' => FALSE,
333 'PDFFilename' => ts('confirmation').'.pdf',
334 );
335
336 // try to send emails only if email id is present
337 // and the do-not-email option is not checked for that contact
338 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
339 $sendTemplateParams['from'] = $receiptFrom;
340 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
341 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
342 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails);
343 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails);
344 }
bc2eeabb 345
0aaf8fe9
PJ
346 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
347 return $mailSent;
bc2eeabb 348 }
d5397f2f 349}