"use strict"; (function () { 'use strict'; angular.module('assetModule') .controller('AssetEditInventoryController', ['$scope', 'assetModel', 'ticketService', 'searchModel', 'userModel', '$q', 'i18nService', 'systemAlertService', '$modalInstance', 'events', function ($scope, assetModel, ticketService, searchModel, userModel, $q, i18nService, systemAlertService, $modalInstance, events) { var role = null, state = { processing: false, loadingInventory: false, tooManyCompanies: false, tooManySites: false, tooManySupportCompanies: false, tooManyOrganizations: false, tooManySupportGroups: false, tooManySupportPeople: false, tooManyInventories: false, isDirty: false }, allSites = { name: i18nService.getLocalizedString('asset.actionBlade.editInventory.site.all'), id: 'All' }, allSupportCompanies = { name: i18nService.getLocalizedString('asset.actionBlade.editInventory.company.all'), id: 'All' }, allOrganizations = { name: i18nService.getLocalizedString('assignBlade.organization.all'), id: 'All' }, allSupportGroups = { name: i18nService.getLocalizedString('assignBlade.supportGroup.all'), id: 'All' }, allOwners = { fullName: i18nService.getLocalizedString('asset.actionBlade.editInventory.owner.all'), loginId: 'All' }; $scope.company = {}; $scope.site = {}; $scope.floor = ''; $scope.room = ''; $scope.grid = ''; $scope.bin = ''; $scope.supportCompany = {}; $scope.supportOrganization = {}; $scope.supportGroup = {}; $scope.companies = []; $scope.organizations = []; $scope.groups = []; $scope.inventories = []; $scope.search = { text: '', filterText: '' }; $scope.selectedInventory = {}; assetModel.getAssetDetailsByID(assetModel.assetId, assetModel.assetClassId).then(function (assetDetails) { $scope.asset = assetDetails; $scope.company = $scope.asset.company; $scope.supportCompany = allSupportCompanies; getCompanies().then(function () { loadSites(); }); getSupportCompanies().then(function () { $scope.selectSupportCompany($scope.supportCompany); }); }); $scope.selectCompany = function (company) { $scope.company = company; loadSites(); loadInventories(); }; function loadSites() { state.processing = true; $scope.site = allSites; searchModel.getSitesByCompany($scope.company.name) .then(function (response) { $scope.sites = response.objects; state.tooManySites = response.exceedsChunkSize; }) .finally(completeProcessing); } $scope.selectSite = function (site) { state.isDirty = true; $scope.site = site; loadInventories(); }; $scope.getSitesByTextAndCompany = function (searchText) { return searchModel.getSitesByTextAndCompany(searchText, $scope.company.name).then(function (response) { return { list: response.list, exceedsChunkSize: response.exceedsChunkSize }; }); }; $scope.selectSupportCompany = function (company) { state.isDirty = true; state.processing = true; $scope.supportCompany = company; $scope.organizations = []; $scope.supportOrganization = allOrganizations; $scope.supportGroup = allSupportGroups; $scope.owner = allOwners; $scope.groups = []; $scope.owners = []; var promises = [], customerCompany = null, locationCompany = null, ownerCompany = null, primaryCompany = null; if ($scope.supportCompany.name) { promises.push(getSupportOrganizationsForCompany($scope.supportCompany.name, role, customerCompany, locationCompany, ownerCompany, primaryCompany)); promises.push(getSupportGroupsForCompanyAndOrg($scope.supportCompany, $scope.supportOrganization, role, customerCompany, locationCompany, ownerCompany, primaryCompany)); promises.push(ticketService.getPersonsBySupportCompanyAndOrg($scope.supportCompany.name, '', '', role, null, customerCompany, locationCompany, ownerCompany, primaryCompany)); } $q.all(promises).then(function (results) { $scope.organizations = results[0]; $scope.groups = results[1]; if (($scope.groups && $scope.groups.length) || state.tooManySupportGroups) { $scope.supportGroup = allSupportGroups; } state.tooManySupportPeople = results[2].exceedsChunkSize; handleOwners(results[2].results); }).finally(completeProcessing); loadInventories(); }; $scope.selectSupportOrganization = function (organization) { state.processing = true; $scope.supportOrganization = organization; $scope.supportGroup = {}; $scope.owners = []; $scope.owner = {}; var promises = [], customerCompany = null, locationCompany = null, ownerCompany = null, primaryCompany = null; promises.push(getSupportGroupsForCompanyAndOrg($scope.supportCompany, $scope.supportOrganization, '', customerCompany, locationCompany, ownerCompany, primaryCompany)); promises.push(ticketService.getPersonsBySupportCompanyAndOrg($scope.supportCompany.name, organization.id === 'All' ? '' : organization.name, '', '', null, customerCompany, locationCompany, ownerCompany, primaryCompany)); $q.all(promises).then(function (results) { $scope.groups = results[0]; if ($scope.groups.length || state.tooManySupportGroups) { $scope.supportgroup = allSupportGroups; } state.tooManySupportPeople = results[1].exceedsChunkSize; handleOwners(results[1].results); }).finally(completeProcessing); loadInventories(); }; $scope.selectSupportGroup = function (group) { state.isDirty = true; state.processing = true; $scope.supportGroup = group; $scope.owner = {}; var customerCompany = null, locationCompany = null, ownerCompany = null, primaryCompany = null; if (group.id === 'All') { ticketService.getPersonsBySupportCompanyAndOrg($scope.supportCompany.name, $scope.supportOrganization.id === 'All' ? '' : $scope.supportOrganization.name, '', role, null, customerCompany, locationCompany, ownerCompany, primaryCompany) .then(function (response) { state.tooManySupportPeople = response.exceedsChunkSize; handleOwners(response.results); }).finally(completeProcessing); } else { ticketService.getPersonsBySupportGroupId($scope.supportGroup.id, '', role, null) .then(function (response) { state.tooManySupportPeople = response.exceedsChunkSize; handleOwners(response.results); }) .finally(completeProcessing); } loadInventories(); }; $scope.selectOwner = function (owner) { state.isDirty = true; $scope.owner = owner; loadInventories(); }; $scope.onSearchTextChanged = function () { state.isDirty = true; if ($scope.search.text.length > 2) { loadInventories(); } else { $scope.inventories = []; } }; $scope.onInputTextChanged = function () { state.isDirty = true; loadInventories(); }; function loadInventories() { state.loadingInventory = true; var data = { name: $scope.search.text, company: { name: $scope.company.name }, site: $scope.site.id === 'All' ? {} : { name: $scope.site.name }, floor: $scope.floor, room: $scope.room, grid: $scope.grid, bin: $scope.bin, supportCompany: $scope.supportCompany.id === 'All' ? {} : { name: $scope.supportCompany.name }, supportOrganization: $scope.supportOrganization.id === 'All' ? {} : { organization: $scope.supportOrganization.name }, supportOwnerGroup: $scope.supportGroup.id === 'All' ? {} : { name: $scope.supportGroup.name }, supportOwner: $scope.owner.loginId === 'All' ? {} : { fullName: $scope.owner.fullName } }; assetModel.searchInventory(data) .then(function (result) { var inventories = (result[0].items[0].objects); if (inventories.length >= searchModel.inventoryChunkSize) { $scope.inventories = []; state.tooManyInventories = true; } else { $scope.inventories = _.sortBy(inventories, 'name'); state.tooManyInventories = false; } }) .finally(function () { state.loadingInventory = false; }); } $scope.selectInventory = function (inventory) { state.isDirty = true; $scope.selectedInventory = inventory; }; $scope.isSaveButtonDisabled = function () { return !$scope.selectedInventory.name; }; $scope.submit = function () { state.processing = true; assetModel.putIntoInventory(assetModel.assetId, assetModel.assetClassId, $scope.selectedInventory).then(function (assetDetails) { assetModel.updateCacheAssetDetails(assetDetails); $modalInstance.close(); }).catch(function (error) { if (error) { systemAlertService.error({ text: error.data.error, clear: false }); } return $q.reject(error); }).finally(completeProcessing); }; $scope.getOwnersByName = function (name) { $scope.owner = {}; if ($scope.supportGroup.id === 'All') { ticketService.getPersonsBySupportCompanyAndOrg($scope.supportCompany.name, $scope.supportOrganization.id === 'All' ? '' : $scope.supportOrganization.name, name, role, null) .then(function (response) { state.tooManySupportPeople = response.exceedsChunkSize; handleOwners(response.results); }); } else { ticketService.getPersonsBySupportGroupId($scope.supportGroup.id, name, role, null) .then(function (response) { state.tooManySupportPeople = response.exceedsChunkSize; handleOwners(response.results); }); } }; $scope.getSupportGroupsForCompanyAndOrgByName = function (name) { return searchModel.getSupportGroupsForCompanyAndOrgByName($scope.supportCompany.name, $scope.supportOrganization.id === 'All' ? '' : $scope.supportOrganization.name, name, searchModel.supportGroupChunkSize); }; function getSupportGroupsForCompanyAndOrg(company, organization, role, customerCompany, locationCompany, ownerCompany, primaryCompany) { return searchModel.getSupportGroupsForCompanyAndOrg(company.name, organization.id === 'All' ? '' : organization.name, role, null, customerCompany, locationCompany, ownerCompany, primaryCompany) .then(function (response) { if (response.supportGroups.length && response.supportGroups[0].id !== 'All') { response.supportGroups.unshift(allSupportGroups); } state.tooManySupportGroups = response.exceedsChunkSize; return response.supportGroups; }); } $scope.getSupportOrganizationsByTextAndCompany = function (searchText) { return searchModel.getSupportOrganizationsByTextAndCompany(searchText, $scope.supportCompany.name, searchModel.organizationChunkSize).then(function (result) { return result.organizations; }); }; function getSupportOrganizationsForCompany(companyName, role, customerCompany, locationCompany, ownerCompany, primaryCompany) { return searchModel.getSupportOrganizationsByCompany(companyName, null, role, customerCompany, locationCompany, ownerCompany, primaryCompany) .then(function (response) { if (response.organizations.length && response.organizations[0].id !== 'All') { response.organizations.unshift(allOrganizations); } state.tooManyOrganizations = response.exceedsChunkSize; return response.organizations; }); } function getCompanies() { var options = {}; return searchModel.getOperatingCompanies(options, -1).then(function (response) { $scope.companies = _.cloneDeep(response.companies); state.tooManyCompanies = response.exceedsChunkSize; }); } $scope.getCompaniesByName = function (name) { return searchModel.getCompaniesByText(name).then(function (response) { return response.companies; }); }; function getSupportCompanies() { var options = {}; return searchModel.getSupportCompanies(options).then(function (supportCompanies) { $scope.supportCompanies = _.cloneDeep(supportCompanies); if (supportCompanies.length >= searchModel.companyChunkSize) { state.tooManySupportCompanies = true; } else { if (supportCompanies.length && supportCompanies[0].id !== 'All') { supportCompanies.unshift(allSupportCompanies); } state.tooManySupportCompanies = false; } }); } $scope.getSupportCompaniesByName = function (name) { return searchModel.getCompaniesByText(name).then(function (response) { return response.companies; }); }; function handleOwners(members) { members = _.uniqBy(members, function (member) { return member.loginId; }); $scope.owners = _.sortBy(members, 'fullName'); } function completeProcessing() { state.processing = 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); $scope.state = state; }]); })();