"use strict"; /** * Created by geetha arvind on 12/22/2015. */ (function () { 'use strict'; angular.module('personModule') .controller('createPersonController', ["$scope", "$state", "$q", "$modalInstance", "i18nService", "systemAlertService", "userModel", "searchModel", "personModel", "metadataModel", "screenConfigurationModel", "personCompany", function ($scope, $state, $q, $modalInstance, i18nService, systemAlertService, userModel, searchModel, personModel, metadataModel, screenConfigurationModel, personCompany) { 'ngInject'; var person = { type: EntityVO.TYPE_PERSON }; function init() { $scope.state = { dataIsLoading: true, tooManyCompanies: false }; $scope.selections = {}; $scope.person = person; initAlertForDirtyForm(); var userPromise = searchModel.getOperatingCompanies(null, -1).then(function (response) { $scope.selections.companies = _.cloneDeep(response.companies); $scope.state.tooManyCompanies = response.exceedsChunkSize; }), personPromise = metadataModel.getPersonMetadata(EntityVO.TYPE_PERSON).then(function (personData) { angular.extend(person, personData); $scope.person.company = userModel.userFullData.company; $scope.person.isVIP = _.find(personData.vip, { name: 'No' }); $scope.person.clientSensitivity = _.find(personData.clientSensitivities, { name: 'Standard' }); $scope.person.clientType = _.find(personData.clientTypes, { name: 'Customer' }); $scope.person.contactType = _.find(personData.contactTypes, { name: 'Customer' }); }); screenConfigurationModel .loadScreenConfigurationAndCustomFieldLabels(ScreenConfigurationVO.prototype.PERSON_DETAILS_SCREEN, EntityVO.TYPE_PERSON); $q.all([personPromise, userPromise]).then(function () { if (personCompany) { $scope.updateCompany(personCompany); } $scope.state.dataIsLoading = false; }); } $scope.updateCompany = function (newCompany) { $scope.person.company = { name: newCompany.name, id: newCompany.id }; $scope.person.organization = ''; $scope.person.department = ''; $scope.person.site = ''; }; $scope.getCompaniesByName = function (name) { return searchModel.getCompaniesByText(name).then(function (response) { return response.companies; }); }; $scope.getOrganizationsAndUpdate = function (company) { $scope.organizationsLoading = true; personModel.getOrganizationList(company.name).then(function (data) { $scope.organizations = data; }).finally(function () { $scope.organizationsLoading = false; }); }; $scope.getDepartmentsAndUpdate = function (organization, company) { $scope.departmentsLoading = true; personModel.getDepartmentList(organization, company.name).then(function (data) { $scope.departments = data; }).finally(function () { $scope.departmentsLoading = false; }); }; $scope.getSitesAndUpdate = function (company) { $scope.sitesLoading = true; personModel.getSiteList(company.name).then(function (data) { $scope.sites = data; }).finally(function () { $scope.sitesLoading = false; }); }; $scope.createPerson = function () { $scope.state.dataIsLoading = true; personModel.createPerson($scope.person).then(function (result) { $modalInstance.close(result); }).catch(function (error) { if (error && error.data) { systemAlertService.error({ text: error.data.error, clear: false }); } }).finally(function () { $scope.state.dataIsLoading = false; }); }; $scope.cancel = function () { $modalInstance.dismiss(); }; var isFormDirty = function () { return $scope.createCustomerForm.$dirty || !_.isEmpty(person.firstName) || !_.isEmpty(person.lastName); }; function initAlertForDirtyForm() { $scope.$on('$stateChangeStart', function (event, toState, toParams) { if (isFormDirty()) { 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)) { person = {}; $scope.createCustomerForm.$setPristine(); $modalInstance.dismiss(); $state.transitionTo(data.stateName, data.stateParams); } }); } else { $modalInstance.dismiss(); } }); } init(); }]).value('personCompany', null); }());