CRM-19213 fix payment_instrument_id on event & add first test (shell really)
[civicrm-core.git] / CRM / Event / Form / Registration / Confirm.php
index 0485d4c6a1578b290fceb99b0897d8813ad3fb7a..e08c82d13592667bbee37708559938829c636f97 100644 (file)
@@ -99,9 +99,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
           $params['discountAmount'] = $this->_params[0]['discountAmount'];
           $params['discountMessage'] = $this->_params[0]['discountMessage'];
         }
-        if (!empty($this->_params[0]['amount_priceset_level_radio'])) {
-          $params['amount_priceset_level_radio'] = $this->_params[0]['amount_priceset_level_radio'];
-        }
+
         $params['amount_level'] = $this->_params[0]['amount_level'];
         $params['currencyID'] = $this->_params[0]['currencyID'];
 
@@ -610,7 +608,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
           }
 
           //passing contribution id is already registered.
-          $contribution = self::processContribution($this, $value, $result, $contactID, $pending, $isAdditionalAmount);
+          $contribution = self::processContribution($this, $value, $result, $contactID, $pending, $isAdditionalAmount, $this->_paymentProcessor);
           $value['contributionID'] = $contribution->id;
           $value['contributionTypeID'] = $contribution->financial_type_id;
           $value['receive_date'] = $contribution->receive_date;
@@ -945,7 +943,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
    */
   public static function processContribution(
     &$form, $params, $result, $contactID,
-    $pending = FALSE, $isAdditionalAmount = FALSE
+    $pending = FALSE, $isAdditionalAmount = FALSE,
+    $paymentProcessor = NULL
   ) {
     $transaction = new CRM_Core_Transaction();
 
@@ -974,8 +973,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
       'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
     );
 
-    if (empty($params['is_pay_later'])) {
-      $contribParams['payment_instrument_id'] = 1;
+    if ($paymentProcessor) {
+      $contribParams['payment_instrument_id'] = $paymentProcessor['payment_instrument_id'];
     }
 
     if (!$pending && $result) {
@@ -1013,6 +1012,18 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
       $contribParams['id'] = $contribID;
     }
 
+    if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
+      $eventStartDate = CRM_Utils_Array::value(
+        'start_date',
+        CRM_Utils_Array::value(
+          'event',
+          $form->_values
+        )
+      );
+      if ($eventStartDate) {
+        $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.
@@ -1025,7 +1036,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
     // create contribution record
     $contribution = CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
     // CRM-11124
-    CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, CRM_Utils_Array::value('amount_priceset_level_radio', $params, NULL));
+    CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, NULL, CRM_Price_BAO_PriceSet::parseFirstPriceSetValueIDFromParams($params));
 
     // process soft credit / pcp pages
     if (!empty($params['pcp_made_through_id'])) {
@@ -1041,6 +1052,11 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
   /**
    * Fix the Location Fields.
    *
+   * @todo Reconcile with the contribution method formatParamsForPaymentProcessor
+   * rather than adding different logic to check when to keep the billing
+   * fields. There might be a difference in handling guest/multiple
+   * participants though.
+   *
    * @param array $params
    * @param array $fields
    * @param CRM_Core_Form $form
@@ -1052,6 +1068,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
       }
     }
 
+    // If there's no 'first_name' in the profile then overwrite the names from
+    // the billing fields (if they are set)
     if (is_array($fields)) {
       if (!array_key_exists('first_name', $fields)) {
         $nameFields = array('first_name', 'middle_name', 'last_name');
@@ -1065,11 +1083,12 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
       }
     }
 
-    // also add location name to the array
-    if ($form->_values['event']['is_monetary']) {
+    // Add the billing names to the billing address, if a billing name is set
+    if (!empty($params['billing_first_name'])) {
       $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);
       $fields["address_name-{$form->_bltID}"] = 1;
     }
+
     $fields["email-{$form->_bltID}"] = 1;
     $fields['email-Primary'] = 1;
 
@@ -1259,4 +1278,26 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
     }
   }
 
+  /**
+   * Submit in test mode.
+   *
+   * @param $params
+   */
+  public static function testSubmit($params) {
+    $form = new CRM_Event_Form_Registration_Confirm();
+    // This way the mocked up controller ignores the session stuff.
+    $_SERVER['REQUEST_METHOD'] = 'GET';
+    $_REQUEST['id'] = $form->_eventId = $params['id'];
+    $form->controller = new CRM_Event_Controller_Registration();
+    $form->_params = $params['params'];
+    $form->set('params', $params['params']);
+    $form->_values['custom_pre_id'] = array();
+    $form->_values['custom_post_id'] = array();
+    $form->_contributeMode = $params['contributeMode'];
+    $eventParams = array('id' => $params['id']);
+    CRM_Event_BAO_Event::retrieve($eventParams, $form->_values['event']);
+    $form->set('registerByID', $params['registerByID']);
+    $form->postProcess();
+  }
+
 }