275 lines
11 KiB
JavaScript
275 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
app.controller('costSavingController', ['$scope', '$http', '$location', '$timeout', function ($scope, $http, $location, $timeout) {
|
|
$scope.CollapsedIcon = 'fa-plus-square';
|
|
$scope.NonCollapsedIcon = 'fa-minus-square';
|
|
$scope.Dec = Math.pow(10, 3);//precision decimals
|
|
$scope.data = null;
|
|
$scope.items = null;
|
|
$scope.IsEditable = null;
|
|
$scope.$on('adjustFinancial', function (event, data) {
|
|
$scope.IsEditable = data;
|
|
});
|
|
$scope.$on('changedDates', function (event, data) {
|
|
$scope.setStartDate(data.sDate);
|
|
});
|
|
$scope.$on('cutoffDates', function (event, data) {
|
|
$scope.cutoffDate(data.sDate);
|
|
});
|
|
|
|
$scope.filters = {
|
|
CollapsedClass: $scope.CollapsedIcon,
|
|
Show: false,
|
|
Months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
|
|
StartDate: null,
|
|
EndDate: null,
|
|
CostSavings: null,
|
|
CostSavingType: null,
|
|
ScenarioStartDate: null,
|
|
IsCostChanging: false,
|
|
Description: null
|
|
};
|
|
$scope.$watch("filters.StartDate", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
$('#' + $scope.ctrlStartDateId).parents('.input-daterange').data('datepicker').pickers[0].setDate(newValue);
|
|
$scope.getYears();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
});
|
|
$scope.$watch("filters.EndDate", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
$scope.getYears();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
});
|
|
$scope.$watch("filters.CostSavings", function (newValue, oldValue) {
|
|
if (newValue != oldValue && !$scope.filters.IsCostChanging) {
|
|
$scope.getYears();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
$scope.filters.IsCostChanging = false;
|
|
});
|
|
$scope.$watch("filters.CostSavingType", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
});
|
|
$scope.$watch("filters.Description", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
});
|
|
$scope.$watch("data", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
$scope.items = $scope.data ? JSON.stringify($scope.data) : null;
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
});
|
|
$scope.setStartDate = function (value) {
|
|
$scope.filters.StartDate = value;
|
|
var nd = new Date(new Date(value).getTime() + 1000 * 60 * 60 * 24);
|
|
$scope.filters.EndDate = (nd.getMonth() + 1) + '/' + nd.getDate() + '/' + nd.getFullYear();
|
|
$scope.getYears();
|
|
};
|
|
|
|
$scope.cutoffDate = function (value) {
|
|
if ($('#CostSaving_CostSavingStartDate').data !== 'function' || typeof $('#CostSaving_CostSavingEndDate').data('datepicker').setStartDate !== 'function')
|
|
return;
|
|
var date = new Date(value);
|
|
$('#CostSaving_CostSavingStartDate').data('datepicker').setStartDate(date);
|
|
$('#CostSaving_CostSavingEndDate').data('datepicker').setStartDate(date);
|
|
|
|
//var options = {
|
|
// format: 'mm/dd/yyyy',
|
|
// autoclose: true,
|
|
// orientation: $('body').hasClass('right-to-left') ? "auto right" : 'auto auto',
|
|
// minDate: value
|
|
//};
|
|
|
|
//$('#bs-datepicker-cost-range').datepicker(options);
|
|
//$('#CostSaving_CostSavingStartDate').datepicker("option", "minDate", value);
|
|
//$('#CostSaving_CostSavingEndDate').datepicker("option", "minDate", value);
|
|
};
|
|
$scope.setEndDate = function (value) {
|
|
|
|
};
|
|
$scope.init = function (initData) {
|
|
$scope.data = null;
|
|
$scope.items = initData.items;
|
|
$scope.filters.StartDate = initData.startDate;
|
|
$scope.filters.EndDate = initData.endDate;
|
|
$scope.filters.CostSavings = initData.costSavings;
|
|
$scope.filters.CostSavingType = initData.costSavingType;
|
|
$scope.filters.Description = initData.costSavingDescription;
|
|
$scope.filters.Show = initData.isMonthlyMode;
|
|
$scope.IsExpanded = initData.isExpanded;
|
|
$scope.IsEditable = initData.isEditable;
|
|
$scope.ctrlStartDateId = initData.ctrlStartDateId;
|
|
|
|
if ($scope.filters.StartDate == null || $scope.filters.StartDate.length == 0 ||
|
|
$scope.filters.EndDate == null || $scope.filters.EndDate.length == 0 ||
|
|
$scope.filters.CostSavings == null || $scope.filters.CostSavings.length == 0) {
|
|
return;
|
|
}
|
|
var sd = new Date($scope.filters.StartDate);
|
|
var ed = new Date($scope.filters.EndDate);
|
|
var cs = parseFloat($scope.filters.CostSavings);
|
|
if (isNaN(sd) || isNaN(ed) || isNaN(cs) || cs <= 0) {
|
|
return;
|
|
}
|
|
|
|
if (initData.items != null && initData.items.length > 0) {
|
|
blockUI();
|
|
|
|
$scope.data = JSON.parse(initData.items);
|
|
|
|
unblockUI();
|
|
}
|
|
};
|
|
$scope.getYears = function () {
|
|
$scope.data = null;
|
|
if ($scope.filters.StartDate == null || $scope.filters.StartDate.length == 0 ||
|
|
$scope.filters.EndDate == null || $scope.filters.EndDate.length == 0 ||
|
|
$scope.filters.CostSavings == null || $scope.filters.CostSavings.length == 0) {
|
|
return;
|
|
}
|
|
var sd = new Date($scope.filters.StartDate);
|
|
var ed = new Date($scope.filters.EndDate);
|
|
var cs = parseFloat($scope.filters.CostSavings);
|
|
if (isNaN(sd) || isNaN(ed) || isNaN(cs) || cs <= 0) {
|
|
return;
|
|
}
|
|
blockUI();
|
|
if (ed - sd > 3155760000000)
|
|
ed = new Date(sd + 3155760000000);
|
|
var sdYear = sd.getFullYear();
|
|
var edYear = ed.getFullYear();
|
|
var data = new Array();
|
|
var startMonth = sd.getMonth();
|
|
var endMonth = ed.getMonth();
|
|
var monthNum = 0;
|
|
for (var yearIndex = 0; yearIndex <= edYear - sdYear; yearIndex++) {
|
|
var arrYear = new Array();
|
|
arrYear.push(0);
|
|
for (var monthIndex = 0; monthIndex < 12; monthIndex++) {
|
|
if (yearIndex == 0 && monthIndex < startMonth) {// before start date
|
|
arrYear.push(null);
|
|
} else if (yearIndex == (edYear - sdYear) && monthIndex > endMonth) {// after end date
|
|
arrYear.push(null);
|
|
} else {// inside date range
|
|
arrYear.push(0);
|
|
monthNum++;
|
|
}
|
|
}
|
|
data.push({ Year: sdYear + yearIndex, Costs: arrYear });
|
|
}
|
|
var dec = Math.pow(10, 3);//precision decimals
|
|
var itemCost = Math.round(cs * dec / monthNum) / dec;
|
|
for (var i = 0; i < data.length; i++) {
|
|
for (var j = 1; j < data[i].Costs.length; j++) {
|
|
if (data[i].Costs[j] != null) {
|
|
data[i].Costs[j] = itemCost;
|
|
data[i].Costs[0] += itemCost;
|
|
}
|
|
}
|
|
data[i].Costs[0] = Math.round(data[i].Costs[0] * dec) / dec;
|
|
}
|
|
$scope.data = data;
|
|
unblockUI();
|
|
};
|
|
$scope.onMonthHeaderClick = function () {
|
|
$scope.filters.CollapsedClass = $scope.filters.Show ? $scope.NonCollapsedIcon : $scope.CollapsedIcon;
|
|
};
|
|
$scope.onShow = function(obj) {
|
|
obj.$form.$show([obj.$editable]);
|
|
};
|
|
$scope.onTxtBlur = function (txt) {
|
|
txt.$form.$submit([txt.$editable]);
|
|
};
|
|
$scope.watchKeyInput = function (t) {
|
|
$timeout(function () {
|
|
if (t.$editable.inputEl.select)
|
|
t.$editable.inputEl.select();
|
|
else if (t.$editable.inputEl.setSelectionRange)
|
|
t.$editable.inputEl.setSelectionRange(0, t.$editable.inputEl.val().length);
|
|
}, 3);
|
|
|
|
t.$editable.inputEl.on('keydown', function (e) {
|
|
if (e.which == 9) { //when tab key is pressed
|
|
e.preventDefault();
|
|
var tab2Cell;
|
|
if (e.shiftKey) { // when shift + tab use with 'onblur' set to 'submit' for automatic submission find the parent of the editable before this one in the markup grab the editable and display it
|
|
tab2Cell = t.$editable.elem.parentsUntil('table#costSaving-table').prevAll(":has(.editable:visible):first").find(".editable:visible:last");
|
|
t.$form.$submit([t.$editable]);
|
|
tab2Cell.click();
|
|
} else { // when just tab use with 'onblur' set to 'submit' for automatic submission find the parent of the editable after this one in the markup grab the editable and display it
|
|
tab2Cell = t.$editable.elem.parentsUntil('table#costSaving-table').nextAll(":has(.editable:visible):first").find(".editable:visible:first");
|
|
t.$form.$submit([t.$editable]);
|
|
tab2Cell.click();
|
|
}
|
|
} else if (e.which == 13) {
|
|
t.$form.$submit([t.$editable]);
|
|
}
|
|
});
|
|
};
|
|
$scope.checkValue = function (data, yearIndex, colIndex) {
|
|
var newValue = parseFloat(data);
|
|
if (isNaN(newValue))
|
|
newValue = 0;
|
|
if (newValue < 0) {
|
|
return "Value should not be less than zero";
|
|
}
|
|
if (colIndex == 0) {
|
|
$scope.data[yearIndex].Costs[0] = Math.round(newValue * $scope.Dec) / $scope.Dec;
|
|
var num = 0;
|
|
var j;
|
|
for (j = 1; j <= 12; j++) {
|
|
if ($scope.data[yearIndex].Costs[j] != null)
|
|
num++;
|
|
}
|
|
var val = Math.round(newValue * $scope.Dec / num) / $scope.Dec;
|
|
for (j = 1; j <= 12; j++) {
|
|
if ($scope.data[yearIndex].Costs[j] != null)
|
|
$scope.data[yearIndex].Costs[j] = val;
|
|
}
|
|
} else {
|
|
var oldValue = $scope.data[yearIndex].Costs[colIndex];
|
|
$scope.data[yearIndex].Costs[colIndex] = Math.round(newValue * $scope.Dec) / $scope.Dec;
|
|
$scope.data[yearIndex].Costs[0] += Math.round((newValue - oldValue) * $scope.Dec) / $scope.Dec;
|
|
if ($scope.data[yearIndex].Costs[0] < 0)
|
|
$scope.data[yearIndex].Costs[0] = 0;
|
|
}
|
|
var sum = 0;
|
|
for (var i = 0; i < $scope.data.length; i++) {
|
|
sum += $scope.data[i].Costs[0];
|
|
}
|
|
sum = Math.round(sum * 100) / 100;
|
|
$scope.filters.IsCostChanging = true;
|
|
$scope.filters.CostSavings = sum;
|
|
|
|
triggerCostSavingChangedEvent();
|
|
//required to be false by xeditable grid
|
|
return false;
|
|
};
|
|
$scope.addCostSaving = function () {
|
|
$scope.IsExpanded = true;
|
|
};
|
|
|
|
var getCostSavings = function () {
|
|
var startDate = new Date($scope.filters.StartDate),
|
|
endDate = new Date($scope.filters.EndDate);
|
|
|
|
return {
|
|
CostSavingItems: $scope.data,
|
|
CostSavingStartDate: Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()),
|
|
CostSavingEndDate: Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()),
|
|
CostSavings: $scope.filters.CostSavings,
|
|
CostSavingType: $scope.filters.CostSavingType,
|
|
CostSavingDescription: $scope.filters.Description
|
|
};
|
|
};
|
|
|
|
var triggerCostSavingChangedEvent = function () {
|
|
$scope.$emit('costSavingChanged', getCostSavings());
|
|
};
|
|
}]); |