let schedule = []; let currentDate = new Date(disbursementDate);
// Calculate schedule for each installment
for (let i = 1; i <= totalInstallments; i++) { let days = masterDays[installmentFrequency]; // Calculate interest and principal components let interest = (loanAmount * workingInterest * days) / (365 * 100); let principal = installmentAmount - interest;
// Update outstanding principal
let outstandingPrincipal = loanAmount - principal;
let paymentDate = new Date(currentDate.getTime()); paymentDate.setDate(paymentDate.getDate() + days);
// Check if there is an actual repayment date for the current installment
let actualRepaymentDate = actualRepaymentDates[i - 1]; let actualRepaidAmount = actualRepaidAmounts[i - 1];
// Adjust remaining principal and interest based on the actual repaid
amount if (actualRepaidAmount !== null) { if (actualRepaidAmount > installmentAmount) { // If actual repaid amount is greater than installment, reduce next scheduled interest amount let remainingAmount = actualRepaidAmount - installmentAmount; interest -= remainingAmount; principal += remainingAmount; } else if (actualRepaidAmount < installmentAmount) { // If actual repaid amount is less than installment, increase next scheduled interest amount let remainingAmount = installmentAmount - actualRepaidAmount; interest += remainingAmount; principal -= remainingAmount; } }
// Adjust principal if repaid amount is greater than scheduled
if (actualRepaidAmount !== null && actualRepaidAmount > installmentAmount) { let excessAmount = actualRepaidAmount - installmentAmount; principal -= excessAmount; outstandingPrincipal -= excessAmount; }
// Adjust payment date if deviated from scheduled date