diff --git a/CRM/MembershipExtras/Service/MembershipInstalmentsSchedule.php b/CRM/MembershipExtras/Service/MembershipInstalmentsSchedule.php index 73c156d8..fc82744f 100644 --- a/CRM/MembershipExtras/Service/MembershipInstalmentsSchedule.php +++ b/CRM/MembershipExtras/Service/MembershipInstalmentsSchedule.php @@ -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'); diff --git a/js/paymentPlanToggler.js b/js/paymentPlanToggler.js index f30daa90..904eb844 100644 --- a/js/paymentPlanToggler.js +++ b/js/paymentPlanToggler.js @@ -25,6 +25,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) { */ function initializeMembershipForm() { selectPaymentPlanTab(togglerValue); + addContributionAmountHandler(); } /** @@ -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'); } @@ -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'); } @@ -302,6 +303,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) { function setMembershipFormEvents() { setupPayPlanTogglingEvents(); setScheduleEvents(); + contributionAmountHandler(); } /** @@ -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); + } + } }); }