Exemplo n.º 1
0
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            try
            {
                var startDate  = Convert.ToDateTime(model.StartDate);
                var endDate    = Convert.ToDateTime(model.EndDate);
                var leaveTypes = _leaveTypeRepository.FindAll();
                var leaveItems = leaveTypes.Select(q => new SelectListItem
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });
                model.LeaveTypes = leaveItems;
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                if (DateTime.Compare(startDate, endDate) > 0)
                {
                    ModelState.AddModelError("", "Start date cannot be farther in the future than end date");
                    return(View(model));
                }
                var employee      = _userManager.GetUserAsync(User).Result;
                var allocation    = _leaveAllocationRepository.GetLeaveAllocationsByEmployeeAndType(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");
                    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 isSuccess    = _leaveRequestRepository.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong while creating the leaverequest");
                    return(View(model));
                }

                return(RedirectToAction("MyLeave"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View(model));
            }
        }
Exemplo n.º 2
0
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            try
            {
                var leaveTypes      = _leaveTypeRepo.FindAll();
                var leaveTypesItems = leaveTypes.Select(q => new SelectListItem
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });
                model.LeaveTypes = leaveTypesItems;
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (DateTime.Compare(model.StartDate, model.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; //Obtiene el usuario que ha iniciado sesión actualmente
                var allocation    = _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);
                int daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You Do Not have Sufficient Days For This Request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = model.StartDate,
                    EndDate       = model.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 Submiting Your Record");
                    return(View(model));
                }

                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something Went Wrong");
                return(View(model));
            }
        }
Exemplo n.º 3
0
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            try
            {
                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(model.StartDate, model.EndDate) > 1)
                {
                    ModelState.AddModelError("", "Start Date cannot be after the end date, pls review!!");
                    return(View(model));
                }
                var employee      = _userManager.GetUserAsync(User).Result;
                var allocation    = _leaveAllocRepo.GetLeaveAllocationByEmployeeAndType(employee.Id, model.LeaveTypeId);
                int daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You Do Not Have Sufficient Days For  This Request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = model.StartDate,
                    EndDate         = model.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 Request");
                    return(View(model));
                }
                return(RedirectToAction("EmployeeLeaveRequests"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something Went Wrong");
                return(View(model));
            }
        }
        public ActionResult Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                // TODO: Add insert logic here
                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(model.StartDate, model.EndDate) > 1)
                {
                    ModelState.AddModelError("", "Invalid Date selection...");
                    return(View(model));
                }

                var employee      = _userManager.GetUserAsync(User).Result;
                var allocation    = _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);
                var daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You Do not have sufficien days for this request.");
                    return(View(model));
                }

                var leaveRequest = new LeaveRequestViewModel
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = model.StartDate,
                    EndDate       = model.EndDate,
                    Approved      = null,
                    DateRequested = DateTime.Now,
                    DateActioned  = DateTime.Now,
                    LeaveTypeId   = model.LeaveTypeId
                };

                var leaveRequst = _mapper.Map <LeaveRequest>(leaveRequest);
                var isSuccess   = _leaveRequestRepo.Create(leaveRequst);
                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong");
                    return(View());
                }


                return(RedirectToAction("UserLeaveRequests"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View());
            }
        }
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            var leaveTypes     = _leaveTypeRepo.FindAll();
            var leaveTypeItems = leaveTypes.Select(q => new SelectListItem
            {
                Text  = q.Name,
                Value = q.Id.ToString()
            });

            try
            {
                // TODO: Add insert logic here
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (DateTime.Compare(model.StartDate, model.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    = _leaveAlloRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);
                int daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You Don't Have Enough Days Left");
                    return(RedirectToAction(nameof(Index), "Home"));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = model.StartDate,
                    EndDate       = model.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(string.Empty, "Errr, Something Went Wrong....");
                    return(RedirectToAction(nameof(Index), "Home"));
                }

                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(View(model));
            }
        }
