public IActionResult PersonalData(EmployeePersonalData personalData, FormCollection collectedValues) { var employee = new Employee(); var restaurant = _session.GetInt32("restaurantsessionid"); var allEmployees = _db.EmployeePersonalDatas; var _employee = _db.Employees.Find(restaurant); if (_employee != null) { personalData.Title = typeof(NameTitle).GetEnumName(int.Parse(personalData.Title)); personalData.DOB = Convert.ToString(Convert.ToDateTime(collectedValues["DOB"])); _employee.EmployeePersonalDatas = new List <EmployeePersonalData> { personalData }; //_session.("Employee") _session.SetObject("Employee", _employee); } else { var employeePersonalData = new Employee(); personalData.DOB = Convert.ToString(Convert.ToDateTime(collectedValues["DOB"])); personalData.Title = typeof(NameTitle).GetEnumName(int.Parse(personalData.Title)); _session.SetObject("Employee", employeePersonalData); } if (allEmployees.Any(p => p.Email == personalData.Email)) { TempData["personal"] = "The email already exists!"; TempData["notificationType"] = NotificationType.Error.ToString(); //return next view ViewBag.State = new SelectList(_db.States, "StateId", "Name", personalData.StateId); return(View(personalData)); } var returnUrl = Convert.ToBoolean(collectedValues["returnUrl"]); //if it is edit from review page return to the review page if (returnUrl) { return(View("ReviewEmployeeData")); } //return next view return(RedirectToAction("EducationalQualification")); }
public async Task <IActionResult> AddEmployee(PreEmployee preEmployee) { var userId = _session.GetInt32("loggedinusersessionid"); var restaurantid = _session.GetInt32("restaurantsessionid"); var restaurant = _db.Restaurants.Find(restaurantid); try { if (_db.EmployeePersonalDatas.Any(n => n.Email == preEmployee.Email) == false && _db.AppUsers.Any(n => n.Email == preEmployee.Email) == false) { var _employee = new Employee { RestaurantId = Convert.ToInt32(restaurantid), CreatedBy = userId, LastModifiedBy = Convert.ToInt32(userId), DateCreated = DateTime.Now, DateLastModified = DateTime.Now }; _db.Employees.Add(_employee); await _db.SaveChangesAsync(); if (_employee.EmployeeId > 0) { //Popluate the personal data object var _employeePersonalData = new EmployeePersonalData { RestaurantId = Convert.ToInt32(restaurantid), CreatedBy = userId, LastModifiedBy = Convert.ToInt32(userId), DateCreated = DateTime.Now, DateLastModified = DateTime.Now, FirstName = preEmployee.Firstname, LastName = preEmployee.Lastname, Email = preEmployee.Email, PrimaryAddress = preEmployee.PrimaryAddress, SecondaryAddress = "N/A", State = "N/A", MiddleName = "N/A", LGA = "N/A", HomePhone = preEmployee.HomePhoneNumber, WorkPhone = "N/A", DOB = DateTime.Now, Title = 0.ToString(), MaritalStatus = 0.ToString(), Gender = 0.ToString(), POB = "N/A", EmployeeId = _employee.EmployeeId }; _db.EmployeePersonalDatas.Add(_employeePersonalData); await _db.SaveChangesAsync(); var password = new Md5Encryption().RandomString(7); var _appUser = new AppUser { EmployeeId = _employee.EmployeeId, Email = _employeePersonalData.Email, Name = _employeePersonalData.DisplayName, RestaurantId = Convert.ToInt32(restaurantid), CreatedBy = userId, LastModifiedBy = Convert.ToInt32(userId), DateCreated = DateTime.Now, DateLastModified = DateTime.Now, Password = new Hashing().HashPassword(password), ConfirmPassword = new Hashing().HashPassword(password), Status = UserStatus.Inactive.ToString() }; _db.AppUsers.Add(_appUser); await _db.SaveChangesAsync(); if (_appUser.AppUserId > 0) { //define acceskeys and save transactions var accesskey = new AppUserAccessKey { PasswordAccessCode = new Md5Encryption().RandomString(15), AccountActivationAccessCode = new Md5Encryption().RandomString(20), CreatedBy = _appUser.AppUserId, LastModifiedBy = _appUser.AppUserId, DateCreated = DateTime.Now, DateLastModified = DateTime.Now, ExpiryDate = DateTime.Now.AddDays(1), AppUserId = _appUser.AppUserId }; _db.AppUserAccessKeys.Add(accesskey); await _db.SaveChangesAsync(); //new Mailer() } TempData["display"] = "You have successfully added a new employee!"; TempData["notificationType"] = NotificationType.Success.ToString(); return(View()); } TempData["display"] = "There is an error performing this action. Try again!"; TempData["notificationType"] = NotificationType.Error.ToString(); return(View(preEmployee)); } TempData["display"] = "The employee already exist, try a different email!"; TempData["notificationtype"] = NotificationType.Error.ToString(); return(View(preEmployee)); } catch (Exception ex) { TempData["display"] = ex.Message; TempData["notificationtype"] = NotificationType.Error.ToString(); return(View()); } }