Merge branch 'VAT-417-Push' of github.com:Parag18/civicrm-core into Parag18-VAT-417...
[civicrm-core.git] / CRM / Event / Form / Registration / Confirm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 useful, 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 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36
37 /**
38 * This class generates form components for processing Event
39 *
40 */
41 class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
42
43 /**
44 * the values for the contribution db object
45 *
46 * @var array
47 * @protected
48 */
49 public $_values;
50
51 /**
52 * the total amount
53 *
54 * @var float
55 * @public
56 */
57 public $_totalAmount;
58
59 /**
60 * Function to set variables up before form is built
61 *
62 * @return void
63 * @access public
64 */
65 function preProcess() {
66 parent::preProcess();
67
68 // lineItem isn't set until Register postProcess
69 $this->_lineItem = $this->get('lineItem');
70
71 $this->_params = $this->get('params');
72 $this->_params[0]['tax_amount'] = $this->get('tax_amount');
73
74 $this->_params[0]['is_pay_later'] = $this->get('is_pay_later');
75 $this->assign('is_pay_later', $this->_params[0]['is_pay_later']);
76 if ($this->_params[0]['is_pay_later']) {
77 $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']);
78 }
79
80 CRM_Utils_Hook::eventDiscount($this, $this->_params);
81
82 if (!empty($this->_params[0]['discount']) && !empty($this->_params[0]['discount']['applied'])) {
83 $this->set('hookDiscount', $this->_params[0]['discount']);
84 $this->assign('hookDiscount', $this->_params[0]['discount']);
85 }
86
87 if ($this->_contributeMode == 'express') {
88 $params = array();
89 // rfp == redirect from paypal
90 $rfp = CRM_Utils_Request::retrieve('rfp', 'Boolean',
91 CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET'
92 );
93
94 //we lost rfp in case of additional participant. So set it explicitly.
95 if ($rfp || CRM_Utils_Array::value('additional_participants', $this->_params[0], FALSE)) {
96 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
97 $paymentObjError = ts('The system did not record payment details for this payment and so could not process the transaction. Please report this error to the site administrator.');
98 if (is_object($payment))
99 $expressParams = $payment->getExpressCheckoutDetails($this->get('token'));
100 else
101 CRM_Core_Error::fatal($paymentObjError);
102
103 $params['payer'] = $expressParams['payer'];
104 $params['payer_id'] = $expressParams['payer_id'];
105 $params['payer_status'] = $expressParams['payer_status'];
106
107 CRM_Core_Payment_Form::mapParams($this->_bltID, $expressParams, $params, FALSE);
108
109 // fix state and country id if present
110 if (isset($params["billing_state_province_id-{$this->_bltID}"])) {
111 $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]);
112 }
113 if (isset($params['billing_country_id'])) {
114 $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
115 }
116
117 // set a few other parameters for PayPal
118 $params['token'] = $this->get('token');
119 $params['amount'] = $this->_params[0]['amount'];
120 if (!empty($this->_params[0]['discount'])) {
121 $params['discount'] = $this->_params[0]['discount'];
122 $params['discountAmount'] = $this->_params[0]['discountAmount'];
123 $params['discountMessage'] = $this->_params[0]['discountMessage'];
124 }
125 $params['amount_level'] = $this->_params[0]['amount_level'];
126 $params['currencyID'] = $this->_params[0]['currencyID'];
127 $params['payment_action'] = 'Sale';
128
129 // also merge all the other values from the profile fields
130 $values = $this->controller->exportValues('Register');
131 $skipFields = array(
132 'amount',
133 "street_address-{$this->_bltID}",
134 "city-{$this->_bltID}",
135 "state_province_id-{$this->_bltID}",
136 "postal_code-{$this->_bltID}",
137 "country_id-{$this->_bltID}",
138 );
139
140 foreach ($values as $name => $value) {
141 // skip amount field
142 if (!in_array($name, $skipFields)) {
143 $params[$name] = $value;
144 }
145 }
146 $this->set('getExpressCheckoutDetails', $params);
147 }
148 else {
149 $params = $this->get('getExpressCheckoutDetails');
150 }
151 $this->_params[0] = $params;
152 $this->_params[0]['is_primary'] = 1;
153 }
154 else {
155 //process only primary participant params.
156 $registerParams = $this->_params[0];
157 if (isset($registerParams["billing_state_province_id-{$this->_bltID}"])
158 && $registerParams["billing_state_province_id-{$this->_bltID}"]
159 ) {
160 $registerParams["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($registerParams["billing_state_province_id-{$this->_bltID}"]);
161 }
162
163 if (isset($registerParams["billing_country_id-{$this->_bltID}"]) && $registerParams["billing_country_id-{$this->_bltID}"]) {
164 $registerParams["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($registerParams["billing_country_id-{$this->_bltID}"]);
165 }
166 if (isset($registerParams['credit_card_exp_date'])) {
167 $registerParams['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($registerParams);
168 $registerParams['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($registerParams);
169 }
170 if ($this->_values['event']['is_monetary']) {
171 $registerParams['ip_address'] = CRM_Utils_System::ipAddress();
172 $registerParams['currencyID'] = $this->_params[0]['currencyID'];
173 $registerParams['payment_action'] = 'Sale';
174 }
175 //assign back primary participant params.
176 $this->_params[0] = $registerParams;
177 }
178
179 if ($this->_values['event']['is_monetary']) {
180 $this->_params[0]['invoiceID'] = $this->get('invoiceID');
181 }
182 $this->assign('defaultRole', FALSE);
183 if (CRM_Utils_Array::value('defaultRole', $this->_params[0]) == 1) {
184 $this->assign('defaultRole', TRUE);
185 }
186
187 if (empty($this->_params[0]['participant_role_id']) &&
188 $this->_values['event']['default_role_id']
189 ) {
190 $this->_params[0]['participant_role_id'] = $this->_values['event']['default_role_id'];
191 }
192
193 if (isset($this->_values['event']['confirm_title'])) {
194 CRM_Utils_System::setTitle($this->_values['event']['confirm_title']);
195 }
196
197 if ($this->_pcpId) {
198 $params = CRM_Contribute_Form_Contribution_Confirm::processPcp($this, $this->_params[0]);
199 $this->_params[0] = $params;
200 }
201
202 $this->set('params', $this->_params);
203 }
204
205 /**
206 * overwrite action, since we are only showing elements in frozen mode
207 * no help display needed
208 *
209 * @return int
210 * @access public
211 */
212 function getAction() {
213 if ($this->_action & CRM_Core_Action::PREVIEW) {
214 return CRM_Core_Action::VIEW | CRM_Core_Action::PREVIEW;
215 }
216 else {
217 return CRM_Core_Action::VIEW;
218 }
219 }
220
221 /**
222 * Function to build the form
223 *
224 * @return void
225 * @access public
226 */
227 public function buildQuickForm() {
228 $this->assignToTemplate();
229
230 if ($this->_values['event']['is_monetary'] &&
231 ($this->_params[0]['amount'] || $this->_params[0]['amount'] == 0)
232 ) {
233 $this->_amount = array();
234
235 $taxAmount = 0;
236 foreach ($this->_params as $k => $v) {
237 //display tax amount on confirmation page
238 $taxAmount += $v['tax_amount'];
239 if (is_array($v)) {
240 foreach (array(
241 'first_name', 'last_name') as $name) {
242 if (isset($v['billing_' . $name]) &&
243 !isset($v[$name])
244 ) {
245 $v[$name] = $v['billing_' . $name];
246 }
247 }
248
249 if (!empty($v['first_name']) && !empty($v['last_name'])) {
250 $append = $v['first_name'] . ' ' . $v['last_name'];
251 }
252 else {
253 //use an email if we have one
254 foreach ($v as $v_key => $v_val) {
255 if (substr($v_key, 0, 6) == 'email-') {
256 $append = $v[$v_key];
257 }
258 }
259 }
260
261 $this->_amount[$k]['amount'] = $v['amount'];
262 if (!empty($v['discountAmount'])) {
263 $this->_amount[$k]['amount'] -= $v['discountAmount'];
264 }
265
266 $this->_amount[$k]['label'] = preg_replace('/\ 1/', '', $v['amount_level']) . ' - ' . $append;
267 $this->_part[$k]['info'] = CRM_Utils_Array::value('first_name', $v) . ' ' . CRM_Utils_Array::value('last_name', $v);
268 if (empty($v['first_name'])) {
269 $this->_part[$k]['info'] = $append;
270 }
271 $this->_totalAmount = $this->_totalAmount + $this->_amount[$k]['amount'];
272 if (!empty($v['is_primary'])) {
273 $this->set('primaryParticipantAmount', $this->_amount[$k]['amount']);
274 }
275 }
276 }
277
278 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
279 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
280 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
281 if ($invoicing) {
282 $this->assign('totalTaxAmount', $taxAmount);
283 $this->assign('taxTerm', $taxTerm);
284 }
285 $this->assign('part', $this->_part);
286 $this->set('part', $this->_part);
287 $this->assign('amounts', $this->_amount);
288 $this->assign('totalAmount', $this->_totalAmount);
289 $this->set('totalAmount', $this->_totalAmount);
290 }
291
292 if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) {
293 $lineItemForTemplate = array();
294 $getTaxDetails = FALSE;
295 foreach ($this->_lineItem as $key => $value) {
296 if (!empty($value)) {
297 $lineItemForTemplate[$key] = $value;
298 }
299 if ($invoicing) {
300 foreach ($value as $v) {
301 if (isset($v['tax_rate'])) {
302 $getTaxDetails = TRUE;
303 }
304 }
305 }
306 }
307 if (!empty($lineItemForTemplate)) {
308 $this->assign('lineItem', $lineItemForTemplate);
309 }
310 $this->assign('getTaxDetails', $getTaxDetails);
311 }
312
313 //display additional participants profile.
314 self::assignProfiles($this);
315
316 //consider total amount.
317 $this->assign('isAmountzero', ($this->_totalAmount <= 0) ? TRUE : FALSE);
318
319 if ($this->_paymentProcessor['payment_processor_type'] == 'Google_Checkout' && empty($this->_params[0]['is_pay_later']) && !($this->_params[0]['amount'] == 0) &&
320 !$this->_allowWaitlist && !$this->_requireApproval
321 ) {
322 $this->_checkoutButtonName = $this->getButtonName('next', 'checkout');
323 $this->add('image',
324 $this->_checkoutButtonName,
325 $this->_paymentProcessor['url_button'],
326 array('class' => 'form-submit')
327 );
328
329 $this->addButtons(array(
330 array(
331 'type' => 'back',
332 'name' => ts('<< Go Back'),
333 ),
334 )
335 );
336 }
337 else {
338 $contribButton = ts('Continue');
339 $this->addButtons(array(
340 array(
341 'type' => 'next',
342 'name' => $contribButton,
343 'isDefault' => TRUE,
344 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"),
345 ),
346 array(
347 'type' => 'back',
348 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
349 'name' => ts('Go Back'),
350 ),
351 )
352 );
353 }
354
355 $defaults = array();
356 $fields = array();
357 if (!empty($this->_fields)) {
358 foreach ($this->_fields as $name => $dontCare) {
359 $fields[$name] = 1;
360 }
361 }
362 $fields["billing_state_province-{$this->_bltID}"] = $fields["billing_country-{$this->_bltID}"] = $fields["email-{$this->_bltID}"] = 1;
363 foreach ($fields as $name => $dontCare) {
364 if (isset($this->_params[0][$name])) {
365 $defaults[$name] = $this->_params[0][$name];
366 if (substr($name, 0, 7) == 'custom_') {
367 $timeField = "{$name}_time";
368 if (isset($this->_params[0][$timeField])) {
369 $defaults[$timeField] = $this->_params[0][$timeField];
370 }
371 if (isset($this->_params[0]["{$name}_id"])) {
372 $defaults["{$name}_id"] = $this->_params[0]["{$name}_id"];
373 }
374 }
375 elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)
376 && !empty($this->_params[0][$name . '_custom'])
377 ) {
378 $defaults[$name . '_custom'] = $this->_params[0][$name . '_custom'];
379 }
380 }
381 }
382
383 // now fix all state country selectors
384 CRM_Core_BAO_Address::fixAllStateSelects($this, $defaults);
385
386 $this->setDefaults($defaults);
387 $this->freeze();
388
389 //lets give meaningful status message, CRM-4320.
390 $this->assign('isOnWaitlist', $this->_allowWaitlist);
391 $this->assign('isRequireApproval', $this->_requireApproval);
392
393 // Assign Participant Count to Lineitem Table
394 $this->assign('pricesetFieldsCount', CRM_Price_BAO_PriceSet::getPricesetCount($this->_priceSetId));
395 }
396
397 /**
398 * Function to process the form
399 *
400 * @access public
401 *
402 * @return void
403 */
404 public function postProcess() {
405 $now = date('YmdHis');
406
407 $this->_params = $this->get('params');
408 if (!empty($this->_params[0]['contact_id'])) {
409 // unclear when this would be set & whether it could be checked in getContactID.
410 // perhaps it relates to when cid is in the url
411 //@todo someone who knows add comments on the various contactIDs in this form
412 $contactID = $this->_params[0]['contact_id'];
413 }
414 else {
415 $contactID = $this->getContactID();
416 }
417
418 // if a discount has been applied, lets now deduct it from the amount
419 // and fix the fee level
420 if (!empty($this->_params[0]['discount']) && !empty($this->_params[0]['discount']['applied'])) {
421 foreach ($this->_params as $k => $v) {
422 if (CRM_Utils_Array::value('amount', $this->_params[$k]) > 0 && !empty($this->_params[$k]['discountAmount'])) {
423 $this->_params[$k]['amount'] -= $this->_params[$k]['discountAmount'];
424 $this->_params[$k]['amount_level'] .= CRM_Utils_Array::value('discountMessage', $this->_params[$k]);
425 }
426 }
427 $this->set('params', $this->_params);
428 }
429
430 // CRM-4320, lets build array of cancelled additional participant ids
431 // those are drop or skip by primary at the time of confirmation.
432 // get all in and then unset those we want to process.
433 $cancelledIds = $this->_additionalParticipantIds;
434
435 $params = $this->_params;
436 if ($this->_values['event']['is_monetary']) {
437 $this->set('finalAmount', $this->_amount);
438 }
439 $participantCount = array();
440
441 //unset the skip participant from params.
442 //build the $participantCount array.
443 //maintain record for all participants.
444 foreach ($params as $participantNum => $record) {
445 if ($record == 'skip') {
446 unset($params[$participantNum]);
447 $participantCount[$participantNum] = 'skip';
448 }
449 elseif ($participantNum) {
450 $participantCount[$participantNum] = 'participant';
451 }
452
453 //lets get additional participant id to cancel.
454 if ($this->_allowConfirmation && is_array($cancelledIds)) {
455 $additonalId = CRM_Utils_Array::value('participant_id', $record);
456 if ($additonalId && $key = array_search($additonalId, $cancelledIds)) {
457 unset($cancelledIds[$key]);
458 }
459 }
460 }
461
462 $payment = $registerByID = $primaryCurrencyID = $contribution = NULL;
463 $paymentObjError = ts('The system did not record payment details for this payment and so could not process the transaction. Please report this error to the site administrator.');
464
465 $this->participantIDS = array();
466 $fields = array();
467 foreach ($params as $key => $value) {
468 CRM_Event_Form_Registration_Confirm::fixLocationFields($value, $fields, $this);
469 //unset the billing parameters if it is pay later mode
470 //to avoid creation of billing location
471 if ($this->_allowWaitlist || $this->_requireApproval || !empty($value['is_pay_later']) || empty($value['is_primary'])) {
472 $billingFields = array(
473 "email-{$this->_bltID}",
474 'billing_first_name',
475 'billing_middle_name',
476 'billing_last_name',
477 "billing_street_address-{$this->_bltID}",
478 "billing_city-{$this->_bltID}",
479 "billing_state_province-{$this->_bltID}",
480 "billing_state_province_id-{$this->_bltID}",
481 "billing_postal_code-{$this->_bltID}",
482 "billing_country-{$this->_bltID}",
483 "billing_country_id-{$this->_bltID}",
484 "address_name-{$this->_bltID}",
485 );
486 foreach ($billingFields as $field) {
487 unset($value[$field]);
488 }
489 if (!empty($value['is_pay_later'])) {
490 $this->_values['params']['is_pay_later'] = TRUE;
491 }
492 }
493
494 //Unset ContactID for additional participants and set RegisterBy Id.
495 if (empty($value['is_primary'])) {
496 $contactID = CRM_Utils_Array::value('contact_id', $value);
497 $registerByID = $this->get('registerByID');
498 if ($registerByID) {
499 $value['registered_by_id'] = $registerByID;
500 }
501 }
502 else {
503 $value['amount'] = $this->_totalAmount;
504 }
505
506 $contactID = CRM_Event_Form_Registration_Confirm::updateContactFields($contactID, $value, $fields, $this);
507
508 // lets store the contactID in the session
509 // we dont store in userID in case the user is doing multiple
510 // transactions etc
511 // for things like tell a friend
512 if (!$this->getContactID() && !empty($value['is_primary'])) {
513 $session = CRM_Core_Session::singleton();
514 $session->set('transaction.userID', $contactID);
515 }
516
517 $value['description'] = ts('Online Event Registration') . ': ' . $this->_values['event']['title'];
518 $value['accountingCode'] = CRM_Utils_Array::value('accountingCode',
519 $this->_values['event']
520 );
521
522 // required only if paid event
523 if ($this->_values['event']['is_monetary']) {
524 if (is_array($this->_paymentProcessor)) {
525 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
526 }
527 $pending = FALSE;
528 $result = NULL;
529
530 if ($this->_allowWaitlist || $this->_requireApproval) {
531 //get the participant statuses.
532 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
533 if ($this->_allowWaitlist) {
534 $value['participant_status_id'] = $value['participant_status'] = array_search('On waitlist', $waitingStatuses);
535 }
536 else {
537 $value['participant_status_id'] = $value['participant_status'] = array_search('Awaiting approval', $waitingStatuses);
538 }
539
540 //there might be case user seleted pay later and
541 //now becomes part of run time waiting list.
542 $value['is_pay_later'] = FALSE;
543 }
544 elseif (!empty($value['is_pay_later']) ||
545 $value['amount'] == 0 ||
546 $this->_contributeMode == 'checkout' ||
547 $this->_contributeMode == 'notify'
548 ) {
549 if ($value['amount'] != 0) {
550 $pending = TRUE;
551 //get the participant statuses.
552 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
553 $status = !empty($value['is_pay_later']) ? 'Pending from pay later' : 'Pending from incomplete transaction';
554 $value['participant_status_id'] = $value['participant_status'] = array_search($status, $pendingStatuses);
555 }
556 }
557 elseif ($this->_contributeMode == 'express' && !empty($value['is_primary'])) {
558 if (is_object($payment))
559 $result = $payment->doExpressCheckout($value);
560 else
561 CRM_Core_Error::fatal($paymentObjError);
562 }
563 elseif (!empty($value['is_primary'])) {
564 CRM_Core_Payment_Form::mapParams($this->_bltID, $value, $value, TRUE);
565 // payment email param can be empty for _bltID mapping
566 // thus provide mapping for it with a different email value
567 if (empty($value['email'])) {
568 $value['email'] = CRM_Utils_Array::valueByRegexKey('/^email-/', $value);
569 }
570
571 if (is_object($payment)) {
572 $result = $payment->doDirectPayment($value);
573 }
574 else {
575 CRM_Core_Error::fatal($paymentObjError);
576 }
577 }
578
579 if (is_a($result, 'CRM_Core_Error')) {
580 CRM_Core_Error::displaySessionError($result);
581 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "id={$this->_eventId}"));
582 }
583
584 if ($result) {
585 $value = array_merge($value, $result);
586 }
587
588 $value['receive_date'] = $now;
589 if ($this->_allowConfirmation) {
590 $value['participant_register_date'] = $this->_values['participant']['register_date'];
591 }
592
593 $createContrib = ($value['amount'] != 0) ? TRUE : FALSE;
594 // force to create zero amount contribution, CRM-5095
595 if (!$createContrib && ($value['amount'] == 0)
596 && $this->_priceSetId && $this->_lineItem
597 ) {
598 $createContrib = TRUE;
599 }
600
601 if ($createContrib && !empty($value['is_primary']) &&
602 !$this->_allowWaitlist && !$this->_requireApproval
603 ) {
604 // if paid event add a contribution record
605 //if primary participant contributing additional amount
606 //append (multiple participants) to its fee level. CRM-4196.
607 $isAdditionalAmount = FALSE;
608 if (count($params) > 1) {
609 $isAdditionalAmount = TRUE;
610 }
611
612 //passing contribution id is already registered.
613 $contribution =
614 self::processContribution($this, $value, $result, $contactID, $pending, $isAdditionalAmount);
615 $value['contributionID'] = $contribution->id;
616 $value['contributionTypeID'] = $contribution->financial_type_id;
617 $value['receive_date'] = $contribution->receive_date;
618 $value['trxn_id'] = $contribution->trxn_id;
619 $value['contributionID'] = $contribution->id;
620 $value['contributionTypeID'] = $contribution->financial_type_id;
621 }
622 $value['contactID'] = $contactID;
623 $value['eventID'] = $this->_eventId;
624 $value['item_name'] = $value['description'];
625 }
626 $this->_values['contributionId'] = $value['contributionID'] ;
627
628 //CRM-4453.
629 if (!empty($value['is_primary'])) {
630 $primaryCurrencyID = CRM_Utils_Array::value('currencyID', $value);
631 }
632 if (empty($value['currencyID'])) {
633 $value['currencyID'] = $primaryCurrencyID;
634 }
635
636 // CRM-11182 - Confirmation page might not be monetary
637 if ($this->_values['event']['is_monetary']) {
638 if (!$pending && !empty($value['is_primary']) &&
639 !$this->_allowWaitlist && !$this->_requireApproval
640 ) {
641 // transactionID & receive date required while building email template
642 $this->assign('trxn_id', $value['trxn_id']);
643 $this->assign('receive_date', CRM_Utils_Date::mysqlToIso($value['receive_date']));
644 $this->set('receiveDate', CRM_Utils_Date::mysqlToIso($value['receive_date']));
645 $this->set('trxnId', CRM_Utils_Array::value('trxn_id', $value));
646 }
647 }
648
649 $value['fee_amount'] = CRM_Utils_Array::value('amount', $value);
650 $this->set('value', $value);
651
652 // handle register date CRM-4320
653 if ($this->_allowConfirmation) {
654 $registerDate = CRM_Utils_Array::value( 'participant_register_date', $params );
655 }
656 elseif (!empty($params['participant_register_date']) &&
657 is_array($params['participant_register_date']) &&
658 !empty($params['participant_register_date'])
659 ) {
660 $registerDate = CRM_Utils_Date::format($params['participant_register_date']);
661 }
662 else {
663 $registerDate = date('YmdHis');
664 }
665 $this->assign('register_date', $registerDate);
666
667 $this->confirmPostProcess($contactID, $contribution, $payment);
668 }
669
670 //handle if no additional participant.
671 if (!$registerByID) {
672 $registerByID = $this->get('registerByID');
673 }
674
675 $this->set('participantIDs', $this->_participantIDS);
676
677 // create line items, CRM-5313
678 if ($this->_priceSetId &&
679 !empty($this->_lineItem)
680 ) {
681 // take all processed participant ids.
682 $allParticipantIds = $this->_participantIDS;
683
684 // when participant re-walk wizard.
685 if ($this->_allowConfirmation &&
686 !empty($this->_additionalParticipantIds)
687 ) {
688 $allParticipantIds = array_merge(array($registerByID), $this->_additionalParticipantIds);
689 }
690
691 $entityTable = 'civicrm_participant';
692 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
693 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
694 $totalTaxAmount = 0;
695 $dataArray = array();
696 foreach ($this->_lineItem as $key => $value) {
697 if (($value != 'skip') &&
698 ($entityId = CRM_Utils_Array::value($key, $allParticipantIds))
699 ) {
700
701 // do cleanup line items if participant re-walking wizard.
702 if ($this->_allowConfirmation) {
703 CRM_Price_BAO_LineItem::deleteLineItems($entityId, $entityTable);
704 }
705 $lineItem[$this->_priceSetId] = $value;
706 CRM_Price_BAO_LineItem::processPriceSet($entityId, $lineItem, $contribution, $entityTable);
707 }
708 if ($invoicing) {
709 foreach ($value as $line) {
710 if (isset($line['tax_amount']) && isset($line['tax_rate'])) {
711 $totalTaxAmount = $line['tax_amount'] + $totalTaxAmount;
712 if (isset($dataArray[$line['tax_rate']])) {
713 $dataArray[$line['tax_rate']] = $dataArray[$line['tax_rate']] + CRM_Utils_Array::value('tax_amount', $line);
714 }
715 else {
716 $dataArray[$line['tax_rate']] = CRM_Utils_Array::value('tax_amount', $line);
717 }
718 }
719 }
720 }
721 }
722 if ($invoicing) {
723 $this->assign('dataArray', $dataArray);
724 $this->assign('totalTaxAmount', $totalTaxAmount);
725 }
726 }
727
728 //update status and send mail to cancelled additonal participants, CRM-4320
729 if ($this->_allowConfirmation && is_array($cancelledIds) && !empty($cancelledIds)) {
730 $cancelledId = array_search('Cancelled',
731 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'")
732 );
733 CRM_Event_BAO_Participant::transitionParticipants($cancelledIds, $cancelledId);
734 }
735
736 $isTest = FALSE;
737 if ($this->_action & CRM_Core_Action::PREVIEW) {
738 $isTest = TRUE;
739 }
740
741 // for Transfer checkout.
742 if (($this->_contributeMode == 'checkout' ||
743 $this->_contributeMode == 'notify'
744 ) && empty($params[0]['is_pay_later']) &&
745 !$this->_allowWaitlist && !$this->_requireApproval &&
746 $this->_totalAmount > 0
747 ) {
748
749 $primaryParticipant = $this->get('primaryParticipant');
750
751 if (empty($primaryParticipant['participantID'])) {
752 $primaryParticipant['participantID'] = $registerByID;
753 }
754
755 //build an array of custom profile and assigning it to template
756 $customProfile = CRM_Event_BAO_Event::buildCustomProfile($registerByID, $this->_values, NULL, $isTest);
757 if (count($customProfile)) {
758 $this->assign('customProfile', $customProfile);
759 $this->set('customProfile', $customProfile);
760 }
761
762 // do a transfer only if a monetary payment greater than 0
763 if ($this->_values['event']['is_monetary'] && $primaryParticipant) {
764 if ($payment && is_object($payment)) {
765 // call postprocess hook before leaving
766 $this->postProcessHook();
767 // this does not return
768 $payment->doTransferCheckout($primaryParticipant, 'event');
769 }
770 else {
771 CRM_Core_Error::fatal($paymentObjError);
772 }
773 }
774 }
775 else {
776 //otherwise send mail Confirmation/Receipt
777 $primaryContactId = $this->get('primaryContactId');
778
779 //build an array of cId/pId of participants
780 $additionalIDs = CRM_Event_BAO_Event::buildCustomProfile($registerByID,
781 NULL, $primaryContactId, $isTest,
782 TRUE
783 );
784 //lets send mails to all with meaningful text, CRM-4320.
785 $this->assign('isOnWaitlist', $this->_allowWaitlist);
786 $this->assign('isRequireApproval', $this->_requireApproval);
787
788 //need to copy, since we are unsetting on the way.
789 $copyParticipantCount = $participantCount;
790
791 //lets carry all paticipant params w/ values.
792 foreach ($additionalIDs as $participantID => $contactId) {
793 $participantNum = NULL;
794 if ($participantID == $registerByID) {
795 $participantNum = 0;
796 }
797 else {
798 if ($participantNum = array_search('participant', $copyParticipantCount)) {
799 unset($copyParticipantCount[$participantNum]);
800 }
801 }
802 if ($participantNum === NULL)
803 break;
804
805 //carry the participant submitted values.
806 $this->_values['params'][$participantID] = $params[$participantNum];
807 }
808
809 foreach ($additionalIDs as $participantID => $contactId) {
810 $participantNum = 0;
811 if ($participantID == $registerByID) {
812 //set as Primary Participant
813 $this->assign('isPrimary', 1);
814 //build an array of custom profile and assigning it to template.
815 $customProfile = CRM_Event_BAO_Event::buildCustomProfile($participantID, $this->_values, NULL, $isTest);
816
817 if (count($customProfile)) {
818 $this->assign('customProfile', $customProfile);
819 $this->set('customProfile', $customProfile);
820 }
821 $this->_values['params']['additionalParticipant'] = FALSE;
822 }
823 else {
824 //take the Additional participant number.
825 if ($participantNum = array_search('participant', $participantCount)) {
826 unset($participantCount[$participantNum]);
827 }
828 $this->assign('isPrimary', 0);
829 $this->assign('customProfile', NULL);
830 //Additional Participant should get only it's payment information
831 if (!empty($this->_amount)) {
832 $amount = array();
833 $params = $this->get('params');
834 $amount[$participantNum]['label'] = preg_replace('/\ 1/', '', $params[$participantNum]['amount_level']);
835 $amount[$participantNum]['amount'] = $params[$participantNum]['amount'];
836 $this->assign('amounts', $amount);
837 }
838 if ($this->_lineItem) {
839 $lineItems = $this->_lineItem;
840 $lineItem = array();
841 if ($lineItemValue = CRM_Utils_Array::value($participantNum, $lineItems)) {
842 $lineItem[] = $lineItemValue;
843 }
844 $this->assign('lineItem', $lineItem);
845 }
846 $this->_values['params']['additionalParticipant'] = TRUE;
847 $this->assign('isAdditionalParticipant', $this->_values['params']['additionalParticipant']);
848 }
849
850 //pass these variables since these are run time calculated.
851 $this->_values['params']['isOnWaitlist'] = $this->_allowWaitlist;
852 $this->_values['params']['isRequireApproval'] = $this->_requireApproval;
853
854 //send mail to primary as well as additional participants.
855 $this->assign('contactID', $contactId);
856 $this->assign('participantID', $participantID);
857 CRM_Event_BAO_Event::sendMail($contactId, $this->_values, $participantID, $isTest);
858 }
859 }
860 }
861 //end of function
862
863 /**
864 * Process the contribution
865 *
866 * @param $form
867 * @param $params
868 * @param $result
869 * @param $contactID
870 * @param bool $pending
871 * @param bool $isAdditionalAmount
872 *
873 * @return void
874 * @access public
875 */
876 static function processContribution(&$form, $params, $result, $contactID,
877 $pending = FALSE, $isAdditionalAmount = FALSE
878 ) {
879 $transaction = new CRM_Core_Transaction();
880
881 $now = date('YmdHis');
882 $receiptDate = NULL;
883
884 if ($form->_values['event']['is_email_confirm']) {
885 $receiptDate = $now;
886 }
887 //CRM-4196
888 if ($isAdditionalAmount) {
889 $params['amount_level'] = $params['amount_level'] . ts(' (multiple participants)') . CRM_Core_DAO::VALUE_SEPARATOR;
890 }
891
892 $contribParams = array(
893 'contact_id' => $contactID,
894 'financial_type_id' => $form->_values['event']['financial_type_id'] ?
895 $form->_values['event']['financial_type_id'] : $params['financial_type_id'],
896 'receive_date' => $now,
897 'total_amount' => $params['amount'],
898 'tax_amount' => $params['tax_amount'],
899 'amount_level' => $params['amount_level'],
900 'invoice_id' => $params['invoiceID'],
901 'currency' => $params['currencyID'],
902 'source' => !empty($params['participant_source']) ? $params['participant_source'] : $params['description'],
903 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
904 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
905 );
906
907 if (empty($params['is_pay_later'])) {
908 $contribParams['payment_instrument_id'] = 1;
909 }
910
911 if (!$pending && $result) {
912 $contribParams += array(
913 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result),
914 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']),
915 'trxn_id' => $result['trxn_id'],
916 'receipt_date' => $receiptDate,
917 );
918 }
919
920 $allStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
921 $contribParams['contribution_status_id'] = array_search('Completed', $allStatuses);
922 if ($pending) {
923 $contribParams['contribution_status_id'] = array_search('Pending', $allStatuses);
924 }
925
926 $contribParams['is_test'] = 0;
927 if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
928 $contribParams['is_test'] = 1;
929 }
930
931 $contribID = NULL;
932 if (!empty($contribParams['invoice_id'])) {
933 $contribID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution',
934 $contribParams['invoice_id'],
935 'id',
936 'invoice_id'
937 );
938 }
939
940 $ids = array();
941 if ($contribID) {
942 $ids['contribution'] = $contribID;
943 $contribParams['id'] = $contribID;
944 }
945
946 //create an contribution address
947 if ($form->_contributeMode != 'notify' && empty($params['is_pay_later'])) {
948 $contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID);
949 }
950
951 // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
952 if (!empty($params['pcp_made_through_id']) || !empty($params['soft_credit_to'])) {
953
954 // if its due to pcp
955 if (!empty($params['pcp_made_through_id'])) {
956 $contribSoftContactId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
957 $params['pcp_made_through_id'],
958 'contact_id'
959 );
960 }
961 else {
962 $contribSoftContactId = CRM_Utils_Array::value('soft_credit_to', $params);
963 }
964
965 // Pass these details onto with the contribution to make them
966 // available at hook_post_process, CRM-8908
967 $contribParams['soft_credit_to'] = $params['soft_credit_to'] = $contribSoftContactId;
968 }
969 $contribParams['payment_processor'] = CRM_Utils_Array::value('payment_processor', $params);
970 $contribParams['skipLineItem'] = 1;
971 // create contribution record
972 $contribution = CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
973 // CRM-11124
974 CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, CRM_Utils_Array::value('amount_priceset_level_radio', $params, NULL));
975
976 // process soft credit / pcp pages
977 CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution);
978
979 $transaction->commit();
980
981 return $contribution;
982 }
983
984 /**
985 * Fix the Location Fields
986 *
987 * @param $params
988 * @param $fields
989 * @param $form
990 *
991 * @return void
992 * @access public
993 */
994 public static function fixLocationFields(&$params, &$fields, &$form) {
995 if (!empty($form->_fields)) {
996 foreach ($form->_fields as $name => $dontCare) {
997 $fields[$name] = 1;
998 }
999 }
1000
1001 if (is_array($fields)) {
1002 if (!array_key_exists('first_name', $fields)) {
1003 $nameFields = array('first_name', 'middle_name', 'last_name');
1004 foreach ($nameFields as $name) {
1005 $fields[$name] = 1;
1006 if (array_key_exists("billing_$name", $params)) {
1007 $params[$name] = $params["billing_{$name}"];
1008 $params['preserveDBName'] = TRUE;
1009 }
1010 }
1011 }
1012 }
1013
1014 // also add location name to the array
1015 if ($form->_values['event']['is_monetary']) {
1016 $params["address_name-{$form->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params);
1017 $fields["address_name-{$form->_bltID}"] = 1;
1018 }
1019 $fields["email-{$form->_bltID}"] = 1;
1020 $fields['email-Primary'] = 1;
1021
1022 //if its pay later or additional participant set email address as primary.
1023 if ((!empty($params['is_pay_later']) || empty($params['is_primary']) ||
1024 !$form->_values['event']['is_monetary'] ||
1025 $form->_allowWaitlist ||
1026 $form->_requireApproval
1027 ) && !empty($params["email-{$form->_bltID}"])) {
1028 $params['email-Primary'] = $params["email-{$form->_bltID}"];
1029 }
1030 }
1031
1032 /**
1033 * function to update contact fields
1034 *
1035 * @param $contactID
1036 * @param $params
1037 * @param $fields
1038 * @param $form
1039 *
1040 * @return void
1041 * @access public
1042 */
1043 public static function updateContactFields($contactID, $params, $fields, &$form) {
1044 //add the contact to group, if add to group is selected for a
1045 //particular uf group
1046
1047 // get the add to groups
1048 $addToGroups = array();
1049
1050 if (!empty($form->_fields)) {
1051 foreach ($form->_fields as $key => $value) {
1052 if (!empty($value['add_to_group_id'])) {
1053 $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id'];
1054 }
1055 }
1056 }
1057
1058 // check for profile double opt-in and get groups to be subscribed
1059 $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID);
1060
1061 foreach ($addToGroups as $k) {
1062 if (array_key_exists($k, $subscribeGroupIds)) {
1063 unset($addToGroups[$k]);
1064 }
1065 }
1066
1067 // since we are directly adding contact to group lets unset it from mailing
1068 if (!empty($addToGroups)) {
1069 foreach ($addToGroups as $groupId) {
1070 if (isset($subscribeGroupIds[$groupId])) {
1071 unset($subscribeGroupIds[$groupId]);
1072 }
1073 }
1074 }
1075 if ($contactID) {
1076 $ctype = CRM_Core_DAO::getFieldValue(
1077 'CRM_Contact_DAO_Contact',
1078 $contactID,
1079 'contact_type'
1080 );
1081
1082 if(array_key_exists('contact_id', $params) && empty($params['contact_id'])) {
1083 // we unset this here because the downstream function ignores the contactID we give it
1084 // if it is set & it is difficult to understand the implications of 'fixing' this downstream
1085 // but if we are passing a contact id into this function it's reasonable to assume we don't
1086 // want it ignored
1087 unset($params['contact_id']);
1088 }
1089
1090 $contactID = CRM_Contact_BAO_Contact::createProfileContact(
1091 $params,
1092 $fields,
1093 $contactID,
1094 $addToGroups,
1095 NULL,
1096 $ctype,
1097 TRUE
1098 );
1099 }
1100 else {
1101
1102 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
1103 if (!isset($params[$greeting . '_id'])) {
1104 $params[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
1105 }
1106 }
1107
1108 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params,
1109 $fields,
1110 NULL,
1111 $addToGroups,
1112 NULL,
1113 NULL,
1114 TRUE
1115 );
1116 $form->set('contactID', $contactID);
1117 }
1118
1119 //get email primary first if exist
1120 $subscribtionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params));
1121 if (!$subscribtionEmail['email']) {
1122 $subscribtionEmail['email'] = CRM_Utils_Array::value("email-{$form->_bltID}", $params);
1123 }
1124 // subscribing contact to groups
1125 if (!empty($subscribeGroupIds) && $subscribtionEmail['email']) {
1126 CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscribtionEmail, $contactID);
1127 }
1128
1129 return $contactID;
1130 }
1131
1132 public static function assignProfiles(&$form) {
1133 $addParticipantProfile = $form->get('addParticipantProfile');
1134 $primaryParticipantProfile = $form->get('primaryParticipantProfile');
1135 if (!empty($addParticipantProfile) || !empty($primaryParticipantProfile)) {
1136 $form->assign('addParticipantProfile', $addParticipantProfile);
1137 $form->assign('primaryParticipantProfile', $primaryParticipantProfile);
1138 return;
1139 }
1140
1141 $participantParams = $form->_params;
1142 $formattedValues = $profileFields = array();
1143 $count = 1;
1144 foreach ($participantParams as $participantNum => $participantValue) {
1145 if ($participantNum) {
1146 $prefix1 = 'additional';
1147 $prefix2 = 'additional_';
1148 } else {
1149 $prefix1 = '';
1150 $prefix2 = '';
1151 }
1152 if ($participantValue != 'skip') {
1153 //get the customPre profile info
1154 if (!empty($form->_values[$prefix2 . 'custom_pre_id'])) {
1155 $values = $groupName = array();
1156 CRM_Event_BAO_Event::displayProfile($participantValue,
1157 $form->_values[ $prefix2 . 'custom_pre_id'],
1158 $groupName,
1159 $values,
1160 $profileFields
1161 );
1162
1163 if (count($values)) {
1164 $formattedValues[$count][$prefix1 . 'CustomPre'] = $values;
1165 }
1166 $formattedValues[$count][$prefix1 . 'CustomPreGroupTitle'] = CRM_Utils_Array::value('groupTitle', $groupName);
1167 }
1168 //get the customPost profile info
1169 if (!empty($form->_values[$prefix2 . 'custom_post_id'])) {
1170 $values = $groupName = array();
1171 foreach ($form->_values[$prefix2 . 'custom_post_id'] as $gids) {
1172 $val = array();
1173 CRM_Event_BAO_Event::displayProfile($participantValue,
1174 $gids,
1175 $group,
1176 $val,
1177 $profileFields
1178 );
1179 $values[$gids] = $val;
1180 $groupName[$gids] = $group;
1181 }
1182
1183 if (count($values)) {
1184 $formattedValues[$count][$prefix1 . 'CustomPost'] = $values;
1185 }
1186
1187 if (isset($formattedValues[$count][$prefix1 . 'CustomPre'])) {
1188 $formattedValues[$count][$prefix1 . 'CustomPost'] = array_diff_assoc($formattedValues[$count][$prefix1 . 'CustomPost'],
1189 $formattedValues[$count][$prefix1 . 'CustomPre']
1190 );
1191 }
1192
1193 $formattedValues[$count][$prefix1 . 'CustomPostGroupTitle'] = $groupName;
1194 }
1195 $count++;
1196 }
1197 $form->_fields = $profileFields;
1198 }
1199 if (!empty($formattedValues) ) {
1200 $form->assign('primaryParticipantProfile', $formattedValues[1]);
1201 $form->set('primaryParticipantProfile', $formattedValues[1]);
1202 if ($count > 2) {
1203 unset($formattedValues[1]);
1204 $form->assign('addParticipantProfile', $formattedValues);
1205 $form->set('addParticipantProfile', $formattedValues);
1206 }
1207 }
1208 }
1209 }