public ActionResult AddUser(AddUser newUser) { using (mmpEntities mP = new mmpEntities()) { ViewBag.roles = mP.roles.ToList <role>(); ViewBag.regions = mP.regions.ToList <region>(); ViewBag.supervisors = mP.users.Where(a => a.role_id == mP.roles.FirstOrDefault(x => x.role_name == "supervisor").role_id).ToList <user>(); ViewBag.departments = mP.category_type_details.Where(x => x.category_id == (mP.categories.FirstOrDefault(z => z.category_name == "departments").category_id)).ToList <category_type_details>(); ViewBag.projects = mP.category_type_details.Where(x => x.category_id == (mP.categories.FirstOrDefault(z => z.category_name == "projects").category_id)).ToList <category_type_details>(); ViewBag.supervisorsl = mP.users.Where(a => a.role_id == mP.roles.FirstOrDefault(x => x.role_name == "supervisor").role_id&& a.user_status == "active").Select(p => new SelectListItem() { Text = p.employee_id + " " + p.user_name, Value = p.user_id.ToString() }).ToList(); #region Employee ID Already Exists var isEmployeeIDExists = isEmployeeIDExist(newUser.employee_id); if (isEmployeeIDExists) { ModelState.AddModelError("EmployeeExist", "Employee already exists"); return(View(newUser)); } #endregion /*#region User Already Exists * var isUserExists = isUserExist(newUser.user_name); * if (isUserExists) * { * ModelState.AddModelError("UserExist", "User already exists"); * return View(newUser); * } #endregion*/ #region Email Already exists var isEmailExists = IsEmailExist(newUser.user_email); if (isEmailExists) { ModelState.AddModelError("EmailExist", "Email already exists"); newUser.user_name = "Enter New Name"; return(View(newUser)); } #endregion if (ModelState.IsValid) { ModelState.AddModelError("UserExist", "User already exists"); #region Password Hashing newUser.user_password = Crypto.Hash(newUser.user_password); newUser.confirmPassword = Crypto.Hash(newUser.confirmPassword); #endregion user user = new user() { role_id = newUser.role_id, employee_id = newUser.employee_id, user_name = newUser.user_name, user_email = newUser.user_email, designation = newUser.designation, user_password = newUser.user_password, created_at = DateTime.Now, supervisor = newUser.supervisor, region_id = newUser.region_id, user_primary_department = newUser.user_primary_department, user_primary_project = newUser.user_primary_project, user_status = "active", join_date = newUser.joining_date }; mP.users.Add(user); //var timesheet = mP.timesheet_mr.Where(x => x.tsmr_valid_till > DateTime.Now && x.tsmr_start_date < DateTime.Now).FirstOrDefault<timesheet_mr>(); var timesheets = mP.timesheet_mr.Where(x => x.tsmr_valid_till > user.join_date).ToList <timesheet_mr>(); foreach (timesheet_mr timesheet in timesheets) { string body; if (timesheet != null) { timesheet ts = new timesheet() { timesheet_user = user.user_id, time_my = DateTime.Now, timesheet_status = "saved", timesheet_caller = timesheet.tsmr_id, tsmr_extension = timesheet.tsmr_valid_till > DateTime.Now ? timesheet.tsmr_valid_till : DateTime.Now.AddDays(-1) }; mP.timesheets.Add(ts); foreach (DateTime day in EachDay(timesheet.tsmr_start_date, timesheet.tsmr_valid_till)) { presence ps = new presence() { p_date = day, total_hours = 0, leave_status = "", user_id = user.user_id }; mP.presences.Add(ps); } body = "<br></br>" + user.user_name + ", New TimeSheet is assigned to you. TimeSheet is valid Till " + timesheet.tsmr_valid_till + ". Visit the following website to access timeSheet<br></br>" + "<a href='http://magcom-001-site3.etempurl.com'>http://magcom-001-site3.etempurl.com</a>"; Task.Run(() => EmailAlert.SendEmail(user.user_email, body)); } } #region Save to Database mP.SaveChanges(); #endregion return(Json(new { success = true, message = "Saved Successfully" })); } else { return(Json(new { success = true, message = "Invalid Request" })); } } }