Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAE-1178: Fix Pro Rata Calculation For Membership #539

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
});

}
Loading