Skip to content

Commit

Permalink
MAE-1178: Fix pro rata calculation for membership
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Shahrukh committed Oct 18, 2024
1 parent 1275459 commit c245372
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function generate($paymentMethod, DateTime $startDate = NULL, DateTime $e

$instalments['sub_total'] = $this->instalmentCalculator->getInstalmentsSubTotalAmount($instalments['instalments']);
$instalments['tax_amount'] = $this->instalmentCalculator->getInstalmentsTaxAmount($instalments['instalments']);
$instalments['total_amount'] = $this->instalmentCalculator->getInstalmentsTotalAmount($instalments['instalments']);
$instalments['total_amount'] = number_format($this->instalmentCalculator->getInstalmentsTotalAmount($instalments['instalments']), 2);

$instalments['membership_start_date'] = $this->startDate->format('Y-m-d');
$instalments['membership_end_date'] = $this->endDate->format('Y-m-d');
Expand Down
53 changes: 51 additions & 2 deletions js/paymentPlanToggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
*/
function initializeMembershipForm() {
selectPaymentPlanTab(togglerValue);
addContributionAmountHandler();
}

/**
Expand Down Expand Up @@ -91,7 +92,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
CRM.api3('PaymentSchedule', 'getscheduleoptionsbypricefieldvalues', params).then(function (result) {
if (result.is_error === 0) {
setPaymentPlanScheduleOption(result.values);
generateInstalmentSchedule(isPriceSet);
generateInstalmentSchedule(isPriceSet, $('#start_date').val());
} else {
CRM.alert(result.error_message, 'Error', 'error');
}
Expand All @@ -102,7 +103,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
}).then(function (result) {
if (result.is_error === 0) {
setPaymentPlanScheduleOption(result.values);
generateInstalmentSchedule(isPriceSet);
generateInstalmentSchedule(isPriceSet, $('#start_date').val());
} else {
CRM.alert(result.error_message, 'Error', 'error');
}
Expand Down Expand Up @@ -302,6 +303,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
function setMembershipFormEvents() {
setupPayPlanTogglingEvents();
setScheduleEvents();
contributionAmountHandler();
}

/**
Expand Down Expand Up @@ -474,6 +476,53 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
$(".ui-dialog-buttonset button, .crm-submit-buttons button").prop('disabled',true);
});
}

function contributionAmountHandler() {
let membershipTypeId = $('#membership_type_id_1').val();
let isPriceSet = isPriceSetSelected();
if (membershipTypeId > 0 || isPriceSet) {
if ($('#start_date').val() === '' && $('#join_date').length && $('#join_date').val() !== '') {
$('#start_date').val($('#join_date').val());
$('#start_date').next('.hasDatepicker').datepicker('setDate', new Date($('#join_date').val()));
}

CRM.api3('MembershipType', 'getSingle', { id: membershipTypeId }).then(function (result) {
if (result?.period_type === 'fixed' || isPriceSet) {
let params = {
start_date: $('#start_date').val(),
payment_method: $('#payment_instrument_id').val(),
schedule: 'monthly',
};
let action = 'getbymembershiptype';

if (isPriceSet) {
let selectedPriceFieldValues = getSelectedPriceFieldValues();
params.price_field_values = { 'IN': selectedPriceFieldValues };
action = 'getbypricefieldvalues';
} else {
params.membership_type_id = membershipTypeId;
}

CRM.api3('PaymentSchedule', action, params).then(function (result) {
if (result.is_error === 0) {
updateTotalAmount(result.values.total_amount, isPriceSet);
$('#end_date').val(result.values.membership_end_date);
$('#end_date').next('.hasDatepicker').datepicker('setDate', new Date(result.values.membership_end_date));
} else {
CRM.alert(result.error_message, 'Error', 'error');
}
});
}
});
}
}

function addContributionAmountHandler() {
if (($('#membership_type_id_1').length || $('#price_set_id').length) && $('#start_date').length) {
$(document).off( 'change', '#membership_type_id_1, #start_date, #priceset :input');
$(document).on( 'change', '#membership_type_id_1, #start_date, #priceset :input', contributionAmountHandler);
}
}
});

}

0 comments on commit c245372

Please sign in to comment.