Beispiel #1
0
 public HttpStatusCode Post(VacationPolicy model)
 {
     if (model != null)
     {
         _vacationRepository.Add(model);
         return(HttpStatusCode.OK);
     }
     return(HttpStatusCode.BadRequest);
 }
        private CountOfVacationDTO CheckVacationPolicies(UserVacationRequest newrequest)
        {
            var             allvacations = _userVacationRequestRepository.FindByConditionWithUser(x => x.User.Id == newrequest.User.Id).Where(x => (x.StartDate.Year == 2019) && (x.VacationType.Name == newrequest.VacationType.Name)).ToList();
            var             allHolidays  = _companyHolidayRepository.FindByCondition(x => x.Date.Year == DateTime.Now.Year);
            List <DateTime> allDatesPrev = new List <DateTime>();

            if (allvacations.Count != 0)
            {
                //count prev days without sat and sun
                foreach (var vacation in allvacations)
                {
                    for (DateTime date = vacation.StartDate; date <= vacation.EndDate; date = date.AddDays(1))
                    {
                        if (date.DayOfWeek.ToString() != "Saturday" && date.DayOfWeek.ToString() != "Sunday")
                        {
                            allDatesPrev.Add(date);
                        }
                    }
                }
                //count prev days without company holidays
                allDatesPrev = GetListDaysWithoutCompanyHolidays(allDatesPrev);
                //foreach (var checkHoliday in allHolidays)
                //{
                //    var item = allDatesPrev.SingleOrDefault(x => x.DayOfYear == checkHoliday.Date.DayOfYear);
                //    if (item != null)
                //    {
                //        allDatesPrev.Remove(item);
                //    }
                //}
            }

            //count days of current request
            List <DateTime> allDatesForCurrentRequest = new List <DateTime>();

            for (DateTime date = newrequest.StartDate; date <= newrequest.EndDate; date = date.AddDays(1))
            {
                if (date.DayOfWeek.ToString() != "Saturday" && date.DayOfWeek.ToString() != "Sunday")
                {
                    allDatesForCurrentRequest.Add(date);
                }
            }
            //count days of current request without company holiday
            allDatesForCurrentRequest = GetListDaysWithoutCompanyHolidays(allDatesForCurrentRequest);
            //foreach (var checkHoliday in allHolidays)
            //{
            //    var item = allDatesForCurrentRequest.SingleOrDefault(x => x.DayOfYear == checkHoliday.Date.DayOfYear);
            //    if (item != null)
            //    {
            //        allDatesForCurrentRequest.Remove(item);
            //    }
            //}

            //get vacation policy for category
            List <VacationPolicy> currentVacationPolicy = _vacationPolicyRepository.FindCurrentVacationPolicy(newrequest);
            int commonCountOfday = currentVacationPolicy[0].Count + currentVacationPolicy[0].Count;

            //it check if is dates yet
            int countAllDatesForCurrentRequest = allDatesForCurrentRequest.Count;
            int countAllDatesPrev = allDatesPrev.Count;

            if (commonCountOfday >= countAllDatesForCurrentRequest + countAllDatesPrev)
            {
                VacationPolicy PolicyWithPay    = currentVacationPolicy.Find(x => x.Payments == x.Count);
                int            remainderPayDays = PolicyWithPay.Count - (countAllDatesForCurrentRequest + countAllDatesPrev);

                if (remainderPayDays >= 0)
                {
                    return(new CountOfVacationDTO {
                        Payments = countAllDatesForCurrentRequest, Free = 0
                    });
                }
                else if (countAllDatesPrev > PolicyWithPay.Count)
                {
                    return(new CountOfVacationDTO {
                        Payments = 0, Free = countAllDatesForCurrentRequest
                    });
                }
                else if (countAllDatesPrev < PolicyWithPay.Count)
                {
                    return(new CountOfVacationDTO {
                        Payments = allDatesForCurrentRequest.Count + remainderPayDays, Free = -remainderPayDays
                    });
                }
            }
            return(null);
        }