Exemplo n.º 6
0
        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();

                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 is later than end date.");
                    return(View(model));
                }
                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays;
                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You don't have enough of these leave-type days!");
                    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 isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong submitting your request.");
                    return(View(model));
                }
                return(RedirectToAction("MyLeave"));
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Something went wrong.");
                return(View(model));
            }
        }
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            try
            {
                var leaveTypes = _leaveTypeRepo.FindAll().Select(q => new SelectListItem {
                    Value = q.Id.ToString(), Text = q.Name
                });
                model.LeaveTypes = leaveTypes;

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (DateTime.Compare(model.StartDate, model.EndDate) > 0)
                {
                    ModelState.AddModelError("", "Error");
                    return(View(model));
                }

                var employee      = _userManager.GetUserAsync(User).Result;
                var allocations   = _allocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);
                int daysRequested = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays;
                if (daysRequested > allocations.NumberOfDays)
                {
                    ModelState.AddModelError("", "Error to mach days");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = model.StartDate,
                    EndDate       = model.EndDate,
                    Approved      = null,
                    DateRequested = DateTime.Now,
                    DateActioned  = DateTime.Now,
                    LeaveTypeId   = model.LeaveTypeId
                };

                var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = _requestRepo.Create(leaveRequest);
                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Error create");
                    return(View(model));
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Error");
                return(View(model));
            }
        }
Exemplo n.º 8
0
        public async Task <ActionResult> Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                var startDate = Convert.ToDateTime(model.StartDate);
                var endDate   = Convert.ToDateTime(model.EndDate);

                var leaveTypes = await _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 farther in 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)(startDate - endDate).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have enough days to make this request.");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestViewModel
                {
                    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 isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong trying to add your Leave Request to the database.");
                    return(View(model));
                }

                return(RedirectToAction(nameof(MyLeave)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Something went wrong trying to create your Leave Request:\n {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();

                var leaveTypeItems = leaveTypes.Select(q => new SelectListItem
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });
                model.LeaveTypes = leaveTypeItems;
                // TODO: Add insert logic here
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (DateTime.Compare(StartDate, EndDate) > 1)
                {
                    ModelState.AddModelError("", "Start Date cannot greater than End Date");
                    return(View(model));
                }

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(EndDate.Date - StartDate.Date).TotalDays;
                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "Do not have enough Number of Days");
                    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,
                    Cancelled       = false,
                    RequestComments = model.RequestComments
                };
                var leaveRequest = _mapper.Map <LeaveRequestVM, LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Database Error");
                    return(View(model));
                }
                return(RedirectToAction(nameof(MyLeave)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View(model));
            }
        }
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            try
            {
                var startDate = Convert.ToDateTime(model.StartDate);
                var endDate   = Convert.ToDateTime(model.EndDate);

                //Retrieve leavetype list from the database
                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) // validation to compare the startDate with EndDate, eliminates user putting garbage into the system
                {
                    ModelState.AddModelError("", "Error...Start date can't be further than End date");
                    return(View(model));
                }

                var employee      = _userManager.GetUserAsync(User).Result;                                                    // retrieve the employee
                var allocation    = _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId); //retrieve the allocations that the employee has
                int daysRequested = (int)(endDate - startDate).TotalDays;

                if (daysRequested > allocation.NumberofDays)
                {
                    ModelState.AddModelError("", "Not enough days to process request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestViewModel
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = startDate,
                    EndDate         = endDate,
                    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("", "Submission Error...Contact Your Administrator");
                    return(View(model));
                }
                return(RedirectToAction("MyLeave"));
            }
            catch
            {
                ModelState.AddModelError("", "Error...");
                return(View(model));
            }
        }
        public ActionResult Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                var leaveTypes     = _leaveTypeRepository.FindAll().ToList();
                var employees      = _employeeRepository.FindAll().ToList();
                var startDate      = Convert.ToDateTime(model.StartDate);
                var endDate        = Convert.ToDateTime(model.EndDate);
                var leaveTypeItems = leaveTypes.Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Id.ToString()
                });
                var employeeItems = employees.Select(x => new SelectListItem
                {
                    Text  = x.FirstName + " " + x.LastName,
                    Value = x.Id
                });
                model.LeaveTypes = leaveTypeItems;
                model.Employees  = employeeItems;
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (DateTime.Compare(startDate, endDate) > 1)
                {
                    ModelState.AddModelError("", "End Date Can not be earlier than Start Date");
                    return(View(model));
                }


                var allocation =
                    _leaveAllocationRepository.GetLeaveAllocationByEmployeeAndLeaveType(model.EmployeeId, model.LeaveTypeId);

                var daysRequested = (int)(endDate - startDate).TotalDays;
                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have enough days");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestViewModel
                {
                    RequestingEmployeeId = model.EmployeeId,
                    StartDate            = startDate,
                    EndDate     = endDate,
                    Approved    = null,
                    RequestDate = DateTime.Now,
                };
                var leaveRequest = new LeaveRequest
                {
                    Approved           = null,
                    ApprovedBy         = null,
                    EndDate            = endDate,
                    LeaveType          = _leaveTypeRepository.FindById(model.LeaveTypeId),
                    RequestDate        = DateTime.Now,
                    RequestingEmployee = _employeeRepository.FindById(Guid.Parse(model.EmployeeId)),
                    StartDate          = startDate
                };

                var isSuccess = _leaveRequestRepository.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong with submitting the record");
                    return(View(model));
                }

                if (User.IsInRole("Administrator"))
                {
                    return(RedirectToAction(nameof(Index)));
                }

                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", "Something Went Wrong: " + exception.Message);
                return(View(model));
            }
        }
