private void FilterTables(TableAvailNewVM model, TableAvailabilityFilter filters)
        {
            if (filters.selectedTables.Count > 0)
            {
                model.Tables.RemoveAll(t => !filters.selectedTables.Contains(t.FloorTable.FloorTableId));
            }

            if (filters.selectedTableClass.Count > 0)
            {
                model.Tables.RemoveAll(t => !filters.selectedTableClass.Contains((t.FloorTable.IsTemporary.HasValue ? ((t.FloorTable.IsTemporary.Value == true) ? 2 : 1) : 2)));
            }

            foreach (var table in model.Tables)
            {
                if (!string.IsNullOrEmpty(filters.StartTime) && !string.IsNullOrEmpty(filters.EndTime))
                {
                    var date = table.AvailStatus.First().time.Date;

                    var startTime = date.AddTicks(Convert.ToDateTime(filters.StartTime).TimeOfDay.Ticks);
                    var endTime = date.AddTicks(Convert.ToDateTime(filters.EndTime).TimeOfDay.Ticks);

                    startTime = startTime.TimeOfDay.TotalMinutes < 240 ? startTime.AddDays(1) : startTime;
                    endTime = endTime.TimeOfDay.TotalMinutes < 240 ? endTime.AddDays(1) : endTime;

                    foreach (var AStatus in table.AvailStatus)
                    {
                        bool isRejected = false;

                        if (AStatus.time < startTime || AStatus.time > endTime)
                        {
                            isRejected = true;
                        }

                        if (isRejected)
                        {
                            AStatus.Reservation = null;
                            AStatus.Status = 0;
                            AStatus.shiftId = 0;
                        }
                    }
                }

                foreach (var AStatus in table.AvailStatus)
                {
                    bool isRejected = false;

                    if (filters.selectedAvailability.Count > 0)
                    {
                        isRejected = (filters.selectedAvailability.Contains(AStatus.Status) || AStatus.Status == 0) ? false : true;
                    }

                    if (isRejected)
                    {
                        AStatus.Reservation = null;
                        AStatus.Status = 0;
                        AStatus.shiftId = 0;
                    }
                }
            }
        }
        public ActionResult getTableAvailablityNew(TableAvailabilityFilter filters, string date, Int64? shiftId)
        {
            var obj = new TimeAvailablityVM();
            var dat = Convert.ToDateTime(date, CultureInfo.InvariantCulture);

            var startTime = dat.AddTicks(DateTime.Parse("4:00:00").TimeOfDay.Ticks);
            var endTime = startTime.AddHours(23).AddMinutes(45);

            var array = new string[] { "Sofa", "Chair", "SofaTable", "Wall", "SolidWall", "GlassWall", "BarTable", "Fence", "Pillar" };
            var tableDB = db.tabFloorTables.Include("Reservations").Where(p => p.IsDeleted == false).ToList().Where(p => !array.Contains(p.TableName.Split('-')[0])).ToList();

            var day = dat.DayOfWeek.ToString();
            var menuShifts = db.tabMenuShiftHours.Where(s => s.WeekDays.DayName.Equals(day, StringComparison.CurrentCultureIgnoreCase)).ToList();

            var dId = db.GetWeekDays().Single(p => p.DayName.Contains(day)).DayId;
            var availList = db.tabTableAvailabilities
                .Include("TableAvailabilityFloorTables")
                .Include("TableAvailabilityWeekDays")
                .Where(ta => ta.StartDate <= dat && dat <= ta.EndDate
                && ta.TableAvailabilityWeekDays.Any(taw => taw.DayId == dId)).ToList();

            var model = new TableAvailNewVM();
            model.Tables = new List<Table>();

            var cday = db.tabWeekDays.Where(p => p.DayName == dat.DayOfWeek.ToString());
            var nday = db.tabWeekDays.Where(p => p.DayName == dat.AddDays(1).DayOfWeek.ToString());

            var shiftStime = new DateTime();
            var shiftEtime = new DateTime();

            if (shiftId.HasValue)
            {
                var dayId = db.tabWeekDays.AsEnumerable().Single(p => p.DayName == dat.DayOfWeek.ToString()).DayId;

                var dday = db.tabMenuShiftHours.Where(p => p.FoodMenuShiftId == shiftId && p.DayId == dayId).SingleOrDefault();

                var open = dday.OpenAt;
                var close = dday.CloseAt;

                shiftStime = dat.AddTicks(DateTime.Parse(open).TimeOfDay.Ticks);

                if (shiftId == 4 && close.Contains("AM"))
                {
                    shiftEtime = dat.AddDays(1).AddTicks(DateTime.Parse(close).TimeOfDay.Ticks);
                }
                else
                {
                    shiftEtime = dat.AddTicks(DateTime.Parse(close).TimeOfDay.Ticks);
                }
            }

            foreach (var tbl in tableDB)
            {
                var table = new Table();
                table.FloorTable = tbl;
                table.AvailStatus = new List<AvailStatus>();

                var time = startTime;

                while (time <= endTime)
                {
                    var AStatus = new AvailStatus();
                    AStatus.time = time;
                    if (shiftId.HasValue == true)
                    {
                        if (shiftStime <= time && shiftEtime > time)
                        {
                            bool isStartRes = false;
                            AStatus.Reservation = this.CheckReservationStatus(tbl, time, endTime, out isStartRes);
                            AStatus.Status = this.CheckAvailStatus(time, AStatus.Reservation, availList, tbl);
                            AStatus.shiftId = this.CheckShift(time, menuShifts);
                            AStatus.IsResStart = isStartRes;
                        }
                        else
                        {
                            AStatus.Reservation = null;
                            AStatus.Status = 0;
                            AStatus.shiftId = 0;
                        }
                    }
                    else
                    {
                        bool isStartRes = false;
                        AStatus.Reservation = this.CheckReservationStatus(tbl, time, endTime, out isStartRes);
                        AStatus.Status = this.CheckAvailStatus(time, AStatus.Reservation, availList, tbl);
                        AStatus.shiftId = this.CheckShift(time, menuShifts);
                        AStatus.IsResStart = isStartRes;
                    }

                    table.AvailStatus.Add(AStatus);

                    time = time.AddMinutes(15);
                }
                model.Tables.Add(table);
            }

            model.Tables = model.Tables
                .OrderBy(t => t.FloorTable.FloorPlan.FLevel)
                .ThenBy(t => t.FloorTable.SectionId)
                .ThenBy(t => t.FloorTable.TableName, new AlphaNumericComparer())
                .ToList();

            this.FilterTables(model, filters);

            return PartialView(model);
        }