116 lines
5.2 KiB
JavaScript
116 lines
5.2 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('assetModule')
|
|
.controller('AssetEditStatusController', ['$scope', '$modalInstance', 'assetModel', 'metadataModel', '$q', 'systemAlertService', '$filter', 'events', 'i18nService',
|
|
function ($scope, $modalInstance, assetModel, metadataModel, $q, systemAlertService, $filter, events, i18nService) {
|
|
var state = {
|
|
updatingStatus: false,
|
|
isDirty: false
|
|
}, selected = {
|
|
status: {},
|
|
statusReason: {}
|
|
};
|
|
$scope.state = state;
|
|
$scope.selected = selected;
|
|
var currentStatus = { value: '', reason: '' };
|
|
var assetType = '';
|
|
var assetTypes = {};
|
|
var assetDetailsPromise = assetModel.getAssetDetailsByID(assetModel.assetId, assetModel.assetClassId).then(function (assetDetails) {
|
|
currentStatus = angular.copy(assetDetails.status);
|
|
assetType = assetDetails.assetType;
|
|
});
|
|
var metadataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_ASSET).then(function (meta) {
|
|
$scope.statuses = meta.statuses;
|
|
assetTypes = meta.assetTypes;
|
|
});
|
|
$q.all([metadataPromise, assetDetailsPromise])
|
|
.finally(function () {
|
|
selected.status = _.find($scope.statuses, { name: currentStatus.value }) || {};
|
|
selected.statusReason = _.find(selected.status.statusReasons, { name: currentStatus.reason }) || {};
|
|
var currentAssetType = _.find(assetTypes, { name: assetType });
|
|
var currentSubType = _.find(currentAssetType.subType, { name: assetModel.assetClassId });
|
|
if (currentSubType.putIntoInventory) {
|
|
$scope.statuses = _.filter($scope.statuses, function (status) {
|
|
return status.name !== EntityVO.STATUS_IN_INVENTORY;
|
|
});
|
|
}
|
|
});
|
|
$scope.changeStatus = function (status) {
|
|
selected.status = status;
|
|
state.isDirty = true;
|
|
selected.statusReason = {};
|
|
};
|
|
$scope.changeStatusReason = function (statusReason) {
|
|
selected.statusReason = statusReason;
|
|
state.isDirty = true;
|
|
};
|
|
$scope.isSaveButtonDisabled = function () {
|
|
if (_.isEmpty(selected.status)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
$scope.submit = function () {
|
|
state.updatingStatus = true;
|
|
var data = {
|
|
status: selected.status.name,
|
|
statusReason: selected.statusReason.name ? selected.statusReason.name : ''
|
|
};
|
|
if (!_.isEmpty(currentStatus) && currentStatus.value === EntityVO.STATUS_IN_INVENTORY) {
|
|
data.removeFromInventory = true;
|
|
}
|
|
assetModel.update(data).then(function (assetDetails) {
|
|
assetModel.updateCacheAssetDetails(assetDetails);
|
|
$modalInstance.close();
|
|
if (assetDetails.needsReconciliation) {
|
|
systemAlertService.warning({
|
|
text: $filter('i18n')('asset.reconWarning'),
|
|
hide: 10000
|
|
});
|
|
}
|
|
}).catch(function (error) {
|
|
if (error) {
|
|
systemAlertService.error({
|
|
text: error.data.error,
|
|
clear: false
|
|
});
|
|
}
|
|
return $q.reject(error);
|
|
}).finally(function () {
|
|
state.updatingStatus = false;
|
|
});
|
|
};
|
|
$scope.close = function () {
|
|
if (state.isDirty) {
|
|
var modalInstance = systemAlertService.modal({
|
|
title: i18nService.getLocalizedString('common.notification.dirty.title'),
|
|
text: i18nService.getLocalizedString('common.notification.dirty.message'),
|
|
buttons: [
|
|
{
|
|
text: i18nService.getLocalizedString('common.labels.yes'),
|
|
data: true
|
|
},
|
|
{
|
|
text: i18nService.getLocalizedString('common.labels.no'),
|
|
data: false
|
|
}
|
|
]
|
|
});
|
|
modalInstance.result.then(function (data) {
|
|
if (data) {
|
|
$modalInstance.dismiss();
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
$modalInstance.dismiss();
|
|
}
|
|
};
|
|
function handleModalBackdropClick() {
|
|
$scope.close();
|
|
}
|
|
$scope.$on(events.MODAL_BACKDROP_CLICK, handleModalBackdropClick);
|
|
}]);
|
|
})();
|