public ActionResult Edit(PersonnelProfileViewModel personnelViewModel) { if (ModelState.IsValid) { personnelViewModel.Personnel = HRBusinessService.UpdatePersonnel(UserOrganisationId, 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 }; if (personnelApprovalModel.PersonnelApprovalModelId == 0) { HRBusinessService.CreatePersonnelApprovalModel(UserOrganisationId, personnelApprovalModel); } else if (personnelApprovalModel.ApprovalModelId == 0) { HRBusinessService.DeletePersonnelApprovalModel(UserOrganisationId, personnelApprovalModel.PersonnelApprovalModelId); } else { HRBusinessService.UpdatePersonnelApprovalModel(UserOrganisationId, personnelApprovalModel); } } } var editUser = UserManager.FindByPersonnelId(personnelViewModel.Personnel.PersonnelId); editUser.Email = personnelViewModel.Personnel.Email; var result = UserManager.Update(editUser); if (result.Succeeded) { if (User.IsInRole("User")) { return(RedirectToAction("Profile", "Personnel", new { id = personnelViewModel.Personnel.PersonnelId })); } else { return(RedirectToAction("Index")); } } foreach (var error in result.Errors) { ModelState.AddModelError("", error); } } var approvalEntityTypeAssignments = HRBusinessService.ApprovalEntityTypeAssignments(UserOrganisationId, personnelViewModel.Personnel.PersonnelId); var approvalModels = HRBusinessService.RetrieveApprovalModels(UserOrganisationId); var viewModel = new PersonnelProfileViewModel { Countries = new SelectList(HRBusinessService.RetrieveCountries(UserOrganisationId, null, null).Items, "CountryId", "Name"), Personnel = personnelViewModel.Personnel, ApprovalEntityTypeAssignments = approvalEntityTypeAssignments, ApprovalModels = approvalModels }; return(View(viewModel)); }
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)); } }