Remove legacy tpl assigns
[civicrm-core.git] / CRM / Member / Form / Membership.php
index f82bd2897d8ca5190c6fa6263c2fb7126c0084b8..5840a4c37a1db6587a6993c2ff81e481bc81ee90 100644 (file)
@@ -610,9 +610,7 @@ DESC limit 1");
 
     // Retrieve the name and email of the contact - this will be the TO for receipt email
     if ($this->_contactID) {
-      list($this->_memberDisplayName,
-        $this->_memberEmail
-        ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
+      [$this->_memberDisplayName, $this->_memberEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
 
       $this->assign('emailExists', $this->_memberEmail);
       $this->assign('displayName', $this->_memberDisplayName);
@@ -651,8 +649,8 @@ DESC limit 1");
   public static function formRule($params, $files, $self) {
     $errors = [];
 
-    $priceSetId = self::getPriceSetID($params);
-    $priceSetDetails = self::getPriceSetDetails($params);
+    $priceSetId = $self->getPriceSetID($params);
+    $priceSetDetails = $self->getPriceSetDetails($params);
 
     $selectedMemberships = self::getSelectedMemberships($priceSetDetails[$priceSetId], $params);
 
@@ -934,9 +932,6 @@ DESC limit 1");
 
       $valuesForForm = CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($form->_params);
       $form->assignVariables($valuesForForm, ['credit_card_exp_date', 'credit_card_type', 'credit_card_number']);
-
-      $form->assign('contributeMode', 'direct');
-      $form->assign('isAmountzero', 0);
       $form->assign('is_pay_later', 0);
       $form->assign('isPrimary', 1);
     }
@@ -1030,8 +1025,8 @@ DESC limit 1");
     $this->storeContactFields($this->_params);
     $this->beginPostProcess();
     $endDate = NULL;
-    $membershipTypes = $membership = $calcDate = [];
-    $membershipType = NULL;
+    $membership = $calcDate = [];
+
     $paymentInstrumentID = $this->_paymentProcessor['object']->getPaymentInstrumentID();
     $params = $softParams = $ids = [];
 
@@ -1184,13 +1179,8 @@ DESC limit 1");
         $this->_id,
         'Membership'
       );
-      $membershipTypes[$memType] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
-        $memType
-      );
     }
 
-    $membershipType = implode(', ', $membershipTypes);
-
     // Retrieve the name and email of the current user - this will be the FROM for the receipt email
     list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails(CRM_Core_Session::getLoggedInContactID());
 
@@ -1225,7 +1215,7 @@ DESC limit 1");
 
       if (empty($formValues['source'])) {
         $params['contribution_source'] = ts('%1 Membership: Offline signup (by %2)', [
-          1 => $membershipType,
+          1 => $this->getSelectedMembershipLabels(),
           2 => $userName,
         ]);
       }
@@ -1317,7 +1307,7 @@ DESC limit 1");
         $financialType->find(TRUE);
         $this->_params = $formValues;
 