Exemplo n.º 12
0
        public async Task <ActionResult> Create(CreateLeaveRequestVM model)
        {
            try
            {
                var StartDate = Convert.ToDateTime(model.StartDate);
                var EndDate   = Convert.ToDateTime(model.EndDate);

                var leaveTypes     = _leaveTypeRepo.FindAll();
                var leaveTypeItems = (await 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("", "Ngày kết thúc phải sau ngày bắt đầu.");
                    return(View(model));
                }

                if (DateTime.Compare(StartDate, DateTime.Now.Date) < 0)
                {
                    ModelState.AddModelError("", "Ngày bắt đầu và ngày kết thúc phải ở tương lai." + DateTime.Now.Date.ToString());
                    return(View(model));
                }


                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(EndDate - StartDate).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "Số ngày bạn yêu cầu vượt quá số ngày cho phép");
                    return(View(model));
                }

                var previousApprovedLeaveRequests = (await _leaveRequestRepo.FindAll())
                                                    .Where(q => q.RequestingEmployeeId == employee.Id);
                foreach (var request in previousApprovedLeaveRequests)
                {
                    if (DateTime.Compare(StartDate, request.StartDate) > 0 && DateTime.Compare(StartDate, request.EndDate) < 0 ||
                        DateTime.Compare(EndDate, request.StartDate) > 0 && DateTime.Compare(EndDate, request.EndDate) < 0 ||
                        DateTime.Compare(StartDate, request.StartDate) <= 0 && DateTime.Compare(EndDate, request.EndDate) >= 0
                        )
                    {
                        ModelState.AddModelError("", "Khoảng thời gian nghỉ phép mà bạn yêu cầu trùng với khoảng thời gian nghỉ phép đã gửi.");
                        return(View(model));
                    }
                }

                var LichSuChamCongList = await nhatKylamViecRepository.FindByMaNhanVien(employee.Id);

                foreach (var item in LichSuChamCongList)
                {
                    if (StartDate.CompareTo(item.ThoiGianBatDau) >= 0 && StartDate.CompareTo(item.ThoiGianKetThuc) < 0
                        ||
                        EndDate.CompareTo(item.ThoiGianBatDau) > 0 && EndDate.CompareTo(item.ThoiGianKetThuc) <= 0)
                    {
                        ModelState.AddModelError("", "Khoảng thời gian được chọn bị trùng với lịch biểu trước đó" +
                                                 "\n Khoảng thời gian được chọn: " + StartDate + " => " + EndDate +
                                                 "\n Lịch biểu bị trùng: " + item.ThoiGianBatDau + " => " + item.ThoiGianKetThuc);
                        return(View(model));
                    }
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = StartDate,
                    EndDate       = EndDate,
                    ApprovedById  = null,
                    DateRequested = DateTime.Now,
                    DateActioned  = DateTime.Now,
                    LeaveTypeId   = model.LeaveTypeId
                };

                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(model));
                }
                return(RedirectToAction(nameof(MyLeave)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View(model));
            }
        }
