Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save eppspacjar/de550162cc16322149d7d41dc8d4bae9 to your computer and use it in GitHub Desktop.

Select an option

Save eppspacjar/de550162cc16322149d7d41dc8d4bae9 to your computer and use it in GitHub Desktop.
/* -- Repayment Schedule Scripting rule -- */
// --------------------
// STEP 1: Calculate the monthly payment value.
// 1.1: Define and assign the monthly payment variables using the input object.
let monthlyPaymentInputData = {
loanAmount: input.loanAmount,
loanDuration: input.loanDuration,
interestRate: input.interestRate,
}
// 1.2: Calculate the monthly payment using the "Loan Calculator" rule.
let monthlyPayment = DR.solve("14fb4d59-bd75-c431-f772-88afca322cc0", monthlyPaymentInputData);
// --------------------
// --------------------
// STEP 2: Calculate the repayment schedule values.
// 2.1: Define and assign the schedule variables using the input object and the calculated monthly payment values.
let calendarInputData = {
monthlyPayment: monthlyPayment[0].monthlyPayment,
remainingPrincipal: input.loanAmount,
interestRate: input.interestRate
};
// 2.2: Define the calendar array to store all the rows of the calendar.
let calendarArray = [];
// 2.3: Generate all the rows of the calendar using the "Repayment Schedule" rule.
while(calendarInputData.remainingPrincipal > 0) {
// 2.3.1: Get and store the result of the "Repayment Schedule" rule.
let calendarOutputRaw = DR.solve("990e7b11-887b-c688-229f-9a954d18ad99", calendarInputData, "latest", SolverStrategy.STANDARD);
let calendarOutput = calendarOutputRaw[0];
// 2.3.2: Check if the ending balance is bigger than 0.
calendarOutput.endingBalance = (Math.round( calendarOutput.endingBalance * 1e2 ) / 1e2) > 0 ? calendarOutput.endingBalance : 0;
// 2.3.3: Set the validated ending balance as the remaining principal.
calendarInputData.remainingPrincipal = calendarOutput.endingBalance;
// 2.3.4: Round the calculated values to 2 decimal places
calendarOutput.beginningBalance = Number(calendarOutput.beginningBalance).toFixed(2);
calendarOutput.principal = Number(calendarOutput.principal).toFixed(2);
calendarOutput.interest = Number(calendarOutput.interest).toFixed(2);
calendarOutput.endingBalance = Number(calendarOutput.endingBalance).toFixed(2);
// 2.3.5: Add (push) the current row of the calendar to the calendar array.
calendarArray.push(calendarOutput);
}
// --------------------
// --------------------
// STEP 3: Return the generated calendar array as an output.
// 3.1: Set the generated calendar array as the output.
output.calendar = calendarArray;
// 3.2: Return the output.
return output;
// --------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment