public async Task <ActionResult> CancelRequest(int id) { try { var leaveRequest = await _leaveRequestRepo.FindById(id); var allocation = await _allocationsRepo.GetLeaveAllocationsByEmployeeAndType(leaveRequest.RequestingEmployeeId, leaveRequest.LeaveTypeId); var user = _userManager.GetUserAsync(User).Result; var userId = user.Id; int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate).TotalDays; allocation.NumberOfDays += daysRequested; leaveRequest.Cancelled = true; leaveRequest.CancellationStaffId = userId; await _leaveRequestRepo.Update(leaveRequest); await _allocationsRepo.Update(allocation); return(RedirectToAction("MyLeave")); } catch (Exception) { return(RedirectToAction("MyLeave")); } }
public ActionResult ApproveRequest(int id) { LeaveRequest model = null; try { model = _requestRepo.FindById(id); model.Approved = true; model.ApprovedById = _userManager.GetUserId(User); model.DateActioned = DateTime.Now; var allocation = _allocationRepo.GetLeaveAllocationsByEmployeeAndType(model.RequestingEmployee.Id, model.LeaveTypeId); allocation.NumberOfDays -= (int)(model.EndDate - model.StartDate).TotalDays; var isSuccess = _requestRepo.Update(model); if (!isSuccess) { ModelState.AddModelError("", "Error req"); return(View(model)); } isSuccess = _allocationRepo.Update(allocation); if (!isSuccess) { ModelState.AddModelError("", "Error alloc"); return(View(model)); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { ModelState.AddModelError("", "Error"); return(View(model)); } }
public async Task <ActionResult> ApproveRequest(int id) { try { var user = await _userManager.GetUserAsync(User); var leaveRequest = await _leaveRequestRepo.FindById(id); var employeeid = leaveRequest.RequestingEmployeeId; var leaveTypeID = leaveRequest.LeaveTypeId; var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employeeid, leaveTypeID); int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate).TotalDays; allocation.NumberOfDays -= daysRequested; leaveRequest.Approved = true; leaveRequest.ApprovedById = user.Id; leaveRequest.DateActioned = DateTime.Now; await _leaveRequestRepo.Update(leaveRequest); await _leaveAllocRepo.Update(allocation); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { return(RedirectToAction(nameof(Index))); } }
public ActionResult ApproveRequest(int id) { try { var user = _userManager.GetUserAsync(User).Result; var leaveRequest = _leaveRequestRepository.FindById(id); var employeeid = leaveRequest.RequestingEmployeeId; var leaveTypeId = leaveRequest.LeaveTypeId; var allocation = _leaveAllocationRepository.GetLeaveAllocationsByEmployeeAndType(employeeid, leaveTypeId); int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate).TotalDays; allocation.NumberofDays = allocation.NumberofDays - daysRequested; leaveRequest.Approved = true; leaveRequest.ApprovedById = user.Id; leaveRequest.DateActioned = DateTime.Now; _leaveRequestRepository.Update(leaveRequest); _leaveAllocationRepository.Update(allocation); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { return(RedirectToAction(nameof(Index))); } }
public async Task <ActionResult> ApproveRequest(int id) { try { var user = await _userManager.GetUserAsync(User); var leaverequest = await _repoleaverequest.FindById(id); var allocation = await _repoleaveallocation.GetLeaveAllocationsByEmployeeAndType(leaverequest.RequestingEmployeeId, leaverequest.LeaveTypeId); int daysrequested = (int)(leaverequest.EndDate - leaverequest.StartDate).Days; allocation.NumberOfDays = allocation.NumberOfDays - daysrequested; leaverequest.Approved = true; leaverequest.ApprovedById = user.Id; leaverequest.DateActioned = DateTime.Now; var isSuccess = await _repoleaverequest.Update(leaverequest); if (!isSuccess) { return(RedirectToAction(nameof(Index), "Home")); } await _repoleaveallocation.Update(allocation); return(RedirectToAction(nameof(Details), new { id = id })); } catch { return(RedirectToAction(nameof(Index), "Home")); } }
public async Task <ActionResult> ApproveRequest(int id) { try { var user = await _userManager.GetUserAsync(User); var leaveRequest = await _leaveRequestRepo.FindById(id); var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(leaveRequest.RequestingEmployeeId, leaveRequest.LeaveTypeId); leaveRequest.Approved = true; leaveRequest.ApprovedById = user.Id; leaveRequest.DateActioned = DateTime.Now; var success = await _leaveRequestRepo.Update(leaveRequest); if (!success) { return(RedirectToAction(nameof(Index))); } int daysRequested = (leaveRequest.EndDate.Date - leaveRequest.StartDate.Date).Days; // doesn't account for weekends allocation.NumberOfDays -= daysRequested; await _leaveAllocRepo.Update(allocation); return(RedirectToAction(nameof(Index))); } catch (Exception) { return(RedirectToAction(nameof(Index))); } }
public async Task <ActionResult> ApproveRequest(int id) { try { var leaveRequestObj = await _leaveRequestRepo.FindById(id); leaveRequestObj.Approved = true; leaveRequestObj.ApprovedById = _userManager.GetUserId(User); leaveRequestObj.DateActioned = DateTime.Now; var leaveAllocationObj = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(leaveRequestObj.RequestingEmployeeId, leaveRequestObj.LeaveTypeId); int daysRequested = (int)(leaveRequestObj.EndDate - leaveRequestObj.StartDate).TotalDays; leaveAllocationObj.NumberOfDays -= daysRequested; await _leaveRequestRepo.Update(leaveRequestObj); await _leaveAllocationRepo.Update(leaveAllocationObj); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { return(RedirectToAction(nameof(Index))); } }
public ActionResult ApproveRequest(int id) { try { var user = _userManager.GetUserAsync(User).Result; var leaveRequest = _leaveRequestRepo.FindById(id); leaveRequest.Approved = true; var allocation = _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(leaveRequest.RequestingEmployeeId, leaveRequest.LeaveTypeId); int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate).TotalDays; allocation.NumberOfDays -= daysRequested; leaveRequest.ApprovedById = user.Id; leaveRequest.DateActioned = DateTime.Now; _leaveRequestRepo.Update(leaveRequest); _leaveAllocationRepo.Update(allocation); // ModelState.AddModelError("", "You do not have sufficient days for this request."); return(RedirectToAction(nameof(Index), "LeaveRequest")); } catch { ModelState.AddModelError("", "Something went wrong."); return(RedirectToAction(nameof(Index), "LeaveRequest")); } }
public async Task <ActionResult> ApproveRequest(int id) { try { var user = await _userManager.GetUserAsync(User); var leaveRequest = await _leaveRequestRepo.FindById(id); var employeeId = leaveRequest.RequestingEmployeeId; var currentUser = _userManager.GetUserAsync(User).Result; if (employeeId == currentUser.Id) { return(NotFound("Bạn không thể phê duyệt yêu cầu nghỉ phép của chính mình.")); } var LichSuChamCongList = await nhatKylamViecRepository.FindByMaNhanVien(employeeId); foreach (var item in LichSuChamCongList) { if (leaveRequest.StartDate.CompareTo(item.ThoiGianBatDau.Date) <= 0 && leaveRequest.EndDate.CompareTo(item.ThoiGianBatDau.Date) >= 0 ) { string errorMessage = "Khoảng thời gian của nghỉ phép được phê duyệt trùng với lịch biểu chấm công" + "\n Khoảng thời gian của nghỉ phép: " + leaveRequest.StartDate + " => " + leaveRequest.EndDate + "\n Lịch biểu bị trùng: " + item.ThoiGianBatDau + " => " + item.ThoiGianKetThuc; return(NotFound(errorMessage)); } } var leaveTypeId = leaveRequest.LeaveTypeId; var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employeeId, leaveTypeId); int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate).TotalDays; allocation.NumberOfDays -= daysRequested; leaveRequest.Approved = true; leaveRequest.ApprovedById = user.Id; leaveRequest.DateActioned = DateTime.Now; await _leaveRequestRepo.Update(leaveRequest); await _leaveAllocationRepo.Update(allocation); return(RedirectToAction(nameof(Index))); } catch { return(RedirectToAction(nameof(Index))); } }
public ActionResult Create(CreateLeaveRequestVM model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = _leaveTypeRepo.FindAll(); var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int dayRequested = (int)(endDate - startDate).TotalDays; if (dayRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not sufficient days for this request"); return(View(model)); } var leaveRequestModel = new LeaveRequestVM { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = _leaveRequestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with submitting your record"); return(View(model)); } return(RedirectToAction(nameof(Index), "Home")); } catch { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } }
public async Task <ActionResult> ApproveRequest(int id) { try { var leaveRequest = await _leaveRequestRepo.FindById(id); leaveRequest.Approved = true; leaveRequest.ApprovedById = _userManager.GetUserAsync(User).Result.Id; leaveRequest.DateActioned = DateTime.Now; var isSuccess = await _leaveRequestRepo.Update(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something Went Wrong with the registration...."); return(RedirectToAction(nameof(Index))); } else { var employeeAllocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(leaveRequest.RequestingEmployeeId, leaveRequest.LeaveTypeId); var InitialNumberOfDays = employeeAllocation.NumberOfDays; var DaysRequested = (int)(leaveRequest.EndDate.Date - leaveRequest.StartDate.Date).TotalDays; var DaysRemaining = InitialNumberOfDays - DaysRequested; employeeAllocation.NumberOfDays = DaysRemaining; var IsSuccess = await _leaveAllocationRepo.Update(employeeAllocation); if (!isSuccess) { ModelState.AddModelError("", "Something Went Wrong with the registration...."); return(RedirectToAction(nameof(Index))); } return(RedirectToAction(nameof(Index))); } } catch { ModelState.AddModelError("", "Something Went Wrong with the registration...."); return(RedirectToAction(nameof(Index))); } }
public ActionResult Create(CreateLeaveRequestVM model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leavetypes = _LeaveTypeRepo.Findall(); var leavetypeItems = leavetypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leavetypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 0) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); return(View(model)); } var employee = _userManager.GetUserAsync(User).Result; var allocation = _AllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do NOT have sufficient days, get back to work lazy ass"); return(View(model)); } var leaveRequestModel = new LeaveRequestVM { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, LeaveTypeID = model.LeaveTypeId, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now }; var leaverequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = _LeaveRequestRepo.Create(leaverequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } return(RedirectToAction("MyLeave")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); return(View(model)); } }
public ActionResult Create(CreateLeaveRequestVM model) { try { var startDate = DateTime.ParseExact(model.StartDate, "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); var endDate = DateTime.ParseExact(model.EndDate, "mm/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); var leaveTypes = _leaveTypeRepo.FindAll(); var employee = _userManager.GetUserAsync(User).Result; var allocation = _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id.ToString(), model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (allocation == null) { ModelState.AddModelError("", "You Have No Days Left"); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); } if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You Do Not Sufficient Days For This Request"); } if (!ModelState.IsValid) { return(View(model)); } var leaveRequestModel = new LeaveRequestVM { RequestingEmployeeId = employee.Id.ToString(), StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId, RequestComments = model.RequestComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = _leaveRequestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with submitting your record"); return(View(model)); } return(RedirectToAction("MyLeave")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestVM modal) { try { var startDate = Convert.ToDateTime(modal.StartDate); var endDate = Convert.ToDateTime(modal.EndDate); var leaveTypes = await _leaveTypeRepository.FindAll(); var leaveTypeItems = leaveTypes.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); modal.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(modal)); } if (DateTime.Compare(startDate, endDate) > 1) { ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date"); return(View(modal)); } var employee = await _userManager.GetUserAsync(User); var allocation = await _leaveAllocationrepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, modal.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You Do Not Sufficient Days For This Request"); return(View(modal)); } var leaveRequestModel = new LeaveRequestVM { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = modal.LeaveTypeId, RequestComments = modal.RequestComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _leaverequestrepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with submitting your record"); return(View(modal)); } return(RedirectToAction("MyLeave")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong"); Console.WriteLine(ex); return(View(modal)); } }
public async Task <ActionResult> Create(CreateLeaveRequestViewModel model) { try { var startDate = DateTime.Parse(model.StartDate); var endDate = DateTime.Parse(model.EndDate); var leaveTypes = await _leaveTypeRepo.FindAll(); var leaveTypeItems = leaveTypes.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { return(View(model)); } if (DateTime.Compare(startDate, endDate) > 0) { ModelState.AddModelError("", "Start date cannot be the future than the end date"); return(View(model)); } var employee = await _userManager.GetUserAsync(User); var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; var leaveRequestModel = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId, }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var isSuccess = await _requestRepo.Create(leaveRequest); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong with sumbitting your record"); return(View()); } return(RedirectToAction("MyLeave")); } catch { ModelState.AddModelError("", "Something went wrong"); return(View()); } }