Exemplo n.º 13
0
        public async Task <ActionResult> Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                /* Loads in the leaveTypes data again in the event of
                 * model evaluation returning a result that is not valid.
                 * This allows us to return the view with model without
                 * returning with missing data which would be a poor UX.
                 */
                var leaveTypes = await _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));
                }

                /* Date selection validation 1:
                 * Checks that the end date is not before the start date
                 * if so it returns to the view and doesn't process.
                 */
                if (DateTime.Compare(model.StartDate, model.EndDate) > 1)
                {
                    ModelState.AddModelError("", "The end date cannot be before the start date.");
                    return(View(model));
                }


                /* load up the:
                 * user details
                 * allocation type
                 * number of days requested
                 */
                var employee   = _userManager.GetUserAsync(User).Result;
                var allocation = await _allocationsRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(model.EndDate - model.StartDate).TotalDays;

                /* Date selection validation 2:
                 * Checks that the end date is not before the start date
                 * if so it returns to the view and doesn't process.
                 */
                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "Insufficient allocation exists to process the request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestViewModel
                {
                    RequestingEmployeeId = employee.Id,
                    LeaveTypeId          = model.LeaveTypeId,
                    StartDate            = model.StartDate,
                    EndDate       = model.EndDate,
                    Approved      = null,
                    Cancelled     = false,
                    DateRequested = DateTime.Now.Date
                };

                var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Insufficient allocation exists to process the request");
                    return(View(model));
                }

                return(RedirectToAction("MyLeave"));
            }
            catch
            {
                ModelState.AddModelError("", "Error sending new leave request");
                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();

                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)
                {
                    //The start date is after the end date
                    ModelState.AddModelError("", "Start Date cannot be further in the future than the End Date");
                    return(View(model));
                }

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                var daysRequested = (int)(endDate - startDate).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have sufficient days for this request.");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    LeaveTypeId          = model.LeaveTypeId,
                    Approved             = null,
                    StartDate            = startDate,
                    EndDate              = endDate,
                    DateRequested        = DateTime.Now,
                    DateActioned         = DateTime.Now,
                    RequestingEmployeeId = employee.Id,
                    RequestComments      = model.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(model));
                }

                //To be changed later....
                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch
            {
                return(View(model));
            }
        }
        public async Task <ActionResult> Create(CreateLeaveRequestVMClass parCreateLeaveRequestVMClass)
        {
            try
            {
                var varStartDate = Convert.ToDateTime(parCreateLeaveRequestVMClass.StartDate);
                var varEndDate   = Convert.ToDateTime(parCreateLeaveRequestVMClass.EndDate);

                var varLeaveTypeRepositoryClass = await _ILeaveTypeRepository.findAll();

                var varSelectListItem = varLeaveTypeRepositoryClass.Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.LeaveTypeID.ToString()
                });

                parCreateLeaveRequestVMClass.selectLeaveTypes = varSelectListItem;

                if (!ModelState.IsValid)
                {
                    return(View(parCreateLeaveRequestVMClass));
                }


                int intWTF = (int)DateTime.Compare(varEndDate, varStartDate);
                if ((int)DateTime.Compare(varEndDate, varStartDate) < 0)
                {
                    ModelState.AddModelError("", "To Date < then From Date");
                    return(View(parCreateLeaveRequestVMClass));
                }



                var varEmployeeLoggedIn     = _userManager.GetUserAsync(User).Result;
                var varLeaveAllocationClass = await _ILeaveAllocationRepository.getEmployeeTypeAllocation(varEmployeeLoggedIn.Id, parCreateLeaveRequestVMClass.LeaveTypeID);

                if (varLeaveAllocationClass.LeaveAllocationID == 0)
                {
                    ModelState.AddModelError("", "No Leave Allocated");
                    return(View(parCreateLeaveRequestVMClass));
                }

                double dblDaysRequested = (double)(varEndDate.Date - varStartDate.Date).TotalDays;
                if (dblDaysRequested > varLeaveAllocationClass.NumberOfDays)
                {
                    ModelState.AddModelError("", "Exceeds Leave Allocated");
                    return(View(parCreateLeaveRequestVMClass));
                }

                var varLeaveRequestVMClass = new LeaveRequestVMClass
                {
                    StartDate           = varStartDate,
                    EndDate             = varEndDate,
                    LeaveTypeID         = parCreateLeaveRequestVMClass.LeaveTypeID,
                    DateRequested       = DateTime.Now,
                    DateActioned        = DateTime.Now,
                    RequestedEmployeeID = varEmployeeLoggedIn.Id
                };

                var  varLeaveRequestDataModel = _IMapper.Map <LeaveRequest>(varLeaveRequestVMClass);
                bool isSuccess = await _ILeaveRequestRepository.Create(varLeaveRequestDataModel);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Insert Applivation Error");
                    return(View(parCreateLeaveRequestVMClass));
                }


                // TODO: Add insert logic here

                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch (Exception exError)
            {
                ModelState.AddModelError("", exError.Message);
                return(View(parCreateLeaveRequestVMClass));
            }
        }
