finance.js
function addLateFees(loan, cb) {

    var d = Q.defer();
    if (!loan || !loan.closingDate || !loan.loanAmount || !loan.interestRate || !loan.paymentAmount) {
      d.reject(new Error('required parameters for addLateFees not provided'));
    } else {
      var lateFee = calcLateFee(loan);
      var determinationDate = moment();
      var txDate, pmtDate, graceDate, pmtTransactions, elapsedMonths, i;

      var lateFeeTxs = loan.transactions.filter(function (tx) {
        return tx.type === 'Late Fee';
      });
      //remove all existing late fees
      lateFeeTxs.forEach(function (tx) {
        loan.transactions.id(tx._id).remove();
      });

      pmtTransactions = loan.transactions.filter(function (tx) {
        return tx.type !== 'Late Fee';
      });
      //calculate the number of months that have lapsed starting with the first payment date
      //until the determination date
      elapsedMonths = determinationDate.diff(moment(loan.firstPaymentDate), 'months') + 1;

      for (i = 0; i < elapsedMonths; i++) {
        pmtDate = moment(loan.firstPaymentDate).add('months', i);
        graceDate = moment(loan.firstPaymentDate).add('months', i).add('days', loan.daysUntilLate);
        if (!paymentMadeOnTime(pmtTransactions, pmtDate, graceDate, loan.paymentAmount)
            && pmtDate.isBefore(determinationDate) && !lateFeeAppliedForDate(loan, graceDate)) {
          txDate = graceDate.toISOString();
          console.log('loan balance in late fees: ', getLoanBalance(loan, txDate));
          loan.transactions.push({
            txDate: txDate,
            type: "Late Fee",
            amount: -1 * lateFee,
            comments: "Late fee imposed for failure to pay on time or to pay proper amount",
            loanBalance: getLoanBalance(loan, txDate)
          });
        }
      }
    }

    console.log('addLateFees');
    d.resolve(loan);
    if (cb) return cb(loan);
    return d.promise;
  }
function addLateFees(loan, cb) {

    var d = Q.defer();
    if (!loan || !loan.closingDate || !loan.loanAmount || !loan.interestRate || !loan.paymentAmount) {
      d.reject(new Error('required parameters for addLateFees not provided'));
    } else {
      var lateFee = calcLateFee(loan);
      var determinationDate = moment();
      var txDate, pmtDate, graceDate, pmtTransactions, elapsedMonths, i;

      var lateFeeTxs = loan.transactions.filter(function (tx) {
        return tx.type === 'Late Fee';
      });
      //remove all existing late fees
      lateFeeTxs.forEach(function (tx) {
        loan.transactions.id(tx._id).remove();
      });

      pmtTransactions = loan.transactions.filter(function (tx) {
        return tx.type !== 'Late Fee';
      });
      //calculate the number of months that have lapsed starting with the first payment date
      //until the determination date
      elapsedMonths = determinationDate.diff(moment(loan.firstPaymentDate), 'months') + 1;

      for (i = 0; i < elapsedMonths; i++) {
        pmtDate = moment(loan.firstPaymentDate).add('months', i);
        graceDate = moment(loan.firstPaymentDate).add('months', i).add('days', loan.daysUntilLate);
        if (!paymentMadeOnTime(pmtTransactions, pmtDate, graceDate, loan.paymentAmount)
            && pmtDate.isBefore(determinationDate) && !lateFeeAppliedForDate(loan, graceDate)) {
          txDate = graceDate.toISOString();
          console.log('loan balance in late fees: ', getLoanBalance(loan, txDate));
          loan.transactions.push({
            txDate: txDate,
            type: "Late Fee",
            amount: -1 * lateFee,
            comments: "Late fee imposed for failure to pay on time or to pay proper amount",
            loanBalance: getLoanBalance(loan, txDate)
          });
        }
      }
    }

    console.log('addLateFees');
    d.resolve(loan);
    if (cb) return cb(loan);
    return d.promise;
  }
function addLateFees ( loan , cb ) { var d = Q . defer ( ) ; if ( ! loan || ! loan . closingDate || ! loan . loanAmount || ! loan . interestRate || ! loan . paymentAmount ) { d . reject ( new Error ( 'required parameters for addLateFees not provided' ) ) ; } else { var lateFee = calcLateFee ( loan ) ; var determinationDate = moment ( ) ; var txDate , pmtDate , graceDate , pmtTransactions , elapsedMonths , i ; var lateFeeTxs = loan . transactions . filter ( function ( tx ) { return tx . type === 'Late Fee' ; } ) ; //remove all existing late fees lateFeeTxs . forEach ( function ( tx ) { loan . transactions . id ( tx . _id ) . remove ( ) ; } ) ; pmtTransactions = loan . transactions . filter ( function ( tx ) { return tx . type !== 'Late Fee' ; } ) ; //calculate the number of months that have lapsed starting with the first payment date //until the determination date elapsedMonths = determinationDate . diff ( moment ( loan . firstPaymentDate ) , 'months' ) + 1 ; for ( i = 0 ; i < elapsedMonths ; i ++ ) { pmtDate = moment ( loan . firstPaymentDate ) . add ( 'months' , i ) ; graceDate = moment ( loan . firstPaymentDate ) . add ( 'months' , i ) . add ( 'days' , loan . daysUntilLate ) ; if ( ! paymentMadeOnTime ( pmtTransactions , pmtDate , graceDate , loan . paymentAmount ) && pmtDate . isBefore ( determinationDate ) && ! lateFeeAppliedForDate ( loan , graceDate ) ) { txDate = graceDate . toISOString ( ) ; console . log ( 'loan balance in late fees: ' , getLoanBalance ( loan , txDate ) ) ; loan . transactions . push ( { txDate : txDate , type : "Late Fee" , amount : - 1 * lateFee , comments : "Late fee imposed for failure to pay on time or to pay proper amount" , loanBalance : getLoanBalance ( loan , txDate ) } ) ; } } } console . log ( 'addLateFees' ) ; d . resolve ( loan ) ; if ( cb ) return cb ( loan ) ; return d . promise ; }
Add Late Fees
@param loan
@param cb
@returns {*}
Add Late Fees