public static bool IsVacationDaysTakenBefore(CreateEmployeeVacationModel model) { DateTimeRange requestedDays = new DateTimeRange(model.EmployeeVacation.StartDate, model.EmployeeVacation.EndDate); try { List <EmployeeVacation> result = EmployeeVacationsRepositories.GetAllEmployeeVacationsByVacationTypeId(model.EmployeeVacation.VacationTypeId, model.EmployeeVacation.EmployeeUserId, DateTimeHelper.Today()); foreach (EmployeeVacation vacation in result) { DateTimeRange lastTakenDaysOfVacation = new DateTimeRange(vacation.StartDate, vacation.EndDate); if (requestedDays.Intersects(lastTakenDaysOfVacation)) { return(true); } } return(false); } catch (Exception e) { LogsLogic.InsertLog(new Log() { Message = e.Message, StackTrace = e.StackTrace, StoryName = "BusinessLogic/EmployeeVacationLogic/IsVacationDaysExistInPreviousRequestedVacation", Parameters = "requestedDays=" + new JavaScriptSerializer().Serialize(requestedDays) + "userId=" + model.EmployeeVacation.EmployeeUserId + "& vacationTypeId= " + model.EmployeeVacation.VacationTypeId }); return(true); } }
public ActionResult Create(CreateEmployeeVacationModel model) { try { model.IsAdmin = (SessionData.UserRole == UserRoles.Admin); EmployeeVacationsLogic.CreateEmployeeVacationRequest(model); if (!model.Succeeded) { ModelState.AddModelError("ErrorMessage", model.ErrorMessage); return(PartialView("CreatePartial", model)); } string url = model.IsAdmin? "/Users/DeveloperIndex" : "/Employees/Index"; return(PartialView("JavascriptRedirect", new JavascriptRedirectModel(url))); } catch (Exception e) { LogsLogic.InsertLog(new Log() { Message = e.Message, StackTrace = e.StackTrace, StoryName = "ManagementProject/EmployeeVacations/Create(Post)", Parameters = new JavaScriptSerializer().Serialize(model) }); } return(PartialView("CreatePartial", model)); }
public ActionResult Create(int userId = 0) { CreateEmployeeVacationModel model; try { if (userId == 0) { userId = SessionData.UserId; } model = EmployeeVacationsLogic.CreateEmployeeVacationRequest(userId); } catch (Exception e) { LogsLogic.InsertLog(new Log() { Message = e.Message, StackTrace = e.StackTrace, StoryName = "ManagementProject/EmployeeVacations/Create(Get)", }); model = new CreateEmployeeVacationModel() { EmployeeVacation = new EmployeeVacation(), VacationTypesList = new List <VacationType>() }; } return(PartialView("CreatePartial", model)); }
public static bool IsRequestedVacationDaysValid(CreateEmployeeVacationModel model) { int vacationMaxDays = VacationTypesRepositories.GetVacationTypeById(model.EmployeeVacation.VacationTypeId).VacationLength; int userVacationDays = EmployeeVacationsRepositories.GetUserVacationDays(model.EmployeeVacation.EmployeeUserId, model.EmployeeVacation.VacationTypeId, model.EmployeeVacation.StartDate.Year); int remainingDaysOfVacation = vacationMaxDays - userVacationDays; return(model.EmployeeVacation.VacationDays <= remainingDaysOfVacation); }
public static CreateEmployeeVacationModel CreateEmployeeVacationRequest(int userId) { CreateEmployeeVacationModel model = new CreateEmployeeVacationModel(); model.EmployeeVacation = new EmployeeVacation() { EmployeeUserId = userId, StartDate = DateTimeHelper.Today(), EndDate = DateTimeHelper.Today() }; model.VacationTypesList = VacationTypesRepositories.GetVacationTypesExceptUnpaid(); return(model); }
public static void CreateEmployeeVacationRequest(CreateEmployeeVacationModel model) { model.Succeeded = false; if (model.EmployeeVacation.StartDate.Date.CompareTo(DateTimeHelper.Today().Date) < 0 && !model.IsAdmin) { model.ErrorMessage = "Start Date Must Not be Less than Today"; return; } if (model.EmployeeVacation.EndDate.Date.CompareTo(DateTimeHelper.Today().Date) < 0 && !model.IsAdmin) { model.ErrorMessage = "End Date Must be Greater than Today"; return; } if (model.EmployeeVacation.EndDate.CompareTo(model.EmployeeVacation.StartDate) < 0) { model.ErrorMessage = "End Date Must be Later than Start Date"; return; } if (model.EmployeeVacation.StartDate.Year != model.EmployeeVacation.EndDate.Year) { model.ErrorMessage = "End Date Must be in same Start Date Year."; return; } model.EmployeeVacation.StatusId = model.IsAdmin ? (int)EVacationStatus.Approved : (int)EVacationStatus.Pending; model.VacationTypesList = VacationTypesRepositories.GetVacationTypesExceptUnpaid(); model.EmployeeVacation.VacationDays = GetRequestedVacationDays(model.EmployeeVacation.StartDate, model.EmployeeVacation.EndDate); if (model.EmployeeVacation.VacationDays < 1) { model.ErrorMessage = "Requested Vacation Period is already Vacation in our system ;)."; return; } if (!IsRequestedVacationDaysValid(model)) { model.ErrorMessage = "Requested Vacation Days Must be less than Remaining Days Of This Vacation."; return; } if (IsVacationDaysTakenBefore(model)) { model.ErrorMessage = "There are days or part of them was requested before"; return; } EmployeeVacationsRepositories.InsertNewEmployeeVacation(model.EmployeeVacation); //EmailHelper.VacationSendEmail(model, SessionData.UserName); model.Succeeded = true; }