From 583a3da38bfa2bc2eeaf51b4eeb87faabf0ca48d Mon Sep 17 00:00:00 2001 From: Omar Hussein Date: Fri, 1 Sep 2023 21:00:15 +0300 Subject: [PATCH] CIWEMB-469: Fix next period tab line item amounts --- .../Page/EditContributionRecurLineItems.php | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/CRM/MembershipExtras/Page/EditContributionRecurLineItems.php b/CRM/MembershipExtras/Page/EditContributionRecurLineItems.php index 95e3e532..f8a0fabb 100755 --- a/CRM/MembershipExtras/Page/EditContributionRecurLineItems.php +++ b/CRM/MembershipExtras/Page/EditContributionRecurLineItems.php @@ -354,25 +354,42 @@ private function getNextPeriodLineItems() { 'is_removed' => 0, ]; - $installments = CRM_Utils_Array::value('installments', $this->contribRecur, 0); - if ($installments <= 1) { - $conditions['end_date'] = ['IS NULL' => 1]; + $paymentPlanLinkedScheme = $this->getPaymentPlanLinkedSchemeIfExists(); + if (!empty($paymentPlanLinkedScheme)) { + // Usually the recurring contribution `installments` match the number + // of instalments configured on the payment scheme, but we do this as + // safeguarding measure in case they did not match. + $installments = count($paymentPlanLinkedScheme['instalments']); + } + else { + $installments = CRM_Utils_Array::value('installments', $this->contribRecur, 0); + if ($installments <= 1) { + $conditions['end_date'] = ['IS NULL' => 1]; + } } - $nextLineItems = $this->getLineItems($conditions); + $useLatestMembershipPrice = Civi::settings()->get('membershipextras_paymentplan_use_membership_latest_price'); + $nextLineItems = $this->getLineItems($conditions); foreach ($nextLineItems as &$nextLineItem) { if (!empty($nextLineItem['related_membership']['id'])) { $relatedMembershipId = $nextLineItem['related_membership']['id']; $autoUpgradableMembershipChecker = new CRM_MembershipExtras_Service_AutoUpgradableMembershipChecker(); - $upgradedMembershipTypeId = $autoUpgradableMembershipChecker->calculateMembershipTypeToUpgradeTo($relatedMembershipId); + $nextPeriodMembershipTypeId = $autoUpgradableMembershipChecker->calculateMembershipTypeToUpgradeTo($relatedMembershipId); - if (!empty($upgradedMembershipTypeId)) { + if (empty($nextPeriodMembershipTypeId) && $useLatestMembershipPrice) { + $nextPeriodMembershipTypeId = $nextLineItem['related_membership']['related_membership_type']['id']; + } + + // If the membership will be upgraded in the next period, or if + // "use latest membership price" setting is used, then we need + // to calculate the line item amount since it will be different. + if (!empty($nextPeriodMembershipTypeId)) { $membershipType = civicrm_api3('MembershipType', 'get', [ 'sequential' => 1, 'return' => ['name', 'minimum_fee', 'financial_type_id'], - 'id' => $upgradedMembershipTypeId, + 'id' => $nextPeriodMembershipTypeId, ]); if (!empty($membershipType['values'][0])) { @@ -392,6 +409,16 @@ private function getNextPeriodLineItems() { return $nextLineItems; } + private function getPaymentPlanLinkedSchemeIfExists() { + try { + $paymentPlanScheduleGenerator = new CRM_MembershipExtras_Service_PaymentScheme_PaymentPlanScheduleGenerator($this->contribRecur['id']); + return $paymentPlanScheduleGenerator->generateSchedule(); + } + catch (CRM_Extension_Exception $e) { + return NULL; + } + } + /** * @return string */