Exemplo n.º 1
0
 public int GetFreeSpace(DateTime date, int delegation)
 {
     try
     {
         var specialdays = _context.SpecialDays.Where(r => r.DelegationId == delegation && r.Date == date.Date);
         if (specialdays.Any())
         {
             var specialDay = specialdays.FirstOrDefault();
             if (specialDay.IsNonWorking)
             {
                 return(0);
             }
             var speSche =
                 _context.SpecialDaysSchedules.Where(r => r.DelegationId == delegation && r.Date == date.Date);
             return(speSche.Any() ? speSche.Sum(r => r.Capacity) : 0);
         }
         var weekday = ConverterData.GetWeekEnumDay(date.DayOfWeek);
         var query   = _context.Schedules.Where(r => r.DelegationId == delegation && r.WeekdayId == (int)weekday);
         return(query.Any() ? query.Sum(r => r.Capacity) : 0);
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
Exemplo n.º 2
0
        //public async Task<int> CancelAppointment(Guid appointmentId)
        //{
        //    try
        //    {
        //        int count = 0;
        //        Guid requestId = appointmentId;
        //        string userId = string.Empty;
        //        var value = Convert.ToInt32(_context.Configurations.FirstOrDefault(r => r.Name == "NumberOfDates").Value);
        //        var appoinments = _context.Appoinments.Where(r => r.AppoinmentId.Equals(appointmentId));
        //        foreach (var apoin in appoinments)
        //        {
        //            var allAppoinments = _context.Appoinments.Where(r => r.RequestId.Equals(apoin.RequestId)).ToList();
        //            foreach (var all in allAppoinments)
        //            {
        //                count = count + 1;
        //                requestId = all.RequestId;
        //            }
        //        }
        //        if (count == value)
        //        {
        //            /* CANCELAR Appoinments */
        //            var apps = _context.Appoinments.Find(appointmentId);
        //            var reqStas = new List<RequestStatu>();
        //            apps.IsAttended = false;
        //            apps.IsCancelled = true;
        //            //_context.Appoinments.AddOrUpdate(apps);

        //            /* CANCELAR RequestStatus (Cancelar los IsCurrentStatus = 1 y Agregar nueva fila) */
        //            var selectRequestStatus = _context.RequestStatus.Where(r => r.RequestId == requestId && r.IsCurrentStatus == true);
        //            foreach (var query in selectRequestStatus)
        //            {
        //                query.IsCurrentStatus = false;
        //                //userId = query.UserId;
        //            }
        //            var reqStatus = new RequestStatu
        //            {
        //                RequestId = requestId,
        //                StatusId = 410,
        //                Date = DateTime.Now,
        //                IsCurrentStatus = true,
        //                //UserId = userId,
        //                Observations = "",
        //                //ElapsedDays = 0,
        //                //ElapsedWorkDays = 0,
        //                //Data = null
        //            };

        //            Request req = new Request();
        //            req.RequestId = reqStatus.RequestId;
        //            req.IsComplete = false;
        //            reqStas.Add(reqStatus);
        //          //  _context.Requests.AddOrUpdate(req);
        //            _context.RequestStatus.AddOrUpdate(reqStatus);
        //        }
        //        else
        //        {
        //            var app = _context.Appoinments.Find(appointmentId);
        //            app.IsAttended = false;
        //            app.IsCancelled = true;
        //            _context.Appoinments.AddOrUpdate(app);
        //        }
        //        return await _context.SaveChangesAsync();
        //    }
        //    catch (Exception exception)
        //    {
        //        throw exception;
        //    }
        //}


        public async Task <CalendarApi> GetCalendarByMonthAndDelegation(int delegationId, DateTime montYear)


        {
            try
            {
                var calendar = new CalendarApi {
                    Days = new DayCalendar[6, 7]
                };
                var initDate = new DateTime(montYear.Year, montYear.Month, 1, 0, 0, 0);
                var init     = new DateTime(montYear.Year, montYear.Month, 1, 0, 0, 0);
                var day      = ConverterData.GetWeekEnumDay(init.DayOfWeek);
                var space    = 0;
                var occu     = 0;

                for (var i = 0; i < 6; i++)
                {
                    for (var j = 0; j < 7; j++)
                    {
                        if (i == 0)
                        {
                            if (j + 1 >= (int)day)
                            {
                                init  = j + 1 == (int)day ? init : init.AddDays(1);
                                space = GetFreeSpace(init, delegationId);
                                occu  = GetOccupiedSpace(init, delegationId);
                                calendar.Days[i, j] = FillAvalitiyAndRate(space, occu, init);
                            }
                            else
                            {
                                calendar.Days[i, j] = FillAvalitiyAndRate(space, occu, null);
                            }
                        }
                        else
                        {
                            if (init.AddDays(1).Month == initDate.Month)
                            {
                                init  = init.AddDays(1);
                                space = GetFreeSpace(init, delegationId);
                                occu  = GetOccupiedSpace(init, delegationId);
                                calendar.Days[i, j] = FillAvalitiyAndRate(space, occu, init);
                            }
                            else
                            {
                                calendar.Days[i, j] = FillAvalitiyAndRate(space, occu, null);
                            }
                        }
                    }
                }
                return(calendar);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
Exemplo n.º 3
0
        public async Task <List <ScheduleApi> > GetTimeCalendar(DateTime date, int delegation)
        {
            try
            {
                //if (date == null)
                //    return new List<ScheduleApi>();
                var dateN = date;
                var query = new List <ScheduleApi>();
                var res   = new List <ScheduleApi>();
                var apps  =
                    _context.Appoinments.Where(
                        r => r.Delegationid == delegation && !r.IsCancelled && r.Date == dateN.Date);
                var specialdays = _context.SpecialDays.Where(r => r.DelegationId == delegation && r.Date == dateN.Date);
                if (specialdays.Any())
                {
                    var specialDay = specialdays.FirstOrDefault();
                    if (specialDay.IsNonWorking)
                    {
                        return(new List <ScheduleApi>());
                    }
                    var speSche =
                        _context.SpecialDaysSchedules.Where(r => r.DelegationId == delegation && r.Date == dateN.Date);
                    query = (from s in speSche
                             select new ScheduleApi
                    {
                        Date = s.Date,
                        DelegationId = s.DelegationId,
                        Time = s.Time,
                        Capacity = s.Capacity
                    }).ToList();
                }
                else
                {
                    var weekday = ConverterData.GetWeekEnumDay(dateN.DayOfWeek);

                    int dia = (int)weekday;


                    // revisar por que no funciona la consulta por delegacion y dia: MFP 12-01-2017
                    query =
                        _context.Schedules
                        .Where(x => x.WeekdayId.Equals(dia))
                        .Select(s => new ScheduleApi
                    {
                        Date         = dateN.Date,
                        DelegationId = s.DelegationId,
                        Time         = s.Time,
                        Capacity     = s.Capacity
                    }).ToList();
                }


                foreach (var api in query)
                {
                    var count = apps.Count(r => r.Date == api.Date && r.Time == api.Time);
                    if (count < api.Capacity)
                    {
                        res.Add(api);
                    }
                }
                return(res);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }