'use strict';

angular.module('ThePFMind.loanCalculator', [])

    .service('loanCalculator', function () {
        return {
            f1: function (i, m) { // i = interest per month, m = months
                return i * Math.pow(1 + i, m);
            },
            f2: function (interestRates, n) {
                if (n < 0) return 1;
                var r = interestRates[n];
                var o = 1;
                if (r.rate > 0)
                    o = this.f1(r.rate / 12 / 100, r.months);
                return this.f2(interestRates, n - 1) * o;
            },
            g2: function (interestRates, n) {
                if (n < 0) return 0;

                var o = 1;
                interestRates.map(function (r, i) {
                    var mr = r.rate / 12 / 100;
                    if (mr > 0) {
                        if (i < n) {
                            o = o * mr;
                        } else if (i == n) {
                            o = o * (Math.pow(1 + mr, r.months) - 1);
                        } else {
                            o = o * mr * (Math.pow(1 + mr, r.months));
                        }
                    } else {
                        if (i == n) {
                            o = o * r.months;
                        }
                    }
                });
                return this.g2(interestRates, n - 1) + o;
            },
            getInstalment: function (principal, interestRates) {
                var f2 = this.f2(interestRates, interestRates.length - 1);
                var g2 = this.g2(interestRates, interestRates.length - 1);
                return principal * f2 / g2;
            },
            perpareInterestRates: function (interestRates, term) {
                var interestRatesCopy = interestRates.slice();
                var totalMonths = 12 * term;
                interestRatesCopy.map(function (r, i, a) {
                    if (totalMonths == 0) {
                        a.splice(i, 1);
                    } else {
                        totalMonths = totalMonths - parseFloat(r.months);
                        if (totalMonths < 0) {
                            a[i].months = parseFloat(totalMonths) + parseFloat(a[i].months);
                            totalMonths = 0;
                        }
                        if (i == a.length - 1) {
                            a[i].months = parseFloat(totalMonths) + parseFloat(a[i].months);
                        }
                    }
                });
                return interestRatesCopy;
            },
            calculate: function (principal, interestRates, term, callback) {

                var interestRatesCopy = this.perpareInterestRates(interestRates, term);

                var instalment = this.getInstalment(principal, interestRatesCopy, term);

                var data = {
                    principal: principal,
                    interestRates: interestRatesCopy,
                    term: term,                     // Years
                    months: 12 * term,
                    instalment: instalment
                };
                callback(data);
            }

        };
    });


@@ -1753,35 +1753,60 @@
     var 
-interestRatesCopy =
+totalMonths = 12 * term;%0A                var
  interes
@@ -1815,16 +1815,17 @@
 ates
-.slice()
+Copy = %5B%5D
 ;%0A  
@@ -1842,35 +1842,54 @@
     
-var totalMonths = 12 * term
+angular.copy(interestRates, interestRatesCopy)
 ;%0A  
@@ -2018,21 +2018,22 @@
    a
-.splice(i, 1)
+%5Bi%5D.months = 0
 ;%0A  
@@ -2389,16 +2389,35 @@
 ngth - 1
+ && totalMonths %3E 0
 ) %7B%0A    
@@ -2545,32 +2545,32 @@
               %7D%0A
-
                 
@@ -2565,32 +2565,209 @@
             %7D);%0A
+%0A                // remove months = 0;%0A                interestRatesCopy = interestRatesCopy.filter(function (e) %7B%0A                    return e.months %3E 0;%0A                %7D);%0A%0A
                 

Fix calc logic