From 9e582c5fc762bcf6c1742558f9294be7c2a52146 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 26 Mar 2021 16:23:25 +1300 Subject: [PATCH] [REF] Extract getMembershipTypeValues --- CRM/Member/Form/Membership.php | 83 +++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index b3f6b63bf33c..0214b6b0280c 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -991,7 +991,7 @@ public function submit(): void { $this->storeContactFields($this->_params); $this->beginPostProcess(); $endDate = NULL; - $membership = $calcDate = []; + $membership = []; $params = $softParams = $ids = []; @@ -1014,15 +1014,9 @@ public function submit(): void { $formValues ); $formValues['financial_type_id'] = $this->getFinancialTypeID(); - $membershipTypeValues = []; - foreach ($this->_memTypeSelected as $memType) { - $membershipTypeValues[$memType]['membership_type_id'] = $memType; - } $isQuickConfig = $this->_priceSet['is_quick_config']; - $termsByType = []; - $lineItem = [$this->order->getPriceSetID() => $this->order->getLineItems()]; $params['tax_amount'] = $this->order->getTotalTaxAmount(); @@ -1031,36 +1025,7 @@ public function submit(): void { $params = array_merge($params, $this->getFormMembershipParams()); - $joinDate = $formValues['join_date']; - $startDate = $formValues['start_date']; - $endDate = $formValues['end_date']; - - $calcDates = []; - foreach ($this->order->getMembershipLineItems() as $membershipLineItem) { - $memTypeNumTerms = $this->getSubmittedValue('num_terms') ?: $membershipLineItem['membership_num_terms']; - $calcDates[$membershipLineItem['membership_type_id']] = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipLineItem['membership_type_id'], - $joinDate, $startDate, $endDate, $memTypeNumTerms - ); - } - - foreach ($calcDates as $memType => $calcDate) { - foreach (['join_date', 'start_date', 'end_date'] as $d) { - //first give priority to form values then calDates. - $date = $formValues[$d] ?? NULL; - if (!$date) { - $date = $calcDate[$d] ?? NULL; - } - - $membershipTypeValues[$memType][$d] = CRM_Utils_Date::processDate($date); - } - } - - foreach ($this->_memTypeSelected as $memType) { - if (array_key_exists('max_related', $formValues)) { - // max related memberships - take from form or inherit from membership type - $membershipTypeValues[$memType]['max_related'] = $formValues['max_related'] ?? NULL; - } - } + $membershipTypeValues = $this->getMembershipParameters($formValues); // Retrieve the name and email of the current user - this will be the FROM for the receipt email [$userName] = CRM_Contact_BAO_Contact_Location::getEmailDetails(CRM_Core_Session::getLoggedInContactID()); @@ -1894,4 +1859,48 @@ protected function getCreatedMemberships(): array { return civicrm_api3('Membership', 'get', ['id' => ['IN' => $this->_membershipIDs]])['values']; } + /** + * @param array $formValues + * + * @return array + * @throws \CiviCRM_API3_Exception + */ + protected function getMembershipParameters(array $formValues): array { + $membershipTypeValues = []; + foreach ($this->_memTypeSelected as $memType) { + $membershipTypeValues[$memType]['membership_type_id'] = $memType; + } + $joinDate = $formValues['join_date']; + $startDate = $formValues['start_date']; + $endDate = $formValues['end_date']; + + $calcDates = []; + foreach ($this->order->getMembershipLineItems() as $membershipLineItem) { + $memTypeNumTerms = $this->getSubmittedValue('num_terms') ?: $membershipLineItem['membership_num_terms']; + $calcDates[$membershipLineItem['membership_type_id']] = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipLineItem['membership_type_id'], + $joinDate, $startDate, $endDate, $memTypeNumTerms + ); + } + + foreach ($calcDates as $memType => $calcDate) { + foreach (['join_date', 'start_date', 'end_date'] as $d) { + //first give priority to form values then calDates. + $date = $formValues[$d] ?? NULL; + if (!$date) { + $date = $calcDate[$d] ?? NULL; + } + + $membershipTypeValues[$memType][$d] = CRM_Utils_Date::processDate($date); + } + } + + foreach ($this->_memTypeSelected as $memType) { + if (array_key_exists('max_related', $formValues)) { + // max related memberships - take from form or inherit from membership type + $membershipTypeValues[$memType]['max_related'] = $formValues['max_related'] ?? NULL; + } + } + return $membershipTypeValues; + } + }