'use strict'; var enVisageServices = angular.module('enVisageServices', []); enVisageServices .factory('hoursResourcesConverter', ['$http', function ($http) { var ec2uom = {}; $http({ method: 'POST', url: '/ExpenditureCategory/GetCategoriesWithUoMValues' }).success(function (data, status, headers, config) { ec2uom = data; }); var retrieveHoursForEC = function (expCatId) { if (!ec2uom || ec2uom[expCatId] == undefined || ec2uom[expCatId] == null) { throw 'Error with data for h/r converter'; } var hours = parseFloat(ec2uom[expCatId].UoMValue); if (hours <= 0) { throw 'Incorrect data for expenditure [' + expCatId + ']: ' + ec2uom[expCatId]; } return hours; } var converter = { convertToHours: function (expCatId, value) { if (!expCatId || !value || value < 0) return 0; var hours = retrieveHoursForEC(expCatId); if (hours <= 0) return 0; return hours * value; }, convertToResources: function (expCatId, value) { if (!expCatId || !value || value < 0) return 0; var hours = retrieveHoursForEC(expCatId); if (hours <= 0) return 0; return value / hours; }, getExpenditures: function () { return ec2uom; } }; return converter; }]) .factory('dataSources', ['$http', function ($http) { var expCats = {}; $http({ method: 'POST', url: '/ExpenditureCategory/GetAllCategories' }).success(function (data, status, headers, config) { expCats = data; }); var serviceContainer = { getExpenditures: function () { return expCats; }, getExpenditureById: function (id) { var scenarioExpCatKeys = Object.keys(expCats); if ($.inArray(id, scenarioExpCatKeys) < 0) { return null; } return expCats[id]; } }; return serviceContainer; }]) .factory('cellHighligting', [function () { var service = { // compares 2 decimals rounded to 3 decimals // if difference is less than 0.005 then consider values are equal compare: function (val1, val2) { if (!isNaN(parseFloat(val1)) && !isNaN(parseFloat(val2))) { var val1_ = Math.round(parseFloat(val1) * 1000) / 1000; var val2_ = Math.round(parseFloat(val2) * 1000) / 1000; if ((-0.005 > (val1_ - val2_))) return -1; else if ((0.005 < (val1_ - val2_))) return 1; else if (val1_ == 0 && val2_ == 0) return null; else return 0; } }, setCellCssClasses: function (expCatRow, resRow, colIndex) { if (resRow == null) { // category row if (expCatRow == null || colIndex == null || expCatRow.Cells == null || expCatRow.Cells.length <= colIndex) return; if (expCatRow.CSSClass == null) expCatRow.CSSClass = new Array(expCatRow.Cells.length); expCatRow.CSSClass[colIndex] = (expCatRow.CSSClass[colIndex] || '').replace(/ cellover/g, '').replace(/ cellless/g, '').replace(/ cellequal/g, ''); var compareResult = this.compare(expCatRow.Cells[colIndex].Value1, expCatRow.Cells[colIndex].Value2); if (compareResult > 0) expCatRow.CSSClass[colIndex] += ' cellover'; else if (compareResult < 0) expCatRow.CSSClass[colIndex] += ' cellless'; else if (compareResult == 0) expCatRow.CSSClass[colIndex] += ' cellequal'; // assign more classes if needed expCatRow.CSSClass[colIndex] = expCatRow.CSSClass[colIndex]; } else { // updating resource row if (expCatRow == null || colIndex == null || resRow.Cells == null || resRow.Cells.length <= colIndex) return; if (resRow.CSSClass == null) resRow.CSSClass = new Array(resRow.Cells.length); resRow.CSSClass[colIndex] = (resRow.CSSClass[colIndex] || '').replace(/ cellover/g, '').replace(/ cellless/g, '').replace(/ cellequal/g, ''); var compareResult = this.compare(resRow.Cells[colIndex].Value1, resRow.Cells[colIndex].Value2); if (compareResult > 0) resRow.CSSClass[colIndex] += ' cellover'; else if (compareResult < 0) resRow.CSSClass[colIndex] += ' cellless'; else if (compareResult == 0) resRow.CSSClass[colIndex] += ' cellequal'; // assign more classes if needed resRow.CSSClass[colIndex] = resRow.CSSClass[colIndex]; } } }; return service; }]) .factory('teamInfoService', [function () { var teams = {}; return { init: function (teamsArray) { if (!teamsArray) { teams = {}; return; } if (!angular.isArray(teamsArray)) { teams = angular.copy(teamsArray); return; } teams = {}; // SA. ENV-1254 for (var i = 0; i < teamsArray.length; i++) { teams[teamsArray[i].Id] = angular.copy(teamsArray[i]); } }, getById: function (teamId) { if (!teams) return null; return teams[teamId] || null; }, getAll: function () { return teams; }, isExists: function (teamId) { if (!teamId || !teams) return false; return !!teams[teamId]; }, getExpenditureCategoryInTeam: function (teamId, expCatId) { if (!teamId || !expCatId) return null; var team = this.getById(teamId); if (!team || !team.ExpCategories) return null; return team.ExpCategories[expCatId] || null; }, changeExpenditureCategoryValue: function (teamId, expCatId, weekEndingMs, deltaValue) { if (!teamId || !expCatId || !weekEndingMs || !deltaValue) return; var category = this.getExpenditureCategoryInTeam(teamId, expCatId); if (!category) return; if (!category.NeedCapacity[weekEndingMs]) category.NeedCapacity[weekEndingMs] = 0; var newNeedCapacity = category.NeedCapacity[weekEndingMs] + deltaValue; category.NeedCapacity[weekEndingMs] = Math.max(newNeedCapacity, 0); }, changeResourceValue: function (teamId, expCatId, resourceId, weekEndingMs, deltaValue) { if (!teamId || !expCatId || !resourceId || !weekEndingMs || !deltaValue) return; var category = this.getExpenditureCategoryInTeam(teamId, expCatId); if (!category || !category.Resources) return; var resource = category.Resources[resourceId]; if (!resource || !resource.AllocatedCapacity) return; if (!resource.AllocatedCapacity[weekEndingMs]) resource.AllocatedCapacity[weekEndingMs] = 0; if (!category.AllocatedCapacity[weekEndingMs]) category.AllocatedCapacity[weekEndingMs] = 0; var newResourceAllocatedCapacity = resource.AllocatedCapacity[weekEndingMs] + deltaValue; var newCategoryAllocatedCapacity = category.AllocatedCapacity[weekEndingMs] + deltaValue; resource.AllocatedCapacity[weekEndingMs] = newResourceAllocatedCapacity; category.AllocatedCapacity[weekEndingMs] = newCategoryAllocatedCapacity; } }; }]);