Exemplo n.º 16
0
        public async Task <ActionResult> Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                var startDate  = Convert.ToDateTime(model.StartDate);
                var endDate    = Convert.ToDateTime(model.EndDate);
                var leavetypes = await _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("", "Invalid Date");
                    return(View(model));
                }

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveallocationrepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(endDate.Date - startDate.Date).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "Insufficient days");
                    return(View(model));
                }

                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 _leaverequestrepo.Create(leaverequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong");
                    return(View(model));
                }

                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View());
            }
        }
        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();

                var leaveTypeItems = leaveTypes.Select(q => new SelectListItem
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });

                model.LeaveTypes = leaveTypeItems;

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if ((StartDate < DateTime.Now) || (EndDate < DateTime.Now))
                {
                    ModelState.AddModelError("", "Start Date or End Date cannot be earlier than today's date");
                    return(View(model));
                }

                if (DateTime.Compare(StartDate, EndDate) > 0)
                {
                    ModelState.AddModelError("", "Start date cannot be greater than End date");
                    return(View(model));
                }

                var employee   = _userManager.GetUserAsync(User).Result;
                var allocation = await _leaveAllocationRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int DaysRequested = (int)(EndDate.Date - StartDate.Date).TotalDays;

                if (DaysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "Days Requested Exceeds number of days available");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    LeaveTypeId          = model.LeaveTypeId,
                    StartDate            = StartDate,
                    EndDate          = EndDate,
                    Approved         = null,
                    DateRequested    = DateTime.Now,
                    DateActioned     = DateTime.Now,
                    RequestsComments = model.RequestsComments
                };

                var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something Went Wrong with the registration....");
                    return(View());
                }



                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch
            {
                ModelState.AddModelError("", "Something Went Wrong....");
                return(View());
            }
        }
Exemplo n.º 18
0
        public async Task <ActionResult> Create(CreateLeaveRequestVM model)
        {
            try
            {
                var startDate  = DateTime.Parse(model.StartDate);
                var endDate    = DateTime.Parse(model.EndDate);
                var leaveTypes = await _leaveTypeRepo.FindAll();

                var leaveTypeItems = leaveTypes.Select(lt => new SelectListItem(lt.Name, lt.Id.ToString()));

                model.LeaveTypes = leaveTypeItems;

                if (DateTime.Compare(startDate, endDate) > 0)
                {
                    ModelState.AddModelError("", "Start Date can not be later than End Date");
                }

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (endDate.Date - startDate.Date).Days; // doesn't account for weekends

                if (daysRequested > allocation?.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have sufficient days for this request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    LeaveTypeId          = model.LeaveTypeId,
                    StartDate            = startDate,
                    EndDate       = endDate,
                    Approved      = null,
                    DateRequested = DateTime.Now,
                    DateActioned  = DateTime.Now // HACK - should make the property nullable
                };
                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));
                }

                return(RedirectToAction("MyLeave"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                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();

                var leaveTypesItems = leaveTypes.Select(q => new SelectListItem
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });

                model.LeaveTypes = leaveTypesItems;
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }


                if (DateTime.Compare(startDate, endDate) > 0)
                {
                    ModelState.AddModelError("", "Start date can not be further in 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;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "Sorry, You have requested more days than you have left");
                    return(View(model));
                }

                var leaveRequest = new LeaveRequestVm
                {
                    DateActioned         = DateTime.Now,
                    RequestingEmployeeId = employee.Id,
                    StartDate            = startDate,
                    EndDate         = endDate,
                    Approved        = null,
                    DateRequested   = DateTime.Now,
                    LeaveTypeId     = model.LeaveTypeId,
                    RequestComments = model.RequestComments
                };
                var obj = _mapper.Map <LeaveRequest>(leaveRequest);


                var success = await _leaveRequestRepo.Create(obj);

                if (!success)
                {
                    ModelState.AddModelError("", "Something went wrong when applying for leave");
                    return(View(model));
                }


                return(RedirectToAction(nameof(EmployeeIndex)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong when appliying for leave");
                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();

                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("", "Invalid Dates");
                    return(View(model));
                }

                //Getting logged in user
                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id.ToString(), model.LeaveTypeId);

                int daysRequested = (int)(endDate - startDate).TotalDays;

                if (allocation == null)
                {
                }

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "you do not have enough days for this request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = startDate,
                    EndDate       = endDate,
                    Approved      = null,
                    DateRequested = DateTime.Now,
                    DateActioned  = null,
                    LeaveTypeId   = model.LeaveTypeId
                };

                var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong when submitting your application");
                    return(View(model));
                }

                return(RedirectToAction("MyLeaves"));
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return(View(model));
            }
        }
        public async Task <ActionResult> Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                DateTime startDate = Convert.ToDateTime(model.StartDate);
                DateTime endDate   = Convert.ToDateTime(model.EndDate);
                ICollection <LeaveType> leaveTypes = await _leaveTypeRepo.FindAll();

                IEnumerable <SelectListItem> 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("", "The start date may not be futher"
                                             + " in the future than the end date.");

                    return(View(model));
                }

                Employee employee = await _userManager.GetUserAsync(User);

                LeaveAllocation allocation = await _leaveAllocationRepo
                                             .GetLeaveAllocationsByEmployeeAndType(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.");

                    return(View(model));
                }

                LeaveRequestViewModel leaveRequestModel = new LeaveRequestViewModel
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = startDate,
                    EndDate         = endDate,
                    Approved        = null,
                    DateRequested   = DateTime.Now,
                    DateActioned    = DateTime.Now,
                    LeaveTypeId     = model.LeaveTypeId,
                    RequestComments = model.RequestComments
                };

                LeaveRequest leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                bool         isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong with submitting your record.");

                    return(View(model));
                }

                return(RedirectToAction("MyLeave"));
            }
            catch
            {
                ModelState.AddModelError("", "Something went wrong");

                return(View(model));
            }
        }
