UNPKG

cbe-npv-interest-waiver

Version:
266 lines (222 loc) 8.08 kB
/* * CBEAmortization * Calculating CBE's Amortization * https://github.com/gama1221/cbe-npv-interest-waiver * * Copyright (c) 2023 Getinet Amare Mekonnen * Licensed under the CBE license. */ /** * * @param {number} num - An input to be rounded * @returns - It returns two decimal point rounded value */ const roundTheValue = (num) => { return Math.round(num * 100) / 100; }; /** * * @param {number} amount - Original Loan * @param {number} annualInterestRate - Annual interest rate to be paid for loan * @param {number} loanPeriodInYears - Length of a year that a loan takes to complete * @param {number} numberOfPaymentsPerYear - It holds how many number of years it tooks to complete the loan * @param {number} principalSum - Summation of each principal for number of payments * @param {number} cumulativeInterest - Summation of interests. * * @returns {{ principal: number, interest: number, scheduledPayment: number, endingBalance: number, cumulativeInterest: number }} */ var counter = 0; var getNextPaymentSchedule = ( month, amount, annualInterestRate, loanPeriodInYears, numberOfPaymentsPerYear, waivedNumberOfPayment, principalSum, cumulativeInterest ) => { var principal; var interest; var scheduledPayment; var irmPow; var interestRateMonth = annualInterestRate / 100 / (numberOfPaymentsPerYear); var numberOfPayment; var endingBalance; // var counter = 0; numberOfPayment = loanPeriodInYears * numberOfPaymentsPerYear; /** * * We need to change it for Interest waiver */ if(counter < waivedNumberOfPayment ){ // scheduledPayment = interest = ((annualInterestRate / 100)/numberOfPaymentsPerYear)*amount; // principal = 0; // endingBalance = parseFloat(amount);waivedNumberOfPayment irmPow = Math.pow(1 + interestRateMonth, - numberOfPayment); scheduledPayment = roundTheValue( (parseFloat(amount) * (annualInterestRate / 100)) / ((numberOfPaymentsPerYear) * (1 - irmPow)) ); interest = roundTheValue((parseFloat(amount) - principalSum) * interestRateMonth); principal = scheduledPayment - interest; endingBalance = roundTheValue(parseFloat(amount) - principalSum - principal) ; scheduledPayment = principal; cumulativeInterest = cumulativeInterest ; } else{ irmPow = Math.pow(1 + interestRateMonth, -numberOfPayment); scheduledPayment = roundTheValue( (parseFloat(amount) * (annualInterestRate / 100)) / ((numberOfPaymentsPerYear) * (1 - irmPow)) ); interest = roundTheValue((parseFloat(amount) - principalSum) * interestRateMonth); principal = roundTheValue(scheduledPayment - interest); endingBalance = parseFloat(amount) - principalSum - principal; cumulativeInterest = cumulativeInterest + interest; } counter++; return { month:month, principal:roundTheValue(principal), interest: roundTheValue(interest), scheduledPayment: scheduledPayment, endingBalance:roundTheValue(endingBalance), cumulativeInterest: cumulativeInterest, }; }; /** * It holds the parametrs we provide on the GUI, It has four parametrs * @param {number} amount - Original Loan * @param {number} annualInterestRate - Annual interest rate to be paid for loan * @param {number} loanPeriodInYears - Length of a year that a loan takes to complete * @param {number} numberOfPaymentsPerYear - It holds how many number of years it tooks to complete the loan* * @returns {{ scheduledPayments: number, amount: number, cumulativeInterest: number, principalSum: number, sum: number }} */ const CBEAmortization = ( amount, annualInterestRate, loanPeriodInYears, riskPremium, numberOfPaymentsPerYear, waivedNumberOfPayment ) => { if ( !amount || amount <= 0 || !loanPeriodInYears || loanPeriodInYears <= 0 || !annualInterestRate || annualInterestRate <= 0 || !riskPremium || riskPremium <= 0 ) { throw new Error( "wrong parameters: " .concat(amount, " ") .concat(loanPeriodInYears, " ") .concat(riskPremium, " ") .concat(annualInterestRate) ); } var scheduledPayments = []; var cumulativeInterest = 0; var principalSum = 0; var sum = 0; var p_s = 0; var s_s = 0; var i_s = 0; var m = 0; var yearlyScheduledPaymentInterestWaiver = []; var yearlyPrincipalSummationInterestWaiver = []; var yearlyInterestSummationInterestWaiver = []; var NPV = []; var month = 1; for (var i = 0; i < loanPeriodInYears * numberOfPaymentsPerYear; i++) { var inst = getNextPaymentSchedule( month, amount, annualInterestRate, loanPeriodInYears, numberOfPaymentsPerYear, waivedNumberOfPayment, principalSum, cumulativeInterest, ); month = month; sum += inst.scheduledPayment; principalSum += inst.principal; var scheduleTime = loanPeriodInYears * numberOfPaymentsPerYear; var interestRateMonth = annualInterestRate / 100 / (numberOfPaymentsPerYear); // if(interestprincipalWaiverExtension = interestprincipalWaiverExtension){ waivedInterestSum = roundTheValue((parseFloat(amount) - principalSum) * interestRateMonth); waivedInterestSum = waivedInterestSum * waivedNumberOfPayment; additionalInterest = waivedInterestSum / (scheduleTime - waivedNumberOfPayment); // } if (counter <= waivedNumberOfPayment) { inst.interest = 0; cumulativeInterest += inst.interest; } else{ cumulativeInterest += inst.interest; inst.interest = roundTheValue(inst.interest + additionalInterest); } s_s += inst.scheduledPayment; p_s += inst.principal; i_s += inst.interest; if (i === loanPeriodInYears * numberOfPaymentsPerYear - 1) { principalSum += inst.endingBalance; sum += inst.endingBalance; inst.endingBalance = 0; } if(m === (numberOfPaymentsPerYear - 1)) { yearlyScheduledPaymentInterestWaiver.push(s_s); yearlyPrincipalSummationInterestWaiver.push(p_s); yearlyInterestSummationInterestWaiver.push(i_s); s_s = 0; i_s = 0; p_s = 0; m = -1; } month++; m++; scheduledPayments.push(inst); var riskPremiumWithAnnualInterestRate = (1+(parseFloat(annualInterestRate) + parseFloat(riskPremium))/100); if(i === (loanPeriodInYears * numberOfPaymentsPerYear - 1)){ /** * Let me sum the following values * yearlyPrincipalSummation * yearlyInterestSummations * And compute the NPV */ var power = 1; for(var n = 0;n < yearlyInterestSummationInterestWaiver.length; n++){ var npv1 = ((yearlyInterestSummationInterestWaiver[n] + yearlyPrincipalSummationInterestWaiver[n]) / Math.pow(riskPremiumWithAnnualInterestRate, power)); NPV.push(npv1); npv1 = 0; power++; } /** * Summation of NPV */ var npmSummation = 0; for(var s = 0; s < NPV.length; s++){ npmSummation += NPV[s]; } } } return { scheduledPayments: scheduledPayments, amount: roundTheValue(amount), cumulativeInterest: roundTheValue(cumulativeInterest), principalSum: roundTheValue(principalSum), sum: roundTheValue(sum), scheduledPayment:inst.scheduledPayment, actualNumberOfPayments: (loanPeriodInYears * numberOfPaymentsPerYear), yearlyPrincipalSummationInterestWaiver:yearlyPrincipalSummationInterestWaiver, yearlyInterestSummationInterestWaiver:yearlyInterestSummationInterestWaiver, yearlyScheduledPaymentInterestWaiver:yearlyScheduledPaymentInterestWaiver, NPV:roundTheValue(npmSummation), }; }; module.exports = CBEAmortization;