"use strict"; (function () { 'use strict'; angular.module('ticketModule') .controller('EditStatusController', ['$scope', '$modalInstance', 'editStatusParams', 'ticketModel', 'metadataModel', 'categoriesService', '$q', 'knowledgeArticleModel', 'systemAlertService', 'permissionModel', 'roles', 'searchModel', 'searchService', 'ticketService', 'fieldValidationModel', '$filter', '$state', 'i18nService', 'events', 'assetModel', function ($scope, $modalInstance, editStatusParams, ticketModel, metadataModel, categoriesService, $q, knowledgeArticleModel, systemAlertService, permissionModel, roles, searchModel, searchService, ticketService, fieldValidationModel, $filter, $state, i18nService, events, assetModel) { var isSingle = editStatusParams.actionItems.length === 1, id = editStatusParams.actionItems[0].id, type = editStatusParams.actionItems[0].type, state = { processing: true, updatingStatus: false, loadingSupportPersons: false, loadingSupportGroups: false, loadingAssigneeGroups: false, tooManySupportCompanies: false, tooManyAssigneeCompanies: false, tooManySupportOrganizations: false, tooManyAssigneeOrganizations: false, tooManySupportGroups: false, tooManyAssigneeGroups: false }, selected = { status: {}, statusReason: {}, milestone: {}, resolutionNote: '', worknote: '', manager: {}, group: {}, targetDate: '', isDisabled: false, assignee: {}, assigneeGroup: {} }, originalTicket, milestoneStatus, milestoneMetadata; $scope.isOptional = { managerGroup: false, manager: false, targetDate: false, coordinatorGroup: false, coordinator: false, assigneeGroup: false, assignee: false }; $scope.companies = []; $scope.coordinatorCompany = {}; $scope.assigneeCompany = {}; $scope.coordinatorOrganizations = []; $scope.coordinatorOrganization = {}; $scope.assigneeOrganizations = []; $scope.assigneeOrganization = {}; $scope.showUpdateWarningForRequired = false; if (!isSingle) { $scope.isOptional = { managerGroup: true, manager: true, targetDate: true, coordinatorGroup: true, coordinator: true, assigneeGroup: true, assignee: true }; _.each(editStatusParams.actionItems, function (ticket) { switch (type) { case EntityVO.TYPE_CHANGE: if ($scope.isOptional.manager && ticket.changeManager && ticket.changeManager.fullName === "") { $scope.isOptional.manager = false; } if ($scope.isOptional.managerGroup && ticket.changeManagerGroup && ticket.changeManagerGroup.name === "") { $scope.isOptional.managerGroup = false; } if (!$scope.showUpdateWarningForRequired && ((ticket.changeManager && ticket.changeManager.fullName) || (ticket.changeManagerGroup && ticket.changeManagerGroup.name))) { $scope.showUpdateWarningForRequired = true; } break; case EntityVO.TYPE_PROBLEM: if ($scope.isOptional.targetDate && ticket.targetDate === "") { $scope.isOptional.targetDate = false; } if ($scope.isOptional.coordinator && ticket.problemCoordinator && ticket.problemCoordinator.fullName === "") { $scope.isOptional.coordinator = false; } if ($scope.isOptional.coordinatorGroup && ticket.problemCoordinatorGroup && ticket.problemCoordinatorGroup.name === "") { $scope.isOptional.coordinatorGroup = false; } if ($scope.isOptional.assignee && ticket.assignee && ticket.assignee.fullName === "") { $scope.isOptional.assignee = false; } if ($scope.isOptional.assigneeGroup && ticket.supportGroup && ticket.supportGroup.name === "") { $scope.isOptional.assigneeGroup = false; } if (!$scope.showUpdateWarningForRequired && ((ticket.problemCoordinator && ticket.problemCoordinator.fullName) || (ticket.problemCoordinatorGroup && ticket.problemCoordinatorGroup.name) || (ticket.assignee && ticket.assignee.fullName) || (ticket.supportGroup && ticket.supportGroup.name))) { $scope.showUpdateWarningForRequired = true; } break; } }); } $scope.state = state; $scope.selected = selected; $scope.isReopen = editStatusParams.isReopen; $scope.allowLocationCompany = (type === EntityVO.TYPE_INCIDENT); $scope.isDraft = editStatusParams.isDraft; $scope.needResolutionNote = function () { return (type === EntityVO.TYPE_INCIDENT) && ~[EntityVO.STATUS_CLOSED, EntityVO.STATUS_RESOLVED].indexOf(selected.status.name.toLowerCase()); }; $scope.isSaveButtonDisabled = function () { return ($scope.needResolutionNote() && selected.resolutionNote === '') || (selected.status.statusReasons && _.isEmpty(selected.statusReason) && $scope.isFieldRequired('statusReason')) ? true : false; }; $scope.checkStatusSwitcherIsDisabled = function (type, ticketType) { var noSwitchStatuses = ['Pending', 'Cancelled', 'Closed', 'Rejected'], currentStatusIndex, nextStatusIndex, nextStatus; if (ticketType !== 'release') { currentStatusIndex = _.findIndex($scope.availableStatuses, function (status) { return status.index === $scope.selected.status.index; }); } else { currentStatusIndex = _.findIndex($scope.availableMilestoneStatuses, function (status) { return status.index === $scope.selected.milestone.index; }); } if (currentStatusIndex !== -1 && ticketType !== 'release') { if (type === 'prev') { return ((currentStatusIndex === 0) || noSwitchStatuses.indexOf($scope.selected.status.name) >= 0); } else if (type === 'next') { if (((ticketType === 'change' && ($scope.availableStatuses[currentStatusIndex].name === 'Implementation In Progress' || $scope.availableStatuses[currentStatusIndex].name === 'Planning In Progress')) || (ticketType === 'problem' || ticketType === 'knownerror')) && $scope.availableStatuses[currentStatusIndex + 1] && ($scope.availableStatuses[currentStatusIndex + 1].name === 'Pending' || $scope.availableStatuses[currentStatusIndex + 1].name === 'Cancelled')) { nextStatusIndex = parseInt(currentStatusIndex + 2); nextStatus = $scope.availableStatuses[nextStatusIndex] || {}; } else { nextStatusIndex = parseInt(currentStatusIndex + 1); nextStatus = $scope.availableStatuses[nextStatusIndex] || {}; } if ($scope.selected.status.name === 'Completed' && nextStatus.name === 'Closed') { return (noSwitchStatuses.indexOf($scope.selected.status.name) >= 0); } else { return (noSwitchStatuses.indexOf(nextStatus.name) >= 0 || noSwitchStatuses.indexOf($scope.selected.status.name) >= 0); } } } if (currentStatusIndex !== -1 && ticketType === 'release') { if (type === 'prev') { return (currentStatusIndex === 0); } else if (type === 'next') { nextStatusIndex = parseInt(currentStatusIndex + 1); if ($scope.availableMilestoneStatuses[nextStatusIndex] === undefined) { return true; } } } }; $scope.switchState = function (direction, ticketType) { var currentStatusIndex, switchToState = {}, prevStatusIndex, nextStatusIndex; if (ticketType !== 'release') { currentStatusIndex = _.findIndex($scope.availableStatuses, function (status) { return status.index === $scope.selected.status.index; }); } else { currentStatusIndex = _.findIndex($scope.availableMilestoneStatuses, function (status) { return status.index === $scope.selected.milestone.index; }); } if (currentStatusIndex !== -1) { if (direction === 'prev') { if ((ticketType === 'change' || ticketType === 'problem') && $scope.availableStatuses[currentStatusIndex].name === 'Completed') { prevStatusIndex = currentStatusIndex !== 0 ? parseInt(currentStatusIndex - 2) : 0; } else { prevStatusIndex = currentStatusIndex !== 0 ? parseInt(currentStatusIndex - 1) : 0; } if (ticketType === 'release') { switchToState = $scope.availableMilestoneStatuses[prevStatusIndex] || {}; } else { switchToState = $scope.availableStatuses[prevStatusIndex] || {}; } } else if (direction === 'next') { if (((ticketType === 'change' && ($scope.availableStatuses[currentStatusIndex].name === 'Implementation In Progress' || $scope.availableStatuses[currentStatusIndex].name === 'Planning In Progress')) || ticketType === 'problem') && $scope.availableStatuses[currentStatusIndex + 1] && $scope.availableStatuses[currentStatusIndex + 1].name === 'Pending') { nextStatusIndex = parseInt(currentStatusIndex + 2); } else { nextStatusIndex = parseInt(currentStatusIndex + 1); } if (ticketType === 'release') { switchToState = $scope.availableMilestoneStatuses[nextStatusIndex] || {}; } else { switchToState = $scope.availableStatuses[nextStatusIndex] || {}; } } } if (ticketType !== 'release') { $scope.changeStatus(switchToState, ticketType); } else { selected.status = {}; $scope.changeMileStone(switchToState, ticketType); } }; $scope.changeMileStone = function (status, ticketType, onStatusLoad) { selected.milestone = status; $scope.availableStatuses = {}; if (onStatusLoad) { selected.status = {}; } //$scope.ticket.milestone = status.name; var milestoneStatusValues = []; _.forEach(milestoneStatus, function (key) { if (key.milestone === status.name) { milestoneStatusValues.push(key); } }); $scope.availableStatuses = _.filter(milestoneMetadata.statuses, function (status) { return _.findIndex(milestoneStatusValues, { 'status': status.name }) !== -1; }); }; $scope.changeStatus = function (status, ticketType) { selected.status = status; selected.statusReason = {}; $scope.ticket.status.value = status.name; $scope.showDates = $scope.isFieldRequired('scheduledStartDate') || $scope.isFieldRequired('actualStartDate'); if ($scope.isFieldRequired('manager') && originalTicket.manager && !originalTicket.manager.loginId) { $scope.noChangeManagerLoginId = true; } else { $scope.noChangeManagerLoginId = false; } $scope.fixScheduledDates = ticketType === 'change' && selected.status.name === 'Scheduled For Review' && (($scope.ticket.scheduledStartDate && $scope.ticket.scheduledStartDate.valueOf() < $scope.ticket.earliestStartDate) || ($scope.ticket.scheduledEndDate && $scope.ticket.scheduledEndDate.valueOf() < $scope.ticket.earliestStartDate)); if (ticketType && ticketType === 'change' && $scope.ticket.status.value === 'Completed') { selected.statusReason = _.find(status.statusReasons, { name: 'Final Review Required' }); } $scope.editStatusForm.$dirty = true; }; $scope.$watch('ticket.scheduledStartDate', function () { if ($scope.ticket && $scope.ticket.type === 'change' && selected.status.name === 'Scheduled For Review' && $scope.ticket.scheduledStartDate) { checkIfValidDates(); } }); $scope.$watch('ticket.scheduledEndDate', function () { if ($scope.ticket && $scope.ticket.type === 'change' && selected.status.name === 'Scheduled For Review' && $scope.ticket.scheduledStartTime) { checkIfValidDates(); } }); $scope.$watch('ticket.scheduledStartTime', function () { if ($scope.ticket && $scope.ticket.type === 'change' && selected.status.name === 'Scheduled For Review' && $scope.ticket.scheduledEndDate) { checkIfValidDates(); } }); $scope.$watch('ticket.scheduledEndTime', function () { if ($scope.ticket && $scope.ticket.type === 'change' && selected.status.name === 'Scheduled For Review' && $scope.ticket.scheduledEndTime) { checkIfValidDates(); } }); $scope.changeStatusReason = function (statusReason) { selected.statusReason = statusReason; $scope.editStatusForm.$dirty = true; }; $scope.updateGroup = function (group) { selected.group = group; $scope.state.loadingSupportPersons = true; var role = ""; if ($scope.ticket.type === EntityVO.TYPE_CHANGE) { role = 'role=changemanager'; } else if ($scope.ticket.type === EntityVO.TYPE_PROBLEM) { role = 'role=problemcoordinator'; } $scope.editStatusForm.$dirty = true; return ticketService.getGroupMembers(group.id, role).then(function (response) { $scope.state.loadingSupportPersons = false; $scope.supportGroupPersons = response.results; $scope.updateManager(null); }); }; $scope.updateManager = function (manager) { selected.manager = manager; $scope.editStatusForm.$dirty = true; }; $scope.updateAssigneeGroup = function (group) { selected.assigneeGroup = group; $scope.state.loadingAssignees = true; $scope.editStatusForm.$dirty = true; return ticketService.getGroupMembers(group.id, "").then(function (response) { $scope.state.loadingAssignees = false; $scope.assignees = response.results; $scope.updateAssignee(null); }); }; $scope.updateAssignee = function (assignee) { selected.assignee = assignee; $scope.editStatusForm.$dirty = true; }; $scope.isFieldRequired = function (field) { if (field === 'statusReason') { if ($scope.ticket.type === EntityVO.TYPE_INCIDENT || $scope.ticket.type === EntityVO.TYPE_CHANGE || $scope.ticket.type === EntityVO.TYPE_RELEASE || $scope.ticket.type === EntityVO.TYPE_PROBLEM || $scope.ticket.type === EntityVO.TYPE_TASK || $scope.ticket.type === EntityVO.TYPE_KNOWNERROR) { return fieldValidationModel.isFieldRequired($scope.ticket.type, $scope.ticket.status.value, $scope.ticket.timing, field); } //for defect SW00512588 and SW00523488 else if ($scope.ticket.type === EntityVO.TYPE_WORKORDER || $scope.ticket.type === EntityVO.TYPE_ACTIVITY) { return false; } else { return true; } } //only show when moving from a status where it's not required to a status where it's required return fieldValidationModel.isFieldRequired($scope.ticket.type, $scope.ticket.status.value, $scope.ticket.timing, field) && !fieldValidationModel.isFieldRequired($scope.ticket.type, originalTicket.status.value, $scope.ticket.timing, field); }; $scope.close = function () { if ($scope.editStatusForm.$dirty) { 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(); } }; $scope.submit = function () { state.updatingStatus = true; var ids, statusData, updateFn; ids = _.map(editStatusParams.actionItems, function (item) { return { uuid: item.id, type: item.type }; }); statusData = { status: selected.status.name }; if (!_.isEmpty(selected.statusReason)) { statusData.statusReason = selected.statusReason.name; } if (!_.isEmpty(selected.milestone)) { statusData.milestone = selected.milestone.name; } if (selected.resolutionNote) { statusData.resNote = selected.resolutionNote; } var resCat = categoriesService.collectValues($scope.resCategories, $scope.ticket).resCategorizations; if (resCat && resCat.length) { statusData.resCategorizations = resCat; } var categories = categoriesService.collectValues($scope.categories, $scope.ticket).categorizations; if (categories && categories.length) { statusData.categorizations = categories; } if (selected.worknote) { statusData.worknote = selected.worknote; } if ($scope.ticket.type === EntityVO.TYPE_CHANGE) { if ($scope.showDates) { var scheduledStart = originalTicket.scheduledStartDate === null ? 0 : originalTicket.scheduledStartDate.valueOf(), scheduledEnd = originalTicket.scheduledEndDate === null ? 0 : originalTicket.scheduledEndDate.valueOf(), actualStart = originalTicket.actualStartDate === null ? 0 : originalTicket.actualStartDate.valueOf(), actualEnd = originalTicket.actualEndDate === null ? 0 : originalTicket.actualEndDate.valueOf(), targetDate = originalTicket.targetDate === null ? 0 : originalTicket.targetDate.valueOf(); if ($scope.ticket.scheduledStartDate !== null && scheduledStart !== $scope.ticket.scheduledStartDate.valueOf()) { statusData.scheduledStartDate = moment($scope.ticket.scheduledStartDate).valueOf(); } if ($scope.ticket.scheduledEndDate !== null && scheduledEnd !== $scope.ticket.scheduledEndDate.valueOf()) { statusData.scheduledEndDate = moment($scope.ticket.scheduledEndDate).valueOf(); } if ($scope.ticket.actualStartDate !== null && actualStart !== $scope.ticket.actualStartDate.valueOf()) { statusData.actualStartDate = moment($scope.ticket.actualStartDate).valueOf(); } if ($scope.ticket.actualEndDate !== null && actualEnd !== $scope.ticket.actualEndDate.valueOf()) { statusData.actualEndDate = moment($scope.ticket.actualEndDate).valueOf(); } if ($scope.ticket.targetDate !== null && targetDate !== $scope.ticket.targetDate.valueOf()) { statusData.targetDate = moment($scope.ticket.targetDate).valueOf(); } } if ($scope.isFieldRequired('managerGroup')) { if (selected.manager.loginId !== originalTicket.manager.loginId) { statusData.managerLoginId = selected.manager.loginId; statusData.managerFullName = selected.manager.fullName; } if (selected.group.name !== originalTicket.managerGroup.name) { statusData.managerGroup = selected.group.name; statusData.managerOrganization = selected.group.organization; statusData.managerCompany = selected.group.company && selected.group.company.name; } } } else if ($scope.ticket.type === EntityVO.TYPE_PROBLEM) { if (selected.targetDate) { statusData.targetDate = moment(selected.targetDate).valueOf(); } statusData.coordinatorLoginId = selected.manager ? selected.manager.loginId : undefined; statusData.coordinatorFullName = selected.manager ? selected.manager.fullName : undefined; statusData.coordinatorGroupId = selected.group ? selected.group.id : undefined; statusData.coordinatorGroup = selected.group ? selected.group.name : undefined; statusData.coordinatorOrganization = selected.group ? selected.group.organization : undefined; statusData.coordinatorCompany = (selected.group && selected.group.company) ? selected.group.company.name : undefined; statusData.group = selected.assigneeGroup ? selected.assigneeGroup.name : undefined; statusData.groupId = selected.assigneeGroup ? selected.assigneeGroup.id : undefined; statusData.organization = selected.assigneeGroup ? selected.assigneeGroup.organization : undefined; statusData.company = (selected.assigneeGroup && selected.assigneeGroup.company) ? selected.assigneeGroup.company.name : undefined; if (selected.assignee) { statusData.fullName = selected.assignee.fullName; statusData.loginId = selected.assignee.loginId; } } updateFn = editStatusParams.isDraft ? $q.when : ticketModel.updateStatus; return updateFn(ids, statusData, $scope.ticket.type).then(function (response) { if (!editStatusParams.isDraft && !_.isEmpty(response)) { statusData.updatedTickets = response; } if ($scope.ticket.type === EntityVO.TYPE_PROBLEM && statusData.status === 'Completed' && statusData.statusReason === 'Known Error') { if ($scope.ticket.hasInitiatesRelation) { showPKECreateConfirmationDialog().then(function (result) { if (result) { createRelatedDraft(EntityVO.TYPE_KNOWNERROR, 'draftKnownerror', RelationItemVO.TYPE_INITIATEDBY); } }); } else { createRelatedDraft(EntityVO.TYPE_KNOWNERROR, 'draftKnownerror', RelationItemVO.TYPE_INITIATEDBY); } } $modalInstance.close(statusData); }).catch(function (error) { $modalInstance.dismiss(error.data.error); }).finally(function () { state.updatingStatus = false; }); }; function createRelatedDraft(type, state, relationship) { ticketModel.cache.draft = { type: type }; ticketModel.cache.relatedDraft = { type: type, fromType: $scope.ticket.type, id: $scope.ticket.id, fromContext: $scope.ticket, relationship: relationship }; $state.go(state); } function showPKECreateConfirmationDialog() { var modal = systemAlertService.modal({ type: 'info', title: $filter('i18n')('common.notification.dirty.title'), text: $filter('i18n')('create.knownerror.draft.problem'), buttons: [ { text: $filter('i18n')('common.button.yes'), data: true }, { text: $filter('i18n')('common.button.no'), data: false } ] }); return modal.result; } function getTimestamp(date, time) { time = time ? moment(time) : moment(date); return moment(date).set('hours', time.get('hours')).set('minutes', time.get('minutes')).format('X') * 1000; } function checkIfValidDates() { var scheduledStartTime = getTimestamp($scope.ticket.scheduledStartDate, $scope.ticket.scheduledStartTime), scheduledEndTime = getTimestamp($scope.ticket.scheduledEndDate, $scope.ticket.scheduledEndTime); $scope.fixScheduledDates = $scope.ticket && $scope.ticket.type === 'change' && selected.status.name === 'Scheduled For Review' && (scheduledStartTime < $scope.ticket.earliestStartDate || scheduledEndTime < $scope.ticket.earliestStartDate || scheduledEndTime < scheduledStartTime); } function handleModalBackdropClick() { $scope.close(); } $scope.$on(events.MODAL_BACKDROP_CLICK, handleModalBackdropClick); var getEntryPromise; if (type === EntityVO.TYPE_KNOWLEDGE) { getEntryPromise = knowledgeArticleModel.getKnowledgeArticleById(id, false, true); } else if ($scope.isDraft) { getEntryPromise = $q.when(editStatusParams.actionItems[0]); } else { getEntryPromise = ticketModel.getTicket(id, type); } var availableStatusPromise; if (type === EntityVO.TYPE_CHANGE || type === EntityVO.TYPE_RELEASE || type === EntityVO.TYPE_PROBLEM || type === EntityVO.TYPE_KNOWNERROR) { availableStatusPromise = ticketModel.getAvailableStatuses(id, type); } else { availableStatusPromise = $q.when(1); } function getSupportGroupsFromAllCompany(role, customerCompany, locationCompany, isAssignee) { var organizationName = isAssignee ? ($scope.assigneeOrganization && $scope.assigneeOrganization.name) || ($scope.selected.assigneeGroup && $scope.selected.assigneeGroup.organization) || '' : ($scope.coordinatorOrganization && $scope.coordinatorOrganization.name) || ($scope.selected.group && $scope.selected.group.organization) || ''; return searchModel.getAssigneeSupportGroupsForCompanyAndOrg('', organizationName, role, null, customerCompany, locationCompany) .then(function (result) { result.supportGroups = _.unique(result.supportGroups, function (group) { return group.id; }); result.supportGroups = _.sortBy(result.supportGroups, function (group) { return group.name; }); if (role) { state.tooManySupportGroups = result.exceedsChunkSize; } else { state.tooManyAssigneeGroups = result.exceedsChunkSize; } return result.supportGroups; }); } $scope.getSupportGroupsForAllCompanyByName = function (name, group) { var customerCompany = null, locationCompany = null, organizationName = ''; customerCompany = $scope.ticket.customer ? $scope.ticket.customer.company.name : $scope.ticket.company ? $scope.ticket.company.name : ''; if (($scope.ticket.type === EntityVO.TYPE_TASK || $scope.ticket.type === EntityVO.TYPE_CHANGE) && $scope.ticket.id && $scope.ticket.company) { locationCompany = $scope.ticket.company.name; } else { locationCompany = $scope.ticket.location ? $scope.ticket.location.companyName : null; } if (group === 'supportGroup') { $scope.state.loadingSupportGroups = true; organizationName = ($scope.coordinatorOrganization && $scope.coordinatorOrganization.name) || ''; } else if (group === 'assigneeGroup') { $scope.state.loadingAssigneeGroups = true; organizationName = ($scope.assigneeOrganization && $scope.assigneeOrganization.name) || ''; } var groupsPromise = searchModel.getAssigneeSupportGroupsForCompanyAndOrgByName('', organizationName, name, null, customerCompany, locationCompany); groupsPromise.then(function (result) { $scope.state.loadingSupportGroups = false; $scope.state.loadingAssigneeGroups = false; if (group === 'supportGroup') { state.tooManySupportGroups = result.exceedsChunkSize; } else if (group === 'assigneeGroup') { state.tooManyAssigneeGroups = result.exceedsChunkSize; } return result.supportGroups; }); return groupsPromise; }; $q.all([metadataModel.getMetadataByType(type), getEntryPromise, availableStatusPromise]) .then(function (resultArray) { originalTicket = resultArray[1]; var i, metaData = resultArray[0], validStatuses = resultArray[2], emptyCount = { 'schedule': 0, 'actual': 0 }; //SW00522514 fixed for canclled case only for change and release if (originalTicket.isInApproval && (type === EntityVO.TYPE_CHANGE || type === EntityVO.TYPE_RELEASE)) { selected.status = (isSingle || type === EntityVO.TYPE_CHANGE) ? _.find(metaData.statuses, { name: 'Cancelled' }) : {}; selected.isDisabled = true; } else { selected.status = (isSingle || type === EntityVO.TYPE_CHANGE) ? _.find(metaData.statuses, { name: originalTicket.status.value }) : {}; } selected.milestone = (isSingle) ? _.find(metaData.milestones, { name: originalTicket.milestone }) : {}; // selected.status = (isSingle || type === EntityVO.TYPE_CHANGE) ? _.find(metaData.statuses, { name: originalTicket.status.value }) : {}; selected.statusReason = !_.isEmpty(selected.status) ? _.find(selected.status.statusReasons, { name: originalTicket.status.reason }) : {}; selected.statusReason = (!_.isEmpty(selected.statusReason) && !isSingle && type === EntityVO.TYPE_CHANGE) ? {} : selected.statusReason; // SW00541615 - if multiple tickets selected for status update, resolutionNote of first ticket will be // sent as update param for all selected tickets selected.resolutionNote = originalTicket.resolution && isSingle ? originalTicket.resolution : ''; if (!isSingle && type === EntityVO.TYPE_CHANGE) { $scope.showChangeUpdateWarning = true; $scope.isChangeScheduleDatesRequired = false; $scope.isChangeActualDatesRequired = false; for (i = 0; i < editStatusParams.actionItems.length; i++) { if (!$scope.isChangeScheduleDatesRequired && (editStatusParams.actionItems[i].scheduledEndDate === "" || editStatusParams.actionItems[i].scheduledStartDate === "")) { $scope.isChangeScheduleDatesRequired = true; } if (!$scope.isChangeActualDatesRequired && (editStatusParams.actionItems[i].actualEndDate === "" || editStatusParams.actionItems[i].actualStartDate === "")) { $scope.isChangeActualDatesRequired = true; } if ((editStatusParams.actionItems[i].scheduledEndDate === "" && editStatusParams.actionItems[i].scheduledStartDate === "")) { emptyCount.schedule++; } if ((editStatusParams.actionItems[i].actualEndDate === "" && editStatusParams.actionItems[i].actualStartDate === "")) { emptyCount.actual++; } } if (emptyCount.schedule === editStatusParams.actionItems.length || emptyCount.actual === editStatusParams.actionItems.length) { $scope.showChangeUpdateWarning = false; } } else { $scope.isChangeScheduleDatesRequired = true; $scope.isChangeActualDatesRequired = true; } if (type === EntityVO.TYPE_WORKORDER && editStatusParams.isDraft) { $scope.availableStatuses = _.reject(metaData.statuses, { isValidForCreate: false }); } else if (selected.status && selected.status.invalidStatusTransitions && selected.status.invalidStatusTransitions.length) { $scope.availableStatuses = _.filter(metaData.statuses, function (status) { return ~_.indexOf(selected.status.invalidStatusTransitions, status.name) ? false : true; }); } else if (selected.status && selected.status.validKnowledgeTransitions) { if (selected.status.validKnowledgeTransitions.length) { $scope.availableStatuses = _.filter(metaData.statuses, function (status) { return _.find(selected.status.validKnowledgeTransitions, { validStatus: status.name }); }); } else { $scope.availableStatuses = []; } } else if (type === EntityVO.TYPE_CHANGE || ((type === EntityVO.TYPE_PROBLEM || type === EntityVO.TYPE_KNOWNERROR) && !editStatusParams.isDraft)) { $scope.availableStatuses = _.filter(metaData.statuses, function (status) { return validStatuses.indexOf(status.name) !== -1; }); } else if (type === EntityVO.TYPE_RELEASE && !editStatusParams.isDraft) { milestoneStatus = validStatuses; milestoneMetadata = metaData; $scope.availableMilestoneStatuses = _.filter(metaData.milestones, function (milestone) { return _.findIndex(validStatuses, { 'milestone': milestone.name }) !== -1; }); $scope.changeMileStone(selected.milestone, 'release'); } else if (type === EntityVO.TYPE_PROBLEM && editStatusParams.isDraft) { $scope.availableStatuses = _.reject(metaData.statuses, { isValidForCreate: false }); } else { $scope.availableStatuses = metaData.statuses; } $scope.ticket = angular.copy(originalTicket); if (originalTicket.resCategorizations) { $scope.resCategories = angular.copy(categoriesService.populateCategories(originalTicket.resCategorizations, metaData)); } if (originalTicket.categorizations) { //omit the resolution product categories from $scope.categories list as these are already available under $scope.resCategories //ResCategories were appended to the categories list in ticket controller to show them on the ticket details page //resolution categories are also already added in ticket controller var categories = _.filter(originalTicket.categorizations, function (category) { return category.name !== 'resolutionProduct' && category.name !== 'resolution'; }); $scope.categories = angular.copy(categoriesService.populateCategories(categories, metaData)); } state.processing = false; if (type === EntityVO.TYPE_CHANGE) { if (isSingle) { selected.manager = originalTicket.manager; selected.group = originalTicket.managerGroup; ticketService.getGroupMembers(selected.group.id, "role=changemanager").then(function (response) { $scope.supportGroupPersons = response.results; }); } else { $scope.ticket.scheduledStartDate = null; $scope.ticket.scheduledEndDate = null; $scope.ticket.actualStartDate = null; $scope.ticket.actualEndDate = null; } searchService.getSupportGroupsList({ companyName: $scope.ticket.company.name, organization: ($scope.selected.group && $scope.selected.group.organization) || '', role: 'changemanager' }).then(function (response) { $scope.coordinatorSupportGroups = response.supportGroups; }); if ($scope.isFieldRequired('manager') && originalTicket.manager && !originalTicket.manager) { $scope.noChangeManagerLoginId = true; } else { $scope.noChangeManagerLoginId = false; } } else if (type === EntityVO.TYPE_PROBLEM) { if (isSingle) { selected.manager = _.isEmpty(originalTicket.coordinator) || !originalTicket.coordinator.id ? null : originalTicket.coordinator; selected.targetDate = _.isUndefined(originalTicket.targetDate) ? null : originalTicket.targetDate; selected.assignee = _.isEmpty(originalTicket.assignee) || !originalTicket.assignee.id ? null : originalTicket.assignee; if (!_.isEmpty(originalTicket.coordinatorGroup) && originalTicket.coordinatorGroup.name) { selected.group = originalTicket.coordinatorGroup; ticketService.getGroupMembers(selected.group.id, "role=problemcoordinator").then(function (response) { $scope.supportGroupPersons = response.results; }); } else { selected.group = null; } if (!_.isEmpty(originalTicket.supportGroup) && originalTicket.supportGroup.name) { selected.assigneeGroup = originalTicket.supportGroup; ticketService.getGroupMembers(selected.assigneeGroup.id, "").then(function (response) { $scope.assignees = response.results; }); } else { selected.assigneeGroup = null; } } else { selected.targetDate = null; selected.manager = null; selected.assignee = null; selected.group = null; selected.assigneeGroup = null; } var customerCompany = $scope.ticket.customer ? $scope.ticket.customer.company.name : $scope.ticket.company ? $scope.ticket.company.name : '', locationCompany = $scope.ticket.location ? $scope.ticket.location.companyName : null; getCompanies('problemcoordinator').then(function () { $scope.coordinatorCompany = originalTicket.coordinatorGroup.company; $scope.assigneeCompany = originalTicket.assignee.company; getSupportOrganizationsForCompany($scope.coordinatorCompany.name, 'problemcoordinator', null, locationCompany, null, null) .then(function (response) { $scope.coordinatorOrganizations = response; $scope.coordinatorOrganization = { name: originalTicket.coordinatorGroup.organization }; }); getSupportOrganizationsForCompany($scope.assigneeCompany.name, null, null, locationCompany, null, null) .then(function (response) { $scope.assigneeOrganizations = response; $scope.assigneeOrganization = { name: originalTicket.assignee.organization }; }); }); getSupportGroupsFromAllCompany('problemcoordinator', customerCompany, locationCompany, false).then(function (response) { $scope.coordinatorSupportGroups = response; }); getSupportGroupsFromAllCompany(null, customerCompany, locationCompany, true).then(function (response) { $scope.assigneeGroups = response; }); } }); function getCompanies(role) { var options, customerCompany = $scope.ticket.customer ? $scope.ticket.customer.company.name : $scope.ticket.company ? $scope.ticket.company.name : ''; if (!customerCompany) { customerCompany = $scope.ticket.selectedCompany; } options = { customerCompany: customerCompany }; if ($scope.ticket.locationCompany && $scope.ticket.locationCompany.name) { options.locationCompany = $scope.ticket.locationCompany.name; } else if (($scope.ticket.type === EntityVO.TYPE_TASK || $scope.ticket.type === EntityVO.TYPE_CHANGE) && $scope.ticket.id && $scope.ticket.company) { options.locationCompany = $scope.ticket.company.name; } if (role) { options.role = role; } return searchModel.getAssigneeSupportCompanies(options).then(function (companies) { state.tooManySupportCompanies = companies.exceedsChunkSize; state.tooManyAssigneeCompanies = companies.exceedsChunkSize; $scope.companies = _.cloneDeep(companies.objects); }); } $scope.getCompaniesByName = function (name) { return searchModel.getCompaniesByText(name).then(function (response) { return response.companies; }); }; $scope.getSupportOrganizationsByTextAndCompany = function (searchText, company) { var companyName; if (company === 'coordinatorCompany') { companyName = ($scope.coordinatorCompany && $scope.coordinatorCompany.name) || ''; } else if (company === 'assigneeCompany') { companyName = ($scope.assigneeCompany && $scope.assigneeCompany.name) || ''; } return searchModel.getSupportOrganizationsByTextAndCompany(searchText, companyName).then(function (response) { if (company === 'coordinatorCompany') { state.tooManySupportOrganizations = response.exceedsChunkSize; } else if (company === 'assigneeCompany') { state.tooManyAssigneeOrganizations = response.exceedsChunkSize; } return response.organizations; }); }; $scope.selectCoordinatorCompany = function (company) { $scope.coordinatorCompany = company; var locationCompany = $scope.ticket.location ? $scope.ticket.location.companyName : null; getSupportOrganizationsForCompany(company.name, 'problemcoordinator', null, locationCompany, null, null) .then(function (response) { $scope.coordinatorOrganizations = response; }); }; $scope.selectAssigneeCompany = function (company) { var locationCompany = $scope.ticket.location ? $scope.ticket.location.companyName : null; $scope.assigneeCompany = company; getSupportOrganizationsForCompany(company.name, null, null, locationCompany, null, null) .then(function (response) { $scope.assigneeOrganizations = response; }); }; $scope.selectCoordinatorOrganization = function (organization) { var locationCompany = $scope.ticket.location ? $scope.ticket.location.companyName : null; $scope.coordinatorOrganization = organization; getSupportGroupsForCompanyAndOrg($scope.coordinatorCompany, organization, '', null, locationCompany, null, null) .then(function (response) { $scope.coordinatorSupportGroups = response; }); }; $scope.selectAssigneeOrganization = function (organization) { var locationCompany = $scope.ticket.location ? $scope.ticket.location.companyName : null; $scope.assigneeOrganization = organization; getSupportGroupsForCompanyAndOrg($scope.assigneeCompany, organization, '', null, locationCompany, null, null) .then(function (response) { $scope.assigneeGroups = response; }); }; function getSupportOrganizationsForCompany(companyName, role, customerCompany, locationCompany, ownerCompany, primaryCompany) { return searchModel.getSupportOrganizationsByCompany(companyName, null, role, customerCompany, locationCompany, ownerCompany, primaryCompany) .then(function (response) { if (role) { state.tooManySupportOrganizations = response.exceedsChunkSize; } else { state.tooManyAssigneeOrganizations = response.exceedsChunkSize; } return response.organizations; }); } 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 (role) { state.tooManySupportGroups = response.exceedsChunkSize; } else { state.tooManyAssigneeGroups = response.exceedsChunkSize; } return response.supportGroups; }); } }]); })();