예제 #1
0
        public IHttpActionResult CancelVacation(GeneralIdModel model)
        {
            var vacation = VacationRepository.First(x => x.Id == model.Id);
            var employee = EmployeeRepository.First(x => x.Id == CurrentUser.Id);

            employee.VacationDays += vacation.Days;
            vacation.Status        = VacationStatus.Declined;

            EmployeeRepository.SaveOrUpdate(employee);
            VacationRepository.SaveOrUpdate(vacation);

            EmployeeRepository.Commit();
            VacationRepository.Commit();
            return(Ok());
        }
예제 #2
0
        public IHttpActionResult ApproveVacation(GeneralIdModel model)
        {
            var vacation = VacationRepository.FirstOrDefault(x => x.Id == model.Id);

            if (vacation == null)
            {
                return(BadRequest("ERROR: Puhkus puudub andmebaasis"));
            }

            vacation.Status = VacationStatus.Approved;
            VacationRepository.SaveOrUpdate(vacation);
            VacationRepository.Commit();

            var emails = EmployeeRepository
                         .Where(x => x.VacationMessages == true && x.Id == vacation.EmployeeId)
                         .Select(x => x.Email)
                         .ToList();

            EmailService.VacationApproved(vacation, emails);

            return(Ok());
        }
예제 #3
0
        public IHttpActionResult ModifyVacation(VacationModel model)
        {
            if (model.End < model.Start)
            {
                return(BadRequest());
            }

            var vacation = VacationRepository.First(x => x.Id == model.Id);
            var employee = vacation.Employee;
            var tempDays = vacation.Days;

            vacation.StartDate = model.Start;
            vacation.EndDate   = model.End;
            vacation.Days      = (int)model.End.Subtract(model.Start).TotalDays + 1;
            tempDays          -= vacation.Days;

            if (employee.VacationDays - tempDays < 0)
            {
                return(BadRequest());
            }

            employee.VacationDays += tempDays;

            EmployeeRepository.SaveOrUpdate(employee);
            EmployeeRepository.Commit();

            VacationRepository.SaveOrUpdate(vacation);
            VacationRepository.Commit();


            var emails = EmployeeRepository.Where(x => x.VacationMessages == true && x.Id == vacation.EmployeeId).Select(x => x.Email).ToList();

            EmailService.VacationModified(vacation, emails);

            return(Ok());
        }
예제 #4
0
        public IHttpActionResult SetVacation(VacationModel model)
        {
            var id = CurrentUserId;
            var currentUserVacations = VacationRepository.Where(x => x.EmployeeId == id && x.StartDate.Year == DateTime.Now.Year && x.Status == VacationStatus.Declined && x.Status != VacationStatus.Retired)
                                       .Select(x => x.Days)
                                       .ToList();

            int days = (int)model.End.Subtract(model.Start).TotalDays + 1;

            if (model.Start > model.End)
            {
                return(BadRequest("ERROR: Puhkuse lõpp on enne puhkuse algust"));
            }

            var employee = EmployeeRepository.First(x => x.Id == id);

            if (employee.VacationDays - days < 0)
            {
                return(BadRequest("ERROR: Pole piisavalt kasutamata puhkusepäevi"));
            }

            if (model.Comment?.Length > 250)
            {
                return(BadRequest("ERROR: Lisainfo väljal ei tohi olla rohkem kui 250 tähemärki"));
            }


            int[]   vacationLimits         = new int[] { 1, 2, 3, 4, 5, 6, 7, 14 };
            int[][] specificVacationLimits = new int[][] { new int[] { 14, 1 }, new int[] { 7, 1 } };


            if (vacationLimits.Contains(days))
            {
                foreach (var limit in specificVacationLimits)
                {
                    if (currentUserVacations.Where(x => x == limit[0]).Count() >= limit[1] && days == limit[0])
                    {
                        return(BadRequest($"ERROR: Aastas on lubatud ainult {limit[1]} puhkus pikkusega {limit[0]} päeva."));
                    }
                }

                employee.VacationDays -= days;
                EmployeeRepository.SaveOrUpdate(employee);
                EmployeeRepository.Commit();

                var vacation = new Vacation()
                {
                    StartDate  = model.Start,
                    EndDate    = model.End,
                    Status     = VacationStatus.Pending,
                    EmployeeId = id,
                    Days       = days,
                    Comments   = model.Comment
                };

                VacationRepository.AddIfNew(vacation);
                VacationRepository.Commit();

                var emails = EmployeeRepository.Where(x => x.VacationMessages == true && x.Role.Rights.HasFlag(AccessRights.Vacations)).Select(x => x.Email).ToList();
                EmailService.VacationRequested(vacation, emails);

                return(Ok());
            }
            return(BadRequest("ERROR: Valitud puhkuse pikkus ei vasta eeskirjadele, lubatud on üks 14 päevane puhkus, üks 7 päevane puhkus ja ülejäänud puhkused on lühemad kui 7 päeva"));
        }