Exemplo n.º 22
0
        public async Task <ActionResult> Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                var startDate  = Convert.ToDateTime(model.StartDate);
                var endDate    = Convert.ToDateTime(model.EndDate);
                var leaveTypes = await _typeRepo.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("", "The start date cannot be greater than the end date!");
                    return(View(model));
                }

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _allocationRepo.GetAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(endDate - startDate).TotalDays;

                if (daysRequested > allocation.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have sufficient leave time for this request!");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestViewModel
                {
                    LeaveTypeId          = model.LeaveTypeId,
                    RequestingEmployeeId = employee.Id,
                    StartDate            = startDate,
                    EndDate         = endDate,
                    Approved        = null,
                    DateRequested   = DateTime.Now,
                    DateActioned    = DateTime.Now,
                    RequestComments = model.RequestComments
                };

                var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _requestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Unable to submit record! Please try another request.");
                    return(View(model));
                }

                return(RedirectToAction("MyLeave"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong! Please try another request.");
                return(View(model));
            }
        }
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            var startDate          = Convert.ToDateTime(model.StartDate);
            var EndDate            = Convert.ToDateTime(model.EndDate);
            var leavetypes         = _leavTyperepo.FindAll();
            var leavetypeviewietms = leavetypes.Select(q => new SelectListItem
            {
                Text  = q.Name,
                Value = q.Id.ToString()
            });

            model.LeaveTypes = leavetypeviewietms;
            try
            {
                if (!ModelState.IsValid)

                {
                    return(View(model));
                }

                if (DateTime.Compare(startDate, EndDate) > 1)
                {
                    ModelState.AddModelError("", "Start date can not be further in the furture than End Date");
                    return(View(model));
                }
                var employee      = _userManager.GetUserAsync(User).Result;
                var allocations   = _leavAllocationrepo.GetLeaveAllocationsByEmployeeandType(employee.Id, model.LeaveTypeId);
                int daysRequested = (int)(EndDate - startDate).TotalDays;
                if (daysRequested > allocations.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have sufficinet days for thsi Request");
                    return(View(model));
                }
                var leaveRequetsmodel = 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>(leaveRequetsmodel);
                var isuccess     = _leaveRequestrepo.Create(leaverequest);

                if (!isuccess)
                {
                    ModelState.AddModelError("", "Somthing went wrong with somthing with submitting your record");
                    return(View(model));
                }
                return(RedirectToAction("MyLeave"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Somthing went wrong");
                return(View(model));
            }
        }
        public async Task <ActionResult> Create(CreateLeaveRequestVM model)
        {
            try
            {
                var startDate  = Convert.ToDateTime(model.startDate);
                var endtDate   = Convert.ToDateTime(model.endDate);
                var leaveTypes = await _leaveTypeRepo.FindAll();

                var leaveTypeItems = leaveTypes.Select(q => new SelectListItem // To convert it to IEnumerable<SelectListItem> .
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });
                model.LeaveTypes = leaveTypeItems;
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                if (DateTime.Compare(startDate, endtDate) > 1)
                {
                    ModelState.AddModelError("", "Start date can'nt be further in the futere than end date !");
                    return(View(model));
                }
                var employee = await _userManager.GetUserAsync(User); // get user info. (Who is singed in).

                var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                int daysRequested = (int)(endtDate - startDate).TotalDays;
                if (daysRequested > allocation.NumberOfDays) // number of days for the specific emp and leave type.
                {
                    ModelState.AddModelError("", "You don't have sufficient days for this request");
                    return(View(model));
                }

                var leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = startDate,
                    EndDate         = endtDate,
                    Approved        = null,
                    DateRequested   = DateTime.Now,
                    DateActioned    = DateTime.Now,
                    LeaveTypeId     = model.LeaveTypeId,
                    RequestComments = model.RequestComments
                };

                var leaveRequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _leaveRequestRepo.Create(leaveRequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong with submitting your recoed");
                    return(View(model));
                }

                return(RedirectToAction("MyLeave")); // redirect to Home Index page.
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "Something went wrong !");
                return(View(model));
            }
        }
