public async Task <ActionResult> SetLeave(int id) { var leaveType = await _leaveRepo.FindById(id); var employees = await _userManager.GetUsersInRoleAsync("Employee"); foreach (var emp in employees) { if (await _leaveAllocationRepo.CheckAllocation(id, emp.Id)) { continue; } var allocation = new LeaveAllocationViewModel { DateCreated = DateTime.Now, EmployeeId = emp.Id, LeaveTypeId = id, NumberOfDays = leaveType.DefaultDays, Period = DateTime.Now.Year }; var leaveAllocation = _mapper.Map <LeaveAllocation>(allocation); await _leaveAllocationRepo.Create(leaveAllocation); } await _leaveAllocationRepo.Save(); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> ApproveRequest(int id) { var employee = await _userManager.GetUserAsync(User); var leaveRequest = await _leaveRequestRepo.FindById(id); var allocation = await _leaveAllocationRepo .GetLeaveAllocationByEmployeeAndType(leaveRequest.RequestingEmployeeId, leaveRequest.LeaveTypeId); int daysRequested = (int)(leaveRequest.EndDate.Date - leaveRequest.StartDate.Date).TotalDays; allocation.NumberOfDays -= daysRequested; leaveRequest.Approved = true; leaveRequest.ApprovedById = employee.Id; leaveRequest.DateActioned = DateTime.Now; await _leaveAllocationRepo.Update(allocation); await _leaveRequestRepo.Update(leaveRequest); await _leaveRequestRepo.Save(); await _leaveAllocationRepo.Save(); return(RedirectToAction(nameof(Index))); }