Merge pull request #3943 from civicrm/master
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.php
index c5eb229956f7eced7c4d63777971463582bcdca7..7ff20fb1ac92e1ec2d55535f496cdaf36343aaf6 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -40,6 +40,8 @@
  */
 class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
 
+  public $useLivePageJS = TRUE;
+
   protected $_contactId = NULL;
 
   protected $_contributorDisplayName = NULL;
@@ -52,7 +54,7 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
 
   protected $fromEmailId = NULL;
 
-  protected $_eventId = NULL;
+  public $_eventId = NULL;
 
   public $_action = NULL;
 
@@ -75,6 +77,12 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
     $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
 
     $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id');
+    if (!$this->_contributionId) {
+      if ($primaryParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'registered_by_id')) {
+        $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $primaryParticipantId, 'contribution_id', 'participant_id');
+      }
+    }
+
     if ($this->_contributionId) {
       $this->_isPaidEvent = TRUE;
     }
@@ -94,7 +102,19 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
     $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantId, 'event');
     $this->_paidAmount = $paymentInfo['paid'];
     $this->assign('paymentInfo', $paymentInfo);
-    CRM_Core_Resources::singleton()->addSetting(array('feePaid' => $this->_paidAmount));
+    $this->assign('feePaid', $this->_paidAmount);
+
+    $ids = CRM_Event_BAO_Participant::getParticipantIds($this->_contributionId);
+    if (count($ids) > 1) {
+      $total = 0;
+      foreach ($ids as $val) {
+        $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant');
+      }
+      $this->assign('totalLineTotal', $total);
+
+      $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($this->_participantId, 'civicrm_participant');
+      $this->assign('lineItemTotal', $lineItemTotal);
+    }
 
     $title = "Change selections for {$this->_contributorDisplayName}";
     if ($title) {
@@ -108,7 +128,7 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
     CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
     $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);
 
-    $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId);
+    $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId, FALSE);
     if (!empty($priceSetValues)) {
       $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues);
     }
@@ -125,14 +145,11 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
   }
 
   public function buildQuickForm() {
-    CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
 
     $statuses = CRM_Event_PseudoConstant::participantStatus();
-    CRM_Core_Resources::singleton()->addSetting(array(
-        'partiallyPaid' => array_search('Partially paid', $statuses),
-        'pendingRefund' => array_search('Pending refund', $statuses),
-        'participantStatus' => $this->_participantStatus
-      ));
+    $this->assign('partiallyPaid',  array_search('Partially paid', $statuses));
+    $this->assign('pendingRefund',  array_search('Pending refund', $statuses));
+    $this->assign('participantStatus', $this->_participantStatus);
 
     $config = CRM_Core_Config::singleton();
     $this->assign('currencySymbol',  $config->defaultCurrencySymbol);
@@ -180,12 +197,13 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
       'isDefault' => TRUE,
     );
 
-    $buttons[] = array(
-      'type' => 'upload',
-      'name' => ts('Save and Record Payment'),
-      'subName' => 'new'
-    );
-
+    if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_participantId)) {
+      $buttons[] = array(
+        'type' => 'upload',
+        'name' => ts('Save and Record Payment'),
+        'subName' => 'new'
+      );
+    }
     $buttons[] = array(
       'type' => 'cancel',
       'name' => ts('Cancel'),
@@ -195,6 +213,13 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
     $this->addFormRule(array('CRM_Event_Form_ParticipantFeeSelection', 'formRule'), $this);
   }
 
+  /**
+   * @param $fields
+   * @param $files
+   * @param $self
+   *
+   * @return array
+   */
   static function formRule($fields, $files, $self) {
     $errors = array();
     return $errors;
@@ -232,13 +257,25 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->getButtonName('upload', 'new')) {
       $session = CRM_Core_Session::singleton();
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/payment/add',
+      $session->pushUserContext(CRM_Utils_System::url('civicrm/payment/add',
           "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
         ));
     }
   }
 
+  /**
+   * @param $params
+   *
+   * @return mixed
+   */
   function emailReceipt(&$params) {
+    $updatedLineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant', NULL, FALSE);
+    $lineItem = array();
+    if ($updatedLineItem) {
+      $lineItem[] = $updatedLineItem;
+    }
+    $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
+
     // offline receipt sending
     if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
       $receiptFrom = $params['from_email_address'];
@@ -341,4 +378,4 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
     list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
     return $mailSent;
   }
-}
\ No newline at end of file
+}