-        $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this,
+        $contribution = self::processFormContribution($this,
           $paymentParams,
           NULL,
           [
@@ -1386,23 +1376,18 @@ DESC limit 1");
         // unset send-receipt option, since receipt will be sent when ipn is received.
         unset($formValues['send_receipt'], $formValues['send_receipt']);
         //as membership is pending set dates to null.
-        $memberDates = [
-          'join_date' => 'joinDate',
-          'start_date' => 'startDate',
-          'end_date' => 'endDate',
-        ];
-        foreach ($memberDates as $dv) {
-          $$dv = NULL;
-          foreach ($this->_memTypeSelected as $memType) {
-            $membershipTypeValues[$memType][$dv] = NULL;
-          }
+        foreach ($this->_memTypeSelected as $memType) {
+          $membershipTypeValues[$memType]['joinDate'] = NULL;
+          $membershipTypeValues[$memType]['startDate'] = NULL;
+          $membershipTypeValues[$memType]['endDate'] = NULL;
         }
+        $endDate = $startDate = NULL;
       }
       $now = date('YmdHis');
       $params['receive_date'] = date('Y-m-d H:i:s');
       $params['invoice_id'] = $formValues['invoiceID'];
       $params['contribution_source'] = ts('%1 Membership Signup: Credit card or direct debit (by %2)',
-        [1 => $membershipType, 2 => $userName]
+        [1 => $this->getSelectedMembershipLabels(), 2 => $userName]
       );
       $params['source'] = $formValues['source'] ?: $params['contribution_source'];
       $params['trxn_id'] = $result['trxn_id'] ?? NULL;
@@ -1465,33 +1450,34 @@ DESC limit 1");
     }
     else {
       $params['action'] = $this->_action;
-      $count = 0;
-      foreach ($this->_memTypeSelected as $memType) {
-        if ($count && !empty($formValues['record_contribution']) &&
-          ($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id))
-        ) {
-          $membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution;
+      foreach ($lineItem[$this->_priceSetId] as $id => $lineItemValues) {
+        if (empty($lineItemValues['membership_type_id'])) {
+          continue;
         }
 
         // @todo figure out why recieve_date isn't being set right here.
         if (empty($params['receive_date'])) {
           $params['receive_date'] = date('Y-m-d H:i:s');
         }
-        $membershipParams = array_merge($params, $membershipTypeValues[$memType]);
+        $membershipParams = array_merge($params, $membershipTypeValues[$lineItemValues['membership_type_id']]);
 
         if (!empty($softParams)) {
           $membershipParams['soft_credit'] = $softParams;
         }
+        unset($membershipParams['contribution_status_id']);
+        $membershipParams['skipLineItem'] = TRUE;
+        unset($membershipParams['lineItems']);
         // @todo stop passing $ids (membership and userId only are set above)
         $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
-        $params['contribution'] = $membershipParams['contribution'] ?? NULL;
-        unset($params['lineItems']);
-        // skip line item creation for next interation since line item(s) are already created.
-        $params['skipLineItem'] = TRUE;
+        $lineItem[$this->_priceSetId][$id]['entity_id'] = $membership->id;
+        $lineItem[$this->_priceSetId][$id]['entity_table'] = 'civicrm_membership';
 
         $this->_membershipIDs[] = $membership->id;
-        $createdMemberships[$memType] = $membership;
-        $count++;
+        $createdMemberships[$membership->membership_type_id] = $membership;
+      }
+      $params['lineItems'] = $lineItem;
+      if (!empty($formValues['record_contribution'])) {
+        CRM_Member_BAO_Membership::recordMembershipContribution($params);
       }
     }
     $isRecur = $params['is_recur'] ?? NULL;
@@ -1843,4 +1829,218 @@ DESC limit 1");
     return $customValues;
   }
 
+  /**
+   * Get the selected memberships as a string of labels.
+   *
+   * @return string
+   */
+  protected function getSelectedMembershipLabels(): string {
+    $return = [];
+    foreach ($this->_memTypeSelected as $membershipTypeID) {
+      $return[] = $this->allMembershipTypeDetails[$membershipTypeID]['name'];
+    }
+    return implode(', ', $return);
+  }
+
+  /**
+   * Legacy contribution processing function.
+   *
+   * This is copied from a shared function in order to clean it up. Most of the
+   * stuff in it, maybe all except the ContributionRecur create is
+   * not applicable to this form & can be removed in follow up cleanup.
+   *
+   * It's like the contribution create being done here is actively bad and
+   * being fixed later.
+   *
+   * @param CRM_Core_Form $form
+   * @param array $params
+   * @param array $result
+   * @param array $contributionParams
+   *   Parameters to be passed to contribution create action.
+   *   This differs from params in that we are currently adding params to it and 1) ensuring they are being
+   *   passed consistently & 2) documenting them here.
+   *   - contact_id
+   *   - line_item
+   *   - is_test
+   *   - campaign_id
+   *   - contribution_page_id
+   *   - source
+   *   - payment_type_id
+   *   - thankyou_date (not all forms will set this)
+   *
+   * @param CRM_Financial_DAO_FinancialType $financialType
+   * @param bool $online
+   *   Is the form a front end form? If so set a bunch of unpredictable things that should be passed in from the form.
+   *
+   * @param int $billingLocationID
+   *   ID of billing location type.
+   * @param bool $isRecur
+   *   Is this recurring?
+   *
+   * @return \CRM_Contribute_DAO_Contribution
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public static function processFormContribution(
+    &$form,
+    $params,
+    $result,
+    $contributionParams,
+    $financialType,
+    $online,
+    $billingLocationID,
+    $isRecur
+  ) {
+    $transaction = new CRM_Core_Transaction();
+    $contactID = $contributionParams['contact_id'];
+
+    $isEmailReceipt = !empty($form->_values['is_email_receipt']);
+    $isSeparateMembershipPayment = !empty($params['separate_membership_payment']);
+    $pledgeID = !empty($params['pledge_id']) ? $params['pledge_id'] : $form->_values['pledge_id'] ?? NULL;
+    if (!$isSeparateMembershipPayment && !empty($form->_values['pledge_block_id']) &&
+      (!empty($params['is_pledge']) || $pledgeID)) {
+      $isPledge = TRUE;
+    }
+    else {
+      $isPledge = FALSE;
+    }
+
+    // add these values for the recurringContrib function ,CRM-10188
+    $params['financial_type_id'] = $financialType->id;
+
+    $contributionParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $billingLocationID);
+
+    //@todo - this is being set from the form to resolve CRM-10188 - an
+    // eNotice caused by it not being set @ the front end
+    // however, we then get it being over-written with null for backend contributions
+    // a better fix would be to set the values in the respective forms rather than require
+    // a function being shared by two forms to deal with their respective values
+    // moving it to the BAO & not taking the $form as a param would make sense here.
+    if (!isset($params['is_email_receipt']) && $isEmailReceipt) {
+      $params['is_email_receipt'] = $isEmailReceipt;
+    }
+    $params['is_recur'] = $isRecur;
+    $params['payment_instrument_id'] = $contributionParams['payment_instrument_id'] ?? NULL;
+    $recurringContributionID = CRM_Contribute_Form_Contribution_Confirm::processRecurringContribution($form, $params, $contactID, $financialType);
+
+    $now = date('YmdHis');
+    $receiptDate = $params['receipt_date'] ?? NULL;
+    if ($isEmailReceipt) {
+      $receiptDate = $now;
+    }
+
+    if (isset($params['amount'])) {
+      $contributionParams = array_merge(CRM_Contribute_Form_Contribution_Confirm::getContributionParams(
+        $params, $financialType->id,
+        $result, $receiptDate,
+        $recurringContributionID), $contributionParams
+      );
+      $contributionParams['non_deductible_amount'] = CRM_Contribute_Form_Contribution_Confirm::getNonDeductibleAmount($params, $financialType, $online, $form);
+      $contributionParams['skipCleanMoney'] = TRUE;
+      // @todo this is the wrong place for this - it should be done as close to form submission
+      // as possible
+      $contributionParams['total_amount'] = $params['amount'];
+
+      $contribution = CRM_Contribute_BAO_Contribution::add($contributionParams);
+
+      $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
+      $invoicing = $invoiceSettings['invoicing'] ?? NULL;
+      if ($invoicing) {
+        $dataArray = [];
+        // @todo - interrogate the line items passed in on the params array.
+        // No reason to assume line items will be set on the form.
+        foreach ($form->_lineItem as $lineItemKey => $lineItemValue) {
+          foreach ($lineItemValue as $key => $value) {
+            if (isset($value['tax_amount']) && isset($value['tax_rate'])) {
+              if (isset($dataArray[$value['tax_rate']])) {
+                $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + $value['tax_amount'];
+              }
+              else {
+                $dataArray[$value['tax_rate']] = $value['tax_amount'];
+              }
+            }
+          }
+        }
+        $smarty = CRM_Core_Smarty::singleton();
+        $smarty->assign('dataArray', $dataArray);
+        $smarty->assign('totalTaxAmount', $params['tax_amount'] ?? NULL);
+      }
+
+      // lets store it in the form variable so postProcess hook can get to this and use it
+      $form->_contributionID = $contribution->id;
+    }
+
+    // process soft credit / pcp params first
+    CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form);
+
+    //CRM-13981, processing honor contact into soft-credit contribution
+    CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution);
+
+    if ($isPledge) {
+      $form = CRM_Contribute_Form_Contribution_Confirm::handlePledge($form, $params, $contributionParams, $pledgeID, $contribution, $isEmailReceipt);
+    }
+
+    if ($online && $contribution) {
+      CRM_Core_BAO_CustomValueTable::postProcess($params,
+        'civicrm_contribution',
+        $contribution->id,
+        'Contribution'
+      );
+    }
+    elseif ($contribution) {
+      //handle custom data.
+      $params['contribution_id'] = $contribution->id;
+      if (!empty($params['custom']) &&
+        is_array($params['custom'])
+      ) {
+        CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id);
+      }
+    }
+    // Save note
+    if ($contribution && !empty($params['contribution_note'])) {
+      $noteParams = [
+        'entity_table' => 'civicrm_contribution',
+        'note' => $params['contribution_note'],
+        'entity_id' => $contribution->id,
+        'contact_id' => $contribution->contact_id,
+      ];
+
+      CRM_Core_BAO_Note::add($noteParams, []);
+    }
+
+    if (isset($params['related_contact'])) {
+      $contactID = $params['related_contact'];
+    }
+    elseif (isset($params['cms_contactID'])) {
+      $contactID = $params['cms_contactID'];
+    }
+
+    //create contribution activity w/ individual and target
+    //activity w/ organisation contact id when onbelf, CRM-4027
+    $actParams = [];
+    $targetContactID = NULL;
+    if (!empty($params['onbehalf_contact_id'])) {
+      $actParams = [
+        'source_contact_id' => $params['onbehalf_contact_id'],
+        'on_behalf' => TRUE,
+      ];
+      $targetContactID = $contribution->contact_id;
+    }
+
+    // create an activity record
+    if ($contribution) {
+      CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution', $targetContactID, $actParams);
+    }
+
+    $transaction->commit();
+    // CRM-13074 - create the CMSUser after the transaction is completed as it
+    // is not appropriate to delete a valid contribution if a user create problem occurs
+    CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params,
+      $contactID,
+      'email-' . $billingLocationID
+    );
+    return $contribution;
+  }
+
 }