Skip to content

Commit

Permalink
CIWEMB-469: Fix next period tab line item amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabuhussein committed Sep 1, 2023
1 parent a80d180 commit d9b3ce1
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions CRM/MembershipExtras/Page/EditContributionRecurLineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
// If the payment plan is linked to a payment scheme, then next period
// line items amounts should be divided on the number of instalments
// configured on the linked payment scheme.
$paymentPlanLinkedScheme = $this->getPaymentPlanLinkedSchemeIfExists();
if (!empty($paymentPlanLinkedScheme)) {
$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])) {
Expand All @@ -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
*/
Expand Down

0 comments on commit d9b3ce1

Please sign in to comment.