Hello Team,
Please am using angular controllerJS in my MVC application to Sum up item Quantity,SubTotal,DiscountPercent,VatPercent,TotalAmount but the columns value sum up higher value than expected, kindly help
function sumOfSalesDetails($scope) {
$.each($scope.PrintSalesRecordList.modifiedData, function (index, val) {
$scope.totalQty += val.Quantity;
$scope.subTotal += val.SubTotal;
$scope.DiscountTotal += val.DiscountPercent;
$scope.VatTotal += val.VatPercent;
$scope.Total += val.TotalAmount;
})
}
}]);
}).call(angular);
Loading

Vishal JoshiRank #377 · 6.7K reputationPosted Oct 21, 2023, 1:42 AM EDT
Hello Emmanual,
Please check the below code. you need to set the default zero before adding new values to variables/properties.
function sumOfSalesDetails($scope) { $scope.totalQty = 0; $scope.subTotal = 0; $scope.DiscountTotal = 0; $scope.VatTotal = 0; $scope.Total = 0; $.each($scope.PrintSalesRecordList.modifiedData, function (index, val) { $scope.totalQty += val.Quantity; $scope.subTotal += val.SubTotal; $scope.DiscountTotal += val.DiscountPercent; $scope.VatTotal += val.VatPercent; $scope.Total += val.TotalAmount; }) }Thanks
Vishal Joshi
Tahir AnsariRank #366 · 6.8K reputationPosted Oct 20, 2023, 10:25 AM EDT
I added explicit initializations of each variable to zero before summing up the values. This should ensure that your summation starts from a clean slate, preventing the issue where the values are higher than expected.
function sumOfSalesDetails($scope) { // Initialize variables to zero $scope.totalQty = 0; $scope.subTotal = 0; $scope.DiscountTotal = 0; $scope.VatTotal = 0; $scope.Total = 0; $.each($scope.PrintSalesRecordList.modifiedData, function (index, val) { $scope.totalQty += val.Quantity; $scope.subTotal += val.SubTotal; $scope.DiscountTotal += val.DiscountPercent; $scope.VatTotal += val.VatPercent; $scope.Total += val.TotalAmount; }); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Oct 21, 2023, 5:11 AM EDT
Thank you Vishal and Mohammed for your help