Exemplo n.º 25
0
        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();

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, 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,
                    StartDate            = startDate,
                    EndDate       = endDate,
                    Approved      = null,
                    DateRequested = DateTime.Now,
                    DateActioned  = DateTime.Now,
                    LeaveTypeId   = model.LeaveTypeId,
                };

                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(model));
                }

                return(RedirectToAction("MyLeave"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                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));
            }
        }
Exemplo n.º 27
0
        public ActionResult Create(CreateLeaveRequestViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var errormodel = GetCreateLeaveRequestVM();
                    return(View(errormodel));
                }

                if (model.StartDate >= model.EndDate)
                {
                    var errormodel = GetCreateLeaveRequestVM();
                    ModelState.AddModelError("", "Start date must be smaller than end date.");
                    return(View(errormodel));
                }

                var numberOfDays = (int)(model.EndDate.Date - model.StartDate.Date).TotalDays;


                var employee   = _userManager.GetUserAsync(User).Result;
                var allocation = _leaveAllocationRepo.GetLeaveAllocationsByEmployeeIdandLeaveType(employee.Id, model.LeaveTypeId, DateTime.Now.Year).FirstOrDefault();

                if (numberOfDays > allocation.NumberofDays)
                {
                    var errormodel = GetCreateLeaveRequestVM();
                    ModelState.AddModelError("", "Not enough remaining days.");
                    return(View(errormodel));
                }

                allocation.NumberofDays -= numberOfDays;
                var request = new LeaveRequestViewModel()
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = model.StartDate,
                    EndDate       = model.EndDate,
                    DateRequested = DateTime.UtcNow,
                    Approved      = null,
                    DateActioned  = DateTime.UtcNow,
                    LeaveTypeId   = model.LeaveTypeId
                };

                var op1 = _repo.Create(_mapper.Map <LeaveRequest>(request));
                var op2 = _leaveAllocationRepo.Update(allocation);

                if (!op1 || !op2)
                {
                    var errormodel = GetCreateLeaveRequestVM();
                    ModelState.AddModelError("", "Error during save.");
                    return(View(errormodel));
                }

                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch
            {
                ModelState.AddModelError("", "Operation Error");
                var errormodel = GetCreateLeaveRequestVM();
                return(View(errormodel));
            }
        }
        public async Task <ActionResult> Create(CreateLeaveRequestVM model)
        {
            var leaveTypes = await _repoleavetype.FindAll();

            var leavetypeitems = leaveTypes.Select(x => new SelectListItem
            {
                Value = x.Id.ToString(),
                Text  = x.Name
            });

            model.LeaveTypes = leavetypeitems;
            try
            {
                var StartDate = Convert.ToDateTime(model.StartDate);
                var EndDate   = Convert.ToDateTime(model.EndDate);
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                if (DateTime.Compare(StartDate, EndDate) > 1)
                {
                    ModelState.AddModelError("", "Starting Date cannot be greater then End Date...");
                    return(View(model));
                }

                var employee = await _userManager.GetUserAsync(User);

                var allocation = await _repoleaveallocation.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);

                var 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 leaveRequestModel = new LeaveRequestVM
                {
                    RequestingEmployeeId = employee.Id,
                    StartDate            = StartDate,
                    EndDate        = EndDate,
                    Approved       = null,
                    DateRequested  = DateTime.Now,
                    DateActioned   = DateTime.Now,
                    LeaveTypeId    = model.LeaveTypeId,
                    CommentRequest = model.CommentRequest
                };

                var leaverequest = _mapper.Map <LeaveRequest>(leaveRequestModel);
                var isSuccess    = await _repoleaverequest.Create(leaverequest);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something Went Wrong with submitting your record...");
                    return(View(model));
                }
                return(RedirectToAction(nameof(MyLeave)));
            }
            catch
            {
                ModelState.AddModelError("", "Something Went Wrong...");
                return(View(model));
            }
        }
