SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/create/create-asset-controller.js

549 lines
27 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('createTicketModule')
.controller('CreateAssetController', ['$scope', '$modal', '$state', '$timeout', 'createTicketModel', 'screenConfigurationModel', 'ticketModel', 'i18nService', 'systemAlertService', 'categoriesService', '$q', 'metadataModel', 'searchModel', 'assetModel', '$filter', 'events', 'searchService', 'configurationModel',
function ($scope, $modal, $state, $timeout, createTicketModel, screenConfigurationModel, ticketModel, i18nService, systemAlertService, categoriesService, $q, metadataModel, searchModel, assetModel, $filter, events, searchService, configurationModel) {
if (!configurationModel.isServerApplicationEnabled(EntityVO.TYPE_ASSET)) {
$state.go('unauthorized');
return;
}
var asset = {
type: EntityVO.TYPE_ASSET,
advancedType: 'Computer System',
classId: '',
customFields: {},
availableDate: { 'date': undefined, 'time': undefined },
installationDate: { 'date': undefined, 'time': undefined },
returnDate: { 'date': undefined, 'time': undefined },
receivedDate: { 'date': undefined, 'time': undefined },
lastScanDate: { 'date': undefined, 'time': undefined },
purchaseDate: { 'date': undefined, 'time': undefined },
disposalDate: { 'date': undefined, 'time': undefined },
/*SW00503167 - hiding currency fields for the time being till this issue is resolved*/
//unitPrice: { currencycode: 'USD' },
//marketValue: { currencycode: 'USD' },
//salesTax: { currencycode: 'USD' },
ticketType: EntityVO.TYPE_ASSET
}, assetMetadata = {}, assetSaved = false, selectedCapabilities = [];
function initMetadata() {
var metadataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_ASSET).then(function (metadata) {
angular.extend(assetMetadata, metadata);
asset.selectedStatus = _.find(assetMetadata.statuses, { name: 'Deployed' });
$scope.updateStatusReason();
$scope.metadata = assetMetadata;
assetMetadata.assetTypes = _.sortBy(assetMetadata.assetTypes, 'label');
_.forEach(assetMetadata.assetTypes, function (type) {
type.subType = _.sortBy(type.subType, 'label');
});
asset.selectedDepreciation = _.head(assetMetadata.depreciated);
/*if (!_.isEmpty(assetMetadata.currencyFields)) {
asset.unitPrice.currencycode = assetMetadata.currencyFields.unitPrice.currencycode;
asset.salesTax.currencycode = assetMetadata.currencyFields.salesTax.currencycode;
asset.marketValue.currencycode = assetMetadata.currencyFields.marketValue.currencycode;
}*/
searchModel.getOperatingCompanies(null, -1).then(function (response) {
$scope.selections.companies = _.cloneDeep(response.companies);
$scope.state.tooManyCompanies = response.exceedsChunkSize;
$scope.setCompany(_.find(response.companies, { name: assetMetadata.systemConfigurations.defaultCompany }));
});
});
var customFieldLabelsPromise = screenConfigurationModel
.loadScreenConfigurationAndCustomFieldLabels(ScreenConfigurationVO.prototype.ASSET_SCREEN, EntityVO.TYPE_ASSET);
$q.all([metadataPromise, customFieldLabelsPromise]).then(function () {
asset.allCategories = _.cloneDeep(categoriesService.populateCategories([{
name: 'product',
tiers: {}
}], assetMetadata));
var fields = screenConfigurationModel.getEditableFieldsForScreen(ScreenConfigurationVO.prototype.ASSET_SCREEN);
$scope.customFields = angular.copy(fields);
$scope.genericCustomFields = [];
$scope.typeSpecificCustomFields = [];
_.forEach($scope.customFields, function (field) {
if (field.panelId === 'assetScreen.generic') {
$scope.genericCustomFields.push(field);
}
else {
$scope.typeSpecificCustomFields.push(field);
}
});
$scope.getPrimaryCapabilitiesByName('', true).then(function (data) {
$scope.primaryCapabilities = data.list;
});
asset.selectedType = _.find(assetMetadata.assetTypes, { name: 'Computer System' });
$scope.updateType();
}).finally(function () {
$scope.state.dataIsLoading = false;
});
}
function init() {
$scope.state = {
dataIsLoading: true,
selectType: true,
tooManyCompanies: false
};
$scope.selections = {
companies: []
};
$scope.datePickerOptions = {
startingDay: configurationModel.getWeekStartingDay(),
'show-weeks': false,
minDate: moment().year(1970).month(0).date(2),
maxDate: moment().year(2038).month(0).date(18)
};
$scope.assetDate = {
availableDatePicker: { open: false },
installationDatePicker: { open: false },
returnDatePicker: { open: false },
receivedDatePicker: { open: false },
lastScanDatePicker: { open: false },
purchaseDatePicker: { open: false },
disposalDatePicker: { open: false }
};
$scope.formContainsInvalidFields = createTicketModel.formContainsInvalidFields;
createTicketModel.currentTicket = asset;
$scope.asset = asset;
$scope.assetMetadata = assetMetadata;
$scope.isNew = true;
initAlertForDirtyForm();
clearFormFields();
$scope.capabilityList = [];
assetModel.getCapabilityListByName('', 2000).then(function (data) {
$scope.capabilityList = data.items;
});
$scope.siteOptions = {
company: {
visible: false,
attribute: 'companyName'
},
region: {
attribute: 'region'
},
siteGroup: {
attribute: 'siteGroup'
},
site: {
attribute: 'name'
}
};
$scope.siteSelected = {
companyName: $scope.asset.company ? $scope.asset.company.name : ''
};
}
$scope.isCapabilityChecked = function (item) {
return (selectedCapabilities.indexOf(item.value) > -1);
};
$scope.addCapability = function (item) {
var index = selectedCapabilities.indexOf(item.value);
if (index > -1) {
selectedCapabilities.splice(index, 1);
}
else {
selectedCapabilities.push(item.value);
}
asset.capabilityList = $scope.getCapabilities();
};
$scope.getCapabilities = function () {
if (selectedCapabilities.length) {
return selectedCapabilities.join('; ');
}
else {
return $filter('i18n')('common.button.select');
}
};
$scope.getPrimaryCapabilitiesByName = function (searchText, setTooManyPrimaryCapabilities) {
return screenConfigurationModel.loadDynamicSelectionFieldLabels(EntityVO.TYPE_ASSET, 'SHR:SDF:AST-Capabilities', searchText, null, null, null, null, true).then(function (response) {
if (setTooManyPrimaryCapabilities) {
$scope.state.tooManyPrimaryCapabilities = response.exceedsChunkSize;
}
return { list: response.items, exceedsChunkSize: response.exceedsChunkSize };
});
};
$scope.openDatePicker = function (calendar) {
calendar.open = true;
};
$scope.updateDateTime = function (type) {
if ((asset[type].date && !asset[type].time) || (!asset[type].date && asset[type].time)) {
asset[type].time = _.clone(asset[type].date);
}
var timeObj = moment(asset[type].time);
asset[type].value = moment(asset[type].date).set('hours', timeObj.get('hours')).set('minutes', timeObj.get('minutes')).valueOf();
};
$scope.updateAssetField = function (type, item) {
if (type === 'depreciated') {
asset[type] = item.index;
return;
}
asset[type] = item.name;
};
$scope.getSelectedTypeObject = function (type, value) {
return _.find(assetMetadata[type], { name: value });
};
// This block of code is duplication of same code in 'asset-edit-details-controller'
// Uses to hot-fix SW00540862, performance degradation in long typeahead lists
$scope.getCostCentersByCompany = function (text) {
$scope.loadingCostCenters = true;
return assetModel.getCostCentersByCompanyName(text, asset.company.name).then(function (results) {
$scope.isTooltipOpenCostCenter = $scope.showTooManyResultsMessage = results.exceedsChunkSize;
$timeout(function () {
$scope.isTooltipOpenCostCenter = false;
}, 10000);
return results.items;
}).finally(function () {
$scope.loadingCostCenters = false;
});
};
$scope.hideTooManyResultsMessage = function () {
$scope.isTooltipOpenCostCenter = false;
$scope.isTooltipOpenManufacturer = false;
$scope.isTooltipOpenSupplier = false;
};
// finish of same code //
$scope.updateCostCenter = function (item) {
assetModel.costCenter = item.value;
};
$scope.updateType = function () {
asset.selectedSubtype = asset.selectedType.name === asset.advancedType ? _.find(asset.selectedType.subType, { name: 'BMC_COMPUTERSYSTEM' }) : _.head(asset.selectedType.subType);
asset.classId = asset.selectedSubtype.name;
clearCategories();
clearTypeFields();
validateCustomFields();
updateStatuses();
};
$scope.updateSubtype = function () {
asset.classId = asset.selectedSubtype.name;
clearCategories();
clearTypeFields();
validateCustomFields();
updateStatuses();
};
function updateStatuses() {
if (asset.selectedSubtype.putIntoInventory) {
$scope.statuses = _.filter(assetMetadata.statuses, function (status) {
return status.name !== EntityVO.STATUS_IN_INVENTORY;
});
}
else {
$scope.statuses = _.cloneDeep(assetMetadata.statuses);
}
}
$scope.showAssetForm = function (todo) {
$scope.state.selectType = !todo;
if (!todo) {
clearFormFields();
}
};
$scope.setCompany = function (company) {
asset.company = company;
asset.customer = {
company: company
};
$scope.siteSelected = {
companyName: company ? company.name : ''
};
};
$scope.getList = function (type, term) {
return createTicketModel.getList(type, term);
};
$scope.getCompaniesByName = function (name) {
return searchModel.getCompaniesByText(name).then(function (response) {
return { list: response.companies, exceedsChunkSize: response.exceedsChunkSize };
});
};
$scope.getCompaniesByType = function (text, type) {
return searchModel.getCompaniesByText(text, -1, { "companyType": type }).then(function (data) {
if (type === 'Supplier') {
$scope.isTooltipOpenSupplier = $scope.exceedsChunkSizeSupplier = data.exceedsChunkSize;
}
else if (type === 'Manufacturer') {
$scope.isTooltipOpenManufacturer = $scope.exceedsChunkSizeManufacturer = data.exceedsChunkSize;
}
$timeout(function () {
$scope.isTooltipOpenManufacturer = false;
$scope.isTooltipOpenSupplier = false;
}, 10000);
return data.companies;
});
};
$scope.setCompanyByType = function (item, type) {
asset[type] = item;
};
$scope.clearValuesByType = function (type) {
asset[type] = null;
};
$scope.createAsset = function () {
$scope.state.dataIsLoading = true;
if ($scope.siteSelected.name) {
asset.siteName = $scope.siteSelected.name;
}
if ($scope.siteSelected.siteGroup) {
asset.siteGroup = $scope.siteSelected.siteGroup;
}
if ($scope.siteSelected.region) {
asset.siteRegion = $scope.siteSelected.region;
}
createTicketModel.createAsset().then(function (data) {
if (data) {
if (data.error) {
$scope.state.dataIsLoading = false;
systemAlertService.error({
text: data.error,
clear: true
});
}
else if (data.assetName) {
showSandboxModeMsg(data.assetName);
}
}
}).finally(function () {
$scope.state.dataIsLoading = false;
});
};
$scope.cancel = function () {
$state.go('dashboard');
};
$scope.updateStatusReason = function () {
asset.selectedStatusReason = {};
//Fix: SW00549493 - don't poppulate by default
// if (!_.isEmpty(asset.selectedStatus.statusReasons)) {
// asset.selectedStatusReason = _.head(asset.selectedStatus.statusReasons);
// }
};
function isFormDirty() {
return $scope.createAssetForm.$dirty;
}
$scope.$on(events.ASSET_UPDATE_PRODUCT_CATEGORY_MANUFACTURER, function ($event, data) {
asset.manufacturer = data.productManufacturer;
});
function initAlertForDirtyForm() {
$scope.$on('$stateChangeStart', function (event, toState, toParams) {
if (assetSaved) {
return;
}
if (isFormDirty() && toState.name !== EntityVO.TYPE_ASSET) {
event.preventDefault();
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: {
stateName: toState.name,
stateParams: toParams
}
},
{
text: i18nService.getLocalizedString('common.labels.no')
}
]
});
modalInstance.result.then(function (data) {
if (!_.isEmpty(data)) {
createTicketModel.currentTicket = asset = {};
$scope.createAssetForm.$setPristine();
$state.transitionTo(data.stateName, data.stateParams);
}
});
}
});
}
function showSandboxModeMsg(assetName) {
var modalInstance = systemAlertService.modal({
title: '',
text: i18nService.getLocalizedStringwithParams('create.asset.success.reconciliationDelayBanner', [assetName]),
buttons: [
{
text: i18nService.getLocalizedString('controls.action.ok')
}
]
});
modalInstance.result.then(function () {
assetSaved = true;
$state.go('dashboard');
});
}
function clearCategories() {
if (asset.allCategories) {
_.forEach(asset.allCategories[0].listOfTiers, function (tier) {
tier.selectedValue = '';
});
}
}
function clearTypeFields() {
asset.hostName = '';
asset.primaryCapability = null;
asset.capabilityList = '';
asset.selectedSystemType = null;
asset.selectedVirtualSystemType = null;
selectedCapabilities = [];
}
function clearFormFields() {
asset = {
type: EntityVO.TYPE_ASSET,
advancedType: 'Computer System',
classId: '',
customFields: {},
availableDate: { 'date': undefined, 'time': undefined },
installationDate: { 'date': undefined, 'time': undefined },
returnDate: { 'date': undefined, 'time': undefined },
receivedDate: { 'date': undefined, 'time': undefined },
lastScanDate: { 'date': undefined, 'time': undefined },
purchaseDate: { 'date': undefined, 'time': undefined },
disposalDate: { 'date': undefined, 'time': undefined },
//unitPrice: { currencycode: 'USD' },
//marketValue: { currencycode: 'USD' },
//salesTax: { currencycode: 'USD' },
ticketType: EntityVO.TYPE_ASSET
};
clearTypeFields();
createTicketModel.currentTicket = asset;
$scope.asset = asset;
initMetadata();
$scope.$broadcast(events.CLEAR_DESC);
$scope.$broadcast(events.DISCARD_CHANGES);
if ($scope.createAssetForm) {
$scope.createAssetForm.$setPristine();
}
}
function validateCustomFields() {
$scope.validTypeCustomFields = _.filter($scope.typeSpecificCustomFields, function (field) {
delete asset.customFields[field.name];
return _.includes(_.map(field.extension, 'classId'), asset.selectedSubtype.name);
});
}
$scope.$watch('selected.region', function (region) {
if (region) {
if ($scope.selected.siteGroup && $scope.selected.siteGroup.attributeMap
&& $scope.selected.siteGroup.attributeMap.regionName !== region.name) {
$scope.selected.siteGroup = null;
}
if ($scope.selected.site && $scope.selected.site.attributeMap
&& $scope.selected.site.attributeMap.regionName !== region.name) {
$scope.selected.site = null;
}
$scope.loadSiteGroups();
$scope.loadSites();
}
else {
if ($scope.selected) {
$scope.selected.siteGroup = "";
$scope.selected.site = "";
$scope.loadSiteGroups();
$scope.loadSites();
}
}
});
$scope.$watch('selected.siteGroup', function (siteGroup) {
if (siteGroup) {
$scope.selected.region = { name: siteGroup.attributeMap.regionName };
if ($scope.selected.site && $scope.selected.site.attributeMap
&& $scope.selected.site.attributeMap.siteGroupName !== siteGroup.name) {
$scope.selected.site = null;
}
}
else {
if ($scope.selected) {
$scope.selected.site = "";
}
}
});
$scope.$watch('selected.site', function (site) {
if (site) {
$scope.selected.siteGroup = {
name: site.attributeMap.siteGroupName,
attributeMap: { regionName: site.attributeMap.regionName }
};
asset.siteName = site.name;
asset.siteRegion = site.attributeMap.regionName;
asset.siteGroup = site.attributeMap.siteGroupName;
}
});
//DRSMX-66920 : Menu dependency in SmartIT not working as expected
$scope.$watch('siteSelected', function (site) {
if (site.region) {
$scope.asset.siteRegion = site.region;
}
if (site.siteGroup) {
$scope.asset.siteGroup = site.siteGroup;
}
if (site.name) {
$scope.asset.siteName = site.name;
}
if (site.companyName) {
$scope.asset.companyName = site.companyName;
}
});
$scope.loadRegions = function () {
$scope.selections.regions = null;
$scope.state.regionsLoading = true;
if ($scope.selected) {
$scope.selected.siteGroup = "";
$scope.selected.region = "";
$scope.selected.site = "";
}
if ($scope.asset.company && $scope.asset.company.name) {
var queryParam = {
companyName: $scope.asset.company.name,
chunkInfo: { startIndex: 0, chunkSize: searchModel.regionChunkSize }
};
createTicketModel.getList(EntityVO.TYPE_REGION, queryParam).then(function (regions) {
$scope.state.regionsLoading = false;
$scope.selections.regions = regions.objects;
$scope.state.tooManyRegions = regions.exceedsChunkSize;
});
}
};
$scope.loadSiteGroups = function () {
$scope.selections.siteGroups = null;
$scope.state.siteGroupsLoading = true;
if ($scope.asset.company && $scope.asset.company.name) {
var queryParam = {
companyName: $scope.asset.company.name,
chunkInfo: { startIndex: 0, chunkSize: searchModel.siteGroupChunkSize }
};
if ($scope.selected !== undefined && $scope.selected.region) {
queryParam.regionName = $scope.selected.region.name;
}
else {
queryParam.regionName = "";
}
createTicketModel.getList(EntityVO.TYPE_SITE_GROUP, queryParam).then(function (siteGroups) {
$scope.state.siteGroupsLoading = false;
$scope.selections.siteGroups = siteGroups.objects;
$scope.state.tooManySiteGroups = siteGroups.exceedsChunkSize;
});
}
};
$scope.loadSites = function () {
$scope.selections.sites = null;
$scope.state.sitesLoading = true;
if ($scope.asset.company && $scope.asset.company.name) {
var queryParam = {
companyName: $scope.asset.company.name,
chunkInfo: { startIndex: 0, chunkSize: searchModel.siteChunkSize }
};
if ($scope.selected !== undefined && $scope.selected.region) {
queryParam.regionName = $scope.selected.region.name;
}
else {
queryParam.regionName = "";
}
if ($scope.selected !== undefined && $scope.selected.siteGroup) {
queryParam.siteGroupName = $scope.selected.siteGroup.name;
}
else {
queryParam.siteGroupName = "";
}
createTicketModel.getList(EntityVO.TYPE_SITE, queryParam).then(function (sites) {
$scope.state.sitesLoading = false;
$scope.selections.sites = sites.objects;
$scope.state.tooManySites = sites.exceedsChunkSize;
});
}
};
init();
}
]);
})();