public PartialViewResult GetReservtionListPartial(GetReservationsParamVM model)
        {
            ViewBag.ResParam = model;

            return PartialView("~/Views/FloorPlan/FloorRightReservationTabsPartial.cshtml");
        }
        public JsonResult GetJSONAllReservationList(GetReservationsParamVM model)
        {
            var floorPlan = model.FloorPlanId.HasValue ? model.FloorPlanId.Value.ToString() : string.Empty;
            var shift = model.ShiftId.HasValue ? model.ShiftId.Value.ToString() : string.Empty;

            string key = string.Format(CacheKeys.FILTERED_RESERVATION,
                User.Identity.GetDatabaseName(),
                model.Date.Ticks,
                model.Time,
                model.Filter,
                floorPlan,
                shift);

            var ResListVM = cache.Get<CachedReservationItemListVM>(key, () =>
            {
                var allRes = db.GetReservationByDate(model.Date)
                        .Where(r => (r.FloorTableId > 0) ? (!r.FloorTable.IsDeleted) : (!r.MergedFloorTable.OrigionalTables.Any(t => t.FloorTable.IsDeleted))).ToList();

                allRes = allRes.Where(r => r.StatusId.Value != ReservationStatus.Cancelled).ToList();
                var covers = allRes.Sum(r => r.Covers);

                IList<ReservationListItemVM> ResList = GetFilteredReservations20150617(model);

                var currentList = new CachedReservationItemListVM
                {
                    ReservationListItems = ResList,
                    Covers = allRes.Sum(r => r.Covers)
                };

                return currentList;
            });

            //ViewBag.Covers = ResListVM.Covers;
            var liTemplate = @"<li id='resL{0}' class='{1}cover popUp' data-cover='{1}' data-time='{2}'>{3}</li>";

            var HTMLArray = new List<string>();
            HTMLArray.Add("<ul class='reslist'>");
            HTMLArray.AddRange(ResListVM.ReservationListItems
                .Select(rvm => string.Format(liTemplate,
                    rvm.Reservation.ReservationId,
                    rvm.Reservation.Covers,
                    rvm.Reservation.TimeForm.TimeOfDay.TotalMinutes,
                    rvm.HTMLString)));
            HTMLArray.Add("</ul>");

            return Json(new
            {
                HTMLArray = HTMLArray,
                Covers = ResListVM.Covers
            }, JsonRequestBehavior.AllowGet);
        }
        public PartialViewResult GetAllReservationList(GetReservationsParamVM model)
        {
            string key = string.Format(CacheKeys.FILTERED_RESERVATION,
                User.Identity.GetDatabaseName(),
                model.Date.Ticks,
                model.Time,
                model.Filter,
                model.FloorPlanId.HasValue ? model.FloorPlanId.Value.ToString() : string.Empty,
                model.ShiftId.HasValue ? model.ShiftId.Value.ToString() : string.Empty);

            return cache.Get<PartialViewResult>(key, 60, () =>
            {
                IList<ReservationListItemVM> ResList = GetFilteredReservations20150617(model);

                var allRes = db.GetReservationByDate(model.Date)
                        .Where(r => (r.FloorTableId > 0) ? (!r.FloorTable.IsDeleted) : (!r.MergedFloorTable.OrigionalTables.Any(t => t.FloorTable.IsDeleted))).ToList();

                allRes = allRes.Where(r => r.StatusId.Value != ReservationStatus.Cancelled).ToList();

                ViewBag.Covers = allRes.Sum(r => r.Covers);

                return PartialView("~/Views/FloorPlan/ReservationListPartial.cshtml", ResList);
            });
        }
        public PartialViewResult GetAllReservationList20150617(GetReservationsParamVM model)
        {
            var floorPlan = model.FloorPlanId.HasValue ? model.FloorPlanId.Value.ToString() : string.Empty;
            var shift = model.ShiftId.HasValue ? model.ShiftId.Value.ToString() : string.Empty;

            string key = string.Format(CacheKeys.FILTERED_RESERVATION,
                User.Identity.GetDatabaseName(),
                model.Date.Ticks,
                model.Time,
                model.Filter,
                floorPlan,
                shift);

            var ResListVM = cache.Get<CachedReservationItemListVM>(key, () =>
            {
                var allRes = db.GetReservationByDate(model.Date)
                        .Where(r => (r.FloorTableId > 0) ? (!r.FloorTable.IsDeleted) : (!r.MergedFloorTable.OrigionalTables.Any(t => t.FloorTable.IsDeleted))).ToList();

                allRes = allRes.Where(r => r.StatusId.Value != ReservationStatus.Cancelled).ToList();
                var covers = allRes.Sum(r => r.Covers);

                IList<ReservationListItemVM> ResList = GetFilteredReservations20150617(model);

                var currentList = new CachedReservationItemListVM
                {
                    ReservationListItems = ResList,
                    Covers = allRes.Sum(r => r.Covers)
                };

                //var day = model.Date.DayOfWeek.ToString().Trim();
                //var dId = db.GetWeekDays().Single(p => p.DayName.Contains(day)).DayId;

                //var openTime = new DateTime();
                //var closeTime = new DateTime();

                //var ttime = db.GetMenuShiftHours().Where(p => p.DayId == dId).AsEnumerable();
                //var minOpenAt = ttime.Where(p => p.OpenAt != null).Min(p => Convert.ToDateTime(p.OpenAt));
                //var maxCloseAt = ttime.Where(p => p.CloseAt != null).Max(p => Convert.ToDateTime(p.CloseAt).AddDays(Convert.ToInt32(p.IsNext)));

                //openTime = Convert.ToDateTime(minOpenAt);
                //closeTime = Convert.ToDateTime(maxCloseAt);

                //while (openTime < closeTime)
                //{
                //    var startTime = openTime;
                //    openTime = openTime.AddMinutes(15);

                //    string otherKey = string.Format(CacheKeys.FILTERED_RESERVATION,
                //    model.Date.Ticks,
                //    startTime.ToString("HH:mm"),
                //    model.Filter,
                //    floorPlan,
                //    shift);

                //    var other = cache.Get<CachedReservationItemListVM>(otherKey, () =>
                //          {
                //              IList<ReservationListItemVM> otherResList = GetFilteredReservations20150617(model);

                //              var otherList = new CachedReservationItemListVM
                //              {
                //                  ReservationListItems = ResList,
                //                  Covers = covers
                //              };

                //              return otherList;
                //          });
                //}

                return currentList;
            });

            ViewBag.Covers = ResListVM.Covers;
            return PartialView("~/Views/FloorPlan/ReservationListPartial.cshtml", ResListVM.ReservationListItems);
        }
        //private IList<Reservation> GetFilteredReservations(GetReservationsParamVM options)
        //{
        //    IList<Reservation> reservations = new List<Reservation>();
        //    var clientDate = DateTime.UtcNow.ToClientTime().Date;
        //    using (var dbContext = new UsersContext())
        //    {
        //        reservations = dbContext.GetReservationByDate(options.Date)
        //            .Where(r => (r.FloorTableId > 0) ? (!r.FloorTable.IsDeleted) : (!r.MergedFloorTable.OrigionalTables.Any(t => t.FloorTable.IsDeleted))).ToList();
        //        if (options.FloorPlanId.HasValue)
        //        {
        //            reservations = reservations.Where(r => r.FloorPlanId == options.FloorPlanId.Value).ToList();
        //        }
        //        if (options.ShiftId.HasValue)
        //        {
        //            reservations = reservations.Where(r => r.FoodMenuShiftId == options.ShiftId.Value).ToList();
        //        }
        //        DateTime? startTime = null;
        //        string filter = string.Empty;
        //        if (!string.IsNullOrEmpty(options.Filter) && !string.IsNullOrEmpty(options.Time))
        //        {
        //            filter = options.Filter.Trim().ToUpper();
        //            switch (filter)
        //            {
        //                case "ALL":
        //                    {
        //                        break;
        //                    }
        //                case "SEATED":
        //                    {
        //                        options.IncludeStatusIds.Add(ReservationStatus.Seated);
        //                        options.ExcludeStatusIds.AddRange(
        //                            new List<long>
        //                        {
        //                            ReservationStatus.Cancelled,
        //                            ReservationStatus.Finished
        //                        });
        //                        options.Time = options.Time.Trim();
        //                        startTime = clientDate.Add(Convert.ToDateTime(options.Time).TimeOfDay);
        //                        break;
        //                    }
        //                case "UPCOMING":
        //                    {
        //                        options.ExcludeStatusIds.AddRange(
        //                            new List<long>
        //                        {
        //                            ReservationStatus.Seated,
        //                            ReservationStatus.Cancelled,
        //                            ReservationStatus.Finished
        //                        });
        //                        options.Time = options.Time.Trim();
        //                        startTime = clientDate.Add(Convert.ToDateTime(options.Time).TimeOfDay).AddMinutes(15);
        //                        break;
        //                    }
        //                default:
        //                    break;
        //            }
        //        }
        //        //var reservations = resList.ToList();
        //        if (options.IncludeStatusIds.Count > 0 && startTime.HasValue)
        //        {
        //            reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value) || ((filter == "SEATED") ? (r.TimeForm <= startTime) : (r.TimeForm >= startTime))).ToList();
        //        }
        //        else if (options.IncludeStatusIds.Count > 0)
        //        {
        //            reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value)).ToList();
        //        }
        //        else if (startTime.HasValue)
        //        {
        //            reservations = reservations.Where(r => ((filter == "SEATED") ? (r.TimeForm <= startTime) : (r.TimeForm >= startTime))).ToList();
        //        }
        //        if (options.ExcludeStatusIds.Count > 0)
        //        {
        //            reservations = reservations.Where(r => !options.ExcludeStatusIds.Contains(r.StatusId.Value)).ToList();
        //        }
        //    }
        //    return reservations.OrderBy(r => r.TimeForm).ToList();
        //}
        //private IList<ReservationListItemVM> GetFilteredReservations20150608(GetReservationsParamVM options)
        //{
        //    IList<Reservation> reservations = new List<Reservation>();
        //    var clientDate = DateTime.UtcNow.ToClientTime().Date;
        //    using (var dbContext = new UsersContext())
        //    {
        //        reservations = dbContext.GetReservationByDate(options.Date)
        //            .Where(r => (r.FloorTableId > 0) ? (!r.FloorTable.IsDeleted) : (!r.MergedFloorTable.OrigionalTables.Any(t => t.FloorTable.IsDeleted))).ToList();
        //        if (options.FloorPlanId.HasValue)
        //        {
        //            reservations = reservations.Where(r => r.FloorPlanId == options.FloorPlanId.Value).ToList();
        //        }
        //        if (options.ShiftId.HasValue)
        //        {
        //            reservations = reservations.Where(r => r.FoodMenuShiftId == options.ShiftId.Value).ToList();
        //        }
        //        DateTime? startTime = null;
        //        string filter = string.Empty;
        //        if (!string.IsNullOrEmpty(options.Filter) && !string.IsNullOrEmpty(options.Time))
        //        {
        //            filter = options.Filter.Trim().ToUpper();
        //            switch (filter)
        //            {
        //                case "ALL":
        //                    {
        //                        break;
        //                    }
        //                case "SEATED":
        //                    {
        //                        options.IncludeStatusIds.Add(ReservationStatus.Seated);
        //                        options.ExcludeStatusIds.AddRange(
        //                            new List<long>
        //                        {
        //                            ReservationStatus.Cancelled,
        //                            ReservationStatus.Finished
        //                        });
        //                        options.Time = options.Time.Trim();
        //                        startTime = clientDate.Add(Convert.ToDateTime(options.Time).TimeOfDay);
        //                        break;
        //                    }
        //                case "UPCOMING":
        //                    {
        //                        options.ExcludeStatusIds.AddRange(
        //                            new List<long>
        //                        {
        //                            ReservationStatus.Seated,
        //                            ReservationStatus.Cancelled,
        //                            ReservationStatus.Finished
        //                        });
        //                        options.Time = options.Time.Trim();
        //                        startTime = clientDate.Add(Convert.ToDateTime(options.Time).TimeOfDay).AddMinutes(15);
        //                        break;
        //                    }
        //                default:
        //                    break;
        //            }
        //        }
        //        //var reservations = resList.ToList();
        //        if (options.IncludeStatusIds.Count > 0 && startTime.HasValue)
        //        {
        //            reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value) || ((filter == "SEATED") ? (r.TimeForm <= startTime) : (r.TimeForm >= startTime))).ToList();
        //        }
        //        else if (options.IncludeStatusIds.Count > 0)
        //        {
        //            reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value)).ToList();
        //        }
        //        else if (startTime.HasValue)
        //        {
        //            reservations = reservations.Where(r => ((filter == "SEATED") ? (r.TimeForm <= startTime) : (r.TimeForm >= startTime))).ToList();
        //        }
        //        if (options.ExcludeStatusIds.Count > 0)
        //        {
        //            reservations = reservations.Where(r => !options.ExcludeStatusIds.Contains(r.StatusId.Value)).ToList();
        //        }
        //    }
        //    return reservations.OrderBy(r => r.TimeForm).Select(r => new ReservationListItemVM
        //    {
        //        Reservation = r,
        //        HTMLString = this.RenderPartialViewToString("~/Views/FloorPlan/ReservationListItemPartial.cshtml", r)
        //    }).ToList();
        //}
        private IList<ReservationListItemVM> GetFilteredReservations20150617(GetReservationsParamVM options)
        {
            IList<Reservation> reservations = new List<Reservation>();
            //var clientDate = DateTime.UtcNow.ToClientTime().Date;
            var clientDate = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName()).Date;

            using (var dbContext = new UsersContext())
            {
                reservations = dbContext.GetReservationByDate(options.Date)
                    .Where(r => (r.FloorTableId > 0) ? (!r.FloorTable.IsDeleted) : (!r.MergedFloorTable.OrigionalTables.Any(t => t.FloorTable.IsDeleted))).ToList();

                if (options.FloorPlanId.HasValue)
                {
                    reservations = reservations.Where(r => r.FloorPlanId == options.FloorPlanId.Value).ToList();
                }

                if (options.ShiftId.HasValue)
                {
                    reservations = reservations.Where(r => r.FoodMenuShiftId == options.ShiftId.Value).ToList();
                }

                DateTime? startTime = null;

                string filter = string.Empty;

                if (!string.IsNullOrEmpty(options.Filter) && !string.IsNullOrEmpty(options.Time))
                {
                    filter = options.Filter.Trim().ToUpper();

                    if (!string.IsNullOrWhiteSpace(options.Time))
                    {
                        options.Time = options.Time.Trim();
                        clientDate = clientDate.Add(Convert.ToDateTime(options.Time).TimeOfDay);
                    }

                    switch (filter)
                    {
                        case "ALL":
                            {
                                break;
                            }
                        case "SEATED":
                            {
                                options.IncludeStatusIds.AddRange(new List<long>{
                                    ReservationStatus.Partially_Arrived,
                                    ReservationStatus.All_Arrived,
                                    ReservationStatus.Partially_Seated,
                                    ReservationStatus.Seated,
                                    ReservationStatus.Appetizer,
                                    ReservationStatus.Entree,
                                    ReservationStatus.Dessert,
                                    ReservationStatus.Table_Cleared,
                                    ReservationStatus.Check_Dropped,
                                    ReservationStatus.Check_Paid
                                });
                                options.ExcludeStatusIds.AddRange(new List<long>
                                {
                                    ReservationStatus.Finished,
                                    ReservationStatus.Cancelled,
                                    ReservationStatus.No_show
                                });
                                //options.Time = options.Time.Trim();
                                startTime = clientDate;
                                break;
                            }
                        case "UPCOMING":
                            {
                                options.ExcludeStatusIds.AddRange(new List<long>
                                {
                                    ReservationStatus.Partially_Arrived,
                                    ReservationStatus.All_Arrived,
                                    ReservationStatus.Partially_Seated,
                                    ReservationStatus.Seated,
                                    ReservationStatus.Appetizer,
                                    ReservationStatus.Entree,
                                    ReservationStatus.Dessert,
                                    ReservationStatus.Table_Cleared,
                                    ReservationStatus.Check_Dropped,
                                    ReservationStatus.Check_Paid,
                                    ReservationStatus.Seated,
                                    ReservationStatus.Cancelled,
                                    ReservationStatus.Finished,
                                    ReservationStatus.No_show
                                });
                                //options.Time = options.Time.Trim();
                                startTime = clientDate.AddMinutes(15);
                                break;
                            }
                        default:
                            break;
                    }
                }

                //var reservations = resList.ToList();

                if (options.IncludeStatusIds.Count > 0 && startTime.HasValue)
                {
                    //reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value) || ((filter == "SEATED") ? (r.TimeForm <= startTime) : (r.TimeForm >= startTime))).ToList();
                    reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value)
                        || ((filter == "SEATED") ? (r.TimeForm <= startTime && startTime <= r.TimeForm.AddMinutes(r.Duration.GetMinutesFromDuration())) : (r.TimeForm >= startTime))).ToList();
                }
                else if (options.IncludeStatusIds.Count > 0)
                {
                    reservations = reservations.Where(r => options.IncludeStatusIds.Contains(r.StatusId.Value)).ToList();
                }
                else if (startTime.HasValue)
                {
                    reservations = reservations.Where(r => ((filter == "SEATED") ? (r.TimeForm <= startTime) : (r.TimeForm >= startTime))).ToList();
                }

                if (options.ExcludeStatusIds.Count > 0)
                {
                    reservations = reservations.Where(r => !options.ExcludeStatusIds.Contains(r.StatusId.Value)).ToList();
                }
            }

            reservations = reservations.OrderBy(r => r.TimeForm).ToList();

            //if (options.Filter == "ALL")
            //{
            //    var currentTime = !string.IsNullOrWhiteSpace(options.Time) ? options.Date.Add(Convert.ToDateTime(options.Time).TimeOfDay) : clientDate;
            //    var sortedList = new List<Reservation>();
            //    sortedList.AddRange(reservations.SkipWhile(r => r.TimeForm < currentTime));
            //    sortedList.AddRange(reservations.TakeWhile(r => r.TimeForm < currentTime));

            //    return sortedList.Select(r => new ReservationListItemVM
            //    {
            //        Reservation = r,
            //        HTMLString = cache.Get<string>(string.Format(CacheKeys.RESERVATION_RIGHT_LIST_ITEM, r.ReservationId), () =>
            //        {
            //            return this.RenderPartialViewToString("~/Views/FloorPlan/ReservationListItemPartial.cshtml", r);
            //        })
            //    }).ToList();
            //}
            //else
            //{

            var htmlMinifier = new HtmlMinifier();

            return reservations.Select(r => new ReservationListItemVM
            {
                Reservation = r,
                HTMLString = cache.Get<string>(string.Format(CacheKeys.RESERVATION_RIGHT_LIST_ITEM,db.Database.Connection.Database, r.ReservationId), () =>
                {
                    ModelState.Clear();
                    return htmlMinifier.Minify(this.RenderPartialViewToString("~/Views/FloorPlan/ReservationListItemPartial.cshtml", r),
                        generateStatistics: false).MinifiedContent;

                    //return this.RenderPartialViewToString("~/Views/FloorPlan/ReservationListItemPartial.cshtml", r);
                })
            }).ToList();
            //}
        }