Skip to content

Commit

Permalink
Merge pull request #19924 from eileenmcnaughton/mem_ex
Browse files Browse the repository at this point in the history
[REF] Extract getMembershipTypeValues
  • Loading branch information
monishdeb authored Mar 31, 2021
2 parents 8e88fa0 + 9e582c5 commit f0cf4ce
Showing 1 changed file with 46 additions and 37 deletions.
83 changes: 46 additions & 37 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ public function submit(): void {
$this->storeContactFields($this->_params);
$this->beginPostProcess();
$endDate = NULL;
$membership = $calcDate = [];
$membership = [];

$params = $softParams = $ids = [];

Expand All @@ -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();
Expand All @@ -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());
Expand Down Expand Up @@ -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;
}

}

0 comments on commit f0cf4ce

Please sign in to comment.