public ActionResult Index(Models.Page.Pager pagedModel)
        {
            ViewBag.Search = Request.QueryString["Search"] ?? "".ToString();

            var allActiveInventories = Services.InventoryService.GetAllActiveInventories(ViewBag.search, pagedModel);
            var inventoryCount       = Services.InventoryService.AllActiveInventoriesCount;

            pagedModel.PageCount = Math.Ceiling(inventoryCount / (double)pagedModel.ItemsPerPage);

            if (pagedModel.Button == "next")
            {
                if (Models.Page.PageNumber.Instance.InventoryCurrentPage <= (pagedModel.PageCount / pagedModel.PageRange))
                {
                    Models.Page.PageNumber.Instance.InventoryCurrentPage += 1;
                }
            }
            else if (pagedModel.Button == "prev")
            {
                if (Models.Page.PageNumber.Instance.InventoryCurrentPage > 1)
                {
                    Models.Page.PageNumber.Instance.InventoryCurrentPage -= 1;
                }
            }

            pagedModel.EndPage = Models.Page.PageNumber.Instance.InventoryCurrentPage * pagedModel.PageRange;

            if (pagedModel.PageCount < pagedModel.EndPage && pagedModel.PageCount != 1)
            {
                pagedModel.EndPage = (int)pagedModel.PageCount;
            }
            if (pagedModel.PageCount < pagedModel.PageRange)
            {
                pagedModel.EndPage = (int)pagedModel.PageCount;
            }

            pagedModel.StartPage = (pagedModel.EndPage - pagedModel.PageRange) + 1;

            if (pagedModel.StartPage <= 0)
            {
                pagedModel.StartPage = 1;
            }

            ViewBag.Title = "Products";
            ViewBag.AllActiveInventories = allActiveInventories;

            return(View(pagedModel));
        }
        public static List <Models.Inventory> GetAllActiveInventories(string search, Models.Page.Pager pagedModel)
        {
            var result = new List <Models.Inventory>();

            using (var ctx = new Data.AMEntDataContext())
            {
                var data = ctx.Inventories
                           .Where(x => x.Status == 1 &&
                                  x.InventoryWarehouses.Sum(y => y.OnhandQty) > 0);

                _AllActiveInventoriesCount = data.Count();

                if (string.IsNullOrWhiteSpace(search))
                {
                    result = data
                             .Select(x => new Models.Inventory
                    {
                        Id          = x.ID,
                        ItemCode    = x.ItemCode,
                        Description = x.Description,
                        ListPrice   = x.InventoryPrices != null ?
                                      x.InventoryPrices.FirstOrDefault() != null ?
                                      x.InventoryPrices.FirstOrDefault().InventoryListPrices != null && x.InventoryPrices.FirstOrDefault().InventoryListPrices.Count() > 0 ?
                                      x.InventoryPrices.FirstOrDefault().InventoryListPrices.Max(a => a.ListPrice) : 0
                                    : 0
                                : 0,
                        ImagePath = Resource.StaticResource.ImagePath
                    })
                             .Skip(pagedModel.Start)
                             .Take(pagedModel.ItemsPerPage)
                             .ToList();
                }
                else
                {
                    var newData = data
                                  .Where(x => x.ItemCode.ToLower().Contains(search.ToLower()));

                    _AllActiveInventoriesCount = newData.Count();

                    result = newData
                             .Select(x => new Models.Inventory
                    {
                        Id          = x.ID,
                        ItemCode    = x.ItemCode,
                        Description = x.Description,
                        ListPrice   = x.InventoryPrices != null ?
                                      x.InventoryPrices.FirstOrDefault() != null ?
                                      x.InventoryPrices.FirstOrDefault().InventoryListPrices != null && x.InventoryPrices.FirstOrDefault().InventoryListPrices.Count() > 0 ?
                                      x.InventoryPrices.FirstOrDefault().InventoryListPrices.Max(a => a.ListPrice) : 0
                                    : 0
                                : 0,
                        ImagePath = Resource.StaticResource.ImagePath
                    })
                             .Skip(pagedModel.Start)
                             .Take(pagedModel.ItemsPerPage)
                             .ToList();
                }
            }

            return(result);
        }