public async Task <ActionResult> ApproveRequest(int id) { try { var user = await _userManager.GetUserAsync(User); var leaveRequests = await _leaveRequestRepository.FindById(id); var allocation = await _leaveAllocationRepository.GetLeaveAllocationByEmployeeAndType( leaveRequests.RequestingEmployeeId, leaveRequests.LeaveTypeId); int dateRequested = (int)(leaveRequests.EndDate - leaveRequests.StartDate).TotalDays; allocation.NumberOfDays -= dateRequested; leaveRequests.Approved = true; leaveRequests.ApprovedById = _userManager.GetUserAsync(User).Result.Id; leaveRequests.DateActioned = DateTime.Now; await _leaveRequestRepository.Update(leaveRequests); await _leaveAllocationRepository.Update(allocation); return(RedirectToAction(nameof(Index))); } catch (Exception) { return(RedirectToAction(nameof(Index), "Home")); } }
public ActionResult ApproveRequest(int id) { try { var user = _userManager.GetUserAsync(User).Result; var leaveRequest = _leaveRequestRepo.FindById(id); var employeeid = leaveRequest.RequestingEmployeeId; var leaveTypeId = leaveRequest.LeaveTypeId; var allocation = _leaveAllocRepo.GetLeaveAllocationByEmployeeAndType(employeeid, leaveTypeId); int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate).TotalDays; allocation.NumberOfDays -= daysRequested; leaveRequest.Approved = true; leaveRequest.ApprovedById = user.Id; leaveRequest.DateActioned = DateTime.Now; _leaveRequestRepo.Update(leaveRequest); _leaveAllocRepo.Update(allocation); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { return(RedirectToAction(nameof(Index))); } }
public async Task <ActionResult> CancelRequest(int id) { var leaveRequest = await _leaveRequestRepo.FindById(id); if (leaveRequest.Approved.Value == true) { int daysRequested = (int)(leaveRequest.EndDate - leaveRequest.StartDate.Date).TotalDays; var allocation = await _leaveAllocationRepo .GetLeaveAllocationByEmployeeAndType(leaveRequest.RequestingEmployeeId, leaveRequest.LeaveTypeId); allocation.NumberOfDays += daysRequested; } leaveRequest.Cancelled = true; await _leaveRequestRepo.Update(leaveRequest); return(RedirectToAction("MyLeave")); }
public ActionResult Create(CreateLeaveRequestViewModel 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 = _leaveAllocationRepo.GetLeaveAllocationByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not sufficient days for this request"); return(View(model)); } var leaveRequest = new LeaveRequestViewModel { RequestingEmployeeId = employee.Id, StartDate = startDate, EndDate = endDate, Approved = null, DateRequested = DateTime.Now, DateActioned = DateTime.Now, LeaveTypeId = model.LeaveTypeId }; var leaveRequestModel = _mapper.Map <LeaveRequest>(leaveRequest); var isSuccess = _leaveRequestRepo.Create(leaveRequestModel); if (!isSuccess) { ModelState.AddModelError("", "Something Went Wrong..."); return(View(model)); } return(RedirectToAction(nameof(Index), "Home")); } catch (Exception ex) { ModelState.AddModelError("", "Something went wrong" + ex.Message); return(View(model)); } }
public async Task <ActionResult> Create(CreateLeaveRequestVM model) { try { var startDate = Convert.ToDateTime(model.StartDate); var endDate = Convert.ToDateTime(model.EndDate); var leaveTypes = await _leaveTypeRepo.FindAll(); //convert the leaveTypes list into an IEnumerable SelectListItem to match our CreateLeaveRequestVM var leaveTypeItems = leaveTypes.Select(t => new SelectListItem { Text = t.Name, //what we see rendered Value = t.id.ToString() //the actual value (value is string) }); model.LeaveTypes = leaveTypeItems; if (!ModelState.IsValid) { ModelState.AddModelError("", "Start Date must come before end date"); return(View(model)); } if (DateTime.Compare(startDate, endDate) > 1) { return(View(model)); } //get the user (employee) that is currently logged in var employee = _userManager.GetUserAsync(User).Result; var allocation = await _leaveAllocationRepo.GetLeaveAllocationByEmployeeAndType(employee.Id, model.LeaveTypeId); int daysRequested = (int)(endDate - startDate).TotalDays; if (daysRequested > allocation.NumberOfDays) { ModelState.AddModelError("", "You do not have sufficient days for this request\nNumber of days left: " + allocation.NumberOfDays); 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, RequestComments = model.RequestComments }; var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel); var success = await _leaveRequestRepo.Create(leaveRequest); if (!success) { ModelState.AddModelError("", "Something went wrong with submitting your record"); return(View(model)); } //redirect to index of Home controller return(RedirectToAction(nameof(Index), "Home")); } catch (Exception e) { ModelState.AddModelError("", "Something went wrong"); return(View(model)); } }