Merge pull request #16424 from eileenmcnaughton/cont_setting_handling
[civicrm-core.git] / CRM / Event / Form / Participant.php
index 121286a99a38900e26ae55b5fd342bb3a9908b1a..929a130879da3dc3af5e45842869d4413149d0c8 100644 (file)
  */
 
 /**
- *
+ * Back office participant form.
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
 /**
- * This class generates form components for processing a participation
- * in an event
+ * Back office participant form.
  */
 class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment {
 
+  /**
+   * Participant ID - use getParticipantID.
+   *
+   * @var int
+   */
+  public $_pId;
+
+  /**
+   * ID of discount record.
+   *
+   * @var int
+   */
+  public $_discountId;
+
   public $useLivePageJS = TRUE;
 
   /**
@@ -191,6 +204,13 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
    */
   public $_onlinePendingContributionId = NULL;
 
+  /**
+   * Stored participant record.
+   *
+   * @var array
+   */
+  protected $participantRecord;
+
   /**
    * Explicitly declare the entity api name.
    */
@@ -1662,19 +1682,10 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
         $form->addElement('hidden', 'hidden_feeblock', 1);
       }
 
-      $eventfullMsg = CRM_Event_BAO_Participant::eventFullMessage($form->_eventId, $form->_pId);
+      $eventfullMsg = CRM_Event_BAO_Participant::eventFullMessage($form->_eventId, $this->getParticipantID());
       $form->addElement('hidden', 'hidden_eventFullMsg', $eventfullMsg, ['id' => 'hidden_eventFullMsg']);
     }
 
-    if ($form->_pId) {
-      if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
-        $form->_pId, 'contribution_id', 'participant_id'
-      )
-      ) {
-        $form->_online = !$form->isBackOffice;
-      }
-    }
-
     if ($form->_isPaidEvent) {
       $params = ['id' => $form->_eventId];
       CRM_Event_BAO_Event::retrieve($params, $event);
@@ -1712,10 +1723,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
           FALSE,
           ['class' => "crm-select2"]
         );
-
-        if ($form->_online) {
-          $element->freeze();
-        }
       }
       if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
         && !CRM_Utils_Array::value('fee', $form->_values)
@@ -1817,30 +1824,24 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
    *
    * @return array
    *
-   * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
   protected function preparePaidEventProcessing($params): array {
     $participantStatus = CRM_Event_PseudoConstant::participantStatus();
-    $contributionParams = ['skipCleanMoney' => TRUE];
+    $contributionParams = [
+      'skipCleanMoney' => TRUE,
+      'revenue_recognition_date' => $this->getRevenueRecognitionDate(),
+    ];
     $lineItem = [];
     $additionalParticipantDetails = [];
-    if (Civi::settings()->get('deferred_revenue_enabled')) {
-      $eventStartDate = $this->getEventValue('start_date');
-      if (strtotime($eventStartDate) > strtotime(date('Ymt'))) {
-        $contributionParams['revenue_recognition_date'] = date('Ymd', strtotime($eventStartDate));
-      }
-    }
+
     if ($this->isPaymentOnExistingContribution()) {
-      $participantBAO = new CRM_Event_BAO_Participant();
-      $participantBAO->id = $this->_id;
-      $participantBAO->find(TRUE);
-      $contributionParams['total_amount'] = $participantBAO->fee_amount;
+      $contributionParams['total_amount'] = $this->getParticipantValue('fee_amount');
 
       $params['discount_id'] = NULL;
       //re-enter the values for UPDATE mode
-      $params['fee_level'] = $params['amount_level'] = $participantBAO->fee_level;
-      $params['fee_amount'] = $participantBAO->fee_amount;
+      $params['fee_level'] = $params['amount_level'] = $this->getParticipantValue('fee_level');
+      $params['fee_amount'] = $this->getParticipantValue('fee_amount');
       if (isset($params['priceSetId'])) {
         $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id);
       }
@@ -2046,19 +2047,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
         'invoice_id'
       );
     }
+    $contribParams['revenue_recognition_date'] = $this->getRevenueRecognitionDate();
 
-    if (Civi::settings()->get('deferred_revenue_enabled')) {
-      $eventStartDate = CRM_Utils_Array::value(
-        'start_date',
-        CRM_Utils_Array::value(
-          'event',
-          $form->_values
-        )
-      );
-      if (strtotime($eventStartDate) > strtotime(date('Ymt'))) {
-        $contribParams['revenue_recognition_date'] = date('Ymd', strtotime($eventStartDate));
-      }
-    }
     //create an contribution address
     // The concept of contributeMode is deprecated. Elsewhere we use the function processBillingAddress() - although
     // currently that is only inherited by back-office forms.
@@ -2182,7 +2172,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
    * @return bool
    */
   protected function isPaymentOnExistingContribution(): bool {
-    return ($this->_id && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId;
+    return ($this->getParticipantID() && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId;
   }
 
   /**
@@ -2200,4 +2190,45 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
     return $this->_event[$fieldName];
   }
 
+  /**
+   * Get a value from the existing participant record (applies to edits).
+   *
+   * @param string $fieldName
+   *
+   * @return array
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function getParticipantValue($fieldName) {
+    if (!$this->participantRecord) {
+      $this->participantRecord = civicrm_api3('Participant', 'get', ['id' => $this->_id]);
+    }
+    return $this->participantRecord[$fieldName];
+  }
+
+  /**
+   * Get id of participant being edited.
+   *
+   * @return int|null
+   */
+  protected function getParticipantID() {
+    return $this->_id ?? $this->_pId;
+  }
+
+  /**
+   * Get the value for the revenue recognition date field.
+   *
+   * @return string
+   *
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function getRevenueRecognitionDate() {
+    if (Civi::settings()->get('deferred_revenue_enabled')) {
+      $eventStartDate = $this->getEventValue('start_date');
+      if (strtotime($eventStartDate) > strtotime(date('Ymt'))) {
+        return date('Ymd', strtotime($eventStartDate));
+      }
+    }
+    return '';
+  }
+
 }