Exemplo n.º 1
0
        public ActionResult Create(PersonnelProfileViewModel personnelViewModel)
        {
            try
            {
                var organisationId = UserOrganisationId;
                // check if user with this email already exists for the current organisation
                var userExists = UserManager.FindByEmail(personnelViewModel.Personnel.Email);
                if (userExists != null)
                {
                    ModelState.AddModelError("", string.Format("An account already exists for the email address {0}", personnelViewModel.Personnel.Email));
                }

                var modelError = ModelState.Values.Where(e => e.Errors.Count > 0);
                if (ModelState.IsValid)
                {
                    personnelViewModel.Personnel = HRBusinessService.CreatePersonnel(organisationId, personnelViewModel.Personnel, personnelViewModel.EmploymentViewModel.Employment, personnelViewModel.EmploymentViewModel.WorkingPatternDays.ToList(), personnelViewModel.EmploymentViewModel.SelectedDepartmentIds, personnelViewModel.EmploymentViewModel.SelectedTeamIds);
                    var result = CreateUserAndRole(personnelViewModel.Personnel);
                    if (personnelViewModel.ApprovalEntityTypeAssignments != null)
                    {
                        foreach (var approvalEntityTypeAssignment in personnelViewModel.ApprovalEntityTypeAssignments)
                        {
                            PersonnelApprovalModel personnelApprovalModel = new PersonnelApprovalModel
                            {
                                PersonnelId              = personnelViewModel.Personnel.PersonnelId,
                                ApprovalEntityTypeId     = approvalEntityTypeAssignment.ApprovalEntityId,
                                ApprovalModelId          = approvalEntityTypeAssignment.ApprovalModelId,
                                PersonnelApprovalModelId = approvalEntityTypeAssignment.PersonnelApprovalModelId
                            };
                            HRBusinessService.CreatePersonnelApprovalModel(UserOrganisationId, personnelApprovalModel);
                        }
                    }
                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }

                    //delete the orphaned personnel & employment records
                    HRBusinessService.DeletePersonnel(organisationId, personnelViewModel.Personnel.PersonnelId);
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
                var jobTitleJobgrades = HRBusinessService.RetrieveJobTitleJobGrade(organisationId);
                var defaultSelectedJobTitleJobgrades = jobTitleJobgrades.FirstOrDefault();
                var countries                     = HRBusinessService.RetrieveCountries(organisationId, null, null).Items;
                var reportsTo                     = HRBusinessService.RetrieveReportsToPersonnel(organisationId, 0);
                var absencePolices                = HRBusinessService.RetrieveAbsencePolices(organisationId);
                var publicHolidayPolices          = HRBusinessService.RetrievePublicHolidayPolices(organisationId);
                var companyBuilding               = HRBusinessService.RetrieveEmploymentCompanyBuilding(organisationId);
                var employmentTypes               = HRBusinessService.RetrieveEmploymentTypes(organisationId);
                var approvalEntityTypeAssignments = HRBusinessService.ApprovalEntityTypeAssignments(UserOrganisationId, 0);
                var approvalModels                = HRBusinessService.RetrieveApprovalModels(UserOrganisationId);
                var viewModel                     = new PersonnelProfileViewModel
                {
                    Countries           = new SelectList(countries, "CountryId", "Name"),
                    Personnel           = personnelViewModel.Personnel,
                    EmploymentViewModel = new EmploymentViewModel
                    {
                        ReportsTo                     = new SelectList(reportsTo, "PersonnelId", "Fullname", personnelViewModel.EmploymentViewModel.Employment.ReportsToPersonnelId),
                        EmploymentType                = new SelectList(employmentTypes, "EmploymentTypeId", "Name", personnelViewModel.EmploymentViewModel.Employment.EmploymentTypeId),
                        AbsencePolicy                 = new SelectList(absencePolices, "AbsencePolicyId", "Name"),
                        PublicHolidayPolicy           = new SelectList(publicHolidayPolices, "PublicHolidayPolicyId", "Name"),
                        JobTitleJobGrades             = jobTitleJobgrades,
                        Employment                    = personnelViewModel.EmploymentViewModel.Employment,
                        WorkingPatternDays            = personnelViewModel.EmploymentViewModel.WorkingPatternDays,
                        EmploymentCompanyBuildinglist = companyBuilding,
                        SelectedDepartmentIds         = personnelViewModel.EmploymentViewModel.SelectedDepartmentIds,
                        SelectedTeamIds               = personnelViewModel.EmploymentViewModel.SelectedTeamIds,
                        SelectedBuildingId            = personnelViewModel.EmploymentViewModel.Employment.BuildingId.ToString(),
                        SelectedCompanyId             = personnelViewModel.EmploymentViewModel.Employment.CompanyId.ToString(),
                        SelectedJobTitleId            = defaultSelectedJobTitleJobgrades.JobTitle.JobTitleId.ToString(),
                        SelectedJobGradeId            = defaultSelectedJobTitleJobgrades.JobGrade.JobGradeId.ToString()
                    },
                    ApprovalEntityTypeAssignments = approvalEntityTypeAssignments,
                    ApprovalModels = approvalModels
                };

                return(View(viewModel));
            }
            catch (Exception ex)
            {
                return(this.JsonNet(ex));
            }
        }