1

I created a small angular js service for my application. In the below code "Final Calculated " prints fine but "Final Balance " doesn't print and doesn't return the value in the return statement.

Any help/suggestions are highly appreciated.

app.service('MyService', function($http) {

console.log("Inside Service ");
var finalAccountBalance = [];
this.myFunc = function (x) {
    return x.toString(16);
}

this.calculateAccountBook = function (x,y) {
    console.log("Inside Service :: Inside calculateAccountBook");
    var calculated = [];

    var payments =[];
    //console.log(JSON.stringify(x));
    $http.get('/totalpayments/search/findByHoaId_hoaId?hoaId='+y).
    then(function (response) {

        if (response.data._embedded != undefined) {

            payments = response.data._embedded.currentPaymentses;
        } else {
            payments = [];
        }

        angular.forEach(payments, function (value, key) {
            var obj={};

            obj = {
            "processDate" : value.processDate,
            "description" : value.paymentTypeId.paymentTypeName+" - "+value.documentNum,
            "currentCharge" : "",
            "currentPayment" : value.amount,
            "accountBalance" : value.transactionBalance
            };

            calculated.push(obj);
        });

        angular.forEach(x, function (value, key) {
            var obj={};

            obj = {
            "processDate" : value.assessmentDate,
            "description" : value.assessmentRuleType.name,
            "currentCharge" : value.amount,
            "currentPayment" : "",
            "accountBalance" : value.transactionBalance
            };

            calculated.push(obj);
        }); 
        calculated = sortByKey(calculated,'processDate');
        finalAccountBalance = calculateAccountBalance(calculated);
        console.log("Final Calculated "+JSON.stringify(finalAccountBalance));   
    });
    console.log("Final Balance "+JSON.stringify(finalAccountBalance));
    return finalAccountBalance;
};

function sortByKey(array, key) {
    return array.sort(function(a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
};

function calculateAccountBalance (cc) {
    var accountBalanceArr = [];

    angular.forEach(cc, function (value, key) {
        var obj={};
        var lastObj = accountBalanceArr.slice(-1)[0];
        var lastCharge;
        var lastPay;
        var lastBal;
        var currentBal;

        if(lastObj != undefined){
            console.log("Inside Last Object");
            lastCharge = lastObj.currentCharge;
            lastPay = lastObj.currentPayment;
            lastBal = lastObj.accountBalance ? lastObj.accountBalance : 0;
            console.log("Last Charge : "+lastCharge+" Last Pay : "+lastPay+" Last Balance : "+lastBal);
        }

        currentBal = lastBal + value.currentCharge - value.currentPayment;
        console.log("Current Balance "+currentBal);
        obj = {
        "processDate" : value.processDate,
        "description" : value.description,
        "currentCharge" : value.currentCharge,
        "currentPayment" : value.currentPayment,
        "accountBalance" : currentBal
        };

        accountBalanceArr.push(obj);
    }); 

    return accountBalanceArr;
  };


});
mplungjan
  • 169,008
  • 28
  • 173
  • 236

0 Answers0