Fix theoretical regression
[civicrm-core.git] / CRM / Event / Form / Participant.php
index 1a42efd40f65c8fad6f6f4acdfd108f8c565d68c..d1d5a95861b2ac879c94b844915c761285911ae0 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 {
 
@@ -108,60 +107,70 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
 
   /**
    * If event is paid or unpaid.
+   *
    * @var bool
    */
   public $_isPaidEvent;
 
   /**
    * Page action.
+   *
    * @var int
    */
   public $_action;
 
   /**
    * Role Id.
+   *
    * @var int
    */
   protected $_roleId = NULL;
 
   /**
    * Event Type Id.
+   *
    * @var int
    */
   protected $_eventTypeId = NULL;
 
   /**
    * Participant status Id.
+   *
    * @var int
    */
   protected $_statusId = NULL;
 
   /**
    * Cache all the participant statuses.
+   *
    * @var array
    */
   protected $_participantStatuses;
 
   /**
    * Participant mode.
+   *
    * @var string
    */
   public $_mode = NULL;
 
   /**
    * Event ID preselect.
+   *
    * @var int
    */
   public $_eID = NULL;
 
   /**
    * Line Item for Price Set.
+   *
    * @var array
    */
   public $_lineItem = NULL;
 
   /**
    * Contribution mode for event registration for offline mode.
+   *
    * @var string
    * @deprecated
    */
@@ -171,30 +180,35 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
 
   /**
    * Store id of role custom data type ( option value )
+   *
    * @var int
    */
   protected $_roleCustomDataTypeID;
 
   /**
    * Store id of event Name custom data type ( option value)
+   *
    * @var int
    */
   protected $_eventNameCustomDataTypeID;
 
   /**
    * Selected discount id.
+   *
    * @var int
    */
   public $_originalDiscountId = NULL;
 
   /**
    * Event id.
+   *
    * @var int
    */
   public $_eventId = NULL;
 
   /**
    * Id of payment, if any
+   *
    * @var int
    */
   public $_paymentId = NULL;
@@ -229,8 +243,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
   /**
    * Set variables up before form is built.
    *
-   * @return void
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function preProcess() {
     parent::preProcess();
@@ -1000,6 +1014,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
     if (!empty($params['contact_id'])) {
       $this->_contactID = $this->_contactId = $params['contact_id'];
     }
+    if (!$this->_priceSetId && $this->_isPaidEvent) {
+      CRM_Core_Error::deprecatedFunctionWarning('this should never be true, handling to be removed');
+    }
     if ($this->_priceSetId && $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) {
       $this->_quickConfig = $isQuickConfig;
     }
@@ -1041,7 +1058,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
     //participant method (addParticipant)
     $this->_params['participant_status_id'] = $params['status_id'];
     $this->_params['participant_role_id'] = is_array($params['role_id']) ? $params['role_id'] : explode(',', $params['role_id']);
-    $this->_params['participant_register_date'] = $params['register_date'];
     $roleIdWithSeparator = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['participant_role_id']);
 
     $now = date('YmdHis');
@@ -1068,8 +1084,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
       $fields['email-Primary'] = 1;
       $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail;
 
-      $params['register_date'] = $now;
-
       // now set the values for the billing location.
       foreach ($this->_fields as $name => $dontCare) {
         $fields[$name] = 1;
@@ -1212,7 +1226,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
       //CRM-15372 patch to fix fee amount replacing amount
       $this->_params['fee_amount'] = $this->_params['amount'];
 
-      $participants[] = $this->addParticipant($this, $contactID);
+      $participants[] = $this->addParticipant($this, $this->_params, $contactID);
 
       //add custom data for participant
       CRM_Core_BAO_CustomValueTable::postProcess($this->_params,
@@ -1411,10 +1425,14 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
               $lineItem[$this->_priceSetId][$lineKey] = $line;
             }
             CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant');
-            CRM_Contribute_BAO_Contribution::addPayments($contributions);
           }
         }
       }
+      foreach ($contributions as $contribution) {
+        if ('Partially paid' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id)) {
+          CRM_Contribute_BAO_Contribution::addPayments($contribution);
+        }
+      }
     }
 
     $updateStatusMsg = NULL;
@@ -1432,9 +1450,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
     $sent = [];
     $notSent = [];
     if (!empty($params['send_receipt'])) {
-      if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
-        $receiptFrom = $params['from_email_address'];
-      }
 
       $this->assign('module', 'Event Registration');
       $this->assignEventDetailsToTpl($params['event_id'], CRM_Utils_Array::value('role_id', $params), CRM_Utils_Array::value('receipt_text', $params), $this->_isPaidEvent);
@@ -1568,6 +1583,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
         // try to send emails only if email id is present
         // and the do-not-email option is not checked for that contact
         if ($this->_contributorEmail and !$this->_toDoNotEmail) {
+          if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
+            $receiptFrom = $params['from_email_address'];
+          }
           $sendTemplateParams['from'] = $receiptFrom;
           $sendTemplateParams['toName'] = $this->_contributorDisplayName;
           $sendTemplateParams['toEmail'] = $this->_contributorEmail;
@@ -1829,15 +1847,13 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
    */
   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()) {
       $contributionParams['total_amount'] = $this->getParticipantValue('fee_amount');
 
@@ -2050,19 +2066,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.
@@ -2092,40 +2097,26 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
    * Process the participant.
    *
    * @param CRM_Core_Form $form
+   * @param array $params
    * @param int $contactID
    *
    * @return \CRM_Event_BAO_Participant
+   * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  protected function addParticipant(&$form, $contactID) {
-    if (empty($form->_params)) {
-      return NULL;
-    }
-    $params = $form->_params;
+  protected function addParticipant(&$form, $params, $contactID) {
     $transaction = new CRM_Core_Transaction();
 
-    // handle register date CRM-4320
-    $registerDate = NULL;
-    if (!empty($form->_allowConfirmation) && $form->_participantId) {
-      $registerDate = $params['participant_register_date'];
-    }
-    elseif (!empty($params['participant_register_date']) &&
-      is_array($params['participant_register_date']) &&
-      !empty($params['participant_register_date'])
-    ) {
-      $registerDate = CRM_Utils_Date::format($params['participant_register_date']);
-    }
-
     $participantFields = CRM_Event_DAO_Participant::fields();
-    $participantParams = array(
+    $participantParams = [
       'id' => CRM_Utils_Array::value('participant_id', $params),
       'contact_id' => $contactID,
       'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'],
       'status_id' => CRM_Utils_Array::value('participant_status',
         $params, 1
       ),
-      'role_id' => CRM_Utils_Array::value('participant_role_id', $params) ?: self::getDefaultRoleID(),
-      'register_date' => ($registerDate) ? $registerDate : date('YmdHis'),
+      'role_id' => CRM_Utils_Array::value('participant_role_id', $params) ?: CRM_Event_BAO_Participant::getDefaultRoleID(),
+      'register_date' => $params['register_date'],
       'source' => CRM_Utils_String::ellipsify(
         isset($params['participant_source']) ? CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params),
         $participantFields['participant_source']['maxlength']
@@ -2137,7 +2128,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
       'discount_id' => CRM_Utils_Array::value('discount_id', $params),
       'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
       'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
-    );
+    ];
 
     if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
       $participantParams['is_test'] = 1;
@@ -2228,4 +2219,21 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
     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 '';
+  }
+
 }