282 lines
12 KiB
JavaScript
282 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
app.controller('costSavingController', ['$scope', '$http', '$location', '$timeout', '$document', 'costSavingService', function ($scope, $http, $location, $timeout, $document, costSavingService) {
|
|
var commonErrorTitle = 'Oops!',
|
|
commonErrorMessage = 'An error occurred while processing your request. Please, try again later.';
|
|
|
|
$scope.ViewModel = {
|
|
Items: null,
|
|
ItemsJson: null,
|
|
IsEditable: null,
|
|
IsExpanded: null,
|
|
Show: false,
|
|
Months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
|
|
ScenarioId: null,
|
|
StartDate: null,
|
|
EndDate: null,
|
|
CostSavings: null,
|
|
CostSavingType: null,
|
|
Description: null
|
|
};
|
|
$scope.$on('adjustFinancial', function (event, data) {
|
|
$scope.ViewModel.IsEditable = data;
|
|
});
|
|
$scope.$on('scenarioRangeChanged', function (event, data) {
|
|
try {
|
|
changeRange(data);
|
|
}
|
|
catch (e) {
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
}
|
|
});
|
|
|
|
$scope.$watch("ViewModel.StartDate", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
var isChanged = false;
|
|
var backup = costSavingService.createBackup($scope.ViewModel.ScenarioId);
|
|
try {
|
|
isChanged = costSavingService.setStartDate($scope.ViewModel.ScenarioId, newValue);
|
|
}
|
|
catch (e) {
|
|
if (!!backup) {
|
|
costSavingService.restoreBackup($scope.ViewModel.ScenarioId, backup);
|
|
}
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
return;
|
|
}
|
|
|
|
if (isChanged) {
|
|
refreshGridView();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
}
|
|
});
|
|
$scope.$watch("ViewModel.EndDate", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
var isChanged = false;
|
|
var backup = costSavingService.createBackup($scope.ViewModel.ScenarioId);
|
|
try {
|
|
isChanged = costSavingService.setEndDate($scope.ViewModel.ScenarioId, newValue);
|
|
}
|
|
catch (e) {
|
|
if (!!backup) {
|
|
costSavingService.restoreBackup($scope.ViewModel.ScenarioId, backup);
|
|
}
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
return;
|
|
}
|
|
|
|
if (isChanged) {
|
|
refreshGridView();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
}
|
|
});
|
|
$scope.$watch("ViewModel.CostSavings", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
var isChanged = false;
|
|
var backup = costSavingService.createBackup($scope.ViewModel.ScenarioId);
|
|
try {
|
|
isChanged = costSavingService.setCostSavings($scope.ViewModel.ScenarioId, newValue);
|
|
}
|
|
catch (e) {
|
|
if (!!backup) {
|
|
costSavingService.restoreBackup($scope.ViewModel.ScenarioId, backup);
|
|
}
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
return;
|
|
}
|
|
|
|
if (isChanged) {
|
|
refreshGridView();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
}
|
|
});
|
|
$scope.$watch("ViewModel.CostSavingType", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
try {
|
|
var isChanged = costSavingService.setCostSavingType($scope.ViewModel.ScenarioId, newValue);
|
|
if (isChanged) {
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
}
|
|
catch (e) {
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
}
|
|
}
|
|
});
|
|
$scope.$watch("ViewModel.Description", function (newValue, oldValue) {
|
|
if (newValue != oldValue) {
|
|
try {
|
|
var isChanged = costSavingService.setDescription($scope.ViewModel.ScenarioId, newValue);
|
|
if (isChanged) {
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
}
|
|
catch (e) {
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
}
|
|
}
|
|
});
|
|
|
|
$scope.init = function (initData) {
|
|
costSavingService.init({
|
|
ScenarioId: initData.scenarioId,
|
|
ScenarioStartDate: initData.scenarioStartDate,
|
|
ScenarioEndDate: initData.scenarioEndDate,
|
|
StartDate: initData.startDate,
|
|
EndDate: initData.endDate,
|
|
CostSavings: initData.costSavings,
|
|
CostSavingType: initData.costSavingType,
|
|
Description: initData.costSavingDescription,
|
|
Items: !!initData.items ? JSON.parse(initData.items) : null
|
|
});
|
|
|
|
$scope.ViewModel.ItemsJson = initData.items;
|
|
$scope.ViewModel.ScenarioId = initData.scenarioId;
|
|
$scope.ViewModel.StartDate = initData.startDate;
|
|
$scope.ViewModel.EndDate = initData.endDate;
|
|
$scope.ViewModel.CostSavings = initData.costSavings;
|
|
$scope.ViewModel.CostSavingType = initData.costSavingType;
|
|
$scope.ViewModel.Description = initData.costSavingDescription;
|
|
$scope.ViewModel.Show = initData.isMonthlyMode;
|
|
$scope.ViewModel.IsExpanded = initData.isExpanded;
|
|
$scope.ViewModel.IsEditable = initData.isEditable;
|
|
|
|
refreshGridView();
|
|
};
|
|
$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]);
|
|
$timeout(function () {
|
|
tab2Cell.click();
|
|
}, 0);
|
|
} 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]);
|
|
$timeout(function () {
|
|
tab2Cell.click();
|
|
}, 0);
|
|
}
|
|
} 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";
|
|
}
|
|
|
|
var isChanged = false;
|
|
var backup = costSavingService.createBackup($scope.ViewModel.ScenarioId);
|
|
try {
|
|
isChanged = costSavingService.setCellValue($scope.ViewModel.ScenarioId, yearIndex, colIndex, newValue);
|
|
}
|
|
catch (e) {
|
|
if (!!backup) {
|
|
costSavingService.restoreBackup($scope.ViewModel.ScenarioId, backup);
|
|
}
|
|
showErrorModal(commonErrorTitle, commonErrorMessage);
|
|
return;
|
|
}
|
|
|
|
if (isChanged) {
|
|
$scope.ViewModel.CostSavings = costSavingService.getCostSavings($scope.ViewModel.ScenarioId);
|
|
refreshGridView();
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
|
|
//required to be false by xeditable grid
|
|
return false;
|
|
};
|
|
$scope.addCostSaving = function () {
|
|
$scope.ViewModel.IsExpanded = true;
|
|
};
|
|
$scope.removeCostSaving = function () {
|
|
$scope.ViewModel.IsExpanded = false;
|
|
};
|
|
function refreshGridView() {
|
|
var data = costSavingService.getCostSavingData($scope.ViewModel.ScenarioId);
|
|
if (!data || !data.Items) {
|
|
$scope.ViewModel.Items = null;
|
|
return;
|
|
}
|
|
|
|
// in this case DAL object is similar to view model object
|
|
// in case when view model will be different from DAL model replace the following line with a convertation logic
|
|
$scope.ViewModel.Items = angular.copy(data.Items);
|
|
};
|
|
|
|
function triggerCostSavingChangedEvent() {
|
|
var data4Save = costSavingService.getCostSavings4Save($scope.ViewModel.ScenarioId);
|
|
$scope.ViewModel.ItemsJson = (!!data4Save && !!data4Save.CostSavingItems) ? JSON.stringify(data4Save.CostSavingItems) : null;
|
|
$scope.$emit('costSavingChanged', data4Save);
|
|
};
|
|
|
|
function changeRange(data) {
|
|
data = data || {};
|
|
|
|
var rangeIsChanged = false;
|
|
var backup = costSavingService.createBackup($scope.ViewModel.ScenarioId);
|
|
try {
|
|
var range = costSavingService.changeScenarioRange($scope.ViewModel.ScenarioId, data.startDate, data.endDate) || {};
|
|
var eventData = {
|
|
scenarioStartDate: data.startDate,
|
|
scenarioEndDate: data.endDate,
|
|
costSavingStartDateOld: $scope.ViewModel.StartDate,
|
|
costSavingStartDateNew: range.startDate,
|
|
costSavingEndDateOld: $scope.ViewModel.EndDate,
|
|
costSavingEndDateNew: range.endDate
|
|
};
|
|
|
|
// 1st step: first of all we should change start/end date of DAL object to prevent double recalculation of cost saving items (if we would set them through view model as in the 2nd step)
|
|
rangeIsChanged = costSavingService.setRange($scope.ViewModel.ScenarioId, range.startDate, range.endDate);
|
|
}
|
|
catch (e) {
|
|
if (!!backup) {
|
|
costSavingService.restoreBackup($scope.ViewModel.ScenarioId, backup);
|
|
}
|
|
throw 'Error occured during changing cost saving range';
|
|
}
|
|
|
|
// we do not need to include this part of code in the try/catch block because these changes relate to the view model, but not to data
|
|
if (rangeIsChanged) {
|
|
// 2nd step: then we should set view model values (recalculation on the service will not be called because service already contains new values)
|
|
$scope.ViewModel.StartDate = range.startDate;
|
|
$scope.ViewModel.EndDate = range.endDate;
|
|
|
|
// 3rd step: refresh grid with cost saving items
|
|
refreshGridView();
|
|
|
|
// 4th step: we should trigger event to other controllers
|
|
triggerCostSavingChangedEvent();
|
|
}
|
|
|
|
// we should trigger event to UI (it is useful when we need to update non-angular datepickers manually)
|
|
// and we need to do it always because in all cases after changing of scenario dates we need to update min dates for cost saving datepickers
|
|
$document.trigger('cs.auto.costSaving-range-changed', eventData);
|
|
};
|
|
}]); |