Exemplo n.º 29
0
        public ActionResult Create(CreateLeaveRequestVM model)
        {
            try
            {
                CultureInfo provider = CultureInfo.InvariantCulture;
                var         format   = "MM/dd/yyyy";


                var startDate = DateTime.ParseExact(model.StartDate, format, provider);
                var endDate   = DateTime.ParseExact(model.EndTime, format, provider);
                //var startDate = Convert.ToDateTime(model.StartDate, CultureInfo.CurrentCulture);
                //var endDate = Convert.ToDateTime(model.EndTime);
                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 allocations  = _leaveAllocRepo.GetLeaveAllocationsByEmployeeAndType(employee.Id, model.LeaveTypeId);
                int daysRequired = (int)(endDate - startDate).TotalDays;
                if (daysRequired > allocations.NumberOfDays)
                {
                    ModelState.AddModelError("", "You do not have 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");
                    return(View(model));
                }
                return(RedirectToAction("MyLeave"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View(model));
            }
        }
Exemplo n.º 30
0
        public async Task <ActionResult> Create(createLeaveRequestVM model)
        {
            try
            {
                var startDate  = Convert.ToDateTime(model.startDate);
                var endDate    = Convert.ToDateTime(model.endDate);
                var leaveTypes = await _leaveTypeRepo.fndAll();

                var leaveTypeItem = leaveTypes.Select(q => new SelectListItem
                {
                    Text  = q.Name,
                    Value = q.Id.ToString()
                });
                model.leaveTypes = leaveTypeItem;


                if (ModelState.IsValid)
                {
                    if (DateTime.Compare(startDate, endDate) > 0)
                    {
                        ModelState.AddModelError("", "Start date cannot be greater than end date.");
                        return(View(model));
                    }
                    else
                    {
                        var employee = await _userManager.GetUserAsync(User);

                        var allocations = await _leavaAllocRepo.getLeaveAllocationDetailonLeaveTypeId(employee.Id, model.LeaveTypeId);

                        int requestedDays = (int)(endDate.Date - startDate.Date).TotalDays;
                        if (requestedDays > allocations.NumberOfDays)
                        {
                            ModelState.AddModelError("", "You dont have sufficient days left , please choose less days.");
                            return(View(model));
                        }

                        var leaveRequestData = new LeaveRequestVM
                        {
                            requestEmployeeid = employee.Id,
                            LeaveTypeId       = model.LeaveTypeId,
                            startDate         = startDate,
                            endDate           = endDate,
                            Approved          = null,
                            dateRequested     = DateTime.Now,
                            dateActioned      = DateTime.Now,
                            RequestComments   = model.RequestComments
                        };
                        var leaveRequestUpdate = _mapper.Map <LeaveRequest>(leaveRequestData);
                        var isSuccess          = await _leaveRequestrepo.Create(leaveRequestUpdate);

                        if (!isSuccess)
                        {
                            ModelState.AddModelError("", "Unable to process the request, Something went wron while submitting!");
                            return(View(model));
                        }
                    }
                }
                else
                {
                    return(View(model));
                }
                return(RedirectToAction(nameof(Index), "Home"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Somethig went wrong!");
                return(View(model));
            }
        }